-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
604 lines (501 loc) · 22.5 KB
/
Copy pathmain.cpp
File metadata and controls
604 lines (501 loc) · 22.5 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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#include <iostream>
#include <string>
#include <filesystem>
#include <optional>
#define IMGUI_DEFINE_MATH_OPERATORS
#include "imgui.h"
#include "imgui_stdlib.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "ImGuiFileDialog.h"
#include "imutils/fonts.hpp"
#include "NotoSansSC-Regular.ttf.h"
#include "BS_thread_pool.hpp"
#include "imgui_internal.h"
#include "bin2file/Bin2C.hpp"
#include <GLFW/glfw3.h>
using namespace BS;
namespace fs = std::filesystem;
inline constexpr int WINDOW_SIZE_OFFSET = 2;
inline constexpr int WINDOW_POS_OFFSET = -(WINDOW_SIZE_OFFSET / 2);
inline constexpr ImVec2 APP_WINDOW_DEFAULT_SIZE(1000, 750);
bool initImGui = false;
thread_pool g_BackgroundTask(2);
static std::stringstream g_OSS;
class{}* g_App = nullptr;
static void glfw_error_callback(const int error, const char* description) {
std::cerr << "GLFW error " << error << " " << description << std::endl;
}
static ImVec4 g_BackgroundColor(0.45f, 0.55f, 0.60f, 1.00f);
static float g_Scale;
GLFWmonitor* g_PrimaryMonitor;
GLFWwindow* g_Window;
using env = std::optional<std::string>;
env GetENV(const std::string& key) {
using std::nullopt;
std::string result;
#ifdef _MSC_VER
char* value = nullptr;
if (_dupenv_s(&value, nullptr, key.c_str()) == 0 && value) {
result = value;
free(value);
} else return nullopt;
#else
char* value = getenv(key.c_str());
if (value) result = value;
else return nullopt;
#endif
return result;
}
enum 屏幕方向 : bool { 横屏, 竖屏 };
inline 屏幕方向 获取屏幕方向(const ImVec2i& 屏幕尺寸) {
return 屏幕尺寸.x > 屏幕尺寸.y ? 横屏 : 竖屏;
}
inline ImVec2 ToImVec2(const ImVec2i& o) {
return { static_cast<float>(o.x), static_cast<float>(o.y )};
}
inline ImVec2i ToImVec2i(const ImVec2& o) {
return { static_cast<int>(o.x), static_cast<int>(o.y) };
}
void GUI() {
static ImVec2i WindowSize;
glfwGetWindowSize(g_Window, &WindowSize.x, &WindowSize.y);
if (!initImGui) {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
ImGui::StyleColorsLight();
ImGuiStyle& style = ImGui::GetStyle();
style.ScaleAllSizes(g_Scale);
style.FontScaleDpi = g_Scale;
io.Fonts->AddFontFromMemoryTTF(
NotoSansSC_Regular_ttf,
// NOLINTNEXTLINE(*-narrowing-conversions)
NotoSansSC_Regular_ttf_len,
21.0f,
nullptr,
ImUtils::Glyph::GetGlyphRangesChineseFull() +
ImUtils::Glyph::GetGlyphRangesCyrillic() +
ImUtils::Glyph({
{0x2190, 0x21FF}, // Arrows
{0x25A0, 0x25FF}, // kaomoji graphics and geometry
{0x2600, 0x26FF}, // kaomoji miscellaneous
{0x2700, 0x27BF}, // kaomoji dingbats
{0x3400, 0x4DBF}, // 生僻字
})
);
io.IniFilename = nullptr;
ImGui_ImplGlfw_InitForOpenGL(g_Window, true);
ImGui_ImplOpenGL3_Init("#version 300es");
io.Fonts->Build();
initImGui = true;
}
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::SetNextWindowPos(ImVec2(WINDOW_POS_OFFSET, WINDOW_POS_OFFSET));
ImGui::SetNextWindowSize(
ImVec2(
static_cast<float>(WindowSize.x + WINDOW_SIZE_OFFSET),
static_cast<float>(WindowSize.y + WINDOW_SIZE_OFFSET)
)
);
static constexpr auto MAIN_WINDOW_FLAG =
ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_NoScrollWithMouse;
static const auto BottomText = "By 江芷酱紫. ";
static const auto TelegramChannel = "@ZCYMOD";
static const std::string HOME_DIR =
#ifdef _WIN32
GetENV("USERPROFILE").value_or("C:\\\\")
#elifdef __ANDROID__
"/sdcard"
#elifdef __unix__ || __linux__
std::getenv("HOME")
#else
"."
#endif
; // 语句ended
static std::string inputFile = fs::absolute(HOME_DIR).string();
static std::string outputFile = (fs::path(inputFile) / fs::path("Bin2C_Output")).string();
[[maybe_unused]] static bool _init_once_1 = []() {
fs::create_directory(outputFile);
if (!fs::exists(outputFile) || !fs::is_directory(outputFile))
outputFile = inputFile;
return false;
}();
const fs::path inputFS = inputFile;
const fs::path outputFS = outputFile;
static Bin2::Bin inputBin;
static std::future<Bin2::Res> outputFuture;
auto IntImVec2 = [](const int x, const int y) {
return ImVec2(
static_cast<float>(x),
static_cast<float>(y)
);
};
[[maybe_unused]] auto IntImVec4 = [](const int x, const int y, const int z, const int w) {
return ImVec4(
static_cast<float>(x),
static_cast<float>(y),
static_cast<float>(z),
static_cast<float>(w)
);
};
auto SeparatorTitle = [](const char* label, const float size = 1.0f) {
ImGui::SetWindowFontScale(size);
ImGui::SeparatorText(std::forward<const char*>(label));
ImGui::Spacing();
ImGui::SetWindowFontScale(1.0f);
};
const ImVec2 ChooseWindowSize = IntImVec2(WindowSize.x / 3 * 2, WindowSize.y / 3 * 2);
auto ChooseFile = [&ChooseWindowSize](std::string& filename, const char* label = "...", ImVec2 size = {0, 0}) {
if (size.x <= 0 || size.y <= 0)
size = ChooseWindowSize;
ImGui::SetWindowSize("选择文件...##内置选择文件", size);
if (ImGui::Button(label)) {
static IGFD::FileDialogConfig cfg;
if (fs::is_regular_file(filename))
cfg.path = fs::path(filename).parent_path().string();
else cfg.path = filename;
cfg.countSelectionMax = 1;
cfg.flags = ImGuiFileDialogFlags_Modal | ImGuiFileDialogFlags_NaturalSorting;
ImGuiFileDialog::Instance()->OpenDialog(
"内置选择文件",
"选择文件...",
".*",
cfg
);
}
if (ImGuiFileDialog::Instance()->Display("内置选择文件", ImGuiWindowFlags_NoResize)) {
if (ImGuiFileDialog::Instance()->IsOk()) filename = ImGuiFileDialog::Instance()->GetFilePathName();
ImGuiFileDialog::Instance()->Close();
}
};
auto ChoosePath = [&ChooseWindowSize](std::string& path, const char* label = "...", ImVec2 size = {0, 0}) {
if (size.x <= 0 || size.y <= 0)
size = ChooseWindowSize;
ImGui::SetWindowSize("选择目录...##内置选择目录", size);
if (ImGui::Button(label)) {
static IGFD::FileDialogConfig cfg;
cfg.path = path;
cfg.countSelectionMax = 1;
cfg.flags = ImGuiFileDialogFlags_Modal | ImGuiFileDialogFlags_NaturalSorting;
ImGuiFileDialog::Instance()->OpenDialog(
"内置选择目录",
"选择目录...",
nullptr,
cfg
);
}
if (ImGuiFileDialog::Instance()->Display("内置选择目录", ImGuiWindowFlags_NoResize)) {
if (ImGuiFileDialog::Instance()->IsOk()) path = ImGuiFileDialog::Instance()->GetCurrentPath();
ImGuiFileDialog::Instance()->Close();
}
};
if (ImGui::Begin("##Main", nullptr, MAIN_WINDOW_FLAG)) {
if (ImGui::BeginChild("###I/O###", ImVec2(0, ImGui::GetContentRegionAvail().y * 0.6f))) {
if (ImGui::BeginChild("##Input", ImVec2(ImGui::GetContentRegionAvail().x / 2 - 1.0f, 0), true)) {
ImGui::PushStyleVar(ImGuiStyleVar_SeparatorTextAlign, ImVec2(0.0f, 0.5f));
SeparatorTitle(" Input 输入", 1.75f);
ImGui::PopStyleVar();
ImGui::TextUnformatted("输入文件:");
// ImGui::TextUnformatted("临时测试 ☆*: .。. o(≧▽≦)o .。.:*☆ 한국어 Русский にほんご ");
ImGui::InputText("##输入", &inputFile);
ImGui::SameLine();
ChooseFile(inputFile);
ImGui::Spacing();
if (ImGui::BeginChild("###File", ImVec2(), true)) {
auto FileSize = [](const fs::path& ifs) {
std::uintmax_t bitSize = 0ULL;
if (!fs::is_regular_file(ifs))
return std::string("-");
bitSize = std::filesystem::file_size(ifs);
const std::vector<std::string> units = {"B", "KB", "MB", "GB", "TB"};
int unit_index = 0;
auto size = static_cast<double>(bitSize);
while (size >= 1024.0 && unit_index < 4) {
size /= 1024.0;
unit_index++;
}
return std::format("{:.2f} {:s}", size, units[unit_index]);
};
ImGui::PushStyleVar(ImGuiStyleVar_SeparatorTextAlign, ImVec2(0.5f, 0.5f));
if (fs::is_regular_file(inputFS)) {
ImGui::SeparatorText("文件信息:");
ImGui::TextWrapped("文件: %s", inputFS.has_stem() ? inputFS.filename().string().c_str() : "(空)");
ImGui::TextWrapped(
"类型: %s",
inputFS.has_extension() ? inputFS.extension().string().c_str() : "未知文件"
);
ImGui::TextWrapped("路径:%s", "");
ImGui::SameLine();
if (fs::is_regular_file(inputFS))
ImGui::TextLinkOpenURL(
inputFile.c_str(),
inputFS.has_parent_path()
? inputFS.parent_path().string().c_str()
: inputFS.string().c_str()
);
else
ImGui::TextLinkOpenURL(
inputFile.c_str(),
inputFile.c_str()
);
ImGui::TextWrapped("大小: %s", FileSize(inputFS).c_str());
} else ImGui::SeparatorText("请选择一个文件 ");
ImGui::PopStyleVar();
ImGui::EndChild();
}
ImGui::EndChild();
}
ImGui::SameLine();
if (ImGui::BeginChild("##Output", ImVec2(0, 0), true)) {
ImGui::PushStyleVar(ImGuiStyleVar_SeparatorTextAlign, ImVec2(1.0f, 0.5f));
SeparatorTitle("输出 Output ", 1.75f);
ImGui::PopStyleVar();
ImGui::TextUnformatted("输出目录/文件:");
ImGui::InputText("##输出", &outputFile);
ImGui::SameLine();
ChoosePath(outputFile);
if (ImGui::Button(
"使用推荐值",
ImVec2(ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().x > 50.0f ? 50.0f : 0)
))
outputFile = inputFS.has_parent_path() && fs::is_regular_file(inputFS)
? inputFS.parent_path().string()
: "Bin2C_Output";
if (ImGui::BeginChild("###Path/File", ImVec2(), true)) {
ImGui::PushStyleVar(ImGuiStyleVar_SeparatorTextAlign, ImVec2(0.5f, 0.5f));
if (fs::is_directory(outputFS)) {
ImGui::SeparatorText("目录信息:");
ImGui::TextWrapped("目录: %s", outputFS.filename().string().c_str());
ImGui::TextWrapped("路径:%s", "");
ImGui::SameLine();
ImGui::TextLinkOpenURL(outputFile.c_str(), outputFile.c_str());
} else if (fs::is_regular_file(outputFS)) {
ImGui::SeparatorText("文件信息:");
ImGui::TextWrapped("文件: %s", outputFS.has_stem() ? outputFS.filename().string().c_str() : "(空)");
ImGui::TextWrapped(
"类型: %s",
outputFS.has_extension() ? outputFS.extension().string().c_str() : "未知文件"
);
ImGui::TextWrapped("路径:%s", "");
ImGui::SameLine();
ImGui::TextLinkOpenURL(
inputFile.c_str(),
outputFS.has_parent_path()
? outputFS.parent_path().string().c_str()
: outputFS.string().c_str()
);
} else ImGui::SeparatorText("请选择一个文件/目录");
ImGui::PopStyleVar();
ImGui::EndChild();
}
ImGui::EndChild();
}
ImGui::EndChild();
}
if (ImGui::BeginChild(
"##Task",
ImVec2(0, ImGui::GetContentRegionAvail().y - ImGui::CalcTextSize(BottomText).y - 5.0f),
ImGuiChildFlags_Borders
)) {
ImGui::PushStyleVar(ImGuiStyleVar_SeparatorTextAlign, ImVec2(0.5f, 0.5f));
ImGui::Columns(3);
const bool isNotFile = !fs::is_regular_file(inputFS);
static bool hasErr = false;
static std::string errMsg;
if (isNotFile) {
ImGui::SeparatorText("请选择一个输入文件");
ImGui::BeginDisabled();
} else ImGui::SeparatorText("二进制信息");
if (ImGui::BeginChild("##二进制信息")) {
if (!inputBin.DeclareOnly()) {
ImGui::TextWrapped("二进制大小: %zu", inputBin.GetSize());
ImGui::TextWrapped("二进制文件: %s", inputBin.GetFile().filename().string().c_str());
}
ImGui::EndChild();
}
ImGui::NextColumn();
if (isNotFile) {
ImGui::SeparatorText("无可操作的文件");
} else ImGui::SeparatorText("二进制操作");
if (ImGui::BeginChild("##操作")) {
static Bin2C::Output::Config cfg;
try {
ImGui::Checkbox(" 美观化 ", &cfg.pretty);
ImGui::SameLine();
ImGui::Checkbox(" 常量化 ", &cfg.constant);
ImGui::SameLine();
ImGui::Checkbox(" 源输出 ", &cfg.source);
ImGui::Text("类型: ");
ImGui::SameLine();
static const char* 输出类型_选项[] = {
Bin2::Type::u8.GetName(),
Bin2::Type::u16.GetName(),
Bin2::Type::u32.GetName(),
Bin2::Type::u64.GetName()
};
static int 输出类型_SelectedItem = 0;
ImGui::Combo("##输出类型", &输出类型_SelectedItem, 输出类型_选项, IM_ARRAYSIZE(输出类型_选项));
switch (输出类型_SelectedItem) {
case 0:
cfg.flag = Bin2::Type::u8;
break;
case 1:
cfg.flag = Bin2::Type::u16;
break;
case 2:
cfg.flag = Bin2::Type::u32;
break;
case 3:
cfg.flag = Bin2::Type::u64;
break;
default:
输出类型_SelectedItem = 0;
break;
}
ImGui::BeginDisabled(cfg.source);
ImGui::Text("形式: ");
ImGui::SameLine();
static const char* 输出形式_选项[] = {
"默认 None",
"静态 static",
"内联 inline (C++17)"
};
static int 输出形式_SelectedItem = 0;
ImGui::Combo("##输出形式", &输出形式_SelectedItem, 输出形式_选项, IM_ARRAYSIZE(输出形式_选项));
if (输出形式_SelectedItem < 0 || 输出形式_SelectedItem >= IM_ARRAYSIZE(输出形式_选项))
输出形式_SelectedItem = 0;
cfg.exportType = static_cast<Bin2::ExportType>(输出形式_SelectedItem);
ImGui::EndDisabled();
ImGui::Spacing();
ImGui::Separator();
ImGui::Spacing();
const ImVec2 AContentRegionAvail(
ImGui::GetContentRegionAvail().x,
ImGui::GetContentRegionAvail().y / 2.5f
);
ImGui::BeginDisabled(inputBin.GetFile() == inputFS);
if (ImGui::Button(
std::format("加载二进制", inputFile).c_str(),
AContentRegionAvail
) && fs::exists(inputFS)) {
inputBin(inputFS);
}
ImGui::EndDisabled();
ImGui::BeginDisabled(inputBin.DeclareOnly());
if (ImGui::Button(
std::format("导出二进制", outputFile).c_str(),
AContentRegionAvail
) && fs::exists(outputFS)) {
if (inputBin.DeclareOnly()) {
hasErr = true;
errMsg = "二进制不可为空";
}
outputFuture = g_BackgroundTask.submit_task(
[&]() {
Bin2::Res result;
const Bin2C::Output functor = fs::is_regular_file(outputFS)
? Bin2C::Output(outputFS)
: Bin2C::Output(outputFS, &inputBin, cfg);
return functor(&inputBin,cfg);
}
);
}
ImGui::EndDisabled();
} catch (const std::exception& e) {
hasErr = true;
errMsg = e.what();
}
ImGui::EndChild();
}
ImGui::NextColumn();
if (ImGui::BeginChild("##输出")) {
if (outputFuture.valid()) {
g_OSS.clear();
if (auto [wasWrong, wrongMsg] = outputFuture.get(); !wasWrong)
g_OSS << "Error: " << wrongMsg << std::endl;
else g_OSS << "操作成功。" << std::endl;
}
ImGui::SeparatorText("输出");
ImGui::TextWrapped("%s", g_OSS.str().c_str());
ImGui::EndChild();
}
ImGui::Columns();
if (isNotFile) ImGui::EndDisabled();
if (hasErr) {
ImGui::OpenPopup("##WarningPopup");
if (ImGui::BeginPopupModal("##WarningPopup", &hasErr, ImGuiWindowFlags_AlwaysAutoResize)) {
SeparatorTitle("警告 Warning", 1.5f);
ImGui::Spacing();
ImGui::TextWrapped("%s", errMsg.c_str());
ImGui::Separator();
ImGui::Text("请确保输入、输出信息设置正确或者权限是否足够");
ImGui::EndPopup();
}
}
ImGui::PopStyleVar();
ImGui::EndChild();
}
const float ContentWidth = ImGui::GetContentRegionAvail().x;
ImGui::TextAligned(1.0f, ContentWidth - ImGui::CalcTextSize(TelegramChannel).x - 20.0f, "%s", BottomText);
ImGui::SameLine();
ImGui::SetCursorPosX(ContentWidth - ImGui::CalcTextSize(TelegramChannel).x);
ImGui::TextLinkOpenURL(TelegramChannel, "https://ZCYMOD.T.ME");
ImGui::End();
}
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
int main(int /*argc*/, char** argv) {
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit()) return -1;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
g_PrimaryMonitor = glfwGetPrimaryMonitor();
g_Scale = ImGui_ImplGlfw_GetContentScaleForMonitor(glfwGetPrimaryMonitor());
static const std::string AppName = fs::path(argv[0]).filename().string() + " | Bin2C-ImGui";
g_Window = glfwCreateWindow(
static_cast<int>(APP_WINDOW_DEFAULT_SIZE.x * g_Scale),
static_cast<int>(APP_WINDOW_DEFAULT_SIZE.y * g_Scale),
AppName.c_str(), nullptr, nullptr
);
if (!g_Window) return -1;
glfwMakeContextCurrent(g_Window);
glfwSwapInterval(1);
while (!glfwWindowShouldClose(g_Window)) {
glfwPollEvents();
if (glfwGetWindowAttrib(g_Window, GLFW_ICONIFIED) != 0) {
ImGui_ImplGlfw_Sleep(10);
continue;
}
static ImVec2i display;
glfwGetFramebufferSize(g_Window, &display.x, &display.y);
glViewport(0, 0, display.x, display.y);
glClearColor(
g_BackgroundColor.x * g_BackgroundColor.w,
g_BackgroundColor.y * g_BackgroundColor.w,
g_BackgroundColor.z * g_BackgroundColor.w,
g_BackgroundColor.w
);
glClear(GL_COLOR_BUFFER_BIT);
GUI();
glfwSwapBuffers(g_Window);
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
// ImGui::DestroyContext();
// FIXME: 异常: Exception 0x80000003 encountered at address 0x7ffc21f06766
glfwDestroyWindow(g_Window);
glfwTerminate();
return 0;
}