Skip to content

Commit

Permalink
Add compilation check for filter string for SetEventHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
llde committed Jul 30, 2023
1 parent 761004b commit 1476a9b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
31 changes: 30 additions & 1 deletion obse/obse/Commands_Script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,35 @@ static bool Cmd_DispatchEvent_Execute (COMMAND_ARGS)

#endif


static bool Cmd_SetEventHandler_Parse(UInt32 numParams, ParamInfo* paramInfo, ScriptLineBuffer* lineBuf, ScriptBuffer* scriptBuf) {
bool res = Cmd_Expression_Parse(numParams, paramInfo, lineBuf, scriptBuf);
char* context;
strtok_s(lineBuf->paramText, " ", &context); //TODO validate handler;
strtok_s(NULL, " ", &context); //Script already validated;
char* tok = strtok_s(NULL, " ", &context);
if (tok == NULL) return true; //No arguments specified
std::string inner = tok;
inner = inner.substr(0, inner.find(':'));
if (inner.find("\"") != std::string::npos) inner = inner.substr(1, inner.size() - 1);
bool valid = inner.starts_with("first") || inner.starts_with("ref") || inner.starts_with("second") || inner.starts_with("object");
if (!valid) {
CompilerMessages::Show(CompilerMessages::kError_InvalidEventFilter, scriptBuf, inner.data());
res = false;
}
tok = strtok_s(NULL, " ", &context);
if (tok == NULL) return true; //No more arguments specified
inner = tok;
inner = inner.substr(0, inner.find(':'));
if (inner.find("\"") != std::string::npos) inner = inner.substr(1, inner.size() - 1);
valid = inner.starts_with("first") || inner.starts_with("ref") || inner.starts_with("second") || inner.starts_with("object");
if (!valid) {
CompilerMessages::Show(CompilerMessages::kError_InvalidEventFilter, scriptBuf, inner.data());
res = false;
}
return res;
}

CommandInfo kCommandInfo_IsScripted =
{
"IsScripted",
Expand Down Expand Up @@ -794,7 +823,7 @@ CommandInfo kCommandInfo_SetEventHandler =
"defines a function script to serve as a callback for game events",
0, 4, kOBSEParams_SetEventHandler,
HANDLER(Cmd_SetEventHandler_Execute),
Cmd_Expression_Parse,
Cmd_SetEventHandler_Parse,
NULL,
0
};
Expand Down
9 changes: 4 additions & 5 deletions obse/obse/ScriptUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1752,11 +1752,7 @@ bool ExpressionParser::ParseArgs(ParamInfo* params, UInt32 numParams, bool bUses
{
if (!ValidateArgType(params[m_numArgsParsed].typeID, argType, bUsesOBSEParamTypes))
{
#if OBLIVION
ShowCompilerError(m_lineBuf, "Invalid expression for parameter %d. Expected %s.", m_numArgsParsed + 1, params[m_numArgsParsed].typeStr);
#else
ShowCompilerError(m_scriptBuf, "Invalid expression for parameter %d. Expected %s.", m_numArgsParsed + 1, params[m_numArgsParsed].typeStr);
#endif
CompilerMessages::Show(CompilerMessages::kError_InvalidParametersExpression, m_scriptBuf, m_numArgsParsed + 1, params[m_numArgsParsed].typeStr);
return false;
}
}
Expand Down Expand Up @@ -2194,6 +2190,9 @@ ErrOutput::Message CompilerMessages::s_Messages[] =
ErrOutput::Message ("Usage of ref variables as pointers to user-defined functions prevents type-checking of function arguments. Make sure the arguments provided match those expected by the function being called.", true, true),
ErrOutput::Message ("Command '%s' is deprecated. Consult the command documentation for an alternative command.", true, true),

"Argument filter string is not recognized : %s. Should be \"first\" or \"ref\" for the first filter and \"second\" or \"object\" for the second one",
"Invalid expression for parameter %d. Expected %s.",
ErrOutput::Message("Event %s is not found between the game events. User defined events must be manually dispatched", true, true),
// default
"Undefined message."
};
Expand Down
4 changes: 3 additions & 1 deletion obse/obse/ScriptUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ struct CompilerMessages
kWarning_UnquotedString, // string:unquotedString
kWarning_FunctionPointer,
kWarning_DeprecatedCommand, // string:commandName

kError_InvalidEventFilter, //events
kError_InvalidParametersExpression,
kWarning_EventNotAvailable,
kMessageCode_Max
};
private:
Expand Down

0 comments on commit 1476a9b

Please sign in to comment.