Skip to content

Commit

Permalink
Lua Script Debug Fix (#622)
Browse files Browse the repository at this point in the history
* Lua Script Debug Fix AND Get Args Params

* restore wiArgs, change AttachScript as one liner

* cleanups

* revert argument changes

Co-authored-by: Turánszki János <[email protected]>
  • Loading branch information
megumumpkin and turanszkij authored Jan 16, 2023
1 parent 4f4c725 commit 8af7ad8
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 24 deletions.
1 change: 0 additions & 1 deletion WickedEngine/wiArguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ namespace wi::arguments
{
return params.find(value) != params.end();
}

}
36 changes: 17 additions & 19 deletions WickedEngine/wiLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,29 @@ namespace wi::lua
return scriptpid_next.fetch_add(1);
}

uint32_t AttachScriptParameters(std::string& script, const std::string& filename, uint32_t PID, const std::string& customparameters_prepend, const std::string& customparameters_append)
{
static const std::string persistent_inject = R"(
local runProcess = function(func)
success, co = Internal_runProcess(script_file(), script_pid(), func)
return success, co
end
if _ENV.PROCESSES_DATA[script_pid()] == nil then
_ENV.PROCESSES_DATA[script_pid()] = { _INITIALIZED = -1 }
end
if _ENV.PROCESSES_DATA[script_pid()]._INITIALIZED < 1 then
_ENV.PROCESSES_DATA[script_pid()]._INITIALIZED = _ENV.PROCESSES_DATA[script_pid()]._INITIALIZED + 1
end
)";
uint32_t AttachScriptParameters(std::string& script, const std::string& filename, uint32_t PID, const std::string& customparameters_prepend, const std::string& customparameters_append)
{
static const std::string persistent_inject =
"local runProcess = function(func) "
" success, co = Internal_runProcess(script_file(), script_pid(), func);"
" return success, co;"
"end;"
"if _ENV.PROCESSES_DATA[script_pid()] == nil then"
" _ENV.PROCESSES_DATA[script_pid()] = { _INITIALIZED = -1 };"
"end;"
"if _ENV.PROCESSES_DATA[script_pid()]._INITIALIZED < 1 then"
" _ENV.PROCESSES_DATA[script_pid()]._INITIALIZED = _ENV.PROCESSES_DATA[script_pid()]._INITIALIZED + 1;"
"end;";

// Make sure the file path doesn't contain backslash characters, replace them with forward slash.
// - backslash would be recognized by lua as escape character
// - the path string could be coming from unknown location (content, programmer, filepicker), so always do this
std::string filepath = filename;
std::replace(filepath.begin(), filepath.end(), '\\', '/');

std::string dynamic_inject = "local function script_file() return \"" + filepath + "\" end\n";
dynamic_inject += "local function script_pid() return \"" + std::to_string(PID) + "\" end\n";
dynamic_inject += "local function script_dir() return \"" + wi::helper::GetDirectoryFromPath(filepath) + "\" end\n";
std::string dynamic_inject = "local function script_file() return \"" + filepath + "\" end;";
dynamic_inject += "local function script_pid() return \"" + std::to_string(PID) + "\" end;";
dynamic_inject += "local function script_dir() return \"" + wi::helper::GetDirectoryFromPath(filepath) + "\" end;";
dynamic_inject += persistent_inject;
script = dynamic_inject + customparameters_prepend + script + customparameters_append;

Expand Down Expand Up @@ -537,12 +536,11 @@ namespace wi::lua
{
ss += error;
}
wi::backlog::post(ss);
wi::backlog::post(ss, wi::backlog::LogLevel::Error);
}

void SAddMetatable(lua_State* L, const std::string& name)
{
luaL_newmetatable(L, name.c_str());
}

}
2 changes: 1 addition & 1 deletion WickedEngine/wiLua.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace wi::lua

// Adds some local management functions to the script
// returns the PID
uint32_t AttachScriptParameters(std::string& script, const std::string& filename = "", uint32_t PID = GeneratePID(), const std::string& customparameters_prepend = "", const std::string& customparameters_append = "");
uint32_t AttachScriptParameters(std::string& script, const std::string& filename = "", uint32_t PID = GeneratePID(), const std::string& customparameters_prepend = "", const std::string& customparameters_append = "");

//Following functions are "static", operating on specified lua state:

Expand Down
3 changes: 1 addition & 2 deletions WickedEngine/wiScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4230,9 +4230,8 @@ namespace wi::scene
{
if (script.script.empty() && script.resource.IsValid())
{
script.script += "local function GetEntity() return " + std::to_string(entity) + "; end\n";
script.script += script.resource.GetScript();
wi::lua::AttachScriptParameters(script.script, script.filename);
wi::lua::AttachScriptParameters(script.script, script.filename, wi::lua::GeneratePID(), "local function GetEntity() return " + std::to_string(entity) + "; end;", "");
}
wi::lua::RunText(script.script);

Expand Down
2 changes: 1 addition & 1 deletion WickedEngine/wiVersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace wi::version
// minor features, major updates, breaking compatibility changes
const int minor = 71;
// minor bug fixes, alterations, refactors, updates
const int revision = 137;
const int revision = 138;

const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);

Expand Down

0 comments on commit 8af7ad8

Please sign in to comment.