forked from opentibia-xx/otserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileloader.cpp
542 lines (494 loc) · 11.3 KB
/
fileloader.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include "fileloader.h"
#include <cmath>
FileLoader::FileLoader()
{
m_file = NULL;
m_root = NULL;
m_buffer = new unsigned char[1024];
m_buffer_size = 1024;
m_lastError = ERROR_NONE;
//cache
m_use_cache = false;
m_cache_size = 0;
m_cache_index = NO_VALID_CACHE;
m_cache_offset = NO_VALID_CACHE;
memset(m_cached_data, 0, sizeof(m_cached_data));
}
FileLoader::~FileLoader()
{
if(m_file){
fclose(m_file);
m_file = NULL;
}
NodeStruct::clearNet(m_root);
delete[] m_buffer;
for(int i = 0; i < CACHE_BLOCKS; ++i){
if(m_cached_data[i].data)
delete[] m_cached_data[i].data;
}
}
bool FileLoader::openFile(const char* filename, bool write, bool caching /*= false*/)
{
if(write) {
m_file = fopen(filename, "wb");
if(m_file) {
uint32_t version = 0;
writeData(&version, sizeof(version), false);
return true;
}
else{
m_lastError = ERROR_CAN_NOT_CREATE;
return false;
}
}
else {
m_file = fopen(filename, "rb");
if(m_file){
uint32_t version;
if(fread(&version, sizeof(version), 1, m_file) && version > 0){
fclose(m_file);
m_file = NULL;
m_lastError = ERROR_INVALID_FILE_VERSION;
return false;
}
else{
if(caching){
m_use_cache = true;
fseek(m_file, 0, SEEK_END);
int file_size = ftell(m_file);
m_cache_size = std::min(32768, std::max(file_size/20, 8192)) & ~0x1FFF;
}
//parse nodes
if(safeSeek(4)){
delete m_root;
m_root = new NodeStruct();
m_root->start = 4;
int byte;
if(safeSeek(4) && readByte(byte) && byte == NODE_START){
bool ret = parseNode(m_root);
return ret;
}
else{
return false;
}
}
else{
m_lastError = ERROR_INVALID_FORMAT;
return false;
}
}
}
else{
m_lastError = ERROR_CAN_NOT_OPEN;
return false;
}
}
}
bool FileLoader::parseNode(NODE node)
{
int byte;
long pos;
NODE currentNode = node;
while(1){
//read node type
if(readByte(byte)){
currentNode->type = byte;
bool setPropsSize = false;
while(1){
//search child and next node
if(readByte(byte)){
if(byte == NODE_START){
//child node start
if(safeTell(pos)){
NODE childNode = new NodeStruct();
childNode->start = pos;
setPropsSize = true;
currentNode->propsSize = pos - currentNode->start - 2;
currentNode->child = childNode;
if(!parseNode(childNode)){
return false;
}
}
else{
return false;
}
}
else if(byte == NODE_END){
//current node end
if(!setPropsSize){
if(safeTell(pos)){
currentNode->propsSize = pos - currentNode->start - 2;
}
else{
return false;
}
}
if(readByte(byte)){
if(byte == NODE_START){
//starts next node
if(safeTell(pos)){
NODE nextNode = new NodeStruct();
nextNode->start = pos;
currentNode->next = nextNode;
currentNode = nextNode;
break;
}
else{
return false;
}
}
else if(byte == NODE_END){
//up 1 level and move 1 position back
if(safeTell(pos) && safeSeek(pos)){
return true;
}
else{
return false;
}
}
else{
//wrong format
m_lastError = ERROR_INVALID_FORMAT;
return false;
}
}
else{
//end of file?
return true;
}
}
else if(byte == ESCAPE_CHAR){
if(!readByte(byte))
return false;
}
}
else{
return false;
}
}
}
else{
return false;
}
}
}
const unsigned char* FileLoader::getProps(const NODE node, unsigned long &size)
{
if(node){
while(node->propsSize >= m_buffer_size){
delete[] m_buffer;
while (node->propsSize >= m_buffer_size)
m_buffer_size *= 2;
m_buffer = new unsigned char[m_buffer_size];
}
//get buffer
if(readBytes(m_buffer, node->propsSize, node->start + 2)){
//unscape buffer
unsigned int j = 0;
bool escaped = false;
for(unsigned int i = 0; i < node->propsSize; ++i, ++j){
if(m_buffer[i] == ESCAPE_CHAR){
//escape char found, skip it and write next
++i;
m_buffer[j] = m_buffer[i];
//is neede a displacement for next bytes
escaped = true;
}
else if(escaped){
//perform that displacement
m_buffer[j] = m_buffer[i];
}
else{
//the buffer is right as is
}
}
size = j;
return m_buffer;
}
else{
return NULL;
}
}
else{
return NULL;
}
}
bool FileLoader::getProps(const NODE node, PropStream &props)
{
unsigned long size;
const unsigned char* a = getProps(node, size);
if(!a){
props.init(NULL, 0);
return false;
}
else{
props.init((char*)a, size);
return true;
}
}
int FileLoader::setProps(void* data, unsigned short size)
{
//data
if(!writeData(data, size, true))
return getError();
return ERROR_NONE;
}
void FileLoader::startNode(unsigned char type)
{
unsigned char nodeBegin = NODE_START;
writeData(&nodeBegin, sizeof(nodeBegin), false);
writeData(&type, sizeof(type), true);
}
void FileLoader::endNode()
{
unsigned char nodeEnd = NODE_END;
writeData(&nodeEnd, sizeof(nodeEnd), false);
}
const NODE FileLoader::getChildNode(const NODE parent, unsigned long &type)
{
if(parent){
NODE child = parent->child;
if(child){
type = child->type;
}
return child;
}
else{
type = m_root->type;
return m_root;
}
}
const NODE FileLoader::getNextNode(const NODE prev, unsigned long &type)
{
if(prev){
NODE next = prev->next;
if(next){
type = next->type;
}
return next;
}
else{
return NO_NODE;
}
}
inline bool FileLoader::readByte(int &value)
{
if(m_use_cache){
if(m_cache_index == NO_VALID_CACHE){
m_lastError = ERROR_CACHE_ERROR;
return false;
}
if(m_cache_offset >= m_cached_data[m_cache_index].size){
long pos = m_cache_offset + m_cached_data[m_cache_index].base;
long tmp = getCacheBlock(pos);
if(tmp < 0)
return false;
m_cache_index = tmp;
m_cache_offset = pos - m_cached_data[m_cache_index].base;
if(m_cache_offset >= m_cached_data[m_cache_index].size){
return false;
}
}
value = m_cached_data[m_cache_index].data[m_cache_offset];
m_cache_offset++;
return true;
}
else{
value = fgetc(m_file);
if(value == EOF){
m_lastError = ERROR_EOF;
return false;
}
else
return true;
}
}
inline bool FileLoader::readBytes(unsigned char* buffer, unsigned int size, long pos)
{
if(m_use_cache){
//seek at pos
unsigned long reading, remain = size, bufferPos = 0;
do{
//prepare cache
unsigned long i = getCacheBlock(pos);
if(i == NO_VALID_CACHE)
return false;
m_cache_index = i;
m_cache_offset = pos - m_cached_data[i].base;
//get maximun read block size and calculate remaining bytes
reading = std::min(remain, m_cached_data[i].size - m_cache_offset);
remain = remain - reading;
//read it
memcpy(buffer + bufferPos, m_cached_data[m_cache_index].data + m_cache_offset, reading);
//update variables
m_cache_offset = m_cache_offset + reading;
bufferPos = bufferPos + reading;
pos = pos + reading;
}while(remain > 0);
return true;
}
else{
if(fseek(m_file, pos, SEEK_SET)){
m_lastError = ERROR_SEEK_ERROR;
return false;
}
size_t value = fread(buffer, 1, size, m_file);
if(value != size){
m_lastError = ERROR_EOF;
return false;
}
else{
return true;
}
}
}
/*
inline bool FileLoader::writeData(void* data, int size, bool unescape)
{
for(int i = 0; i < size; ++i) {
unsigned char c = *(((unsigned char*)data) + i);
if(unescape && (c == NODE_START || c == NODE_END || c == ESCAPE_CHAR)) {
unsigned char escape = ESCAPE_CHAR;
int value = fwrite(&escape, 1, 1, m_file);
if(value != 1) {
m_lastError = ERROR_COULDNOTWRITE;
return false;
}
}
int value = fwrite(&c, 1, 1, m_file);
if(value != 1) {
m_lastError = ERROR_COULDNOTWRITE;
return false;
}
}
return true;
}
*/
inline bool FileLoader::checks(const NODE node)
{
if(!m_file){
m_lastError = ERROR_NOT_OPEN;
return false;
}
if(!node){
m_lastError = ERROR_INVALID_NODE;
return false;
}
return true;
}
inline bool FileLoader::safeSeek(unsigned long pos)
{
if(m_use_cache){
unsigned long i = getCacheBlock(pos);
if(i == NO_VALID_CACHE)
return false;
m_cache_index = i;
m_cache_offset = pos - m_cached_data[i].base;
}
else{
if(fseek(m_file, pos, SEEK_SET)){
m_lastError = ERROR_SEEK_ERROR;
return false;
}
}
return true;
}
inline bool FileLoader::safeTell(long &pos)
{
if(m_use_cache){
if(m_cache_index == NO_VALID_CACHE){
m_lastError = ERROR_CACHE_ERROR;
return false;
}
pos = m_cached_data[m_cache_index].base + m_cache_offset - 1;
return true;
}
else{
pos = ftell(m_file);
if(pos == -1){
m_lastError = ERROR_TELL_ERROR;
return false;
}
else{
pos = pos - 1;
return true;
}
}
}
inline unsigned long FileLoader::getCacheBlock(unsigned long pos)
{
bool found = false;
unsigned long i;
unsigned long base_pos = pos & ~(m_cache_size - 1);
for(i = 0; i < CACHE_BLOCKS; ++i){
if(m_cached_data[i].loaded){
if(m_cached_data[i].base == base_pos){
found = true;
break;
}
}
}
if(!found){
i = loadCacheBlock(pos);
}
return i;
}
long FileLoader::loadCacheBlock(unsigned long pos)
{
long i;
long loading_cache = -1;
long base_pos = pos & ~(m_cache_size - 1);
for(i = 0; i < CACHE_BLOCKS; ++i){
if(!m_cached_data[i].loaded){
loading_cache = i;
break;
}
}
if(loading_cache == -1){
for(i = 0; i < CACHE_BLOCKS; ++i){
if((long)(labs((long)m_cached_data[i].base - base_pos)) > (long)(2*m_cache_size)){
loading_cache = i;
break;
}
}
if(loading_cache == -1){
loading_cache = 0;
}
}
if(m_cached_data[loading_cache].data == NULL){
m_cached_data[loading_cache].data = new unsigned char[m_cache_size];
}
m_cached_data[loading_cache].base = base_pos;
if(fseek(m_file, m_cached_data[loading_cache].base, SEEK_SET)){
m_lastError = ERROR_SEEK_ERROR;
return -1;
}
size_t size = fread(m_cached_data[loading_cache].data, 1, m_cache_size, m_file);
m_cached_data[loading_cache].size = size;
if(size < (pos - m_cached_data[loading_cache].base)){
m_lastError = ERROR_SEEK_ERROR;
return -1;
}
m_cached_data[loading_cache].loaded = 1;
return loading_cache;
}