-
Notifications
You must be signed in to change notification settings - Fork 28
/
wzmain.cpp
158 lines (151 loc) · 4.46 KB
/
wzmain.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
///////////////////////////////////
// Copyright 2012 Peter Atechian //
// Licensed under GPLv3 //
///////////////////////////////////
#include "wzmain.h"
namespace WZ {
path Path;
Node Base;
vector<path> Paths;
bool Lazy;
class File {
public:
File(Node n);
private:
void Directory(Node n);
uint32_t fileStart;
MapFile file;
};
File::File(Node n) {
path filename = Path / path(n.Name() + ".wz");
if (!exists(filename)) die();
file.Open(filename);
if (file.Read<uint32_t>() != *(uint32_t *)"PKG1") throw;
uint64_t fileSize = file.Read<uint64_t>();
fileStart = file.Read<uint32_t>();
file.Seek(fileStart);
if (!Version) {
EncVersion = file.Read<int16_t>();
int32_t count = file.ReadCInt();
uint32_t c = 0;
for (int k = 0; k < count; ++k) {
uint8_t type = file.Read<uint8_t>();
if (type == 3) {
file.ReadEncString();
file.ReadCInt();
file.ReadCInt();
file.Read<uint32_t>();
continue;
} else if (type == 4) {
file.ReadEncString();
file.ReadCInt();
file.ReadCInt();
c = file.Tell();
break;
} else
die();
}
if (c == 0) die();
bool success = false;
for (uint8_t j = 0; j < 3 && !success; ++j) {
Key = Keys[j];
AKey = AKeys[j];
WKey = WKeys[j];
for (Version = 0; Version < 512; ++Version) {
string s = to_string(Version);
size_t l = s.length();
VersionHash = 0;
for (int i = 0; i < l; ++i) VersionHash = 32 * VersionHash + s[i] + 1;
uint32_t result = 0xFF ^ (VersionHash >> 24) ^ (VersionHash << 8 >> 24)
^ (VersionHash << 16 >> 24) ^ (VersionHash << 24 >> 24);
if (result == EncVersion) {
file.Seek(c);
uint32_t off = file.ReadOffset(fileStart);
if (off > fileSize) continue;
file.Seek(off);
uint8_t a = file.Read<uint8_t>();
if (a != 0x73) continue;
string ss = file.ReadEncString();
if (ss != "Property") continue;
success = true;
break;
}
}
}
if (!success) die();
file.Seek(fileStart + 2);
} else {
int16_t eversion = file.Read<int16_t>();
if (eversion != EncVersion) die();
}
if (file.ReadCInt() == 0) die();
file.Seek(fileStart + 2);
Directory(n);
delete this;
}
void File::Directory(Node n) {
int32_t count = file.ReadCInt();
if (count == 0) {
new File(n);
return;
}
vector<Node> dirs;
n.Reserve(count);
for (int i = 0; i < count; ++i) {
char * name;
uint8_t type = file.Read<uint8_t>();
if (type == 1) {
file.Skip(10);
die();
continue;
} else if (type == 2) {
int32_t s = file.Read<int32_t>();
uint32_t p = file.Tell();
file.Seek(fileStart + s);
type = file.Read<uint8_t>();
name = file.ReadEncString();
file.Seek(p);
} else if (type == 3)
name = file.ReadEncString();
else if (type == 4)
name = file.ReadEncString();
else
die();
uint32_t fsize = file.ReadCInt();
uint32_t checksum = file.ReadCInt();
uint32_t off = file.ReadOffset(fileStart);
if (type == 3) { dirs.emplace_back(n.g(name, i)); } else if (type == 4) {
size_t len = strlen(name);
name[len - 4] = '\0';
new Img(file, n.g(name, i), fsize, off);
} else
die();
}
for (Node nn : dirs) { Directory(nn); }
}
void Load(string name) {
Base.InitTop(name);
new File(Base);
if (!Lazy) {
for (Img * img : Img::Imgs) img->Parse();
Base.Resolve();
}
}
void Init(bool lazy) {
GenKeys();
Lazy = lazy;
for (path p : Paths) {
Path = p;
if (exists(Path / path("Data.wz"))) {
Load("Data");
return;
}
if (exists(Path / path("Base.wz"))) {
Load("Base");
return;
}
}
die();
}
void AddPath(string path) { Paths.push_back(path); }
}