From b0c467f97194a479d7408acfb57830d7b0dff5f4 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Fri, 17 Sep 2021 01:25:52 +0200 Subject: [PATCH] Lua: Add timer --- src/TLuaEngine.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index db11755..9efbc15 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -229,6 +229,20 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi StateView.set_function("print", &LuaAPI::Print); StateView.set_function("exit", &Application::GracefullyShutdown); auto Table = StateView.create_named_table("MP"); + Table.set_function("CreateTimer", [&]() -> sol::table { + sol::state_view StateView(mState); + sol::table Result = StateView.create_table(); + Result["__StartTime"] = std::chrono::high_resolution_clock::now(); + Result.set_function("GetCurrent", [&](const sol::table& Table) -> float { + auto End = std::chrono::high_resolution_clock::now(); + auto Start = Table.get("__StartTime"); + return std::chrono::duration_cast(End - Start).count() / 1000000.0f; + }); + Result.set_function("Start", [&](sol::table Table) { + Table["__StartTime"] = std::chrono::high_resolution_clock::now(); + }); + return Result; + }); Table.set_function("GetOSName", &LuaAPI::MP::GetOSName); Table.set_function("GetServerVersion", &LuaAPI::MP::GetServerVersion); Table.set_function("RegisterEvent", [this](const std::string& EventName, const std::string& FunctionName) { @@ -252,9 +266,6 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi }); Table.set_function("IsPlayerGuest", &LuaAPI::MP::IsPlayerGuest); Table.set_function("DropPlayer", &LuaAPI::MP::DropPlayer); - Table.set_function("GetOSTimeMS", []() -> size_t { - return std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); - }); Table.set_function("GetPlayerIdentifiers", [&](int ID) -> sol::table { return Lua_GetPlayerIdentifiers(ID); });