diff --git a/autoloads/entity_data_manager.cpp b/autoloads/entity_data_manager.cpp index d786ff1..18caea9 100644 --- a/autoloads/entity_data_manager.cpp +++ b/autoloads/entity_data_manager.cpp @@ -213,7 +213,7 @@ Vector > *EntityDataManager::get_auras() { } Ref EntityDataManager::get_aura(int aura_id) { - ERR_FAIL_COND_V(!_aura_map.has(aura_id), Ref(NULL)); + ERR_FAIL_COND_V_MSG(!_aura_map.has(aura_id), Ref(NULL), "Could not find aura! Id:" + String::num(aura_id)); return _aura_map.get(aura_id); } @@ -1007,7 +1007,7 @@ void EntityDataManager::_bind_methods() { EntityDataManager::EntityDataManager() { instance = this; - //_automatic_load = GLOBAL_DEF("ess/data/automatic_load", false); + _automatic_load = GLOBAL_DEF("ess/data/automatic_load", false); _xp_data_path = GLOBAL_DEF("ess/data/xp_data_path", ""); _entity_resources_folder = GLOBAL_DEF("ess/data/entity_resources_folder", ""); @@ -1021,9 +1021,9 @@ EntityDataManager::EntityDataManager() { _mob_data_folder = GLOBAL_DEF("ess/data/mob_data_folder", ""); _player_character_data_folder = GLOBAL_DEF("ess/data/player_character_data_folder", ""); - //if (_automatic_load) { - // load_all(); - //} + if (_automatic_load) { + call_deferred("load_all"); + } } EntityDataManager::~EntityDataManager() { diff --git a/profile_manager/profile_manager.cpp b/profile_manager/profile_manager.cpp index 122bf5f..b8b7750 100644 --- a/profile_manager/profile_manager.cpp +++ b/profile_manager/profile_manager.cpp @@ -21,6 +21,9 @@ SOFTWARE. */ #include "profile_manager.h" +#include "core/os/file_access.h" +#include "core/io/json.h" +#include "core/project_settings.h" const String ProfileManager::DEFAULT_PROFILE_FILE_NAME = "default.profile"; @@ -30,6 +33,13 @@ ProfileManager *ProfileManager::get_instance() { return _instance; } +String ProfileManager::get_save_file() const { + return _save_file; +} +void ProfileManager::set_save_file(const String &file) { + _save_file = file; +} + int ProfileManager::get_last_used_class() { return _last_used_class; } @@ -81,17 +91,53 @@ Ref ProfileManager::get_class_profile(int class_id) { } void ProfileManager::save() { - if (has_method("_save")) { - call("_save"); - } + call("_save"); } void ProfileManager::load() { - if (has_method("_load")) { - call("_load"); - } //else { - //load_defaults(); - //} + call("_load"); +} + +void ProfileManager::_save() { + Error err; + FileAccess *f = FileAccess::open(_save_file, FileAccess::WRITE, &err); + + if (!f) { + ERR_FAIL_MSG("Couldn't open file: " + err); + } + + f->store_line(JSON::print(to_dict())); + f->close(); +} + +void ProfileManager::_load() { + if (FileAccess::exists(_save_file)) { + clear_class_profiles(); + + Error err; + String text = FileAccess::get_file_as_string(_save_file, &err); + + if (err) { + load_defaults(); + ERR_FAIL_MSG("Couldn't open file: " + err); + } + + String err_txt; + int err_line; + Variant v; + err = JSON::parse(text, v, err_txt, err_line); + + if (err) { + load_defaults(); + ERR_FAIL_MSG("Error parsing profile: " + err); + } + + Dictionary d = v; + + from_dict(d); + } else { + load_defaults(); + } } void ProfileManager::save_profile(String name) { @@ -163,6 +209,13 @@ ProfileManager::ProfileManager() { _last_used_class = 0; _profile_name = ProfileManager::DEFAULT_PROFILE_FILE_NAME; + + _automatic_load = GLOBAL_DEF("ess/profiles/automatic_load", false); + _save_file = GLOBAL_DEF("ess/profiles/save_file", "user://profile.save"); + + if (_automatic_load) { + call_deferred("load"); + } } ProfileManager::~ProfileManager() { @@ -177,6 +230,17 @@ void ProfileManager::_bind_methods() { BIND_VMETHOD(MethodInfo("_save")); BIND_VMETHOD(MethodInfo("_load")); + ClassDB::bind_method(D_METHOD("_save"), &ProfileManager::_save); + ClassDB::bind_method(D_METHOD("_load"), &ProfileManager::_load); + + ClassDB::bind_method(D_METHOD("get_automatic_load"), &ProfileManager::get_automatic_load); + ClassDB::bind_method(D_METHOD("set_automatic_load", "load"), &ProfileManager::set_automatic_load); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "automatic_load"), "set_automatic_load", "get_automatic_load"); + + ClassDB::bind_method(D_METHOD("get_save_file"), &ProfileManager::get_save_file); + ClassDB::bind_method(D_METHOD("set_save_file", "path"), &ProfileManager::set_save_file); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "set_save_file"), "set_save_file", "get_save_file"); + ClassDB::bind_method(D_METHOD("get_last_used_class"), &ProfileManager::get_last_used_class); ClassDB::bind_method(D_METHOD("set_last_used_class", "value"), &ProfileManager::set_last_used_class); ADD_PROPERTY(PropertyInfo(Variant::INT, "last_used_class"), "set_last_used_class", "get_last_used_class"); diff --git a/profile_manager/profile_manager.h b/profile_manager/profile_manager.h index ba3285f..cc4e7dd 100644 --- a/profile_manager/profile_manager.h +++ b/profile_manager/profile_manager.h @@ -23,19 +23,26 @@ SOFTWARE. #ifndef PROFILE_MANAGER_H #define PROFILE_MANAGER_H +#include "core/object.h" + #include "core/vector.h" -#include "scene/main/node.h" #include "class_profile.h" -class ProfileManager : public Node { - GDCLASS(ProfileManager, Node); +class ProfileManager : public Object { + GDCLASS(ProfileManager, Object); public: static const String DEFAULT_PROFILE_FILE_NAME; static ProfileManager *get_instance(); + bool get_automatic_load() { return _automatic_load; } + void set_automatic_load(bool load) { _automatic_load = load; } + + String get_save_file() const; + void set_save_file(const String &file); + int get_last_used_class(); void set_last_used_class(int value); @@ -51,6 +58,9 @@ public: void save(); void load(); + void _save(); + void _load(); + void save_profile(String name); void load_profile(String name); @@ -68,9 +78,12 @@ protected: private: static ProfileManager *_instance; + bool _automatic_load; + int _last_used_class; String _profile_name; + String _save_file; Vector > _class_profiles; }; diff --git a/register_types.cpp b/register_types.cpp index e5c547b..f210b82 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -134,6 +134,7 @@ SOFTWARE. #include "profile_manager/profile_manager.h" static EntityDataManager *entity_data_manager = NULL; +static ProfileManager *profile_manager = NULL; void register_entity_spell_system_types() { ClassDB::register_class(); @@ -262,10 +263,17 @@ void register_entity_spell_system_types() { ClassDB::register_class(); Engine::get_singleton()->add_singleton(Engine::Singleton("EntityDataManager", EntityDataManager::get_instance())); + profile_manager = memnew(ProfileManager); + ClassDB::register_class(); + Engine::get_singleton()->add_singleton(Engine::Singleton("ProfileManager", ProfileManager::get_instance())); } void unregister_entity_spell_system_types() { if (entity_data_manager) { memdelete(entity_data_manager); } + + if (profile_manager) { + memdelete(profile_manager); + } }