Skip to content

Commit

Permalink
feat(citizen-scripting-lua): ClearTimeout function
Browse files Browse the repository at this point in the history
  • Loading branch information
poco8537 committed Jan 25, 2025
1 parent 0d108a3 commit 14c347f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
10 changes: 10 additions & 0 deletions code/components/citizen-scripting-lua/include/LuaScriptRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,16 @@ class LuaScriptRuntime : public OMClass<LuaScriptRuntime, IScriptRuntime, IScrip

lua_State* GetRunningThread();

lua_State* GetState()
{
return m_state.Get();
}

auto& GetPendingBookmarks()
{
return m_pendingBookmarks;
}

/// <summary>
/// Manage the fx::ProfilerComponent state while the script runtime is active
///
Expand Down
30 changes: 29 additions & 1 deletion code/components/citizen-scripting-lua/src/LuaScriptRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,8 @@ static int Lua_CreateThreadInternal(lua_State* L, bool now, int timeout, int fun
if (!now)
{
luaRuntime->ScheduleBookmarkSoon(ref, -timeout);
return 0;
lua_pushinteger(L, ref);
return 1;
}
else
{
Expand Down Expand Up @@ -1151,6 +1152,32 @@ static int Lua_SetTimeout(lua_State* L)
return Lua_CreateThreadInternal(L, false, timeout, 2);
}

static int Lua_ClearTimeout(lua_State* L)
{
int bookmark = luaL_checkinteger(L, 1);

auto& luaRuntime = fx::LuaScriptRuntime::GetCurrent();
lua_State* runtimeState = luaRuntime->GetState();

bool removed = false;

auto& pending = luaRuntime->GetPendingBookmarks();
for (auto it = pending.begin(); it != pending.end(); ++it)
{
if (std::get<0>(*it) == static_cast<uint64_t>(bookmark))
{
pending.erase(it);

luaL_unref(runtimeState, LUA_REGISTRYINDEX, bookmark);
removed = true;
break;
}
}

lua_pushboolean(L, removed);
return 1;
}

static int Lua_Noop(lua_State* L)
{
return 0;
Expand All @@ -1165,6 +1192,7 @@ static const struct luaL_Reg g_citizenLib[] = {
{ "CreateThreadNow", Lua_CreateThreadNow },
{ "Wait", Lua_Wait },
{ "SetTimeout", Lua_SetTimeout },
{ "ClearTimeout", Lua_ClearTimeout },
{ "InvokeNative", Lua_InvokeNative },
#ifndef IS_FXSERVER
{ "GetNative", Lua_GetNativeHandler },
Expand Down
1 change: 1 addition & 0 deletions data/shared/citizen/scripting/lua/scheduler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ end)
Wait = Citizen.Wait
CreateThread = Citizen.CreateThread
SetTimeout = Citizen.SetTimeout
ClearTimeout = Citizen.ClearTimeout

--[[
Expand Down

0 comments on commit 14c347f

Please sign in to comment.