| 1 |
#include "GuiScript.h" |
| 2 |
|
| 3 |
#include "itextstream.h" |
| 4 |
#include "parser/DefTokeniser.h" |
| 5 |
#include "Gui.h" |
| 6 |
#include "GuiExpression.h" |
| 7 |
#include "Variable.h" |
| 8 |
|
| 9 |
#include "string/case_conv.h" |
| 10 |
#include "string/trim.h" |
| 11 |
#include "string/predicate.h" |
| 12 |
|
| 13 |
namespace gui |
| 14 |
{ |
| 15 |
|
| 16 |
GuiScript::GuiScript(GuiWindowDef& owner) : |
| 17 |
_owner(owner), |
| 18 |
_ip(0), |
| 19 |
_curLevel(0) |
| 20 |
{} |
| 21 |
|
| 22 |
void GuiScript::parseIfStatement(parser::DefTokeniser& tokeniser) |
| 23 |
{ |
| 24 |
// Prototype: if (<condition>) <statement> [else <statement>] |
| 25 |
// The initial "if" has already been parsed |
| 26 |
StatementPtr ifStatement(new Statement(Statement::ST_IF)); |
| 27 |
|
| 28 |
// Any opening and closing parentheses are handled by the expression parser |
| 29 |
ifStatement->_condition = getIfExpression(tokeniser); // condition |
| 30 |
|
| 31 |
// Add the statement at the current position |
| 32 |
pushStatement(ifStatement); |
| 33 |
|
| 34 |
// Parse the statement(s) to execute if the above condition is true |
| 35 |
parseStatement(tokeniser); |
| 36 |
|
| 37 |
// Check the next token to see where we need to jump to if the condition evaluates to false |
| 38 |
std::string nextToken = tokeniser.nextToken(); |
| 39 |
|
| 40 |
if (nextToken == "else") |
| 41 |
{ |
| 42 |
// There is an "else" block, so we need to add a JMP statement before proceeding |
| 43 |
StatementPtr jmpStatement(new Statement(Statement::ST_JMP)); |
| 44 |
pushStatement(jmpStatement); |
| 45 |
|
| 46 |
// Set the original IF jump position to this else |
| 47 |
ifStatement->jmpDest = getCurPosition(); |
| 48 |
|
| 49 |
// As next step, parse the code in the else block |
| 50 |
parseStatement(tokeniser); |
| 51 |
|
| 52 |
// Finally, position the jump at the location right after the else block |
| 53 |
jmpStatement->jmpDest = getCurPosition(); |
| 54 |
} |
| 55 |
else |
| 56 |
{ |
| 57 |
// No else, execution falls through, but we need to set the jump destination first |
| 58 |
ifStatement->jmpDest = getCurPosition(); |
| 59 |
|
| 60 |
switchOnToken(nextToken, tokeniser); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
void GuiScript::parseSetStatement(parser::DefTokeniser& tokeniser) |
| 65 |
{ |
| 66 |
// Prototype: set [window::]<variable> <value> |
| 67 |
StatementPtr st(new Statement(Statement::ST_SET)); |
| 68 |
|
| 69 |
st->args.push_back(_owner.parseString(tokeniser)); // variable |
| 70 |
|
| 71 |
// Add all tokens up to the semicolon as arguments |
| 72 |
while (true) |
| 73 |
{ |
| 74 |
std::string token = tokeniser.peek(); |
| 75 |
|
| 76 |
// Sometimes the semicolon is missing |
| 77 |
if (token == ";" || token == "}") break; |
| 78 |
|
| 79 |
st->args.push_back(ConstantExpression<std::string>::Create(tokeniser.nextToken())); // argument |
| 80 |
} |
| 81 |
|
| 82 |
pushStatement(st); |
| 83 |
} |
| 84 |
|
| 85 |
void GuiScript::parseTransitionStatement(parser::DefTokeniser& tokeniser) |
| 86 |
{ |
| 87 |
// Prototype: transition [window::]<variable> <from> <to> <time> [ <accel> <decel> ] |
| 88 |
StatementPtr st(new Statement(Statement::ST_TRANSITION)); |
| 89 |
|
| 90 |
st->args.push_back(ConstantExpression<std::string>::Create(tokeniser.nextToken())); // variable |
| 91 |
|
| 92 |
st->args.push_back(ConstantExpression<std::string>::Create(tokeniser.nextToken())); // from |
| 93 |
st->args.push_back(ConstantExpression<std::string>::Create(tokeniser.nextToken())); // to |
| 94 |
st->args.push_back(ConstantExpression<std::string>::Create(tokeniser.nextToken())); // time |
| 95 |
|
| 96 |
if (tokeniser.peek() != ";") |
| 97 |
{ |
| 98 |
// no semicolon, parse optional acceleration and deceleration |
| 99 |
st->args.push_back(ConstantExpression<std::string>::Create(tokeniser.nextToken())); // accel |
| 100 |
st->args.push_back(ConstantExpression<std::string>::Create(tokeniser.nextToken())); // decel |
| 101 |
|
| 102 |
tokeniser.assertNextToken(";"); |
| 103 |
} |
| 104 |
else |
| 105 |
{ |
| 106 |
tokeniser.nextToken(); // pull semicolon |
| 107 |
} |
| 108 |
|
| 109 |
pushStatement(st); |
| 110 |
} |
| 111 |
|
| 112 |
void GuiScript::parseSetFocusStatement(parser::DefTokeniser& tokeniser) |
| 113 |
{ |
| 114 |
// Prototype: setFocus <window> |
| 115 |
StatementPtr st(new Statement(Statement::ST_SET_FOCUS)); |
| 116 |
|
| 117 |
st->args.push_back(_owner.parseString(tokeniser)); // window |
| 118 |
tokeniser.assertNextToken(";"); |
| 119 |
|
| 120 |
pushStatement(st); |
| 121 |
} |
| 122 |
|
| 123 |
void GuiScript::parseEndGameStatement(parser::DefTokeniser& tokeniser) |
| 124 |
{ |
| 125 |
// Prototype: endGame |
| 126 |
StatementPtr st(new Statement(Statement::ST_ENDGAME)); |
| 127 |
tokeniser.assertNextToken(";"); |
| 128 |
|
| 129 |
pushStatement(st); |
| 130 |
} |
| 131 |
|
| 132 |
void GuiScript::parseResetTimeStatement(parser::DefTokeniser& tokeniser) |
| 133 |
{ |
| 134 |
// Prototype: resetTime [<window>] [<time>] |
| 135 |
StatementPtr st(new Statement(Statement::ST_RESET_TIME)); |
| 136 |
|
| 137 |
std::string token = tokeniser.peek(); |
| 138 |
|
| 139 |
if (token != ";") |
| 140 |
{ |
| 141 |
// Check if this is a numeric token |
| 142 |
try |
| 143 |
{ |
| 144 |
// Remove quotes from string before checking numerics |
| 145 |
std::string trimmed = string::trim_copy(token, "\""); |
| 146 |
|
| 147 |
// Try to cast the string to a number, throws |
| 148 |
std::stoul(trimmed); |
| 149 |
|
| 150 |
// Cast succeeded this is just the time for the current window |
| 151 |
st->args.push_back(_owner.parseString(tokeniser)); |
| 152 |
} |
| 153 |
catch (std::logic_error&) |
| 154 |
{ |
| 155 |
// Not a number, must be window plus time |
| 156 |
st->args.push_back(_owner.parseString(tokeniser)); // window |
| 157 |
st->args.push_back(_owner.parseString(tokeniser)); // time |
| 158 |
} |
| 159 |
|
| 160 |
// Reset statement complete, check the next token, as some |
| 161 |
// GUI scripts don't bother writing an ending semicolon before the closing brace |
| 162 |
std::string next = tokeniser.peek(); |
| 163 |
|
| 164 |
if (next == ";" || next == "}") |
| 165 |
{ |
| 166 |
tokeniser.nextToken(); // exhaust |
| 167 |
} |
| 168 |
else |
| 169 |
{ |
| 170 |
tokeniser.assertNextToken(";"); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
pushStatement(st); |
| 175 |
} |
| 176 |
|
| 177 |
void GuiScript::parseShowCursorStatement(parser::DefTokeniser& tokeniser) |
| 178 |
{ |
| 179 |
// Prototype: showCursor <bool> |
| 180 |
StatementPtr st(new Statement(Statement::ST_SHOW_CURSOR)); |
| 181 |
|
| 182 |
st->args.push_back(_owner.parseString(tokeniser)); // boolean |
| 183 |
tokeniser.assertNextToken(";"); |
| 184 |
|
| 185 |
pushStatement(st); |
| 186 |
} |
| 187 |
|
| 188 |
void GuiScript::parseResetCinematicStatement(parser::DefTokeniser& tokeniser) |
| 189 |
{ |
| 190 |
// Prototype: resetCinematics |
| 191 |
StatementPtr st(new Statement(Statement::ST_RESET_CINEMATICS)); |
| 192 |
tokeniser.assertNextToken(";"); |
| 193 |
|
| 194 |
pushStatement(st); |
| 195 |
} |
| 196 |
|
| 197 |
void GuiScript::parseLocalSoundStatement(parser::DefTokeniser& tokeniser) |
| 198 |
{ |
| 199 |
// Prototype: localSound <sound> |
| 200 |
StatementPtr st(new Statement(Statement::ST_LOCALSOUND)); |
| 201 |
|
| 202 |
st->args.push_back(_owner.parseString(tokeniser)); // sound |
| 203 |
tokeniser.assertNextToken(";"); |
| 204 |
|
| 205 |
pushStatement(st); |
| 206 |
} |
| 207 |
|
| 208 |
void GuiScript::parseRunScriptStatement(parser::DefTokeniser& tokeniser) |
| 209 |
{ |
| 210 |
// Prototype: runScript <function> |
| 211 |
StatementPtr st(new Statement(Statement::ST_RUNSCRIPT)); |
| 212 |
|
| 213 |
st->args.push_back(_owner.parseString(tokeniser)); // function |
| 214 |
tokeniser.assertNextToken(";"); |
| 215 |
|
| 216 |
pushStatement(st); |
| 217 |
} |
| 218 |
|
| 219 |
void GuiScript::parseEvalRegsStatement(parser::DefTokeniser& tokeniser) |
| 220 |
{ |
| 221 |
// Prototype: evalRegs |
| 222 |
StatementPtr st(new Statement(Statement::ST_EVALREGS)); |
| 223 |
tokeniser.assertNextToken(";"); |
| 224 |
|
| 225 |
pushStatement(st); |
| 226 |
} |
| 227 |
|
| 228 |
void GuiScript::switchOnToken(const std::string& token, parser::DefTokeniser& tokeniser) |
| 229 |
{ |
| 230 |
if (token == "}") |
| 231 |
{ |
| 232 |
assert(_curLevel > 0); |
| 233 |
_curLevel--; |
| 234 |
} |
| 235 |
else if (token == "{") |
| 236 |
{ |
| 237 |
std::size_t blockLevel = ++_curLevel; |
| 238 |
|
| 239 |
// Another block, a group of statements, enter recursion |
| 240 |
while (tokeniser.hasMoreTokens() && _curLevel == blockLevel) |
| 241 |
{ |
| 242 |
std::string nextToken = tokeniser.nextToken(); |
| 243 |
string::to_lower(nextToken); |
| 244 |
|
| 245 |
switchOnToken(nextToken, tokeniser); |
| 246 |
} |
| 247 |
} |
| 248 |
else if (token == "set") |
| 249 |
{ |
| 250 |
parseSetStatement(tokeniser); |
| 251 |
} |
| 252 |
else if (token == "transition") |
| 253 |
{ |
| 254 |
parseTransitionStatement(tokeniser); |
| 255 |
} |
| 256 |
else if (token == "if") |
| 257 |
{ |
| 258 |
parseIfStatement(tokeniser); |
| 259 |
} |
| 260 |
else if (token == "setfocus") |
| 261 |
{ |
| 262 |
parseSetFocusStatement(tokeniser); |
| 263 |
} |
| 264 |
else if (token == "endgame") |
| 265 |
{ |
| 266 |
parseEndGameStatement(tokeniser); |
| 267 |
} |
| 268 |
else if (token == "resettime") |
| 269 |
{ |
| 270 |
parseResetTimeStatement(tokeniser); |
| 271 |
} |
| 272 |
else if (token == "resetcinematics") |
| 273 |
{ |
| 274 |
parseResetCinematicStatement(tokeniser); |
| 275 |
} |
| 276 |
else if (token == "showcursor") |
| 277 |
{ |
| 278 |
parseShowCursorStatement(tokeniser); |
| 279 |
} |
| 280 |
else if (token == "localsound") |
| 281 |
{ |
| 282 |
parseLocalSoundStatement(tokeniser); |
| 283 |
} |
| 284 |
else if (token == "runscript") |
| 285 |
{ |
| 286 |
parseRunScriptStatement(tokeniser); |
| 287 |
} |
| 288 |
else if (token == "evalregs") |
| 289 |
{ |
| 290 |
parseEvalRegsStatement(tokeniser); |
| 291 |
} |
| 292 |
else if (token == ";") |
| 293 |
{ |
| 294 |
// A single semicolon is also a valid statement, do nothing |
| 295 |
} |
| 296 |
else |
| 297 |
{ |
| 298 |
rWarning() << "Unknown token " << token << " in GUI script in " |
| 299 |
<< _owner.name << std::endl; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
void GuiScript::parseStatement(parser::DefTokeniser& tokeniser) |
| 304 |
{ |
| 305 |
if (!tokeniser.hasMoreTokens()) |
| 306 |
{ |
| 307 |
return; |
| 308 |
} |
| 309 |
|
| 310 |
std::string token = tokeniser.nextToken(); |
| 311 |
string::to_lower(token); |
| 312 |
|
| 313 |
switchOnToken(token, tokeniser); |
| 314 |
} |
| 315 |
|
| 316 |
void GuiScript::constructFromTokens(parser::DefTokeniser& tokeniser) |
| 317 |
{ |
| 318 |
// Remove any previous statements |
| 319 |
_statements.clear(); |
| 320 |
_ip = 0; |
| 321 |
|
| 322 |
// Treat the upcoming { } block as "Statement" |
| 323 |
parseStatement(tokeniser); |
| 324 |
} |
| 325 |
|
| 326 |
std::size_t GuiScript::getCurPosition() |
| 327 |
{ |
| 328 |
return _statements.size(); |
| 329 |
} |
| 330 |
|
| 331 |
std::size_t GuiScript::pushStatement(const StatementPtr& statement) |
| 332 |
{ |
| 333 |
_statements.push_back(statement); |
| 334 |
|
| 335 |
return _statements.size() - 1; |
| 336 |
} |
| 337 |
|
| 338 |
GuiExpressionPtr GuiScript::getExpression(parser::DefTokeniser& tokeniser) |
| 339 |
{ |
| 340 |
return GuiExpression::CreateFromTokens(_owner.getGui(), tokeniser); |
| 341 |
} |
| 342 |
|
| 343 |
std::shared_ptr<IGuiExpression<bool>> GuiScript::getIfExpression(parser::DefTokeniser& tokeniser) |
| 344 |
{ |
| 345 |
// Parse the IF condition and pack it into a bool-typed expression |
| 346 |
return std::make_shared<TypedExpression<bool>>(getExpression(tokeniser)); |
| 347 |
} |
| 348 |
|
| 349 |
const Statement& GuiScript::getStatement(std::size_t index) |
| 350 |
{ |
| 351 |
assert(index < _statements.size()); |
| 352 |
return *_statements[index]; |
| 353 |
} |
| 354 |
|
| 355 |
VariablePtr GuiScript::getVariableFromExpression(const std::shared_ptr<IGuiExpression<std::string>>& expression) |
| 356 |
{ |
| 357 |
std::string expr = expression->evaluate(); |
| 358 |
|
| 359 |
// Not a gui:: variable, check if a namespace has been specified |
| 360 |
std::size_t ddPos = expr.find("::"); |
| 361 |
|
| 362 |
if (ddPos != std::string::npos) |
| 363 |
{ |
| 364 |
// Retrieve the windowDef name |
| 365 |
std::string windowDefName = expr.substr(0, ddPos); |
| 366 |
|
| 367 |
if (windowDefName == "gui") |
| 368 |
{ |
| 369 |
// Is a GUI state variable |
| 370 |
return std::make_shared<GuiStateVariable>(_owner.getGui(), expr.substr(ddPos + 2)); |
| 371 |
} |
| 372 |
|
| 373 |
// Look up the windowDef |
| 374 |
IGuiWindowDefPtr windowDef = _owner.getGui().findWindowDef(windowDefName); |
| 375 |
|
| 376 |
if (windowDef) |
| 377 |
{ |
| 378 |
// Cut off the "<windowDef>::" from the name |
| 379 |
return std::make_shared<AssignableWindowVariable>(*windowDef, expr.substr(ddPos+2)); |
| 380 |
} |
| 381 |
else |
| 382 |
{ |
| 383 |
rWarning() << "GUI Script: unknown windowDef " << windowDefName << std::endl; |
| 384 |
return VariablePtr(); |
| 385 |
} |
| 386 |
} |
| 387 |
else |
| 388 |
{ |
| 389 |
// Use the owner windowDef if no namespace was defined |
| 390 |
return std::make_shared<AssignableWindowVariable>(_owner, expr); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
std::string GuiScript::getValueFromExpression(const std::shared_ptr<IGuiExpression<std::string>>& expr) |
| 395 |
{ |
| 396 |
std::string value = expr->evaluate(); |
| 397 |
|
| 398 |
if (string::starts_with(value, "$gui::")) |
| 399 |
{ |
| 400 |
// This is the value of a GUI state variable |
| 401 |
return _owner.getGui().getStateString(value.substr(6)); |
| 402 |
} |
| 403 |
|
| 404 |
return value; |
| 405 |
} |
| 406 |
|
| 407 |
void GuiScript::execute() |
| 408 |
{ |
| 409 |
// Go back to the beginning |
| 410 |
_ip = 0; |
| 411 |
|
| 412 |
while (_ip < _statements.size()) |
| 413 |
{ |
| 414 |
const Statement& st = getStatement(_ip++); |
| 415 |
|
| 416 |
switch (st.type) |
| 417 |
{ |
| 418 |
case Statement::ST_NOP: |
| 419 |
break; |
| 420 |
case Statement::ST_JMP: |
| 421 |
_ip = st.jmpDest; |
| 422 |
break; |
| 423 |
case Statement::ST_SET: |
| 424 |
if (st.args.size() == 2) |
| 425 |
{ |
| 426 |
// Try to find the target variable |
| 427 |
VariablePtr var = getVariableFromExpression(st.args[0]); |
| 428 |
|
| 429 |
if (!var) |
| 430 |
{ |
| 431 |
rWarning() << "Cannot assign to variable " << st.args[0] << std::endl; |
| 432 |
continue; |
| 433 |
} |
| 434 |
|
| 435 |
std::string value = getValueFromExpression(st.args[1]); |
| 436 |
|
| 437 |
if (!var->assignValueFromString(value)) |
| 438 |
{ |
| 439 |
rWarning() << "Cannot assign value " << st.args[1] << " to variable " << st.args[1] << std::endl; |
| 440 |
} |
| 441 |
} |
| 442 |
break; |
| 443 |
case Statement::ST_TRANSITION: |
| 444 |
break; |
| 445 |
case Statement::ST_IF: |
| 446 |
// Evaluate expression |
| 447 |
if (!st._condition || st._condition->evaluate() == false) |
| 448 |
{ |
| 449 |
// Expression evaluated to false, jump over the block |
| 450 |
_ip = st.jmpDest; |
| 451 |
} |
| 452 |
break; |
| 453 |
case Statement::ST_SET_FOCUS: |
| 454 |
break; |
| 455 |
case Statement::ST_ENDGAME: |
| 456 |
break; |
| 457 |
case Statement::ST_RESET_TIME: |
| 458 |
break; |
| 459 |
case Statement::ST_SHOW_CURSOR: |
| 460 |
break; |
| 461 |
case Statement::ST_RESET_CINEMATICS: |
| 462 |
break; |
| 463 |
case Statement::ST_LOCALSOUND: |
| 464 |
break; |
| 465 |
case Statement::ST_RUNSCRIPT: |
| 466 |
break; |
| 467 |
case Statement::ST_EVALREGS: |
| 468 |
break; |
| 469 |
}; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
} // namespace |