pandemonium_engine/modules/users/managers/user_manager_file.cpp

274 lines
5.3 KiB
C++
Raw Normal View History

2022-07-21 19:33:04 +02:00
#include "user_manager_file.h"
2022-08-17 15:35:28 +02:00
#include "core/config/engine.h"
2022-07-21 19:33:04 +02:00
#include "core/io/resource_loader.h"
#include "core/io/resource_saver.h"
#include "core/os/dir_access.h"
#include "core/os/file_access.h"
#include "core/variant/variant.h"
2022-07-21 19:33:04 +02:00
#include "../users/user.h"
String UserManagerFile::get_save_folder_path() {
return _save_folder_path;
}
void UserManagerFile::set_save_folder_path(const String &val) {
_save_folder_path = val;
}
Ref<User> UserManagerFile::_get_user(const int id) {
_rw_lock.read_lock();
if (id < 0 || id >= _users.size()) {
_rw_lock.read_unlock();
ERR_PRINT("INDEX ERROR");
return Ref<User>();
}
Ref<User> u = _users[id];
_rw_lock.read_unlock();
return u;
}
Ref<User> UserManagerFile::_get_user_name(const String &user_name) {
_rw_lock.read_lock();
for (int i = 0; i < _users.size(); ++i) {
Ref<User> u = _users[i];
if (u.is_valid()) {
if (u->get_user_name() == user_name) {
2022-07-21 19:33:04 +02:00
_rw_lock.read_unlock();
return u;
}
}
}
_rw_lock.read_unlock();
return Ref<User>();
}
Ref<User> UserManagerFile::_get_user_email(const String &user_email) {
_rw_lock.read_lock();
for (int i = 0; i < _users.size(); ++i) {
Ref<User> u = _users[i];
if (u.is_valid()) {
if (u->get_email() == user_email) {
_rw_lock.read_unlock();
return u;
}
}
}
_rw_lock.read_unlock();
return Ref<User>();
}
2022-07-21 19:33:04 +02:00
void UserManagerFile::_save_user(Ref<User> user) {
_save_queued = true;
}
Ref<User> UserManagerFile::_create_user() {
_rw_lock.write_lock();
Ref<User> u;
u.instance();
2022-07-23 20:32:42 +02:00
u->set_user_id(_users.size());
u->connect("changed", this, "_on_user_changed");
2022-07-21 19:33:04 +02:00
_users.push_back(u);
_rw_lock.write_unlock();
_save_queued = true;
return u;
}
bool UserManagerFile::_is_username_taken(const String &user_name) {
_rw_lock.read_lock();
for (int i = 0; i < _users.size(); ++i) {
Ref<User> u = _users[i];
if (u.is_valid()) {
if (u->get_user_name() == user_name) {
2022-07-21 19:33:04 +02:00
_rw_lock.read_unlock();
return true;
}
}
}
_rw_lock.read_unlock();
return false;
}
bool UserManagerFile::_is_email_taken(const String &email) {
_rw_lock.read_lock();
for (int i = 0; i < _users.size(); ++i) {
Ref<User> u = _users[i];
if (u.is_valid()) {
if (u->get_email() == email) {
2022-07-21 19:33:04 +02:00
_rw_lock.read_unlock();
return true;
}
}
}
_rw_lock.read_unlock();
return false;
}
Vector<Ref<User>> UserManagerFile::get_all() {
return _users;
}
UserManagerFile::UserManagerFile() {
set_process_internal(true);
_save_folder_path = "users";
2022-07-21 19:33:04 +02:00
}
UserManagerFile::~UserManagerFile() {
}
void UserManagerFile::load() {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
String fpath = "user://" + _save_folder_path;
DirAccess *dir = DirAccess::open(fpath);
2022-07-21 19:33:04 +02:00
if (!dir) {
return;
}
dir->list_dir_begin();
_rw_lock.write_lock();
String file = dir->get_next();
while (file != "") {
if (!dir->current_is_dir()) {
if (!file.ends_with(".tres")) {
file = dir->get_next();
2022-07-21 19:33:04 +02:00
continue;
}
String uids = file.get_slice(".", 0);
if (!uids.is_valid_unsigned_integer()) {
continue;
}
int id = uids.to_int();
//Overflow
ERR_CONTINUE(id < 0);
//TODO Users should be able to serialize themselves to json, could work similarly to Entity
//need to think, maybe it can be done better.
//Also this is not a super safe way to do this, this will definitely be changed, relatively soon
Ref<User> u = ResourceLoader::load(fpath.plus_file(file), "User", true);
//Unset script, just for good measure
u->set_script(RefPtr());
2022-07-21 19:33:04 +02:00
u->connect("changed", this, "_on_user_changed");
2022-07-21 19:33:04 +02:00
if (_users.size() <= id) {
_users.resize(id + 1);
}
_users.write[id] = u;
}
file = dir->get_next();
2022-07-21 19:33:04 +02:00
}
dir->list_dir_end();
_rw_lock.write_unlock();
}
void UserManagerFile::save() {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
2022-07-21 19:33:04 +02:00
String fpath = "user://" + _save_folder_path;
2022-07-21 19:33:04 +02:00
DirAccess *dir = DirAccess::open(fpath);
2022-07-21 19:33:04 +02:00
if (!dir) {
DirAccess *diru = DirAccess::open("user://");
diru->make_dir_recursive(fpath);
2022-07-21 19:33:04 +02:00
memdelete(diru);
dir = DirAccess::open(fpath);
}
2022-07-21 19:33:04 +02:00
ERR_FAIL_COND(!dir);
dir->list_dir_begin();
String file = dir->get_next();
while (file != "") {
if (!dir->current_is_dir()) {
if (!file.ends_with(".tres")) {
file = dir->get_next();
continue;
}
2022-07-21 19:33:04 +02:00
dir->remove(file);
}
file = dir->get_next();
2022-07-21 19:33:04 +02:00
}
dir->list_dir_end();
memdelete(dir);
_rw_lock.read_lock();
for (int i = 0; i < _users.size(); ++i) {
Ref<User> u = _users[i];
if (u.is_valid()) {
u->read_lock();
ResourceSaver::save(fpath.plus_file(itos(i) + ".tres"), u);
u->read_unlock();
}
2022-07-21 19:33:04 +02:00
}
_rw_lock.read_unlock();
}
void UserManagerFile::_on_user_changed() {
_save_queued = true;
}
void UserManagerFile::_notification(int p_what) {
if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
if (_save_queued) {
_save_queued = false;
save();
}
} else if (p_what == NOTIFICATION_POST_ENTER_TREE) {
load();
}
}
void UserManagerFile::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_save_folder_path"), &UserManagerFile::get_save_folder_path);
ClassDB::bind_method(D_METHOD("set_save_folder_path", "val"), &UserManagerFile::set_save_folder_path);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "save_folder_path"), "set_save_folder_path", "get_save_folder_path");
ClassDB::bind_method(D_METHOD("_on_user_changed"), &UserManagerFile::_on_user_changed);
}