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