1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "bladerunner/text_resource.h"
24 
25 #include "bladerunner/bladerunner.h"
26 
27 #include "common/debug.h"
28 #include "common/util.h"
29 
30 namespace BladeRunner {
31 
TextResource(BladeRunnerEngine * vm)32 TextResource::TextResource(BladeRunnerEngine *vm) {
33 	_vm      = vm;
34 	_count   = 0;
35 	_ids     = nullptr;
36 	_offsets = nullptr;
37 	_strings = nullptr;
38 }
39 
~TextResource()40 TextResource::~TextResource() {
41 	delete[] _ids;
42 	delete[] _offsets;
43 	delete[] _strings;
44 }
45 
open(const Common::String & name,bool localized)46 bool TextResource::open(const Common::String &name, bool localized) {
47 	assert(name.size() <= 8);
48 
49 	Common::String resName;
50 	if (localized) {
51 		resName = Common::String::format("%s.TR%s", name.c_str(), _vm->_languageCode.c_str());
52 	} else {
53 		resName = Common::String::format("%s.TRE", name.c_str());
54 	}
55 	Common::ScopedPtr<Common::SeekableReadStream> s(_vm->getResourceStream(resName));
56 	if (!s) {
57 		warning("TextResource::open(): Can not open %s", resName.c_str());
58 		return false;
59 	}
60 
61 	_count = s->readUint32LE();
62 
63 	_ids = new uint32[_count];
64 	_offsets = new uint32[_count + 1];
65 
66 	for (uint32 i = 0; i != _count; ++i) {
67 		_ids[i] = s->readUint32LE();
68 	}
69 
70 	for (uint32 i = 0; i != _count + 1; ++i) {
71 		_offsets[i] = s->readUint32LE();
72 	}
73 
74 	uint32 stringsStart = s->pos() - 4;
75 
76 	for (uint32 i = 0; i != _count + 1; ++i) {
77 		_offsets[i] -= stringsStart;
78 	}
79 
80 	uint32 remain = s->size() - s->pos();
81 	_strings = new char[remain];
82 
83 	assert(remain >= _offsets[_count]);
84 
85 	s->read(_strings, remain);
86 
87 #if BLADERUNNER_DEBUG_CONSOLE
88 	debug("\n%s\n----------------", resName.c_str());
89 	for (uint32 i = 0; i != (uint32)_count; ++i) {
90 		debug("%3d: %s", _ids[i], getText(_ids[i]));
91 	}
92 #endif
93 
94 	return true;
95 }
96 
getText(uint32 id) const97 const char *TextResource::getText(uint32 id) const {
98 	for (uint32 i = 0; i != _count; ++i) {
99 		if (_ids[i] == id) {
100 			return _strings + _offsets[i];
101 		}
102 	}
103 
104 	return "";
105 }
106 
getOuttakeTextByFrame(uint32 frame) const107 const char *TextResource::getOuttakeTextByFrame(uint32 frame) const {
108 	for (uint32 i = 0; i != _count; ++i) {
109 		//debug("Checking %d - so within: %d , %d", _ids[i], (0x0000FFFF & _ids[i]), ((_ids[i] >> 16) & 0x0000FFFF ) );
110 		if ((frame >= (0x0000FFFF & _ids[i]) )   && (frame <  ((_ids[i] >> 16) & 0x0000FFFF ) )) {
111 			// we found an id with lower 16bits smaller or equal to our frame key
112 			// and with higher 16 bits higher than the frame key
113 			return _strings + _offsets[i];
114 		}
115 	}
116 	return "";
117 }
118 
getCount() const119 int TextResource::getCount() const {
120 	return _count;
121 }
122 
123 } // End of namespace BladeRunner
124