Skip to content

Commit

Permalink
Add ElunaConfig class and remove enabled bool
Browse files Browse the repository at this point in the history
All config should be handled through this class. There is no need to check internally if Eluna is enabled, if Eluna wasn't enabled it wouldn't ever even reach said checks
  • Loading branch information
Foereaper committed Jan 28, 2024
1 parent 23203c3 commit 9806ef7
Show file tree
Hide file tree
Showing 18 changed files with 122 additions and 66 deletions.
2 changes: 0 additions & 2 deletions BattleGroundHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
using namespace Hooks;

#define START_HOOK(EVENT) \
if (!IsEnabled())\
return;\
auto key = EventKey<BGEvents>(EVENT);\
if (!BGEventBindings->HasBindingsFor(key))\
return;
Expand Down
4 changes: 0 additions & 4 deletions CreatureHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@
using namespace Hooks;

#define START_HOOK(EVENT, CREATURE) \
if (!IsEnabled())\
return;\
auto entry_key = EntryKey<CreatureEvents>(EVENT, CREATURE->GetEntry());\
auto unique_key = UniqueObjectKey<CreatureEvents>(EVENT, CREATURE->GET_GUID(), CREATURE->GetInstanceId());\
if (!CreatureEventBindings->HasBindingsFor(entry_key))\
if (!CreatureUniqueBindings->HasBindingsFor(unique_key))\
return;

#define START_HOOK_WITH_RETVAL(EVENT, CREATURE, RETVAL) \
if (!IsEnabled())\
return RETVAL;\
auto entry_key = EntryKey<CreatureEvents>(EVENT, CREATURE->GetEntry());\
auto unique_key = UniqueObjectKey<CreatureEvents>(EVENT, CREATURE->GET_GUID(), CREATURE->GetInstanceId());\
if (!CreatureEventBindings->HasBindingsFor(entry_key))\
Expand Down
58 changes: 58 additions & 0 deletions ElunaConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2010 - 2024 Eluna Lua Engine <https://elunaluaengine.github.io/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/

#include "Config.h"
#include "ElunaConfig.h"

ElunaConfig::ElunaConfig()
{
}

ElunaConfig* ElunaConfig::instance()
{
static ElunaConfig instance;
return &instance;
}

ElunaConfig::~ElunaConfig()
{
}

void ElunaConfig::Initialize()
{
// Load bools
SetConfig(CONFIG_ELUNA_ENABLED, "Eluna.Enabled", true);
SetConfig(CONFIG_ELUNA_COMPATIBILITY_MODE, "Eluna.CompatibilityMode", true);
SetConfig(CONFIG_ELUNA_TRACEBACK, "Eluna.TraceBack", false);

// Load strings
SetConfig(CONFIG_ELUNA_SCRIPT_PATH, "Eluna.ScriptPath", "lua_scripts");
SetConfig(CONFIG_ELUNA_ONLY_ON_MAPS, "Eluna.OnlyOnMaps", "");
}

void ElunaConfig::SetConfig(ElunaConfigBoolValues index, char const* fieldname, bool defvalue)
{
#ifdef TRINITY
SetConfig(index, sConfigMgr->GetBoolDefault(fieldname, defvalue));
#endif
}

void ElunaConfig::SetConfig(ElunaConfigStringValues index, char const* fieldname, std::string defvalue)
{
#ifdef TRINITY
SetConfig(index, sConfigMgr->GetStringDefault(fieldname, defvalue));
#endif
}

bool ElunaConfig::IsElunaEnabled()
{
return GetConfig(CONFIG_ELUNA_ENABLED);
}

bool ElunaConfig::IsElunaCompatibilityMode()
{
return GetConfig(CONFIG_ELUNA_COMPATIBILITY_MODE);
}
58 changes: 58 additions & 0 deletions ElunaConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2010 - 2024 Eluna Lua Engine <https://elunaluaengine.github.io/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/

#ifndef _ELUNACONFIG_H
#define _ELUNACONFIG_H

#include "ElunaUtility.h"

