Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 97d22dd

Browse files
committed
Remove luaState parameter from LuaMan::SetLuaPath - only one state exists and param wasn't even used in the method
1 parent 62bad41 commit 97d22dd

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

Managers/LuaMan.cpp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,28 @@ namespace RTE {
244244
}
245245
}
246246

247+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
248+
249+
void LuaMan::SetLuaPath(const std::string &filePath) {
250+
const std::string moduleName = g_PresetMan.GetModuleNameFromPath(filePath);
251+
const std::string moduleFolder = g_PresetMan.IsModuleOfficial(moduleName) ? System::GetDataDirectory() : System::GetModDirectory();
252+
const std::string scriptPath = moduleFolder + moduleName + "/?.lua";
253+
254+
lua_getglobal(m_MasterState, "package");
255+
lua_getfield(m_MasterState, -1, "path"); // get field "path" from table at top of stack (-1).
256+
std::string currentPath = lua_tostring(m_MasterState, -1); // grab path string from top of stack.
257+
258+
// check if scriptPath is already in there, if not add it.
259+
if (currentPath.find(scriptPath) == std::string::npos) {
260+
currentPath.append(";" + scriptPath);
261+
}
262+
263+
lua_pop(m_MasterState, 1); // get rid of the string on the stack we just pushed previously.
264+
lua_pushstring(m_MasterState, currentPath.c_str()); // push the new one.
265+
lua_setfield(m_MasterState, -2, "path"); // set the field "path" in table at -2 with value at top of stack.
266+
lua_pop(m_MasterState, 1); // get rid of package table from top of stack.
267+
}
268+
247269
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
248270

249271
int LuaMan::RunScriptFunctionString(const std::string &functionName, const std::string &selfObjectName, const std::vector<std::string_view> &variablesToSafetyCheck, const std::vector<const Entity *> &functionEntityArguments, const std::vector<std::string_view> &functionLiteralArguments) {
@@ -378,7 +400,7 @@ namespace RTE {
378400
int error = 0;
379401

380402
lua_pushcfunction(m_MasterState, &AddFileAndLineToError);
381-
SetLuaPath(m_MasterState, fullScriptPath);
403+
SetLuaPath(fullScriptPath);
382404
// Load the script file's contents onto the stack and then execute it with pcall. Pcall will call the file and line error handler if there's an error by pointing 2 up the stack to it.
383405
if (luaL_loadfile(m_MasterState, fullScriptPath.c_str()) || lua_pcall(m_MasterState, 0, LUA_MULTRET, -2)) {
384406
m_LastError = lua_tostring(m_MasterState, -1);
@@ -395,27 +417,6 @@ namespace RTE {
395417
return error;
396418
}
397419

398-
void LuaMan::SetLuaPath( lua_State *luaState, const std::string &filePath )
399-
{
400-
const std::string moduleName = g_PresetMan.GetModuleNameFromPath( filePath );
401-
const std::string moduleFolder = g_PresetMan.IsModuleOfficial(moduleName) ? "Data/" : System::GetModDirectory();
402-
const std::string scriptPath = moduleFolder + moduleName + "/?.lua";
403-
404-
lua_getglobal( m_MasterState, "package" );
405-
lua_getfield( m_MasterState, -1, "path" ); // get field "path" from table at top of stack (-1)
406-
std::string cur_path = lua_tostring( m_MasterState, -1 ); // grab path string from top of stack
407-
408-
if (cur_path.find(scriptPath) == cur_path.npos) { // check if scriptPath is already in there
409-
cur_path.append( ";" );
410-
cur_path.append( scriptPath ); // if not add it
411-
}
412-
413-
lua_pop( m_MasterState, 1 ); // get rid of the string on the stack we just pushed previously
414-
lua_pushstring( m_MasterState, cur_path.c_str() ); // push the new one
415-
lua_setfield( m_MasterState, -2, "path" ); // set the field "path" in table at -2 with value at top of stack
416-
lua_pop( m_MasterState, 1 ); // get rid of package table from top of stack
417-
}
418-
419420
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
420421

421422
int LuaMan::RunScriptFileAndRetrieveFunctions(const std::string &filePath, const std::vector<std::string> &functionNamesToLookFor, std::unordered_map<std::string, LuabindObjectWrapper *> &outFunctionNamesAndObjects) {

Managers/LuaMan.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ namespace RTE {
6767
/// </summary>
6868
/// <param name="entityVector">The temporary vector of entities. Ownership is NOT transferred!</param>
6969
void SetTempEntityVector(const std::vector<const Entity *> &entityVector);
70+
71+
/// <summary>
72+
/// Sets the proper package.path for the script to run.
73+
/// </summary>
74+
/// <param name="filePath">The path to the file to load and run.</param>
75+
void SetLuaPath(const std::string &filePath);
7076
#pragma endregion
7177

7278
#pragma region Script Execution Handling
@@ -118,13 +124,6 @@ namespace RTE {
118124
/// <param name="outFunctionNamesAndObjects">The map of function names to LuabindObjectWrappers to be retrieved from the script that was run.</param>
119125
/// <returns>Returns less than zero if any errors encountered when running this script. To get the actual error string, call GetLastError.</returns>
120126
int RunScriptFileAndRetrieveFunctions(const std::string &filePath, const std::vector<std::string> &functionNamesToLookFor, std::unordered_map<std::string, LuabindObjectWrapper *> &outFunctionNamesAndObjects);
121-
122-
/// <summary>
123-
/// Sets the proper package.path for the script to run.
124-
/// </summary>
125-
/// <param name="luaState">The script parent state.</param>
126-
/// <param name="filePath">The path to the file to load and run.</param>
127-
void SetLuaPath(lua_State* luaState, const std::string& filePath);
128127
#pragma endregion
129128

130129
#pragma region

0 commit comments

Comments
 (0)