-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor console argument type checking and variable handling
- Loading branch information
Showing
2 changed files
with
41 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,6 @@ You can contact Cyan Worlds, Inc. by email [email protected] | |
#include <string_theory/format> | ||
#include <string_theory/string_stream> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#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<ST::string> 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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,11 +58,13 @@ You can contact Cyan Worlds, Inc. by email [email protected] | |
#include "HeadSpin.h" | ||
|
||
#include <string_theory/string> | ||
#include <vector> | ||
|
||
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<ST::string> argTokens, pfConsoleCmdParam* paramArray); | ||
|
||
ST::string fErrorMsg; | ||
ST::string fLastErrorLine; | ||
|