Store the owner UserManager in User.

This commit is contained in:
Relintai 2025-05-11 18:37:59 +02:00
parent 5c752900dd
commit 066e88e336
4 changed files with 32 additions and 4 deletions

View File

@ -27,6 +27,9 @@
var um : SomeUserModule = SomeUserModule.new() var um : SomeUserModule = SomeUserModule.new()
um.module_name = "SomeUserModule" um.module_name = "SomeUserModule"
user.add_module(um) user.add_module(um)
# Only needed if the default implementation is not called
#user.set_owner_user_manager(self)
return ._create_user(user)[/code] return ._create_user(user)[/code]
</description> </description>

View File

@ -80,6 +80,8 @@ Ref<User> UserManager::_create_user(Ref<User> p_user) {
if (!p_user.is_valid()) { if (!p_user.is_valid()) {
p_user.instance(); p_user.instance();
} }
p_user->set_owner_user_manager(this);
return p_user; return p_user;
} }

View File

@ -30,9 +30,10 @@
/*************************************************************************/ /*************************************************************************/
#include "user.h" #include "user.h"
#include "../managers/user_manager.h"
#include "../singleton/user_db.h"
#include "core/io/json.h" #include "core/io/json.h"
#include "core/object/class_db.h" #include "core/object/class_db.h"
#include "../singleton/user_db.h"
#include "user_module.h" #include "user_module.h"
@ -199,7 +200,7 @@ Dictionary User::_to_dict() {
// RW Lock writes are not re-entrant (read locking would work), but since concurrency is hard // RW Lock writes are not re-entrant (read locking would work), but since concurrency is hard
// api consistency is important. These types of classes hould always be locked from the outside. // api consistency is important. These types of classes hould always be locked from the outside.
// Use read_lock() in the caller and then do all read operations that you want. // Use read_lock() in the caller and then do all read operations that you want.
Dictionary dict; Dictionary dict;
dict["user_id"] = _user_id; dict["user_id"] = _user_id;
@ -240,7 +241,7 @@ void User::_from_dict(const Dictionary &dict) {
// RW Locks are not re-entrant, so no write_lock() here. // RW Locks are not re-entrant, so no write_lock() here.
// Use write_lock() in the caller and do all write operations that you want if you need concurrency. // Use write_lock() in the caller and do all write operations that you want if you need concurrency.
// This is the only way to properly preserve atomicity. // This is the only way to properly preserve atomicity.
_user_id = dict["user_id"]; _user_id = dict["user_id"];
_user_name = dict["user_name"]; _user_name = dict["user_name"];
_email = dict["email"]; _email = dict["email"];
@ -276,7 +277,7 @@ void User::_from_dict(const Dictionary &dict) {
} }
ERR_CONTINUE(!m.is_valid()); ERR_CONTINUE(!m.is_valid());
// Call write lock from the outside // Call write lock from the outside
m->write_lock(); m->write_lock();
m->from_dict(mdict["data"]); m->from_dict(mdict["data"]);
@ -324,11 +325,22 @@ Error User::write_try_lock() {
return _rw_lock.write_try_lock(); return _rw_lock.write_try_lock();
} }
UserManager *User::get_owner_user_manager() {
return _owner_user_manager;
}
void User::set_owner_user_manager(UserManager *p_user_manager) {
_owner_user_manager = p_user_manager;
}
void User::set_owner_user_manager_bind(Node *p_user_manager) {
set_owner_user_manager(Object::cast_to<UserManager>(p_user_manager));
}
User::User() { User::User() {
_user_id = -1; _user_id = -1;
_rank = 0; _rank = 0;
_banned = false; _banned = false;
_locked = false; _locked = false;
_owner_user_manager = NULL;
} }
User::~User() { User::~User() {
@ -422,4 +434,8 @@ void User::_bind_methods() {
ClassDB::bind_method(D_METHOD("write_unlock"), &User::write_unlock); ClassDB::bind_method(D_METHOD("write_unlock"), &User::write_unlock);
ClassDB::bind_method(D_METHOD("read_try_lock"), &User::read_try_lock); ClassDB::bind_method(D_METHOD("read_try_lock"), &User::read_try_lock);
ClassDB::bind_method(D_METHOD("write_try_lock"), &User::write_try_lock); ClassDB::bind_method(D_METHOD("write_try_lock"), &User::write_try_lock);
// No property. Changing this should not really happen normally, only during more advanced uses.
ClassDB::bind_method(D_METHOD("get_owner_user_manager"), &User::get_owner_user_manager);
ClassDB::bind_method(D_METHOD("set_owner_user_manager", "user_manager"), &User::set_owner_user_manager_bind);
} }

View File

@ -38,6 +38,7 @@
#include "core/object/resource.h" #include "core/object/resource.h"
class UserModule; class UserModule;
class UserManager;
class User : public Resource { class User : public Resource {
GDCLASS(User, Resource); GDCLASS(User, Resource);
@ -108,6 +109,10 @@ public:
Error read_try_lock(); Error read_try_lock();
Error write_try_lock(); Error write_try_lock();
UserManager *get_owner_user_manager();
void set_owner_user_manager(UserManager *p_user_manager);
void set_owner_user_manager_bind(Node *p_user_manager);
User(); User();
~User(); ~User();
@ -128,6 +133,8 @@ protected:
Vector<Ref<UserModule>> _modules; Vector<Ref<UserModule>> _modules;
RWLock _rw_lock; RWLock _rw_lock;
UserManager *_owner_user_manager;
}; };
#endif #endif