yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
common.h
Go to the documentation of this file.
1#ifndef YAZE_CORE_COMMON_H
2#define YAZE_CORE_COMMON_H
3
4#include <chrono>
5#include <cstdint>
6#include <fstream>
7#include <functional>
8#include <iostream>
9#include <memory>
10#include <stack>
11#include <string>
12
13#include "absl/status/statusor.h"
14#include "absl/strings/str_format.h"
15#include "imgui/imgui.h"
16
17namespace yaze {
18namespace app {
19
24namespace core {
25
31 public:
32 struct Flags {
33 // Log instructions to the GUI debugger.
34 bool kLogInstructions = true;
35
36 // Flag to enable ImGui input config flags. Currently is
37 // handled manually by controller class but should be
38 // ported away from that eventually.
39 bool kUseNewImGuiInput = false;
40
41 // Flag to enable the saving of all palettes to the Rom.
42 bool kSaveAllPalettes = false;
43
44 // Flag to enable the saving of gfx groups to the rom.
45 bool kSaveGfxGroups = false;
46
47 // Flag to enable the change queue, which could have any anonymous
48 // save routine for the Rom. In practice, just the overworld tilemap
49 // and tile32 save.
51
52 // Attempt to run the dungeon room draw routine when opening a room.
54
55 // Use the new platform specific file dialog wrappers.
57
58 // Platform specific loading of fonts from the system. Currently
59 // only supports macOS.
60 bool kLoadSystemFonts = true;
61
62 // Uses texture streaming from SDL for my dynamic updates.
64
65 // Save dungeon map edits to the Rom.
66 bool kSaveDungeonMaps = false;
67
68 // Log to the console.
69 bool kLogToConsole = false;
70
71 // Overworld flags
72 struct Overworld {
73 // Load and render overworld sprites to the screen. Unstable.
75
76 // Save overworld map edits to the Rom.
77 bool kSaveOverworldMaps = true;
78
79 // Save overworld entrances to the Rom.
81
82 // Save overworld exits to the Rom.
84
85 // Save overworld items to the Rom.
87
88 // Save overworld properties to the Rom.
90
91 // Load custom overworld data from the ROM and enable UI.
94 };
95
96 ExperimentFlags() = default;
97 virtual ~ExperimentFlags() = default;
98 auto flags() const {
99 if (!flags_) {
100 flags_ = std::make_shared<Flags>();
101 }
102 Flags *flags = flags_.get();
103 return flags;
104 }
106 if (!flags_) {
107 flags_ = std::make_shared<Flags>();
108 }
109 return flags_.get();
110 }
111 std::string Serialize() const {
112 std::string result;
113 result +=
114 "kLogInstructions: " + std::to_string(flags_->kLogInstructions) + "\n";
115 result +=
116 "kUseNewImGuiInput: " + std::to_string(flags_->kUseNewImGuiInput) +
117 "\n";
118 result +=
119 "kSaveAllPalettes: " + std::to_string(flags_->kSaveAllPalettes) + "\n";
120 result +=
121 "kSaveGfxGroups: " + std::to_string(flags_->kSaveGfxGroups) + "\n";
122 result += "kSaveWithChangeQueue: " +
123 std::to_string(flags_->kSaveWithChangeQueue) + "\n";
124 result += "kDrawDungeonRoomGraphics: " +
125 std::to_string(flags_->kDrawDungeonRoomGraphics) + "\n";
126 result += "kNewFileDialogWrapper: " +
127 std::to_string(flags_->kNewFileDialogWrapper) + "\n";
128 result +=
129 "kLoadSystemFonts: " + std::to_string(flags_->kLoadSystemFonts) + "\n";
130 result += "kLoadTexturesAsStreaming: " +
131 std::to_string(flags_->kLoadTexturesAsStreaming) + "\n";
132 result +=
133 "kSaveDungeonMaps: " + std::to_string(flags_->kSaveDungeonMaps) + "\n";
134 result += "kLogToConsole: " + std::to_string(flags_->kLogToConsole) + "\n";
135 result += "kDrawOverworldSprites: " +
136 std::to_string(flags_->overworld.kDrawOverworldSprites) + "\n";
137 result += "kSaveOverworldMaps: " +
138 std::to_string(flags_->overworld.kSaveOverworldMaps) + "\n";
139 result += "kSaveOverworldEntrances: " +
140 std::to_string(flags_->overworld.kSaveOverworldEntrances) + "\n";
141 result += "kSaveOverworldExits: " +
142 std::to_string(flags_->overworld.kSaveOverworldExits) + "\n";
143 result += "kSaveOverworldItems: " +
144 std::to_string(flags_->overworld.kSaveOverworldItems) + "\n";
145 result += "kSaveOverworldProperties: " +
146 std::to_string(flags_->overworld.kSaveOverworldProperties) + "\n";
147 return result;
148 }
149
150 private:
151 static std::shared_ptr<Flags> flags_;
152};
153
159template <typename T>
161 public:
163 NotifyValue(const T &value)
164 : value_(value), modified_(false), temp_value_() {}
165
166 void set(const T &value) {
167 value_ = value;
168 modified_ = true;
169 }
170
171 const T &get() {
172 modified_ = false;
173 return value_;
174 }
175
177 modified_ = false;
179 return temp_value_;
180 }
181
183 if (temp_value_ != value_) {
185 modified_ = true;
186 }
187 }
188
189 void operator=(const T &value) { set(value); }
190 operator T() { return get(); }
191
192 bool modified() const { return modified_; }
193
194 private:
198};
199
200static bool log_to_console = false;
201static std::string log_file_out = "log.txt";
202
203template <typename... Args>
204static void logf(const absl::FormatSpec<Args...> &format, const Args &...args) {
205 std::string message = absl::StrFormat(format, args...);
206 if (log_to_console) {
207 std::cout << message << std::endl;
208 }
209 static std::ofstream fout(log_file_out, std::ios::out | std::ios::app);
210 fout << message << std::endl;
211}
212
213class Logger {
214 public:
215 static void log(std::string message) {
216 static std::ofstream fout(log_file_out, std::ios::out | std::ios::app);
217 fout << message << std::endl;
218 }
219};
220
221constexpr uint32_t kFastRomRegion = 0x808000;
222
223inline uint32_t SnesToPc(uint32_t addr) noexcept {
224 if (addr >= kFastRomRegion) {
225 addr -= kFastRomRegion;
226 }
227 uint32_t temp = (addr & 0x7FFF) + ((addr / 2) & 0xFF8000);
228 return (temp + 0x0);
229}
230
231inline uint32_t PcToSnes(uint32_t addr) {
232 uint8_t *b = reinterpret_cast<uint8_t *>(&addr);
233 b[2] = static_cast<uint8_t>(b[2] * 2);
234
235 if (b[1] >= 0x80) {
236 b[2] += 1;
237 } else {
238 b[1] += 0x80;
239 }
240
241 return addr;
242}
243
244inline int AddressFromBytes(uint8_t bank, uint8_t high, uint8_t low) noexcept {
245 return (bank << 16) | (high << 8) | low;
246}
247
248inline uint32_t MapBankToWordAddress(uint8_t bank, uint16_t addr) noexcept {
249 uint32_t result = 0;
250 result = (bank << 16) | addr;
251 return result;
252}
253
254uint32_t Get24LocalFromPC(uint8_t *data, int addr, bool pc = true);
255
260void stle16b_i(uint8_t *const p_arr, size_t const p_index,
261 uint16_t const p_val);
262
263void stle16b(uint8_t *const p_arr, uint16_t const p_val);
264
270uint16_t ldle16b_i(uint8_t const *const p_arr, size_t const p_index);
271
272// Load little endian halfword (16-bit) dereferenced from
273uint16_t ldle16b(uint8_t const *const p_arr);
274
276 std::string name;
277 std::vector<FolderItem> subfolders;
278 std::vector<std::string> files;
279};
280
281typedef struct FolderItem FolderItem;
282
283void CreateBpsPatch(const std::vector<uint8_t> &source,
284 const std::vector<uint8_t> &target,
285 std::vector<uint8_t> &patch);
286
287void ApplyBpsPatch(const std::vector<uint8_t> &source,
288 const std::vector<uint8_t> &patch,
289 std::vector<uint8_t> &target);
290
291absl::StatusOr<std::string> CheckVersion(const char *version);
292
293} // namespace core
294} // namespace app
295} // namespace yaze
296
297#endif
A class to manage experimental feature flags.
Definition common.h:30
static std::shared_ptr< Flags > flags_
Definition common.h:151
std::string Serialize() const
Definition common.h:111
virtual ~ExperimentFlags()=default
static void log(std::string message)
Definition common.h:215
A class to manage a value that can be modified and notify when it changes.
Definition common.h:160
NotifyValue(const T &value)
Definition common.h:163
void operator=(const T &value)
Definition common.h:189
void set(const T &value)
Definition common.h:166
void ApplyBpsPatch(const std::vector< uint8_t > &source, const std::vector< uint8_t > &patch, std::vector< uint8_t > &target)
Definition common.cc:222
uint32_t PcToSnes(uint32_t addr)
Definition common.h:231
uint32_t MapBankToWordAddress(uint8_t bank, uint16_t addr) noexcept
Definition common.h:248
void stle16b(uint8_t *const p_arr, uint16_t const p_val)
Definition common.cc:118
uint16_t ldle16b(uint8_t const *const p_arr)
Definition common.cc:123
absl::StatusOr< std::string > CheckVersion(const char *version)
Definition common.cc:292
uint32_t Get24LocalFromPC(uint8_t *data, int addr, bool pc)
Definition common.cc:104
int AddressFromBytes(uint8_t bank, uint8_t high, uint8_t low) noexcept
Definition common.h:244
void CreateBpsPatch(const std::vector< uint8_t > &source, const std::vector< uint8_t > &target, std::vector< uint8_t > &patch)
Definition common.cc:133
uint16_t ldle16b_i(uint8_t const *const p_arr, size_t const p_index)
Load little endian halfword (16-bit) dereferenced from an arrays of bytes. This version provides an i...
Definition common.cc:129
uint32_t SnesToPc(uint32_t addr) noexcept
Definition common.h:223
constexpr uint32_t kFastRomRegion
Definition common.h:221
void stle16b_i(uint8_t *const p_arr, size_t const p_index, uint16_t const p_val)
Store little endian 16-bit value using a byte pointer, offset by an index before dereferencing.
Definition common.cc:113
Definition common.cc:21
struct yaze::app::core::ExperimentFlags::Flags::Overworld overworld
std::vector< FolderItem > subfolders
Definition common.h:277
std::vector< std::string > files
Definition common.h:278