diff --git a/Sources/Plasma/FeatureLib/pfConsoleCore/pfConsoleEngine.cpp b/Sources/Plasma/FeatureLib/pfConsoleCore/pfConsoleEngine.cpp index 50fb3ba22c..5393840e5b 100644 --- a/Sources/Plasma/FeatureLib/pfConsoleCore/pfConsoleEngine.cpp +++ b/Sources/Plasma/FeatureLib/pfConsoleCore/pfConsoleEngine.cpp @@ -53,7 +53,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include #include #include -#include #include "plFile/plEncryptedStream.h" @@ -188,9 +187,7 @@ bool pfConsoleEngine::ExecuteFile(const plFileName &fileName) bool pfConsoleEngine::RunCommand(const ST::string& line, void (*PrintFn)(const ST::string&)) { pfConsoleCmd *cmd; - int32_t numParams, i; pfConsoleCmdParam paramArray[ fMaxNumParams + 1 ]; - bool valid = true; pfConsoleParser parser(line); cmd = parser.ParseCommand(); @@ -210,40 +207,12 @@ bool pfConsoleEngine::RunCommand(const ST::string& line, void (*PrintFn)(const S return false; } - for (numParams = 0; numParams < fMaxNumParams - && numParams < argTokens->size() - && valid; numParams++ ) - { - // Special case for context variables--if we're specifying one, we want to just grab - // the value of it and return that instead - valid = false; - ST::string argString = (*argTokens)[numParams]; - if (argString.starts_with("$")) { - pfConsoleContext &context = pfConsoleContext::GetRootContext(); + hsSsize_t numParams = IResolveParams(cmd, std::move(*argTokens), paramArray); - // Potential variable, see if we can find it - hsSsize_t idx = context.FindVar(argString.substr(1)); - if( idx == -1 ) - { - fErrorMsg = ST_LITERAL("Invalid console variable name"); - } - else - { - paramArray[ numParams ] = context.GetVarValue( idx ); - valid = true; - } - } - - if( !valid ) - valid = IConvertToParam(cmd->GetSigEntry(numParams), argString, ¶mArray[numParams]); - } - - for( i = numParams; i < fMaxNumParams + 1; i++ ) - paramArray[ i ].SetNone(); - - if (!valid || (cmd->GetSigEntry(numParams) != pfConsoleCmd::kAny && - cmd->GetSigEntry(numParams) != pfConsoleCmd::kNone)) - { + if (numParams == -1 || ( + cmd->GetSigEntry(numParams) != pfConsoleCmd::kAny + && cmd->GetSigEntry(numParams) != pfConsoleCmd::kNone + )) { // Print help string and return fErrorMsg.clear(); // Printed on next line PrintFn(ST_LITERAL("Invalid parameters to command")); @@ -309,6 +278,39 @@ bool pfConsoleEngine::IConvertToParam(uint8_t type, ST::string string, pfConsole return true; } +hsSsize_t pfConsoleEngine::IResolveParams(pfConsoleCmd* cmd, std::vector argTokens, pfConsoleCmdParam* paramArray) +{ + size_t numParams; + for (numParams = 0; numParams < fMaxNumParams && numParams < argTokens.size(); numParams++) { + // Special case for context variables--if we're specifying one, + // we want to just grab the value of it and return that instead + ST::string& argString = argTokens[numParams]; + if (argString.starts_with("$")) { + pfConsoleContext& context = pfConsoleContext::GetRootContext(); + + // Potential variable, see if we can find it + hsSsize_t idx = context.FindVar(argString.substr(1)); + if (idx == -1) { + fErrorMsg = ST_LITERAL("Invalid console variable name"); + } else { + paramArray[numParams] = context.GetVarValue(idx); + continue; + } + } + + bool valid = IConvertToParam(cmd->GetSigEntry(numParams), std::move(argString), ¶mArray[numParams]); + if (!valid) { + return -1; + } + } + + for (size_t i = numParams; i < fMaxNumParams + 1; i++) { + paramArray[i].SetNone(); + } + + return numParams; +} + //// FindPartialCmd ////////////////////////////////////////////////////////// // Given a string which is the beginning of a console command, // returns the best match of command (or group) for that string. diff --git a/Sources/Plasma/FeatureLib/pfConsoleCore/pfConsoleEngine.h b/Sources/Plasma/FeatureLib/pfConsoleCore/pfConsoleEngine.h index 0b3c25421b..bcaa89c9c6 100644 --- a/Sources/Plasma/FeatureLib/pfConsoleCore/pfConsoleEngine.h +++ b/Sources/Plasma/FeatureLib/pfConsoleCore/pfConsoleEngine.h @@ -58,11 +58,13 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "HeadSpin.h" #include +#include class plFileName; //// pfConsoleEngine Class Definition //////////////////////////////////////// +class pfConsoleCmd; class pfConsoleCmdParam; class pfConsoleCmdGroup; class pfConsoleEngine @@ -72,6 +74,7 @@ class pfConsoleEngine static const int32_t fMaxNumParams; bool IConvertToParam(uint8_t type, ST::string string, pfConsoleCmdParam *param); + hsSsize_t IResolveParams(pfConsoleCmd* cmd, std::vector argTokens, pfConsoleCmdParam* paramArray); ST::string fErrorMsg; ST::string fLastErrorLine;