-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement API for setting user info and assigning user ID (#5)
* API to set user info * Update demo to show how to set user information * Generate unique ID for users, print useful user info * Store user info in a runtime config Runtime info is stored in the user data dir in "sentry.config" file (like user ID, email, etc.). A call to Sentry.set_user() also saves that data to the conf file, which is loaded on a subsequent launch. Demo project is updated to show that data. * Clean up * Generate device and user UUIDs via sentry-native * Pass arguments as const ref * Better whitespace when printing SentryUser object * Automatically assign user ID * Inherit user ID from runtime config if not set explicitly or generate a new one * Clean include * Fix runtime config issues * Add `SentryUser.generate_user_id()` for convenience * Improve demo project UI layout * Store user data in sentry.dat * Corrections
- Loading branch information
Showing
11 changed files
with
327 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include "runtime_config.h" | ||
#include "sentry_user.h" | ||
|
||
#include <godot_cpp/core/memory.hpp> | ||
|
||
namespace { | ||
|
||
inline String _ensure_string(const Variant &p_value, const String &p_fallback) { | ||
return p_value.get_type() == Variant::STRING ? (String)p_value : p_fallback; | ||
} | ||
|
||
inline CharString _ensure_cstring(const Variant &p_value, const CharString &p_fallback) { | ||
return p_value.get_type() == Variant::STRING ? ((String)p_value).utf8() : p_fallback; | ||
} | ||
|
||
} // unnamed namespace | ||
|
||
void RuntimeConfig::set_user(const Ref<SentryUser> &p_user) { | ||
ERR_FAIL_COND(p_user.is_null()); | ||
user = p_user; | ||
|
||
conf->set_value("user", "id", p_user->get_id()); | ||
conf->set_value("user", "email", p_user->get_email()); | ||
conf->set_value("user", "username", p_user->get_username()); | ||
conf->save(conf_path); | ||
} | ||
|
||
void RuntimeConfig::set_device_id(const CharString &p_device_id) { | ||
ERR_FAIL_COND(p_device_id.length() == 0); | ||
device_id = p_device_id; | ||
conf->set_value("device", "id", (String)device_id); | ||
conf->save(conf_path); | ||
} | ||
|
||
void RuntimeConfig::load_file(const String &p_conf_path) { | ||
ERR_FAIL_COND(p_conf_path.is_empty()); | ||
|
||
conf_path = p_conf_path; | ||
conf->load(conf_path); | ||
|
||
user = Ref(memnew(SentryUser)); | ||
user->set_id(_ensure_string(conf->get_value("user", "id", ""), "")); | ||
user->set_email(_ensure_string(conf->get_value("user", "email", ""), "")); | ||
user->set_username(_ensure_string(conf->get_value("user", "username", ""), "")); | ||
|
||
device_id = _ensure_cstring(conf->get_value("device", "id", ""), ""); | ||
} | ||
|
||
RuntimeConfig::RuntimeConfig() { | ||
conf.instantiate(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef RUNTIME_CONFIG_H | ||
#define RUNTIME_CONFIG_H | ||
|
||
#include "sentry_user.h" | ||
|
||
#include <godot_cpp/classes/config_file.hpp> | ||
#include <godot_cpp/variant/char_string.hpp> | ||
|
||
using namespace godot; | ||
|
||
class RuntimeConfig { | ||
private: | ||
String conf_path; | ||
Ref<ConfigFile> conf; | ||
|
||
// Cached values. | ||
Ref<SentryUser> user; | ||
CharString device_id; | ||
|
||
public: | ||
Ref<SentryUser> get_user() const { return user; } | ||
void set_user(const Ref<SentryUser> &p_user); | ||
|
||
CharString get_device_id() const { return device_id; } | ||
void set_device_id(const CharString &p_device_id); | ||
|
||
void load_file(const String &p_conf_path); | ||
|
||
RuntimeConfig(); | ||
}; | ||
|
||
#endif // RUNTIME_CONFIG_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "sentry_user.h" | ||
|
||
#include "sentry_util.h" | ||
|
||
#include <godot_cpp/core/error_macros.hpp> | ||
#include <godot_cpp/variant/packed_string_array.hpp> | ||
#include <godot_cpp/variant/variant.hpp> | ||
|
||
bool SentryUser::is_user_valid() const { | ||
return !id.is_empty() || !username.is_empty() || !email.is_empty() || !ip_address.is_empty(); | ||
} | ||
|
||
String SentryUser::_to_string() const { | ||
PackedStringArray parts; | ||
if (!id.is_empty()) { | ||
parts.append("id: " + id); | ||
} | ||
if (!username.is_empty()) { | ||
parts.append("name: " + username); | ||
} | ||
if (!email.is_empty()) { | ||
parts.append("email: " + email); | ||
} | ||
if (!ip_address.is_empty()) { | ||
parts.append("ip: " + ip_address); | ||
} | ||
return "SentryUser:{ " + String("; ").join(parts) + " }"; | ||
} | ||
|
||
void SentryUser::generate_new_id() { | ||
id = SentryUtil::generate_uuid(); | ||
} | ||
|
||
void SentryUser::_bind_methods() { | ||
// Setters / getters | ||
ClassDB::bind_method(D_METHOD("set_id", "id"), &SentryUser::set_id); | ||
ClassDB::bind_method(D_METHOD("get_id"), &SentryUser::get_id); | ||
ClassDB::bind_method(D_METHOD("set_username", "username"), &SentryUser::set_username); | ||
ClassDB::bind_method(D_METHOD("get_username"), &SentryUser::get_username); | ||
ClassDB::bind_method(D_METHOD("set_email", "email"), &SentryUser::set_email); | ||
ClassDB::bind_method(D_METHOD("get_email"), &SentryUser::get_email); | ||
ClassDB::bind_method(D_METHOD("set_ip_address", "ip_address"), &SentryUser::set_ip_address); | ||
ClassDB::bind_method(D_METHOD("get_ip_address"), &SentryUser::get_ip_address); | ||
|
||
ADD_PROPERTY(PropertyInfo(Variant::STRING, "id"), "set_id", "get_id"); | ||
ADD_PROPERTY(PropertyInfo(Variant::STRING, "username"), "set_username", "get_username"); | ||
ADD_PROPERTY(PropertyInfo(Variant::STRING, "email"), "set_email", "get_email"); | ||
ADD_PROPERTY(PropertyInfo(Variant::STRING, "ip_address"), "set_ip_address", "get_ip_address"); | ||
|
||
// Other methods | ||
ClassDB::bind_method(D_METHOD("infer_ip_address"), &SentryUser::infer_ip_address); | ||
ClassDB::bind_method(D_METHOD("is_user_valid"), &SentryUser::is_user_valid); | ||
ClassDB::bind_method(D_METHOD("generate_new_id"), &SentryUser::generate_new_id); | ||
} |
Oops, something went wrong.