From 066e88e3366acc600b132214e4fb432095a46d14 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 11 May 2025 18:37:59 +0200 Subject: [PATCH] Store the owner UserManager in User. --- modules/users/doc_classes/UserManager.xml | 3 +++ modules/users/managers/user_manager.cpp | 2 ++ modules/users/users/user.cpp | 24 +++++++++++++++++++---- modules/users/users/user.h | 7 +++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/modules/users/doc_classes/UserManager.xml b/modules/users/doc_classes/UserManager.xml index 678d008dd..41370bb65 100644 --- a/modules/users/doc_classes/UserManager.xml +++ b/modules/users/doc_classes/UserManager.xml @@ -27,6 +27,9 @@ var um : SomeUserModule = SomeUserModule.new() um.module_name = "SomeUserModule" user.add_module(um) + + # Only needed if the default implementation is not called + #user.set_owner_user_manager(self) return ._create_user(user)[/code] diff --git a/modules/users/managers/user_manager.cpp b/modules/users/managers/user_manager.cpp index ba8256225..a427b37a1 100644 --- a/modules/users/managers/user_manager.cpp +++ b/modules/users/managers/user_manager.cpp @@ -80,6 +80,8 @@ Ref UserManager::_create_user(Ref p_user) { if (!p_user.is_valid()) { p_user.instance(); } + + p_user->set_owner_user_manager(this); return p_user; } diff --git a/modules/users/users/user.cpp b/modules/users/users/user.cpp index e7e135c21..22e663215 100644 --- a/modules/users/users/user.cpp +++ b/modules/users/users/user.cpp @@ -30,9 +30,10 @@ /*************************************************************************/ #include "user.h" +#include "../managers/user_manager.h" +#include "../singleton/user_db.h" #include "core/io/json.h" #include "core/object/class_db.h" -#include "../singleton/user_db.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 // 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. - + Dictionary dict; 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. // 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. - + _user_id = dict["user_id"]; _user_name = dict["user_name"]; _email = dict["email"]; @@ -276,7 +277,7 @@ void User::_from_dict(const Dictionary &dict) { } ERR_CONTINUE(!m.is_valid()); - + // Call write lock from the outside m->write_lock(); m->from_dict(mdict["data"]); @@ -324,11 +325,22 @@ Error User::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(p_user_manager)); +} + User::User() { _user_id = -1; _rank = 0; _banned = false; _locked = false; + _owner_user_manager = NULL; } 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("read_try_lock"), &User::read_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); } diff --git a/modules/users/users/user.h b/modules/users/users/user.h index d3f629bab..b985e9f68 100644 --- a/modules/users/users/user.h +++ b/modules/users/users/user.h @@ -38,6 +38,7 @@ #include "core/object/resource.h" class UserModule; +class UserManager; class User : public Resource { GDCLASS(User, Resource); @@ -108,6 +109,10 @@ public: Error read_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(); @@ -128,6 +133,8 @@ protected: Vector> _modules; RWLock _rw_lock; + + UserManager *_owner_user_manager; }; #endif