-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDat2Dir.cpp
More file actions
109 lines (85 loc) · 2.86 KB
/
Dat2Dir.cpp
File metadata and controls
109 lines (85 loc) · 2.86 KB
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
#include <cstdio>
#include <iostream>
#include <ttvfs/VFSFile.h>
#include <ttvfs/VFSTools.h>
#include "Dat2ArchiveRef.h"
#include "Dat2File.h"
#include "Dat2Dir.h"
namespace ttvfs
{
int read32(File *file)
{
char buffer[4];
file->read(&buffer, 4);
return int((unsigned char) (buffer[3]) << 24 |
(unsigned char) (buffer[2]) << 16 |
(unsigned char) (buffer[1]) << 8 |
(unsigned char) (buffer[0]));
}
char read8(File *file)
{
char buffer;
file->read(&buffer, 1);
return buffer;
}
Dat2Dir::Dat2Dir(Dat2ArchiveRef *handle, const char *fullpath, bool canLoad)
: Dir(fullpath, NULL),
_archiveHandle(handle),
_canLoad(canLoad),
_couldLoad(canLoad)
{
}
Dat2Dir::~Dat2Dir()
{
close();
}
void Dat2Dir::close()
{
_archiveHandle->close();
_canLoad = _couldLoad; // allow loading again after re-opening (in case archive was replaced)
}
DirBase *Dat2Dir::createNew(const char *fullpath) const
{
const Dat2ArchiveRef *czref = _archiveHandle;
Dat2ArchiveRef *zref = const_cast<Dat2ArchiveRef*>(czref);
return new Dat2Dir(zref, fullpath, false);
}
void Dat2Dir::load()
{
if (!_canLoad) {
return;
}
const Dat2ArchiveRef *czref = _archiveHandle;
Dat2ArchiveRef *zref = const_cast<Dat2ArchiveRef*>(czref);
_archiveHandle->openRead();
File *file = _archiveHandle->archiveFile.content();
file->seek(-8, SEEK_END);
int filesTreeSize = read32(file);
file->seek(-filesTreeSize - 8, SEEK_END);
int filesCount = read32(file);
for (unsigned int i = 0; i != filesCount; ++i) {
int filenameSize = read32(file);
std::string filename;
filename.resize(filenameSize);
file->read(&filename[0], filenameSize);
std::replace(filename.begin(), filename.end(), '\\', '/');
std::transform(filename.begin(), filename.end(), filename.begin(), ::tolower);
char isCompressed = read8(file);
unsigned int unpackedSize = read32(file);
unsigned int packedSize = read32(file);
unsigned int dataOffset = read32(file);
std::string dir = filename;
StripLastPath(dir);
if (!getDir(dir.c_str())) {
_createAndInsertSubtree(dir.c_str());
}
Dat2File *vf = new Dat2File(filename.c_str(), _archiveHandle);
vf->setIsCompressed(isCompressed);
vf->setPackedSize(packedSize);
vf->setUnpackedSize(unpackedSize);
vf->setDataOffset(dataOffset);
_addRecursiveSkip(vf, fullnameLen() + 1);
}
_canLoad = false;
}
}