Skip to content

Commit e8e8a06

Browse files
authored
Add method flag support (#513)
Added config variables for unsafe and deprecated methods. By default, these will be enabled for some time, but will be set to default disabled in the future. Flagged synchronous DB queries as unsafe
1 parent a93c425 commit e8e8a06

File tree

5 files changed

+48
-18
lines changed

5 files changed

+48
-18
lines changed

ElunaConfig.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ void ElunaConfig::Initialize()
3131
SetConfig(CONFIG_ELUNA_ENABLED, "Eluna.Enabled", true);
3232
SetConfig(CONFIG_ELUNA_TRACEBACK, "Eluna.TraceBack", false);
3333
SetConfig(CONFIG_ELUNA_SCRIPT_RELOADER, "Eluna.ScriptReloader", false);
34+
SetConfig(CONFIG_ELUNA_ENABLE_UNSAFE, "Eluna.UseUnsafeMethods", true);
35+
SetConfig(CONFIG_ELUNA_ENABLE_DEPRECATED, "Eluna.UseDeprecatedMethods", true);
3436

3537
// Load strings
3638
SetConfig(CONFIG_ELUNA_SCRIPT_PATH, "Eluna.ScriptPath", "lua_scripts");
@@ -62,11 +64,6 @@ void ElunaConfig::SetConfig(ElunaConfigStringValues index, char const* fieldname
6264
#endif
6365
}
6466

65-
bool ElunaConfig::IsElunaEnabled()
66-
{
67-
return GetConfig(CONFIG_ELUNA_ENABLED);
68-
}
69-
7067
bool ElunaConfig::ShouldMapLoadEluna(uint32 id)
7168
{
7269
// if the set is empty (all maps), return true

ElunaConfig.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ enum ElunaConfigBoolValues
1414
CONFIG_ELUNA_ENABLED,
1515
CONFIG_ELUNA_TRACEBACK,
1616
CONFIG_ELUNA_SCRIPT_RELOADER,
17+
CONFIG_ELUNA_ENABLE_UNSAFE,
18+
CONFIG_ELUNA_ENABLE_DEPRECATED,
1719
CONFIG_ELUNA_BOOL_COUNT
1820
};
1921

@@ -44,7 +46,9 @@ class ElunaConfig
4446
void SetConfig(ElunaConfigBoolValues index, bool value) { _configBoolValues[index] = value; }
4547
void SetConfig(ElunaConfigStringValues index, std::string value) { _configStringValues[index] = value; }
4648

47-
bool IsElunaEnabled();
49+
bool IsElunaEnabled() { return GetConfig(CONFIG_ELUNA_ENABLED); }
50+
bool UnsafeMethodsEnabled() { return GetConfig(CONFIG_ELUNA_ENABLE_UNSAFE); }
51+
bool DeprecatedMethodsEnabled() { return GetConfig(CONFIG_ELUNA_ENABLE_DEPRECATED); }
4852
bool ShouldMapLoadEluna(uint32 mapId);
4953

5054
private:

ElunaTemplate.h

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extern "C"
1616
#include "LuaEngine.h"
1717
#include "ElunaUtility.h"
1818
#include "ElunaCompat.h"
19+
#include "ElunaConfig.h"
1920
#if !defined ELUNA_CMANGOS
2021
#include "SharedDefines.h"
2122
#else
@@ -161,18 +162,19 @@ struct ElunaRegister
161162
const char* name;
162163
typename std::conditional<std::is_same_v<T, void>, int(*)(Eluna*), int(*)(Eluna*, T*)>::type mfunc;
163164
MethodRegisterState regState;
165+
MethodFlags flags;
164166

165-
// constructor for non-globals (with T*)
166-
ElunaRegister(const char* name, int(*func)(Eluna*, T*), MethodRegisterState state = METHOD_REG_ALL)
167-
: name(name), mfunc(func), regState(state) {}
167+
// constructor for class methods
168+
ElunaRegister(const char* name, int(*func)(Eluna*, T*), MethodRegisterState state = METHOD_REG_ALL, uint32 flags = METHOD_FLAG_NONE)
169+
: name(name), mfunc(func), regState(state), flags(static_cast<MethodFlags>(flags)) {}
168170

169-
// constructor for globals (without T*)
170-
ElunaRegister(const char* name, int(*func)(Eluna*), MethodRegisterState state = METHOD_REG_ALL)
171-
: name(name), mfunc(func), regState(state) {}
171+
// constructor for global methods
172+
ElunaRegister(const char* name, int(*func)(Eluna*), MethodRegisterState state = METHOD_REG_ALL, uint32 flags = METHOD_FLAG_NONE)
173+
: name(name), mfunc(func), regState(state), flags(static_cast<MethodFlags>(flags)) {}
172174

173-
// constructor for nullptr functions and METHOD_REG_NONE (unimplemented methods)
174-
ElunaRegister(const char* name, MethodRegisterState state = METHOD_REG_NONE)
175-
: name(name), mfunc(nullptr), regState(state) {}
175+
// constructor for unimplemented methods
176+
ElunaRegister(const char* name, MethodRegisterState state = METHOD_REG_NONE, uint32 flags = METHOD_FLAG_NONE)
177+
: name(name), mfunc(nullptr), regState(state), flags(static_cast<MethodFlags>(flags)) {}
176178
};
177179

