-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
487 lines (415 loc) · 16.3 KB
/
main.cpp
File metadata and controls
487 lines (415 loc) · 16.3 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
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
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <atomic>
#include <string>
#include <vector>
#include "src/parser.h"
#include "src/levels.h"
#include "src/misc_utils.h"
#include "src/vox_writer.h"
#include "glad/glad.h"
#include <GLFW/glfw3.h>
#include "imgui/imgui.h"
#include "imgui/backend/imgui_impl_glfw.h"
#define IMGUI_IMPL_OPENGL_LOADER_CUSTOM
#include "imgui/backend/imgui_impl_opengl3.h"
#include "file_dialog/ImGuiFileDialog.h"
#include "lib/tinyxml2.h"
using namespace tinyxml2;
atomic<float> progress;
void* DecompileMap(void* param) {
ConverterParams* data = (ConverterParams*)param;
create_folder(data->map_folder);
if (!data->legacy_format) {
string input_image = "preview/" + data->level_id;
if (data->dlc_id == "space")
input_image += "_dlc3";
input_image += ".png";
string output_image = data->map_folder + "preview.jpg";
SaveImageJPG(input_image, output_image);
SaveInfoTxt(data->map_folder, data->level_name, data->level_desc);
}
if (data->legacy_format) {
create_folder(data->map_folder + "custom");
copy_folder(data->script_folder, data->map_folder + "custom/script");
} else {
create_folder(data->map_folder + "vox");
copy_folder(data->script_folder + data->level_id, data->map_folder + "main");
if (!data->dlc_id.empty())
copy_folder(data->script_folder + "script", data->map_folder + "script");
}
ParseFile(*data);
return nullptr;
}
MV_Color HSVtoRGB(float h, float s, float v) {
float c = v * s;
float x = c * (1 - abs(fmod(h / 60.0f, 2) - 1));
float m = v - c;
float r_prime, g_prime, b_prime;
if (h >= 0 && h < 60) {
r_prime = c; g_prime = x; b_prime = 0;
} else if (h >= 60 && h < 120) {
r_prime = x; g_prime = c; b_prime = 0;
} else if (h >= 120 && h < 180) {
r_prime = 0; g_prime = c; b_prime = x;
} else if (h >= 180 && h < 240) {
r_prime = 0; g_prime = x; b_prime = c;
} else if (h >= 240 && h < 300) {
r_prime = x; g_prime = 0; b_prime = c;
} else {
r_prime = c; g_prime = 0; b_prime = x;
}
uint8_t r = 255.0 * (r_prime + m);
uint8_t g = 255.0 * (g_prime + m);
uint8_t b = 255.0 * (b_prime + m);
return { r, g, b, 1 };
}
vector<MV_Color> GenerateColorSpectrum(int n) {
vector<MV_Color> spectrum;
spectrum.reserve(n);
float start_hue = 300.0f;
float end_hue = 0.0f;
float saturation = 0.9f;
float value = 0.9f;
for (int i = 0; i < n; i++) {
float t = (n == 1) ? 0.0f : (float)i / (n - 1);
float hue = start_hue + t * (end_hue - start_hue);
spectrum.push_back(HSVtoRGB(hue, saturation, value));
}
return spectrum;
}
void CreateTestPalette() {
MV_FILE vox_file("palette.vox", false);
vector<MV_Color> spectrum = GenerateColorSpectrum(256);
for (int i = 1; i < 254; i++)
vox_file.SetEntry(i, spectrum[i], { Material::None, DIFFUSE, {} });
vox_file.SetEntry(SNOW_INDEX, SNOW_COLOR, SNOW_MATERIAL);
vox_file.SetEntry(HOLE_INDEX, HOLE_COLOR, HOLE_MATERIAL);
MV_Shape mvshape = { "", 0, 0, 128, Tensor3D(8 * 8, 8, 8 * 32) };
for (int i = 0; i < 255; i++) {
for (int x = 0; x < 8; x++)
for (int y = 0; y < 8; y++)
for (int z = 0; z < 8; z++)
mvshape.voxels.Set(x + 8 * (i % 8), y, z + 8 * (i / 8), i + 1);
}
vox_file.AddShape(mvshape);
vox_file.SaveModel();
}
int main(int argc, char* argv[]) {
#ifdef _WIN32
_setmaxstdio(2048);
#endif
progress.store(0.0f);
if (argc > 1) {
if (argc == 2) {
ConverterParams params;
params.bin_path = argv[1];
params.map_folder = GetFilename(argv[1]) + "/";
ParseFile(params);
SaveInfoTxt(params.map_folder, "Converted", "Converted map");
return 0;
} else {
printf("CLI Usage: %s quicksave.bin\n", argv[0]);
CreateTestPalette();
return 0;
}
}
GLFWwindow* window = InitOpenGL("Teardown Converter", 700, 600);
ImVec4 clear_color = ImVec4(0.27f, 0.57f, 0.72f, 1.00f);
ImGui::CreateContext();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init();
float scale = ImGui_ImplGlfw_GetContentScaleForWindow(window);
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
ImGuiWindowFlags dialog_flags = ImGuiWindowFlags_None;
dialog_flags |= ImGuiWindowFlags_NoResize;
ImGui::StyleColorsDark();
ImGuiStyle& style = ImGui::GetStyle();
style.FontScaleMain = scale;
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f);
style.Colors[ImGuiCol_PlotHistogram] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);
char game_folder[256] = "<game_folder>";
char mods_folder[256] = "<mods_folder>";
char quicksave_folder[256] = "<quicksave_folder>";
#ifdef _WIN32
string appdata_path = GetAppDataLocal();
string documents_path = GetMyDocuments();
snprintf(game_folder, sizeof(game_folder), "C:\\Program Files (x86)\\Steam\\steamapps\\common\\Teardown");
snprintf(mods_folder, sizeof(mods_folder), "%s\\Teardown\\mods", documents_path.c_str());
snprintf(quicksave_folder, sizeof(quicksave_folder), "%s\\Teardown", appdata_path.c_str());
//const char* quicksave_folder_legacy = "C:\\Users\\user\\Documents\\Teardown";
//const char* mods_folder_legacy = "C:\\Program Files (x86)\\Steam\\steamapps\\common\\Teardown\\create";
#elif __linux__
const char* resolvedHome = getenv("HOME");
if (resolvedHome != nullptr) {
snprintf(game_folder, sizeof(game_folder), "%s/.local/share/Steam/steamapps/common/Teardown", resolvedHome);
snprintf(mods_folder, sizeof(mods_folder), "%s/.local/share/Steam/steamapps/compatdata/1167630/pfx/drive_c/users/steamuser/My Documents/Teardown/mods", resolvedHome);
snprintf(quicksave_folder, sizeof(quicksave_folder), "%s/.local/share/Steam/steamapps/compatdata/1167630/pfx/drive_c/users/steamuser/AppData/Local/Teardown", resolvedHome);
}
#endif
XMLDocument config_file;
XMLElement* config_root = nullptr;
if (config_file.LoadFile("config.xml") == XML_SUCCESS) {
config_root = config_file.RootElement();
strcpy(quicksave_folder, config_root->FindAttribute("quicksave_folder")->Value());
strcpy(mods_folder, config_root->FindAttribute("mods_folder")->Value());
strcpy(game_folder, config_root->FindAttribute("game_folder")->Value());
}
int transform_precision = 2;
bool disable_convert = false;
bool save_as_legacy = false;
bool remove_snow = false;
bool no_voxbox = false;
bool use_tdcz = false;
int game_version = 0;
string selected_preview = "";
GLuint preview_texture = 0;
pthread_t parse_thread = 0;
ConverterParams* params = new ConverterParams();
string selected_category = "Sandbox";
vector<string> categories = { "Sandbox", "Challenges", "Hub", "Missions", "Art Vandals", "Time Campers", "Folkrace", "The Greenwash Gambit", "Relic Hunters", "Others" };
vector<LevelInfo> levels = LoadLevels(selected_category);
vector<LevelInfo>::iterator selected_level = levels.begin();
glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
{
ImGui::SetNextWindowPos(ImVec2(50, 0), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(600, 600), ImGuiCond_FirstUseEver);
ImGui::Begin("Convert TDBIN file", nullptr, dialog_flags);
ImGui::TextUnformatted("Quicksave folder:");
ImGui::SameLine();
ImGui::PushItemWidth(350 * scale);
ImGui::InputText("##qsfolder", quicksave_folder, IM_ARRAYSIZE(quicksave_folder));
ImGui::PopItemWidth();
ImGui::SameLine();
ImGui::SetNextWindowPos(ImVec2(100, 50), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(500, 500), ImGuiCond_FirstUseEver);
if (ImGui::Button("Select Folder##1"))
ImGuiFileDialog::Instance()->OpenDialog("DirDialogQF", "Select Quicksave folder", ".");
if (ImGuiFileDialog::Instance()->Display("DirDialogQF")) {
if (ImGuiFileDialog::Instance()->IsOk())
strcpy(quicksave_folder, ImGuiFileDialog::Instance()->GetCurrentPath().c_str());
ImGuiFileDialog::Instance()->Close();
}
ImGui::TextUnformatted("Mods folder: ");
ImGui::SameLine();
ImGui::PushItemWidth(350 * scale);
ImGui::InputText("##modsfolder", mods_folder, IM_ARRAYSIZE(mods_folder));
ImGui::PopItemWidth();
ImGui::SameLine();
if (ImGui::Button("Select Folder##2"))
ImGuiFileDialog::Instance()->OpenDialog("DirDialogMF", "Select Mods folder", ".");
if (ImGuiFileDialog::Instance()->Display("DirDialogMF")) {
if (ImGuiFileDialog::Instance()->IsOk())
strcpy(mods_folder, ImGuiFileDialog::Instance()->GetCurrentPath().c_str());
ImGuiFileDialog::Instance()->Close();
}
ImGui::TextUnformatted("Game folder: ");
ImGui::SameLine();
ImGui::PushItemWidth(350 * scale);
ImGui::InputText("##gamefolder", game_folder, IM_ARRAYSIZE(game_folder));
ImGui::PopItemWidth();
ImGui::SameLine();
if (ImGui::Button("Select Folder##3"))
ImGuiFileDialog::Instance()->OpenDialog("DirDialogGF", "Select Game folder", ".");
if (ImGuiFileDialog::Instance()->Display("DirDialogGF")) {
if (ImGuiFileDialog::Instance()->IsOk())
strcpy(game_folder, ImGuiFileDialog::Instance()->GetCurrentPath().c_str());
ImGuiFileDialog::Instance()->Close();
}
ImGui::Dummy(ImVec2(0, 5));
ImGui::TextUnformatted("Filter maps: ");
ImGui::SameLine();
ImGui::PushItemWidth(350 * scale);
if (ImGui::BeginCombo("##combo", selected_category.c_str())) {
for (vector<string>::iterator it = categories.begin(); it != categories.end(); it++) {
bool is_selected = selected_category == *it;
if (ImGui::Selectable(it->c_str(), is_selected)) {
selected_category = *it;
levels = LoadLevels(selected_category);
selected_level = levels.begin();
}
if (is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
ImGui::PopItemWidth();
ImGui::SameLine();
ImGui::PushItemWidth(80 * scale);
ImGui::Combo("##gameversion", &game_version, " 1.7.0\0 1.6.3\0 1.6.0\0 1.5.4\0");
ImGui::PopItemWidth();
ImGui::Spacing();
ImGui::SameLine(64 * scale);
ImGui::TextUnformatted("File Name");
ImGui::SameLine(364 * scale);
ImGui::TextUnformatted("Level Name");
if (ImGui::BeginListBox("##listbox", ImVec2(-FLT_MIN, 10 * ImGui::GetTextLineHeightWithSpacing() + 5 * scale))) {
for (vector<LevelInfo>::iterator it = levels.begin(); it != levels.end(); it++) {
bool is_selected = selected_level == it;
if (ImGui::Selectable(it->filename.c_str(), is_selected) && !disable_convert) {
selected_level = it;
}
ImGui::SameLine(300 * scale);
if (it->title.empty())
ImGui::TextUnformatted(it->level_id.c_str());
else
ImGui::TextUnformatted(it->title.c_str());
if (is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndListBox();
}
ImGui::Dummy(ImVec2(0, 10 * scale));
ImGui::Text("Selected Level:");
ImGui::SameLine();
ImGui::TextUnformatted(selected_level->title.c_str());
ImGui::BeginChild("LevelDesc", ImVec2(0, 50 * scale), ImGuiChildFlags_Border);
ImGui::TextWrapped("%s", selected_level->description.c_str());
ImGui::EndChild();
ImGui::Dummy(ImVec2(0, 10 * scale));
// Avoid conflicts with file names
string preview_name = selected_level->level_id;
if (selected_category == "The Greenwash Gambit")
preview_name = preview_name + "_dlc3";
if (selected_preview != preview_name) {
selected_preview = preview_name;
string texture_path = "preview/" + selected_preview + ".png";
preview_texture = LoadTexture(texture_path.c_str());
}
if (preview_texture != 0)
ImGui::Image((void*)(uintptr_t)preview_texture, ImVec2(175 * scale, 100 * scale));
else
ImGui::Dummy(ImVec2(175 * scale, 100 * scale));
ImGui::SameLine();
ImGui::BeginGroup();
ImGui::Checkbox("Remove snow", &remove_snow);
ImGui::Checkbox("Legacy format", &save_as_legacy);
ImGui::Checkbox("Do not use voxboxes", &no_voxbox);
ImGui::Checkbox("Compress .vox files (very slow)", &use_tdcz);
ImGui::EndGroup();
ImGui::SameLine();
ImGui::BeginGroup();
ImGui::TextUnformatted("Transform settings:");
ImGui::TextUnformatted("Decimal digits");
ImGui::PushItemWidth(150 * scale);
ImGui::SliderInt("##precision", &transform_precision, 0, 10);
ImGui::EndGroup();
ImGui::Dummy(ImVec2(0, 5 * scale));
if (disable_convert && progress >= 1) {
progress = 1;
disable_convert = false;
}
const ImU32 col = ImGui::GetColorU32(ImGuiCol_ButtonHovered);
const ImU32 bg = ImGui::GetColorU32(ImGuiCol_Button);
if (disable_convert)
ImGui::BufferingBar("##buffer_bar", progress, ImVec2(600 * scale, 8 * scale), bg, col);
ImGui::Dummy(ImVec2(0, 10 * scale));
bool disabled = disable_convert;
if (disabled)
ImGui::BeginDisabled();
ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor::HSV(0.3f, 0.6f, 0.6f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, (ImVec4)ImColor::HSV(0.3f, 0.7f, 0.7f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV(0.3f, 0.8f, 0.8f));
ImGui::Spacing();
ImGui::SameLine(ImGui::GetWindowSize().x / 2 - 72 * scale);
if (ImGui::Button("CONVERT", ImVec2(72 * scale, 32 * scale))) {
progress = 0;
disable_convert = true;
params->level_id = selected_level->level_id;
params->level_name = selected_level->title;
params->level_desc = selected_level->description;
if (selected_category == "Art Vandals")
params->dlc_id = "artvandals";
else if (selected_category == "Time Campers")
params->dlc_id = "wildwestheist";
else if (selected_category == "Folkrace")
params->dlc_id = "folkrace";
else if (selected_category == "The Greenwash Gambit")
params->dlc_id = "space";
else if (selected_category == "Relic Hunters")
params->dlc_id = "relichunters";
if (!params->dlc_id.empty()) {
params->script_folder = game_folder;
params->script_folder += "/dlcs/" + params->dlc_id + "/";
} else {
params->script_folder = game_folder;
params->script_folder += "/data/level/";
}
if (selected_level->filename == "quicksave") {
params->bin_path = quicksave_folder;
params->bin_path += "/";
} else if (!params->dlc_id.empty()) {
params->bin_path = game_folder;
params->bin_path += "/dlcs/" + params->dlc_id + "/bin/";
} else {
params->bin_path = game_folder;
params->bin_path += "/data/bin/";
}
params->bin_path += selected_level->filename;
string tdbin = params->bin_path + ".tdbin";
FILE* already_decompressed = fopen(tdbin.c_str(), "rb");
if (already_decompressed != nullptr) {
printf("A decompressed file was found for the current level.\n");
params->bin_path += ".tdbin";
fclose(already_decompressed);
} else
params->bin_path += ".bin";
params->map_folder = mods_folder;
params->map_folder += "/";
if (!save_as_legacy)
params->map_folder += selected_level->filename + "/";
params->use_voxbox = !no_voxbox;
params->remove_snow = remove_snow;
params->compress_vox = use_tdcz;
params->legacy_format = save_as_legacy;
params->transform_precision = transform_precision;
pthread_create(&parse_thread, nullptr, DecompileMap, params);
}
ImGui::PopStyleColor(3);
ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor::HSV(0.0f, 0.7f, 0.7f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, (ImVec4)ImColor::HSV(0.0f, 0.8f, 0.8f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV(0.0f, 0.9f, 0.9f));
ImGui::SameLine(ImGui::GetWindowSize().x / 2 + 36 * scale);
if (ImGui::Button("Close", ImVec2(72 * scale, 32 * scale)))
glfwSetWindowShouldClose(window, true);
ImGui::PopStyleColor(3);
if (disabled)
ImGui::EndDisabled();
ImGui::End();
}
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
pthread_join(parse_thread, nullptr);
delete params;
if (config_root == nullptr) {
config_root = config_file.NewElement("config");
config_file.InsertFirstChild(config_root);
}
config_root->SetAttribute("game_folder", game_folder);
config_root->SetAttribute("mods_folder", mods_folder);
config_root->SetAttribute("quicksave_folder", quicksave_folder);
config_file.SaveFile("config.xml");
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}