-
Notifications
You must be signed in to change notification settings - Fork 4
/
spritefile.cpp
177 lines (145 loc) · 4.54 KB
/
spritefile.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
#include <QMap>
#include <QDir>
#include <QTemporaryFile>
#include "spritefile.h"
#include "resourcehandler.h"
#include "userthread.h"
extern ResourceHandler g_resourceHandler;
SpriteFile::SpriteFile( QObject *parent ) : TibiaFile( parent )
{
m_count = 0;
m_loaded = false;
m_thread = new SpriteThread( this );
}
SpriteFile::~SpriteFile( void )
{
delete m_thread;
}
void SpriteFile::unload( void )
{
TibiaFile::unload();
m_count = 0;
m_loaded = false;
}
bool SpriteFile::createNew( void )
{
if( !m_loaded ) {
m_count = 2;
TibiaSprite sprite;
sprite.id = 1;
sprite.setDummy( false );
SharedResource newResource = g_resourceHandler.createLocalResource( RESOURCE_TYPE_SPRITE, 1, sprite );
ResourceList resources;
resources.push_back( newResource );
g_resourceHandler.addLocalResources( RESOURCE_TYPE_SPRITE, 1, resources );
m_loaded = true;
return true;
}
return false;
}
bool SpriteFile::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;
}
TibiaFile::seek( 0 );
if( read ) {
QDataStream in( this );
in.setByteOrder( QDataStream::LittleEndian );
in >> m_signature;
in >> m_count;
}
m_idle = true;
m_loaded = true;
return m_idle;
}
/*bool SpriteFile::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;
}
quint16 currentSprite = 1;
quint32 offset = 0, now = 0;
QDataStream dest( &file );
dest.setByteOrder( QDataStream::LittleEndian );
dest << m_signature;
dest << m_count;
now = file.pos();
offset = m_count * sizeof( offset ) + sizeof( m_signature ) + sizeof( m_count );
// Loop 1 - Count, check "spriteChanges" before reading original
while ( currentSprite < m_count )
{
file.seek( now );
TibiaSprite sprite = getSprite( currentSprite );
if( sprite.isDummy() ){
dest << ( quint32 )0x00000000;
now += sizeof( quint32 );
currentSprite++;
continue;
}
dest << offset;
file.seek( offset );
TibiaFile::writeSprite( dest, sprite, 0, 0, offset );
now += sizeof( quint32 );
currentSprite++;
}
if( QFile::exists( name ) )
{
if( name.compare( TibiaFile::fileName(), Qt::CaseInsensitive ) == 0 ) // Destination == Source, Close source
TibiaFile::close();
if( !QFile::remove( name ) ) // Overwrite Error
return false;
}
file.copy( name );
idle( name, true ); // Upon saving we must re-idle our new file to continue
return true;
}
return false;
//m_thread->setup();
//m_thread->setName( name );
//m_thread->execute();
return true;
}*/
TibiaSprite SpriteFile::loadSprite( quint16 spriteId )
{
QMutexLocker locker( &mutex );
TibiaSprite sprite;
sprite.id = spriteId;
if( isIdle() ) { // We are idling our file so we can read constantly
if( spriteId < 1 || spriteId > m_count )
return sprite;
QDataStream in( this );
in.setByteOrder( QDataStream::LittleEndian );
seek( sizeof( m_signature ) + sizeof( m_count ) + ( ( spriteId - 1 ) * sizeof( quint32 ) ) );
quint32 offset;
in >> offset;
if ( offset == 0 ) // Don't cache dummy images
return sprite;
TibiaFile::seek( offset );
sprite.id = spriteId;
sprite.setDummy( false );
TibiaFile::readSprite( in, sprite, 0, 0 );
return sprite;
}
return sprite;
}
TibiaSprite SpriteFile::getSprite( quint16 id )
{
SharedResource resource = g_resourceHandler.loadLocalResource( RESOURCE_TYPE_SPRITE, id, false );
if( resource )
return g_resourceHandler.getSpriteByResource( resource );
if( isIdle() )
return loadSprite( id );
return dummy;
}