enum ElunaConfigBoolValues
{
CONFIG_ELUNA_ENABLED,
CONFIG_ELUNA_COMPATIBILITY_MODE,
CONFIG_ELUNA_TRACEBACK,
CONFIG_ELUNA_BOOL_COUNT
};

enum ElunaConfigStringValues
{
CONFIG_ELUNA_SCRIPT_PATH,
CONFIG_ELUNA_ONLY_ON_MAPS,
CONFIG_ELUNA_STRING_COUNT
};

class ElunaConfig
{
private:
ElunaConfig();
~ElunaConfig();
ElunaConfig(ElunaConfig const&) = delete;
ElunaConfig& operator=(ElunaConfig const&) = delete;

public:
static ElunaConfig* instance();

void Initialize();

bool GetConfig(ElunaConfigBoolValues index) const { return _configBoolValues[index]; }
const std::string& GetConfig(ElunaConfigStringValues index) const { return _configStringValues[index]; }
void SetConfig(ElunaConfigBoolValues index, bool value) { _configBoolValues[index] = value; }
void SetConfig(ElunaConfigStringValues index, std::string value) { _configStringValues[index] = value; }

bool IsElunaEnabled();
bool IsElunaCompatibilityMode();

private:
bool _configBoolValues[CONFIG_ELUNA_BOOL_COUNT];
std::string _configStringValues[CONFIG_ELUNA_STRING_COUNT];

void SetConfig(ElunaConfigBoolValues index, char const* fieldname, bool defvalue);
void SetConfig(ElunaConfigStringValues index, char const* fieldname, std::string defvalue);
};

#define sElunaConfig ElunaConfig::instance()

