90 lines
2.2 KiB
C++
90 lines
2.2 KiB
C++
#include "ScriptCore.h"
|
|
#include "../../core/utils/Logging.h"
|
|
#include "../../core/utils/ExceptionHandler.h"
|
|
#include "../../core/utils/Profiler.h"
|
|
|
|
void ScriptCore::Init()
|
|
{
|
|
s_LuaStates.clear();
|
|
}
|
|
|
|
void ScriptCore::RegisterState(lua_State* L, const std::string& scriptPath, const std::string& name)
|
|
{
|
|
s_LuaStates.push_back({ L, scriptPath, name });
|
|
|
|
}
|
|
|
|
void ScriptCore::UnregisterState(lua_State* L)
|
|
{
|
|
auto it = std::remove_if(s_LuaStates.begin(), s_LuaStates.end(),
|
|
[L](const LuaScript& script) {
|
|
return script.L == L;
|
|
});
|
|
|
|
if (it != s_LuaStates.end())
|
|
{
|
|
Logger::LogVerbose("[ScriptCore] Unregistered Lua state");
|
|
s_LuaStates.erase(it, s_LuaStates.end());
|
|
}
|
|
else
|
|
{
|
|
Logger::LogWarning("[ScriptCore] Tried to unregister unknown Lua state");
|
|
}
|
|
}
|
|
|
|
|
|
void ScriptCore::CallAllInits()
|
|
{
|
|
for (auto& script : s_LuaStates)
|
|
{
|
|
PROFILE_DEEP_SCOPE("ScriptCore::OnInit");
|
|
|
|
if (!script.L)
|
|
continue;
|
|
|
|
lua_getglobal(script.L, "OnInit");
|
|
if (lua_isfunction(script.L, -1))
|
|
{
|
|
if (lua_pcall(script.L, 0, 0, 0) != LUA_OK)
|
|
{
|
|
Logger::LogError("[Lua] %s", lua_tostring(script.L, -1));
|
|
RecoverableError("OnInit failed in: " + script.scriptPath, Create::Exceptions::ComponentLoad).Handle();
|
|
lua_pop(script.L, 1);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
lua_pop(script.L, 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ScriptCore::CallAllUpdates(float dt)
|
|
{
|
|
PROFILE_DEEP_SCOPE("ScriptCore::OnUpdate");
|
|
|
|
for (auto& script : s_LuaStates)
|
|
{
|
|
PROFILE_SCOPE("Script: " + script.name);
|
|
|
|
if (!script.L)
|
|
continue;
|
|
|
|
lua_getglobal(script.L, "OnUpdate");
|
|
if (lua_isfunction(script.L, -1))
|
|
{
|
|
lua_pushnumber(script.L, dt);
|
|
if (lua_pcall(script.L, 1, 0, 0) != LUA_OK)
|
|
{
|
|
Logger::LogError("[Lua] %s", lua_tostring(script.L, -1));
|
|
RecoverableError("OnUpdate failed in: " + script.scriptPath, Create::Exceptions::ComponentLoad).Handle();
|
|
lua_pop(script.L, 1);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
lua_pop(script.L, 1);
|
|
}
|
|
}
|
|
}
|