yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
patch_export_usage.cc
Go to the documentation of this file.
1// Example: How to use WasmPatchExport in the yaze editor
2// This code would typically be integrated into the ROM file manager or editor menu
3
5#include "app/rom.h"
6#include "imgui.h"
7
8namespace yaze {
9namespace editor {
10
11// Example function that could be added to RomFileManager or MenuOrchestrator
13 static bool show_export_dialog = false;
14 static int patch_format = 0; // 0 = BPS, 1 = IPS
15 static char filename[256] = "my_hack";
16
17 // Menu item to trigger export
18 if (ImGui::MenuItem("Export Patch...", nullptr, nullptr, rom->is_loaded())) {
19 show_export_dialog = true;
20 }
21
22 // Export dialog window
23 if (show_export_dialog) {
24 ImGui::OpenPopup("Export Patch");
25 }
26
27 if (ImGui::BeginPopupModal("Export Patch", &show_export_dialog)) {
28 // Get the original ROM data (assuming rom stores both original and modified)
29 const auto& original_data = rom->original_data(); // Would need to add this
30 const auto& modified_data = rom->data();
31
32 // Show patch preview information
34 original_data, modified_data);
35
36 ImGui::Text("Patch Summary:");
37 ImGui::Separator();
38 ImGui::Text("Total changed bytes: %zu", patch_info.changed_bytes);
39 ImGui::Text("Number of regions: %zu", patch_info.num_regions);
40
41 if (patch_info.changed_bytes == 0) {
42 ImGui::TextColored(ImVec4(1, 1, 0, 1), "No changes detected!");
43 }
44
45 // Show changed regions (limit to first 10 for UI)
46 if (!patch_info.changed_regions.empty()) {
47 ImGui::Separator();
48 ImGui::Text("Changed Regions:");
49 int region_count = 0;
50 for (const auto& region : patch_info.changed_regions) {
51 if (region_count >= 10) {
52 ImGui::Text("... and %zu more regions",
53 patch_info.changed_regions.size() - 10);
54 break;
55 }
56 ImGui::Text(" Offset: 0x%06X, Size: %zu bytes",
57 static_cast<unsigned>(region.first), region.second);
58 region_count++;
59 }
60 }
61
62 ImGui::Separator();
63
64 // Format selection
65 ImGui::Text("Patch Format:");
66 ImGui::RadioButton("BPS (Beat)", &patch_format, 0);
67 ImGui::SameLine();
68 ImGui::RadioButton("IPS", &patch_format, 1);
69
70 // Filename input
71 ImGui::Text("Filename:");
72 ImGui::InputText("##filename", filename, sizeof(filename));
73
74 // Export buttons
75 ImGui::Separator();
76 if (ImGui::Button("Export", ImVec2(120, 0))) {
77 if (patch_info.changed_bytes > 0) {
78 absl::Status status;
79 std::string full_filename = std::string(filename);
80
81 if (patch_format == 0) {
82 // BPS format
83 if (full_filename.find(".bps") == std::string::npos) {
84 full_filename += ".bps";
85 }
87 original_data, modified_data, full_filename);
88 } else {
89 // IPS format
90 if (full_filename.find(".ips") == std::string::npos) {
91 full_filename += ".ips";
92 }
94 original_data, modified_data, full_filename);
95 }
96
97 if (status.ok()) {
98 ImGui::CloseCurrentPopup();
99 show_export_dialog = false;
100 // Could show success toast here
101 } else {
102 // Show error message
103 ImGui::OpenPopup("Export Error");
104 }
105 }
106 }
107
108 ImGui::SameLine();
109 if (ImGui::Button("Cancel", ImVec2(120, 0))) {
110 ImGui::CloseCurrentPopup();
111 show_export_dialog = false;
112 }
113
114 // Error popup
115 if (ImGui::BeginPopupModal("Export Error")) {
116 ImGui::Text("Failed to export patch!");
117 if (ImGui::Button("OK", ImVec2(120, 0))) {
118 ImGui::CloseCurrentPopup();
119 }
120 ImGui::EndPopup();
121 }
122
123 ImGui::EndPopup();
124 }
125}
126
127// Alternative: Quick export functions for toolbar/menu
128void QuickExportBPS(Rom* rom) {
129#ifdef __EMSCRIPTEN__
130 if (!rom || !rom->is_loaded()) return;
131
132 const auto& original = rom->original_data(); // Would need to add this
133 const auto& modified = rom->data();
134
135 // Generate default filename based on ROM name
136 std::string filename = rom->filename();
137 size_t dot_pos = filename.find_last_of('.');
138 if (dot_pos != std::string::npos) {
139 filename = filename.substr(0, dot_pos);
140 }
141 filename += ".bps";
142
144 original, modified, filename);
145
146 if (!status.ok()) {
147 // Show error toast or log
148 }
149#endif
150}
151
152void QuickExportIPS(Rom* rom) {
153#ifdef __EMSCRIPTEN__
154 if (!rom || !rom->is_loaded()) return;
155
156 const auto& original = rom->original_data();
157 const auto& modified = rom->data();
158
159 // Check IPS size limit
160 if (modified.size() > 0xFFFFFF) {
161 // Show error: ROM too large for IPS format
162 return;
163 }
164
165 std::string filename = rom->filename();
166 size_t dot_pos = filename.find_last_of('.');
167 if (dot_pos != std::string::npos) {
168 filename = filename.substr(0, dot_pos);
169 }
170 filename += ".ips";
171
173 original, modified, filename);
174
175 if (!status.ok()) {
176 // Show error toast or log
177 }
178#endif
179}
180
181} // namespace editor
182} // namespace yaze
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:24
auto filename() const
Definition rom.h:141
auto data() const
Definition rom.h:135
bool is_loaded() const
Definition rom.h:128
static absl::Status ExportIPS(const std::vector< uint8_t > &, const std::vector< uint8_t > &, const std::string &)
static PatchInfo GetPatchPreview(const std::vector< uint8_t > &, const std::vector< uint8_t > &)
static absl::Status ExportBPS(const std::vector< uint8_t > &, const std::vector< uint8_t > &, const std::string &)
void QuickExportIPS(Rom *rom)
void QuickExportBPS(Rom *rom)
void ShowPatchExportDialog(Rom *rom)