Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Commit

Permalink
Fix createThread on client and server.
Browse files Browse the repository at this point in the history
  • Loading branch information
drakeee committed Feb 6, 2021
1 parent 7ab5ac8 commit 2dc993f
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/CLuaResourceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ void CLuaResourceImpl::OnTick()

for (auto& timer : this->timerReferences)
{
uint64_t timeNow = GetTime();
uint64_t timeNow = CLuaScriptRuntime::Instance().GetModuleTime();
if ((timeNow - timer.second.lastTime) > timer.second.interval)
{
lua_rawgeti(this->resourceState, LUA_REGISTRYINDEX, timer.second.functionIndex);
Expand Down Expand Up @@ -513,7 +513,7 @@ bool CLuaResourceImpl::RemoveRemoteEvent(std::string eventName, int functionRefe
uint32_t CLuaResourceImpl::CreateTimer(uint32_t functionIndex, uint32_t interval, bool repeat)
{
this->timerIndex++;
this->timerReferences[timerIndex] = LuaTimer{functionIndex, interval, repeat, GetTime()};
this->timerReferences[timerIndex] = LuaTimer{functionIndex, interval, repeat, CLuaScriptRuntime::Instance().GetModuleTime()};

return timerIndex;
}
Expand Down
8 changes: 0 additions & 8 deletions src/CLuaResourceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,6 @@ class CLuaResourceImpl : public alt::IResource::Impl
return (this->timerReferences.erase(timerIndex) > 0);
}

//Source: https://github.com/altmp/v8-helpers/blob/f4e4c2cacff229df022e68af99756b6f6ef1f6eb/V8ResourceImpl.h#L229
static int64_t GetTime()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
}

private:
lua_State* resourceState = nullptr;
CLuaScriptRuntime* runtime;
Expand Down
2 changes: 2 additions & 0 deletions src/CLuaScriptRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,8 @@ CLuaScriptRuntime::CLuaScriptRuntime()
Core->SubscribeEvent(alt::CEvent::Type::RESOURCE_START, CLuaScriptRuntime::OnResourceStart, this);
Core->SubscribeEvent(alt::CEvent::Type::RESOURCE_STOP, CLuaScriptRuntime::OnResourceStop, this);
#endif

this->moduleStartedTime = this->GetTime();
}

#ifdef ALT_SERVER_API
Expand Down
21 changes: 18 additions & 3 deletions src/CLuaScriptRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ class CLuaScriptRuntime : public alt::IScriptRuntime
}
#endif

//Source: https://github.com/altmp/v8-helpers/blob/f4e4c2cacff229df022e68af99756b6f6ef1f6eb/V8ResourceImpl.h#L229
inline static int64_t GetTime()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
}

inline int64_t GetModuleTime()
{
return (this->GetTime() - this->moduleStartedTime);
}


static CLuaScriptRuntime& Instance()
{
Expand All @@ -82,16 +95,18 @@ class CLuaScriptRuntime : public alt::IScriptRuntime
~CLuaScriptRuntime() { };

private:
const semver::version version{ 1, 1, 2, alt::ICore::SDK_VERSION, semver::branch::dev };
const semver::version version{ 1, 1, 3, alt::ICore::SDK_VERSION, semver::branch::dev };
#ifdef ALT_SERVER_API
alt::config::Node::Dict serverConfigDict;

static bool OnResourceStart(const alt::CEvent* e, void* userData);
static bool OnResourceStop(const alt::CEvent* e, void* userData);
#endif
std::map<lua_State*, CLuaResourceImpl*> resources;
EventsCallbacks eventsCallbacks;
EventsGetter eventsGetter;

static bool OnResourceStart(const alt::CEvent* e, void* userData);
static bool OnResourceStop(const alt::CEvent* e, void* userData);
int64_t moduleStartedTime;

const std::vector<std::string> entityTypes{
"Player",
Expand Down
8 changes: 8 additions & 0 deletions src/Defs/CLuaAltFuncDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ void CLuaAltFuncDefs::Init(lua_State* L)
lua_globalfunction(L, "getRequiredPermissions", GetRequiredPermissions);
lua_globalfunction(L, "getOptionalPermissions", GetOptionalPermissions);

lua_globalfunction(L, "getModuleTime", GetModuleTime);

#ifdef ALT_SERVER_API
lua_globalfunction(L, "getRootDirectory", GetRootDirectory);

Expand Down Expand Up @@ -303,6 +305,12 @@ int CLuaAltFuncDefs::HasMetaData(lua_State* L)
return 1;
}

int CLuaAltFuncDefs::GetModuleTime(lua_State* L)
{
lua_pushnumber(L, CLuaScriptRuntime::Instance().GetModuleTime());
return 1;
}

#ifdef ALT_SERVER_API
int CLuaAltFuncDefs::GetRootDirectory(lua_State* L)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Defs/CLuaAltFuncDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class CLuaAltFuncDefs

static int Hash(lua_State* L);

static int GetModuleTime(lua_State* L);

#ifdef ALT_SERVER_API
static int GetRootDirectory(lua_State* L);

Expand Down
6 changes: 3 additions & 3 deletions src/Defs/CLuaMiscScripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function createThread(callback)
assert(type(callback) == "function", "callback argument is not a function")
threads[coroutine.create(callback)] = {
lastTime = alt.getNetTime(),
lastTime = alt.getModuleTime(),
waitFor = 0,
args = {}
}
Expand All @@ -33,7 +33,7 @@ alt.on("tick", function()
local condition = nil
if type(data.waitFor) == "number" then
condition = (alt.getNetTime() > (data.lastTime + data.waitFor))
condition = (alt.getModuleTime() > (data.lastTime + data.waitFor))
elseif type(data.waitFor) == "function" then
condition = data.waitFor(unpack(data.args))
end
Expand All @@ -43,7 +43,7 @@ alt.on("tick", function()
local args = {coroutine.resume(callback)}
if args[1] then
data.lastTime = alt.getNetTime()
data.lastTime = alt.getModuleTime()
data.waitFor = args[2]
data.args = {unpack(args, 3)}
else
Expand Down

0 comments on commit 2dc993f

Please sign in to comment.