2021-03-12 18:29:51 +01:00
|
|
|
#include "luabindings.hpp"
|
|
|
|
|
2021-08-27 09:26:38 +02:00
|
|
|
#include "luamanagerimp.hpp"
|
|
|
|
|
2021-03-12 18:29:51 +01:00
|
|
|
namespace sol
|
|
|
|
{
|
|
|
|
template <>
|
|
|
|
struct is_automagical<MWLua::AsyncPackageId> : std::false_type {};
|
2022-05-15 21:30:08 +02:00
|
|
|
|
|
|
|
template <>
|
|
|
|
struct is_automagical<LuaUtil::Callback> : std::false_type {};
|
2021-03-12 18:29:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace MWLua
|
|
|
|
{
|
|
|
|
|
|
|
|
struct TimerCallback
|
|
|
|
{
|
|
|
|
AsyncPackageId mAsyncId;
|
|
|
|
std::string mName;
|
|
|
|
};
|
|
|
|
|
|
|
|
sol::function getAsyncPackageInitializer(const Context& context)
|
|
|
|
{
|
2021-12-01 21:28:05 +01:00
|
|
|
using TimerType = LuaUtil::ScriptsContainer::TimerType;
|
2021-03-12 18:29:51 +01:00
|
|
|
sol::usertype<AsyncPackageId> api = context.mLua->sol().new_usertype<AsyncPackageId>("AsyncPackage");
|
|
|
|
api["registerTimerCallback"] = [](const AsyncPackageId& asyncId, std::string_view name, sol::function callback)
|
|
|
|
{
|
2021-09-25 10:46:47 +02:00
|
|
|
asyncId.mContainer->registerTimerCallback(asyncId.mScriptId, name, std::move(callback));
|
2021-03-12 18:29:51 +01:00
|
|
|
return TimerCallback{asyncId, std::string(name)};
|
|
|
|
};
|
2021-12-01 21:28:05 +01:00
|
|
|
api["newSimulationTimer"] = [world=context.mWorldView](const AsyncPackageId&, double delay,
|
|
|
|
const TimerCallback& callback, sol::object callbackArg)
|
2021-03-12 18:29:51 +01:00
|
|
|
{
|
|
|
|
callback.mAsyncId.mContainer->setupSerializableTimer(
|
2021-12-01 21:28:05 +01:00
|
|
|
TimerType::SIMULATION_TIME, world->getSimulationTime() + delay,
|
2021-09-25 10:46:47 +02:00
|
|
|
callback.mAsyncId.mScriptId, callback.mName, std::move(callbackArg));
|
2021-03-12 18:29:51 +01:00
|
|
|
};
|
2021-12-01 21:28:05 +01:00
|
|
|
api["newGameTimer"] = [world=context.mWorldView](const AsyncPackageId&, double delay,
|
|
|
|
const TimerCallback& callback, sol::object callbackArg)
|
2021-03-12 18:29:51 +01:00
|
|
|
{
|
|
|
|
callback.mAsyncId.mContainer->setupSerializableTimer(
|
2021-12-01 21:28:05 +01:00
|
|
|
TimerType::GAME_TIME, world->getGameTime() + delay,
|
2021-09-25 10:46:47 +02:00
|
|
|
callback.mAsyncId.mScriptId, callback.mName, std::move(callbackArg));
|
2021-03-12 18:29:51 +01:00
|
|
|
};
|
2021-12-01 21:28:05 +01:00
|
|
|
api["newUnsavableSimulationTimer"] = [world=context.mWorldView](const AsyncPackageId& asyncId, double delay, sol::function callback)
|
2021-03-12 18:29:51 +01:00
|
|
|
{
|
2021-04-23 02:49:12 +02:00
|
|
|
asyncId.mContainer->setupUnsavableTimer(
|
2021-12-01 21:28:05 +01:00
|
|
|
TimerType::SIMULATION_TIME, world->getSimulationTime() + delay, asyncId.mScriptId, std::move(callback));
|
2021-03-12 18:29:51 +01:00
|
|
|
};
|
2021-12-01 21:28:05 +01:00
|
|
|
api["newUnsavableGameTimer"] = [world=context.mWorldView](const AsyncPackageId& asyncId, double delay, sol::function callback)
|
2021-03-12 18:29:51 +01:00
|
|
|
{
|
2021-04-23 02:49:12 +02:00
|
|
|
asyncId.mContainer->setupUnsavableTimer(
|
2021-12-01 21:28:05 +01:00
|
|
|
TimerType::GAME_TIME, world->getGameTime() + delay, asyncId.mScriptId, std::move(callback));
|
2021-03-12 18:29:51 +01:00
|
|
|
};
|
2022-05-15 21:30:08 +02:00
|
|
|
api["callback"] = [](const AsyncPackageId& asyncId, sol::function fn) -> LuaUtil::Callback
|
2021-08-27 09:26:38 +02:00
|
|
|
{
|
2021-10-31 17:38:06 +01:00
|
|
|
return LuaUtil::Callback{std::move(fn), asyncId.mHiddenData};
|
2021-08-27 09:26:38 +02:00
|
|
|
};
|
2021-03-12 18:29:51 +01:00
|
|
|
|
2022-05-15 21:30:08 +02:00
|
|
|
sol::usertype<LuaUtil::Callback> callbackType = context.mLua->sol().new_usertype<LuaUtil::Callback>("Callback");
|
|
|
|
callbackType[sol::meta_function::call] =
|
|
|
|
[](const LuaUtil::Callback& callback, sol::variadic_args va) { return callback.call(sol::as_args(va)); };
|
|
|
|
|
2021-03-12 18:29:51 +01:00
|
|
|
auto initializer = [](sol::table hiddenData)
|
|
|
|
{
|
2021-10-31 17:38:06 +01:00
|
|
|
LuaUtil::ScriptsContainer::ScriptId id = hiddenData[LuaUtil::ScriptsContainer::sScriptIdKey];
|
2021-09-25 10:46:47 +02:00
|
|
|
return AsyncPackageId{id.mContainer, id.mIndex, hiddenData};
|
2021-03-12 18:29:51 +01:00
|
|
|
};
|
|
|
|
return sol::make_object(context.mLua->sol(), initializer);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|