#endif //_ELUNACONFIG_H
1 change: 0 additions & 1 deletion ElunaIncludes.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ typedef Opcodes OpcodesList;
#define CORE_VERSION (GitRevision::GetFullVersion())
#define eWorld (sWorld)
#define eMapMgr (sMapMgr)
#define eConfigMgr (sConfigMgr)
#define eGuildMgr (sGuildMgr)
#define eObjectMgr (sObjectMgr)
#define eAccountMgr (sAccountMgr)
Expand Down
7 changes: 4 additions & 3 deletions ElunaLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/*
* Copyright (C) 2010 - 2022 Eluna Lua Engine <https://elunaluaengine.github.io/>
* Copyright (C) 2010 - 2024 Eluna Lua Engine <https://elunaluaengine.github.io/>
* Copyright (C) 2022 - 2022 Hour of Twilight <https://www.houroftwilight.net/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/

#include "LuaEngine.h"
#include "ElunaConfig.h"
#include "ElunaLoader.h"
#include "ElunaUtility.h"
#include "ElunaIncludes.h"
Expand Down Expand Up @@ -40,7 +41,7 @@ ElunaLoader::~ElunaLoader()

void ElunaLoader::LoadScripts()
{
lua_folderpath = eConfigMgr->GetStringDefault("Eluna.ScriptPath", "lua_scripts");
lua_folderpath = sElunaConfig->GetConfig(CONFIG_ELUNA_SCRIPT_PATH);

uint32 oldMSTime = ElunaUtil::GetCurrTime();
lua_scripts.clear();
Expand All @@ -61,7 +62,7 @@ void ElunaLoader::LoadScripts()

ELUNA_LOG_INFO("[Eluna]: Loaded and precompiled %u scripts in %u ms", uint32(combined_scripts.size()), ElunaUtil::GetTimeDiff(oldMSTime));
requiredMaps.clear();
std::istringstream maps(eConfigMgr->GetStringDefault("Eluna.OnlyOnMaps", ""));
std::istringstream maps(sElunaConfig->GetConfig(CONFIG_ELUNA_ONLY_ON_MAPS));
while (maps.good())
{
std::string mapIdStr;
Expand Down
4 changes: 0 additions & 4 deletions GameObjectHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
using namespace Hooks;

#define START_HOOK(EVENT, ENTRY) \
if (!IsEnabled())\
return;\
auto key = EntryKey<GameObjectEvents>(EVENT, ENTRY);\
if (!GameObjectEventBindings->HasBindingsFor(key))\
return;

#define START_HOOK_WITH_RETVAL(EVENT, ENTRY, RETVAL) \
if (!IsEnabled())\
return RETVAL;\
auto key = EntryKey<GameObjectEvents>(EVENT, ENTRY);\
if (!GameObjectEventBindings->HasBindingsFor(key))\
return RETVAL;
Expand Down
4 changes: 0 additions & 4 deletions GossipHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@
using namespace Hooks;

#define START_HOOK(BINDINGS, EVENT, ENTRY) \
if (!IsEnabled())\
return;\
auto key = EntryKey<GossipEvents>(EVENT, ENTRY);\
if (!BINDINGS->HasBindingsFor(key))\
return;

#define START_HOOK_WITH_RETVAL(BINDINGS, EVENT, ENTRY, RETVAL) \
if (!IsEnabled())\
return RETVAL;\
auto key = EntryKey<GossipEvents>(EVENT, ENTRY);\
if (!BINDINGS->HasBindingsFor(key))\
return RETVAL;
Expand Down
4 changes: 0 additions & 4 deletions GroupHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@
using namespace Hooks;

#define START_HOOK(EVENT) \
if (!IsEnabled())\
return;\
auto key = EventKey<GroupEvents>(EVENT);\
if (!GroupEventBindings->HasBindingsFor(key))\
return;

#define START_HOOK_WITH_RETVAL(EVENT, RETVAL) \
if (!IsEnabled())\
return RETVAL;\
auto key = EventKey<GroupEvents>(EVENT);\
if (!GroupEventBindings->HasBindingsFor(key))\
return RETVAL;
Expand Down
2 changes: 0 additions & 2 deletions GuildHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
using namespace Hooks;

#define START_HOOK(EVENT) \
if (!IsEnabled())\
return;\
auto key = EventKey<GuildEvents>(EVENT);\
if (!GuildEventBindings->HasBindingsFor(key))\
return;
Expand Down
4 changes: 0 additions & 4 deletions InstanceHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
using namespace Hooks;

#define START_HOOK(EVENT, AI) \
if (!IsEnabled())\
return;\
auto mapKey = EntryKey<InstanceEvents>(EVENT, AI->instance->GetId());\
auto instanceKey = EntryKey<InstanceEvents>(EVENT, AI->instance->GetInstanceId());\
if (!MapEventBindings->HasBindingsFor(mapKey) && !InstanceEventBindings->HasBindingsFor(instanceKey))\
Expand All @@ -25,8 +23,6 @@ using namespace Hooks;
HookPush(AI->instance)

#define START_HOOK_WITH_RETVAL(EVENT, AI, RETVAL) \
if (!IsEnabled())\
return RETVAL;\
auto mapKey = EntryKey<InstanceEvents>(EVENT, AI->instance->GetId());\
auto instanceKey = EntryKey<InstanceEvents>(EVENT, AI->instance->GetInstanceId());\
if (!MapEventBindings->HasBindingsFor(mapKey) && !InstanceEventBindings->HasBindingsFor(instanceKey))\
Expand Down
4 changes: 0 additions & 4 deletions ItemHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@
using namespace Hooks;

#define START_HOOK(EVENT, ENTRY) \
if (!IsEnabled())\
return;\
auto key = EntryKey<ItemEvents>(EVENT, ENTRY);\
if (!ItemEventBindings->HasBindingsFor(key))\
return;

#define START_HOOK_WITH_RETVAL(EVENT, ENTRY, RETVAL) \
if (!IsEnabled())\
return RETVAL;\
auto key = EntryKey<ItemEvents>(EVENT, ENTRY);\
if (!ItemEventBindings->HasBindingsFor(key))\
return RETVAL;
Expand Down
20 changes: 2 additions & 18 deletions LuaEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "Hooks.h"
#include "LuaEngine.h"
#include "BindingMap.h"
#include "ElunaConfig.h"
#include "ElunaEventMgr.h"
#include "ElunaIncludes.h"
#include "ElunaLoader.h"
Expand Down Expand Up @@ -77,7 +78,6 @@ void Eluna::_ReloadEluna()
Eluna::Eluna(Map* map, bool compatMode) :
event_level(0),
push_counter(0),
enabled(false),
boundMap(map),
compatibilityMode(compatMode),

Expand Down Expand Up @@ -133,13 +133,6 @@ void Eluna::CloseLua()

void Eluna::OpenLua()
{
enabled = eConfigMgr->GetBoolDefault("Eluna.Enabled", true);
if (!IsEnabled())
{
ELUNA_LOG_INFO("[Eluna]: Eluna is disabled in config");
return;
}

L = luaL_newstate();

lua_pushlightuserdata(L, this);
Expand Down Expand Up @@ -378,7 +371,7 @@ bool Eluna::ExecuteCall(int params, int res)
ASSERT(false); // stack probably corrupt
}

bool usetrace = eConfigMgr->GetBoolDefault("Eluna.TraceBack", false);
bool usetrace = sElunaConfig->GetConfig(CONFIG_ELUNA_TRACEBACK);
if (usetrace)
{
lua_pushcfunction(L, &StackTrace);
Expand Down Expand Up @@ -1036,9 +1029,6 @@ int Eluna::CallOneFunction(int number_of_functions, int number_of_arguments, int

CreatureAI* Eluna::GetAI(Creature* creature)
{
if (!IsEnabled())
return NULL;

for (int i = 1; i < Hooks::CREATURE_EVENT_COUNT; ++i)
{
Hooks::CreatureEvents event_id = (Hooks::CreatureEvents)i;
Expand All @@ -1056,9 +1046,6 @@ CreatureAI* Eluna::GetAI(Creature* creature)

InstanceData* Eluna::GetInstanceData(Map* map)
{
if (!IsEnabled())
return NULL;

for (int i = 1; i < Hooks::INSTANCE_EVENT_COUNT; ++i)
{
Hooks::InstanceEvents event_id = (Hooks::InstanceEvents)i;
Expand Down Expand Up @@ -1120,9 +1107,6 @@ void Eluna::CreateInstanceData(Map const* map)
*/
void Eluna::FreeInstanceId(uint32 instanceId)
{
if (!IsEnabled())
return;

for (int i = 1; i < Hooks::INSTANCE_EVENT_COUNT; ++i)
{
auto key = EntryKey<Hooks::InstanceEvents>((Hooks::InstanceEvents)i, instanceId);
Expand Down
2 changes: 0 additions & 2 deletions LuaEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ class ELUNA_GAME_API Eluna
// 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* const boundMap;

Expand Down Expand Up @@ -340,7 +339,6 @@ class ELUNA_GAME_API Eluna
void PushInstanceData(lua_State* L, ElunaInstanceAI* ai, bool incrementCounter = true);

void RunScripts();
bool IsEnabled() const { return enabled; }
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);
Expand Down
4 changes: 0 additions & 4 deletions PacketHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@
using namespace Hooks;

#define START_HOOK_SERVER(EVENT) \
if (!IsEnabled())\
return;\
auto key = EventKey<ServerEvents>(EVENT);\
if (!ServerEventBindings->HasBindingsFor(key))\
return;

#define START_HOOK_PACKET(EVENT, OPCODE) \
if (!IsEnabled())\
return;\
auto key = EntryKey<PacketEvents>(EVENT, OPCODE);\
if (!PacketEventBindings->HasBindingsFor(key))\
return;
Expand Down
4 changes: 0 additions & 4 deletions PlayerHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@
using namespace Hooks;

#define START_HOOK(EVENT) \
if (!IsEnabled())\
return;\
auto key = EventKey<PlayerEvents>(EVENT);\
if (!PlayerEventBindings->HasBindingsFor(key))\
return;

#define START_HOOK_WITH_RETVAL(EVENT, RETVAL) \
if (!IsEnabled())\
return RETVAL;\
auto key = EventKey<PlayerEvents>(EVENT);\
if (!PlayerEventBindings->HasBindingsFor(key))\
return RETVAL;
Expand Down
Loading

0 comments on commit 9806ef7

Please sign in to comment.