-
Notifications
You must be signed in to change notification settings - Fork 4
/
formatfile.cpp
198 lines (146 loc) · 5.83 KB
/
formatfile.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
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
#include <QDomDocument>
#include <QDomElement>
#include <QDomNode>
#include "formatfile.h"
/*
REVISE THIS FILE ASAP!!!!!!!!!!!
FormatHandler g_formatHandler
*/
FormatFile::FormatFile( QObject *parent ) : TibiaFile( parent )
{
_loaded = false;
}
FormatFile::~FormatFile( void )
{
formats.clear();
}
void FormatFile::unload( void )
{
TibiaFile::unload();
_loaded = false;
formats.clear();
}
bool FormatFile::load( const QString& fileName )
{
setFileName( fileName );
if( !TibiaFile::open( QIODevice::ReadOnly ) ) {
emit parseError( QObject::tr( "Open Error" ), TibiaFile::error() );
return false;
}
QString error;
int line;
QDomDocument doc( "formats" );
if( !doc.setContent( this, true, &error, &line ) ) {
emit documentError( fileName, error, line );
TibiaFile::close();
return false;
}
TibiaFile::close();
QDomElement docElem = doc.documentElement();
if( docElem.tagName().compare( "formats", Qt::CaseInsensitive ) == 0 ) {
QDomNode node = docElem.firstChild();
while( !node.isNull() ) {
if( node.isElement() ) {
QDomElement element = node.toElement();
if( !element.isNull() ) {
if( element.tagName().compare( "format", Qt::CaseInsensitive ) == 0 ) {
ItemFormat format;
format.name = element.attribute( "name" );
format.version = element.attribute( "version" ).toInt();
format.redirect = element.attribute( "redirect" ).toInt();
format.ZDivFactor = element.attribute( "zdiv", "1" ).toInt();
QDomElement subelement = element.firstChildElement( "property" );
while( !subelement.isNull() ) {
FormatProperty formatProperty;
formatProperty.header = subelement.attribute( "header", "255" ).toInt();
if( format.version == -1 )
formatProperty.base = formatProperty.header;
else
formatProperty.base = subelement.attribute( "base", "255" ).toInt();
formatProperty.name = subelement.attribute( "name" );
formatProperty.tooltip = subelement.attribute( "tooltip" );
QDomElement child = subelement.firstChildElement( "child" );
while( !child.isNull() ) {
PropertyValue propertyValue;
propertyValue.size = child.attribute( "size", "0" ).toInt();
propertyValue.name = child.attribute( "name" );
propertyValue.tooltip = child.attribute( "tooltip" );
formatProperty.propertyValues.push_back( propertyValue );
child = child.nextSiblingElement( "child" );
}
format.properties.push_back( formatProperty );
subelement = subelement.nextSiblingElement( "property" );
}
format.valid = true;
formats.insert( format.version, format );
}
}
}
node = node.nextSibling();
}
} else {
emit documentError( fileName, tr( "Invalid Element: " ) + docElem.tagName(), -1 );
return false;
}
_loaded = true;
return true;
}
quint8 ItemFormat::getBase( quint8 header )
{
for( FormatPropertyVector::iterator it = properties.begin(); it != properties.end(); it++ ) {
FormatProperty itemProperty = (*it);
if( itemProperty.header == header )
return itemProperty.base;
}
return 0xFF;
}
quint8 ItemFormat::getHeader( quint8 base )
{
for( FormatPropertyVector::iterator it = properties.begin(); it != properties.end(); it++ ) {
FormatProperty itemProperty = (*it);
if( itemProperty.base == base )
return itemProperty.header;
}
return 0xFF;
}
FormatProperty ItemFormat::getPropertyByBase( quint8 base ) const
{
for( FormatPropertyVector::const_iterator it = properties.begin(); it != properties.end(); it++ ) {
FormatProperty itemProperty = (*it);
if( itemProperty.base == base )
return itemProperty;
}
return FormatProperty();
}
FormatProperty ItemFormat::getPropertyByHeader( quint8 header ) const
{
for( FormatPropertyVector::const_iterator it = properties.begin(); it != properties.end(); it++ ) {
FormatProperty itemProperty = (*it);
if( itemProperty.header == header )
return itemProperty;
}
return FormatProperty();
}
ItemFormat FormatFile::getFormatByName( const QString& version ) const
{
for( FormatMap::const_iterator it = formats.begin(); it != formats.end(); it++ ) {
ItemFormat format = (*it);
if( format.name.compare( version, Qt::CaseInsensitive ) == 0 )
return getFormatByClient( format.version ); // Do any redirecting if its redirected
}
return formats.value( BASE_FORMAT_KEY ); // All versions failed, lets get our base format
}
ItemFormat FormatFile::getFormatByClient( qint32 version ) const
{
ItemFormat format = formats.value( version );
if( format.version == 0 ) { // Our found version failed
return formats.value( BASE_FORMAT_KEY );
}
if( format.redirect != 0 ) { // Our version is a redirected version
format = formats.value( format.redirect );
}
if( format.version == 0 ) { // Our redirected version is invalid
return formats.value( BASE_FORMAT_KEY );
}
return format;
}