[Server] Disallow non void callbacks

experimental-mono
Koncord 6 years ago
parent 07a5f5296c
commit e162af0003

@ -62,49 +62,12 @@ public:
return callbacks[N].index == I ? callbacks[N] : CallBackData(I, N + 1); return callbacks[N].index == I ? callbacks[N] : CallBackData(I, N + 1);
} }
template<unsigned int I>
using CallBackReturn = typename CharType<CallBackData(I).callback.ret>::type;
template<size_t N> template<size_t N>
static constexpr unsigned int CallbackIdentity(const char(&str)[N]) static constexpr unsigned int CallbackIdentity(const char(&str)[N])
{ {
return Utils::hash(str); return Utils::hash(str);
} }
template<unsigned int I, bool B = false, typename... Args>
static unsigned int Call(CallBackReturn<I>& result, Args&&... args) {
constexpr ScriptCallbackData const& data = CallBackData(I);
static_assert(data.callback.matches(TypeString<typename std::remove_reference<Args>::type...>::value),
"Wrong number or types of arguments");
unsigned int count = 0;
for (auto& script : scripts)
{
if (!script->callbacks_.count(I))
script->callbacks_.emplace(I, script->GetScript<FunctionEllipsis<void>>(data.name));
auto callback = script->callbacks_[I];
if (!callback)
continue;
if (script->script_type == SCRIPT_CPP)
result = reinterpret_cast<FunctionEllipsis<CallBackReturn<I>>>(callback)(std::forward<Args>(args)...);
#if defined (ENABLE_LUA)
else if (script->script_type == SCRIPT_LUA)
{
boost::any any = script->lang->Call(data.name, data.callback.types, B, std::forward<Args>(args)...);
result = static_cast<CallBackReturn<I>>(boost::any_cast<luabridge::LuaRef>(any).cast<CallBackReturn<I>>());
}
#endif
++count;
}
return count;
}
template<unsigned int I, bool B = false, typename... Args> template<unsigned int I, bool B = false, typename... Args>
static unsigned int Call(Args&&... args) { static unsigned int Call(Args&&... args) {
constexpr ScriptCallbackData const& data = CallBackData(I); constexpr ScriptCallbackData const& data = CallBackData(I);
@ -124,7 +87,7 @@ public:
continue; continue;
if (script->script_type == SCRIPT_CPP) if (script->script_type == SCRIPT_CPP)
reinterpret_cast<FunctionEllipsis<CallBackReturn<I>>>(callback)(std::forward<Args>(args)...); (callback)(std::forward<Args>(args)...);
#if defined (ENABLE_LUA) #if defined (ENABLE_LUA)
else if (script->script_type == SCRIPT_LUA) else if (script->script_type == SCRIPT_LUA)
script->lang->Call(data.name, data.callback.types, B, std::forward<Args>(args)...); script->lang->Call(data.name, data.callback.types, B, std::forward<Args>(args)...);

@ -150,62 +150,62 @@ public:
}; };
static constexpr ScriptCallbackData callbacks[]{ static constexpr ScriptCallbackData callbacks[]{
{"OnServerInit", Function<void>()}, {"OnServerInit", Callback<>()},
{"OnServerPostInit", Function<void>()}, {"OnServerPostInit", Callback<>()},
{"OnServerExit", Function<void, bool>()}, {"OnServerExit", Callback<bool>()},
{"OnPlayerConnect", Function<void, unsigned short>()}, {"OnPlayerConnect", Callback<unsigned short>()},
{"OnPlayerDisconnect", Function<void, unsigned short>()}, {"OnPlayerDisconnect", Callback<unsigned short>()},
{"OnPlayerDeath", Function<void, unsigned short>()}, {"OnPlayerDeath", Callback<unsigned short>()},
{"OnPlayerResurrect", Function<void, unsigned short>()}, {"OnPlayerResurrect", Callback<unsigned short>()},
{"OnPlayerCellChange", Function<void, unsigned short>()}, {"OnPlayerCellChange", Callback<unsigned short>()},
{"OnPlayerAttribute", Function<void, unsigned short>()}, {"OnPlayerAttribute", Callback<unsigned short>()},
{"OnPlayerSkill", Function<void, unsigned short>()}, {"OnPlayerSkill", Callback<unsigned short>()},
{"OnPlayerLevel", Function<void, unsigned short>()}, {"OnPlayerLevel", Callback<unsigned short>()},
{"OnPlayerBounty", Function<void, unsigned short>()}, {"OnPlayerBounty", Callback<unsigned short>()},
{"OnPlayerReputation", Function<void, unsigned short>()}, {"OnPlayerReputation", Callback<unsigned short>()},
{"OnPlayerEquipment", Function<void, unsigned short>()}, {"OnPlayerEquipment", Callback<unsigned short>()},
{"OnPlayerInventory", Function<void, unsigned short>()}, {"OnPlayerInventory", Callback<unsigned short>()},
{"OnPlayerJournal", Function<void, unsigned short>()}, {"OnPlayerJournal", Callback<unsigned short>()},
{"OnPlayerFaction", Function<void, unsigned short>()}, {"OnPlayerFaction", Callback<unsigned short>()},
{"OnPlayerShapeshift", Function<void, unsigned short>()}, {"OnPlayerShapeshift", Callback<unsigned short>()},
{"OnPlayerSpellbook", Function<void, unsigned short>()}, {"OnPlayerSpellbook", Callback<unsigned short>()},
{"OnPlayerQuickKeys", Function<void, unsigned short>()}, {"OnPlayerQuickKeys", Callback<unsigned short>()},
{"OnPlayerTopic", Function<void, unsigned short>()}, {"OnPlayerTopic", Callback<unsigned short>()},
{"OnPlayerDisposition", Function<void, unsigned short>()}, {"OnPlayerDisposition", Callback<unsigned short>()},
{"OnPlayerBook", Function<void, unsigned short>()}, {"OnPlayerBook", Callback<unsigned short>()},
{"OnPlayerItemUse", Function<void, unsigned short>()}, {"OnPlayerItemUse", Callback<unsigned short>()},
{"OnPlayerMiscellaneous", Function<void, unsigned short>()}, {"OnPlayerMiscellaneous", Callback<unsigned short>()},
{"OnPlayerInput", Function<void, unsigned short>()}, {"OnPlayerInput", Callback<unsigned short>()},
{"OnPlayerRest", Function<void, unsigned short>()}, {"OnPlayerRest", Callback<unsigned short>()},
{"OnRecordDynamic", Function<void, unsigned short>()}, {"OnRecordDynamic", Callback<unsigned short>()},
{"OnCellLoad", Function<void, unsigned short, const char*>()}, {"OnCellLoad", Callback<unsigned short, const char*>()},
{"OnCellUnload", Function<void, unsigned short, const char*>()}, {"OnCellUnload", Callback<unsigned short, const char*>()},
{"OnCellDeletion", Function<void, const char*>()}, {"OnCellDeletion", Callback<const char*>()},
{"OnContainer", Function<void, unsigned short, const char*>()}, {"OnContainer", Callback<unsigned short, const char*>()},
{"OnDoorState", Function<void, unsigned short, const char*>()}, {"OnDoorState", Callback<unsigned short, const char*>()},
{"OnObjectActivate", Function<void, unsigned short, const char*>()}, {"OnObjectActivate", Callback<unsigned short, const char*>()},
{"OnObjectPlace", Function<void, unsigned short, const char*>()}, {"OnObjectPlace", Callback<unsigned short, const char*>()},
{"OnObjectState", Function<void, unsigned short, const char*>()}, {"OnObjectState", Callback<unsigned short, const char*>()},
{"OnObjectSpawn", Function<void, unsigned short, const char*>()}, {"OnObjectSpawn", Callback<unsigned short, const char*>()},
{"OnObjectDelete", Function<void, unsigned short, const char*>()}, {"OnObjectDelete", Callback<unsigned short, const char*>()},
{"OnObjectLock", Function<void, unsigned short, const char*>()}, {"OnObjectLock", Callback<unsigned short, const char*>()},
{"OnObjectScale", Function<void, unsigned short, const char*>()}, {"OnObjectScale", Callback<unsigned short, const char*>()},
{"OnObjectTrap", Function<void, unsigned short, const char*>()}, {"OnObjectTrap", Callback<unsigned short, const char*>()},
{"OnVideoPlay", Function<void, unsigned short, const char*>()}, {"OnVideoPlay", Callback<unsigned short, const char*>()},
{"OnActorList", Function<void, unsigned short, const char*>()}, {"OnActorList", Callback<unsigned short, const char*>()},
{"OnActorEquipment", Function<void, unsigned short, const char*>()}, {"OnActorEquipment", Callback<unsigned short, const char*>()},
{"OnActorAI", Function<void, unsigned short, const char*>()}, {"OnActorAI", Callback<unsigned short, const char*>()},
{"OnActorDeath", Function<void, unsigned short, const char*>()}, {"OnActorDeath", Callback<unsigned short, const char*>()},
{"OnActorCellChange", Function<void, unsigned short, const char*>()}, {"OnActorCellChange", Callback<unsigned short, const char*>()},
{"OnActorTest", Function<void, unsigned short, const char*>()}, {"OnActorTest", Callback<unsigned short, const char*>()},
{"OnPlayerSendMessage", Function<void, unsigned short, const char*>()}, {"OnPlayerSendMessage", Callback<unsigned short, const char*>()},
{"OnPlayerEndCharGen", Function<void, unsigned short>()}, {"OnPlayerEndCharGen", Callback<unsigned short>()},
{"OnGUIAction", Function<void, unsigned short, int, const char*>()}, {"OnGUIAction", Callback<unsigned short, int, const char*>()},
{"OnWorldKillCount", Function<void, unsigned short>()}, {"OnWorldKillCount", Callback<unsigned short>()},
{"OnWorldMap", Function<void, unsigned short>()}, {"OnWorldMap", Callback<unsigned short>()},
{"OnWorldWeather", Function<void, unsigned short>() }, {"OnWorldWeather", Callback<unsigned short>() },
{"OnMpNumIncrement", Function<void, int>()}, {"OnMpNumIncrement", Callback<int>()},
{"OnRequestPluginList", Function<void>()} {"OnRequestPluginList", Callback<>()}
}; };
}; };

