scummvm/engines/zvision/scr_file_handling.cpp

320 lines
9.1 KiB
C++
Raw Normal View History

2013-06-24 00:57:07 -05:00
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "common/scummsys.h"
#include "zvision/script_manager.h"
2013-06-24 00:57:07 -05:00
#include "zvision/utility.h"
#include "zvision/puzzle.h"
#include "zvision/actions.h"
2013-06-24 00:57:07 -05:00
#include "common/textconsole.h"
#include "common/file.h"
#include "common/tokenizer.h"
namespace ZVision {
void ScriptManager::parseScrFile(const Common::String &fileName, bool isGlobal) {
2013-06-24 00:57:07 -05:00
Common::File file;
if (!file.open(fileName))
return; // File.open already throws a warning if the file doesn't exist, so there is no need to throw another
while(!file.eos()) {
Common::String line = file.readLine();
if (file.err()) {
warning("Error parsing scr file: %s", fileName.c_str());
2013-06-24 00:57:07 -05:00
return;
}
trimCommentsAndWhiteSpace(&line);
2013-06-24 00:57:07 -05:00
if (line.empty())
continue;
if (line.matchString("puzzle:*", true)) {
Puzzle puzzle;
sscanf(line.c_str(),"puzzle:%u",&(puzzle.key));
2013-06-24 00:57:07 -05:00
parsePuzzle(puzzle, file);
if (isGlobal) {
_globalPuzzles.push_back(puzzle);
} else {
_activePuzzles.push_back(puzzle);
}
2013-06-24 00:57:07 -05:00
} else if (line.matchString("control:*", true)) {
Common::SharedPtr<Control> control;
// Some controls don't require nodes. They just initialize the scene
if (parseControl(line, file, control)) {
_activeControls.push_back(control);
}
2013-06-24 00:57:07 -05:00
}
}
}
void ScriptManager::parsePuzzle(Puzzle &puzzle, Common::SeekableReadStream &stream) {
2013-06-24 00:57:07 -05:00
Common::String line = stream.readLine();
trimCommentsAndWhiteSpace(&line);
2013-06-24 00:57:07 -05:00
while (!line.contains('}')) {
if (line.matchString("criteria {", true)) {
Puzzle::Criteria criteria;
if (parseCriteria(&criteria, stream)) {
puzzle.criteriaList.push_back(criteria);
}
} else if (line.matchString("results {", true)) {
parseResults(stream, puzzle.resultActions);
} else if (line.matchString("flags {", true)) {
puzzle.flags = parseFlags(stream);
}
line = stream.readLine();
trimCommentsAndWhiteSpace(&line);
2013-06-24 00:57:07 -05:00
}
}
bool ScriptManager::parseCriteria(Puzzle::Criteria *criteria, Common::SeekableReadStream &stream) const {
2013-06-24 00:57:07 -05:00
// Loop until we find the closing brace
Common::String line = stream.readLine();
trimCommentsAndWhiteSpace(&line);
2013-06-24 00:57:07 -05:00
// Criteria can be empty
if (line.contains('}')) {
return false;
}
2013-06-24 00:57:07 -05:00
while (!line.contains('}')) {
// Split the string into tokens using ' ' as a delimiter
Common::StringTokenizer tokenizer(line);
Common::String token;
// Parse the id out of the first token
token = tokenizer.nextToken();
sscanf(token.c_str(), "[%u]", &(criteria->key));
2013-06-24 00:57:07 -05:00
// Parse the operator out of the second token
token = tokenizer.nextToken();
if (token.c_str()[0] == '=')
criteria->criteriaOperator = Puzzle::EQUAL_TO;
2013-06-24 00:57:07 -05:00
else if (token.c_str()[0] == '!')
criteria->criteriaOperator = Puzzle::NOT_EQUAL_TO;
2013-06-24 00:57:07 -05:00
else if (token.c_str()[0] == '>')
criteria->criteriaOperator = Puzzle::GREATER_THAN;
2013-06-24 00:57:07 -05:00
else if (token.c_str()[0] == '<')
criteria->criteriaOperator = Puzzle::LESS_THAN;
2013-06-24 00:57:07 -05:00
// First determine if the last token is an id or a value
// Then parse it into 'argument'
token = tokenizer.nextToken();
if (token.contains('[')) {
sscanf(token.c_str(), "[%u]", &(criteria->argument));
criteria->argumentIsAKey = true;
2013-06-24 00:57:07 -05:00
} else {
sscanf(token.c_str(), "%u", &(criteria->argument));
criteria->argumentIsAKey = false;
2013-06-24 00:57:07 -05:00
}
line = stream.readLine();
trimCommentsAndWhiteSpace(&line);
2013-06-24 00:57:07 -05:00
}
return true;
2013-06-24 00:57:07 -05:00
}
void ScriptManager::parseResults(Common::SeekableReadStream &stream, Common::List<Common::SharedPtr<ResultAction> > &actionList) const {
2013-06-24 00:57:07 -05:00
// Loop until we find the closing brace
Common::String line = stream.readLine();
trimCommentsAndWhiteSpace(&line);
2013-06-24 00:57:07 -05:00
// TODO: Re-order the if-then statements in order of highest occurrence
2013-06-24 00:57:07 -05:00
while (!line.contains('}')) {
// Parse for the action type
if (line.matchString("*:add*", true)) {
actionList.push_back(Common::SharedPtr<ResultAction>(new ActionAdd(line)));
} else if (line.matchString("*:animplay*", true)) {
actionList.push_back(Common::SharedPtr<ResultAction>(new ActionPlayAnimation(line)));
} else if (line.matchString("*:animpreload*", true)) {
actionList.push_back(Common::SharedPtr<ResultAction>(new ActionPreloadAnimation(line)));
} else if (line.matchString("*:animunload*", true)) {
//actionList.push_back(Common::SharedPtr<ResultAction>(new ActionUnloadAnimation(line)));
} else if (line.matchString("*:attenuate*", true)) {
2013-06-24 00:57:07 -05:00
} else if (line.matchString("*:assign*", true)) {
2013-06-24 00:57:07 -05:00
} else if (line.matchString("*:change_location*", true)) {
actionList.push_back(Common::SharedPtr<ResultAction>(new ActionChangeLocation(line)));
} else if (line.matchString("*:crossfade*", true)) {
2013-06-24 00:57:07 -05:00
} else if (line.matchString("*:debug*", true)) {
2013-06-24 00:57:07 -05:00
} else if (line.matchString("*:delay_render*", true)) {
2013-06-24 00:57:07 -05:00
} else if (line.matchString("*:disable_control*", true)) {
2013-06-24 00:57:07 -05:00
} else if (line.matchString("*:disable_venus*", true)) {
2013-06-24 00:57:07 -05:00
} else if (line.matchString("*:display_message*", true)) {
2013-06-24 00:57:07 -05:00
} else if (line.matchString("*:dissolve*", true)) {
} else if (line.matchString("*:distort*", true)) {
} else if (line.matchString("*:enable_control*", true)) {
} else if (line.matchString("*:flush_mouse_events*", true)) {
} else if (line.matchString("*:inventory*", true)) {
} else if (line.matchString("*:kill*", true)) {
} else if (line.matchString("*:menu_bar_enable*", true)) {
} else if (line.matchString("*:music*", true)) {
2013-08-05 00:14:20 -05:00
actionList.push_back(Common::SharedPtr<ResultAction>(new ActionMusic(line)));
} else if (line.matchString("*:pan_track*", true)) {
} else if (line.matchString("*:playpreload*", true)) {
} else if (line.matchString("*:preferences*", true)) {
} else if (line.matchString("*:quit*", true)) {
2013-08-10 17:12:23 -05:00
actionList.push_back(Common::SharedPtr<ResultAction>(new ActionQuit()));
} else if (line.matchString("*:random*", true)) {
} else if (line.matchString("*:region*", true)) {
} else if (line.matchString("*:restore_game*", true)) {
} else if (line.matchString("*:rotate_to*", true)) {
} else if (line.matchString("*:save_game*", true)) {
} else if (line.matchString("*:set_partial_screen*", true)) {
} else if (line.matchString("*:set_screen*", true)) {
2013-08-03 15:00:58 -05:00
actionList.push_back(Common::SharedPtr<ResultAction>(new ActionSetScreen(line)));
} else if (line.matchString("*:set_venus*", true)) {
} else if (line.matchString("*:stop*", true)) {
} else if (line.matchString("*:streamvideo*", true)) {
2013-08-10 17:18:29 -05:00
actionList.push_back(Common::SharedPtr<ResultAction>(new ActionStreamVideo(line)));
} else if (line.matchString("*:syncsound*", true)) {
} else if (line.matchString("*:timer*", true)) {
} else if (line.matchString("*:ttytext*", true)) {
} else if (line.matchString("*:universe_music*", true)) {
2013-06-24 00:57:07 -05:00
} else if (line.matchString("*:copy_file*", true)) {
// Not used. Purposely left empty
2013-06-24 00:57:07 -05:00
} else {
warning("Unhandled result action type: %s", line.c_str());
2013-06-24 00:57:07 -05:00
}
line = stream.readLine();
trimCommentsAndWhiteSpace(&line);
2013-06-24 00:57:07 -05:00
}
return;
2013-06-24 00:57:07 -05:00
}
uint ScriptManager::parseFlags(Common::SeekableReadStream &stream) const {
uint flags = 0;
2013-06-24 00:57:07 -05:00
// Loop until we find the closing brace
Common::String line = stream.readLine();
trimCommentsAndWhiteSpace(&line);
2013-06-24 00:57:07 -05:00
while (!line.contains('}')) {
if (line.matchString("ONCE_PER_INST", true)) {
flags |= Puzzle::ONCE_PER_INST;
2013-06-24 00:57:07 -05:00
} else if (line.matchString("DO_ME_NOW", true)) {
flags |= Puzzle::DO_ME_NOW;
2013-06-24 00:57:07 -05:00
} else if (line.matchString("DISABLED", true)) {
flags |= Puzzle::DISABLED;
2013-06-24 00:57:07 -05:00
}
line = stream.readLine();
trimCommentsAndWhiteSpace(&line);
2013-06-24 00:57:07 -05:00
}
return flags;
}
bool ScriptManager::parseControl(Common::String &line, Common::SeekableReadStream &stream, Common::SharedPtr<Control> &control) {
uint32 key;
char controlTypeBuffer[20];
sscanf(line.c_str(), "control:%u %s {", &key, controlTypeBuffer);
Common::String controlType(controlTypeBuffer);
if (controlType.equalsIgnoreCase("push_toggle")) {
} else if (controlType.equalsIgnoreCase("flat")) {
Control::parseFlatControl(_engine);
return false;
} else if (controlType.equalsIgnoreCase("pana")) {
Control::parsePanoramaControl(_engine, stream);
return false;
}
else if (controlType.equalsIgnoreCase("tilt")) {
Control::parseTiltControl(_engine, stream);
return false;
}
2013-06-24 00:57:07 -05:00
return true;
2013-06-24 00:57:07 -05:00
}
} // End of namespace ZVision