forked from scott-t/tlj-residual
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xrc.cpp
134 lines (105 loc) · 3.7 KB
/
xrc.cpp
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
/* Residual - A 3D game interpreter
*
* Residual is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* file distributed with this source distribution.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* $URL$
* $Id$
*
*/
#include "engines/stark/xrc.h"
#include "engines/stark/debug.h"
#include "common/debug-channels.h"
#include "common/util.h"
namespace Stark {
XRCNode::XRCNode() : _data(NULL) {
}
XRCNode::~XRCNode() {
// Delete this node's data
delete[] _data;
// Delete the children nodes
Common::Array<XRCNode *>::iterator i = _children.begin();
while (i != _children.end()) {
delete *i;
i++;
}
}
XRCNode *XRCNode::read(Common::ReadStream *stream) {
// Create a new node
XRCNode *node = new XRCNode();
// Read the node contents
if (!node->readInternal(stream)) {
delete node;
return NULL;
}
return node;
}
bool XRCNode::readInternal(Common::ReadStream *stream) {
// Read the resource type
_dataType = stream->readByte();
// Read unknown data
_unknown1 = stream->readByte();
_unknown2 = stream->readUint16LE();
// Read the resource name length
uint16 nameLength = stream->readUint16LE();
// Read the resource name
char *name = new char[nameLength];
stream->read(name, nameLength);
_name = Common::String(name, nameLength);
delete[] name;
// Read the data length
_dataLength = stream->readUint32LE();
// Show a first batch of information
debugC(10, kDebugXRC, "Stark::XRCNode: Type 0x%02X, Name: \"%s\", %d bytes", _dataType, _name.c_str(), _dataLength);
// Read the data
if (_dataLength) {
_data = new byte[_dataLength];
uint32 bytesRead = stream->read(_data, _dataLength);
// Verify the whole array could be read
if (bytesRead != _dataLength)
return false;
}
// Get the number of children
uint16 numChildren = stream->readUint16LE();
// Read more unknown data
_unknown3 = stream->readUint16LE();
debugC(10, kDebugUnknown, "Stark::XRCNode: \"%s\" has "
"unknown1=0x%02X, unknown2=0x%04X, unknown3=0x%04X",
_name.c_str(), _unknown1, _unknown2, _unknown3);
if (_unknown3 != 0) {
warning("Stark::XRCNode: \"%s\" has unknown3=0x%04X with unknown meaning", _name.c_str(), _unknown3);
}
// Show the resource data
debugC(20, kDebugXRC, "Stark::XRCNode: \"%s\" has %d bytes of data:", _name.c_str(), _dataLength);
if ((gDebugLevel >= 20) && DebugMan.isDebugChannelEnabled(kDebugXRC)) {
Common::hexdump(_data, _dataLength);
}
debug(3, " Raw: %X, %X, %X, %s, %d bytes, %X, %X", _dataType, _unknown1,
_unknown2, _name.c_str(), _dataLength, numChildren, _unknown3);
// Read the children nodes
debugC(10, kDebugXRC, "Stark::XRCNode: \"%s\" has %d children:\n", _name.c_str(), numChildren);
_children.reserve(numChildren);
for (int i = 0; i < numChildren; i++) {
XRCNode *child = XRCNode::read(stream);
// If we can't read a children node, consider this node as broken
if (!child)
return false;
// Save all children read correctly
_children.push_back(child);
}
return true;
}
} // End of namespace Stark