Skip to content

Commit

Permalink
Refactor console argument type checking and variable handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dgelessus committed Aug 10, 2023
1 parent 583dfdb commit 42fd1e0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 36 deletions.
74 changes: 38 additions & 36 deletions Sources/Plasma/FeatureLib/pfConsoleCore/pfConsoleEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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();
Expand All @@ -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, &paramArray[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"));
Expand Down Expand Up @@ -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), &paramArray[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.
Expand Down
3 changes: 3 additions & 0 deletions Sources/Plasma/FeatureLib/pfConsoleCore/pfConsoleEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit 42fd1e0

Please sign in to comment.