Skip to content

fix(citizen-scripting-core): fixed ClearTimeout after Wait #3260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ interface IScriptHostWithBookmarks : fxIBase

void RemoveBookmarks(in IScriptTickRuntimeWithBookmarks runtime);

void RemoveBookmark(in IScriptTickRuntimeWithBookmarks runtime, in uint64_t bookmark);

void CreateBookmarks(in IScriptTickRuntimeWithBookmarks runtime);
};

Expand Down
35 changes: 35 additions & 0 deletions code/components/citizen-scripting-core/src/ScriptHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,34 @@ static void RemoveBookmarks(IScriptTickRuntimeWithBookmarks* scRT)
bookmarkRefs.resourceInsertionIterators.erase(scRT);
}

static void RemoveBookmark(IScriptTickRuntimeWithBookmarks* scRT, uint64_t bookmark)
{
auto exIt = (bookmarkRefs.executing) ? bookmarkRefs.executingIt : nullptr;

auto list = &bookmarkRefs.list;

{
for (auto it = list->begin(); it != list->end();)
{
if (it->bookmark == bookmark)
{
if (exIt && *exIt == it)
{
*exIt = it = list->erase(it);
}
else
{
it = list->erase(it);
}
}
else
{
++it;
}
}
}
}

static void RunBookmarks()
{
bookmarkRefs.executing = true;
Expand Down Expand Up @@ -501,6 +529,13 @@ result_t TestScriptHost::CreateBookmarks(IScriptTickRuntimeWithBookmarks* scRT)
return FX_S_OK;
}

result_t TestScriptHost::RemoveBookmark(IScriptTickRuntimeWithBookmarks* scRT, uint64_t bookmark)
{
::RemoveBookmark(scRT, bookmark);
return FX_S_OK;
}


result_t TestScriptHost::ScheduleBookmark(IScriptTickRuntimeWithBookmarks* scRT, uint64_t bookmark, int64_t deadline)
{
auto newDeadline = std::chrono::microseconds{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ class LuaScriptRuntime : public OMClass<LuaScriptRuntime, IScriptRuntime, IScrip

void SchedulePendingBookmarks();

bool RemoveBookmark(uint64_t bookmark);

public:
NS_DECL_ISCRIPTRUNTIME;

Expand Down
25 changes: 9 additions & 16 deletions code/components/citizen-scripting-lua/src/LuaScriptRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1157,22 +1157,8 @@ 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_State* runtimeState = luaRuntime->GetRunningThread();
bool removed = luaRuntime->RemoveBookmark(bookmark);

lua_pushboolean(L, removed);
return 1;
Expand Down Expand Up @@ -1300,6 +1286,13 @@ void LuaScriptRuntime::SchedulePendingBookmarks()
}
}

bool LuaScriptRuntime::RemoveBookmark(uint64_t bookmark)
{
result_t success = GetScriptHostWithBookmarks()->RemoveBookmark(this, bookmark);

return success == FX_S_OK;
}

void LuaScriptRuntime::SetTickRoutine(const std::function<void(uint64_t, bool)>& tickRoutine)
{
if (!m_tickRoutine)
Expand Down
Loading