23 if (target_version < 2 || target_version > 3) {
24 return absl::InvalidArgumentError(absl::StrFormat(
25 "Invalid target version: %d. Must be 2 or 3.", target_version));
30 if (current_version != 0xFF && current_version >= target_version) {
31 return absl::AlreadyExistsError(absl::StrFormat(
32 "ROM is already version %d or higher", current_version));
36 "Applying ZSCustomOverworld ASM v%d to ROM...", target_version);
39 auto asar_wrapper = std::make_unique<core::AsarWrapper>();
43 std::vector<uint8_t> original_rom_data =
rom_.
vector();
44 std::vector<uint8_t> working_rom_data = original_rom_data;
48 std::string asm_file_name =
49 (target_version == 3) ?
"asm/yaze.asm" :
"asm/ZSCustomOverworld.asm";
53 LOG_DEBUG(
"OverworldUpgradeSystem",
"Using ASM file: %s",
54 asm_file_path.c_str());
56 if (!std::filesystem::exists(asm_file_path)) {
57 return absl::NotFoundError(
58 absl::StrFormat(
"ASM file not found at: %s\n\n"
59 "Expected location: assets/%s\n"
60 "Make sure the assets directory is accessible.",
61 asm_file_path, asm_file_name));
66 asar_wrapper->ApplyPatch(asm_file_path, working_rom_data);
67 if (!patch_result.ok()) {
68 return absl::InternalError(absl::StrFormat(
69 "Failed to apply ASM patch: %s", patch_result.status().message()));
72 const auto& result = patch_result.value();
73 if (!result.success) {
74 std::string error_details =
"ASM patch failed with errors:\n";
75 for (
const auto& error : result.errors) {
76 error_details +=
" - " + error +
"\n";
78 if (!result.warnings.empty()) {
79 error_details +=
"Warnings:\n";
80 for (
const auto& warning : result.warnings) {
81 error_details +=
" - " + warning +
"\n";
84 return absl::InternalError(error_details);
95 "ASM patch applied successfully. Found %zu symbols:",
96 result.symbols.size());
97 for (
const auto& symbol : result.symbols) {
98 LOG_DEBUG(
"OverworldUpgradeSystem",
" %s @ $%06X", symbol.name.c_str(),
103 "ZSCustomOverworld v%d successfully applied to ROM",
105 return absl::OkStatus();
107 }
catch (
const std::exception& e) {
110 if (!restore_result.ok()) {
111 LOG_ERROR(
"OverworldUpgradeSystem",
"Failed to restore ROM data: %s",
112 restore_result.message().data());
114 return absl::InternalError(
115 absl::StrFormat(
"Exception during ASM application: %s", e.what()));
120 int target_version) {
125 if (target_version >= 2) {
129 "Enabled v2+ features: Custom BG colors, Main palettes");
132 if (target_version >= 3) {
139 "Enabled v3+ features: Subscreen overlays, Animated GFX, Tile "
140 "GFX groups, Mosaic");
142 for (
int i = 0; i < 0xA0; i++) {
147 const std::vector<int> large_areas = {
148 0x00, 0x02, 0x05, 0x07, 0x0A, 0x0B, 0x0F, 0x10, 0x11, 0x12, 0x13,
149 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1D, 0x1E, 0x25,
150 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2E, 0x2F, 0x30, 0x32, 0x33, 0x34,
151 0x35, 0x37, 0x3A, 0x40, 0x42, 0x45, 0x47, 0x4A, 0x4B, 0x4F, 0x50,
152 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B,
153 0x5D, 0x5E, 0x65, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6E, 0x6F, 0x70,
154 0x72, 0x73, 0x74, 0x75, 0x77, 0x7A, 0x80, 0x81};
155 for (
int area : large_areas) {
161 return absl::OkStatus();