Add Entity like to_dict and from_dict support to User, alongside to_json and from_json.

This commit is contained in:
Relintai 2022-07-24 01:42:55 +02:00
parent b479b6a1ab
commit 2db29ae49f
4 changed files with 143 additions and 4 deletions

View File

@ -1,5 +1,6 @@
#include "user.h"
#include "core/class_db.h"
#include "core/io/json.h"
#include "user_module.h"
@ -155,6 +156,94 @@ String User::_hash_password(const String &p_password) {
return p.sha256_text();
}
Dictionary User::to_dict() {
return call("_to_dict");
}
void User::from_dict(const Dictionary &dict) {
call("_from_dict", dict);
}
Dictionary User::_to_dict() {
Dictionary dict;
dict["user_id"] = _user_id;
dict["user_name"] = _user_name;
dict["email"] = _email;
dict["rank"] = _rank;
dict["pre_salt"] = _pre_salt;
dict["post_salt"] = _post_salt;
dict["password_hash"] = _password_hash;
dict["banned"] = _banned;
dict["password_reset_token"] = _password_reset_token;
dict["locked"] = _locked;
Array marr;
for (int i = 0; i < _modules.size(); ++i) {
Ref<UserModule> m = _modules[i];
if (m.is_valid()) {
Dictionary mdict;
mdict["index"] = i;
mdict["data"] = m->to_dict();
marr.push_back(mdict);
}
}
dict["modules"] = marr;
return dict;
}
void User::_from_dict(const Dictionary &dict) {
_user_id = dict["user_id "];
_user_name = dict["user_name "];
_email = dict["email "];
_rank = dict["rank "];
_pre_salt = dict["pre_salt "];
_post_salt = dict["post_salt "];
_password_hash = dict["password_hash "];
_banned = dict["banned "];
_password_reset_token = dict["password_reset_token "];
_locked = dict["locked "];
Array marr = dict["modules"];
for (int i = 0; i < marr.size(); ++i) {
Dictionary mdict = marr[i];
if (!mdict.has("index") || !mdict.has("data")) {
continue;
}
int index = mdict["index"];
Ref<UserModule> m = _modules[index];
ERR_CONTINUE(!m.is_valid());
m->from_dict(mdict["data"]);
}
}
String User::to_json() {
return JSON::print(to_dict());
}
void User::from_json(const String &data) {
Error err;
String err_txt;
int err_line;
Variant v;
err = JSON::parse(data, v, err_txt, err_line);
ERR_FAIL_COND(err != OK);
Dictionary d = v;
from_dict(d);
}
void User::save() {
emit_changed();
}
@ -252,6 +341,16 @@ void User::_bind_methods() {
ClassDB::bind_method(D_METHOD("_create_password", "password"), &User::_create_password);
ClassDB::bind_method(D_METHOD("_hash_password", "password"), &User::_hash_password);
BIND_VMETHOD(MethodInfo("_from_dict", PropertyInfo(Variant::DICTIONARY, "dict")));
BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::DICTIONARY, "dict"), "_to_dict"));
ClassDB::bind_method(D_METHOD("from_dict", "dict"), &User::from_dict);
ClassDB::bind_method(D_METHOD("to_dict"), &User::to_dict);
ClassDB::bind_method(D_METHOD("_from_dict", "dict"), &User::_from_dict);
ClassDB::bind_method(D_METHOD("_to_dict"), &User::_to_dict);
ClassDB::bind_method(D_METHOD("to_json"), &User::to_json);
ClassDB::bind_method(D_METHOD("from_json", "data"), &User::from_json);
ClassDB::bind_method(D_METHOD("save"), &User::save);
ClassDB::bind_method(D_METHOD("read_lock"), &User::read_lock);

View File

@ -69,6 +69,15 @@ public:
virtual void _create_password(const String &p_password);
virtual String _hash_password(const String &p_password);
Dictionary to_dict();
void from_dict(const Dictionary &dict);
virtual Dictionary _to_dict();
virtual void _from_dict(const Dictionary &dict);
String to_json();
void from_json(const String &data);
void save();
void read_lock();

View File

@ -5,10 +5,10 @@
#include "user.h"
int UserModule::get_module_index() const {
return _module_id;
return _module_index;
}
void UserModule::set_module_index(const int val) {
_module_id = val;
_module_index = val;
}
String UserModule::get_module_name() const {
@ -25,6 +25,24 @@ void UserModule::set_user(User *user) {
_user = user;
}
Dictionary UserModule::to_dict() {
return call("_to_dict");
}
void UserModule::from_dict(const Dictionary &dict) {
call("_from_dict", dict);
}
Dictionary UserModule::_to_dict() {
Dictionary dict;
dict["module_name"] = _module_name;
return dict;
}
void UserModule::_from_dict(const Dictionary &dict) {
_module_name = dict["module_name"];
}
void UserModule::read_lock() {
_rw_lock.read_lock();
}
@ -39,7 +57,7 @@ void UserModule::write_unlock() {
}
UserModule::UserModule() {
_module_id = -1;
_module_index = -1;
_user = nullptr;
}
@ -55,6 +73,13 @@ void UserModule::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_user"), &UserModule::get_user);
BIND_VMETHOD(MethodInfo("_from_dict", PropertyInfo(Variant::DICTIONARY, "dict")));
BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::DICTIONARY, "dict"), "_to_dict"));
ClassDB::bind_method(D_METHOD("from_dict", "dict"), &UserModule::from_dict);
ClassDB::bind_method(D_METHOD("to_dict"), &UserModule::to_dict);
ClassDB::bind_method(D_METHOD("_from_dict", "dict"), &UserModule::_from_dict);
ClassDB::bind_method(D_METHOD("_to_dict"), &UserModule::_to_dict);
ClassDB::bind_method(D_METHOD("read_lock"), &UserModule::read_lock);
ClassDB::bind_method(D_METHOD("read_unlock"), &UserModule::read_unlock);
ClassDB::bind_method(D_METHOD("write_lock"), &UserModule::write_lock);

View File

@ -21,6 +21,12 @@ public:
Ref<User> get_user();
void set_user(User *user);
Dictionary to_dict();
void from_dict(const Dictionary &dict);
virtual Dictionary _to_dict();
virtual void _from_dict(const Dictionary &dict);
void read_lock();
void read_unlock();
void write_lock();
@ -32,7 +38,7 @@ public:
protected:
static void _bind_methods();
int _module_id;
int _module_index;
String _module_name;
User *_user;