Permalink
Browse files

MUTATIONOFJB: Fix issue with parsing #MACRO and #STARTUP right after …

…end block command.
  • Loading branch information...
LubomirR committed Apr 15, 2018
1 parent 1ee729e commit 01b5ca1ca68a116a00fb60dd36d2899ff6953ef7
Showing with 53 additions and 20 deletions.
  1. +41 −18 engines/mutationofjb/commands/endblockcommand.cpp
  2. +12 −2 engines/mutationofjb/commands/endblockcommand.h
@@ -61,6 +61,7 @@ bool EndBlockCommandParser::parse(const Common::String &line, ScriptParseContext
}

// This is the start or end of section/block.
command = new EndBlockCommand();

if (line.size() >= 4 && (line.hasPrefix("#L ") || line.hasPrefix("-L "))) {
ActionInfo ai = {ActionInfo::Look, line.c_str() + 3, "", firstChar == '#', nullptr};
@@ -112,21 +113,23 @@ bool EndBlockCommandParser::parse(const Common::String &line, ScriptParseContext
_ifTag = line[5];
}
} else if (line.size() >= 8 && line.hasPrefix("#MACRO")) {
_foundMacro = line.c_str() + 7;
NameAndCommand nc = {line.c_str() + 7, command};
_foundMacros.push_back(nc);
} else if (line.size() >= 10 && line.hasPrefix("#STARTUP")) {
_foundStartup = line.c_str() + 9;
const uint8 startupId = atoi(line.c_str() + 9);
IdAndCommand ic = {startupId, command};
_foundStartups.push_back(ic);
}

if (firstChar == '#') {
_hashFound = true;
}

command = new EndBlockCommand();

return true;
}

void EndBlockCommandParser::transition(ScriptParseContext &parseCtx, Command *, Command *newCommand, CommandParser *newCommandParser) {
void EndBlockCommandParser::transition(ScriptParseContext &parseCtx, Command *oldCommand, Command *newCommand, CommandParser *newCommandParser) {
if (_elseFound || _hashFound) {
if (newCommand) {
ScriptParseContext::ConditionalCommandInfos::iterator it = parseCtx._pendingCondCommands.begin();
@@ -146,26 +149,39 @@ void EndBlockCommandParser::transition(ScriptParseContext &parseCtx, Command *,
_ifTag = 0;
}

if (!_foundMacro.empty()) {
if (!_foundMacros.empty()) {
if (newCommand) {
if (!parseCtx._macros.contains(_foundMacro)) {
parseCtx._macros[_foundMacro] = newCommand;
} else {
warning(_("Macro '%s' already exists."), _foundMacro.c_str());
for (NameAndCommandArray::iterator it = _foundMacros.begin(); it != _foundMacros.end();) {
if (it->_command != oldCommand) {
it++;
continue;
}

if (!parseCtx._macros.contains(it->_name)) {
parseCtx._macros[it->_name] = newCommand;
} else {
warning(_("Macro '%s' already exists"), it->_name.c_str());
}
it = _foundMacros.erase(it);
}
}
_foundMacro.clear();
}
if (!_foundStartup.empty()) {
if (!_foundStartups.empty()) {
if (newCommand) {
const uint8 startupId = atoi(_foundStartup.c_str());
if (!parseCtx._startups.contains(startupId)) {
parseCtx._startups[startupId] = newCommand;
} else {
warning(_("Startup %u already exists."), (unsigned int) startupId);
for (IdAndCommandArray::iterator it = _foundStartups.begin(); it != _foundStartups.end();) {
if (it->_command != oldCommand) {
it++;
continue;
}

if (!parseCtx._startups.contains(it->_id)) {
parseCtx._startups[it->_id] = newCommand;
} else {
warning(_("Startup %u already exists"), (unsigned int) it->_id);
}
it = _foundStartups.erase(it);
}
}
_foundStartup.clear();
}

if (newCommandParser != this) {
@@ -186,8 +202,15 @@ void EndBlockCommandParser::finish(ScriptParseContext &) {
if (!_pendingActionInfos.empty()) {
debug("Problem: Pending action infos from end block parser is not empty!");
}
if (!_foundMacros.empty()) {
debug("Problem: Found macros from end block parser is not empty!");
}
if (!_foundStartups.empty()) {
debug("Problem: Found startups from end block parser is not empty!");
}
_pendingActionInfos.clear();
_foundMacro = "";
_foundMacros.clear();
_foundStartups.clear();
}

Command::ExecuteResult EndBlockCommand::execute(ScriptExecutionContext &scriptExecCtx) {
@@ -44,8 +44,18 @@ class EndBlockCommandParser : public CommandParser {
char _ifTag;

Common::Array<uint> _pendingActionInfos;
Common::String _foundMacro;
Common::String _foundStartup;
struct NameAndCommand {
Common::String _name;
Command *_command;
};
struct IdAndCommand {
uint8 _id;
Command *_command;
};
typedef Common::Array<NameAndCommand> NameAndCommandArray;
typedef Common::Array<IdAndCommand> IdAndCommandArray;
NameAndCommandArray _foundMacros;
IdAndCommandArray _foundStartups;
};

class EndBlockCommand : public Command {

0 comments on commit 01b5ca1

Please sign in to comment.