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 <cstdint>
5#include <fstream>
6#include <iostream>
7#include <memory>
8#include <string>
9
10#include "absl/status/statusor.h"
11#include "absl/strings/str_format.h"
12
13namespace yaze {
14namespace app {
15
20namespace core {
21
22constexpr std::string_view kYazeVersion = "0.2.1";
23
24std::string UppercaseHexByte(uint8_t byte, bool leading = false);
25std::string UppercaseHexWord(uint16_t word, bool leading = false);
26std::string UppercaseHexLong(uint32_t dword);
27std::string UppercaseHexLongLong(uint64_t qword);
28
29bool StringReplace(std::string &str, const std::string &from,
30 const std::string &to);
31
37 public:
38 struct Flags {
39 // Log instructions to the GUI debugger.
40 bool kLogInstructions = true;
41
42 // Flag to enable ImGui input config flags. Currently is
43 // handled manually by controller class but should be
44 // ported away from that eventually.
45 bool kUseNewImGuiInput = false;
46
47 // Flag to enable the saving of all palettes to the Rom.
48 bool kSaveAllPalettes = false;
49
50 // Flag to enable the saving of gfx groups to the rom.
51 bool kSaveGfxGroups = false;
52
53 // Flag to enable the change queue, which could have any anonymous
54 // save routine for the Rom. In practice, just the overworld tilemap
55 // and tile32 save.
57
58 // Attempt to run the dungeon room draw routine when opening a room.
60
61 // Use the new platform specific file dialog wrappers.
63
64 // Uses texture streaming from SDL for my dynamic updates.
66
67 // Save dungeon map edits to the Rom.
68 bool kSaveDungeonMaps = false;
69
70 // Save graphics sheet to the Rom.
71 bool kSaveGraphicsSheet = false;
72
73 // Log to the console.
74 bool kLogToConsole = false;
75
76 // Overworld flags
77 struct Overworld {
78 // Load and render overworld sprites to the screen. Unstable.
80
81 // Save overworld map edits to the Rom.
82 bool kSaveOverworldMaps = true;
83
84 // Save overworld entrances to the Rom.
86
87 // Save overworld exits to the Rom.
89
90 // Save overworld items to the Rom.
92
93 // Save overworld properties to the Rom.
95
96 // Load custom overworld data from the ROM and enable UI.
99 };
100
101 ExperimentFlags() = default;
102 virtual ~ExperimentFlags() = default;
103 auto flags() const {
104 if (!flags_) {
105 flags_ = std::make_shared<Flags>();
106 }
107 Flags *flags = flags_.get();
108 return flags;
109 }
111 if (!flags_) {
112 flags_ = std::make_shared<Flags>();
113 }
114 return flags_.get();
115 }
116 std::string Serialize() const {
117 std::string result;
118 result +=
119 "kLogInstructions: " + std::to_string(flags_->kLogInstructions) + "\n";
120 result +=
121 "kUseNewImGuiInput: " + std::to_string(flags_->kUseNewImGuiInput) +
122 "\n";
123 result +=
124 "kSaveAllPalettes: " + std::to_string(flags_->kSaveAllPalettes) + "\n";
125 result +=
126 "kSaveGfxGroups: " + std::to_string(flags_->kSaveGfxGroups) + "\n";
127 result += "kSaveWithChangeQueue: " +
128 std::to_string(flags_->kSaveWithChangeQueue) + "\n";
129 result += "kDrawDungeonRoomGraphics: " +
130 std::to_string(flags_->kDrawDungeonRoomGraphics) + "\n";
131 result += "kNewFileDialogWrapper: " +
132 std::to_string(flags_->kNewFileDialogWrapper) + "\n";
133 result += "kLoadTexturesAsStreaming: " +
134 std::to_string(flags_->kLoadTexturesAsStreaming) + "\n";
135 result +=
136 "kSaveDungeonMaps: " + std::to_string(flags_->kSaveDungeonMaps) + "\n";
137 result += "kLogToConsole: " + std::to_string(flags_->kLogToConsole) + "\n";
138 result += "kDrawOverworldSprites: " +
139 std::to_string(flags_->overworld.kDrawOverworldSprites) + "\n";
140 result += "kSaveOverworldMaps: " +
141 std::to_string(flags_->overworld.kSaveOverworldMaps) + "\n";
142 result += "kSaveOverworldEntrances: " +
143 std::to_string(flags_->overworld.kSaveOverworldEntrances) + "\n";
144 result += "kSaveOverworldExits: " +
145 std::to_string(flags_->overworld.kSaveOverworldExits) + "\n";
146 result += "kSaveOverworldItems: " +
147 std::to_string(flags_->overworld.kSaveOverworldItems) + "\n";
148 result += "kSaveOverworldProperties: " +
149 std::to_string(flags_->overworld.kSaveOverworldProperties) + "\n";
150 return result;
151 }
152
153 private:
154 static std::shared_ptr<Flags> flags_;
155};
156
162template <typename T>
164 public:
166 NotifyValue(const T &value)
167 : value_(value), modified_(false), temp_value_() {}
168
169 void set(const T &value) {
170 value_ = value;
171 modified_ = true;
172 }
173
174 const T &get() {
175 modified_ = false;
176 return value_;
177 }
178
180 modified_ = false;
182 return temp_value_;
183 }
184
186 if (temp_value_ != value_) {
188 modified_ = true;
189 }
190 }
191
192 void operator=(const T &value) { set(value); }
193 operator T() { return get(); }
194
195 bool modified() const { return modified_; }
196
197 private:
201};
202
203static bool log_to_console = false;
204static std::string log_file_out = "log.txt";
205
206template <typename... Args>
207static void logf(const absl::FormatSpec<Args...> &format, const Args &...args) {
208 std::string message = absl::StrFormat(format, args...);
209 if (log_to_console) {
210 std::cout << message << std::endl;
211 }
212 static std::ofstream fout(log_file_out, std::ios::out | std::ios::app);
213 fout << message << std::endl;
214}
215
216class Logger {
217 public:
218 static void log(std::string message) {
219 static std::ofstream fout(log_file_out, std::ios::out | std::ios::app);
220 fout << message << std::endl;
221 }
222};
223
224constexpr uint32_t kFastRomRegion = 0x808000;
225
226inline uint32_t SnesToPc(uint32_t addr) noexcept {
227 if (addr >= kFastRomRegion) {
228 addr -= kFastRomRegion;
229 }
230 uint32_t temp = (addr & 0x7FFF) + ((addr / 2) & 0xFF8000);
231 return (temp + 0x0);
232}
233
234inline uint32_t PcToSnes(uint32_t addr) {
235 uint8_t *b = reinterpret_cast<uint8_t *>(&addr);
236 b[2] = static_cast<uint8_t>(b[2] * 2);
237
238 if (b[1] >= 0x80) {
239 b[2] += 1;
240 } else {
241 b[1] += 0x80;
242 }
243
244 return addr;
245}
246
247inline int AddressFromBytes(uint8_t bank, uint8_t high, uint8_t low) noexcept {
248 return (bank << 16) | (high << 8) | low;
249}
250
251inline uint32_t MapBankToWordAddress(uint8_t bank, uint16_t addr) noexcept {
252 uint32_t result = 0;
253 result = (bank << 16) | addr;
254 return result;
255}
256
257uint32_t Get24LocalFromPC(uint8_t *data, int addr, bool pc = true);
258
263void stle16b_i(uint8_t *const p_arr, size_t const p_index,
264 uint16_t const p_val);
265
266void stle16b(uint8_t *const p_arr, uint16_t const p_val);
267
273uint16_t ldle16b_i(uint8_t const *const p_arr, size_t const p_index);
274
275// Load little endian halfword (16-bit) dereferenced from
276uint16_t ldle16b(uint8_t const *const p_arr);
277
279 std::string name;
280 std::vector<FolderItem> subfolders;
281 std::vector<std::string> files;
282};
283
284typedef struct FolderItem FolderItem;
285
286void CreateBpsPatch(const std::vector<uint8_t> &source,
287 const std::vector<uint8_t> &target,
288 std::vector<uint8_t> &patch);
289
290void ApplyBpsPatch(const std::vector<uint8_t> &source,
291 const std::vector<uint8_t> &patch,
292 std::vector<uint8_t> &target);
293
294absl::StatusOr<std::string> CheckVersion(const char *version);
295
296} // namespace core
297} // namespace app
298} // namespace yaze
299
300#endif
A class to manage experimental feature flags.
Definition common.h:36
static std::shared_ptr< Flags > flags_
Definition common.h:154
std::string Serialize() const
Definition common.h:116
virtual ~ExperimentFlags()=default
static void log(std::string message)
Definition common.h:218
A class to manage a value that can be modified and notify when it changes.
Definition common.h:163
NotifyValue(const T &value)
Definition common.h:166
void operator=(const T &value)
Definition common.h:192
void set(const T &value)
Definition common.h:169
void ApplyBpsPatch(const std::vector< uint8_t > &source, const std::vector< uint8_t > &patch, std::vector< uint8_t > &target)
Definition common.cc:257
std::string UppercaseHexLongLong(uint64_t qword)
Definition common.cc:123
bool StringReplace(std::string &str, const std::string &from, const std::string &to)
Definition common.cc:128
uint32_t PcToSnes(uint32_t addr)
Definition common.h:234
uint32_t MapBankToWordAddress(uint8_t bank, uint16_t addr) noexcept
Definition common.h:251
void stle16b(uint8_t *const p_arr, uint16_t const p_val)
Definition common.cc:153
uint16_t ldle16b(uint8_t const *const p_arr)
Definition common.cc:158
absl::StatusOr< std::string > CheckVersion(const char *version)
Definition common.cc:327
uint32_t Get24LocalFromPC(uint8_t *data, int addr, bool pc)
Definition common.cc:139
int AddressFromBytes(uint8_t bank, uint8_t high, uint8_t low) noexcept
Definition common.h:247
void CreateBpsPatch(const std::vector< uint8_t > &source, const std::vector< uint8_t > &target, std::vector< uint8_t > &patch)
Definition common.cc:168
std::string UppercaseHexLong(uint32_t dword)
Definition common.cc:119
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:164
uint32_t SnesToPc(uint32_t addr) noexcept
Definition common.h:226
constexpr uint32_t kFastRomRegion
Definition common.h:224
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:148
std::string UppercaseHexByte(uint8_t byte, bool leading)
Definition common.cc:103
constexpr std::string_view kYazeVersion
Definition common.h:22
std::string UppercaseHexWord(uint16_t word, bool leading)
Definition common.cc:111
Definition common.cc:22
struct yaze::app::core::ExperimentFlags::Flags::Overworld overworld
std::vector< FolderItem > subfolders
Definition common.h:280
std::vector< std::string > files
Definition common.h:281