178180
template<typename T = void>
@@ -324,6 +326,24 @@ class ElunaTemplate
324326
continue;
325327
}
326328

329+
// if the method is considered unsafe, and unsafe methods have not been enabled, push a closure to error output function
330+
if (method->flags & METHOD_FLAG_UNSAFE && !sElunaConfig->UnsafeMethodsEnabled())
331+
{
332+
lua_pushstring(L, method->name);
333+
lua_pushcclosure(L, MethodUnsafe, 1);
334+
lua_rawset(L, -3);
335+
continue;
336+
}
337+
338+
// if the method is considered deprecated, and deprecated methods have not been enabled, push a closure to error output function
339+
if (method->flags & METHOD_FLAG_DEPRECATED && !sElunaConfig->DeprecatedMethodsEnabled())
340+
{
341+
lua_pushstring(L, method->name);
342+
lua_pushcclosure(L, MethodDeprecated, 1);
343+
lua_rawset(L, -3);
344+
continue;
345+
}
346+
327347
// if we're in multistate mode, we need to check whether a method is flagged as a world or a map specific method
328348
if (method->regState != METHOD_REG_ALL)
329349
{
@@ -492,6 +512,8 @@ class ElunaTemplate
492512

493513
static int MethodWrongState(lua_State* L) { luaL_error(L, "attempt to call method '%s' that does not exist for state: %d", lua_tostring(L, lua_upvalueindex(1)), lua_tointeger(L, lua_upvalueindex(2))); return 0; }
494514
static int MethodUnimpl(lua_State* L) { luaL_error(L, "attempt to call method '%s' that is not implemented for this emulator", lua_tostring(L, lua_upvalueindex(1))); return 0; }
515+
static int MethodUnsafe(lua_State* L) { luaL_error(L, "attempt to call method '%s' that is flagged as unsafe! to use this method, enable unsafe methods in the config file", lua_tostring(L, lua_upvalueindex(1))); return 0; }
516+
static int MethodDeprecated(lua_State* L) { luaL_error(L, "attempt to call method '%s' that is flagged as deprecated! this method will be removed in the future. to use this method, enable deprecated methods in the config file", lua_tostring(L, lua_upvalueindex(1))); return 0; }
495517
};
496518

497519
template<typename T> const char* ElunaTemplate<T>::tname = NULL;

LuaEngine.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ enum MethodRegisterState
128128
METHOD_REG_ALL
129129
};
130130

131+
enum MethodFlags : uint32
132+
{
133+
METHOD_FLAG_NONE = 0x0,
134+
METHOD_FLAG_UNSAFE = 0x1,
135+
METHOD_FLAG_DEPRECATED = 0x2
136+
};
137+
131138
#define ELUNA_STATE_PTR "Eluna State Ptr"
132139

133140
#if defined ELUNA_TRINITY

methods/TrinityCore/GlobalMethods.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3158,13 +3158,13 @@ namespace LuaGlobalFunctions
31583158
{ "ReloadEluna", &LuaGlobalFunctions::ReloadEluna },
31593159
{ "RunCommand", &LuaGlobalFunctions::RunCommand },
31603160
{ "SendWorldMessage", &LuaGlobalFunctions::SendWorldMessage },
3161-
{ "WorldDBQuery", &LuaGlobalFunctions::WorldDBQuery },
3161+
{ "WorldDBQuery", &LuaGlobalFunctions::WorldDBQuery, METHOD_REG_ALL, METHOD_FLAG_UNSAFE },
31623162
{ "WorldDBExecute", &LuaGlobalFunctions::WorldDBExecute },
31633163
{ "WorldDBQueryAsync", &LuaGlobalFunctions::WorldDBQueryAsync },
3164-
{ "CharDBQuery", &LuaGlobalFunctions::CharDBQuery },
3164+
{ "CharDBQuery", &LuaGlobalFunctions::CharDBQuery, METHOD_REG_ALL, METHOD_FLAG_UNSAFE },
31653165
{ "CharDBExecute", &LuaGlobalFunctions::CharDBExecute },
31663166
{ "CharDBQueryAsync", &LuaGlobalFunctions::CharDBQueryAsync },
3167-
{ "AuthDBQuery", &LuaGlobalFunctions::AuthDBQuery },
3167+
{ "AuthDBQuery", &LuaGlobalFunctions::AuthDBQuery, METHOD_REG_ALL, METHOD_FLAG_UNSAFE },
31683168
{ "AuthDBExecute", &LuaGlobalFunctions::AuthDBExecute },
31693169
{ "AuthDBQueryAsync", &LuaGlobalFunctions::AuthDBQueryAsync },
31703170
{ "CreateLuaEvent", &LuaGlobalFunctions::CreateLuaEvent },

0 commit comments

Comments
 (0)