-
Notifications
You must be signed in to change notification settings - Fork 7
/
DATDisk.cpp
491 lines (368 loc) · 9.83 KB
/
DATDisk.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
#include "StdAfx.h"
#include "Preferences.h"
#include "DATDisk.h"
DATDisk *DATDisk::pPortal = NULL;
DATDisk *DATDisk::pCell = NULL;
BOOL DATDisk::OpenDisks()
{
pPortal = new DATDisk(Preferences::GetPortalDATPath());
if (!pPortal->Open())
return FALSE;
pCell = new DATDisk(Preferences::GetCellDATPath());
if (!pCell->Open())
return FALSE;
return TRUE;
}
void DATDisk::CloseDisks()
{
if (pPortal)
{
delete pPortal;
pPortal = NULL;
}
if (pCell)
{
delete pCell;
pCell = NULL;
}
}
DATDisk::DATDisk(const char *Path) : m_BTree(&m_BlockLoader)
{
m_FilePath = strdup(Path);
ZeroMemory(&m_DATHeader, sizeof(DATHeader));
}
DATDisk::~DATDisk()
{
if (m_FilePath)
free(m_FilePath);
}
BOOL DATDisk::Open()
{
if (!m_BlockLoader.Init(m_FilePath, &m_DATHeader))
return FALSE;
if (!m_BTree.Init())
return FALSE;
return TRUE;
}
void DATDisk::FindFileIDsWithinRange(DWORD Min, DWORD Max, void(*FileCallback)(DWORD, BTreeEntry *), void(*ProgressCallback)(float))
{
Time::Update();
double startTime = Time::GetTimeElapsed();
m_BTree.SetFileCallback(FileCallback);
m_BTree.SetProgressCallback(ProgressCallback);
m_BTree.FindEntryIDsWithinRange(Min, Max, 0, 100.0);
Time::Update();
double endTime = Time::GetTimeElapsed();
DEBUGOUT("File search completed in %f seconds.\r\n", endTime - startTime);
}
BOOL DATDisk::GetData(DWORD ID, DATEntry *pEntry)
{
BTreeEntry FileInfo;
// Look up the file within the BTree.
if (!m_BTree.Lookup(ID, &FileInfo))
return FALSE;
// Attempt to allocate a file buffer. (Extra DWORD for block loading)
BYTE *Buffer = new BYTE[ sizeof(DWORD) + FileInfo.Length ];
if (!Buffer)
return FALSE;
if (!m_BlockLoader.LoadData(FileInfo.BlockHead, Buffer, FileInfo.Length))
{
delete[] Buffer;
return FALSE;
}
if (pEntry)
{
pEntry->ID = ID;
pEntry->Data = Buffer;
pEntry->Length = FileInfo.Length;
}
else
delete[] Buffer;
return TRUE;
}
BOOL DATDisk::GetDataEx(DWORD BlockHead, void *Data, DWORD Length)
{
if (!m_BlockLoader.LoadData(BlockHead, Data, Length))
return FALSE;
return TRUE;
}
void (*BTreeNode::m_pfnFileCallback)(DWORD, BTreeEntry *);
void (*BTreeNode::m_pfnProgressCallback)(float);
BTreeNode::BTreeNode(BlockLoader *pBlockLoader)
{
m_pBlockLoader = pBlockLoader;
for (DWORD i = 0; i < 0x3E; i++)
m_Branches[i] = NULL;
}
BTreeNode::~BTreeNode()
{
for (DWORD i = 0; i < 0x3E; i++)
{
if (m_Branches[i])
delete m_Branches[i];
}
}
BOOL BTreeNode::LoadData(DWORD BlockHead)
{
if (!BlockHead)
return FALSE;
if (!m_pBlockLoader->LoadData(BlockHead, &m_TreeData, sizeof(BTreeData) - sizeof(DWORD)))
return FALSE;
m_bLeaf = (!m_TreeData.Branches[0] ? TRUE : FALSE);
return TRUE;
}
DWORD BTreeNode::GetBranchCount() const
{
if (m_bLeaf)
return 0;
return(m_TreeData.EntryCount + 1);
}
void BTreeNode::LoadChildren(void)
{
for (DWORD i = 0; i < GetBranchCount(); i++)
{
if (m_Branches[i])
continue;
m_Branches[i] = new BTreeNode(m_pBlockLoader);
if (!m_Branches[i])
continue;
if (!m_Branches[i]->LoadData(m_TreeData.Branches[i]))
{
delete m_Branches[i];
m_Branches[i] = NULL;
}
}
}
void BTreeNode::LoadChildrenRecursive(void)
{
for (DWORD i = 0; i < GetBranchCount(); i++)
{
if (m_Branches[i])
continue;
m_Branches[i] = new BTreeNode(m_pBlockLoader);
if (!m_Branches[i])
continue;
if (m_Branches[i]->LoadData(m_TreeData.Branches[i]))
m_Branches[i]->LoadChildrenRecursive();
else
{
delete m_Branches[i];
m_Branches[i] = NULL;
}
}
}
void BTreeNode::SetFileCallback(void (*pfnFileCallback)(DWORD, BTreeEntry *))
{
m_pfnFileCallback = pfnFileCallback;
}
void BTreeNode::SetProgressCallback(void (*pfnProgressCallback)(float))
{
m_pfnProgressCallback = pfnProgressCallback;
}
BTreeNode* BTreeNode::GetBranch(DWORD index)
{
if (!m_Branches[index])
{
if (m_Branches[index] = new BTreeNode(m_pBlockLoader))
{
if (!m_Branches[index]->LoadData(m_TreeData.Branches[index]))
{
delete m_Branches[index];
m_Branches[index] = NULL;
}
}
}
return m_Branches[index];
}
BOOL BTreeNode::Lookup(DWORD ID, BTreeEntry *pEntry)
{
unsigned int i;
for (i = 0; i < m_TreeData.EntryCount; i++)
{
if (ID < m_TreeData.Entries[i].ID)
{
BTreeNode *Branch;
if (!m_bLeaf && (Branch = GetBranch(i)))
return Branch->Lookup(ID, pEntry);
else
return FALSE;
}
if (ID == m_TreeData.Entries[i].ID)
{
if (pEntry)
*pEntry = m_TreeData.Entries[i];
return TRUE;
}
}
BTreeNode *LastBranch;
if (!m_bLeaf && (LastBranch = GetBranch(i)))
return LastBranch->Lookup(ID, pEntry);
else
return FALSE;
}
void BTreeNode::FindEntryIDsWithinRange(DWORD Min, DWORD Max, float Progress, float ProgressDelta)
{
DWORD NumBranches = GetBranchCount();
float BranchDelta = (NumBranches ? ProgressDelta / NumBranches : 0);
float ProgressStart = Progress;
unsigned int i;
for (i = 0; i < m_TreeData.EntryCount; i++)
{
DWORD ID = m_TreeData.Entries[i].ID;
if (ID > Max)
{
BTreeNode *Branch;
if (!m_bLeaf && (Branch = GetBranch(i)))
Branch->FindEntryIDsWithinRange(Min, Max, Progress, BranchDelta);
return;
}
if (ID < Min)
continue;
BTreeNode *Branch;
if (!m_bLeaf && (Branch = GetBranch(i)))
Branch->FindEntryIDsWithinRange(Min, Max, Progress, BranchDelta);
if (m_pfnFileCallback)
m_pfnFileCallback(ID, &m_TreeData.Entries[i]);
if (!m_bLeaf)
{
Progress += BranchDelta;
if (m_pfnProgressCallback)
m_pfnProgressCallback(Progress);
}
}
if (!m_bLeaf)
{
BTreeNode *LastBranch;
if (LastBranch = GetBranch(i))
LastBranch->FindEntryIDsWithinRange(Min, Max, Progress, BranchDelta);
}
Progress = ProgressStart + ProgressDelta;
if (m_pfnProgressCallback)
m_pfnProgressCallback(Progress);
}
BTree::BTree(BlockLoader *pBlockLoader) : BTreeNode(pBlockLoader)
{
}
BTree::~BTree()
{
}
BOOL BTree::Init()
{
if (!LoadData(m_pBlockLoader->GetTreeOrigin()))
return FALSE;
// Preload immediate children. ?
// LoadChildren();
// Preload ALL children. ? (very slow)
// LoadChildrenRecursive();
return TRUE;
}
BlockLoader::BlockLoader()
{
m_pHeader = NULL;
}
BlockLoader::~BlockLoader()
{
}
DWORD BlockLoader::GetTreeOrigin()
{
if (!m_pHeader)
return 0;
return m_pHeader->BTree;
}
BOOL BlockLoader::Init(const char *Path, DATHeader *pHeader)
{
m_pHeader = pHeader;
return m_DiskDev.OpenFile(Path, m_pHeader);
}
BOOL BlockLoader::LoadData(DWORD HeadBlock, void *pBuffer, DWORD Length)
{
BYTE* pbBuffer = (BYTE *)pBuffer;
DWORD dwLength = Length;
DWORD dwBlockSize = m_pHeader->BlockSize;
DWORD dwDataPerBlock = dwBlockSize - sizeof(DWORD);
BOOL bLoadOK = TRUE;
if (!dwLength)
return TRUE;
DWORD dwBlock = HeadBlock;
while (dwBlock)
{
if (!bLoadOK)
return FALSE;
DWORD dwOldValue = *((DWORD *)pbBuffer);
if (dwDataPerBlock > dwLength)
{
dwBlockSize = dwLength + sizeof(DWORD);
dwDataPerBlock = dwLength;
}
if (m_DiskDev.SyncRead(pbBuffer, dwBlockSize, dwBlock))
{
dwBlock = *((DWORD *)pbBuffer);
*((DWORD *)pbBuffer) = dwOldValue;
dwLength -= dwDataPerBlock;
pbBuffer += dwDataPerBlock;
if (dwBlock & 0x80000000)
{
dwBlock &= ~0x80000000;
bLoadOK = FALSE;
}
}
else
bLoadOK = FALSE;
if (!dwLength)
break;
}
if (dwLength)
bLoadOK = FALSE;
return bLoadOK;
}
DiskDev::DiskDev()
{
m_hFile = INVALID_HANDLE_VALUE;
}
DiskDev::~DiskDev()
{
CloseFile();
}
BOOL DiskDev::OpenFile(const char* Path, DATHeader *pHeader)
{
CloseFile();
m_hFile = CreateFile(Path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
if (m_hFile == INVALID_HANDLE_VALUE)
return FALSE;
if (pHeader)
{
if (!SyncRead(pHeader, sizeof(DATHeader), DAT_HEADER_OFFSET))
return FALSE;
}
return TRUE;
}
void DiskDev::CloseFile()
{
if (m_hFile != INVALID_HANDLE_VALUE)
{
CloseHandle(m_hFile);
m_hFile = INVALID_HANDLE_VALUE;
}
}
BOOL DiskDev::SyncRead(void *pBuffer, DWORD dwLength, DWORD dwPosition)
{
if (INVALID_SET_FILE_POINTER == SetFilePointer(m_hFile, dwPosition, 0, FILE_BEGIN))
return FALSE;
DWORD dwBytesRead;
if (!ReadFile(m_hFile, pBuffer, dwLength, &dwBytesRead, FALSE))
return FALSE;
if (dwBytesRead != dwLength)
return FALSE;
return TRUE;
}
BOOL DiskDev::SyncWrite(void *pBuffer, DWORD dwLength, DWORD dwPosition)
{
if (INVALID_SET_FILE_POINTER == SetFilePointer(m_hFile, dwPosition, 0, FILE_BEGIN))
return FALSE;
DWORD dwBytesWritten;
if (!WriteFile(m_hFile, pBuffer, dwLength, &dwBytesWritten, FALSE))
return FALSE;
if (dwBytesWritten != dwLength)
return FALSE;
return TRUE;
}