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

Commit

Permalink
Allow disabling current script without needing to supply filepath
Browse files Browse the repository at this point in the history
  • Loading branch information
Causeless committed Dec 30, 2023
1 parent d269141 commit b5cd499
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Entities/MovableObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ int MovableObject::RunScriptedFunctionInAppropriateScripts(const std::string &fu
const LuabindObjectWrapper *luabindObjectWrapper = luaFunction.m_LuaFunction.get();
if (runOnDisabledScripts || luaFunction.m_ScriptIsEnabled) {
LuaStateWrapper& usedState = GetAndLockStateForScript(luabindObjectWrapper->GetFilePath(), &luaFunction);
std::lock_guard<std::recursive_mutex> lock(usedState.GetMutex(), std::adopt_lock);
std::lock_guard<std::recursive_mutex> lock(usedState.GetMutex(), std::adopt_lock);
status = usedState.RunScriptFunctionObject(luabindObjectWrapper, "_ScriptedObjects", std::to_string(m_UniqueID), functionEntityArguments, functionLiteralArguments, functionObjectArguments);
if (status < 0 && stopOnError) {
return status;
Expand Down
3 changes: 2 additions & 1 deletion Lua/LuaAdapterDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ namespace RTE {
static bool HasScript(MovableObject *luaSelfObject, const std::string &scriptPath);
static bool AddScript(MovableObject *luaSelfObject, const std::string &scriptPath);
static bool EnableScript(MovableObject *luaSelfObject, const std::string &scriptPath);
static bool DisableScript(MovableObject *luaSelfObject, const std::string &scriptPath);
static bool DisableScript1(MovableObject *luaSelfObject);
static bool DisableScript2(MovableObject *luaSelfObject, const std::string &scriptPath);
static void SendMessage1(MovableObject *luaSelfObject, const std::string &message);
static void SendMessage2(MovableObject *luaSelfObject, const std::string &message, luabind::object context);
};
Expand Down
10 changes: 9 additions & 1 deletion Lua/LuaAdapters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,17 @@ namespace RTE {
return luaSelfObject->EnableOrDisableScript(g_PresetMan.GetFullModulePath(scriptPath), true);
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool LuaAdaptersMovableObject::DisableScript1(MovableObject* luaSelfObject) {
std::string currentScriptFilePath(g_LuaMan.GetThreadCurrentLuaState()->GetCurrentlyRunningScriptFilePath());
return luaSelfObject->EnableOrDisableScript(currentScriptFilePath, false);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool LuaAdaptersMovableObject::DisableScript(MovableObject *luaSelfObject, const std::string &scriptPath) {
bool LuaAdaptersMovableObject::DisableScript2(MovableObject *luaSelfObject, const std::string &scriptPath) {
return luaSelfObject->EnableOrDisableScript(g_PresetMan.GetFullModulePath(scriptPath), false);
}

Expand Down
3 changes: 2 additions & 1 deletion Lua/LuaBindingsEntities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,8 @@ namespace RTE {
.def("AddScript", &LuaAdaptersMovableObject::AddScript)
.def("ScriptEnabled", &MovableObject::ScriptEnabled)
.def("EnableScript", &LuaAdaptersMovableObject::EnableScript)
.def("DisableScript", &LuaAdaptersMovableObject::DisableScript)
.def("DisableScript", &LuaAdaptersMovableObject::DisableScript1)
.def("DisableScript", &LuaAdaptersMovableObject::DisableScript2)
.def("EnableOrDisableAllScripts", &MovableObject::EnableOrDisableAllScripts)
.def("GetStringValue", &MovableObject::GetStringValue)
.def("GetEncodedStringValue", &MovableObject::GetEncodedStringValue)
Expand Down
5 changes: 5 additions & 0 deletions Managers/LuaMan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace RTE {
m_TempEntity = nullptr;
m_TempEntityVector.clear();
m_LastError.clear();
m_CurrentlyRunningScriptPath = "";
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -571,6 +572,7 @@ namespace RTE {

std::lock_guard<std::recursive_mutex> lock(m_Mutex);
s_currentLuaState = this;
m_CurrentlyRunningScriptPath = functionObject->GetFilePath();

lua_pushcfunction(m_State, &AddFileAndLineToError);
functionObject->GetLuabindObject()->push(m_State);
Expand Down Expand Up @@ -634,6 +636,7 @@ namespace RTE {

lua_pop(m_State, 1);

m_CurrentlyRunningScriptPath = "";
return status;
}

Expand All @@ -659,6 +662,7 @@ namespace RTE {

std::lock_guard<std::recursive_mutex> lock(m_Mutex);
s_currentLuaState = this;
m_CurrentlyRunningScriptPath = filePath;

const int stackStart = lua_gettop(m_State);

Expand Down Expand Up @@ -709,6 +713,7 @@ namespace RTE {
// Pop the line error handler off the stack to clean it up
lua_pop(m_State, 1);

m_CurrentlyRunningScriptPath = "";
RTEAssert(lua_gettop(m_State) == stackStart, "Malformed lua stack!");
return error;
}
Expand Down
7 changes: 7 additions & 0 deletions Managers/LuaMan.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ namespace RTE {
/// </summary>
/// <returns>m_ScriptTimings.</returns>
const std::unordered_map<std::string, PerformanceMan::ScriptTiming> & GetScriptTimings() const;

/// <summary>
/// Gets the currently running script filepath, if applicable.
/// </summary>
/// <returns>The currently running script filepath. May return inaccurate values for non-MO scripts due to weirdness in setup.</returns>
std::string_view GetCurrentlyRunningScriptFilePath() const { return m_CurrentlyRunningScriptPath; }
#pragma endregion

#pragma region Script Responsibility Handling
Expand Down Expand Up @@ -307,6 +313,7 @@ namespace RTE {
Entity *m_TempEntity; //!< Temporary holder for an Entity object that we want to pass into the Lua state without fuss. Lets you export objects to lua easily.
std::vector<Entity *> m_TempEntityVector; //!< Temporary holder for a vector of Entities that we want to pass into the Lua state without a fuss. Usually used to pass arguments to special Lua functions.
std::string m_LastError; //!< Description of the last error that occurred in the script execution.
std::string_view m_CurrentlyRunningScriptPath; //!< The currently running script filepath.

// This mutex is more for safety, and with new script/AI architecture we shouldn't ever be locking on a mutex. As such we use this primarily to fire asserts.
std::recursive_mutex m_Mutex; //!< Mutex to ensure multiple threads aren't running something in this lua state simultaneously.
Expand Down

0 comments on commit b5cd499

Please sign in to comment.