yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
z3dk_wrapper.h
Go to the documentation of this file.
1#ifndef YAZE_CORE_Z3DK_WRAPPER_H
2#define YAZE_CORE_Z3DK_WRAPPER_H
3
4// Adapter around z3dk::Assembler that mirrors the subset of
5// core::AsarWrapper's surface that AssemblyEditor consumes. Only built when
6// YAZE_WITH_Z3DK is defined (YAZE_ENABLE_Z3DK=ON at configure time).
7//
8// The adapter produces a core::AsarPatchResult for drop-in compatibility
9// with the existing editor plumbing, but *always* populates the newer
10// structured_diagnostics field so the DiagnosticsPanel can render with
11// full file:line:column resolution.
12
13#include <map>
14#include <memory>
15#include <optional>
16#include <string>
17#include <utility>
18#include <vector>
19
20#include "absl/status/status.h"
21#include "absl/status/statusor.h"
22#include "core/asar_wrapper.h"
23
24namespace z3dk {
25struct AssembleResult; // Complete in z3dk_core/assembler.h; only the
26 // wrapper .cc needs the definition.
27} // namespace z3dk
28
29namespace yaze {
30namespace core {
31
33 uint32_t start = 0;
34 uint32_t end = 0;
35 std::string reason;
36};
37
39 std::vector<std::string> include_paths;
40 std::vector<std::pair<std::string, std::string>> defines;
41 std::string std_includes_path;
42 std::string std_defines_path;
43 std::string mapper;
44 int rom_size = 0;
46 std::vector<Z3dkMemoryRange> prohibited_memory_ranges;
49 bool warn_unknown_width = true;
50 bool warn_org_collision = true;
52 bool warn_stack_balance = true;
53 bool warn_hook_return = true;
54 std::string hooks_rom_path;
55};
56
58 public:
61
62 Z3dkWrapper(const Z3dkWrapper&) = delete;
64 // Defined out-of-line because last_result_ holds a unique_ptr to a
65 // forward-declared z3dk type.
67 Z3dkWrapper& operator=(Z3dkWrapper&&) noexcept;
68
69 // Matches AsarWrapper::Initialize() shape; z3dk has no runtime init but
70 // we keep the method so AssemblyEditor's call sites can stay
71 // backend-agnostic.
72 absl::Status Initialize();
73 void Shutdown() { initialized_ = false; }
74 bool IsInitialized() const { return initialized_; }
75
76 std::string GetVersion() const;
77
78 // Applies `patch_path` to the in-place `rom_data`. On assemble failure,
79 // returns a StatusOr whose *value* still holds the diagnostics (so the
80 // editor can paint markers even when the patch did not commit).
81 absl::StatusOr<AsarPatchResult> ApplyPatch(
82 const std::string& patch_path, std::vector<uint8_t>& rom_data,
83 const std::vector<std::string>& include_paths = {});
84 absl::StatusOr<AsarPatchResult> ApplyPatch(
85 const std::string& patch_path, std::vector<uint8_t>& rom_data,
86 const Z3dkAssembleOptions& options);
87
88 // Assemble without mutating the ROM — the underlying assembler still
89 // needs a writable buffer, so we use a scratch copy internally.
90 absl::Status ValidateAssembly(
91 const std::string& asm_path,
92 const std::vector<std::string>& include_paths = {});
93 absl::Status ValidateAssembly(const std::string& asm_path,
94 const Z3dkAssembleOptions& options);
95
96 const std::map<std::string, AsarSymbol>& GetSymbolTable() const {
97 return symbol_table_;
98 }
99 std::optional<AsarSymbol> FindSymbol(const std::string& name) const;
100
101 std::vector<std::string> GetLastErrors() const { return last_errors_; }
102 std::vector<std::string> GetLastWarnings() const { return last_warnings_; }
103
104 // Re-runs z3dk::RunLint against the AssembleResult cached by the most
105 // recent successful ApplyPatch, without re-assembling. Returns
106 // FailedPreconditionError if no result is cached (fresh wrapper, failed
107 // assemble, or post-Reset). Callers decide how to filter the returned
108 // diagnostics by severity.
109 absl::StatusOr<std::vector<AssemblyDiagnostic>> RunLintOnLastResult(
110 const Z3dkAssembleOptions& options) const;
111
112 void Reset();
113
114 private:
115 absl::StatusOr<AsarPatchResult> Assemble(const std::string& patch_path,
116 std::vector<uint8_t>& rom_data,
117 const Z3dkAssembleOptions& options,
118 bool update_apply_cache);
119 void ClearApplyCache();
120
121 bool initialized_ = false;
122 std::map<std::string, AsarSymbol> symbol_table_;
123 std::vector<std::string> last_errors_;
124 std::vector<std::string> last_warnings_;
125
126 // Cached copy of the last successful ApplyPatch assemble output so
127 // RunLintOnLastResult can re-run lint without re-assembling. Holds the
128 // assembler's rom_data buffer (~4 MiB for LoROM); cleared by Reset().
129 std::unique_ptr<::z3dk::AssembleResult> last_result_;
130};
131
132} // namespace core
133} // namespace yaze
134
135#endif // YAZE_CORE_Z3DK_WRAPPER_H
std::vector< std::string > GetLastErrors() const
const std::map< std::string, AsarSymbol > & GetSymbolTable() const
absl::StatusOr< AsarPatchResult > Assemble(const std::string &patch_path, std::vector< uint8_t > &rom_data, const Z3dkAssembleOptions &options, bool update_apply_cache)
bool IsInitialized() const
Z3dkWrapper(Z3dkWrapper &&) noexcept
std::vector< std::string > last_errors_
absl::StatusOr< std::vector< AssemblyDiagnostic > > RunLintOnLastResult(const Z3dkAssembleOptions &options) const
std::optional< AsarSymbol > FindSymbol(const std::string &name) const
absl::Status Initialize()
std::vector< std::string > last_warnings_
std::string GetVersion() const
Z3dkWrapper(const Z3dkWrapper &)=delete
std::vector< std::string > GetLastWarnings() const
absl::Status ValidateAssembly(const std::string &asm_path, const std::vector< std::string > &include_paths={})
absl::StatusOr< AsarPatchResult > ApplyPatch(const std::string &patch_path, std::vector< uint8_t > &rom_data, const std::vector< std::string > &include_paths={})
std::unique_ptr<::z3dk::AssembleResult > last_result_
std::map< std::string, AsarSymbol > symbol_table_
Z3dkWrapper & operator=(const Z3dkWrapper &)=delete
std::vector< Z3dkMemoryRange > prohibited_memory_ranges
std::vector< std::string > include_paths
std::vector< std::pair< std::string, std::string > > defines