yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
yaze.cc
Go to the documentation of this file.
1// C API implementation - no heavy GUI/editor dependencies
2#include <cstring>
3#include <memory>
4#include <stdexcept>
5#include <string>
6#include <vector>
7
9#include "rom/rom.h"
10#include "yaze.h"
11#include "yaze_config.h"
12#include "zelda3/game_data.h"
14
15// Static variables for library state
16static bool g_library_initialized = false;
17
18// Version and initialization functions
20 if (g_library_initialized) {
21 return YAZE_OK;
22 }
23
24 // Initialize SDL and other subsystems if needed
25 g_library_initialized = true;
26 return YAZE_OK;
27}
28
30 if (!g_library_initialized) {
31 return;
32 }
33
34 // Cleanup subsystems
35 g_library_initialized = false;
36
37 return;
38}
39
41 switch (status) {
42 case YAZE_OK:
43 return "Success";
45 return "Unknown error";
47 return "Invalid argument";
49 return "File not found";
51 return "Memory allocation failed";
52 case YAZE_ERROR_IO:
53 return "I/O operation failed";
55 return "Data corruption detected";
57 return "Component not initialized";
58 default:
59 return "Unknown status code";
60 }
61}
62
64
66
67bool yaze_check_version_compatibility(const char* expected_version) {
68 if (expected_version == nullptr) {
69 return false;
70 }
71 return strcmp(expected_version, YAZE_VERSION_STRING) == 0;
72}
73
74yaze_status yaze_init(yaze_editor_context* context, const char* rom_filename) {
75 if (context == nullptr) {
77 }
78
79 if (!g_library_initialized) {
80 yaze_status init_status = yaze_library_init();
81 if (init_status != YAZE_OK) {
82 return init_status;
83 }
84 }
85
86 context->rom = nullptr;
87 context->error_message = nullptr;
88
89 if (rom_filename != nullptr && strlen(rom_filename) > 0) {
90 context->rom = yaze_load_rom(rom_filename);
91 if (context->rom == nullptr) {
92 context->error_message = "Failed to load ROM file";
94 }
95 }
96
97 return YAZE_OK;
98}
99
101 if (context == nullptr) {
103 }
104
105 if (context->rom != nullptr) {
106 yaze_unload_rom(context->rom);
107 context->rom = nullptr;
108 }
109
110 context->error_message = nullptr;
111 return YAZE_OK;
112}
113
114zelda3_rom* yaze_load_rom(const char* filename) {
115 if (filename == nullptr || strlen(filename) == 0) {
116 return nullptr;
117 }
118
119 auto internal_rom = std::make_unique<yaze::Rom>();
120 if (!internal_rom->LoadFromFile(filename).ok()) {
121 return nullptr;
122 }
123
124 // Create and load GameData
125 auto internal_game_data = std::make_unique<yaze::zelda3::GameData>();
126 auto load_status = yaze::zelda3::LoadGameData(*internal_rom, *internal_game_data);
127 if (!load_status.ok()) {
128 return nullptr;
129 }
130
131 auto* rom = new zelda3_rom();
132 rom->filename = filename;
133 rom->impl = internal_rom.release(); // Transfer ownership
134 rom->game_data = internal_game_data.release(); // Transfer ownership
135 rom->data = const_cast<uint8_t*>(static_cast<yaze::Rom*>(rom->impl)->data());
136 rom->size = static_cast<yaze::Rom*>(rom->impl)->size();
137 rom->version = ZELDA3_VERSION_US; // Default, should be detected
138 rom->is_modified = false;
139 return rom;
140}
141
143 if (rom == nullptr) {
144 return;
145 }
146
147 if (rom->impl != nullptr) {
148 delete static_cast<yaze::Rom*>(rom->impl);
149 rom->impl = nullptr;
150 }
151
152 if (rom->game_data != nullptr) {
153 delete static_cast<yaze::zelda3::GameData*>(rom->game_data);
154 rom->game_data = nullptr;
155 }
156
157 delete rom;
158}
159
160int yaze_save_rom(zelda3_rom* rom, const char* filename) {
161 if (rom == nullptr || filename == nullptr) {
163 }
164
165 if (rom->impl == nullptr) {
167 }
168
169 auto* internal_rom = static_cast<yaze::Rom*>(rom->impl);
170 auto status = internal_rom->SaveToFile(yaze::Rom::SaveSettings{
171 .backup = true, .save_new = false, .filename = filename});
172
173 if (!status.ok()) {
174 return YAZE_ERROR_IO;
175 }
176
177 rom->is_modified = false;
178 return YAZE_OK;
179}
180
181yaze_bitmap yaze_load_bitmap(const char* filename) {
182 yaze_bitmap bitmap;
183 bitmap.width = 0;
184 bitmap.height = 0;
185 bitmap.bpp = 0;
186 bitmap.data = nullptr;
187 return bitmap;
188}
189
191 int palette_set, int palette,
192 int color) {
193 snes_color color_struct;
194 color_struct.red = 0;
195 color_struct.green = 0;
196 color_struct.blue = 0;
197
198 if (rom->game_data) {
199 auto* game_data = static_cast<yaze::zelda3::GameData*>(rom->game_data);
200 auto get_color =
201 game_data->palette_groups
203 ->palette(palette)[color];
204 color_struct = get_color.rom_color();
205
206 return color_struct;
207 }
208
209 return color_struct;
210}
211
213 if (rom->impl == nullptr) {
214 return nullptr;
215 }
216
217 yaze::Rom* internal_rom = static_cast<yaze::Rom*>(rom->impl);
218 auto* game_data = static_cast<yaze::zelda3::GameData*>(rom->game_data);
219 auto internal_overworld = new yaze::zelda3::Overworld(internal_rom, game_data);
220 if (!internal_overworld->Load(internal_rom).ok()) {
221 return nullptr;
222 }
223
224 zelda3_overworld* overworld = new zelda3_overworld();
225 overworld->impl = internal_overworld;
226 int map_id = 0;
227 for (const auto& ow_map : internal_overworld->overworld_maps()) {
228 overworld->maps[map_id] = new zelda3_overworld_map();
229 overworld->maps[map_id]->id = map_id;
230 map_id++;
231 }
232 return overworld;
233}
234
236 if (rom->impl == nullptr) {
237 return nullptr;
238 }
239 yaze::Rom* internal_rom = static_cast<yaze::Rom*>(rom->impl);
241 return rooms;
242}
243
245 int* message_count) {
246 if (rom == nullptr || messages == nullptr || message_count == nullptr) {
248 }
249
250 if (rom->impl == nullptr) {
252 }
253
254 try {
255 // Use LoadAllTextData from message_data.h
256 std::vector<yaze::editor::MessageData> message_data =
258
259 *message_count = static_cast<int>(message_data.size());
260 *messages = new zelda3_message[*message_count];
261
262 for (size_t i = 0; i < message_data.size(); ++i) {
263 const auto& msg = message_data[i];
264 (*messages)[i].id = msg.ID;
265 (*messages)[i].rom_address = msg.Address;
266 (*messages)[i].length = static_cast<uint16_t>(msg.RawString.length());
267
268 // Allocate and copy string data
269 (*messages)[i].raw_data = new uint8_t[msg.Data.size()];
270 std::memcpy((*messages)[i].raw_data, msg.Data.data(), msg.Data.size());
271
272 (*messages)[i].parsed_text = new char[msg.ContentsParsed.length() + 1];
273 // Safe string copy with bounds checking
274 std::memcpy((*messages)[i].parsed_text, msg.ContentsParsed.c_str(),
275 msg.ContentsParsed.length());
276 (*messages)[i].parsed_text[msg.ContentsParsed.length()] = '\0';
277
278 (*messages)[i].is_compressed = false; // TODO: Detect compression
279 (*messages)[i].encoding_type = 0; // TODO: Detect encoding
280 }
281 } catch (const std::exception& e) {
282 return YAZE_ERROR_MEMORY;
283 }
284
285 return YAZE_OK;
286}
287
288// Additional API functions implementation
289
290// Graphics functions
292 if (bitmap != nullptr && bitmap->data != nullptr) {
293 delete[] bitmap->data;
294 bitmap->data = nullptr;
295 bitmap->width = 0;
296 bitmap->height = 0;
297 bitmap->bpp = 0;
298 }
299}
300
301yaze_bitmap yaze_create_bitmap(int width, int height, uint8_t bpp) {
302 yaze_bitmap bitmap = {};
303
304 if (width <= 0 || height <= 0 ||
305 (bpp != 1 && bpp != 2 && bpp != 4 && bpp != 8)) {
306 return bitmap; // Return empty bitmap on invalid args
307 }
308
309 bitmap.width = width;
310 bitmap.height = height;
311 bitmap.bpp = bpp;
312 bitmap.data = new uint8_t[width * height]();
313
314 return bitmap;
315}
316
317snes_color yaze_rgb_to_snes_color(uint8_t r, uint8_t g, uint8_t b) {
318 snes_color color = {};
319 color.red = r; // Store full 8-bit values (existing code expects this)
320 color.green = g;
321 color.blue = b;
322 return color;
323}
324
325void yaze_snes_color_to_rgb(snes_color color, uint8_t* r, uint8_t* g,
326 uint8_t* b) {
327 if (r != nullptr) *r = static_cast<uint8_t>(color.red);
328 if (g != nullptr) *g = static_cast<uint8_t>(color.green);
329 if (b != nullptr) *b = static_cast<uint8_t>(color.blue);
330}
331
332// Version detection functions
333zelda3_version zelda3_detect_version(const uint8_t* rom_data, size_t size) {
334 if (rom_data == nullptr || size < 0x100000) {
336 }
337
338 // TODO: Implement proper version detection based on ROM header
339 return ZELDA3_VERSION_US; // Default assumption
340}
341
343 switch (version) {
345 return "US/North American";
347 return "Japanese";
349 return "European";
351 return "Prototype";
353 return "Randomizer";
354 default:
355 return "Unknown";
356 }
357}
358
360 zelda3_version version) {
361 switch (version) {
363 return &zelda3_us_pointers;
365 return &zelda3_jp_pointers;
366 default:
367 return &zelda3_us_pointers; // Default fallback
368 }
369}
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:24
absl::Status SaveToFile(const SaveSettings &settings)
Definition rom.cc:164
Represents the full Overworld data, light and dark world.
Definition overworld.h:217
yaze_status yaze_library_init()
Initialize the YAZE library.
Definition yaze.cc:19
int yaze_get_version_number()
Get the current YAZE version number.
Definition yaze.cc:65
const char * yaze_get_version_string()
Get the current YAZE version string.
Definition yaze.cc:63
yaze_status
Status codes returned by YAZE functions.
Definition yaze.h:65
bool yaze_check_version_compatibility(const char *expected_version)
Check if the current YAZE version is compatible with the expected version.
Definition yaze.cc:67
yaze_status yaze_shutdown(yaze_editor_context *context)
Shutdown and clean up a YAZE editor context.
Definition yaze.cc:100
yaze_status yaze_init(yaze_editor_context *context, const char *rom_filename)
Initialize a YAZE editor context.
Definition yaze.cc:74
const char * yaze_status_to_string(yaze_status status)
Convert a status code to a human-readable string.
Definition yaze.cc:40
void yaze_library_shutdown()
Shutdown the YAZE library.
Definition yaze.cc:29
@ YAZE_OK
Definition yaze.h:66
@ YAZE_ERROR_CORRUPTION
Definition yaze.h:72
@ YAZE_ERROR_IO
Definition yaze.h:71
@ YAZE_ERROR_FILE_NOT_FOUND
Definition yaze.h:69
@ YAZE_ERROR_INVALID_ARG
Definition yaze.h:68
@ YAZE_ERROR_MEMORY
Definition yaze.h:70
@ YAZE_ERROR_NOT_INITIALIZED
Definition yaze.h:73
@ YAZE_ERROR_UNKNOWN
Definition yaze.h:67
snes_color yaze_rgb_to_snes_color(uint8_t r, uint8_t g, uint8_t b)
Convert RGB888 color to SNES color.
Definition yaze.cc:317
yaze_bitmap yaze_load_bitmap(const char *filename)
Load a bitmap from file.
Definition yaze.cc:181
yaze_bitmap yaze_create_bitmap(int width, int height, uint8_t bpp)
Create an empty bitmap.
Definition yaze.cc:301
void yaze_free_bitmap(yaze_bitmap *bitmap)
Free bitmap data.
Definition yaze.cc:291
void yaze_snes_color_to_rgb(snes_color color, uint8_t *r, uint8_t *g, uint8_t *b)
Convert SNES color to RGB888.
Definition yaze.cc:325
yaze_status yaze_load_messages(const zelda3_rom *rom, zelda3_message **messages, int *message_count)
Load all text messages from ROM.
Definition yaze.cc:244
zelda3_overworld * yaze_load_overworld(const zelda3_rom *rom)
Load the overworld from ROM.
Definition yaze.cc:212
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.
Definition yaze.cc:142
zelda3_rom * yaze_load_rom(const char *filename)
Load a ROM file.
Definition yaze.cc:114
int yaze_save_rom(zelda3_rom *rom, const char *filename)
Save ROM to file.
Definition yaze.cc:160
const char * zelda3_version_to_string(zelda3_version version)
Get version name as string.
Definition yaze.cc:342
struct zelda3_rom zelda3_rom
ROM data structure.
zelda3_version
Different versions of the game supported by YAZE.
Definition zelda.h:33
const zelda3_version_pointers * zelda3_get_version_pointers(zelda3_version version)
Get version-specific pointers.
Definition yaze.cc:359
zelda3_version zelda3_detect_version(const uint8_t *rom_data, size_t size)
Detect ROM version from header data.
Definition yaze.cc:333
@ ZELDA3_VERSION_JP
Definition zelda.h:36
@ ZELDA3_VERSION_PROTO
Definition zelda.h:38
@ ZELDA3_VERSION_EU
Definition zelda.h:37
@ ZELDA3_VERSION_US
Definition zelda.h:35
@ ZELDA3_VERSION_RANDOMIZER
Definition zelda.h:39
@ ZELDA3_VERSION_UNKNOWN
Definition zelda.h:34
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.
Definition yaze.cc:190
#define YAZE_VERSION_NUMBER
Definition yaze.h:44
#define YAZE_VERSION_STRING
Definition yaze.h:43
std::vector< MessageData > ReadAllTextData(uint8_t *rom, int pos)
constexpr const char * kPaletteGroupAddressesKeys[]
absl::Status LoadGameData(Rom &rom, GameData &data, const LoadOptions &options)
Loads all Zelda3-specific game data from a generic ROM.
Definition game_data.cc:80
SNES color in 15-bit RGB format (BGR555)
Definition yaze.h:218
uint16_t green
Definition yaze.h:220
uint16_t red
Definition yaze.h:219
uint16_t blue
Definition yaze.h:221
PaletteGroup * get_group(const std::string &group_name)
auto palette(int i) const
gfx::PaletteGroupMap palette_groups
Definition game_data.h:89
Bitmap data structure.
Definition yaze.h:171
uint8_t * data
Definition yaze.h:175
int height
Definition yaze.h:173
uint8_t bpp
Definition yaze.h:174
int width
Definition yaze.h:172
zelda3_rom * rom
Definition yaze.h:50
const char * error_message
Definition yaze.h:51
Complete dungeon room data.
Definition zelda.h:461
In-game text message data.
Definition zelda.h:272
uint16_t id
Definition zelda.h:273
Complete overworld data.
Definition zelda.h:333
void * impl
Definition zelda.h:334
zelda3_overworld_map ** maps
Definition zelda.h:335
ROM data structure.
Definition zelda.h:210
uint8_t * data
Definition zelda.h:212
bool is_modified
Definition zelda.h:215
void * game_data
Definition zelda.h:217
void * impl
Definition zelda.h:216
ROM data pointers for different game versions.
Definition zelda.h:71
zelda3_dungeon_room * yaze_load_all_rooms(const zelda3_rom *rom)
Definition yaze.cc:235
Yet Another Zelda3 Editor (YAZE) - Public C API.