|
|
|
@ -22,6 +22,21 @@ namespace LuaUtil
|
|
|
|
|
std::string mName;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Callback Callback::fromLua(const sol::table& t)
|
|
|
|
|
{
|
|
|
|
|
return Callback{ t.raw_get<sol::main_protected_function>(1), t.raw_get<AsyncPackageId>(2).mHiddenData };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Callback::isLuaCallback(const sol::object& t)
|
|
|
|
|
{
|
|
|
|
|
if (!t.is<sol::table>())
|
|
|
|
|
return false;
|
|
|
|
|
sol::object meta = sol::table(t)[sol::metatable_key];
|
|
|
|
|
if (!meta.is<sol::table>())
|
|
|
|
|
return false;
|
|
|
|
|
return sol::table(meta).raw_get_or<bool, std::string_view, bool>("isCallback", false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sol::function getAsyncPackageInitializer(
|
|
|
|
|
lua_State* L, std::function<double()> simulationTimeFn, std::function<double()> gameTimeFn)
|
|
|
|
|
{
|
|
|
|
@ -53,13 +68,20 @@ namespace LuaUtil
|
|
|
|
|
asyncId.mContainer->setupUnsavableTimer(
|
|
|
|
|
TimerType::GAME_TIME, gameTimeFn() + delay, asyncId.mScriptId, std::move(callback));
|
|
|
|
|
};
|
|
|
|
|
api["callback"] = [](const AsyncPackageId& asyncId, sol::main_protected_function fn) -> Callback {
|
|
|
|
|
return Callback{ std::move(fn), asyncId.mHiddenData };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sol::usertype<Callback> callbackType = lua.new_usertype<Callback>("Callback");
|
|
|
|
|
callbackType[sol::meta_function::call]
|
|
|
|
|
= [](const Callback& callback, sol::variadic_args va) { return callback.call(sol::as_args(va)); };
|
|
|
|
|
sol::table callbackMeta = sol::table::create(L);
|
|
|
|
|
callbackMeta[sol::meta_function::call] = [](const sol::table& callback, sol::variadic_args va) {
|
|
|
|
|
return Callback::fromLua(callback).call(sol::as_args(va));
|
|
|
|
|
};
|
|
|
|
|
callbackMeta[sol::meta_function::to_string] = [] { return "Callback"; };
|
|
|
|
|
callbackMeta[sol::meta_function::metatable] = false;
|
|
|
|
|
callbackMeta["isCallback"] = true;
|
|
|
|
|
api["callback"] = [callbackMeta](const AsyncPackageId& asyncId, sol::main_protected_function fn) -> sol::table {
|
|
|
|
|
sol::table c = sol::table::create(fn.lua_state(), 2);
|
|
|
|
|
c.raw_set(1, std::move(fn), 2, asyncId);
|
|
|
|
|
c[sol::metatable_key] = callbackMeta;
|
|
|
|
|
return c;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto initializer = [](sol::table hiddenData) {
|
|
|
|
|
ScriptId id = hiddenData[ScriptsContainer::sScriptIdKey];
|
|
|
|
|