From be4dda36461e57c2ccef95f9a4a2c6dd53afc346 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 12 May 2026 02:29:48 +0300 Subject: [PATCH 1/5] add time.precise_time --- src/logic/scripting/lua/libs/libtime.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/logic/scripting/lua/libs/libtime.cpp b/src/logic/scripting/lua/libs/libtime.cpp index f953955be..342226035 100644 --- a/src/logic/scripting/lua/libs/libtime.cpp +++ b/src/logic/scripting/lua/libs/libtime.cpp @@ -62,11 +62,20 @@ static int l_utc_offset(lua::State* L) { return lua::pushnumber(L, offset); } +static auto start_time = std::chrono::steady_clock::now(); + +static int l_precise_time(lua::State* L) { + auto time = std::chrono::steady_clock::now(); + std::chrono::duration delta = time - start_time; + return lua::pushnumber(L, delta.count()); +} + const luaL_Reg timelib[] = { {"uptime", lua::wrap}, {"delta", lua::wrap}, {"utc_time", lua::wrap}, {"utc_offset", lua::wrap}, {"local_time", lua::wrap}, + {"precise_time", lua::wrap}, {nullptr, nullptr} }; From c7f35dd6be0fe8489f2182c8b536bc18932e09bc Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 12 May 2026 02:47:47 +0300 Subject: [PATCH 2/5] replace os.clock with preciser implementation --- src/logic/scripting/lua/lua_extensions.cpp | 12 ++++++++++++ src/util/platform.cpp | 18 ++++++++++++++++++ src/util/platform.hpp | 2 ++ 3 files changed, 32 insertions(+) diff --git a/src/logic/scripting/lua/lua_extensions.cpp b/src/logic/scripting/lua/lua_extensions.cpp index a7de3fdd8..f8a200de1 100644 --- a/src/logic/scripting/lua/lua_extensions.cpp +++ b/src/logic/scripting/lua/lua_extensions.cpp @@ -8,6 +8,7 @@ #include "engine/Engine.hpp" #include "devtools/DebuggingServer.hpp" #include "logic/scripting/scripting.hpp" +#include "util/platform.hpp" using namespace devtools; using namespace scripting; @@ -381,6 +382,10 @@ static int l_debug_is_debugging(lua::State* L) { return lua::pushboolean(L, engine->getDebuggingServer() != nullptr); } +static int l_os_clock(lua::State* L) { + return lua::pushnumber(L, platform::clock()); +} + void initialize_libs_extends(lua::State* L) { if (lua::getglobal(L, "debug")) { lua::pushcfunction(L, lua::wrap); @@ -415,4 +420,11 @@ void initialize_libs_extends(lua::State* L) { lua::pop(L); } + + if (lua::getglobal(L, "os")) { + lua::pushcfunction(L, lua::wrap); + lua::setfield(L, "clock"); + + lua::pop(L); + } } diff --git a/src/util/platform.cpp b/src/util/platform.cpp index f113641e5..d2df3e77e 100644 --- a/src/util/platform.cpp +++ b/src/util/platform.cpp @@ -327,3 +327,21 @@ bool platform::stdin_has_data() { return poll(&fds, 1, 0) == 1; #endif } + +double platform::clock() { +#ifdef _WIN32 + FILETIME createTime, exitTime, kernelTime, userTime; + + if (GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime, &userTime)) { + ULARGE_INTEGER kernel, user; + kernel.LowPart = kernelTime.dwLowDateTime; + kernel.HighPart = kernelTime.dwHighDateTime; + user.LowPart = userTime.dwLowDateTime; + user.HighPart = userTime.dwHighDateTime; + + return static_cast(kernel.QuadPart + user.QuadPart) / 1000'0000.0; + } +#else + return std::clock() / static_cast(CLOCKS_PER_SEC); +#endif +} diff --git a/src/util/platform.hpp b/src/util/platform.hpp index 54c56a0c5..4c049a0da 100644 --- a/src/util/platform.hpp +++ b/src/util/platform.hpp @@ -25,4 +25,6 @@ namespace platform { bool open_url(const std::string& url); /// @brief Check if stdin has input bool stdin_has_data(); + /// @brief Precise wrapper for std::clock (especially for Windows) + double clock(); } From bcbf09c91642359588d77297e9948fa83d073946 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 12 May 2026 03:03:04 +0300 Subject: [PATCH 3/5] update platform::clock --- src/util/platform.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/util/platform.cpp b/src/util/platform.cpp index d2df3e77e..026c6feb4 100644 --- a/src/util/platform.cpp +++ b/src/util/platform.cpp @@ -331,8 +331,10 @@ bool platform::stdin_has_data() { double platform::clock() { #ifdef _WIN32 FILETIME createTime, exitTime, kernelTime, userTime; + + static HANDLE currentProcess = GetCurrentProcess(); - if (GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime, &userTime)) { + if (GetProcessTimes(currentProcess, &createTime, &exitTime, &kernelTime, &userTime)) { ULARGE_INTEGER kernel, user; kernel.LowPart = kernelTime.dwLowDateTime; kernel.HighPart = kernelTime.dwHighDateTime; From 7c2b7bd8139eec83f994ffb842a90165415e2083 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 12 May 2026 03:51:58 +0300 Subject: [PATCH 4/5] revert os.clock changes --- src/logic/scripting/lua/lua_extensions.cpp | 12 ------------ src/util/platform.cpp | 20 -------------------- src/util/platform.hpp | 2 -- 3 files changed, 34 deletions(-) diff --git a/src/logic/scripting/lua/lua_extensions.cpp b/src/logic/scripting/lua/lua_extensions.cpp index f8a200de1..a7de3fdd8 100644 --- a/src/logic/scripting/lua/lua_extensions.cpp +++ b/src/logic/scripting/lua/lua_extensions.cpp @@ -8,7 +8,6 @@ #include "engine/Engine.hpp" #include "devtools/DebuggingServer.hpp" #include "logic/scripting/scripting.hpp" -#include "util/platform.hpp" using namespace devtools; using namespace scripting; @@ -382,10 +381,6 @@ static int l_debug_is_debugging(lua::State* L) { return lua::pushboolean(L, engine->getDebuggingServer() != nullptr); } -static int l_os_clock(lua::State* L) { - return lua::pushnumber(L, platform::clock()); -} - void initialize_libs_extends(lua::State* L) { if (lua::getglobal(L, "debug")) { lua::pushcfunction(L, lua::wrap); @@ -420,11 +415,4 @@ void initialize_libs_extends(lua::State* L) { lua::pop(L); } - - if (lua::getglobal(L, "os")) { - lua::pushcfunction(L, lua::wrap); - lua::setfield(L, "clock"); - - lua::pop(L); - } } diff --git a/src/util/platform.cpp b/src/util/platform.cpp index 026c6feb4..f113641e5 100644 --- a/src/util/platform.cpp +++ b/src/util/platform.cpp @@ -327,23 +327,3 @@ bool platform::stdin_has_data() { return poll(&fds, 1, 0) == 1; #endif } - -double platform::clock() { -#ifdef _WIN32 - FILETIME createTime, exitTime, kernelTime, userTime; - - static HANDLE currentProcess = GetCurrentProcess(); - - if (GetProcessTimes(currentProcess, &createTime, &exitTime, &kernelTime, &userTime)) { - ULARGE_INTEGER kernel, user; - kernel.LowPart = kernelTime.dwLowDateTime; - kernel.HighPart = kernelTime.dwHighDateTime; - user.LowPart = userTime.dwLowDateTime; - user.HighPart = userTime.dwHighDateTime; - - return static_cast(kernel.QuadPart + user.QuadPart) / 1000'0000.0; - } -#else - return std::clock() / static_cast(CLOCKS_PER_SEC); -#endif -} diff --git a/src/util/platform.hpp b/src/util/platform.hpp index 4c049a0da..54c56a0c5 100644 --- a/src/util/platform.hpp +++ b/src/util/platform.hpp @@ -25,6 +25,4 @@ namespace platform { bool open_url(const std::string& url); /// @brief Check if stdin has input bool stdin_has_data(); - /// @brief Precise wrapper for std::clock (especially for Windows) - double clock(); } From 186889624371eac86a5fedfb6e15f3f2cc43be65 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 12 May 2026 04:00:10 +0300 Subject: [PATCH 5/5] update doc/*/scripting/builtins/libtime.md --- doc/en/scripting/builtins/libtime.md | 30 ++++++++++++++++++---------- doc/ru/scripting/builtins/libtime.md | 3 +++ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/doc/en/scripting/builtins/libtime.md b/doc/en/scripting/builtins/libtime.md index 8fcd4035f..51e125e9c 100644 --- a/doc/en/scripting/builtins/libtime.md +++ b/doc/en/scripting/builtins/libtime.md @@ -1,13 +1,21 @@ -# *time* library +# *time* Library -```python -time.uptime() -> float +```lua +-- Returns the time since engine startup in seconds and milliseconds. +time.uptime() -> number + +-- Returns the time delta in seconds and milliseconds (time elapsed since the previous frame) +time.delta() -> number + +-- Returns UTC time in seconds +time.utc_time() -> int + +-- Returns local (system) time in seconds +time.local_time() -> int + +-- Returns the local time offset from UTC in seconds +time.utc_offset() -> int + +-- Returns high-precision time in seconds (elapsed since the engine startup). +time.precise_time() -> number ``` - -Returns time elapsed since the engine started. - -```python -time.delta() -> float -``` - -Returns time elapsed since the last frame. diff --git a/doc/ru/scripting/builtins/libtime.md b/doc/ru/scripting/builtins/libtime.md index 81bae58f7..62e95892a 100644 --- a/doc/ru/scripting/builtins/libtime.md +++ b/doc/ru/scripting/builtins/libtime.md @@ -15,4 +15,7 @@ time.local_time() -> int -- Возвращает смещение локального времени от UTC в секундах time.utc_offset() -> int + +-- Возвращает время высокой точности в секундах. За точку отсчёта берётся запуск движка. +time.precise_time() -> number ```