11#include "yaze_config.h"
21static bool g_library_initialized =
false;
25 if (g_library_initialized) {
30 g_library_initialized =
true;
35 if (!g_library_initialized) {
40 g_library_initialized =
false;
48 return "Unknown error";
50 return "Invalid argument";
52 return "File not found";
54 return "Memory allocation failed";
56 return "I/O operation failed";
58 return "Data corruption detected";
60 return "Component not initialized";
62 return "Unknown status code";
75 if (expected_version ==
nullptr) {
82 if (context ==
nullptr) {
86 if (!g_library_initialized) {
93 context->
rom =
nullptr;
96 if (rom_filename !=
nullptr && strlen(rom_filename) > 0) {
98 if (context->
rom ==
nullptr) {
108 if (context ==
nullptr) {
112 if (context->
rom !=
nullptr) {
114 context->
rom =
nullptr;
122 if (filename ==
nullptr || strlen(filename) == 0) {
126 auto internal_rom = std::make_unique<yaze::Rom>();
127 if (!internal_rom->LoadFromFile(filename).ok()) {
132 auto internal_game_data = std::make_unique<yaze::zelda3::GameData>();
135 if (!load_status.ok()) {
141 rom->impl = internal_rom.release();
142 rom->game_data = internal_game_data.release();
143 rom->
data =
const_cast<uint8_t*
>(
static_cast<yaze::Rom*
>(rom->impl)->data());
146 rom->is_modified =
false;
151 if (rom ==
nullptr) {
155 if (rom->impl !=
nullptr) {
156 delete static_cast<yaze::Rom*
>(rom->impl);
160 if (rom->game_data !=
nullptr) {
162 rom->game_data =
nullptr;
169 if (rom ==
nullptr || filename ==
nullptr) {
173 if (rom->impl ==
nullptr) {
177 auto* internal_rom =
static_cast<yaze::Rom*
>(rom->impl);
179 .
backup =
true, .save_new =
false, .filename = filename});
185 rom->is_modified =
false;
194 bitmap.
data =
nullptr;
199 int palette_set,
int palette,
202 color_struct.
red = 0;
203 color_struct.
green = 0;
204 color_struct.
blue = 0;
206 if (rom->game_data) {
212 color_struct = get_color.rom_color();
221 if (rom->impl ==
nullptr) {
227 auto internal_overworld =
229 if (!internal_overworld->Load(internal_rom).ok()) {
234 overworld->
impl = internal_overworld;
236 for (
const auto& ow_map : internal_overworld->overworld_maps()) {
238 overworld->
maps[map_id]->
id = map_id;
246 if (room_count !=
nullptr) {
249 if (rom ==
nullptr || rom->impl ==
nullptr) {
253 constexpr int kRoomCount = 256;
255 for (
int i = 0; i < kRoomCount; ++i) {
256 rooms[i].
id =
static_cast<uint16_t
>(i);
258 if (room_count !=
nullptr) {
259 *room_count = kRoomCount;
265 int* message_count) {
266 if (rom ==
nullptr || messages ==
nullptr || message_count ==
nullptr) {
270 if (rom->impl ==
nullptr) {
276 std::vector<yaze::editor::MessageData> message_data =
279 *message_count =
static_cast<int>(message_data.size());
282 for (
size_t i = 0; i < message_data.size(); ++i) {
283 const auto& msg = message_data[i];
284 (*messages)[i].
id = msg.ID;
285 (*messages)[i].rom_address = msg.Address;
286 (*messages)[i].length =
static_cast<uint16_t
>(msg.RawString.length());
289 (*messages)[i].raw_data =
new uint8_t[msg.Data.size()];
290 std::memcpy((*messages)[i].raw_data, msg.Data.data(), msg.Data.size());
292 (*messages)[i].parsed_text =
new char[msg.ContentsParsed.length() + 1];
294 std::memcpy((*messages)[i].parsed_text, msg.ContentsParsed.c_str(),
295 msg.ContentsParsed.length());
296 (*messages)[i].parsed_text[msg.ContentsParsed.length()] =
'\0';
298 (*messages)[i].is_compressed = msg.Data.size() != msg.DataParsed.size();
299 (*messages)[i].encoding_type = 1;
301 }
catch (
const std::exception& e) {
312 if (bitmap !=
nullptr && bitmap->
data !=
nullptr) {
313 delete[] bitmap->
data;
314 bitmap->
data =
nullptr;
324 if (width <= 0 || height <= 0 ||
325 (bpp != 1 && bpp != 2 && bpp != 4 && bpp != 8)) {
329 bitmap.
width = width;
332 bitmap.
data =
new uint8_t[width * height]();
348 *r =
static_cast<uint8_t
>(color.
red);
350 *g =
static_cast<uint8_t
>(color.
green);
352 *b =
static_cast<uint8_t
>(color.
blue);
357 if (rom_data ==
nullptr || size < 0x8000) {
364 for (
int i = 0; i < 21; ++i) {
365 char c =
static_cast<char>(rom_data[0x7FC0 + i]);
366 if (c >= 0x20 && c < 0x7F) {
371 if (absl::StrContains(title,
"THE LEGEND OF ZELDA")) {
374 uint8_t region = rom_data[0x7FD9];
382 if (absl::StrContains(title,
"ZELDA NO DENSETSU")) {
387 if (absl::StrContains(title,
"VT RANDO") ||
388 absl::StrContains(title,
"Z3 RANDOMIZER")) {
398 return "US/North American";
416 return &zelda3_us_pointers;
418 return &zelda3_jp_pointers;
420 return &zelda3_us_pointers;
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
absl::Status SaveToFile(const SaveSettings &settings)
Represents the full Overworld data, light and dark world.
struct zelda3_overworld_map zelda3_overworld_map
Overworld map data.
struct zelda3_overworld zelda3_overworld
Complete overworld data.
void yaze_unload_rom(zelda3_rom *rom)
Unload and free ROM data.
zelda3_rom * yaze_load_rom(const char *filename)
Load a ROM file.
int yaze_save_rom(zelda3_rom *rom, const char *filename)
Save ROM to file.
const char * zelda3_version_to_string(zelda3_version version)
Get version name as string.
struct zelda3_rom zelda3_rom
ROM data structure.
zelda3_version
Different versions of the game supported by YAZE.
const zelda3_version_pointers * zelda3_get_version_pointers(zelda3_version version)
Get version-specific pointers.
zelda3_version zelda3_detect_version(const uint8_t *rom_data, size_t size)
Detect ROM version from header data.
@ ZELDA3_VERSION_RANDOMIZER
#define YAZE_VERSION_NUMBER
#define YAZE_VERSION_STRING
std::vector< MessageData > ReadAllTextData(uint8_t *rom, int pos, int max_pos, bool allow_bank_switch)
constexpr const char * kPaletteGroupAddressesKeys[]
absl::Status LoadGameData(Rom &rom, GameData &data, const LoadOptions &options)
Loads all Zelda3-specific game data from a generic ROM.
SNES color in 15-bit RGB format (BGR555)
PaletteGroup * get_group(const std::string &group_name)
auto palette(int i) const
gfx::PaletteGroupMap palette_groups
const char * error_message
Complete dungeon room data.
In-game text message data.
zelda3_overworld_map ** maps
ROM data pointers for different game versions.
snes_color yaze_rgb_to_snes_color(uint8_t r, uint8_t g, uint8_t b)
Convert RGB888 color to SNES color.
zelda3_overworld * yaze_load_overworld(const zelda3_rom *rom)
Load the overworld from ROM.
yaze_bitmap yaze_load_bitmap(const char *filename)
Load a bitmap from file.
void yaze_library_shutdown()
Shutdown the YAZE library.
yaze_bitmap yaze_create_bitmap(int width, int height, uint8_t bpp)
Create an empty bitmap.
bool yaze_check_version_compatibility(const char *expected_version)
Check if the current YAZE version is compatible with the expected version.
yaze_status yaze_shutdown(yaze_editor_context *context)
Shutdown and clean up a YAZE editor context.
zelda3_dungeon_room * yaze_load_all_rooms(const zelda3_rom *rom, int *room_count)
Load all dungeon rooms from ROM.
yaze_status yaze_init(yaze_editor_context *context, const char *rom_filename)
Initialize a YAZE editor context.
int yaze_get_version_number()
Get the current YAZE version number.
snes_color yaze_get_color_from_paletteset(const zelda3_rom *rom, int palette_set, int palette, int color)
Get a color from a palette set.
const char * yaze_get_version_string()
Get the current YAZE version string.
void yaze_free_bitmap(yaze_bitmap *bitmap)
Free bitmap data.
yaze_status yaze_load_messages(const zelda3_rom *rom, zelda3_message **messages, int *message_count)
Load all text messages from ROM.
void yaze_snes_color_to_rgb(snes_color color, uint8_t *r, uint8_t *g, uint8_t *b)
Convert SNES color to RGB888.
yaze_status yaze_library_init()
Initialize the YAZE library.
const char * yaze_status_to_string(yaze_status status)
Convert a status code to a human-readable string.
Public YAZE API umbrella header.
Core initialization and editor context for the YAZE public API.
Status codes and error helpers for the YAZE public API.
yaze_status
Status codes returned by YAZE functions.
@ YAZE_ERROR_FILE_NOT_FOUND
@ YAZE_ERROR_NOT_INITIALIZED
The Legend of Zelda: A Link to the Past - Data Structures and Constants.