@ -82,6 +82,22 @@ struct ScriptIdentity
constexpr ScriptIdentity(Function<R, Types...>) : types(TypeString<Types...>::value), ret(TypeChar<R, sizeof_void<R>::value>::value), numargs(sizeof(TypeString<Types...>::value) - 1) {} constexpr ScriptIdentity(Function<R, Types...>) : types(TypeString<Types...>::value), ret(TypeChar<R, sizeof_void<R>::value>::value), numargs(sizeof(TypeString<Types...>::value) - 1) {}
}; };
template<typename... Types>
using Callback = void (*)(Types...);
struct CallbackIdentity
{
const char* types;
const unsigned int numargs;
constexpr bool matches(const char* types, const unsigned int N = 0) const
{
return N < numargs ? this->types[N] == types[N] && matches(types, N + 1) : this->types[N] == types[N];
}
template<typename... Types>
constexpr CallbackIdentity(Callback<Types...>) : types(TypeString<Types...>::value), numargs(sizeof(TypeString<Types...>::value) - 1) {}
};
struct ScriptFunctionPointer : public ScriptIdentity struct ScriptFunctionPointer : public ScriptIdentity
@ -104,10 +120,10 @@ struct ScriptCallbackData
{ {
const char* name; const char* name;
const unsigned int index; const unsigned int index;
const ScriptIdentity callback; const CallbackIdentity callback;
template<size_t N> template<size_t N>
constexpr ScriptCallbackData(const char(&name)[N], ScriptIdentity _callback) : name(name), index(Utils::hash(name)), callback(_callback) {} constexpr ScriptCallbackData(const char(&name)[N], CallbackIdentity _callback) : name(name), index(Utils::hash(name)), callback(_callback) {}
}; };
#endif //TMPTYPES_HPP #endif //TMPTYPES_HPP

Loading…
Cancel
Save