-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
330 lines (281 loc) · 10.5 KB
/
main.cpp
File metadata and controls
330 lines (281 loc) · 10.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
#define IMGUI_IMPL_OPENGL_LOADER_GLAD
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <cstdint>
#include <format>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
struct Entry {
uint16_t id;
uint16_t category;
};
struct Section {
std::vector<Entry> entries;
};
constexpr size_t kSectionCount{ 30 };
const std::vector<std::string> kSectionNames = {
"Fighting Stances",
"Taunts",
"Finishers",
"Standing Front Grapples",
"Standing Front Grapples Groggy",
"Standing Strikes",
"Standing Back Grapples",
"Ground Upper Grapples",
"Ground Lower Grapples",
"Ground Strikes",
"Rope Down Grapples",
"Turnbuckle Upper Front Grapples",
"Turnbuckle Upper Back Grapples",
"Turnbuckle Lower Grapples",
"Turnbuckle Lower Running Strikes",
"Aerial Strikes Stand",
"Aerial Strikes Down",
"Jump Down Over Strikes",
"Jump Off Rope Strikes",
"Running Front Grapples",
"Running Strikes",
"Running Back Grapples",
"Running Counter Grapples",
"Double Team Moves",
"Favorites",
"Winning Moves",
"Ring In Moves",
"Ring Out Moves",
"Combo Strikes Basic",
"Combo Strikes Final"
};
// 9999.dat has unknown data at the end, we preserve it as is
std::vector<char> trailingData;
static std::vector<Section> LoadSections(const std::string& path) {
std::ifstream file(path, std::ios::binary);
std::vector<Section> sections;
trailingData.clear();
if (!file) return sections;
uint32_t sectionCount{ 0 };
while (file && sectionCount < kSectionCount) {
uint32_t count{ 0 };
file.read(reinterpret_cast<char*>(&count), sizeof(count));
if (!file) break;
Section section;
for (uint32_t i = 0; i < count; ++i) {
Entry e{ 0,0 };
file.read(reinterpret_cast<char*>(&e.id), sizeof(e.id));
file.read(reinterpret_cast<char*>(&e.category), sizeof(e.category));
if (!file) break;
section.entries.push_back(e);
}
if (!section.entries.empty()) {
sections.push_back(section);
++sectionCount;
}
}
// Read the rest of the file into trailingData
if (file) {
std::streampos currentPos = file.tellg();
file.seekg(0, std::ios::end);
std::streampos endPos = file.tellg();
std::streamsize size = endPos - currentPos;
if (size > 0) {
trailingData.resize(static_cast<size_t>(size));
file.seekg(currentPos);
file.read(trailingData.data(), size);
}
}
return sections;
}
static void SaveSections(const std::string& path, const std::vector<Section>& sections) {
std::ofstream file(path, std::ios::binary | std::ios::trunc);
if (!file) {
std::cerr << "Failed to open file for writing\n";
return;
}
for (size_t i = 0; i < sections.size(); ++i) {
const auto& section = sections[i];
uint32_t count = static_cast<uint32_t>(section.entries.size());
file.write(reinterpret_cast<const char*>(&count), sizeof(count));
for (const auto& entry : section.entries) {
file.write(reinterpret_cast<const char*>(&entry.id), sizeof(entry.id));
file.write(reinterpret_cast<const char*>(&entry.category), sizeof(entry.category));
}
// Section 26 has 12 extra 00 bytes for some reason
if (i == 25) {
constexpr uint8_t kExtraBytes[12] = { 00 };
file.write(reinterpret_cast<const char*>(kExtraBytes), sizeof(kExtraBytes));
}
}
// Append unmodified trailing data
if (!trailingData.empty()) {
file.write(trailingData.data(), trailingData.size());
}
}
int main() {
// Init GLFW
if (!glfwInit()) return -1;
// Create window
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
GLFWwindow* window = glfwCreateWindow(800, 800, "SD2 9999 Editor", nullptr, nullptr);
if (!window) return -1;
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
// Init GLAD
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cerr << "Failed to initialize GLAD\n";
return -1;
}
// Init ImGui
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330");
// Custom font
ImFontConfig font_config;
ImFont* customFont = io.Fonts->AddFontFromFileTTF(
"font/Inconsolata-Regular.ttf",
24.0f,
&font_config
);
if (!customFont) {
std::cerr << "Failed to load font\n";
}
// File to load, section data and default selection
static std::string filePath = "9999.dat";
static std::vector<Section> sections = LoadSections(filePath);
static int selectedSection = -1;
// Main loop
while (!glfwWindowShouldClose(window)) {
glfwWaitEvents();
// ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// ImGui window position and size
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
ImGui::SetNextWindowPos(ImVec2(0, 0));
ImGui::SetNextWindowSize(ImVec2((float)display_w, (float)display_h));
// Section window
ImGui::Begin("Sections", nullptr,
ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove);
ImGui::Indent(5.0f);
// Section list
for (size_t i = 0; i < sections.size(); ++i) {
std::string label = std::format("{:02}", i + 1);
if (i < kSectionNames.size()) {
label += " " + kSectionNames[i];
}
// Select/deselect section
if (ImGui::Selectable(label.c_str(), selectedSection == static_cast<int>(i))) {
if (selectedSection == static_cast<int>(i)) {
selectedSection = -1;
}
else {
selectedSection = static_cast<int>(i);
ImGui::SetScrollY(0.0f); // Scroll up to avoid visual issues
}
}
// Show entries for selected section
if (selectedSection == static_cast<int>(i)) {
auto& entries = sections[i].entries;
// Moves table
ImGui::Indent();
ImGui::PushStyleVar(ImGuiStyleVar_CellPadding, ImVec2(6.0f, 4.0f));
if (ImGui::BeginTable("Moves Table", 5))
{
// Table header
ImGui::TableSetupColumn("Move", ImGuiTableColumnFlags_WidthFixed, 50.0f);
ImGui::TableSetupColumn("ID", ImGuiTableColumnFlags_WidthFixed, 55.0f);
ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_WidthFixed, 55.0f);
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 66.0f);
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 100.0f);
ImGui::PushStyleColor(ImGuiCol_TableHeaderBg, ImVec4(0, 0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_HeaderHovered, ImVec4(0, 0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_HeaderActive, ImVec4(0, 0, 0, 0));
ImGui::TableHeadersRow();
ImGui::PopStyleColor(3);
// Table rows
for (size_t j = 0; j < entries.size(); ++j) {
ImGui::PushID(static_cast<int>(j));
ImGui::TableNextRow();
// Column 1: Move
ImGui::TableSetColumnIndex(0);
ImGui::Text("%03zu", j);
// Column 2: ID
ImGui::TableSetColumnIndex(1);
ImGui::SetNextItemWidth(-FLT_MIN);
ImGui::InputScalar(
"##ID",
ImGuiDataType_U16,
&entries[j].id,
nullptr,
nullptr,
"%04X",
ImGuiInputTextFlags_CharsHexadecimal
);
// Column 3: Type
ImGui::TableSetColumnIndex(2);
ImGui::SetNextItemWidth(-FLT_MIN);
ImGui::InputScalar(
"##Category",
ImGuiDataType_U16,
&entries[j].category,
nullptr,
nullptr,
"%04X",
ImGuiInputTextFlags_CharsHexadecimal
);
// Column 4: Add
ImGui::TableSetColumnIndex(3);
if (ImGui::Button(" Add ")) {
entries.insert(entries.begin() + j + 1, { 0x0000, 0x0000 });
}
// Column 5: Delete
ImGui::TableSetColumnIndex(4);
if (ImGui::Button(" Delete ")) {
entries.erase(entries.begin() + j);
--j;
}
ImGui::PopID();
}
ImGui::EndTable();
ImGui::PopStyleVar();
}
ImGui::Dummy(ImVec2(0.0f, 2.0f));
ImGui::Unindent();
ImGui::Separator();
}
}
ImGui::Dummy(ImVec2(0.0f, 5.0f));
// "Save File" button
if (ImGui::Button("Save File")) {
SaveSections(filePath, sections);
}
ImGui::Dummy(ImVec2(0.0f, 5.0f));
ImGui::Unindent();
ImGui::End();
// Rendering
ImGui::Render();
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}