11#include "absl/strings/numbers.h"
12#include "absl/strings/str_format.h"
13#include "absl/strings/str_split.h"
45 if (!value.has_value()) {
46 return absl::InvalidArgumentError(
47 absl::StrFormat(
"Missing required argument '--%s'",
name));
56 return parsed.status();
58 return parsed.value();
62 const char*
name,
int default_value) {
69 return parsed.status();
71 return parsed.value();
76 return absl::InvalidArgumentError(
77 absl::StrFormat(
"Room ID out of range: 0x%X (expected 0x00-0x%02X)",
80 return absl::OkStatus();
84 if (value < 0 || value > 31) {
85 return absl::InvalidArgumentError(
86 absl::StrFormat(
"%c must be 0-31 (5-bit tile coord)", axis));
88 return absl::OkStatus();
95 auto disk_status = rom->
SaveToFile(save_settings);
96 if (!disk_status.ok()) {
97 formatter.
AddField(
"save_error", std::string(disk_status.message()));
101 formatter.
AddField(
"save_status",
"saved");
102 return absl::OkStatus();
105template <
typename Mutation>
108 Mutation&& mutation) {
111 auto write_status = std::forward<Mutation>(mutation)();
112 if (!write_status.ok()) {
113 formatter.
AddField(
"write_error", std::string(write_status.message()));
116 formatter.
AddField(
"write_status",
"success");
120 return absl::OkStatus();
132absl::StatusOr<std::optional<StreamSaveManifestContext>>
135 const auto manifest_path = parser.
GetString(
"manifest");
136 if (!manifest_path.has_value()) {
144 const char* stream_name =
146 if (manifest_layout ==
nullptr) {
147 return absl::FailedPreconditionError(absl::StrFormat(
148 "Manifest does not define dungeon_stream_regions.%s", stream_name));
151 return absl::FailedPreconditionError(absl::StrFormat(
152 "dungeon_stream_regions.%s must use copy_on_write", stream_name));
158 return std::optional<StreamSaveManifestContext>(std::move(context));
162 uint32_t source_data_pc = 0;
163 uint32_t planned_data_pc = 0;
171 if ((subtype & 0x07) == 0x07 && (
id == 0 ||
id > 0x1A)) {
172 return absl::InvalidArgumentError(
173 "Subtype low bits 7 encode an overlord; only IDs 0x01-0x1A are valid");
175 if ((y | ((subtype & 0x18) << 2) | (layer << 7)) == 0xFF) {
176 return absl::InvalidArgumentError(
177 "Sprite encoding is the end of the sprite list (0xFF)");
179 if (
id == 0xE4 && x == 0 && (y == 29 || y == 30) && subtype == 24 &&
181 return absl::InvalidArgumentError(
182 "This E4 position/subtype/layer encodes a hidden key marker; use "
183 "the owning sprite's Key drop property");
185 return absl::OkStatus();
191 const auto* layout = manifest_context !=
nullptr
202 if (manifest_context !=
nullptr &&
205 return absl::PermissionDeniedError(
"Write conflict with Hack Manifest");
225 const auto& expected = pending_room.
GetSprites();
226 const auto& reopened = scratch_room.
GetSprites();
227 if (reopened.size() != expected.size()) {
228 return absl::FailedPreconditionError(
229 "Sprite placement does not survive save/reopen");
231 for (
size_t i = 0; i < expected.size(); ++i) {
232 if (reopened[i].
id() != expected[i].
id() ||
233 reopened[i].x() != expected[i].x() ||
234 reopened[i].y() != expected[i].y() ||
235 reopened[i].subtype() != expected[i].subtype() ||
236 reopened[i].layer() != expected[i].layer() ||
237 reopened[i].key_drop() != expected[i].key_drop()) {
238 return absl::FailedPreconditionError(
239 "Sprite placement does not survive save/reopen");
248 std::vector<std::pair<uint32_t, uint32_t>> ranges;
253 uint32_t object_pointer_table_snes = 0;
256 const uint32_t object_pointer_table_pc =
SnesToPc(object_pointer_table_snes);
257 const uint64_t current_pointer_slot =
258 static_cast<uint64_t
>(object_pointer_table_pc) +
259 static_cast<uint64_t
>(room_id) * 3u;
260 if (current_pointer_slot + 3u > source_rom.
size()) {
261 return absl::OutOfRangeError(
262 "Selected object pointer slot is outside the ROM");
265 uint32_t current_stream_snes = 0;
267 source_rom.
ReadLong(
static_cast<int>(current_pointer_slot)));
268 const uint32_t current_stream_pc =
SnesToPc(current_stream_snes);
269 const uint64_t current_stream_end =
static_cast<uint64_t
>(current_stream_pc) +
271 if (current_stream_end > std::numeric_limits<uint32_t>::max()) {
272 return absl::OutOfRangeError(
"Selected object write range overflows");
274 ranges.emplace_back(current_stream_pc,
275 static_cast<uint32_t
>(current_stream_end));
277 const uint64_t door_pointer_slot =
279 static_cast<uint64_t
>(room_id) * 3u;
280 if (door_pointer_slot + 3u > std::numeric_limits<uint32_t>::max()) {
281 return absl::OutOfRangeError(
"Selected door pointer range overflows");
283 ranges.emplace_back(
static_cast<uint32_t
>(door_pointer_slot),
284 static_cast<uint32_t
>(door_pointer_slot + 3u));
287 ranges.emplace_back(range.begin, range.end);
293 const uint64_t cow_pointer_slot =
295 static_cast<uint64_t
>(room_id) * pointer_width;
296 if (cow_pointer_slot + pointer_width > std::numeric_limits<uint32_t>::max()) {
297 return absl::OutOfRangeError(
"Selected COW pointer range overflows");
299 ranges.emplace_back(
static_cast<uint32_t
>(cow_pointer_slot),
300 static_cast<uint32_t
>(cow_pointer_slot + pointer_width));
303 if (conflicts.empty() ||
305 return absl::OkStatus();
307 return absl::PermissionDeniedError(
"Write conflict with Hack Manifest");
313 const auto* layout = manifest_context !=
nullptr
320 const auto lookup = [&](
int id) ->
const zelda3::Room* {
321 return id == room_id ? &room :
nullptr;
326 auto chest_ranges = plan.write_ranges();
328 chest_ranges.emplace_back(
329 plan.data_pc,
static_cast<uint32_t
>(plan.data_pc +
330 plan.original_capacity_bytes.size()));
331 for (
const auto& object_range : object_ranges) {
332 for (
const auto& chest_range : chest_ranges) {
333 if (object_range.first < chest_range.second &&
334 chest_range.first < object_range.second) {
335 return absl::FailedPreconditionError(
336 "Object and chest save ranges overlap");
340 if (manifest_context !=
nullptr &&
343 return absl::PermissionDeniedError(
"Write conflict with Hack Manifest");
356 if (manifest_context !=
nullptr) {
358 source_rom, room_id, pending_room, *manifest_context));
381 const Rom& source_rom,
int room_id,
int object_vector_index,
384 if (manifest_context !=
nullptr) {
386 source_rom, room_id, pending_room, *manifest_context));
397 return scratch_room.
SaveObjects(manifest_context !=
nullptr
405 if (!value_or.ok()) {
406 return value_or.status();
410 if (!ParseHexString(*value_or, &value)) {
411 return absl::InvalidArgumentError(absl::StrFormat(
412 "Invalid --%s value '%s' (expected integer/hex)",
name, *value_or));
418 if (type < 0 || type > 0x66 || (type & 1) != 0) {
419 return absl::InvalidArgumentError(absl::StrFormat(
420 "--%s must be an even door type in range 0x00-0x66", argument_name));
422 return absl::OkStatus();
426 return std::find(room_ids.begin(), room_ids.end(),
427 static_cast<uint32_t
>(room_id)) != room_ids.end();
431 if ((snes_address & 0xFFFFu) < 0x8000u) {
434 const uint8_t normalized_bank =
435 static_cast<uint8_t
>((snes_address >> 16) & 0x7Fu);
436 if (normalized_bank >= 0x7Eu) {
439 const uint32_t pc_address =
SnesToPc(snes_address);
440 return (
PcToSnes(pc_address) & 0x7FFFFFu) == (snes_address & 0x7FFFFFu);
452 return start <= rom.
size() && length <= rom.
size() - start;
457 return absl::FailedPreconditionError(
458 "ROM is too small to prove US/OOS dungeon palette table layout");
460 uint8_t destination_code = 0;
463 if (destination_code != 0x01) {
464 return absl::FailedPreconditionError(absl::StrFormat(
465 "Dungeon palette commands currently support only the US/OOS table "
466 "layout (SNES destination code 0x01); got unsupported code 0x%02X",
469 return absl::OkStatus();
474 uint8_t palette_set_id = 0;
475 uint8_t palette_pointer_offset = 0;
476 uint16_t palette_pointer_word = 0;
477 int palette_index = -1;
478 uint32_t room_header_pc = 0;
479 uint32_t palette_set_pc = 0;
480 uint32_t palette_pointer_pc = 0;
481 uint32_t palette_pc = 0;
489 return absl::FailedPreconditionError(
490 "Dungeon room-header pointer metadata is outside the ROM");
493 uint32_t table_snes = 0;
496 return absl::FailedPreconditionError(absl::StrFormat(
497 "Dungeon room-header pointer table is not a valid LoROM pointer: "
501 const uint32_t table_pc =
SnesToPc(table_snes);
502 const uint64_t slot_pc =
503 static_cast<uint64_t
>(table_pc) +
static_cast<uint64_t
>(room_id) * 2u;
505 return absl::FailedPreconditionError(absl::StrFormat(
506 "Dungeon room 0x%03X header pointer slot is outside the ROM", room_id));
509 uint16_t header_offset = 0;
510 uint8_t header_bank = 0;
513 const uint32_t header_snes =
514 (
static_cast<uint32_t
>(header_bank) << 16) | header_offset;
516 return absl::FailedPreconditionError(absl::StrFormat(
517 "Dungeon room 0x%03X header is not a valid LoROM pointer: 0x%06X",
518 room_id, header_snes));
521 const uint32_t header_pc =
SnesToPc(header_snes);
523 return absl::FailedPreconditionError(absl::StrFormat(
524 "Dungeon room 0x%03X header is outside the ROM", room_id));
530 const Rom& rom,
int room_id) {
538 return absl::FailedPreconditionError(absl::StrFormat(
539 "Dungeon room 0x%03X has out-of-range 8-bit palette-set ID 0x%02X "
540 "(expected 0x00-0x%02X)",
547 return absl::FailedPreconditionError(absl::StrFormat(
548 "Palette-set entry 0x%02X is outside the ROM", mapping.
palette_set_id));
554 return absl::FailedPreconditionError(absl::StrFormat(
555 "Palette-set 0x%02X has malformed dungeon palette pointer offset "
556 "0x%02X (expected an even byte offset 0x00-0x%02X)",
564 return absl::FailedPreconditionError(
565 "Resolved dungeon palette pointer is outside the ROM");
570 return absl::FailedPreconditionError(absl::StrFormat(
571 "Palette-set 0x%02X resolves to malformed palette word 0x%04X "
572 "(not divisible by %d)",
581 return absl::FailedPreconditionError(absl::StrFormat(
582 "Palette-set 0x%02X resolves to out-of-range dungeon palette %d",
591 return absl::FailedPreconditionError(absl::StrFormat(
592 "Palette-set 0x%02X resolves outside the fixed dungeon palette "
593 "region [0x%06X,0x%06X)",
606 const Rom& rom,
int room_id) {
614 if (!mapping_or.ok()) {
616 mapping_or.status().code(),
618 "Cannot prove complete shared palette fanout because room "
619 "0x%03X mapping is invalid: %s",
620 candidate, mapping_or.status().message()));
623 selection.
affected_rooms.push_back(
static_cast<uint32_t
>(candidate));
631 return absl::InvalidArgumentError(absl::StrFormat(
634 return absl::OkStatus();
638 if (color < 0 || color > 0x7FFF) {
639 return absl::InvalidArgumentError(absl::StrFormat(
640 "--%s must be a 15-bit SNES color in range 0x0000-0x7FFF",
643 return absl::OkStatus();
648 return mapping.
palette_pc +
static_cast<uint32_t
>(color_index) * 2u;
658 return absl::FailedPreconditionError(
659 "Resolved dungeon palette color is outside the fixed palette region");
663 if (color > 0x7FFF) {
664 return absl::FailedPreconditionError(absl::StrFormat(
665 "Resolved dungeon palette color at PC 0x%06X is not a 15-bit SNES "
674 const auto& mapping = selection.
selected;
675 formatter.
AddHexField(
"room_id", mapping.room_id, 3);
676 formatter.
AddHexField(
"palette_set_id", mapping.palette_set_id, 2);
677 formatter.
AddField(
"resolved_palette_index", mapping.palette_index);
678 formatter.
AddHexField(
"room_header_pc", mapping.room_header_pc, 6);
679 formatter.
AddHexField(
"palette_set_pc", mapping.palette_set_pc, 6);
681 mapping.palette_pointer_offset, 2);
682 formatter.
AddHexField(
"palette_pointer_pc", mapping.palette_pointer_pc, 6);
683 formatter.
AddHexField(
"palette_pointer_word", mapping.palette_pointer_word,
685 formatter.
AddHexField(
"palette_pc", mapping.palette_pc, 6);
687 formatter.
AddField(
"scope",
"shared_global_dungeon_palette");
688 formatter.
AddField(
"affected_room_count",
692 formatter.
AddArrayItem(absl::StrFormat(
"0x%03X", affected_room));
699 const auto conflicts =
701 if (conflicts.empty()) {
702 return absl::OkStatus();
705 return absl::PermissionDeniedError(absl::StrFormat(
706 "Dungeon palette color range [0x%06X,0x%06X) conflicts with Hack "
707 "Manifest ownership at SNES 0x%06X (%s%s%s)",
708 color_pc, color_pc + 2u, conflict.
address,
710 conflict.module.empty() ?
"" :
", module=",
711 conflict.module.empty() ?
"" : conflict.module));
715 const std::vector<uint8_t>& before,
const Rom& after, uint32_t color_pc) {
716 if (after.
size() != before.size()) {
717 return absl::DataLossError(
718 "Dungeon palette write unexpectedly resized the ROM");
721 std::vector<uint32_t> changed;
722 for (uint32_t pc = 0; pc < before.size(); ++pc) {
723 if (before[pc] == after.
data()[pc]) {
726 if (pc < color_pc || pc >= color_pc + 2u) {
727 return absl::DataLossError(absl::StrFormat(
728 "Dungeon palette write changed unexpected ROM byte 0x%06X", pc));
730 changed.push_back(pc);
732 if (changed.empty() || changed.size() > 2u) {
733 return absl::DataLossError(absl::StrFormat(
734 "Dungeon palette write changed %zu bytes; expected one or two bytes "
735 "inside the fenced color word",
743 return absl::FailedPreconditionError(
744 "Dungeon palette write requires a clean file-backed ROM; save or "
745 "discard unrelated in-memory edits first");
748 return absl::FailedPreconditionError(
749 "Dungeon palette write requires a ROM filename");
758 return absl::FailedPreconditionError(
759 "Dungeon palette write requires in-memory ROM bytes to exactly match "
760 "the current file; refusing to persist unrelated or stale bytes");
762 return absl::OkStatus();
766 const Rom& saved_rom,
int room_id,
int expected_palette_set,
767 int expected_palette_index,
int color_index, uint16_t expected_color) {
769 return absl::FailedPreconditionError(
770 "Write mode requires a ROM filename for external readback");
779 return absl::DataLossError(
780 "External reopen bytes do not match the saved in-memory ROM");
789 return absl::DataLossError(
790 "External reopen changed the selected dungeon palette mapping");
792 uint16_t reopened_color = 0;
796 if (reopened_color != expected_color) {
797 return absl::DataLossError(absl::StrFormat(
798 "External reopen color mismatch: expected 0x%04X, got 0x%04X",
799 expected_color, reopened_color));
801 return absl::OkStatus();
807 uint32_t object_stream_pc = 0;
808 uint32_t object_stream_end_pc = 0;
809 uint32_t door_pointer_pc = 0;
810 uint32_t door_entry_pc = 0;
811 uint32_t type_pc = 0;
815 Rom* rom,
int room_id,
int x,
int y,
819 return absl::FailedPreconditionError(
820 "Dungeon object manifest pointer encoding must be long24");
824 return absl::FailedPreconditionError(absl::StrFormat(
825 "Dungeon object manifest pointer_count must equal %d rooms",
829 uint32_t runtime_pointer_table_snes = 0;
833 return absl::FailedPreconditionError(
834 "Runtime object pointer table is not a valid LoROM data pointer");
836 const uint32_t runtime_pointer_table_pc =
837 SnesToPc(runtime_pointer_table_snes);
838 if (runtime_pointer_table_pc !=
840 return absl::FailedPreconditionError(absl::StrFormat(
841 "Manifest object pointer table PC 0x%06X does not match runtime "
844 runtime_pointer_table_pc));
850 if (
static_cast<size_t>(room_id) >= inventory.
streams.size()) {
851 return absl::FailedPreconditionError(
852 "Selected room is absent from the object stream inventory");
854 for (
const auto& issue : inventory.
issues) {
855 if (issue.room_id ==
static_cast<uint32_t
>(room_id)) {
856 return absl::FailedPreconditionError(absl::StrFormat(
857 "Selected object stream is invalid: %s", issue.message));
861 const auto& stream = inventory.
streams[room_id];
863 return absl::FailedPreconditionError(
"Selected object stream is invalid");
865 for (
const auto& alias : inventory.
aliases) {
867 return absl::FailedPreconditionError(absl::StrFormat(
868 "Selected object stream at PC 0x%06X is shared by %d rooms",
869 alias.data_pc,
static_cast<int>(alias.room_ids.size())));
872 for (
const auto& overlap : inventory.
overlaps) {
875 return absl::FailedPreconditionError(absl::StrFormat(
876 "Selected object stream overlaps PC range [0x%06X, 0x%06X)",
877 overlap.intersection.begin, overlap.intersection.end));
880 for (
const auto& other_stream : inventory.
streams) {
881 if (other_stream.room_id ==
static_cast<uint32_t
>(room_id)) {
884 if (other_stream.data_pc >= stream.data_pc &&
885 other_stream.data_pc < stream.logical_end_pc) {
886 return absl::FailedPreconditionError(absl::StrFormat(
887 "Room 0x%03X object pointer PC 0x%06X starts inside selected "
888 "object stream [0x%06X, 0x%06X)",
889 other_stream.room_id, other_stream.data_pc, stream.data_pc,
890 stream.logical_end_pc));
895 if (other_room_id == room_id) {
898 const uint32_t other_door_pointer_slot =
900 uint32_t other_door_pointer_snes = 0;
902 rom->
ReadLong(
static_cast<int>(other_door_pointer_slot)));
906 const uint32_t other_door_pointer_pc =
SnesToPc(other_door_pointer_snes);
907 if (other_door_pointer_pc >= stream.data_pc &&
908 other_door_pointer_pc < stream.logical_end_pc) {
909 return absl::FailedPreconditionError(absl::StrFormat(
910 "Room 0x%03X door pointer PC 0x%06X points inside selected "
911 "object stream [0x%06X, 0x%06X)",
912 other_room_id, other_door_pointer_pc, stream.data_pc,
913 stream.logical_end_pc));
917 const uint64_t runtime_pointer_slot =
918 static_cast<uint64_t
>(runtime_pointer_table_pc) +
919 static_cast<uint64_t
>(room_id) * 3u;
920 if (runtime_pointer_slot + 3u > rom->
size() ||
921 runtime_pointer_slot != stream.pointer_slot_pc) {
922 return absl::FailedPreconditionError(
923 "Selected runtime object pointer slot does not match the manifest");
925 uint32_t runtime_stream_snes = 0;
927 rom->
ReadLong(
static_cast<int>(runtime_pointer_slot)));
929 return absl::FailedPreconditionError(
930 "Selected runtime object stream is not a valid LoROM data pointer");
932 const uint32_t runtime_stream_pc =
SnesToPc(runtime_stream_snes);
933 if (runtime_stream_pc != stream.data_pc) {
934 return absl::FailedPreconditionError(absl::StrFormat(
935 "Selected runtime object stream PC 0x%06X does not match inventory "
937 runtime_stream_pc, stream.data_pc));
941 const auto& doors = room.
GetDoors();
942 int selected_index = -1;
943 for (
int index = 0; index < static_cast<int>(doors.size()); ++index) {
944 const auto [door_x, door_y] = doors[index].GetTileCoords();
945 if (door_x != x || door_y != y) {
948 if (selected_index >= 0) {
949 return absl::FailedPreconditionError(absl::StrFormat(
950 "Multiple doors match tile coordinates (%d, %d)", x, y));
952 selected_index = index;
954 if (selected_index < 0) {
955 return absl::NotFoundError(absl::StrFormat(
956 "No door matches tile coordinates (%d, %d) in room 0x%03X", x, y,
960 const uint64_t door_pointer_slot =
962 static_cast<uint64_t
>(room_id) * 3u;
963 if (door_pointer_slot + 3u > rom->
size()) {
964 return absl::OutOfRangeError(
"Selected door pointer slot is outside ROM");
966 uint32_t door_pointer_snes = 0;
968 rom->
ReadLong(
static_cast<int>(door_pointer_slot)));
970 return absl::FailedPreconditionError(
971 "Selected door pointer is not a valid LoROM data pointer");
973 const uint32_t door_pointer_pc =
SnesToPc(door_pointer_snes);
974 const uint64_t door_entry_pc =
static_cast<uint64_t
>(door_pointer_pc) +
975 static_cast<uint64_t
>(selected_index) * 2u;
976 const uint64_t expected_stream_end =
977 static_cast<uint64_t
>(door_pointer_pc) +
978 static_cast<uint64_t
>(doors.size()) * 2u + 2u;
979 if (door_pointer_pc < stream.data_pc ||
980 door_entry_pc + 2u > stream.logical_end_pc ||
981 expected_stream_end != stream.logical_end_pc) {
982 return absl::FailedPreconditionError(
983 "Door pointer/count does not match the inventoried object stream end");
986 uint8_t raw_byte1 = 0;
987 uint8_t raw_byte2 = 0;
988 for (
int index = 0; index < static_cast<int>(doors.size()); ++index) {
989 const uint32_t entry_pc =
990 door_pointer_pc +
static_cast<uint32_t
>(index) * 2u;
991 uint8_t entry_byte1 = 0;
992 uint8_t entry_byte2 = 0;
995 rom->
ReadByte(
static_cast<int>(entry_pc + 1u)));
996 const auto [model_byte1, model_byte2] = doors[index].EncodeBytes();
997 if (model_byte1 != entry_byte1 || model_byte2 != entry_byte2 ||
998 doors[index].byte1 != entry_byte1 ||
999 doors[index].byte2 != entry_byte2) {
1000 return absl::DataLossError(
1001 "Door model does not match the raw door-pointer entries");
1003 if (index == selected_index) {
1004 raw_byte1 = entry_byte1;
1005 raw_byte2 = entry_byte2;
1008 uint16_t door_terminator = 0;
1010 rom->
ReadWord(
static_cast<int>(expected_stream_end - 2u)));
1011 if (door_terminator != 0xFFFFu) {
1012 return absl::DataLossError(
1013 "Door list does not end with the expected 0xFFFF terminator");
1022 model_door.
type != raw_door.
type || raw_x != x || raw_y != y) {
1023 return absl::DataLossError(
1024 "Door model does not match the raw door-pointer entry");
1027 const uint32_t type_pc =
static_cast<uint32_t
>(door_entry_pc + 1u);
1028 const auto conflicts =
1030 if (!conflicts.empty()) {
1031 return absl::PermissionDeniedError(absl::StrFormat(
1032 "Door type byte at PC 0x%06X conflicts with Hack Manifest (%s)",
1038 target.
door = model_door;
1042 target.
door_entry_pc =
static_cast<uint32_t
>(door_entry_pc);
1054 std::string manifest_path;
1062 if (manifest_layout ==
nullptr) {
1063 return absl::FailedPreconditionError(
1064 "Manifest does not define dungeon_stream_regions.pot_items");
1075 uint32_t entry_pc = 0;
1076 uint32_t item_pc = 0;
1080 uint32_t pointer_slot_pc = 0;
1081 uint32_t stream_pc = 0;
1082 uint32_t stream_end_pc = 0;
1088 if (rom ==
nullptr || !rom->
is_loaded()) {
1089 return absl::InvalidArgumentError(
"ROM not loaded");
1092 const uint64_t pointer_table_end =
1095 if (pointer_table_end > rom->
size()) {
1096 return absl::OutOfRangeError(
1097 "Complete pot-item pointer table is outside ROM");
1099 const uint32_t pointer_slot_pc =
1101 uint16_t pointer_word = 0;
1103 rom->
ReadWord(
static_cast<int>(pointer_slot_pc)));
1104 const uint32_t pointer_snes = 0x010000u | pointer_word;
1106 return absl::FailedPreconditionError(
1107 "Selected pot-item pointer is not a valid bank-01 LoROM pointer");
1109 const uint32_t stream_pc =
SnesToPc(pointer_snes);
1110 const uint64_t bank_end =
1111 (
static_cast<uint64_t
>(stream_pc) / 0x8000u + 1u) * 0x8000u;
1112 const uint32_t parse_end =
1113 static_cast<uint32_t
>(std::min<uint64_t>(rom->
size(), bank_end));
1114 if (stream_pc >= parse_end) {
1115 return absl::OutOfRangeError(
"Selected pot-item stream is outside ROM");
1122 uint32_t cursor = stream_pc;
1124 if (
static_cast<uint64_t
>(cursor) + 2u > parse_end) {
1125 return absl::DataLossError(
"Pot-item stream has no 0xFFFF terminator");
1127 uint16_t position = 0;
1129 if (position == 0xFFFFu) {
1133 if (
static_cast<uint64_t
>(cursor) + 3u > parse_end) {
1134 return absl::DataLossError(
"Pot-item stream has a truncated record");
1138 view.
entries.push_back({.index =
static_cast<int>(view.
entries.size()),
1139 .item = {.position = position, .item = item},
1141 .item_pc = cursor + 2u});
1149 return absl::DataLossError(
1150 "Pot-item room model count does not match the raw stream");
1152 for (
size_t index = 0; index < view.
entries.size(); ++index) {
1156 return absl::DataLossError(
1157 "Pot-item room model does not match the raw stream");
1165 uint32_t pointer_slot_pc = 0;
1166 uint32_t stream_pc = 0;
1167 uint32_t stream_end_pc = 0;
1171 Rom* rom,
int room_id,
int index,
1174 return absl::FailedPreconditionError(absl::StrFormat(
1175 "Dungeon pot-item manifest pointer_count must equal %d rooms",
1181 *rom, manifest_context.
layout));
1182 if (
static_cast<size_t>(room_id) >= inventory.
streams.size()) {
1183 return absl::FailedPreconditionError(
1184 "Selected room is absent from the pot-item stream inventory");
1186 if (!inventory.
issues.empty()) {
1187 const auto& issue = inventory.
issues.front();
1188 return absl::FailedPreconditionError(absl::StrFormat(
1189 "Pot-item stream inventory is invalid at room 0x%03X: %s",
1190 issue.room_id, issue.message));
1193 const auto& stream = inventory.
streams[room_id];
1194 if (!stream.valid) {
1195 return absl::FailedPreconditionError(
"Selected pot-item stream is invalid");
1197 for (
const auto& alias : inventory.
aliases) {
1199 return absl::FailedPreconditionError(absl::StrFormat(
1200 "Selected pot-item stream at PC 0x%06X is shared by %d rooms",
1201 alias.data_pc,
static_cast<int>(alias.room_ids.size())));
1204 for (
const auto& overlap : inventory.
overlaps) {
1207 return absl::FailedPreconditionError(absl::StrFormat(
1208 "Selected pot-item stream overlaps PC range [0x%06X, 0x%06X)",
1209 overlap.intersection.begin, overlap.intersection.end));
1212 for (
const auto& other_stream : inventory.
streams) {
1213 if (other_stream.room_id ==
static_cast<uint32_t
>(room_id)) {
1216 if (other_stream.data_pc >= stream.data_pc &&
1217 other_stream.data_pc < stream.logical_end_pc) {
1218 return absl::FailedPreconditionError(absl::StrFormat(
1219 "Room 0x%03X pot-item pointer PC 0x%06X starts inside selected "
1220 "pot-item stream [0x%06X, 0x%06X)",
1221 other_stream.room_id, other_stream.data_pc, stream.data_pc,
1222 stream.logical_end_pc));
1231 runtime.
entries.size() * 3u + 2u != stream.encoded_stream.size()) {
1232 return absl::DataLossError(
1233 "Runtime pot-item stream does not match the manifest inventory");
1235 if (index < 0 || index >=
static_cast<int>(runtime.
entries.size())) {
1236 return absl::OutOfRangeError(
1237 absl::StrFormat(
"Pot-item index %d out of range (room has %d entries)",
1238 index,
static_cast<int>(runtime.
entries.size())));
1244 if (!conflicts.empty()) {
1245 return absl::PermissionDeniedError(absl::StrFormat(
1246 "Pot-item byte at PC 0x%06X conflicts with Hack Manifest (%s)",
1268 auto room_id_str_or = GetRequiredString(parser,
"room");
1269 if (!room_id_str_or.ok()) {
1270 return room_id_str_or.status();
1272 auto sprite_id_str_or = GetRequiredString(parser,
"id");
1273 if (!sprite_id_str_or.ok()) {
1274 return sprite_id_str_or.status();
1276 const std::string room_id_str = room_id_str_or.value();
1277 const std::string sprite_id_str = sprite_id_str_or.value();
1279 int room_id, sprite_id;
1280 if (!ParseHexString(room_id_str, &room_id)) {
1281 return absl::InvalidArgumentError(
"Invalid room ID. Must be hex.");
1283 if (!ParseHexString(sprite_id_str, &sprite_id)) {
1284 return absl::InvalidArgumentError(
"Invalid sprite ID. Must be hex.");
1286 auto room_status = ValidateRoomId(room_id);
1287 if (!room_status.ok()) {
1291 auto x_or = GetRequiredInt(parser,
"x");
1293 return x_or.status();
1295 auto y_or = GetRequiredInt(parser,
"y");
1297 return y_or.status();
1299 auto subtype_or = GetOptionalInt(parser,
"subtype", 0);
1300 if (!subtype_or.ok()) {
1301 return subtype_or.status();
1303 auto layer_or = GetOptionalInt(parser,
"layer", 0);
1304 if (!layer_or.ok()) {
1305 return layer_or.status();
1308 int x = x_or.value();
1309 int y = y_or.value();
1310 int subtype = subtype_or.value();
1311 int layer = layer_or.value();
1312 bool do_write = parser.
HasFlag(
"write");
1317 if (sprite_id < 0 || sprite_id > 0xFF) {
1318 return absl::InvalidArgumentError(
"Sprite ID must be 0x00-0xFF");
1320 if (subtype < 0 || subtype > 0x1F) {
1321 return absl::InvalidArgumentError(
"Subtype must be 0-31 (5-bit flags)");
1323 if (layer < 0 || layer > 1) {
1324 return absl::InvalidArgumentError(
"Layer must be 0 or 1");
1327 RETURN_IF_ERROR(ValidateSpriteEncoding(sprite_id, x, y, subtype, layer));
1328 std::optional<StreamSaveManifestContext> manifest_context;
1332 const auto* layout = manifest_context.has_value()
1333 ? &manifest_context->allocator_layout
1340 int count_before =
static_cast<int>(room.
GetSprites().size());
1344 static_cast<uint8_t
>(sprite_id),
static_cast<uint8_t
>(x),
1345 static_cast<uint8_t
>(y),
static_cast<uint8_t
>(subtype),
1346 static_cast<uint8_t
>(layer));
1355 formatter.
AddField(
"subtype", subtype);
1356 formatter.
AddField(
"layer", layer);
1357 formatter.
AddField(
"sprites_before", count_before);
1358 formatter.
AddField(
"sprites_after",
1360 formatter.
AddField(
"mode", do_write ?
"write" :
"dry-run");
1362 if (
const auto manifest_path = parser.
GetString(
"manifest");
1363 manifest_path.has_value()) {
1364 formatter.
AddField(
"manifest", *manifest_path);
1365 formatter.
AddField(
"manifest_write_policy",
"block");
1367 formatter.
AddField(
"allocator_capability",
1368 layout !=
nullptr ?
"copy_on_write" :
"none");
1369 const auto preview = PreflightSpriteSave(
1370 *rom, room_id, room,
1371 manifest_context.has_value() ? &*manifest_context :
nullptr);
1372 if (!preview.ok()) {
1373 formatter.
AddField(
"preflight_status",
"failed");
1374 formatter.
AddField(
"preflight_error",
1375 std::string(preview.status().message()));
1377 return preview.status();
1379 formatter.
AddField(
"preflight_status",
"passed");
1380 formatter.
AddField(
"storage_strategy",
1381 preview->source_data_pc == preview->planned_data_pc
1384 formatter.
AddHexField(
"source_data_pc", preview->source_data_pc, 6);
1385 formatter.
AddHexField(
"planned_data_pc", preview->planned_data_pc, 6);
1387 for (
const auto& [begin, end] : preview->write_ranges) {
1396 auto save_status = MutateAndSaveRomWithBackup(
1397 rom, formatter, [&room, layout]() {
return room.
SaveSprites(layout); });
1398 if (!save_status.ok()) {
1405 return absl::OkStatus();
1415 auto room_id_str_or = GetRequiredString(parser,
"room");
1416 if (!room_id_str_or.ok()) {
1417 return room_id_str_or.status();
1419 const std::string room_id_str = room_id_str_or.value();
1422 if (!ParseHexString(room_id_str, &room_id)) {
1423 return absl::InvalidArgumentError(
"Invalid room ID. Must be hex.");
1425 auto room_status = ValidateRoomId(room_id);
1426 if (!room_status.ok()) {
1430 bool do_write = parser.
HasFlag(
"write");
1437 int count_before =
static_cast<int>(sprites.size());
1440 const bool has_index = parser.
GetString(
"index").has_value();
1441 const bool has_x = parser.
GetString(
"x").has_value();
1442 const bool has_y = parser.
GetString(
"y").has_value();
1443 if (has_index && (has_x || has_y)) {
1444 return absl::InvalidArgumentError(
1445 "Use either --index or --x/--y, not both");
1447 if (!has_index && has_x != has_y) {
1448 return absl::InvalidArgumentError(
1449 "Both --x and --y are required when removing by position");
1451 if (!has_index && !has_x) {
1452 return absl::InvalidArgumentError(
1453 "Either --index or both --x and --y are required");
1456 int remove_index = -1;
1458 auto index_or = GetRequiredInt(parser,
"index");
1459 if (!index_or.ok()) {
1460 return index_or.status();
1462 remove_index = index_or.value();
1464 auto x_or = GetRequiredInt(parser,
"x");
1466 return x_or.status();
1468 auto y_or = GetRequiredInt(parser,
"y");
1470 return y_or.status();
1473 const int x = x_or.value();
1474 const int y = y_or.value();
1478 for (
int i = 0; i < static_cast<int>(sprites.size()); ++i) {
1479 if (sprites[i].x() == x && sprites[i].y() == y) {
1484 if (remove_index < 0) {
1485 return absl::NotFoundError(absl::StrFormat(
1486 "No sprite at (%d, %d) in room 0x%02X", x, y, room_id));
1490 if (remove_index < 0 || remove_index >=
static_cast<int>(sprites.size())) {
1491 return absl::OutOfRangeError(
1492 absl::StrFormat(
"Sprite index %d out of range (room has %d sprites)",
1493 remove_index, count_before));
1497 const auto& target = sprites[remove_index];
1500 formatter.
AddField(
"removed_index", remove_index);
1501 formatter.
AddHexField(
"sprite_id", target.id(), 2);
1503 formatter.
AddField(
"x", target.x());
1504 formatter.
AddField(
"y", target.y());
1505 formatter.
AddField(
"sprites_before", count_before);
1508 sprites.erase(sprites.begin() + remove_index);
1510 formatter.
AddField(
"sprites_after",
static_cast<int>(sprites.size()));
1511 formatter.
AddField(
"mode", do_write ?
"write" :
"dry-run");
1514 auto save_status = MutateAndSaveRomWithBackup(
1515 rom, formatter, [&room]() {
return room.
SaveSprites(); });
1516 if (!save_status.ok()) {
1523 return absl::OkStatus();
1533 auto room_id_str_or = GetRequiredString(parser,
"room");
1534 if (!room_id_str_or.ok()) {
1535 return room_id_str_or.status();
1537 auto object_id_str_or = GetRequiredString(parser,
"id");
1538 if (!object_id_str_or.ok()) {
1539 return object_id_str_or.status();
1541 const std::string room_id_str = room_id_str_or.value();
1542 const std::string object_id_str = object_id_str_or.value();
1544 int room_id, object_id;
1545 if (!ParseHexString(room_id_str, &room_id)) {
1546 return absl::InvalidArgumentError(
"Invalid room ID. Must be hex.");
1548 if (!ParseHexString(object_id_str, &object_id)) {
1549 return absl::InvalidArgumentError(
"Invalid object ID. Must be hex.");
1551 auto room_status = ValidateRoomId(room_id);
1552 if (!room_status.ok()) {
1555 if (object_id < 0 || object_id > 0xFFFF) {
1556 return absl::InvalidArgumentError(
"Object ID must be 0x0000-0xFFFF");
1559 auto x_or = GetRequiredInt(parser,
"x");
1561 return x_or.status();
1563 auto y_or = GetRequiredInt(parser,
"y");
1565 return y_or.status();
1567 auto layer_or = GetOptionalInt(parser,
"layer", 0);
1568 if (!layer_or.ok()) {
1569 return layer_or.status();
1572 int x = x_or.value();
1573 int y = y_or.value();
1577 if (parser.
GetString(
"size").has_value()) {
1578 auto size_or = parser.
GetInt(
"size");
1579 if (!size_or.ok()) {
1580 return size_or.status();
1582 size = size_or.value();
1584 int layer = layer_or.value();
1585 bool do_write = parser.
HasFlag(
"write");
1588 if (x < 0 || x > 63) {
1589 return absl::InvalidArgumentError(
"X must be 0-63");
1591 if (y < 0 || y > 63) {
1592 return absl::InvalidArgumentError(
"Y must be 0-63");
1594 if (layer < 0 || layer > 2) {
1595 return absl::InvalidArgumentError(
"Layer must be 0, 1, or 2");
1597 if (size < 0 || size > 0xFF) {
1598 return absl::InvalidArgumentError(
"Size must be 0-255");
1600 const uint8_t canonical_size =
1602 if (size != canonical_size) {
1603 return absl::InvalidArgumentError(absl::StrFormat(
1604 "Size %d is not encodable for object 0x%03X; expected %d", size,
1605 object_id, canonical_size));
1609 const auto chest_item_arg = parser.
GetString(
"chest-item");
1610 if (is_chest && !chest_item_arg.has_value()) {
1611 return absl::InvalidArgumentError(
1612 "Stateful chest placement requires --chest-item <hex receipt>");
1614 if (!is_chest && chest_item_arg.has_value()) {
1615 return absl::InvalidArgumentError(
1616 "--chest-item is only valid for stateful chest objects");
1619 if (chest_item_arg.has_value()) {
1621 if (chest_item < 0 || chest_item > 0xFF) {
1622 return absl::InvalidArgumentError(
"Chest receipt must be 0x00-0xFF");
1626 std::optional<StreamSaveManifestContext> manifest_context;
1631 manifest_context.has_value() ? &manifest_context->allocator_layout
1637 int count_before =
static_cast<int>(room.
GetTileObjects().size());
1641 static_cast<uint8_t
>(x),
static_cast<uint8_t
>(y),
1642 static_cast<uint8_t
>(size),
1643 static_cast<uint8_t
>(layer));
1645 std::optional<std::vector<chest_data>> planned_chests;
1648 auto candidate = original;
1649 candidate.push_back(obj);
1650 std::vector<std::optional<size_t>> source_indices;
1651 source_indices.reserve(candidate.size());
1652 for (
size_t i = 0; i < original.size(); ++i) {
1653 source_indices.emplace_back(i);
1655 source_indices.emplace_back(std::nullopt);
1656 std::vector<std::optional<chest_data>> overrides(candidate.size());
1658 chest_data{
static_cast<uint8_t
>(chest_item), object_id == 0xFB1};
1661 source_indices, overrides));
1672 formatter.
AddField(
"object_type", type);
1676 formatter.
AddField(
"layer", layer);
1677 formatter.
AddField(
"objects_before", count_before);
1678 if (
const auto manifest_path = parser.
GetString(
"manifest");
1679 manifest_path.has_value()) {
1680 formatter.
AddField(
"manifest", *manifest_path);
1681 formatter.
AddField(
"manifest_write_policy",
"block");
1683 formatter.
AddField(
"allocator_capability",
1684 layout !=
nullptr ?
"copy_on_write" :
"none");
1688 if (!add_status.ok()) {
1689 formatter.
AddField(
"error", std::string(add_status.message()));
1693 if (planned_chests.has_value()) {
1694 room.
GetChests() = std::move(*planned_chests);
1696 formatter.
AddHexField(
"chest_item", chest_item, 2);
1702 formatter.
AddField(
"objects_after",
1704 formatter.
AddField(
"mode", do_write ?
"write" :
"dry-run");
1706 const auto preflight_status = PreflightObjectSave(
1707 *rom, room_id, obj, room,
1708 manifest_context.has_value() ? &*manifest_context :
nullptr);
1709 if (!preflight_status.ok()) {
1710 formatter.
AddField(
"preflight_status",
"failed");
1711 formatter.
AddField(
"preflight_error",
1712 std::string(preflight_status.message()));
1714 return preflight_status;
1716 formatter.
AddField(
"preflight_status",
"success");
1719 auto save_status = MutateAndSaveRomWithBackup(
1720 rom, formatter, [&room, rom, room_id, &manifest_context]() {
1721 return SavePlacedObject(
1723 manifest_context.has_value() ? &*manifest_context :
nullptr);
1725 if (!save_status.ok()) {
1732 return absl::OkStatus();
1746 int remove_index = 0;
1748 if (remove_index < 0) {
1749 return absl::InvalidArgumentError(
"Object index must be non-negative");
1752 int expected_id = 0;
1758 int expected_size = 0;
1760 int expected_layer = 0;
1763 if (expected_id < 0 || expected_id > 0xFFF) {
1764 return absl::InvalidArgumentError(
"Expected object ID must be 0x000-0xFFF");
1766 if (expected_x < 0 || expected_x > 63) {
1767 return absl::InvalidArgumentError(
"Expected X must be 0-63");
1769 if (expected_y < 0 || expected_y > 63) {
1770 return absl::InvalidArgumentError(
"Expected Y must be 0-63");
1772 if (expected_size < 0 || expected_size > 0xFF) {
1773 return absl::InvalidArgumentError(
"Expected size must be 0-255");
1775 if (expected_layer < 0 || expected_layer > 2) {
1776 return absl::InvalidArgumentError(
"Expected layer must be 0, 1, or 2");
1779 std::optional<StreamSaveManifestContext> manifest_context;
1784 manifest_context.has_value() ? &manifest_context->allocator_layout
1789 int object_vector_index = -1;
1790 int stream_object_count = 0;
1791 for (
int i = 0; i < static_cast<int>(objects.size()); ++i) {
1795 if (stream_object_count == remove_index) {
1796 object_vector_index = i;
1798 ++stream_object_count;
1800 if (object_vector_index < 0) {
1801 return absl::OutOfRangeError(
1802 absl::StrFormat(
"Object index %d out of range (room has %d objects)",
1803 remove_index, stream_object_count));
1806 const auto& target = objects[object_vector_index];
1809 return absl::FailedPreconditionError(
1810 "Removing chest objects requires coordinated chest-table editing");
1813 if (target.id_ != expected_id || target.x() != expected_x ||
1814 target.y() != expected_y || target.size_ != expected_size ||
1815 target.GetLayerValue() != expected_layer) {
1816 return absl::FailedPreconditionError(
1817 absl::StrFormat(
"Object guard mismatch at index %d: expected 0x%03X at "
1818 "(%d,%d) size=%d layer=%d; found 0x%03X at (%d,%d) "
1820 remove_index, expected_id, expected_x, expected_y,
1821 expected_size, expected_layer, target.id_, target.x(),
1822 target.y(), target.size_, target.GetLayerValue()));
1825 const int object_id = target.id_;
1826 const int object_x = target.x();
1827 const int object_y = target.y();
1828 const int object_size = target.size_;
1829 const int object_layer = target.GetLayerValue();
1830 const auto encoded = target.EncodeObjectToBytes();
1831 const int object_type =
1836 formatter.
AddField(
"removed_index", remove_index);
1839 formatter.
AddField(
"object_type", object_type);
1842 formatter.
AddField(
"size", object_size);
1843 formatter.
AddField(
"layer", object_layer);
1844 formatter.
AddField(
"objects_before", stream_object_count);
1845 if (
const auto manifest_path = parser.
GetString(
"manifest");
1846 manifest_path.has_value()) {
1847 formatter.
AddField(
"manifest", *manifest_path);
1848 formatter.
AddField(
"manifest_write_policy",
"block");
1850 formatter.
AddField(
"allocator_capability",
1851 layout !=
nullptr ?
"copy_on_write" :
"none");
1853 const absl::Status remove_status = room.
RemoveObject(object_vector_index);
1854 if (!remove_status.ok()) {
1855 formatter.
AddField(
"remove_error", std::string(remove_status.message()));
1857 return remove_status;
1859 formatter.
AddField(
"objects_after", stream_object_count - 1);
1860 const bool do_write = parser.
HasFlag(
"write");
1861 formatter.
AddField(
"mode", do_write ?
"write" :
"dry-run");
1863 const absl::Status preflight_status = PreflightObjectRemoval(
1864 *rom, room_id, object_vector_index, room,
1865 manifest_context.has_value() ? &*manifest_context :
nullptr);
1866 if (!preflight_status.ok()) {
1867 formatter.
AddField(
"preflight_status",
"failed");
1868 formatter.
AddField(
"preflight_error",
1869 std::string(preflight_status.message()));
1871 return preflight_status;
1873 formatter.
AddField(
"preflight_status",
"success");
1876 const absl::Status save_status = MutateAndSaveRomWithBackup(
1877 rom, formatter, [&room, layout]() {
return room.
SaveObjects(layout); });
1878 if (!save_status.ok()) {
1885 return absl::OkStatus();
1899 std::optional<int> requested_index;
1900 if (parser.
GetString(
"index").has_value()) {
1901 int color_index = 0;
1904 requested_index = color_index;
1907 DungeonPaletteSelection selection;
1908 ASSIGN_OR_RETURN(selection, ResolveDungeonPaletteSelection(*rom, room_id));
1911 AddDungeonPaletteScope(formatter, selection);
1912 formatter.
AddField(
"color_count", kDungeonPaletteColorCount);
1914 if (requested_index.has_value()) {
1915 const uint32_t color_pc =
1916 DungeonPaletteColorPc(selection.selected, *requested_index);
1920 formatter.
AddField(
"color_index", *requested_index);
1926 for (
int color_index = 0; color_index < kDungeonPaletteColorCount;
1928 const uint32_t color_pc =
1929 DungeonPaletteColorPc(selection.selected, color_index);
1934 formatter.
AddField(
"index", color_index);
1943 formatter.
AddField(
"status",
"success");
1945 return absl::OkStatus();
1955 const bool do_write = parser.
HasFlag(
"write");
1956#if defined(__EMSCRIPTEN__)
1958 return absl::FailedPreconditionError(
1959 "dungeon-set-palette-color --write is unavailable in the browser "
1960 "terminal because durable filesystem sync cannot be guaranteed; use "
1961 "native z3ed on a disposable ROM copy");
1964 if (do_write && rom->
filename().empty()) {
1965 return absl::FailedPreconditionError(
1966 "dungeon-set-palette-color --write requires a ROM loaded from a "
1971 int color_index = 0;
1972 int expected_palette_set = 0;
1973 int expected_palette_index = 0;
1974 int expected_color = 0;
1975 int replacement_color = 0;
1979 GetRequiredHex(parser,
"expect-palette-set"));
1981 GetRequiredInt(parser,
"expect-palette-index"));
1987 if (expected_palette_set < 0 || expected_palette_set > 0xFF) {
1988 return absl::InvalidArgumentError(
1989 "--expect-palette-set must be an 8-bit value in range 0x00-0xFF");
1991 if (expected_palette_index < 0 ||
1992 expected_palette_index >= kDungeonPaletteCount) {
1993 return absl::InvalidArgumentError(
1994 absl::StrFormat(
"--expect-palette-index must be in range 0-%d",
1995 kDungeonPaletteCount - 1));
1997 RETURN_IF_ERROR(ValidateSnes15BitColor(expected_color,
"expect-color"));
2000 const std::string manifest_path = parser.
GetString(
"manifest").value();
2004 DungeonPaletteSelection selection;
2005 ASSIGN_OR_RETURN(selection, ResolveDungeonPaletteSelection(*rom, room_id));
2006 if (selection.selected.palette_set_id != expected_palette_set) {
2007 return absl::FailedPreconditionError(absl::StrFormat(
2008 "Palette-set CAS failed for room 0x%03X: expected full 8-bit ID "
2009 "0x%02X, got 0x%02X",
2010 room_id, expected_palette_set, selection.selected.palette_set_id));
2012 if (selection.selected.palette_index != expected_palette_index) {
2013 return absl::FailedPreconditionError(absl::StrFormat(
2014 "Resolved palette CAS failed for room 0x%03X: expected %d, got %d",
2015 room_id, expected_palette_index, selection.selected.palette_index));
2018 uint16_t actual_color = 0;
2020 *rom, selection.selected, color_index));
2021 if (actual_color != expected_color) {
2022 return absl::FailedPreconditionError(absl::StrFormat(
2023 "Color CAS failed at palette %d index %d: expected 0x%04X, got "
2025 selection.selected.palette_index, color_index, expected_color,
2028 if (replacement_color == actual_color) {
2029 return absl::InvalidArgumentError(absl::StrFormat(
2030 "Refusing no-op dungeon palette write: color is already 0x%04X",
2034 const uint32_t color_pc =
2035 DungeonPaletteColorPc(selection.selected, color_index);
2036 RETURN_IF_ERROR(ValidateDungeonPaletteManifestRange(manifest, color_pc));
2041 formatter.
BeginObject(
"Set Dungeon Palette Color");
2042 AddDungeonPaletteScope(formatter, selection);
2043 formatter.
AddField(
"manifest", manifest_path);
2044 formatter.
AddField(
"manifest_write_policy",
"block");
2045 formatter.
AddField(
"mode", do_write ?
"write" :
"dry-run");
2046 formatter.
AddField(
"color_index", color_index);
2049 formatter.
AddHexField(
"old_color", actual_color, 4);
2050 formatter.
AddHexField(
"new_color", replacement_color, 4);
2051 formatter.
AddField(
"preflight_status",
"success");
2054 formatter.
AddField(
"status",
"dry-run");
2056 return absl::OkStatus();
2061 write_fence.
Allow(color_pc, color_pc + 2u,
"DungeonPaletteColor"));
2062 const std::vector<uint8_t> before = rom->
vector();
2067 rom->
WriteShort(color_pc,
static_cast<uint16_t
>(replacement_color)));
2070 std::vector<uint32_t> changed_bytes;
2072 ValidateDungeonPaletteWholeRomDiff(before, *rom, color_pc));
2073 uint16_t memory_color = 0;
2075 *rom, selection.selected, color_index));
2076 if (memory_color != replacement_color) {
2077 return absl::DataLossError(
2078 "In-memory dungeon palette readback did not match the replacement");
2080 formatter.
AddField(
"write_status",
"success");
2081 formatter.
AddField(
"whole_rom_diff_status",
"verified");
2082 formatter.
AddField(
"changed_byte_count",
2083 static_cast<int>(changed_bytes.size()));
2085 for (uint32_t changed_pc : changed_bytes) {
2086 formatter.
AddArrayItem(absl::StrFormat(
"0x%06X", changed_pc));
2089 formatter.
AddField(
"memory_readback_status",
"verified");
2091 const absl::Status save_status = SaveRomWithBackup(rom, formatter);
2092 if (!save_status.ok()) {
2093 formatter.
AddField(
"status",
"error");
2102 const absl::Status reopen_status = VerifyDungeonPaletteExternalReadback(
2103 *rom, room_id, expected_palette_set, expected_palette_index, color_index,
2104 static_cast<uint16_t
>(replacement_color));
2105 if (!reopen_status.ok()) {
2106 formatter.
AddField(
"readback_status",
"external_reopen_failed");
2107 formatter.
AddField(
"readback_error", std::string(reopen_status.message()));
2108 formatter.
AddField(
"status",
"error");
2110 return reopen_status;
2113 formatter.
AddField(
"readback_status",
"external_reopen_verified");
2114 formatter.
AddField(
"status",
"success");
2116 return absl::OkStatus();
2134 if (x < 0 || x > 63 || y < 0 || y > 63) {
2135 return absl::InvalidArgumentError(
2136 "Door tile coordinates must be in range 0-63");
2140 int expected_type = 0;
2146 std::optional<StreamSaveManifestContext> manifest_context;
2150 if (!manifest_context.has_value()) {
2151 return absl::FailedPreconditionError(
2152 "--manifest is required for dungeon-set-door-type");
2155 DoorTypeTarget target;
2157 target, ResolveDoorTypeTarget(rom, room_id, x, y, *manifest_context));
2158 const int old_type =
static_cast<int>(target.door.type);
2159 if (old_type != expected_type) {
2160 return absl::FailedPreconditionError(absl::StrFormat(
2161 "Door type compare-and-swap failed at (%d, %d): expected 0x%02X, "
2163 x, y, expected_type, old_type));
2165 if (new_type == old_type) {
2166 return absl::InvalidArgumentError(
2167 "--type must differ from the current door type");
2170 const bool do_write = parser.
HasFlag(
"write");
2171 if (do_write && rom->
filename().empty()) {
2172 return absl::FailedPreconditionError(
"Write mode requires a ROM filename");
2177 formatter.
AddField(
"door_index", target.door_index);
2180 formatter.
AddField(
"position", target.door.position);
2181 formatter.
AddField(
"direction", std::string(target.door.GetDirectionName()));
2183 formatter.
AddField(
"old_type_name", std::string(target.door.GetTypeName()));
2189 formatter.
AddHexField(
"object_stream_pc", target.object_stream_pc, 6);
2190 formatter.
AddHexField(
"object_stream_end_pc", target.object_stream_end_pc, 6);
2191 formatter.
AddHexField(
"door_pointer_pc", target.door_pointer_pc, 6);
2192 formatter.
AddHexField(
"door_entry_pc", target.door_entry_pc, 6);
2193 formatter.
AddHexField(
"type_pc", target.type_pc, 6);
2196 formatter.
AddField(
"manifest_write_policy",
"block");
2197 formatter.
AddField(
"stream_alias_status",
"unique");
2198 formatter.
AddField(
"mode", do_write ?
"write" :
"dry-run");
2199 formatter.
AddField(
"preflight_status",
"success");
2202 formatter.
AddField(
"write_status",
"not_requested");
2203 formatter.
AddField(
"save_status",
"not_requested");
2204 formatter.
AddField(
"readback_status",
"not_requested");
2205 formatter.
AddField(
"status",
"success");
2207 return absl::OkStatus();
2210 const auto finish_error = [&formatter](
const absl::Status& status,
2211 const char* field) -> absl::Status {
2212 formatter.
AddField(
"status",
"error");
2213 formatter.
AddField(field, std::string(status.message()));
2217 const auto verify_readback =
2218 [&target, new_type](
const DoorTypeTarget& readback) -> absl::Status {
2219 if (readback.door_index != target.door_index ||
2220 readback.type_pc != target.type_pc ||
2221 readback.door.position != target.door.position ||
2222 readback.door.direction != target.door.direction ||
2223 static_cast<int>(readback.door.type) != new_type) {
2224 return absl::DataLossError(
2225 "Door type readback does not match the requested change");
2227 return absl::OkStatus();
2231 const absl::Status allow_status = write_fence.
Allow(
2232 target.type_pc, target.type_pc + 1u,
"dungeon door type");
2233 if (!allow_status.ok()) {
2234 return finish_error(allow_status,
"write_error");
2237 const std::vector<uint8_t> before = rom->
vector();
2241 const absl::Status write_status = rom->
WriteByte(
2242 static_cast<int>(target.type_pc),
static_cast<uint8_t
>(new_type));
2243 if (!write_status.ok()) {
2244 return finish_error(write_status,
"write_error");
2248 if (rom->
size() != before.size()) {
2249 return finish_error(
2250 absl::DataLossError(
"Door type edit unexpectedly resized the ROM"),
2253 int changed_bytes = 0;
2254 uint32_t changed_pc = 0;
2255 for (uint32_t pc = 0; pc < before.size(); ++pc) {
2256 if (before[pc] != rom->
data()[pc]) {
2261 if (changed_bytes != 1 || changed_pc != target.type_pc) {
2262 return finish_error(
2263 absl::DataLossError(absl::StrFormat(
2264 "Door type edit changed %d bytes; expected only PC 0x%06X",
2265 changed_bytes, target.type_pc)),
2269 auto memory_readback =
2270 ResolveDoorTypeTarget(rom, room_id, x, y, *manifest_context);
2271 if (!memory_readback.ok()) {
2272 return finish_error(memory_readback.status(),
"readback_error");
2274 const absl::Status memory_verify = verify_readback(*memory_readback);
2275 if (!memory_verify.ok()) {
2276 return finish_error(memory_verify,
"readback_error");
2278 formatter.
AddField(
"readback_status",
"pre_save_verified");
2279 formatter.
AddField(
"write_status",
"success");
2281 const absl::Status save_status = SaveRomWithBackup(rom, formatter);
2282 if (!save_status.ok()) {
2283 formatter.
AddField(
"status",
"error");
2289 formatter.
AddField(
"status",
"success");
2291 return absl::OkStatus();
2305 PotItemStreamView stream;
2310 formatter.
AddHexField(
"pointer_slot_pc", stream.pointer_slot_pc, 6);
2311 formatter.
AddHexField(
"stream_pc", stream.stream_pc, 6);
2312 formatter.
AddHexField(
"stream_end_pc", stream.stream_end_pc, 6);
2313 formatter.
AddField(
"item_count",
static_cast<int>(stream.entries.size()));
2315 for (
const PotItemEntryView& entry : stream.entries) {
2317 formatter.
AddField(
"index", entry.index);
2318 formatter.
AddHexField(
"position", entry.item.position, 4);
2319 formatter.
AddField(
"tile_x", entry.item.GetTileX());
2320 formatter.
AddField(
"tile_y", entry.item.GetTileY());
2321 formatter.
AddHexField(
"item_id", entry.item.item, 2);
2322 formatter.
AddHexField(
"item_pc", entry.item_pc, 6);
2327 formatter.
AddField(
"status",
"success");
2329 return absl::OkStatus();
2341 int expected_position = 0;
2342 int expected_item = 0;
2348 GetRequiredHex(parser,
"expect-position"));
2351 if (expected_position < 0 || expected_position > 0xFFFF) {
2352 return absl::InvalidArgumentError(
2353 "--expect-position must be in range 0x0000-0xFFFF");
2355 if (expected_item < 0 || expected_item > 0xFF) {
2356 return absl::InvalidArgumentError(
2357 "--expect-item must be in range 0x00-0xFF");
2359 if (new_item < 0 || new_item > 0xFF) {
2360 return absl::InvalidArgumentError(
"--item must be in range 0x00-0xFF");
2363 PotItemManifestContext manifest_context;
2365 PotItemTarget target;
2367 ResolvePotItemTarget(rom, room_id, index, manifest_context));
2368 if (target.entry.item.position != expected_position ||
2369 target.entry.item.item != expected_item) {
2370 return absl::FailedPreconditionError(absl::StrFormat(
2371 "Pot-item compare-and-swap failed at index %d: expected "
2372 "position/item 0x%04X/0x%02X, found 0x%04X/0x%02X",
2373 index, expected_position, expected_item, target.entry.item.position,
2374 target.entry.item.item));
2376 if (new_item == expected_item) {
2377 return absl::InvalidArgumentError(
2378 "--item must differ from the current pot item");
2381 const bool do_write = parser.
HasFlag(
"write");
2382 if (do_write && rom->
filename().empty()) {
2383 return absl::FailedPreconditionError(
"Write mode requires a ROM filename");
2388 formatter.
AddField(
"index", index);
2389 formatter.
AddHexField(
"position", target.entry.item.position, 4);
2390 formatter.
AddField(
"tile_x", target.entry.item.GetTileX());
2391 formatter.
AddField(
"tile_y", target.entry.item.GetTileY());
2392 formatter.
AddHexField(
"old_item", expected_item, 2);
2394 formatter.
AddHexField(
"pointer_slot_pc", target.pointer_slot_pc, 6);
2395 formatter.
AddHexField(
"stream_pc", target.stream_pc, 6);
2396 formatter.
AddHexField(
"stream_end_pc", target.stream_end_pc, 6);
2397 formatter.
AddHexField(
"item_pc", target.entry.item_pc, 6);
2400 formatter.
AddField(
"manifest_write_policy",
"block");
2401 formatter.
AddField(
"stream_alias_status",
"unique");
2402 formatter.
AddField(
"mode", do_write ?
"write" :
"dry-run");
2403 formatter.
AddField(
"preflight_status",
"success");
2406 formatter.
AddField(
"write_status",
"not_requested");
2407 formatter.
AddField(
"save_status",
"not_requested");
2408 formatter.
AddField(
"readback_status",
"not_requested");
2409 formatter.
AddField(
"status",
"success");
2411 return absl::OkStatus();
2414 const auto finish_error = [&formatter](
const absl::Status& status,
2415 const char* field) -> absl::Status {
2416 formatter.
AddField(
"status",
"error");
2417 formatter.
AddField(field, std::string(status.message()));
2421 const auto verify_readback =
2422 [&target, new_item](
const PotItemTarget& readback) -> absl::Status {
2423 if (readback.entry.index != target.entry.index ||
2424 readback.entry.entry_pc != target.entry.entry_pc ||
2425 readback.entry.item_pc != target.entry.item_pc ||
2426 readback.entry.item.position != target.entry.item.position ||
2427 readback.entry.item.item != new_item ||
2428 readback.stream_pc != target.stream_pc ||
2429 readback.stream_end_pc != target.stream_end_pc) {
2430 return absl::DataLossError(
2431 "Pot-item readback does not match the requested change");
2433 return absl::OkStatus();
2437 const absl::Status allow_status = write_fence.
Allow(
2438 target.entry.item_pc, target.entry.item_pc + 1u,
"dungeon pot item");
2439 if (!allow_status.ok()) {
2440 return finish_error(allow_status,
"write_error");
2443 const std::vector<uint8_t> before = rom->
vector();
2447 const absl::Status write_status = rom->
WriteByte(
2448 static_cast<int>(target.entry.item_pc),
static_cast<uint8_t
>(new_item));
2449 if (!write_status.ok()) {
2450 return finish_error(write_status,
"write_error");
2454 if (rom->
size() != before.size()) {
2455 return finish_error(
2456 absl::DataLossError(
"Pot-item edit unexpectedly resized the ROM"),
2459 int changed_bytes = 0;
2460 uint32_t changed_pc = 0;
2461 for (uint32_t pc = 0; pc < before.size(); ++pc) {
2462 if (before[pc] != rom->
data()[pc]) {
2467 if (changed_bytes != 1 || changed_pc != target.entry.item_pc) {
2468 return finish_error(
2469 absl::DataLossError(absl::StrFormat(
2470 "Pot-item edit changed %d bytes; expected only PC 0x%06X",
2471 changed_bytes, target.entry.item_pc)),
2475 auto memory_readback =
2476 ResolvePotItemTarget(rom, room_id, index, manifest_context);
2477 if (!memory_readback.ok()) {
2478 return finish_error(memory_readback.status(),
"readback_error");
2480 const absl::Status memory_verify = verify_readback(*memory_readback);
2481 if (!memory_verify.ok()) {
2482 return finish_error(memory_verify,
"readback_error");
2484 formatter.
AddField(
"readback_status",
"pre_save_verified");
2485 formatter.
AddField(
"write_status",
"success");
2487 const absl::Status save_status = SaveRomWithBackup(rom, formatter);
2488 if (!save_status.ok()) {
2489 formatter.
AddField(
"status",
"error");
2495 formatter.
AddField(
"status",
"success");
2497 return absl::OkStatus();
2507 auto room_id_str_or = GetRequiredString(parser,
"room");
2508 if (!room_id_str_or.ok()) {
2509 return room_id_str_or.status();
2511 auto tiles_str_or = GetRequiredString(parser,
"tiles");
2512 if (!tiles_str_or.ok()) {
2513 return tiles_str_or.status();
2515 const std::string room_id_str = room_id_str_or.value();
2516 const std::string tiles_str = tiles_str_or.value();
2519 if (!ParseHexString(room_id_str, &room_id)) {
2520 return absl::InvalidArgumentError(
"Invalid room ID. Must be hex.");
2522 auto room_status = ValidateRoomId(room_id);
2523 if (!room_status.ok()) {
2527 bool do_write = parser.
HasFlag(
"write");
2533 std::vector<TileSpec> specs;
2535 for (absl::string_view entry :
2536 absl::StrSplit(tiles_str,
';', absl::SkipEmpty())) {
2537 std::vector<std::string> parts =
2538 absl::StrSplit(entry,
',', absl::SkipEmpty());
2539 if (parts.size() != 3) {
2540 return absl::InvalidArgumentError(absl::StrFormat(
2541 "Invalid tile spec '%s'. Expected x,y,tile (e.g. 10,5,0xB7)", entry));
2546 if (!absl::SimpleAtoi(parts[0], &spec.x)) {
2547 return absl::InvalidArgumentError(
2548 absl::StrFormat(
"Invalid X coord '%s'", parts[0]));
2550 if (!absl::SimpleAtoi(parts[1], &spec.y)) {
2551 return absl::InvalidArgumentError(
2552 absl::StrFormat(
"Invalid Y coord '%s'", parts[1]));
2554 if (!ParseHexString(parts[2], &spec.tile)) {
2555 return absl::InvalidArgumentError(
2556 absl::StrFormat(
"Invalid tile value '%s'. Must be hex.", parts[2]));
2560 if (spec.x < 0 || spec.x > 63 || spec.y < 0 || spec.y > 63) {
2561 return absl::InvalidArgumentError(absl::StrFormat(
2562 "Tile coords (%d,%d) out of range (0-63)", spec.x, spec.y));
2564 if (spec.tile < 0 || spec.tile > 0xFF) {
2565 return absl::InvalidArgumentError(
"Tile value must be 0x00-0xFF");
2568 specs.push_back(spec);
2571 if (specs.empty()) {
2572 return absl::InvalidArgumentError(
"No tile specs provided");
2581 formatter.
AddField(
"tile_count",
static_cast<int>(specs.size()));
2582 formatter.
AddField(
"mode", do_write ?
"write" :
"dry-run");
2586 for (
const auto& spec : specs) {
2596 formatter.
AddField(
"old_override_present", old_override_present);
2597 formatter.
AddField(
"new_override_present",
2605 MutateAndSaveRomWithBackup(rom, formatter, [&rom, &room]()
mutable {
2607 std::array<zelda3::Room, 1> rooms_arr = {std::move(room)};
2610 if (!save_status.ok()) {
2617 return absl::OkStatus();
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
absl::Status LoadFromFile(const std::string &filename, const LoadOptions &options=LoadOptions::Defaults())
absl::Status WriteByte(int addr, uint8_t value)
absl::StatusOr< uint8_t > ReadByte(int offset) const
const auto & vector() const
absl::Status SaveToFile(const SaveSettings &settings)
absl::StatusOr< uint16_t > ReadWord(int offset) const
absl::Status LoadFromData(const std::vector< uint8_t > &data, const LoadOptions &options=LoadOptions::Defaults())
absl::Status WriteShort(int addr, uint16_t value)
absl::StatusOr< uint32_t > ReadLong(int offset) const
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
Utility for parsing common CLI argument patterns.
std::optional< std::string > GetString(const std::string &name) const
Parse a named argument (e.g., –format=json or –format json)
bool HasFlag(const std::string &name) const
Check if a flag is present.
absl::StatusOr< int > GetInt(const std::string &name) const
Parse an integer argument (supports hex with 0x prefix)
Loads and queries the hack manifest JSON for yaze-ASM integration.
const DungeonStreamLayout * GetDungeonStreamLayout(DungeonStreamType stream) const
Get an explicitly declared dungeon stream layout.
absl::Status LoadFromFile(const std::string &filepath)
Load manifest from a JSON file path.
std::vector< WriteConflict > AnalyzePcWriteRanges(const std::vector< std::pair< uint32_t, uint32_t > > &pc_ranges) const
Analyze a set of PC-offset ranges for write conflicts.
absl::Status Allow(uint32_t start, uint32_t end, std::string_view label)
static int DetermineObjectType(uint8_t b1, uint8_t b3)
bool ArePotItemsLoaded() const
const std::vector< chest_data > & GetChests() const
uint8_t GetCollisionTile(int x, int y) const
absl::Status RemoveObject(size_t index)
const std::vector< Door > & GetDoors() const
absl::StatusOr< std::vector< std::pair< uint32_t, uint32_t > > > GetObjectSaveWriteRanges(const DungeonStreamLayout *layout=nullptr) const
absl::Status SaveObjects(const DungeonStreamLayout *layout=nullptr)
bool HasCollisionOverride(int x, int y) const
absl::StatusOr< std::vector< std::pair< uint32_t, uint32_t > > > GetSpriteSaveWriteRanges(const DungeonStreamLayout *layout=nullptr) const
bool chests_dirty() const
std::vector< uint8_t > EncodeObjects() const
const std::vector< zelda3::Sprite > & GetSprites() const
absl::Status SaveSprites(const DungeonStreamLayout *layout=nullptr)
const std::vector< RoomObject > & GetTileObjects() const
void SetCollisionTile(int x, int y, uint8_t tile)
absl::Status AddObject(const RoomObject &object)
bool has_custom_collision() const
const std::vector< PotItem > & GetPotItems() const
#define ASSIGN_OR_RETURN(type_variable_name, expression)
absl::StatusOr< std::optional< StreamSaveManifestContext > > LoadStreamSaveManifestContext(const resources::ArgumentParser &parser, core::DungeonStreamType stream_type)
absl::StatusOr< std::vector< uint32_t > > ValidateDungeonPaletteWholeRomDiff(const std::vector< uint8_t > &before, const Rom &after, uint32_t color_pc)
absl::StatusOr< uint32_t > ResolveRoomHeaderPcStrict(const Rom &rom, int room_id)
absl::Status ValidateRoomId(int room_id)
absl::StatusOr< DungeonPaletteSelection > ResolveDungeonPaletteSelection(const Rom &rom, int room_id)
absl::Status ValidateDungeonPaletteColorIndex(int color_index)
constexpr uint32_t kDungeonPaletteDataStart
absl::StatusOr< SpriteSavePreview > PreflightSpriteSave(const Rom &source_rom, int room_id, const zelda3::Room &pending_room, const StreamSaveManifestContext *manifest_context)
absl::StatusOr< int > GetOptionalInt(const resources::ArgumentParser &parser, const char *name, int default_value)
absl::Status PreflightObjectRemoval(const Rom &source_rom, int room_id, int object_vector_index, const zelda3::Room &pending_room, const StreamSaveManifestContext *manifest_context)
absl::StatusOr< std::string > GetRequiredString(const resources::ArgumentParser &parser, const char *name)
bool IsLoRomDataPointer(uint32_t snes_address)
absl::Status ValidateSupportedDungeonPaletteRom(const Rom &rom)
constexpr int kDungeonPaletteCount
absl::StatusOr< int > GetRequiredHex(const resources::ArgumentParser &parser, const char *name)
constexpr uint32_t kDungeonPaletteDataEnd
uint32_t DungeonPaletteColorPc(const DungeonPaletteRoomMapping &mapping, int color_index)
constexpr uint32_t kSnesDestinationCodePc
absl::Status ValidateSpriteCoord(int value, char axis)
absl::Status SavePlacedObject(Rom *rom, int room_id, zelda3::Room &room, const StreamSaveManifestContext *manifest_context)
absl::Status ValidateSpriteEncoding(int id, int x, int y, int subtype, int layer)
bool ContainsRoomId(const std::vector< uint32_t > &room_ids, int room_id)
absl::Status ValidateSnes15BitColor(int color, const char *argument_name)
absl::StatusOr< DungeonPaletteRoomMapping > ResolveDungeonPaletteRoomMapping(const Rom &rom, int room_id)
absl::Status ValidateDungeonPaletteManifestRange(const core::HackManifest &manifest, uint32_t color_pc)
void AddDungeonPaletteScope(resources::OutputFormatter &formatter, const DungeonPaletteSelection &selection)
absl::StatusOr< int > GetRequiredInt(const resources::ArgumentParser &parser, const char *name)
absl::Status ValidateObjectSaveManifestConflicts(const Rom &source_rom, int room_id, const zelda3::Room &pending_room, const StreamSaveManifestContext &context)
absl::StatusOr< PotItemStreamView > ResolveRuntimePotItemStream(Rom *rom, int room_id)
absl::Status PreflightObjectSave(const Rom &source_rom, int room_id, const zelda3::RoomObject &object, const zelda3::Room &pending_room, const StreamSaveManifestContext *manifest_context)
absl::StatusOr< uint16_t > ReadDungeonPaletteColor(const Rom &rom, const DungeonPaletteRoomMapping &mapping, int color_index)
absl::Status VerifyDungeonPaletteExternalReadback(const Rom &saved_rom, int room_id, int expected_palette_set, int expected_palette_index, int color_index, uint16_t expected_color)
absl::Status ValidateDungeonPaletteWriteBaseline(const Rom &rom)
absl::StatusOr< PotItemManifestContext > LoadPotItemManifestContext(const resources::ArgumentParser &parser)
constexpr int kDungeonPaletteColorCount
absl::Status MutateAndSaveRomWithBackup(Rom *rom, resources::OutputFormatter &formatter, Mutation &&mutation)
absl::StatusOr< DoorTypeTarget > ResolveDoorTypeTarget(Rom *rom, int room_id, int x, int y, const StreamSaveManifestContext &manifest_context)
absl::Status SaveRomWithBackup(Rom *rom, resources::OutputFormatter &formatter)
absl::Status ValidateDoorType(int type, const char *argument_name)
bool IsValidRomRange(const Rom &rom, uint64_t start, uint64_t length)
absl::StatusOr< PotItemTarget > ResolvePotItemTarget(Rom *rom, int room_id, int index, const PotItemManifestContext &manifest_context)
std::string AddressOwnershipToString(AddressOwnership ownership)
DungeonStreamType
Dungeon stream kinds with allocator layouts in the hack manifest.
absl::StatusOr< zelda3::DungeonStreamLayout > ToDungeonStreamAllocatorLayout(DungeonStreamType stream_type, const DungeonStreamLayout &manifest_layout)
constexpr int DungeonsMainPalettesMax
constexpr int kDungeonMainPalettes
bool ParseHexString(absl::string_view str, uint64_t *out)
constexpr uint32_t kPalettesetIdsAddress
constexpr int kDoorPointers
Room LoadRoomHeaderFromRom(Rom *rom, int room_id)
std::optional< size_t > ChestIndexForObject(const std::vector< RoomObject > &objects, size_t object_index)
absl::StatusOr< ChestSavePlan > BuildChestSavePlan(const Rom *rom, int room_count, const std::function< const Room *(int)> &room_lookup)
constexpr uint32_t kNumPalettesets
constexpr int kDungeonPaletteBytes
uint8_t CanonicalRoomObjectSize(int object_id, uint8_t requested_size)
constexpr int kRoomItemsPointers
Room LoadRoomFromRom(Rom *rom, int room_id)
constexpr DoorType DoorTypeFromRaw(uint8_t raw_type)
Convert raw type byte to DoorType enum.
constexpr uint32_t kDungeonPalettePointerTable
bool UsesRoomObjectStream(const RoomObject &object)
absl::StatusOr< DungeonStreamInventory > InventoryDungeonStreams(const Rom &rom, const DungeonStreamLayout &requested_layout)
std::string GetObjectName(int object_id)
constexpr std::string_view GetDoorTypeName(DoorType type)
Get human-readable name for door type.
constexpr int kNumberOfRooms
constexpr bool IsStatefulChestObjectId(int object_id)
absl::Status ApplyChestSavePlan(Rom *rom, const ChestSavePlan &plan, const std::function< const Room *(int)> &room_lookup)
constexpr int kRoomHeaderPointer
constexpr int kRoomHeaderPointerBank
absl::Status SaveAllCollision(Rom *rom, absl::Span< Room > rooms)
const char * ResolveSpriteName(uint16_t id)
constexpr int kRoomObjectPointer
absl::StatusOr< std::vector< chest_data > > PlanChestObjectEdit(const std::vector< RoomObject > &before_objects, const std::vector< chest_data > &before_chests, const std::vector< RoomObject > &after_objects, const std::vector< std::optional< size_t > > &source_indices, const std::vector< std::optional< chest_data > > &chest_overrides)
absl::StatusOr< DungeonStreamRecord > ReadDungeonSpriteStream(const Rom &rom, int room_id)
uint32_t PcToSnes(uint32_t addr)
uint32_t SnesToPc(uint32_t addr) noexcept
#define RETURN_IF_ERROR(expr)
Legacy chest data structure.
bool load_resource_labels
uint32_t object_stream_end_pc
uint32_t object_stream_pc
uint16_t palette_pointer_word
uint32_t palette_pointer_pc
uint8_t palette_pointer_offset
DungeonPaletteRoomMapping selected
std::vector< uint32_t > affected_rooms
core::HackManifest manifest
zelda3::DungeonStreamLayout layout
std::vector< PotItemEntryView > entries
std::vector< std::pair< uint32_t, uint32_t > > write_ranges
core::HackManifest manifest
project::RomWritePolicy write_policy
zelda3::DungeonStreamLayout allocator_layout
Explicit pointer and storage layout for one dungeon stream.
DungeonWriteStrategy strategy
A conflict detected when yaze wants to write to an ASM-owned address.
AddressOwnership ownership
std::vector< DungeonStreamAliasGroup > aliases
std::vector< DungeonStreamIssue > issues
std::vector< DungeonStreamOverlap > overlaps
std::vector< DungeonStreamRecord > streams
std::vector< DungeonStreamPcRange > allocation_ranges
DungeonPointerEncoding pointer_encoding
uint32_t pointer_table_pc
Represents a door in a dungeon room.
DoorType type
Door type (determines appearance/behavior)
static Door FromRomBytes(uint8_t b1, uint8_t b2)
DoorDirection direction
Which wall the door is on.
uint8_t position
Encoded position (5-bit, 0-31)
std::pair< int, int > GetTileCoords() const
Get tile coordinates for this door.