yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
asar_patch.cc
Go to the documentation of this file.
2
3#include <ftxui/component/component.hpp>
4#include <ftxui/component/screen_interactive.hpp>
5
6#include "absl/strings/str_format.h"
7#include "absl/strings/str_join.h"
8#include "cli/tui/tui.h"
9#include "core/asar_wrapper.h"
10
11namespace yaze {
12namespace cli {
13
14using namespace ftxui;
15
16ftxui::Component AsarPatchComponent::Render() {
17 struct AsarState {
18 std::string patch_file;
19 std::string output_message;
20 std::vector<std::string> symbols_list;
21 bool show_symbols = false;
22 };
23
24 auto state = std::make_shared<AsarState>();
25
26 auto patch_file_input =
27 Input(&state->patch_file, "Assembly patch file (.asm)");
28
29 auto apply_button = Button("Apply Asar Patch", [state] {
30 if (state->patch_file.empty()) {
31 app_context.error_message = "Please specify an assembly patch file";
32 // SwitchComponents(screen, LayoutID::kError);
33 return;
34 }
35
36 if (!app_context.rom.is_loaded()) {
37 app_context.error_message = "No ROM loaded. Please load a ROM first.";
38 // SwitchComponents(screen, LayoutID::kError);
39 return;
40 }
41
42 try {
43 core::AsarWrapper wrapper;
44 auto init_status = wrapper.Initialize();
45 if (!init_status.ok()) {
46 app_context.error_message =
47 absl::StrCat("Failed to initialize Asar: ", init_status.message());
48 // SwitchComponents(screen, LayoutID::kError);
49 return;
50 }
51
52 auto rom_data = app_context.rom.vector();
53 auto patch_result = wrapper.ApplyPatch(state->patch_file, rom_data);
54
55 if (!patch_result.ok()) {
56 app_context.error_message =
57 absl::StrCat("Patch failed: ", patch_result.status().message());
58 // SwitchComponents(screen, LayoutID::kError);
59 return;
60 }
61
62 const auto& result = patch_result.value();
63 if (!result.success) {
64 app_context.error_message =
65 absl::StrCat("Patch failed: ", absl::StrJoin(result.errors, "; "));
66 // SwitchComponents(screen, LayoutID::kError);
67 return;
68 }
69
70 state->output_message = absl::StrFormat(
71 "✅ Patch applied successfully!\n"
72 "📊 ROM size: %d bytes\n"
73 "🏷️ Symbols found: %d",
74 result.rom_size, result.symbols.size());
75
76 state->symbols_list.clear();
77 for (const auto& symbol : result.symbols) {
78 state->symbols_list.push_back(
79 absl::StrFormat("% -20s @ $%06X", symbol.name, symbol.address));
80 }
81 state->show_symbols = !state->symbols_list.empty();
82
83 } catch (const std::exception& e) {
84 app_context.error_message = "Exception: " + std::string(e.what());
85 // SwitchComponents(screen, LayoutID::kError);
86 }
87 });
88
89 auto show_symbols_button = Button(
90 "Show Symbols", [state] { state->show_symbols = !state->show_symbols; });
91
92 auto back_button = Button("Back to Main Menu", [state] {
93 state->output_message.clear();
94 state->symbols_list.clear();
95 state->show_symbols = false;
96 // SwitchComponents(screen, LayoutID::kMainMenu);
97 });
98
99 auto container = Container::Vertical({
100 patch_file_input,
101 apply_button,
102 show_symbols_button,
103 back_button,
104 });
105
106 return Renderer(container, [patch_file_input, apply_button,
107 show_symbols_button, back_button, state] {
108 std::vector<Element> elements = {
109 text("Apply Asar Patch") | bold | center,
110 separator(),
111 text("Patch File:"),
112 patch_file_input->Render(),
113 separator(),
114 apply_button->Render() | center,
115 };
116
117 if (!state->output_message.empty()) {
118 elements.push_back(separator());
119 elements.push_back(text(state->output_message) | color(Color::Green));
120
121 if (state->show_symbols && !state->symbols_list.empty()) {
122 elements.push_back(separator());
123 elements.push_back(text("Symbols:") | bold);
124 elements.push_back(show_symbols_button->Render() | center);
125
126 std::vector<Element> symbol_elements;
127 for (const auto& symbol : state->symbols_list) {
128 symbol_elements.push_back(text(symbol) | color(Color::Cyan));
129 }
130 elements.push_back(vbox(symbol_elements) | frame |
131 size(HEIGHT, LESS_THAN, 10));
132 } else if (!state->symbols_list.empty()) {
133 elements.push_back(separator());
134 elements.push_back(show_symbols_button->Render() | center);
135 }
136 }
137
138 elements.push_back(separator());
139 elements.push_back(back_button->Render() | center);
140
141 return vbox(elements) | center | border;
142 });
143}
144
145} // namespace cli
146} // namespace yaze
ftxui::Component Render() override
Definition asar_patch.cc:16
Definition cli.h:17