-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
292 lines (229 loc) · 10.9 KB
/
main.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
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <cstdio>
#include <GLFW/glfw3.h>
#include "Gui.h"
#define STB_IMAGE_IMPLEMENTATION
#include "External/stb_image.h"
static void glfw_error_callback(int error, const char* description)
{
fprintf(stderr, "GLFW Error %d: %s\n", error, description);
}
ResPacker::Gui gui;
vector<char> mainFont;
vector<char> iconFont;
PakTypes::PakFile resFile = Unpacker::ParsePakFile("res.pak");
string resPassword = "res_packer_gui";
void drop_callback(GLFWwindow* window, int num_files, const char** paths);
void window_size_callback(GLFWwindow* window, int width, int height);
void window_content_scale_callback(GLFWwindow* window, float xscale, float yscale);
void window_focus_callback(GLFWwindow* window, int focused);
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
// Main code
int main(int, char**)
{
const int window_width = 1280;
const int window_height = 720;
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit())
return 1;
GLFWmonitor* primary_monitor = glfwGetPrimaryMonitor();
// GL 3.0 + GLSL 130
const char* glsl_version = "#version 330";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
// Create window with graphics context
gui.window = glfwCreateWindow(window_width, window_height, "Resource Packer", nullptr, nullptr);
if (gui.window == nullptr)
return 1;
glfwMakeContextCurrent(gui.window);
glfwSwapInterval(1); // Enable vsync
glfwSetDropCallback(gui.window, drop_callback);
glfwSetWindowSizeCallback(gui.window, window_size_callback);
glfwSetWindowContentScaleCallback(gui.window, window_content_scale_callback);
glfwSetWindowFocusCallback(gui.window, window_focus_callback);
int windowX, windowY;
glfwGetWindowSize(gui.window, &windowX, &windowY);
gui.Setup(windowX, windowY);
int work_posx, work_posy, work_width, work_height;
glfwGetMonitorWorkarea(primary_monitor, &work_posx, &work_posy, &work_width, &work_height);
const int window_posx = (work_width / 2) - windowX / 2;
const int window_posy = (work_height / 2) - windowY / 2;
glfwSetWindowPos(gui.window, window_posx, window_posy);
// Setup Dear ImGui context
// IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
std::string iniPath = Utils::GetCurrentWorkingDirectory() + "/imgui.ini";
io.IniFilename = iniPath.c_str();
// Setup Dear ImGui style
Theme::SetupImGuiStyle();
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(gui.window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
float scaleX, scaleY;
glfwGetWindowContentScale(gui.window, &scaleX, &scaleY);
gui.scale = scaleX;
// Load Fonts
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
// - Read 'docs/FONTS.md' for more instructions and details.
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
// - Our Emscripten build process allows embedding fonts to be accessible at runtime from the "fonts/" folder. See Makefile.emscripten for details.
//io.Fonts->AddFontDefault();
//io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr, io.Fonts->GetGlyphRangesJapanese());
//IM_ASSERT(font != nullptr);
// io.Fonts->AddFontFromFileTTF("Roboto-Medium.ttf", 16.0f * scaleX);
gui.unpacker.setPassword(resPassword);
size_t encryptionOpsLimit = gui.unpacker.getEncryptionOpsLimit();
size_t encryptionMemLimit = gui.unpacker.getEncryptionMemLimit();
gui.unpacker.setEncryptionOpsLimit(crypto_pwhash_OPSLIMIT_MIN);
gui.unpacker.setEncryptionMemLimit(crypto_pwhash_MEMLIMIT_MIN);
iconFont = gui.unpacker.ExtractFileToMemory(resFile, "file1");
mainFont = gui.unpacker.ExtractFileToMemory(resFile, "file2");
auto icon = gui.unpacker.ExtractFileToMemory(resFile, "file3");
gui.unpacker.setEncryptionOpsLimit(encryptionOpsLimit);
gui.unpacker.setEncryptionMemLimit(encryptionMemLimit);
{
int imageWidth, imageHeight, imageChannels;
unsigned char* imagePixels = stbi_load_from_memory(reinterpret_cast<const stbi_uc*>(&icon[0]), icon.size(), &imageWidth, &imageHeight, &imageChannels, STBI_rgb_alpha);
GLFWimage appIcon(imageWidth, imageHeight, imagePixels);
glfwSetWindowIcon(gui.window, 1, &appIcon);
GLuint image_texture;
glGenTextures(1, &image_texture);
glBindTexture(GL_TEXTURE_2D, image_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
#if defined(GL_UNPACK_ROW_LENGTH)
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
#endif
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imagePixels);
stbi_image_free(imagePixels);
gui.iconTexture = image_texture;
gui.iconHeight = imageHeight;
gui.iconWidth = imageWidth;
}
float baseFontSize = 16.0f;
io.Fonts->AddFontFromMemoryTTF(mainFont.data(), mainFont.size(), baseFontSize * scaleX);
float iconFontSize = baseFontSize * 2.0f / 3.0f;
static const ImWchar icons_ranges[] = {ICON_MIN_FA, ICON_MAX_16_FA, 0};
ImFontConfig icons_config;
icons_config.MergeMode = true;
icons_config.PixelSnapH = true;
icons_config.GlyphMinAdvanceX = iconFontSize;
io.Fonts->AddFontFromMemoryTTF(iconFont.data(), iconFont.size(), baseFontSize * scaleX, &icons_config, icons_ranges);
gui.font2 = io.Fonts->AddFontFromMemoryTTF(mainFont.data(), mainFont.size(), 28.0f * scaleX);
string SaveFileName;
// Our state
#ifdef IMGUI_DEMO
bool show_demo_window = false;
#endif
ImVec4 clear_color = ImVec4(0.156f, 0.180f, 0.219, 1.0f);
// Main loop
while (!glfwWindowShouldClose(gui.window)) {
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
glfwPollEvents();
if (gui.renderingPaused) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
continue;
}
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
#ifdef IMGUI_DEMO
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
#endif
{
gui.RenderPlaceholder();
gui.RenderMainMenu();
gui.RenderAboutWindow();
gui.RenderPackingProgressWindow();
gui.RenderPackingCompleteWindow();
gui.RenderSettingsWindow();
gui.RenderFileWindow();
gui.RenderEditPackedPathWindow();
gui.RenderExtractWindow();
gui.RenderHeaderGenerationWindow();
gui.RenderUnpackingCompleteWindow();
}
// Rendering
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(gui.window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
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);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(gui.window);
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
// ImGui::DestroyContext();
glfwDestroyWindow(gui.window);
glfwTerminate();
return 0;
}
void drop_callback(GLFWwindow* window, int num_files, const char** paths) {
if (num_files == 1) {
if (Paths::GetFileExtension(paths[0]) == ".pak") {
gui.showFileWindow = false;
try {
gui.pakFile = Unpacker::ParsePakFile(paths[0]);
gui.showExtractWindow = true;
}
catch (const exception &e) {
MessageBoxA(nullptr, e.what(), "Error", MB_OK | MB_ICONERROR);
}
return;
}
}
for (int i = 0; i < num_files; i++) {
vector<string> tFiles;
Paths::getFiles(paths[i], tFiles);
for (auto &tFile: tFiles) {
string friendlyPath = Paths::StripPath(tFile, Paths::GetDirectory(paths[0]));
Paths::ReplaceSlashes(friendlyPath);
PakTypes::PakFileItem file = {
.name = Paths::GetFileName(tFile),
.path = tFile,
.packedPath = friendlyPath,
.size = Paths::GetFileSize(tFile),
.compressed = false
};
gui.files.emplace_back(file);
}
}
gui.showFileWindow = true;
}
void window_size_callback(GLFWwindow* window, int width, int height) {
gui.windowSize = ImVec2(width, height);
}
void window_content_scale_callback(GLFWwindow* window, float xscale, float yscale) {
gui.scale = xscale;
}
void window_focus_callback(GLFWwindow* window, int focused) {
if (focused == GLFW_FALSE) {
gui.renderingPaused = true;
} else {
gui.renderingPaused = false;
}
}