-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
302 lines (245 loc) · 8.08 KB
/
Copy pathmain.cpp
File metadata and controls
302 lines (245 loc) · 8.08 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
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
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <iostream>
#include <vector>
#include <cmath>
#define SQLITECPP_COMPILE_DLL
#include <SQLiteCpp/SQLiteCpp.h>
#include <SQLiteCpp/VariadicBind.h>
#include "content_structs.hpp"
#include <unordered_map>
#include <filesystem>
#include <thread>
#include <mutex>
#include <coroutine>
#include <zstd.h>
using namespace SQLite;
namespace fs = std::filesystem;
// Console Pretty Print Start
#define COLOR_RED 4
#define COLOR_BLUE 3
HANDLE CONSOLE = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
WORD saved_attributes;
template <typename T>
void colored_print(T str, WORD color = COLOR_BLUE)
{
SetConsoleTextAttribute(CONSOLE, color);
std::cout << str;
SetConsoleTextAttribute(CONSOLE, saved_attributes);
}
// Console Pretty Print End
//Some data Start
#define HASH_SIZE 32
#define ABSOLUTE_DB_PATH
Database contentDB(std::string(getenv("LOCALAPPDATA") + std::string("\\Space Station 14\\launcher\\content.db")));
std::unordered_map<uint32_t, PathInfo> Manifest;
std::vector<ContentVersion> Forks;
std::unordered_map<uint32_t, ContentData> Content;
bool isThreadStopped = false;
std::string LastTrackedSymbol;
uint32_t CurrentIndexFile = 0;
std::mutex mtx;
//Some data End
std::vector<ContentVersion> FetchForks()
{
Statement data(contentDB, "SELECT * FROM ContentVersion");
std::vector<ContentVersion> content;
while (data.executeStep())
{
uint32_t Id = data.getColumn(0).getInt();
void* Hash = (void*)data.getColumn(1).getBlob();
std::string ForkId = data.getColumn(2).getString();
std::string ForkVersion = data.getColumn(3).getString();
content.push_back({ Id, Hash, ForkId, ForkVersion });
}
return content;
}
bool isHashEquals(void* hash1, void* hash2)
{
/*void* alloced_addr = malloc(HASH_SIZE + 1);
memcpy(alloced_addr, hash1, HASH_SIZE);
char* s_hash1 = (char*)alloced_addr;
s_hash1[HASH_SIZE] = '0';
alloced_addr = malloc(HASH_SIZE + 1);
memcpy(alloced_addr, hash2, HASH_SIZE);
char* s_hash2 = (char*)alloced_addr;
s_hash2[HASH_SIZE] = '0';
bool bCan = std::strcmp(s_hash1, s_hash2) ? false : true;
free(s_hash1);
free(s_hash2);
return bCan;*/
return memcmp(hash1, hash2, HASH_SIZE) == 0;
}
std::unordered_map<uint32_t, PathInfo> FetchManifest(uint32_t versionId)
{
Statement data(contentDB, "SELECT * FROM ContentManifest WHERE (VersionId = ?)");
data.bind(1, versionId);
std::unordered_map<uint32_t, PathInfo> manifest;
while (data.executeStep())
{
versionId = data.getColumn(1).getInt();
std::string path = data.getColumn(2).getString();
uint32_t contentId = data.getColumn(3).getInt();
manifest.insert({ contentId, { contentId, versionId, path } });
}
return manifest;
}
ContentData GetContent(uint32_t id)
{
Statement data(contentDB, "SELECT * FROM Content WHERE (Id = ?)");
data.bind(1, id);
data.executeStep();
void* blob = malloc(data.getColumn(4).getBytes());
memcpy(blob, data.getColumn(4).getBlob(), data.getColumn(4).getBytes());
return ContentData(data.getColumn(4).getBytes(), data.getColumn(2).getUInt(), data.getColumn(3).getUInt(), blob);
}
ContentVersion* GetFork(int forkId)
{
for (auto& v : Forks)
{
if (v.Id == forkId)
return &v;
}
return nullptr;
}
bool IsForkExisting(int forkId)
{
if (GetFork(forkId) != nullptr)
return true;
return false;
}
void RemoveLastConsoleLine(uint32_t completeXRRsymb)
{
std::cout << '\r' << std::string(completeXRRsymb, '\xff');
}
void UnpackFiles(std::unordered_map<uint32_t, PathInfo>& manifest, const std::string output_dir)
{
for (auto path : manifest)
{
auto content = GetContent(path.first);
//printf("%s", (std::string(abstract_path) + '/' + std::string(path.second.Path)).c_str());
std::string fullpath = output_dir + path.second.Path;
fs::path pathObj(fullpath);
fs::create_directories(pathObj.parent_path());
FILE* file = fopen(fullpath.c_str(), "wb");
//printf("Proccessing %p\n", file);
if (file == nullptr)
{
printf("Error path: %s\n", fullpath.c_str());
return;
}
void* data = content.Data;
void* decompressedData;
bool proccessAsCompressed = false;
uint32_t size = content.CompressedSize;
if (content.Compression == 2)
{
decompressedData = malloc(content.DecompressedSize);
ZSTD_decompress(decompressedData, content.DecompressedSize, data, content.CompressedSize);
fwrite(decompressedData, 1, content.DecompressedSize, file);
free(decompressedData);
}
else
fwrite(data, 1, size, file);
fclose(file);
delete data;
mtx.lock();
CurrentIndexFile++;
LastTrackedSymbol = path.second.Path;
mtx.unlock();
}
isThreadStopped = true;
}
int main(int argc, char** argv)
{
GetConsoleScreenBufferInfo(CONSOLE, &consoleInfo);
saved_attributes = consoleInfo.wAttributes;
try
{
Forks = FetchForks();
}
catch (const std::exception&)
{
colored_print("Unavailable to fetch forks. Abort!\n", COLOR_RED);
return 1;
}
if (argc < 2)
{
colored_print("[ - ] AWEDePackerRobust\n", COLOR_RED);
colored_print("[ - ] Developed by Alan Wake 2/14/2024\n");
colored_print("[ - ] Select build id to unpack:\n");
for (auto& t : Forks)
{
printf("-\tId: %d\tHash: %s %d %s\tForkId: %s\tForkVersion: %s\nOutput folder: %s\n",
t.Id, "BLOB", HASH_SIZE, "Bytes",
t.ForkId.c_str(),
t.ForkVersion.c_str(),
t.CreateName().c_str());
}
return 0;
}
if (argc == 2)
{
int selectedFork = std::atoi(argv[1]);
printf("Processing forkId %d...\n", selectedFork);
if (!IsForkExisting(selectedFork))
{
colored_print("[ - ] There are no fork with id \"", COLOR_RED);
colored_print(selectedFork, COLOR_RED);
colored_print("\"\n", COLOR_RED);
return 1;
}
colored_print("[ - ] Found forkId \"");
colored_print(selectedFork);
colored_print("\"\n");
printf("Fetching manifest...\n");
try
{
Manifest = FetchManifest(selectedFork);
}
catch (const std::exception&)
{
colored_print("There was an exception while fetching the manifest. Abort!\n", COLOR_RED);
return 1;
}
colored_print("[ - ] Fetch manifest has been finished\n");
colored_print("[ - ] Start unpacking build files\n");
ContentVersion* currentFork = GetFork(selectedFork);
const std::string result_path(std::string(getenv("LOCALAPPDATA"))
+ '\\'
+ "AWEDePackerRobust\\"
+ currentFork->CreateName()
+ "\\");
std::thread unpack([result_path]() {UnpackFiles(Manifest, result_path); });
unpack.detach();
CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo;
GetConsoleScreenBufferInfo(CONSOLE, &screenBufferInfo);
std::string tracked;
std::string _tracked;
std::string completeString;
uint32_t elements = Manifest.size();
char c[256];
while (!isThreadStopped)
{
Sleep(50);
mtx.lock();
_tracked = LastTrackedSymbol;
mtx.unlock();
if (tracked != _tracked)
{
RemoveLastConsoleLine(completeString.length());
sprintf(c, "\r[%d / %d]: %s", CurrentIndexFile, elements, tracked.c_str());
completeString = c;
tracked = _tracked;
printf(c);
}
}
colored_print("\n\n[ - ] End unpacking build files\n");
colored_print("[ - ] Check directory: ");
printf(result_path.c_str());
colored_print("\n");
return 0;
}
//delete contentDB;
}