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

Commit 810d35b

Browse files
committed
Added methods to MovableObject to run scripted functions for them on a given script and on all of their scripts (with the option to run on disabled or not) and moved this out of LuaMan. These methods can optionally take arguments to provide to the functions they run and, if there's more than one script on the MO, will spit out an error telling you want script and function screwed up.
Used these new methods through MovableObject Renamed some supported script function names and made it so removing and disabling a script call the same function, but with a different value for their 2nd argument Added details to LoadScript comments, cleaned up some junk
1 parent 01f7d08 commit 810d35b

File tree

4 files changed

+110
-56
lines changed

4 files changed

+110
-56
lines changed

Entities/MovableObject.cpp

Lines changed: 85 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,7 @@ int MovableObject::Save(Writer &writer) const
455455

456456
void MovableObject::Destroy(bool notInherited) {
457457
if (!m_ScriptObjectName.empty()) {
458-
for (std::pair<std::string, bool> scriptEntry: m_LoadedScripts) {
459-
if (scriptEntry.second == true) {
460-
g_LuaMan.RunFunctionInPresetScript("Destroy", scriptEntry.first, m_ScriptPresetName, m_ScriptObjectName);
461-
}
462-
}
458+
RunScriptedFunctionInAppropriateScripts("Destroy");
463459
g_LuaMan.RunScriptString(m_ScriptObjectName + " = nil;");
464460
}
465461

@@ -499,7 +495,6 @@ int MovableObject::LoadScript(std::string const &scriptPath, bool loadAsEnabledS
499495
// If there's no ScriptPresetName this is the first script being loaded for this preset, or scripts have been reloaded.
500496
// Generate a ScriptPresetName, setup a table for the preset's functions, and clear the instance object name so it gets created in the first run of UpdateScripts
501497
if (m_ScriptPresetName.empty()) {
502-
// TODO WAIT A MINUTE.. is this an original preset????!! .. does it matter? A: not really
503498
m_ScriptPresetName = GetClassName() + "s." + g_LuaMan.GetNewPresetID();
504499

505500
if (g_LuaMan.RunScriptString(m_ScriptPresetName + " = {};") < 0) {
@@ -565,8 +560,8 @@ bool MovableObject::AddScript(std::string const &scriptPath) {
565560
switch (LoadScript(scriptPath)) {
566561
case 0:
567562
// If we have a ScriptObjectName that means Create has already been run for pre-existing scripts. Run it right away for this one.
568-
if (!m_ScriptObjectName.empty() &&g_LuaMan.RunFunctionInPresetScript("Create", scriptPath, m_ScriptPresetName, m_ScriptObjectName) < 0) {
569-
g_ConsoleMan.PrintString("ERROR: Failed to run Create function for newly added script with path " + scriptPath);
563+
if (!m_ScriptObjectName.empty()) {
564+
RunScriptedFunction(scriptPath, "Create");
570565
return false;
571566
}
572567
return true;
@@ -589,9 +584,17 @@ bool MovableObject::AddScript(std::string const &scriptPath) {
589584
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
590585

591586
bool MovableObject::RemoveScript(std::string const &scriptPath) {
587+
if (m_LoadedScripts.empty() || m_ScriptPresetName.empty()) {
588+
return false;
589+
}
590+
592591
auto scriptEntryIterator = FindScript(scriptPath);
593592
if (scriptEntryIterator != m_LoadedScripts.end()) {
594593
m_LoadedScripts.erase(scriptEntryIterator);
594+
if (!m_ScriptObjectName.empty() && RunScriptedFunction(scriptPath, "OnScriptRemoveOrDisable", {}, {"true"}) < 0) {
595+
g_ConsoleMan.PrintString("NOTE: The script has been removed despite this error.");
596+
return false;
597+
}
595598
return true;
596599
}
597600
return false;
@@ -600,14 +603,16 @@ bool MovableObject::RemoveScript(std::string const &scriptPath) {
600603
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
601604

602605
bool MovableObject::EnableScript(std::string const &scriptPath) {
606+
if (m_LoadedScripts.empty() || m_ScriptPresetName.empty()) {
607+
return false;
608+
}
609+
603610
auto scriptEntryIterator = FindScript(scriptPath);
604611
if (scriptEntryIterator != m_LoadedScripts.end() && scriptEntryIterator->second == false) {
605-
scriptEntryIterator->second = true;
606-
607-
if (g_LuaMan.RunFunctionInPresetScript("OnEnableScript", scriptPath, m_ScriptPresetName, m_ScriptObjectName) < 0) {
608-
g_ConsoleMan.PrintString("ERROR: Failed to run OnEnableScript function for newly enabled script with path " + scriptPath);
612+
if (!m_ScriptObjectName.empty() && RunScriptedFunction(scriptPath, "OnScriptEnable") < 0) {
609613
return false;
610614
}
615+
scriptEntryIterator->second = true;
611616
return true;
612617
}
613618
return false;
@@ -616,21 +621,79 @@ bool MovableObject::EnableScript(std::string const &scriptPath) {
616621
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
617622

618623
bool MovableObject::DisableScript(std::string const &scriptPath) {
624+
if (m_LoadedScripts.empty() || m_ScriptPresetName.empty()) {
625+
return false;
626+
}
627+
619628
auto scriptEntryIterator = FindScript(scriptPath);
620629
if (scriptEntryIterator != m_LoadedScripts.end() && scriptEntryIterator->second == true) {
621-
scriptEntryIterator->second = false;
622-
623-
if (g_LuaMan.RunFunctionInPresetScript("OnDisableScript", scriptPath, m_ScriptPresetName, m_ScriptObjectName) < 0) {
624-
g_ConsoleMan.PrintString("ERROR: Failed to run OnDisableScript function for newly disabled script with path " + scriptPath);
630+
if (!m_ScriptObjectName.empty() && RunScriptedFunction(scriptPath, "OnScriptRemoveOrDisable", {}, {"false"}) < 0) {
625631
return false;
626632
}
633+
scriptEntryIterator->second = false;
627634
return true;
628635
}
629636
return false;
630637
}
631638

632639
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
633640

641+
int MovableObject::RunScriptedFunctionInAppropriateScripts(std::string const &functionName, bool runOnDisabledScripts, bool stopOnError, std::vector<Entity *> functionEntityArguments, std::vector<std::string> functionLiteralArguments) {
642+
if (m_LoadedScripts.empty() || m_ScriptPresetName.empty() || m_ScriptObjectName.empty()) {
643+
return -1;
644+
}
645+
646+
int status = 0;
647+
for (std::pair<std::string, bool> scriptEntry : m_LoadedScripts) {
648+
if (runOnDisabledScripts || scriptEntry.second == true) {
649+
status = RunScriptedFunction(scriptEntry.first, functionName, functionEntityArguments, functionLiteralArguments);
650+
if (status < 0 && stopOnError) {
651+
return status;
652+
}
653+
}
654+
}
655+
return status;
656+
}
657+
658+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
659+
660+
int MovableObject::RunScriptedFunction(std::string const &scriptPath, std::string const &functionName, std::vector<Entity *> functionEntityArguments, std::vector<std::string> functionLiteralArguments) {
661+
if (m_LoadedScripts.empty() || m_ScriptPresetName.empty() || m_ScriptObjectName.empty()) {
662+
return -1;
663+
}
664+
665+
std::string presetAndFunctionName = m_ScriptPresetName + "." + functionName;
666+
std::string scriptString = "if " + presetAndFunctionName + " and " + m_ScriptObjectName + " and " + presetAndFunctionName + "[\"" + scriptPath + "\"] then ";
667+
if (!functionEntityArguments.empty()) {
668+
scriptString += "local entityArguments = LuaMan.TempEntities; ";
669+
}
670+
scriptString += presetAndFunctionName + "[\"" + scriptPath + "\"](" + m_ScriptObjectName;
671+
if (!functionEntityArguments.empty()) {
672+
g_LuaMan.SetTempEntityVector(functionEntityArguments);
673+
for (const Entity *functionEntityArgument : functionEntityArguments) {
674+
scriptString += ", To" + functionEntityArgument->GetClassName() + "(entityArguments())";
675+
}
676+
}
677+
if (!functionLiteralArguments.empty()) {
678+
for (const std::string functionLiteralArgument : functionLiteralArguments) {
679+
scriptString += ", " + functionLiteralArgument;
680+
}
681+
}
682+
scriptString += +"); end";
683+
684+
int status = g_LuaMan.RunScriptString(scriptString);
685+
functionEntityArguments.clear();
686+
functionLiteralArguments.clear();
687+
if (status < 0 && m_LoadedScripts.size() > 1) {
688+
g_ConsoleMan.PrintString("ERROR: An error occured while trying to run " + functionName + "function for script at path " + scriptPath);
689+
return -2;
690+
}
691+
692+
return 0;
693+
}
694+
695+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
696+
634697
/*
635698
//////////////////////////////////////////////////////////////////////////////////////////
636699
// Constructor: MovableObject
@@ -902,21 +965,18 @@ int MovableObject::UpdateScripts() {
902965

903966
// Give Lua access to this object, then use that access to set up the object's Lua representation
904967
g_MovableMan.SetScriptedEntity(this);
968+
905969
if (g_LuaMan.RunScriptString(m_ScriptObjectName + " = To" + GetClassName() + "(MovableMan.ScriptedEntity);") < 0) {
906970
return -2;
907971
}
908972

909-
for (std::pair<std::string, bool> scriptEntry : m_LoadedScripts) {
910-
if (g_LuaMan.RunFunctionInPresetScript("Create", scriptEntry.first, m_ScriptPresetName, m_ScriptObjectName) < 0) {
911-
return -3;
912-
}
973+
if (RunScriptedFunctionInAppropriateScripts("Create", true, true) < 0) {
974+
return -3;
913975
}
914976
}
915977

916-
for (std::pair<std::string, bool> scriptEntry : m_LoadedScripts) {
917-
if (scriptEntry.second == true && g_LuaMan.RunFunctionInPresetScript("Update", scriptEntry.first, m_ScriptPresetName, m_ScriptObjectName) < 0) {
918-
return -3;
919-
}
978+
if (RunScriptedFunctionInAppropriateScripts("Update", false, true) < 0) {
979+
return -3;
920980
}
921981

922982
return 0;
@@ -930,12 +990,7 @@ int MovableObject::OnPieMenu(Actor * pActor) {
930990
}
931991
m_pPieMenuActor = pActor;
932992

933-
int status = 0;
934-
for (std::pair<std::string, bool> scriptEntry : m_LoadedScripts) {
935-
status = scriptEntry.second == true ? g_LuaMan.RunFunctionInPresetScript("OnPieMenu", scriptEntry.first, m_ScriptPresetName, m_ScriptObjectName) : status;
936-
if (status < 0) { break; }
937-
}
938-
return status;
993+
return RunScriptedFunctionInAppropriateScripts("OnPieMenu"); //TODO try passing actor here, see if it works. If it does, maybe quietly refactor this whole thing
939994
}
940995

941996
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Entities/MovableObject.h

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ ENTITYALLOCATION(MovableObject)
183183
/// </summary>
184184
/// <param name="scriptPath">The path to the script to load.</param>
185185
/// <param name="loadAsEnabledScript">Whether or not the script should load as enabled. Defaults to true.</param>
186-
/// <returns>An error return value signaling sucess or any particular failure. Anything below 0 is an error signal.</returns>
186+
/// <returns>0 on success. -1 if scriptPath is empty. -2 if the script is already loaded. -3 if setup to load the script or modify the global lua state fails. -4 if the script fails to load.</returns>
187187
virtual int LoadScript(std::string const &scriptPath, bool loadAsEnabledScript = true);
188188

189189
/// <summary>
@@ -241,6 +241,29 @@ ENTITYALLOCATION(MovableObject)
241241
/// <returns>Whether or not the script was succesfully disabled..</returns>
242242
virtual bool DisableScript(std::string const &scriptPath);
243243

244+
/// <summary>
245+
/// Runs the given function in all scripts that have it, with the given arguments, with the ability to not run on disabled scripts and to cease running if there's an error.
246+
/// The first argument to the function will always be 'self'. If either argument list is not emtpy, its entries will be passed into the Lua function in order, with entity arguments first.
247+
/// </summary>
248+
/// <param name="functionName">The name of the function to run.</param>
249+
/// <param name="runOnDisabledScripts">Whether to run the function on disabled scripts. Defaults to false.</param>
250+
/// <param name="stopOnError">Whether to stop if there's an error running any script, or simply print it to the console and continue. Defaults to false.</param>
251+
/// <param name="functionEntityArguments">Optional vector of entity pointers that should be passed into the Lua function. Their internal Lua states will not be accessible. Defaults to empty.</param>
252+
/// <param name="functionLiteralArguments">Optional vector of strings, that should be passed into the Lua function. Entries must be surrounded with escaped quotes (i.e.`\"`) they'll be passed in as-is, allowing them to act as booleans, etc.. Defaults to empty.</param>
253+
/// <returns>An error return value signaling sucess or any particular failure. Anything below 0 is an error signal.</returns>
254+
int RunScriptedFunctionInAppropriateScripts(std::string const &functionName, bool runOnDisabledScripts = false, bool stopOnError = false, std::vector<Entity *> functionEntityArguments = std::vector<Entity *>(), std::vector<std::string> functionLiteralArguments = std::vector<std::string>());
255+
256+
/// <summary>
257+
/// Runs the given function for the given script, with the given arguments. The first argument to the function will always be 'self'.
258+
/// If either argument list is not emtpy, its entries will be passed into the Lua function in order, with entity arguments first.
259+
/// </summary>
260+
/// <param name="scriptPath">The path to the script to run.</param>
261+
/// <param name="functionName">The name of the function to run.</param>
262+
/// <param name="functionEntityArguments">Optional vector of entity pointers that should be passed into the Lua function. Their internal Lua states will not be accessible. Defaults to empty.</param>
263+
/// <param name="functionLiteralArguments">Optional vector of strings, that should be passed into the Lua function. Entries must be surrounded with escaped quotes (i.e.`\"`) they'll be passed in as-is, allowing them to act as booleans, etc.. Defaults to empty.</param>
264+
/// <returns>An error return value signaling sucess or any particular failure. Anything below 0 is an error signal.</returns>
265+
int RunScriptedFunction(std::string const &scriptPath, std::string const &functionName, std::vector<Entity *> functionEntityArguments = std::vector<Entity *>(), std::vector<std::string> functionLiteralArguments = std::vector<std::string>());
266+
244267
//////////////////////////////////////////////////////////////////////////////////////////
245268
// Virtual method: GetClass
246269
//////////////////////////////////////////////////////////////////////////////////////////
@@ -1751,7 +1774,7 @@ ENTITYALLOCATION(MovableObject)
17511774
/// Gets a vector containing the script function names this class supports.
17521775
/// </summary>
17531776
/// <returns>A vector containing the script function names this class supports.</returns>
1754-
virtual const std::vector<std::string> GetSupportedScriptFunctionNames() { return std::vector<std::string> {"Create", "Destroy", "Update", "OnPieMenu", "OnEnableScript", "OnDisableScript"}; }
1777+
virtual const std::vector<std::string> GetSupportedScriptFunctionNames() { return std::vector<std::string> {"Create", "Destroy", "Update", "OnPieMenu", "OnScriptRemoveOrDisable", "OnScriptEnable"}; }
17551778

17561779
//////////////////////////////////////////////////////////////////////////////////////////
17571780
// Virtual method: UpdateChildMOIDs

Managers/LuaMan.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,19 +2419,6 @@ bool LuaMan::ExpressionIsTrue(string expression, bool consoleErrors)
24192419
return result;
24202420
}
24212421

2422-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2423-
2424-
int LuaMan::RunFunctionInPresetScript(std::string const &functionName, std::string const &scriptPath, std::string const &presetLuaInstanceName, std::string const &objectLuaInstanceName) {
2425-
std::string presetAndFunctionName = presetLuaInstanceName + "." + functionName;
2426-
return RunScriptString(
2427-
"if " + presetAndFunctionName + " and " + objectLuaInstanceName + " then " +
2428-
presetAndFunctionName + "[\""+scriptPath+"\"](" + objectLuaInstanceName + "); " +
2429-
"end"
2430-
);
2431-
}
2432-
2433-
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2434-
24352422
//////////////////////////////////////////////////////////////////////////////////////////
24362423
// Method: RunScriptString
24372424
//////////////////////////////////////////////////////////////////////////////////////////

Managers/LuaMan.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,6 @@ enum ServerResult
227227

228228
bool ExpressionIsTrue(std::string expression, bool consoleErrors);
229229

230-
/// <summary>
231-
/// Helper method to run the given function in all of the given preset instance's scripts on the given object instance. Note that this method does not do any safety checks.
232-
/// </summary>
233-
/// <param name="functionName">The name of the function to run.</param>
234-
/// <param name="scriptPath">The path to the script whose function is being run. Used as an identifier in Lua.</param>
235-
/// <param name="presetLuaInstanceName">The name of the lua variable that holds the representation of the preset instance whose function is being run.</param>
236-
/// <param name="objectLuaInstanceName">The name of the lua variable that holds the representation of the object instance the function is being run on.</param>
237-
/// <returns>Returns less than zero if any errors encountered when running this script. To get the actual error string, call GetLastError.</returns>
238-
int LuaMan::RunFunctionInPresetScript(std::string const &functionName, std::string const &scriptPath, std::string const &presetLuaInstanceName, std::string const &objectLuaInstanceName);
239-
240-
241230
//////////////////////////////////////////////////////////////////////////////////////////
242231
// Method: RunScriptString
243232
//////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)