Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions ElunaConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ void ElunaConfig::Initialize()
SetConfig(CONFIG_ELUNA_ENABLED, "Eluna.Enabled", true);
SetConfig(CONFIG_ELUNA_TRACEBACK, "Eluna.TraceBack", false);
SetConfig(CONFIG_ELUNA_SCRIPT_RELOADER, "Eluna.ScriptReloader", false);
SetConfig(CONFIG_ELUNA_ENABLE_UNSAFE, "Eluna.UseUnsafeMethods", true);
SetConfig(CONFIG_ELUNA_ENABLE_DEPRECATED, "Eluna.UseDeprecatedMethods", true);

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

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

bool ElunaConfig::ShouldMapLoadEluna(uint32 id)
{
// if the set is empty (all maps), return true
Expand Down
6 changes: 5 additions & 1 deletion ElunaConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ enum ElunaConfigBoolValues
CONFIG_ELUNA_ENABLED,
CONFIG_ELUNA_TRACEBACK,
CONFIG_ELUNA_SCRIPT_RELOADER,
CONFIG_ELUNA_ENABLE_UNSAFE,
CONFIG_ELUNA_ENABLE_DEPRECATED,
CONFIG_ELUNA_BOOL_COUNT
};

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

bool IsElunaEnabled();
bool IsElunaEnabled() { return GetConfig(CONFIG_ELUNA_ENABLED); }
bool UnsafeMethodsEnabled() { return GetConfig(CONFIG_ELUNA_ENABLE_UNSAFE); }
bool DeprecatedMethodsEnabled() { return GetConfig(CONFIG_ELUNA_ENABLE_DEPRECATED); }
bool ShouldMapLoadEluna(uint32 mapId);

private:
Expand Down
40 changes: 31 additions & 9 deletions ElunaTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extern "C"
#include "LuaEngine.h"
#include "ElunaUtility.h"
#include "ElunaCompat.h"
#include "ElunaConfig.h"
#if !defined ELUNA_CMANGOS
#include "SharedDefines.h"
#else
Expand Down Expand Up @@ -161,18 +162,19 @@ struct ElunaRegister
const char* name;
typename std::conditional<std::is_same_v<T, void>, int(*)(Eluna*), int(*)(Eluna*, T*)>::type mfunc;
MethodRegisterState regState;
MethodFlags flags;

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

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

// constructor for nullptr functions and METHOD_REG_NONE (unimplemented methods)
ElunaRegister(const char* name, MethodRegisterState state = METHOD_REG_NONE)
: name(name), mfunc(nullptr), regState(state) {}
// constructor for unimplemented methods
ElunaRegister(const char* name, MethodRegisterState state = METHOD_REG_NONE, uint32 flags = METHOD_FLAG_NONE)
: name(name), mfunc(nullptr), regState(state), flags(static_cast<MethodFlags>(flags)) {}
};

template<typename T = void>
Expand Down Expand Up @@ -324,6 +326,24 @@ class ElunaTemplate
continue;
}

// if the method is considered unsafe, and unsafe methods have not been enabled, push a closure to error output function
if (method->flags & METHOD_FLAG_UNSAFE && !sElunaConfig->UnsafeMethodsEnabled())
{
lua_pushstring(L, method->name);
lua_pushcclosure(L, MethodUnsafe, 1);
lua_rawset(L, -3);
continue;
}

// if the method is considered deprecated, and deprecated methods have not been enabled, push a closure to error output function
if (method->flags & METHOD_FLAG_DEPRECATED && !sElunaConfig->DeprecatedMethodsEnabled())
{
lua_pushstring(L, method->name);
lua_pushcclosure(L, MethodDeprecated, 1);
lua_rawset(L, -3);
continue;
}

// if we're in multistate mode, we need to check whether a method is flagged as a world or a map specific method
if (method->regState != METHOD_REG_ALL)
{
Expand Down Expand Up @@ -492,6 +512,8 @@ class ElunaTemplate

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; }
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; }
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; }
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; }
};

template<typename T> const char* ElunaTemplate<T>::tname = NULL;
Expand Down
7 changes: 7 additions & 0 deletions LuaEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ enum MethodRegisterState
METHOD_REG_ALL
};

enum MethodFlags : uint32
{
METHOD_FLAG_NONE = 0x0,
METHOD_FLAG_UNSAFE = 0x1,
METHOD_FLAG_DEPRECATED = 0x2
};

#define ELUNA_STATE_PTR "Eluna State Ptr"

#if defined ELUNA_TRINITY
Expand Down
6 changes: 3 additions & 3 deletions methods/TrinityCore/GlobalMethods.h
Original file line number Diff line number Diff line change
Expand Up @@ -3158,13 +3158,13 @@ namespace LuaGlobalFunctions
{ "ReloadEluna", &LuaGlobalFunctions::ReloadEluna },
{ "RunCommand", &LuaGlobalFunctions::RunCommand },
{ "SendWorldMessage", &LuaGlobalFunctions::SendWorldMessage },
{ "WorldDBQuery", &LuaGlobalFunctions::WorldDBQuery },
{ "WorldDBQuery", &LuaGlobalFunctions::WorldDBQuery, METHOD_REG_ALL, METHOD_FLAG_UNSAFE },
{ "WorldDBExecute", &LuaGlobalFunctions::WorldDBExecute },
{ "WorldDBQueryAsync", &LuaGlobalFunctions::WorldDBQueryAsync },
{ "CharDBQuery", &LuaGlobalFunctions::CharDBQuery },
{ "CharDBQuery", &LuaGlobalFunctions::CharDBQuery, METHOD_REG_ALL, METHOD_FLAG_UNSAFE },
{ "CharDBExecute", &LuaGlobalFunctions::CharDBExecute },
{ "CharDBQueryAsync", &LuaGlobalFunctions::CharDBQueryAsync },
{ "AuthDBQuery", &LuaGlobalFunctions::AuthDBQuery },
{ "AuthDBQuery", &LuaGlobalFunctions::AuthDBQuery, METHOD_REG_ALL, METHOD_FLAG_UNSAFE },
{ "AuthDBExecute", &LuaGlobalFunctions::AuthDBExecute },
{ "AuthDBQueryAsync", &LuaGlobalFunctions::AuthDBQueryAsync },
{ "CreateLuaEvent", &LuaGlobalFunctions::CreateLuaEvent },
Expand Down
Loading