yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
gfx_sheet_policy_adapter.cc
Go to the documentation of this file.
2
3#include <string>
4#include <vector>
5
6#include "absl/status/status.h"
7#include "absl/strings/str_format.h"
8#include "absl/strings/str_join.h"
9#include "rom/snes.h"
10#include "util/macro.h"
11
12namespace yaze::core {
13
14absl::StatusOr<zelda3::GfxSheetWritePolicy> BuildGfxSheetWritePolicy(
15 const HackManifest& manifest) {
17 if (!manifest.loaded()) {
18 return policy;
19 }
20
21 const GraphicsSheetLayout& layout = manifest.graphics_sheet_layout();
22 for (const SnesAddressRange& range : layout.allocation_regions) {
23 if ((range.start & 0xFFFFu) < 0x8000u) {
24 return absl::InvalidArgumentError(absl::StrFormat(
25 "Graphics allocation start $%06X is not a mapped LoROM address",
26 range.start));
27 }
28 const uint32_t begin = SnesToPc(range.start);
29 // The half-open end may sit exactly on the next bank's $8000.
30 const uint32_t end = SnesToPc(range.end);
31 if (begin >= end) {
32 return absl::InvalidArgumentError(absl::StrFormat(
33 "Graphics allocation range [$%06X, $%06X) is empty in PC space",
34 range.start, range.end));
35 }
36 policy.allocation_regions.push_back({begin, end});
37 }
38 policy.reserved_sheets.insert(layout.reserved_sheets.begin(),
39 layout.reserved_sheets.end());
40
41 const HackManifest* manifest_ptr = &manifest;
42 policy.check_write = [manifest_ptr](uint32_t begin,
43 uint32_t end) -> absl::Status {
44 const auto conflicts = manifest_ptr->AnalyzePcWriteRanges({{begin, end}});
45 if (conflicts.empty()) {
46 return absl::OkStatus();
47 }
48 std::vector<std::string> parts;
49 for (const WriteConflict& conflict : conflicts) {
50 parts.push_back(absl::StrFormat(
51 "$%06X (%s%s%s)", conflict.address,
52 AddressOwnershipToString(conflict.ownership),
53 conflict.module.empty() ? "" : ": ", conflict.module));
54 }
55 return absl::FailedPreconditionError(absl::StrFormat(
56 "Graphics write [0x%06X, 0x%06X) touches ASM-owned bytes: %s", begin,
57 end, absl::StrJoin(parts, ", ")));
58 };
59 return policy;
60}
61
62absl::StatusOr<zelda3::GfxSheetWritePolicy> BuildGfxSheetWritePolicy(
63 const project::YazeProject& project) {
66 const auto& settings = project.graphics_sheets;
67 policy.reserved_sheets.insert(settings.reserved_sheets.begin(),
68 settings.reserved_sheets.end());
69 for (const auto& [sheet, blocks] : settings.reserved_blocks) {
70 auto& merged = policy.reserved_blocks[sheet];
71 merged.insert(merged.end(), blocks.begin(), blocks.end());
72 }
73 return policy;
74}
75
77 const project::YazeProject* project) {
79 if (project == nullptr) {
80 return options;
81 }
82 const auto& settings = project->graphics_sheets;
83 options.reserved_sheets.insert(settings.reserved_sheets.begin(),
84 settings.reserved_sheets.end());
85 options.flagged_sheets.insert(settings.flagged_sheets.begin(),
86 settings.flagged_sheets.end());
87 options.reserved_blocks = settings.reserved_blocks;
88 const auto& manifest_reserved =
90 options.reserved_sheets.insert(manifest_reserved.begin(),
91 manifest_reserved.end());
92 return options;
93}
94
95} // namespace yaze::core
Loads and queries the hack manifest JSON for yaze-ASM integration.
const GraphicsSheetLayout & graphics_sheet_layout() const
Graphics-sheet free space and reserved sheets. Empty when the optional graphics_sheet_regions section...
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.
bool loaded() const
Check if the manifest has been loaded.
#define ASSIGN_OR_RETURN(type_variable_name, expression)
Definition macro.h:62
absl::StatusOr< zelda3::GfxSheetWritePolicy > BuildGfxSheetWritePolicy(const HackManifest &manifest)
std::string AddressOwnershipToString(AddressOwnership ownership)
zelda3::GfxSheetInventoryOptions BuildGfxSheetInventoryOptions(const project::YazeProject *project)
uint32_t SnesToPc(uint32_t addr) noexcept
Definition snes.h:8
Graphics-sheet storage rules declared by the hack (graphics_sheet_regions).
std::vector< uint16_t > reserved_sheets
std::vector< SnesAddressRange > allocation_regions
A half-open [start, end) range of canonical LoROM SNES addresses.
A conflict detected when yaze wants to write to an ASM-owned address.
Modern project structure with comprehensive settings consolidation.
Definition project.h:287
core::HackManifest hack_manifest
Definition project.h:360
GraphicsSheetSettings graphics_sheets
Definition project.h:348
std::map< uint16_t, std::vector< uint16_t > > reserved_blocks
Rules a sheet write must follow.
std::vector< GfxSheetPcRange > allocation_regions
std::function< absl::Status(uint32_t begin, uint32_t end)> check_write
std::map< uint16_t, std::vector< uint16_t > > reserved_blocks