1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "engines/stark/tools/command.h"
#include "common/debug.h"
#include "common/tokenizer.h"
namespace Stark {
namespace Tools {
Command::Command(Command *command) {
_index = command->_index;
_subType = command->_subType;
_subTypeDesc = command->_subTypeDesc;
_arguments = command->_arguments;
}
Command::Command(Resources::Command *resource) {
_index = resource->getIndex();
_subType = (Resources::Command::SubType) resource->getSubType();
_subTypeDesc = searchSubTypeDesc(_subType);
_arguments = resource->getArguments();
}
const Command::SubTypeDesc *Command::searchSubTypeDesc(Resources::Command::SubType subType) {
static const SubTypeDesc typeNames[] = {
{ Resources::Command::kCommandBegin, "begin", kFlowNormal },
{ Resources::Command::kCommandEnd, "end", kFlowEnd },
{ Resources::Command::kScriptCall, "scriptCall", kFlowNormal },
{ Resources::Command::kDialogCall, "dialogCall", kFlowNormal },
{ Resources::Command::kSetInteractiveMode, "setInteractiveMode", kFlowNormal },
{ Resources::Command::kLocationGoTo, "locationGoTo", kFlowEnd },
{ Resources::Command::kWalkTo, "walkTo", kFlowBranch },
{ Resources::Command::kGameLoop, "gameLoop", kFlowNormal },
{ Resources::Command::kScriptPause, "scriptPause", kFlowNormal },
{ Resources::Command::kScriptPauseRandom, "scriptPauseRandom", kFlowNormal },
{ Resources::Command::kScriptPauseSkippable, "scriptPauseSkippable", kFlowNormal },
{ Resources::Command::kScriptAbort, "scriptAbort", kFlowNormal },
{ Resources::Command::kExit2DLocation, "exit2DLocation", kFlowEnd },
{ Resources::Command::kGoto2DLocation, "goto2DLocation", kFlowEnd },
{ Resources::Command::kRumbleScene, "rumbleScene", kFlowNormal },
{ Resources::Command::kFadeScene, "fadeScene", kFlowNormal },
{ Resources::Command::kSwayScene, "swayScene", kFlowNormal },
{ Resources::Command::kLocationGoToNewCD, "locationGoToNewCD", kFlowEnd },
{ Resources::Command::kGameEnd, "gameEnd", kFlowNormal },
{ Resources::Command::kInventoryOpen, "inventoryOpen", kFlowNormal },
{ Resources::Command::kFloatScene, "floatScene", kFlowNormal },
{ Resources::Command::kBookOfSecretsOpen, "bookOfSecretsOpen", kFlowNormal },
{ Resources::Command::kDoNothing, "doNothing", kFlowNormal },
{ Resources::Command::kItem3DPlaceOn, "item3DPlaceOn", kFlowNormal },
{ Resources::Command::kItem3DWalkTo, "item3DWalkTo", kFlowNormal },
{ Resources::Command::kItem3DFollowPath, "item3DFollowPath", kFlowNormal },
{ Resources::Command::kItemLookAt, "itemLookAt", kFlowNormal },
{ Resources::Command::kItem2DFollowPath, "item2DFollowPath", kFlowNormal },
{ Resources::Command::kItemEnable, "itemEnable", kFlowNormal },
{ Resources::Command::kItemSetActivity, "itemSetActivity", kFlowNormal },
{ Resources::Command::kItemSelectInInventory, "itemSelectInInventory", kFlowNormal },
{ Resources::Command::kUseAnimHierarchy, "useAnimHierarchy", kFlowNormal },
{ Resources::Command::kPlayAnimation, "playAnimation", kFlowNormal },
{ Resources::Command::kScriptEnable, "scriptEnable", kFlowNormal },
{ Resources::Command::kShowPlay, "showPlay", kFlowNormal },
{ Resources::Command::kKnowledgeSetBoolean, "knowledgeSetBoolean", kFlowNormal },
{ Resources::Command::kKnowledgeSetInteger, "knowledgeSetInteger", kFlowNormal },
{ Resources::Command::kKnowledgeAddInteger, "knowledgeAddInteger", kFlowNormal },
{ Resources::Command::kEnableFloorField, "enableFloorField", kFlowNormal },
{ Resources::Command::kPlayAnimScriptItem, "playAnimScriptItem", kFlowNormal },
{ Resources::Command::kItemAnimFollowPath, "itemAnimFollowPath", kFlowNormal },
{ Resources::Command::kKnowledgeAssignBool, "knowledgeAssignBool", kFlowNormal },
{ Resources::Command::kKnowledgeAssignInteger, "knowledgeAssignInteger", kFlowNormal },
{ Resources::Command::kLocationScrollTo, "locationScrollTo", kFlowNormal },
{ Resources::Command::kSoundPlay, "soundPlay", kFlowNormal },
{ Resources::Command::kKnowledgeSetIntRandom, "knowledgeSetIntRandom", kFlowNormal },
{ Resources::Command::kKnowledgeSubValue, "knowledgeSubValue", kFlowNormal },
{ Resources::Command::kItemLookDirection, "itemLookDirection", kFlowNormal },
{ Resources::Command::kStopPlayingSound, "stopPlayingSound", kFlowNormal },
{ Resources::Command::kLayerGoTo, "layerGoTo", kFlowNormal },
{ Resources::Command::kLayerEnable, "layerEnable", kFlowNormal },
{ Resources::Command::kLocationScrollSet, "locationScrollSet", kFlowNormal },
{ Resources::Command::kFullMotionVideoPlay, "fullMotionVideoPlay", kFlowNormal },
{ Resources::Command::kAnimSetFrame, "animSetFrame", kFlowNormal },
{ Resources::Command::kKnowledgeAssignNegatedBool, "knowledgeAssignNegatedBool", kFlowNormal },
{ Resources::Command::kDiaryEnableEntry, "diaryEnableEntry", kFlowNormal },
{ Resources::Command::kPATChangeTooltip, "pATChangeTooltip", kFlowNormal },
{ Resources::Command::kSoundChange, "soundChange", kFlowNormal },
{ Resources::Command::kLightSetColor, "lightSetColor", kFlowNormal },
{ Resources::Command::kLightFollowPath, "lightFollowPath", kFlowNormal },
{ Resources::Command::kItem3DRunTo, "item3DRunTo", kFlowNormal },
{ Resources::Command::kItemPlaceDirection, "itemPlaceDirection", kFlowNormal },
{ Resources::Command::kItemRotateDirection, "itemRotateDirection", kFlowNormal },
{ Resources::Command::kActivateTexture, "activateTexture", kFlowNormal },
{ Resources::Command::kActivateMesh, "activateMesh", kFlowNormal },
{ Resources::Command::kItem3DSetWalkTarget, "item3DSetWalkTarget", kFlowNormal },
{ Resources::Command::kSpeakWithoutTalking, "speakWithoutTalking", kFlowNormal },
{ Resources::Command::kIsOnFloorField, "isOnFloorField", kFlowBranch },
{ Resources::Command::kIsItemEnabled, "isItemEnabled", kFlowBranch },
{ Resources::Command::kIsScriptEnabled, "isScriptEnabled", kFlowBranch },
{ Resources::Command::kIsKnowledgeBooleanSet, "isKnowledgeBooleanSet", kFlowBranch },
{ Resources::Command::kIsKnowledgeIntegerInRange, "isKnowledgeIntegerInRange", kFlowBranch },
{ Resources::Command::kIsKnowledgeIntegerAbove, "isKnowledgeIntegerAbove", kFlowBranch },
{ Resources::Command::kIsKnowledgeIntegerEqual, "isKnowledgeIntegerEqual", kFlowBranch },
{ Resources::Command::kIsKnowledgeIntegerLower, "isKnowledgeIntegerLower", kFlowBranch },
{ Resources::Command::kIsScriptActive, "isScriptActive", kFlowBranch },
{ Resources::Command::kIsRandom, "isRandom", kFlowBranch },
{ Resources::Command::kIsAnimScriptItemReached, "isAnimScriptItemReached", kFlowBranch },
{ Resources::Command::kIsItemOnPlace, "isItemOnPlace", kFlowBranch },
{ Resources::Command::kIsAnimPlaying, "isAnimPlaying", kFlowBranch },
{ Resources::Command::kIsItemActivity, "isItemActivity", kFlowBranch },
{ Resources::Command::kIsItemNearPlace, "isItemNearPlace", kFlowBranch },
{ Resources::Command::kIsAnimAtTime, "isAnimAtTime", kFlowBranch },
{ Resources::Command::kIsLocation2D, "isLocation2D", kFlowBranch },
{ Resources::Command::kIsInventoryOpen, "isInventoryOpen", kFlowBranch }
};
for (uint i = 0; i < ARRAYSIZE(typeNames); i++) {
if (typeNames[i].subType == subType) {
return &typeNames[i];
}
}
return nullptr;
}
Command::ArgumentArray Command::getEffectiveArguments() const {
uint effectiveArgumentsStart;
switch (_subTypeDesc->controlFlowType) {
case kFlowEnd:
effectiveArgumentsStart = 0;
break;
case kFlowNormal:
effectiveArgumentsStart = 1;
break;
case kFlowBranch:
effectiveArgumentsStart = 2;
break;
default:
error("Unhandled control flow type '%d'", _subTypeDesc->controlFlowType);
}
ArgumentArray effectiveArguments;
for (uint i = effectiveArgumentsStart; i < _arguments.size(); i++) {
effectiveArguments.push_back(_arguments[i]);
}
return effectiveArguments;
}
Common::String Command::describeArguments(DefinitionRegistry *definitions) const {
Common::String desc;
for (uint i = 0; i < _arguments.size(); i++) {
switch (_arguments[i].type) {
case Resources::Command::Argument::kTypeInteger1:
case Resources::Command::Argument::kTypeInteger2:
desc += Common::String::format("%d", _arguments[i].intValue);
break;
case Resources::Command::Argument::kTypeResourceReference: {
if (definitions) {
desc += definitions->getFromReference(_arguments[i].referenceValue);
} else {
desc += _arguments[i].referenceValue.describe();
}
}
break;
case Resources::Command::Argument::kTypeString:
desc += _arguments[i].stringValue;
break;
default:
error("Unknown argument type %d", _arguments[i].type);
}
if (i != _arguments.size() - 1) {
desc += ", ";
}
}
return desc;
}
void Command::printCall() const {
debug("%d: %s(%s)", _index, _subTypeDesc->name, describeArguments(nullptr).c_str());
}
uint16 Command::getIndex() const {
return _index;
}
bool Command::hasSubtypeDescription() const {
return _subTypeDesc != nullptr;
}
Resources::Command::SubType Command::getSubType() const {
return _subType;
}
CFGCommand::CFGCommand(Resources::Command *resource) :
Command(resource),
_followerIndex(-1),
_trueBranchIndex(-1),
_falseBranchIndex(-1),
_follower(nullptr),
_trueBranch(nullptr),
_falseBranch(nullptr),
_block(nullptr) {
if (_subTypeDesc) {
initBranches();
}
}
void CFGCommand::initBranches() {
switch (_subTypeDesc->controlFlowType) {
case kFlowNormal:
_followerIndex = _arguments[0].intValue;
break;
case kFlowBranch:
if (_arguments[0].intValue == _arguments[1].intValue) {
// Degenerate conditions are handled here so that blocks are not split after them
_followerIndex = _arguments[0].intValue;
} else {
_falseBranchIndex = _arguments[0].intValue;
_trueBranchIndex = _arguments[1].intValue;
}
break;
case kFlowEnd:
// No followers
break;
}
}
bool CFGCommand::isEntryPoint() const {
return _subType == Resources::Command::kCommandBegin;
}
bool CFGCommand::isBranch() const {
return _trueBranchIndex >= 0 && _falseBranchIndex >= 0;
}
bool CFGCommand::isBranchTarget() const {
return _predecessors.size() > 1;
}
Block *CFGCommand::getBlock() const {
return _block;
}
void CFGCommand::setBlock(Block *block) {
_block = block;
}
CFGCommand *CFGCommand::getFollower() const {
return _follower;
}
CFGCommand *CFGCommand::getTrueBranch() const {
return _trueBranch;
}
CFGCommand *CFGCommand::getFalseBranch() const {
return _falseBranch;
}
void CFGCommand::linkBranches(const Common::Array<CFGCommand *> &commands) {
if (_followerIndex >= 0) {
_follower = findCommandWithIndex(commands, _followerIndex);
_follower->_predecessors.push_back(this);
}
if (_falseBranchIndex >= 0) {
_falseBranch = findCommandWithIndex(commands, _falseBranchIndex);
_falseBranch->_predecessors.push_back(this);
}
if (_trueBranchIndex >= 0) {
_trueBranch = findCommandWithIndex(commands, _trueBranchIndex);
_trueBranch->_predecessors.push_back(this);
}
}
CFGCommand *CFGCommand::findCommandWithIndex(const Common::Array<CFGCommand *> &commands, int32 index) {
for (uint i = 0; i < commands.size(); i++) {
CFGCommand *command = commands[i];
if (command->_index == index) {
return command;
}
}
error("Unable to find command with index %d", index);
}
void DefinitionRegistry::registerReference(const ResourceReference &reference) {
if (!reference.canResolve()) {
// The reference uses archives that are not currently loaded
return;
}
Resources::Object *object = reference.resolve<Resources::Object>();
if (!_definitions.contains(object)) {
// TODO: There is no guarantee the definition is unique
_definitions[object] = object->getType().getName() + stringToCamelCase(object->getName());
}
}
Common::String DefinitionRegistry::getFromReference(const ResourceReference &reference) const {
if (!reference.canResolve()) {
// The reference uses archives that are not currently loaded
return reference.describe();
}
Resources::Object *object = reference.resolve<Resources::Object>();
if (_definitions.contains(object)) {
return _definitions.getVal(object);
} else {
return reference.describe();
}
}
Common::String DefinitionRegistry::stringToCamelCase(const Common::String &input) {
Common::String clean = input;
// First replace all non alphanumerical characters with spaces
for (uint i = 0; i < clean.size(); i++) {
if (!Common::isAlnum(clean[i])) {
clean.setChar(' ', i);
}
}
// Then turn the string into camel case
Common::String output;
Common::StringTokenizer tokens = Common::StringTokenizer(clean);
while (!tokens.empty()) {
Common::String token = tokens.nextToken();
char upperFirstLetter = toupper(token[0]);
token.setChar(upperFirstLetter, 0);
output += token;
}
return output;
}
void DefinitionRegistry::printAll() const {
DefinitionMap::const_iterator it = _definitions.begin();
while (it != _definitions.end()) {
ResourceReference reference;
reference.buildFromResource(it->_key);
debug("let %s = %s", it->_value.c_str(), reference.describe().c_str());
it++;
}
}
} // End of namespace Tools
} // End of namespace Stark
|