Skip to content

Commit b5cd499

Browse files
committed
Allow disabling current script without needing to supply filepath
1 parent d269141 commit b5cd499

File tree

6 files changed

+26
-4
lines changed

6 files changed

+26
-4
lines changed

Entities/MovableObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ int MovableObject::RunScriptedFunctionInAppropriateScripts(const std::string &fu
719719
const LuabindObjectWrapper *luabindObjectWrapper = luaFunction.m_LuaFunction.get();
720720
if (runOnDisabledScripts || luaFunction.m_ScriptIsEnabled) {
721721
LuaStateWrapper& usedState = GetAndLockStateForScript(luabindObjectWrapper->GetFilePath(), &luaFunction);
722-
std::lock_guard<std::recursive_mutex> lock(usedState.GetMutex(), std::adopt_lock);
722+
std::lock_guard<std::recursive_mutex> lock(usedState.GetMutex(), std::adopt_lock);
723723
status = usedState.RunScriptFunctionObject(luabindObjectWrapper, "_ScriptedObjects", std::to_string(m_UniqueID), functionEntityArguments, functionLiteralArguments, functionObjectArguments);
724724
if (status < 0 && stopOnError) {
725725
return status;

Lua/LuaAdapterDefinitions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,8 @@ namespace RTE {
354354
static bool HasScript(MovableObject *luaSelfObject, const std::string &scriptPath);
355355
static bool AddScript(MovableObject *luaSelfObject, const std::string &scriptPath);
356356
static bool EnableScript(MovableObject *luaSelfObject, const std::string &scriptPath);
357-
static bool DisableScript(MovableObject *luaSelfObject, const std::string &scriptPath);
357+
static bool DisableScript1(MovableObject *luaSelfObject);
358+
static bool DisableScript2(MovableObject *luaSelfObject, const std::string &scriptPath);
358359
static void SendMessage1(MovableObject *luaSelfObject, const std::string &message);
359360
static void SendMessage2(MovableObject *luaSelfObject, const std::string &message, luabind::object context);
360361
};

Lua/LuaAdapters.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,17 @@ namespace RTE {
401401
return luaSelfObject->EnableOrDisableScript(g_PresetMan.GetFullModulePath(scriptPath), true);
402402
}
403403

404+
405+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
406+
407+
bool LuaAdaptersMovableObject::DisableScript1(MovableObject* luaSelfObject) {
408+
std::string currentScriptFilePath(g_LuaMan.GetThreadCurrentLuaState()->GetCurrentlyRunningScriptFilePath());
409+
return luaSelfObject->EnableOrDisableScript(currentScriptFilePath, false);
410+
}
411+
404412
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
405413

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

Lua/LuaBindingsEntities.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,8 @@ namespace RTE {
10411041
.def("AddScript", &LuaAdaptersMovableObject::AddScript)
10421042
.def("ScriptEnabled", &MovableObject::ScriptEnabled)
10431043
.def("EnableScript", &LuaAdaptersMovableObject::EnableScript)
1044-
.def("DisableScript", &LuaAdaptersMovableObject::DisableScript)
1044+
.def("DisableScript", &LuaAdaptersMovableObject::DisableScript1)
1045+
.def("DisableScript", &LuaAdaptersMovableObject::DisableScript2)
10451046
.def("EnableOrDisableAllScripts", &MovableObject::EnableOrDisableAllScripts)
10461047
.def("GetStringValue", &MovableObject::GetStringValue)
10471048
.def("GetEncodedStringValue", &MovableObject::GetEncodedStringValue)

Managers/LuaMan.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace RTE {
1818
m_TempEntity = nullptr;
1919
m_TempEntityVector.clear();
2020
m_LastError.clear();
21+
m_CurrentlyRunningScriptPath = "";
2122
}
2223

2324
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -571,6 +572,7 @@ namespace RTE {
571572

572573
std::lock_guard<std::recursive_mutex> lock(m_Mutex);
573574
s_currentLuaState = this;
575+
m_CurrentlyRunningScriptPath = functionObject->GetFilePath();
574576

575577
lua_pushcfunction(m_State, &AddFileAndLineToError);
576578
functionObject->GetLuabindObject()->push(m_State);
@@ -634,6 +636,7 @@ namespace RTE {
634636

635637
lua_pop(m_State, 1);
636638

639+
m_CurrentlyRunningScriptPath = "";
637640
return status;
638641
}
639642

@@ -659,6 +662,7 @@ namespace RTE {
659662

660663
std::lock_guard<std::recursive_mutex> lock(m_Mutex);
661664
s_currentLuaState = this;
665+
m_CurrentlyRunningScriptPath = filePath;
662666

663667
const int stackStart = lua_gettop(m_State);
664668

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

716+
m_CurrentlyRunningScriptPath = "";
712717
RTEAssert(lua_gettop(m_State) == stackStart, "Malformed lua stack!");
713718
return error;
714719
}

Managers/LuaMan.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ namespace RTE {
8888
/// </summary>
8989
/// <returns>m_ScriptTimings.</returns>
9090
const std::unordered_map<std::string, PerformanceMan::ScriptTiming> & GetScriptTimings() const;
91+
92+
/// <summary>
93+
/// Gets the currently running script filepath, if applicable.
94+
/// </summary>
95+
/// <returns>The currently running script filepath. May return inaccurate values for non-MO scripts due to weirdness in setup.</returns>
96+
std::string_view GetCurrentlyRunningScriptFilePath() const { return m_CurrentlyRunningScriptPath; }
9197
#pragma endregion
9298

9399
#pragma region Script Responsibility Handling
@@ -307,6 +313,7 @@ namespace RTE {
307313
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.
308314
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.
309315
std::string m_LastError; //!< Description of the last error that occurred in the script execution.
316+
std::string_view m_CurrentlyRunningScriptPath; //!< The currently running script filepath.
310317

311318
// 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.
312319
std::recursive_mutex m_Mutex; //!< Mutex to ensure multiple threads aren't running something in this lua state simultaneously.

0 commit comments

Comments
 (0)