diff --git a/src/ElunaLuaEngine_SC.cpp b/src/ElunaLuaEngine_SC.cpp index 449c1ed..1de8cb3 100644 --- a/src/ElunaLuaEngine_SC.cpp +++ b/src/ElunaLuaEngine_SC.cpp @@ -1105,6 +1105,7 @@ public: void OnBeforeConfigLoad(bool reload) override { + ElunaConfig::GetInstance().Initialize(reload); if (!reload) { ///- Initialize Lua Engine diff --git a/src/LuaEngine/ElunaConfig.cpp b/src/LuaEngine/ElunaConfig.cpp new file mode 100644 index 0000000..d2f8b3d --- /dev/null +++ b/src/LuaEngine/ElunaConfig.cpp @@ -0,0 +1,30 @@ +#include "ElunaConfig.h" + +ElunaConfig& ElunaConfig::GetInstance() +{ + static ElunaConfig instance; + return instance; +} + +ElunaConfig::ElunaConfig() : ConfigValueCache(ElunaConfigValues::CONFIG_VALUE_COUNT) +{ +} + +void ElunaConfig::Initialize(bool reload) +{ + ConfigValueCache::Initialize(reload); +} + +void ElunaConfig::BuildConfigCache() +{ + SetConfigValue(ElunaConfigValues::ENABLED, "Eluna.Enabled", "false"); + SetConfigValue(ElunaConfigValues::TRACEBACK_ENABLED, "Eluna.TraceBack", "false"); + SetConfigValue(ElunaConfigValues::AUTORELOAD_ENABLED, "Eluna.AutoReload", "false"); + SetConfigValue(ElunaConfigValues::BYTECODE_CACHE_ENABLED, "Eluna.BytecodeCache", "false"); + + SetConfigValue(ElunaConfigValues::SCRIPT_PATH, "Eluna.ScriptPath", "lua_scripts"); + SetConfigValue(ElunaConfigValues::REQUIRE_PATH, "Eluna.RequirePaths", ""); + SetConfigValue(ElunaConfigValues::REQUIRE_CPATH, "Eluna.RequireCPaths", ""); + + SetConfigValue(ElunaConfigValues::AUTORELOAD_INTERVAL, "Eluna.AutoReloadInterval", 1); +} diff --git a/src/LuaEngine/ElunaConfig.h b/src/LuaEngine/ElunaConfig.h new file mode 100644 index 0000000..2714aa4 --- /dev/null +++ b/src/LuaEngine/ElunaConfig.h @@ -0,0 +1,53 @@ +#ifndef ELUNA_CONFIG_HPP +#define ELUNA_CONFIG_HPP + +#include "ConfigValueCache.h" + +enum class ElunaConfigValues : uint32 +{ + // Boolean + ENABLED = 0, + TRACEBACK_ENABLED, + AUTORELOAD_ENABLED, + BYTECODE_CACHE_ENABLED, + + // String + SCRIPT_PATH, + REQUIRE_PATH, + REQUIRE_CPATH, + + // Number + AUTORELOAD_INTERVAL, + + CONFIG_VALUE_COUNT +}; + +class ElunaConfig final : public ConfigValueCache +{ + public: + static ElunaConfig& GetInstance(); + + void Initialize(bool reload = false); + + bool IsElunaEnabled() const { return GetConfigValue(ElunaConfigValues::ENABLED); } + bool IsTraceBackEnabled() const { return GetConfigValue(ElunaConfigValues::TRACEBACK_ENABLED); } + bool IsAutoReloadEnabled() const { return GetConfigValue(ElunaConfigValues::AUTORELOAD_ENABLED); } + bool IsByteCodeCacheEnabled() const { return GetConfigValue(ElunaConfigValues::BYTECODE_CACHE_ENABLED); } + + std::string_view GetScriptPath() const { return GetConfigValue(ElunaConfigValues::SCRIPT_PATH); } + std::string_view GetRequirePath() const { return GetConfigValue(ElunaConfigValues::REQUIRE_PATH); } + std::string_view GetRequireCPath() const { return GetConfigValue(ElunaConfigValues::REQUIRE_CPATH); } + + uint32 GetAutoReloadInterval() const { return GetConfigValue(ElunaConfigValues::AUTORELOAD_INTERVAL); } + + protected: + void BuildConfigCache() override; + + private: + ElunaConfig(); + ~ElunaConfig() = default; + ElunaConfig(const ElunaConfig&) = delete; + ElunaConfig& operator=(const ElunaConfig&) = delete; +}; + +#endif // ELUNA_CONFIG_H \ No newline at end of file diff --git a/src/LuaEngine/LuaEngine.cpp b/src/LuaEngine/LuaEngine.cpp index 7a659c0..c6a2b37 100644 --- a/src/LuaEngine/LuaEngine.cpp +++ b/src/LuaEngine/LuaEngine.cpp @@ -78,7 +78,7 @@ void Eluna::Initialize() GEluna = new Eluna(); // Start file watcher if enabled - if (eConfigMgr->GetOption("Eluna.AutoReload", false)) + if (ElunaConfig::GetInstance().IsAutoReloadEnabled()) { uint32 watchInterval = eConfigMgr->GetOption("Eluna.AutoReloadInterval", 1); fileWatcher = std::make_unique(); @@ -117,9 +117,9 @@ void Eluna::LoadScriptPaths() lua_scripts.clear(); lua_extensions.clear(); - lua_folderpath = eConfigMgr->GetOption("Eluna.ScriptPath", "lua_scripts"); - const std::string& lua_path_extra = eConfigMgr->GetOption("Eluna.RequirePaths", ""); - const std::string& lua_cpath_extra = eConfigMgr->GetOption("Eluna.RequireCPaths", ""); + lua_folderpath = ElunaConfig::GetInstance().GetScriptPath(); + const std::string& lua_path_extra = static_cast(ElunaConfig::GetInstance().GetRequirePath()); + const std::string& lua_cpath_extra = static_cast(ElunaConfig::GetInstance().GetRequireCPath()); #ifndef ELUNA_WINDOWS if (lua_folderpath[0] == '~') @@ -182,7 +182,6 @@ void Eluna::_ReloadEluna() Eluna::Eluna() : event_level(0), push_counter(0), -enabled(false), L(NULL), eventMgr(NULL), @@ -249,9 +248,7 @@ void Eluna::CloseLua() void Eluna::OpenLua() { - enabled = eConfigMgr->GetOption("Eluna.Enabled", true); - - if (!IsEnabled()) + if (!ElunaConfig::GetInstance().IsElunaEnabled()) { ELUNA_LOG_INFO("[Eluna]: Eluna is disabled in config"); return; @@ -532,7 +529,7 @@ int Eluna::TryLoadFromGlobalCache(lua_State* L, const std::string& filepath) int Eluna::LoadScriptWithCache(lua_State* L, const std::string& filepath, bool isMoonScript, uint32* compiledCount, uint32* cachedCount) { - bool cacheEnabled = eConfigMgr->GetOption("Eluna.BytecodeCache", true); + bool cacheEnabled = ElunaConfig::GetInstance().IsByteCodeCacheEnabled(); if (cacheEnabled) { @@ -671,7 +668,7 @@ static bool ScriptPathComparator(const LuaScript& first, const LuaScript& second void Eluna::RunScripts() { LOCK_ELUNA; - if (!IsEnabled()) + if (!ElunaConfig::GetInstance().IsElunaEnabled()) return; uint32 oldMSTime = ElunaUtil::GetCurrTime(); @@ -855,7 +852,7 @@ bool Eluna::ExecuteCall(int params, int res) ASSERT(false); // stack probably corrupt } - bool usetrace = eConfigMgr->GetOption("Eluna.TraceBack", false); + bool usetrace = ElunaConfig::GetInstance().IsTraceBackEnabled(); if (usetrace) { lua_pushcfunction(L, &StackTrace); @@ -1576,7 +1573,7 @@ int Eluna::CallOneFunction(int number_of_functions, int number_of_arguments, int CreatureAI* Eluna::GetAI(Creature* creature) { - if (!IsEnabled()) + if (!ElunaConfig::GetInstance().IsElunaEnabled()) return NULL; for (int i = 1; i < Hooks::CREATURE_EVENT_COUNT; ++i) @@ -1596,7 +1593,7 @@ CreatureAI* Eluna::GetAI(Creature* creature) InstanceData* Eluna::GetInstanceData(Map* map) { - if (!IsEnabled()) + if (!ElunaConfig::GetInstance().IsElunaEnabled()) return NULL; for (int i = 1; i < Hooks::INSTANCE_EVENT_COUNT; ++i) @@ -1662,7 +1659,7 @@ void Eluna::FreeInstanceId(uint32 instanceId) { LOCK_ELUNA; - if (!IsEnabled()) + if (!ElunaConfig::GetInstance().IsElunaEnabled()) return; for (int i = 1; i < Hooks::INSTANCE_EVENT_COUNT; ++i) diff --git a/src/LuaEngine/LuaEngine.h b/src/LuaEngine/LuaEngine.h index 86c408c..efa96d3 100644 --- a/src/LuaEngine/LuaEngine.h +++ b/src/LuaEngine/LuaEngine.h @@ -23,8 +23,9 @@ #include "HttpManager.h" #include "EventEmitter.h" #include "TicketMgr.h" -#include "ElunaFileWatcher.h" #include "LootMgr.h" +#include "ElunaFileWatcher.h" +#include "ElunaConfig.h" #include #include #include @@ -145,7 +146,6 @@ private: // When a hook pushes arguments to be passed to event handlers, // this is used to keep track of how many arguments were pushed. uint8 push_counter; - bool enabled; // Map from instance ID -> Lua table ref std::unordered_map instanceDataRefs; @@ -335,7 +335,6 @@ public: void RunScripts(); bool ShouldReload() const { return reload; } - bool IsEnabled() const { return enabled && IsInitialized(); } bool HasLuaState() const { return L != NULL; } uint64 GetCallstackId() const { return callstackid; } int Register(lua_State* L, uint8 reg, uint32 entry, ObjectGuid guid, uint32 instanceId, uint32 event_id, int functionRef, uint32 shots); diff --git a/src/LuaEngine/hooks/BattleGroundHooks.cpp b/src/LuaEngine/hooks/BattleGroundHooks.cpp index 713a6de..da32e22 100644 --- a/src/LuaEngine/hooks/BattleGroundHooks.cpp +++ b/src/LuaEngine/hooks/BattleGroundHooks.cpp @@ -13,7 +13,7 @@ using namespace Hooks; #define START_HOOK(EVENT) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return;\ auto key = EventKey(EVENT);\ if (!BGEventBindings->HasBindingsFor(key))\ diff --git a/src/LuaEngine/hooks/CreatureHooks.cpp b/src/LuaEngine/hooks/CreatureHooks.cpp index 2450525..eb1b04e 100644 --- a/src/LuaEngine/hooks/CreatureHooks.cpp +++ b/src/LuaEngine/hooks/CreatureHooks.cpp @@ -14,7 +14,7 @@ using namespace Hooks; #define START_HOOK(EVENT, CREATURE) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return;\ auto entry_key = EntryKey(EVENT, CREATURE->GetEntry());\ auto unique_key = UniqueObjectKey(EVENT, CREATURE->GET_GUID(), CREATURE->GetInstanceId());\ @@ -24,7 +24,7 @@ using namespace Hooks; LOCK_ELUNA #define START_HOOK_WITH_RETVAL(EVENT, CREATURE, RETVAL) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return RETVAL;\ auto entry_key = EntryKey(EVENT, CREATURE->GetEntry());\ auto unique_key = UniqueObjectKey(EVENT, CREATURE->GET_GUID(), CREATURE->GetInstanceId());\ diff --git a/src/LuaEngine/hooks/GameObjectHooks.cpp b/src/LuaEngine/hooks/GameObjectHooks.cpp index fc64c81..3eff02e 100644 --- a/src/LuaEngine/hooks/GameObjectHooks.cpp +++ b/src/LuaEngine/hooks/GameObjectHooks.cpp @@ -15,7 +15,7 @@ using namespace Hooks; #define START_HOOK(EVENT, ENTRY) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return;\ auto key = EntryKey(EVENT, ENTRY);\ if (!GameObjectEventBindings->HasBindingsFor(key))\ @@ -23,7 +23,7 @@ using namespace Hooks; LOCK_ELUNA #define START_HOOK_WITH_RETVAL(EVENT, ENTRY, RETVAL) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return RETVAL;\ auto key = EntryKey(EVENT, ENTRY);\ if (!GameObjectEventBindings->HasBindingsFor(key))\ diff --git a/src/LuaEngine/hooks/GossipHooks.cpp b/src/LuaEngine/hooks/GossipHooks.cpp index e9b58e6..5eaf4d1 100644 --- a/src/LuaEngine/hooks/GossipHooks.cpp +++ b/src/LuaEngine/hooks/GossipHooks.cpp @@ -14,7 +14,7 @@ using namespace Hooks; #define START_HOOK(BINDINGS, EVENT, ENTRY) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return;\ auto key = EntryKey(EVENT, ENTRY);\ if (!BINDINGS->HasBindingsFor(key))\ @@ -22,7 +22,7 @@ using namespace Hooks; LOCK_ELUNA #define START_HOOK_WITH_RETVAL(BINDINGS, EVENT, ENTRY, RETVAL) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return RETVAL;\ auto key = EntryKey(EVENT, ENTRY);\ if (!BINDINGS->HasBindingsFor(key))\ diff --git a/src/LuaEngine/hooks/GroupHooks.cpp b/src/LuaEngine/hooks/GroupHooks.cpp index f674957..d21a974 100644 --- a/src/LuaEngine/hooks/GroupHooks.cpp +++ b/src/LuaEngine/hooks/GroupHooks.cpp @@ -13,7 +13,7 @@ using namespace Hooks; #define START_HOOK(EVENT) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return;\ auto key = EventKey(EVENT);\ if (!GroupEventBindings->HasBindingsFor(key))\ diff --git a/src/LuaEngine/hooks/GuildHooks.cpp b/src/LuaEngine/hooks/GuildHooks.cpp index 3662ef1..ccfc37c 100644 --- a/src/LuaEngine/hooks/GuildHooks.cpp +++ b/src/LuaEngine/hooks/GuildHooks.cpp @@ -13,7 +13,7 @@ using namespace Hooks; #define START_HOOK(EVENT) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return;\ auto key = EventKey(EVENT);\ if (!GuildEventBindings->HasBindingsFor(key))\ diff --git a/src/LuaEngine/hooks/InstanceHooks.cpp b/src/LuaEngine/hooks/InstanceHooks.cpp index 29b2607..7da9126 100644 --- a/src/LuaEngine/hooks/InstanceHooks.cpp +++ b/src/LuaEngine/hooks/InstanceHooks.cpp @@ -15,7 +15,7 @@ using namespace Hooks; #define START_HOOK(EVENT, AI) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return;\ auto mapKey = EntryKey(EVENT, AI->instance->GetId());\ auto instanceKey = EntryKey(EVENT, AI->instance->GetInstanceId());\ @@ -26,7 +26,7 @@ using namespace Hooks; Push(AI->instance) #define START_HOOK_WITH_RETVAL(EVENT, AI, RETVAL) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return RETVAL;\ auto mapKey = EntryKey(EVENT, AI->instance->GetId());\ auto instanceKey = EntryKey(EVENT, AI->instance->GetInstanceId());\ diff --git a/src/LuaEngine/hooks/ItemHooks.cpp b/src/LuaEngine/hooks/ItemHooks.cpp index 1deb795..cc617ba 100644 --- a/src/LuaEngine/hooks/ItemHooks.cpp +++ b/src/LuaEngine/hooks/ItemHooks.cpp @@ -14,7 +14,7 @@ using namespace Hooks; #define START_HOOK(EVENT, ENTRY) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return;\ auto key = EntryKey(EVENT, ENTRY);\ if (!ItemEventBindings->HasBindingsFor(key))\ @@ -22,7 +22,7 @@ using namespace Hooks; LOCK_ELUNA #define START_HOOK_WITH_RETVAL(EVENT, ENTRY, RETVAL) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return RETVAL;\ auto key = EntryKey(EVENT, ENTRY);\ if (!ItemEventBindings->HasBindingsFor(key))\ diff --git a/src/LuaEngine/hooks/PacketHooks.cpp b/src/LuaEngine/hooks/PacketHooks.cpp index d11f3fa..84c95e6 100644 --- a/src/LuaEngine/hooks/PacketHooks.cpp +++ b/src/LuaEngine/hooks/PacketHooks.cpp @@ -14,7 +14,7 @@ using namespace Hooks; #define START_HOOK_SERVER(EVENT) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return;\ auto key = EventKey(EVENT);\ if (!ServerEventBindings->HasBindingsFor(key))\ @@ -22,7 +22,7 @@ using namespace Hooks; LOCK_ELUNA #define START_HOOK_PACKET(EVENT, OPCODE) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return;\ auto key = EntryKey(EVENT, OPCODE);\ if (!PacketEventBindings->HasBindingsFor(key))\ diff --git a/src/LuaEngine/hooks/PlayerHooks.cpp b/src/LuaEngine/hooks/PlayerHooks.cpp index b38f529..c0934de 100644 --- a/src/LuaEngine/hooks/PlayerHooks.cpp +++ b/src/LuaEngine/hooks/PlayerHooks.cpp @@ -14,7 +14,7 @@ using namespace Hooks; #define START_HOOK(EVENT) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return;\ auto key = EventKey(EVENT);\ if (!PlayerEventBindings->HasBindingsFor(key))\ @@ -22,7 +22,7 @@ using namespace Hooks; LOCK_ELUNA #define START_HOOK_WITH_RETVAL(EVENT, RETVAL) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return RETVAL;\ auto key = EventKey(EVENT);\ if (!PlayerEventBindings->HasBindingsFor(key))\ diff --git a/src/LuaEngine/hooks/ServerHooks.cpp b/src/LuaEngine/hooks/ServerHooks.cpp index 4f69c09..7228a1f 100644 --- a/src/LuaEngine/hooks/ServerHooks.cpp +++ b/src/LuaEngine/hooks/ServerHooks.cpp @@ -15,7 +15,7 @@ using namespace Hooks; #define START_HOOK(EVENT) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return;\ auto key = EventKey(EVENT);\ if (!ServerEventBindings->HasBindingsFor(key))\ @@ -23,7 +23,7 @@ using namespace Hooks; LOCK_ELUNA #define START_HOOK_WITH_RETVAL(EVENT, RETVAL) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return RETVAL;\ auto key = EventKey(EVENT);\ if (!ServerEventBindings->HasBindingsFor(key))\ diff --git a/src/LuaEngine/hooks/SpellHooks.cpp b/src/LuaEngine/hooks/SpellHooks.cpp index 5cec600..2c18e0a 100644 --- a/src/LuaEngine/hooks/SpellHooks.cpp +++ b/src/LuaEngine/hooks/SpellHooks.cpp @@ -14,7 +14,7 @@ using namespace Hooks; #define START_HOOK(EVENT, ENTRY) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return;\ auto key = EntryKey(EVENT, ENTRY);\ if (!SpellEventBindings->HasBindingsFor(key))\ @@ -22,7 +22,7 @@ using namespace Hooks; LOCK_ELUNA #define START_HOOK_WITH_RETVAL(EVENT, ENTRY, RETVAL) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return RETVAL;\ auto key = EntryKey(EVENT, ENTRY);\ if (!SpellEventBindings->HasBindingsFor(key))\ diff --git a/src/LuaEngine/hooks/TicketHooks.cpp b/src/LuaEngine/hooks/TicketHooks.cpp index b09bc48..c27c10a 100644 --- a/src/LuaEngine/hooks/TicketHooks.cpp +++ b/src/LuaEngine/hooks/TicketHooks.cpp @@ -14,7 +14,7 @@ using namespace Hooks; #define START_HOOK(EVENT) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return;\ auto key = EventKey(EVENT);\ if (!TicketEventBindings->HasBindingsFor(key))\ @@ -22,7 +22,7 @@ using namespace Hooks; LOCK_ELUNA #define START_HOOK(EVENT) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return;\ auto key = EventKey(EVENT);\ if (!TicketEventBindings->HasBindingsFor(key))\ diff --git a/src/LuaEngine/hooks/VehicleHooks.cpp b/src/LuaEngine/hooks/VehicleHooks.cpp index 6e1cd99..b460798 100644 --- a/src/LuaEngine/hooks/VehicleHooks.cpp +++ b/src/LuaEngine/hooks/VehicleHooks.cpp @@ -13,7 +13,7 @@ using namespace Hooks; #define START_HOOK(EVENT) \ - if (!IsEnabled())\ + if (!ElunaConfig::GetInstance().IsElunaEnabled())\ return;\ auto key = EventKey(EVENT);\ if (!VehicleEventBindings->HasBindingsFor(key))\