-
Notifications
You must be signed in to change notification settings - Fork 4
/
libraryfile.cpp
284 lines (235 loc) · 7.48 KB
/
libraryfile.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
#include <QTemporaryFile>
#include "libraryfile.h"
#include "tibiahandler.h"
#include "externalfile.h"
#include "resourcehandler.h"
#include "formathandler.h"
#include "userthread.h"
extern TibiaHandler g_tibiaHandler;
extern ResourceHandler g_resourceHandler;
extern FormatHandler g_formatHandler;
LibraryFile::LibraryFile( QObject *parent ) : TibiaModule( parent )
{
m_thread = new LibraryThread( this );
m_datFormat = NULL;
m_count = 0;
m_loaded = false;
m_mustIdle = true;
}
LibraryFile::LibraryFile( DatFormat *format, const ItemList& items, QObject *parent ) : TibiaModule( parent )
{
m_thread = new LibraryThread( this );
m_idle = false;
m_mustIdle = false;
m_count = items.size();
m_datFormat = format;
m_version = m_datFormat->getVersion();
m_items = items;
QObject::connect( m_thread, SIGNAL( finished() ), this, SLOT( deleteLater() ) );
}
LibraryFile::~LibraryFile( void )
{
if( m_thread ) {
m_thread->wait();
delete m_thread;
}
}
void LibraryFile::unload( void )
{
m_count = 0;
m_version = 0;
m_loaded = false;
m_mustIdle = true;
m_items.clear();
}
bool LibraryFile::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 ) {
TibiaFile::seek( 0 );
quint16 internalVersion = 0;
QDataStream in( this );
in.setByteOrder( QDataStream::LittleEndian );
in >> internalVersion;
in >> m_count;
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_loaded = true;
return true;
}
ItemData LibraryFile::getItemData( qint32 index )
{
QMutexLocker locker( &mutex );
if( isIdle() ) {
QDataStream in( this );
in.setByteOrder( QDataStream::LittleEndian );
TibiaFile::seek( sizeof( quint16 ) + sizeof( m_count ) + sizeof( m_version ) + index * sizeof( quint32 ) );
quint32 offset;
in >> offset;
if( offset == 0x00000000 ) // Blank item
return ItemData();
TibiaFile::seek( offset );
QString error;
quint32 address = 0;
ItemData itemData;
if( ExternalFile::readItem( in, itemData, m_datFormat, this, index, address, error ) ) {
m_spriteHash.insert( index, address );
return itemData;
} else
emit documentError( fileName(), error, -1 );
}
if( m_internalItems.contains( index ) )
return m_internalItems.value( index );
if( m_items.value( index ) )
return m_items.value( index )->getItemData();
return ItemData();
}
TibiaSprite LibraryFile::getSprite( qint32 index, qint32 frame )
{
QMutexLocker locker( &mutex );
TibiaSprite sprite;
quint32 address = m_spriteHash.value( index );
if( address == 0 ) {
getItemData( index );
}
if( address != 0 ) {
QDataStream in( this );
in.setByteOrder( QDataStream::LittleEndian );
TibiaFile::seek( 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 LibraryFile::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 << ( quint16 )0xACED;
dest << ( quint32 )m_items.size();
dest << m_version;
bool 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;
}
}
quint32 now = file.pos();
quint32 offset = now + m_items.size() * sizeof( quint32 );
for( quint32 i = 0; i < m_count; i++ )
{
ItemData itemData = getItemData( i );
file.seek( now );
if( itemData.isNull() )
{
dest << ( quint32 )0x00000000;
now += sizeof( quint32 );
continue;
}
dest << offset;
file.seek( offset );
success = ExternalFile::saveItem( dest, itemData, m_datFormat );
if( !success )
break;
offset = file.pos();
now += sizeof( quint32 );
}
if( success )
{
file.copy( name );
idle( name, true ); // Upon saving we must re-idle our new file to continue
}
return 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 << ( quint16 )0xACED;
out << ( quint32 )m_items.size();
out << m_version;
bool 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;
}
quint32 now = TibiaFile::pos();
quint32 offset = now + m_items.size() * sizeof( quint32 );
for( quint32 i = 0; i < m_count; i++ )
{
ItemData itemData = getItemData( i );
TibiaFile::seek( now );
if( itemData.isNull() )
{
out << ( quint32 )0x00000000;
now += sizeof( quint32 );
continue;
}
out << offset;
TibiaFile::seek( offset );
success = ExternalFile::saveItem( out, itemData, m_datFormat );
if( !success )
break;
offset = TibiaFile::pos();
now += sizeof( quint32 );
}
if( success )
{
if( m_mustIdle )
success = idle( name, true ); // Upon saving we must re-idle our new file to continue
}
TibiaFile::close();
return success;
//m_thread->setup();
//m_thread->setName( name );
//m_thread->execute();
return true;
}*/
void LibraryFile::setItemData( qint32 index, const ItemData& item )
{
QMutexLocker locker( &mutex );
m_internalItems.insert( index, item );
}