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
|
/* 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.
*
*/
/*
* Based on
* WebVenture (c) 2010, Sean Kasun
* https://github.com/mrkite/webventure, http://seancode.com/webventure/
*
* Used with explicit permission from the author
*/
#include "macventure/text.h"
namespace MacVenture {
TextAsset::TextAsset(MacVentureEngine *engine, ObjID objid, ObjID source, ObjID target, Container *container, bool isOld, const HuffmanLists *huffman) {
_id = objid;
_sourceObj = source;
_targetObj = target;
_container = container;
_huffman = huffman;
_isOld = isOld;
_engine = engine;
if (_isOld) {
decodeOld();
} else {
decodeHuffman();
}
}
void TextAsset::decodeOld() {
Common::SeekableReadStream *res = _container->getItem(_id);
uint16 strLen = res->readUint16BE();
Common::BitStream32BELSB stream(res, DisposeAfterUse::YES);
char *str = new char[strLen + 1];
bool lowercase = false;
char c = ' ';
for (uint16 i = 0; i < strLen; i++) {
char val = stream.getBits(5);
if (val == 0x0) { // Space
c = ' ';
} else if (val >= 0x1 && val <= 0x1A) {
if (lowercase) { // Ascii a-z
c = val + 0x60;
} else { // Ascii A-Z
c = val + 0x40;
}
lowercase = true;
} else if (val == 0x1B) {
if (lowercase) {
c = '.';
} else {
c = ',';
}
lowercase = true;
} else if (val == 0x1C) {
if (lowercase) {
c = '\'';
} else {
c = '"';
}
lowercase = true;
} else if (val == 0x1D) { // Composite
ObjID subval = stream.getBits(16);
Common::String child;
if (subval & 0x8000) {
// Composite object id
subval ^= 0xFFFF;
child = getNoun(subval);
} else {
// Just another id
// HACK, see below in getNoun()
child = *TextAsset(_engine, subval, _sourceObj, _targetObj, _container, _isOld, _huffman).decode();
}
if (child.size() > 0) {
c = '?'; // HACK Will fix later, should append
}
lowercase = true;
} else if (val == 0x1E) {
c = stream.getBits(8);
lowercase = true;
} else if (val == 0x1F) {
lowercase = !lowercase;
} else {
warning("Unrecognized char in old text %d, pos %d", _id, i);
}
str[i] = c;
}
str[strLen] = '\0';
debugC(3, kMVDebugText, "Decoded string [%d] (old encoding): %s", _id, str);
_decoded = Common::String(str);
}
void TextAsset::decodeHuffman() {
_decoded = Common::String("");
Common::SeekableReadStream *res = _container->getItem(_id);
Common::BitStream8MSB stream(res, DisposeAfterUse::YES);
uint16 strLen = 0;
if (stream.getBit()) {
strLen = stream.getBits(15);
} else {
strLen = stream.getBits(7);
}
uint32 mask = 0;
uint32 symbol = 0;
char c;
for (uint16 i = 0; i < strLen; i++) {
mask = stream.peekBits(16);
uint32 entry;
// Find the length index
for (entry = 0; entry < _huffman->getNumEntries(); entry++) {
if (mask < _huffman->getMask(entry)) {
break;
}
}
stream.skip(_huffman->getLength(entry));
symbol = _huffman->getSymbol(entry);
if (symbol == 1) { // 7-bit ascii
c = stream.getBits(7);
_decoded += c;
} else if (symbol == 2) { // Composite
if (stream.getBit()) { // TextID
ObjID embedId = stream.getBits(15);
uint pos = stream.pos(); // HACK, part 1
TextAsset embedded(_engine, embedId, _sourceObj, _targetObj, _container, _isOld, _huffman);
stream.rewind();// HACK, part 2
stream.skip(pos);
_decoded.replace(_decoded.end(), _decoded.end(), *embedded.decode());
// Another HACK, to get around that EOS char I insert at the end
_decoded.replace(_decoded.end() - 1, _decoded.end(), "");
} else { //Composite obj string
ObjID embedId = stream.getBits(8);
uint pos = stream.pos(); // HACK, part 1
_decoded.replace(_decoded.end(), _decoded.end(), getNoun(embedId));
stream.rewind();// HACK, part 2
stream.skip(pos);
// Another HACK, to get around that EOS char I insert at the end
_decoded.replace(_decoded.end() - 1, _decoded.end(), "");
}
} else { // Plain ascii
c = symbol & 0xFF;
_decoded.replace(_decoded.end(), _decoded.end(), Common::String(c));
}
}
_decoded += '\0';
debugC(3, kMVDebugText, "Decoded string [%d] (new encoding): %s", _id, _decoded.c_str());
}
Common::String TextAsset::getNoun(ObjID subval) {
ObjID obj;
Common::String name;
if (subval & 8) {
obj = _targetObj;
} else {
obj = _sourceObj;
}
if ((subval & 3) == 1) {
uint idx = _engine->getPrefixNdx(obj);
idx = ((idx >> 4) & 3) + 1;
name = _engine->getNoun(idx);
} else {
// HACK, there should be a pool of assets or something like in the GUI
name = *TextAsset(_engine, obj, _sourceObj, _targetObj, _container, _isOld, _huffman).decode();
switch (subval & 3) {
case 2:
name = _engine->getPrefixString(0, obj) + name;
break;
case 3:
name = _engine->getPrefixString(2, obj) + name;
break;
}
}
if (name.size() && (subval & 4)) {
Common::String tmp = name;
name.toUppercase();
name.replace(1, name.size() - 1, tmp, 1, tmp.size() - 1);
}
return name;
}
} // End of namespace MacVenture
|