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 "yaze.h"
3
4#include <cstring>
5#include <memory>
6#include <stdexcept>
7#include <string>
8#include <vector>
9
11#include "app/rom.h"
13#include "yaze_config.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
65}
66
70
71bool yaze_check_version_compatibility(const char* expected_version) {
72 if (expected_version == nullptr) {
73 return false;
74 }
75 return strcmp(expected_version, YAZE_VERSION_STRING) == 0;
76}
77
78yaze_status yaze_init(yaze_editor_context* context, const char* rom_filename) {
79 if (context == nullptr) {
81 }
82
83 if (!g_library_initialized) {
84 yaze_status init_status = yaze_library_init();
85 if (init_status != YAZE_OK) {
86 return init_status;
87 }
88 }
89
90 context->rom = nullptr;
91 context->error_message = nullptr;
92
93 if (rom_filename != nullptr && strlen(rom_filename) > 0) {
94 context->rom = yaze_load_rom(rom_filename);
95 if (context->rom == nullptr) {
96 context->error_message = "Failed to load ROM file";
98 }
99 }
100
101 return YAZE_OK;
102}
103
105 if (context == nullptr) {
107 }
108
109 if (context->rom != nullptr) {
110 yaze_unload_rom(context->rom);
111 context->rom = nullptr;
112 }
113
114 context->error_message = nullptr;
115 return YAZE_OK;
116}
117
118zelda3_rom* yaze_load_rom(const char* filename) {
119 if (filename == nullptr || strlen(filename) == 0) {
120 return nullptr;
121 }
122
123 auto internal_rom = std::make_unique<yaze::Rom>();
124 if (!internal_rom->LoadFromFile(filename).ok()) {
125 return nullptr;
126 }
127
128 auto* rom = new zelda3_rom();
129 rom->filename = filename;
130 rom->impl = internal_rom.release(); // Transfer ownership
131 rom->data = const_cast<uint8_t*>(static_cast<yaze::Rom*>(rom->impl)->data());
132 rom->size = static_cast<yaze::Rom*>(rom->impl)->size();
133 rom->version = ZELDA3_VERSION_US; // Default, should be detected
134 rom->is_modified = false;
135 return rom;
136}
137
139 if (rom == nullptr) {
140 return;
141 }
142
143 if (rom->impl != nullptr) {
144 delete static_cast<yaze::Rom*>(rom->impl);
145 rom->impl = nullptr;
146 }
147
148 delete rom;
149}
150
151int yaze_save_rom(zelda3_rom* rom, const char* filename) {
152 if (rom == nullptr || filename == nullptr) {
154 }
155
156 if (rom->impl == nullptr) {
158 }
159
160 auto* internal_rom = static_cast<yaze::Rom*>(rom->impl);
161 auto status = internal_rom->SaveToFile(yaze::Rom::SaveSettings{
162 .backup = true, .save_new = false, .filename = filename});
163
164 if (!status.ok()) {
165 return YAZE_ERROR_IO;
166 }
167
168 rom->is_modified = false;
169 return YAZE_OK;
170}
171
172yaze_bitmap yaze_load_bitmap(const char* filename) {
173 yaze_bitmap bitmap;
174 bitmap.width = 0;
175 bitmap.height = 0;
176 bitmap.bpp = 0;
177 bitmap.data = nullptr;
178 return bitmap;
179}
180
182 int palette_set, int palette,
183 int color) {
184 snes_color color_struct;
185 color_struct.red = 0;
186 color_struct.green = 0;
187 color_struct.blue = 0;
188
189 if (rom->impl) {
190 yaze::Rom* internal_rom = static_cast<yaze::Rom*>(rom->impl);
191 auto get_color =
192 internal_rom->palette_group()
193 .get_group(yaze::gfx::kPaletteGroupAddressesKeys[palette_set])
194 ->palette(palette)[color];
195 color_struct = get_color.rom_color();
196
197 return color_struct;
198 }
199
200 return color_struct;
201}
202
204 if (rom->impl == nullptr) {
205 return nullptr;
206 }
207
208 yaze::Rom* internal_rom = static_cast<yaze::Rom*>(rom->impl);
209 auto internal_overworld = new yaze::zelda3::Overworld(internal_rom);
210 if (!internal_overworld->Load(internal_rom).ok()) {
211 return nullptr;
212 }
213
214 zelda3_overworld* overworld = new zelda3_overworld();
215 overworld->impl = internal_overworld;
216 int map_id = 0;
217 for (const auto& ow_map : internal_overworld->overworld_maps()) {
218 overworld->maps[map_id] = new zelda3_overworld_map();
219 overworld->maps[map_id]->id = map_id;
220 map_id++;
221 }
222 return overworld;
223}
224
226 if (rom->impl == nullptr) {
227 return nullptr;
228 }
229 yaze::Rom* internal_rom = static_cast<yaze::Rom*>(rom->impl);
231 return rooms;
232}
233
235 int* message_count) {
236 if (rom == nullptr || messages == nullptr || message_count == nullptr) {
238 }
239
240 if (rom->impl == nullptr) {
242 }
243
244 try {
245 // Use LoadAllTextData from message_data.h
246 std::vector<yaze::editor::MessageData> message_data =
248
249 *message_count = static_cast<int>(message_data.size());
250 *messages = new zelda3_message[*message_count];
251
252 for (size_t i = 0; i < message_data.size(); ++i) {
253 const auto& msg = message_data[i];
254 (*messages)[i].id = msg.ID;
255 (*messages)[i].rom_address = msg.Address;
256 (*messages)[i].length = static_cast<uint16_t>(msg.RawString.length());
257
258 // Allocate and copy string data
259 (*messages)[i].raw_data = new uint8_t[msg.Data.size()];
260 std::memcpy((*messages)[i].raw_data, msg.Data.data(), msg.Data.size());
261
262 (*messages)[i].parsed_text = new char[msg.ContentsParsed.length() + 1];
263 // Safe string copy with bounds checking
264 std::memcpy((*messages)[i].parsed_text, msg.ContentsParsed.c_str(), msg.ContentsParsed.length());
265 (*messages)[i].parsed_text[msg.ContentsParsed.length()] = '\0';
266
267 (*messages)[i].is_compressed = false; // TODO: Detect compression
268 (*messages)[i].encoding_type = 0; // TODO: Detect encoding
269 }
270 } catch (const std::exception& e) {
271 return YAZE_ERROR_MEMORY;
272 }
273
274 return YAZE_OK;
275}
276
277// Additional API functions implementation
278
279// Graphics functions
281 if (bitmap != nullptr && bitmap->data != nullptr) {
282 delete[] bitmap->data;
283 bitmap->data = nullptr;
284 bitmap->width = 0;
285 bitmap->height = 0;
286 bitmap->bpp = 0;
287 }
288}
289
290yaze_bitmap yaze_create_bitmap(int width, int height, uint8_t bpp) {
291 yaze_bitmap bitmap = {};
292
293 if (width <= 0 || height <= 0 ||
294 (bpp != 1 && bpp != 2 && bpp != 4 && bpp != 8)) {
295 return bitmap; // Return empty bitmap on invalid args
296 }
297
298 bitmap.width = width;
299 bitmap.height = height;
300 bitmap.bpp = bpp;
301 bitmap.data = new uint8_t[width * height]();
302
303 return bitmap;
304}
305
306snes_color yaze_rgb_to_snes_color(uint8_t r, uint8_t g, uint8_t b) {
307 snes_color color = {};
308 color.red = r; // Store full 8-bit values (existing code expects this)
309 color.green = g;
310 color.blue = b;
311 return color;
312}
313
314void yaze_snes_color_to_rgb(snes_color color, uint8_t* r, uint8_t* g,
315 uint8_t* b) {
316 if (r != nullptr)
317 *r = static_cast<uint8_t>(color.red);
318 if (g != nullptr)
319 *g = static_cast<uint8_t>(color.green);
320 if (b != nullptr)
321 *b = static_cast<uint8_t>(color.blue);
322}
323
324// Version detection functions
325zelda3_version zelda3_detect_version(const uint8_t* rom_data, size_t size) {
326 if (rom_data == nullptr || size < 0x100000) {
328 }
329
330 // TODO: Implement proper version detection based on ROM header
331 return ZELDA3_VERSION_US; // Default assumption
332}
333
335 switch (version) {
337 return "US/North American";
339 return "Japanese";
341 return "European";
343 return "Prototype";
345 return "Randomizer";
346 default:
347 return "Unknown";
348 }
349}
350
352 zelda3_version version) {
353 switch (version) {
355 return &zelda3_us_pointers;
357 return &zelda3_jp_pointers;
358 default:
359 return &zelda3_us_pointers; // Default fallback
360 }
361}
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:71
auto palette_group() const
Definition rom.h:213
absl::Status SaveToFile(const SaveSettings &settings)
Definition rom.cc:536
Represents the full Overworld data, light and dark world.
Definition overworld.h:135
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:67
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:55
bool yaze_check_version_compatibility(const char *expected_version)
Check if the current YAZE version is compatible with the expected version.
Definition yaze.cc:71
yaze_status yaze_shutdown(yaze_editor_context *context)
Shutdown and clean up a YAZE editor context.
Definition yaze.cc:104
yaze_status yaze_init(yaze_editor_context *context, const char *rom_filename)
Initialize a YAZE editor context.
Definition yaze.cc:78
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:56
@ YAZE_ERROR_CORRUPTION
Definition yaze.h:62
@ YAZE_ERROR_IO
Definition yaze.h:61
@ YAZE_ERROR_FILE_NOT_FOUND
Definition yaze.h:59
@ YAZE_ERROR_INVALID_ARG
Definition yaze.h:58
@ YAZE_ERROR_MEMORY
Definition yaze.h:60
@ YAZE_ERROR_NOT_INITIALIZED
Definition yaze.h:63
@ YAZE_ERROR_UNKNOWN
Definition yaze.h:57
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:306
yaze_bitmap yaze_load_bitmap(const char *filename)
Load a bitmap from file.
Definition yaze.cc:172
yaze_bitmap yaze_create_bitmap(int width, int height, uint8_t bpp)
Create an empty bitmap.
Definition yaze.cc:290
void yaze_free_bitmap(yaze_bitmap *bitmap)
Free bitmap data.
Definition yaze.cc:280
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:314
yaze_status yaze_load_messages(const zelda3_rom *rom, zelda3_message **messages, int *message_count)
Load all text messages from ROM.
Definition yaze.cc:234
zelda3_overworld * yaze_load_overworld(const zelda3_rom *rom)
Load the overworld from ROM.
Definition yaze.cc:203
void yaze_unload_rom(zelda3_rom *rom)
Unload and free ROM data.
Definition yaze.cc:138
zelda3_rom * yaze_load_rom(const char *filename)
Load a ROM file.
Definition yaze.cc:118
int yaze_save_rom(zelda3_rom *rom, const char *filename)
Save ROM to file.
Definition yaze.cc:151
const char * zelda3_version_to_string(zelda3_version version)
Get version name as string.
Definition yaze.cc:334
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:351
zelda3_version zelda3_detect_version(const uint8_t *rom_data, size_t size)
Detect ROM version from header data.
Definition yaze.cc:325
@ 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:181
#define YAZE_VERSION_NUMBER
Definition yaze.h:35
#define YAZE_VERSION_STRING
Definition yaze.h:32
std::vector< MessageData > ReadAllTextData(uint8_t *rom, int pos)
constexpr const char * kPaletteGroupAddressesKeys[]
SNES color in 15-bit RGB format (BGR555)
Definition yaze.h:208
uint16_t green
Definition yaze.h:210
uint16_t red
Definition yaze.h:209
uint16_t blue
Definition yaze.h:211
Bitmap data structure.
Definition yaze.h:161
uint8_t * data
Definition yaze.h:165
int height
Definition yaze.h:163
uint8_t bpp
Definition yaze.h:164
int width
Definition yaze.h:162
zelda3_rom * rom
Definition yaze.h:40
const char * error_message
Definition yaze.h:41
Complete dungeon room data.
Definition zelda.h:460
In-game text message data.
Definition zelda.h:271
uint16_t id
Definition zelda.h:272
Overworld map data.
Definition zelda.h:294
Complete overworld data.
Definition zelda.h:332
void * impl
Definition zelda.h:333
zelda3_overworld_map ** maps
Definition zelda.h:334
ROM data structure.
Definition zelda.h:210
uint8_t * data
Definition zelda.h:212
bool is_modified
Definition zelda.h:215
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:225
Yet Another Zelda3 Editor (YAZE) - Public C API.