-
Notifications
You must be signed in to change notification settings - Fork 4
/
externalfile.cpp
288 lines (231 loc) · 7.63 KB
/
externalfile.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
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
#include <QTemporaryFile>
#include "tibiahandler.h"
#include "externalfile.h"
#include "resourcehandler.h"
#include "formathandler.h"
extern TibiaHandler g_tibiaHandler;
extern ResourceHandler g_resourceHandler;
extern FormatHandler g_formatHandler;
ExternalFile::ExternalFile( QObject *parent ) : TibiaModule( parent )
{
m_address = 0;
m_loaded = false;
m_mustIdle = true;
m_success = false;
}
ExternalFile::ExternalFile( quint16 version, const QString& name, const ItemData& item, QObject *parent ) : TibiaModule( parent )
{
m_version = version;
m_idle = false;
m_mustIdle = false;
m_success = false;
m_internalItemData = item;
save( name );
deleteLater();
}
ExternalFile::~ExternalFile( void )
{
}
void ExternalFile::unload( void )
{
m_version = 0;
m_address = 0;
m_loaded = false;
m_mustIdle = true;
m_internalItemData.reset();
}
bool ExternalFile::idle( const QString& name, bool read )
{
setFileName( name );
if( !TibiaFile::exists() ) {
emit documentError( name, QObject::tr( "File does not exist." ), -1 );
return false;
}
if( !TibiaFile::open( QIODevice::ReadOnly ) ) {
emit parseError( QObject::tr( "Open Error" ), TibiaFile::error() );
return false;
}
if( read ) {
QDataStream in( this );
in.setByteOrder( QDataStream::LittleEndian );
in >> m_version;
m_datFormat = g_formatHandler.getFormatByClient( m_version );
if( !m_datFormat ) {
emit documentError( name, tr( "Unknown format: %1" ).arg( m_version ), -1 );
return false;
}
}
m_idle = true;
m_success = true;
return m_success;
}
bool ExternalFile::readItem( QDataStream& in, ItemData& itemData, DatFormat *datFormat, TibiaModule *tibiaModule, qint32 index, quint32& address, QString& error )
{
QIODevice *device = in.device();
if( !device )
return false;
quint8 type = 0;
in >> type;
if( type != ITEM_TYPE_ITEM && type != ITEM_TYPE_OUTFIT && type != ITEM_TYPE_EFFECT && type != ITEM_TYPE_PROJECTILE ) {
error = QObject::tr( "Unknown Item type" );
return false;
}
ItemData d_itemData;
if( datFormat ) {
if( !ItemFile::loadItem( datFormat, in, d_itemData, error ) )
return false;
}
d_itemData.parent = ITEM_PARENT_EXTERNAL;
d_itemData.type = type;
address = 0;
if( !device->atEnd() ) {
quint32 spriteCount = 0, now = 0, offset = 0;
in >> spriteCount;
address = device->pos();
now = device->pos();
for( quint32 i = 0; i < spriteCount; i++ ) {
device->seek( now );
in >> offset;
if ( offset == 0x00000000 || offset > device->size() ) { // Direct to an image that doesnt exist or out of boundaries
now += sizeof( quint32 );
continue;
}
QMutex mutex;
mutex.lock();
SharedResource *resource = &g_resourceHandler.addResource(RESOURCE_TYPE_SPRITE, index, i, tibiaModule);
d_itemData.setSpriteResource(i, *resource);
mutex.unlock();
now += sizeof( quint32 );
}
}
itemData = d_itemData;
return true;
}
bool ExternalFile::saveItem( QDataStream& out, const ItemData& itemData, DatFormat *datFormat )
{
QMutex mutex;
QMutexLocker locker( &mutex );
QIODevice *device = out.device();
if( !device )
return false;
out << itemData.type;
ItemFile::saveItem( datFormat, out, itemData );
if( itemData.getSpriteCount() == 0 ) {
out << ( quint32 )0;
return true;
}
quint32 spriteCount = itemData.getSpriteCount();
out << spriteCount;
quint32 now = device->pos();
quint32 offset = now + spriteCount * sizeof( quint32 );
for( quint32 i = 0; i < spriteCount; i++ ) {
device->seek( now );
TibiaSprite sprite = g_tibiaHandler.getSprite( i, itemData );
if( sprite.isDummy() ) {
out << ( quint32 )0x00000000;
now += sizeof( quint32 );
continue;
}
out << offset;
device->seek( offset );
TibiaFile::writeSprite( out, sprite, 0, 0, offset );
now += sizeof( quint32 );
}
device->seek( offset );
return true;
}
ItemData ExternalFile::getItemData( void )
{
QMutexLocker locker( &mutex );
if( isIdle() ) {
QDataStream in( this );
in.setByteOrder( QDataStream::LittleEndian );
TibiaFile::seek( sizeof( m_version ) );
ItemData itemData;
QString error;
m_loaded = readItem( in, itemData, m_datFormat, this, 0, m_address, error );
if( m_loaded )
return itemData;
else
emit documentError( fileName(), tr( "Document Error: %1" ).arg( error ), -1 );
}
return m_internalItemData;
}
TibiaSprite ExternalFile::getSprite( qint32, qint32 frame )
{
QMutexLocker locker( &mutex );
TibiaSprite sprite;
if( m_address != 0 ) {
QDataStream in( this );
in.setByteOrder( QDataStream::LittleEndian );
TibiaFile::seek( m_address + frame * sizeof( quint32 ) );
quint32 offset;
in >> offset;
if( offset == 0x00000000 ) // Return a blank image
return sprite;
TibiaFile::seek( offset );
sprite.setDummy( false );
TibiaFile::readSprite( in, sprite, 0, 0 );
}
return sprite;
}
bool ExternalFile::save( const QString& name )
{
if( isIdle() ) { // File is on idle, we need to stream our file to our new file
// Create temporary file, write signature and count
QTemporaryFile file( name );
if( !file.open() ) {
emit parseError( QObject::tr( "Open Error" ), file.error() );
return false;
}
QDataStream dest( &file );
dest.setByteOrder( QDataStream::LittleEndian );
dest << m_version;
m_success = false;
m_datFormat = g_formatHandler.getFormatByClient( m_version );
if( !m_datFormat ) {
emit documentError( name, tr( "Unknown format: %1" ).arg( m_version ), -1 );
return false;
}
if( QFile::exists( name ) ) {
if( name.compare( TibiaFile::fileName(), Qt::CaseInsensitive ) == 0 ) // Destination == Source, Close source
TibiaFile::close();
if( !QFile::remove( name ) ) {
emit documentError( name, tr( "Failed to overwrite existing file." ), -1 );
return false;
}
}
if( m_success = saveItem( dest, getItemData(), m_datFormat ) ) {
file.copy( name );
idle( name, true ); // Upon saving we must re-idle our new file to continue
}
return m_success;
}
// Not streaming, we can write item directly
setFileName( name );
if( !TibiaFile::open( QIODevice::WriteOnly ) ) {
emit parseError( QObject::tr( "Open Error" ), TibiaFile::error() );
return false;
}
TibiaFile::seek( 0 );
QDataStream out( this );
out.setByteOrder( QDataStream::LittleEndian );
out << m_version;
m_success = false;
m_datFormat = g_formatHandler.getFormatByClient( m_version );
if( !m_datFormat ) {
emit documentError( name, tr( "Unknown format: %1" ).arg( m_version ), -1 );
return false;
}
if( m_success = saveItem( out, getItemData(), m_datFormat ) ) {
if( m_mustIdle )
m_success = idle( name, true );
}
TibiaFile::close();
return m_success;
}
void ExternalFile::setItemData( const ItemData& item )
{
QMutexLocker locker( &mutex );
m_internalItemData.copy( item );
}