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

Commit 5e28692

Browse files
committed
Changed const ordering in MovableObject script methods, changed OnPieMenu method comment and moved OnPieMenu nearer the back of the supported script function names in MovableObject since it makes more sense
Moved scriptString construction logic into LuaMan so it can be used by things that aren't MovableObjects (i.e. GAScripted and GlobalScript). Also changed string in RunScript to be const &
1 parent f8f8023 commit 5e28692

File tree

4 files changed

+79
-48
lines changed

4 files changed

+79
-48
lines changed

Entities/MovableObject.cpp

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ int MovableObject::SetupScriptObjectNameAndRunLuaCreateFunctions() {
485485

486486
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
487487

488-
int MovableObject::LoadScript(std::string const &scriptPath, bool loadAsEnabledScript) {
488+
int MovableObject::LoadScript(const std::string &scriptPath, bool loadAsEnabledScript) {
489489
// Return an error if the script path is empty or already there
490490
if (scriptPath.empty()) {
491491
return -1;
@@ -574,7 +574,7 @@ int MovableObject::ReloadScripts() {
574574

575575
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
576576

577-
bool MovableObject::AddScript(std::string const &scriptPath) {
577+
bool MovableObject::AddScript(const std::string &scriptPath) {
578578
switch (LoadScript(scriptPath)) {
579579
case 0:
580580
// If we have a ScriptObjectName that means Create has already been run for pre-existing scripts. Run it right away for this one.
@@ -601,7 +601,7 @@ bool MovableObject::AddScript(std::string const &scriptPath) {
601601

602602
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
603603

604-
bool MovableObject::RemoveScript(std::string const &scriptPath) {
604+
bool MovableObject::RemoveScript(const std::string &scriptPath) {
605605
if (m_LoadedScripts.empty() || m_ScriptPresetName.empty()) {
606606
return false;
607607
}
@@ -620,7 +620,7 @@ bool MovableObject::RemoveScript(std::string const &scriptPath) {
620620

621621
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
622622

623-
bool MovableObject::EnableScript(std::string const &scriptPath) {
623+
bool MovableObject::EnableScript(const std::string &scriptPath) {
624624
if (m_LoadedScripts.empty() || m_ScriptPresetName.empty()) {
625625
return false;
626626
}
@@ -638,7 +638,7 @@ bool MovableObject::EnableScript(std::string const &scriptPath) {
638638

639639
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
640640

641-
bool MovableObject::DisableScript(std::string const &scriptPath) {
641+
bool MovableObject::DisableScript(const std::string &scriptPath) {
642642
if (m_LoadedScripts.empty() || m_ScriptPresetName.empty()) {
643643
return false;
644644
}
@@ -656,35 +656,20 @@ bool MovableObject::DisableScript(std::string const &scriptPath) {
656656

657657
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
658658

659-
int MovableObject::RunScriptedFunction(std::string const &scriptPath, std::string const &functionName, std::vector<Entity *> functionEntityArguments, std::vector<std::string> functionLiteralArguments) {
659+
int MovableObject::RunScriptedFunction(const std::string &scriptPath, const std::string &functionName, std::vector<Entity *> functionEntityArguments, std::vector<std::string> functionLiteralArguments) {
660660
if (m_LoadedScripts.empty() || m_ScriptPresetName.empty() || m_ScriptObjectName.empty()) {
661661
return -1;
662662
}
663663

664664
std::string presetAndFunctionName = m_ScriptPresetName + "." + functionName;
665-
std::string scriptString = "if " + presetAndFunctionName + " and " + m_ScriptObjectName + " and " + presetAndFunctionName + "[\"" + scriptPath + "\"] then ";
666-
if (!functionEntityArguments.empty()) {
667-
scriptString += "local entityArguments = LuaMan.TempEntities; ";
668-
}
669-
scriptString += presetAndFunctionName + "[\"" + scriptPath + "\"](" + m_ScriptObjectName;
670-
if (!functionEntityArguments.empty()) {
671-
g_LuaMan.SetTempEntityVector(functionEntityArguments);
672-
for (const Entity *functionEntityArgument : functionEntityArguments) {
673-
scriptString += ", To" + functionEntityArgument->GetClassName() + "(entityArguments())";
674-
}
675-
}
676-
if (!functionLiteralArguments.empty()) {
677-
for (const std::string functionLiteralArgument : functionLiteralArguments) {
678-
scriptString += ", " + functionLiteralArgument;
679-
}
680-
}
681-
scriptString += +"); end";
682-
683-
int status = g_LuaMan.RunScriptString(scriptString);
665+
std::string fullFunctionName = presetAndFunctionName + "[\"" + scriptPath + "\"]";
666+
667+
int status = g_LuaMan.RunScriptedFunction(fullFunctionName, m_ScriptObjectName, {presetAndFunctionName, m_ScriptObjectName, fullFunctionName}, functionEntityArguments, functionLiteralArguments);
684668
functionEntityArguments.clear();
685669
functionLiteralArguments.clear();
670+
686671
if (status < 0 && m_LoadedScripts.size() > 1) {
687-
g_ConsoleMan.PrintString("ERROR: An error occured while trying to run " + functionName + "function for script at path " + scriptPath);
672+
g_ConsoleMan.PrintString("ERROR: An error occured while trying to run the " + functionName + " function for script at path " + scriptPath);
688673
return -2;
689674
}
690675

@@ -693,7 +678,7 @@ int MovableObject::RunScriptedFunction(std::string const &scriptPath, std::strin
693678

694679
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
695680

696-
int MovableObject::RunScriptedFunctionInAppropriateScripts(std::string const &functionName, bool runOnDisabledScripts, bool stopOnError, std::vector<Entity *> functionEntityArguments, std::vector<std::string> functionLiteralArguments) {
681+
int MovableObject::RunScriptedFunctionInAppropriateScripts(const std::string &functionName, bool runOnDisabledScripts, bool stopOnError, std::vector<Entity *> functionEntityArguments, std::vector<std::string> functionLiteralArguments) {
697682
if (m_LoadedScripts.empty() || m_ScriptPresetName.empty() || m_ScriptObjectName.empty()) {
698683
return -1;
699684
}

Entities/MovableObject.h

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ ENTITYALLOCATION(MovableObject)
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>
186186
/// <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>
187-
virtual int LoadScript(std::string const &scriptPath, bool loadAsEnabledScript = true);
187+
virtual int LoadScript(const std::string &scriptPath, bool loadAsEnabledScript = true);
188188

189189
/// <summary>
190190
/// Reloads the all of the scripts on this object. This will also update the original preset in PresetMan with the updated scripts so future objects spawned will use the new scripts.
@@ -204,42 +204,42 @@ ENTITYALLOCATION(MovableObject)
204204
/// </summary>
205205
/// <param name="scriptPath">The path to the script to check.</param>
206206
/// <returns>Whether or not the script is on this MO.</returns>
207-
virtual bool const HasScript(std::string const &scriptPath) { return FindScript(scriptPath) != m_LoadedScripts.end(); }
207+
virtual bool const HasScript(const std::string &scriptPath) { return FindScript(scriptPath) != m_LoadedScripts.end(); }
208208

209209
/// <summary>
210210
/// Adds the script at the given path as one of the scripts on this MO.
211211
/// </summary>
212212
/// <param name="scriptPath">The path to the script to add.</param>
213213
/// <returns>Whether or not the script was successfully added.</returns>
214-
virtual bool AddScript(std::string const &scriptPath);
214+
virtual bool AddScript(const std::string &scriptPath);
215215

216216
/// <summary>
217217
/// Removes the script at the given path so it will no longer be one of the scripts on this MO.
218218
/// </summary>
219219
/// <param name="scriptPath">The path to the script to remove.</param>
220220
/// <returns>Whether or not the script was successfully removed.</returns>
221-
virtual bool RemoveScript(std::string const &scriptPath);
221+
virtual bool RemoveScript(const std::string &scriptPath);
222222

223223
/// <summary>
224224
/// Checks if the script at the given path is one of the enabled scripts on this MO.
225225
/// </summary>
226226
/// <param name="scriptPath">The path to the script to check.</param>
227227
/// <returns>Whether or not the script is enabled on this MO.</returns>
228-
virtual bool const ScriptEnabled(std::string const &scriptPath) { auto scriptIterator = FindScript(scriptPath); return scriptIterator != m_LoadedScripts.end() && scriptIterator->second == true; }
228+
virtual bool const ScriptEnabled(const std::string &scriptPath) { auto scriptIterator = FindScript(scriptPath); return scriptIterator != m_LoadedScripts.end() && scriptIterator->second == true; }
229229

230230
/// <summary>
231231
/// Enable the script at the given path on this MO.
232232
/// </summary>
233233
/// <param name="scriptPath">The path to the script to enable.</param>
234234
/// <returns>Whether or not the script was succesfully enabled.</returns>
235-
virtual bool EnableScript(std::string const &scriptPath);
235+
virtual bool EnableScript(const std::string &scriptPath);
236236

237237
/// <summary>
238238
/// Disables the script at the given path for this MO.
239239
/// </summary>
240240
/// <param name="scriptPath">The path to the script to disable.</param>
241241
/// <returns>Whether or not the script was succesfully disabled..</returns>
242-
virtual bool DisableScript(std::string const &scriptPath);
242+
virtual bool DisableScript(const std::string &scriptPath);
243243

244244
/// <summary>
245245
/// Runs the given function for the given script, with the given arguments. The first argument to the function will always be 'self'.
@@ -250,7 +250,7 @@ ENTITYALLOCATION(MovableObject)
250250
/// <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>
251251
/// <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>
252252
/// <returns>An error return value signaling sucess or any particular failure. Anything below 0 is an error signal.</returns>
253-
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>());
253+
int RunScriptedFunction(const std::string &scriptPath, const std::string &functionName, std::vector<Entity *> functionEntityArguments = std::vector<Entity *>(), std::vector<std::string> functionLiteralArguments = std::vector<std::string>());
254254

255255
/// <summary>
256256
/// 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.
@@ -262,7 +262,7 @@ ENTITYALLOCATION(MovableObject)
262262
/// <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>
263263
/// <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>
264264
/// <returns>An error return value signaling sucess or any particular failure. Anything below 0 is an error signal.</returns>
265-
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>());
265+
int RunScriptedFunctionInAppropriateScripts(const std::string &functionName, bool runOnDisabledScripts = false, bool stopOnError = false, std::vector<Entity *> functionEntityArguments = std::vector<Entity *>(), std::vector<std::string> functionLiteralArguments = std::vector<std::string>());
266266

267267
//////////////////////////////////////////////////////////////////////////////////////////
268268
// Virtual method: GetClass
@@ -1532,15 +1532,11 @@ ENTITYALLOCATION(MovableObject)
15321532

15331533
virtual int UpdateScripts();
15341534

1535-
1536-
//////////////////////////////////////////////////////////////////////////////////////////
1537-
// Virtual method: OnPieMenu
1538-
//////////////////////////////////////////////////////////////////////////////////////////
1539-
// Description: Executes the Lua-defined OnPieMenu event handler.
1540-
// Arguments: Actor which triggered the pie menu event
1541-
// Return value: An error return value signaling sucess or any particular failure.
1542-
// Anything below 0 is an error signal.
1543-
1535+
/// <summary>
1536+
/// Executes the Lua-defined OnPieMenu event handler for this MO.
1537+
/// </summary>
1538+
/// <param name="pActor">Actor which triggered the pie menu event.</param>
1539+
/// <returns>An error return value signaling sucess or any particular failure. Anything below 0 is an error signal.</returns>
15441540
virtual int OnPieMenu(Actor *pActor);
15451541

15461542

@@ -1774,7 +1770,7 @@ ENTITYALLOCATION(MovableObject)
17741770
/// Gets a vector containing the script function names this class supports.
17751771
/// </summary>
17761772
/// <returns>A vector containing the script function names this class supports.</returns>
1777-
virtual const std::vector<std::string> GetSupportedScriptFunctionNames() { return std::vector<std::string> {"Create", "Destroy", "Update", "OnPieMenu", "OnScriptRemoveOrDisable", "OnScriptEnable"}; }
1773+
virtual const std::vector<std::string> GetSupportedScriptFunctionNames() { return std::vector<std::string> {"Create", "Destroy", "Update", "OnScriptRemoveOrDisable", "OnScriptEnable", "OnPieMenu"}; }
17781774

17791775
/// <summary>
17801776
/// Does necessary work to setup a script object name for this object, allowing it to be accessed in Lua, then runs all of the MO's scripts' Create functions in Lua.

Managers/LuaMan.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2421,13 +2421,51 @@ bool LuaMan::ExpressionIsTrue(string expression, bool consoleErrors)
24212421
return result;
24222422
}
24232423

2424+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2425+
2426+
int LuaMan::RunScriptedFunction(const std::string &functionName, const std::string &selfObjectName, std::vector<std::string> variablesToSafetyCheck, std::vector<Entity *> functionEntityArguments, std::vector<std::string> functionLiteralArguments) {
2427+
std::string scriptString = "";
2428+
if (!variablesToSafetyCheck.empty()) {
2429+
scriptString += "if ";
2430+
for (const std::string &variableToSafetyCheck : variablesToSafetyCheck) {
2431+
if (&variableToSafetyCheck != &variablesToSafetyCheck[0]) {
2432+
scriptString += " and ";
2433+
}
2434+
scriptString += variableToSafetyCheck;
2435+
}
2436+
scriptString += " then ";
2437+
}
2438+
if (!functionEntityArguments.empty()) {
2439+
scriptString += "local entityArguments = LuaMan.TempEntities; ";
2440+
}
2441+
scriptString += functionName + "(" + selfObjectName;
2442+
if (!functionEntityArguments.empty()) {
2443+
g_LuaMan.SetTempEntityVector(functionEntityArguments);
2444+
for (const Entity *functionEntityArgument : functionEntityArguments) {
2445+
scriptString += ", To" + functionEntityArgument->GetClassName() + "(entityArguments())";
2446+
}
2447+
}
2448+
if (!functionLiteralArguments.empty()) {
2449+
for (const std::string functionLiteralArgument : functionLiteralArguments) {
2450+
scriptString += ", " + functionLiteralArgument;
2451+
}
2452+
}
2453+
scriptString += ");";
2454+
2455+
if (!variablesToSafetyCheck.empty()) { scriptString += " end"; };
2456+
2457+
return RunScriptString(scriptString);
2458+
}
2459+
2460+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2461+
24242462
//////////////////////////////////////////////////////////////////////////////////////////
24252463
// Method: RunScriptString
24262464
//////////////////////////////////////////////////////////////////////////////////////////
24272465
// Description: Takes a string containing a script snippet and runs it on the master
24282466
// state.
24292467

2430-
int LuaMan::RunScriptString(string scriptString, bool consoleErrors)
2468+
int LuaMan::RunScriptString(const std::string &scriptString, bool consoleErrors)
24312469
{
24322470
if (scriptString.empty())
24332471
return -1;

Managers/LuaMan.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,18 @@ enum ServerResult
227227

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

230+
/// <summary>
231+
/// Runs the given Lua function with optional safety checks and arguments. The first argument to the function will always be the self object.
232+
/// If either argument list has entries, they will be passed into the function in order, with entity arguments first.
233+
/// </summary>
234+
/// <param name="functionName">The name that gives access to the function in the global Lua namespace.</param>
235+
/// <param name="selfObjectName">The name that gives access to the self object in the global Lua namespace.</param>
236+
/// <param name="variablesToSafetyCheck">Optional vector of strings that should be safety checked in order before running the Lua function. Defaults to empty.</param>
237+
/// <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>
238+
/// <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>
239+
/// <returns>An error return value signaling sucess or any particular failure. Anything below 0 is an error signal.</returns>
240+
int RunScriptedFunction(const std::string &functionName, const std::string &selfObjectName, std::vector<std::string> variablesToSafetyCheck = std::vector<std::string>(), std::vector<Entity *> functionEntityArguments = std::vector<Entity *>(), std::vector<std::string> functionLiteralArguments = std::vector<std::string>());
241+
230242
//////////////////////////////////////////////////////////////////////////////////////////
231243
// Method: RunScriptString
232244
//////////////////////////////////////////////////////////////////////////////////////////
@@ -237,7 +249,7 @@ enum ServerResult
237249
// Return value: Returns less than zero if any errors encountered when running this script.
238250
// To get the actual error string, call GetLastError.
239251

240-
int RunScriptString(std::string scriptString, bool consoleErrors = true);
252+
int RunScriptString(const std::string &scriptString, bool consoleErrors = true);
241253

242254

243255
//////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)