Skip to content

Commit

Permalink
Error and warn functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ParadoxV5 committed Aug 27, 2023
1 parent 69fc0f0 commit 58e634b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
8 changes: 8 additions & 0 deletions include/godot_rb.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ extern bool godot_rb_init_levels[GDEXTENSION_MAX_INITIALIZATION_LEVEL];
extern GDExtensionInterfaceGetProcAddress godot_rb_get_proc;
extern GDExtensionClassLibraryPtr godot_rb_library;

extern struct godot_rb_gdextension {
GDExtensionInterfacePrintErrorWithMessage print_error_with_message;
GDExtensionInterfacePrintWarningWithMessage print_warning_with_message;
} godot_rb_gdextension;

__attribute__((used)) GDExtensionBool godot_rb_main(
GDExtensionInterfaceGetProcAddress p_get_proc_address,
GDExtensionClassLibraryPtr p_library,
GDExtensionInitialization* r_initialization
);

void godot_rb_error(const char* message, const char* func, const char* file, int32_t line);
void godot_rb_warn (const char* message, const char* func, const char* file, int32_t line);

#endif
9 changes: 4 additions & 5 deletions src/cleanup.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ void godot_rb_cleanup_core() {
if(godot_rb_init_levels[GDEXTENSION_INITIALIZATION_CORE]) {
int err = ruby_cleanup(EXIT_SUCCESS);
if(err)
printf("Error %i", err);//TODO: Error
godot_rb_error("Ruby ran into a problem while exiting.", __func__, __FILE__, __LINE__);
} else
puts("Godot.rb\tthis level wasn't set up.");
//TODO: replace select if not all `stdio` printing statements with Godot functions
godot_rb_warn("Godot.rb hasn't set this init level up.", __func__, __FILE__, __LINE__);
}

void (*godot_rb_cleanup_functions[GDEXTENSION_MAX_INITIALIZATION_LEVEL])(void) = {
Expand All @@ -16,9 +15,9 @@ void (*godot_rb_cleanup_functions[GDEXTENSION_MAX_INITIALIZATION_LEVEL])(void) =
void godot_rb_cleanup(__attribute__((unused)) void* userdata, GDExtensionInitializationLevel p_level) {
void (*func)(void) = godot_rb_cleanup_functions[p_level];
if(func) {
printf("Godot.rb\tcleaning up level %u...\n", p_level);
printf("cleaning up Godot.rb init level %u...\n", p_level);
func();
godot_rb_init_levels[p_level] = false;
printf("Godot.rb\tlevel %u cleaned up.\n", p_level);
printf("Godot.rb init level %u cleaned up.\n", p_level);
}
}
14 changes: 13 additions & 1 deletion src/godot_rb.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
bool godot_rb_init_levels[GDEXTENSION_MAX_INITIALIZATION_LEVEL];
GDExtensionInterfaceGetProcAddress godot_rb_get_proc = NULL;
GDExtensionClassLibraryPtr godot_rb_library = NULL;
struct godot_rb_gdextension;

__attribute__((used)) GDExtensionBool godot_rb_main(
GDExtensionInterfaceGetProcAddress p_get_proc_address,
Expand All @@ -16,12 +17,23 @@ __attribute__((used)) GDExtensionBool godot_rb_main(
r_initialization->minimum_initialization_level = GDEXTENSION_INITIALIZATION_CORE;
r_initialization->initialize = &godot_rb_setup;
r_initialization->deinitialize = &godot_rb_cleanup;
// Save GDExtension API
// Save GDExtension Interface
godot_rb_get_proc = p_get_proc_address;
godot_rb_library = p_library;
// Initialize API variables
for(GDExtensionInitializationLevel i = 0; i < GDEXTENSION_MAX_INITIALIZATION_LEVEL; ++i)
godot_rb_init_levels[i] = false;
// Save GDExtension API
#define LOAD(proc_t, proc) godot_rb_gdextension.proc = (proc_t)godot_rb_get_proc(#proc);
LOAD(GDExtensionInterfacePrintErrorWithMessage, print_error_with_message)
LOAD(GDExtensionInterfacePrintWarningWithMessage, print_warning_with_message)
// Success
return true;
}

void godot_rb_error(const char* message, const char* func, const char* file, int32_t line) {
godot_rb_gdextension.print_error_with_message (message, message, func, file, line, false);
}
void godot_rb_warn (const char* message, const char* func, const char* file, int32_t line) {
godot_rb_gdextension.print_warning_with_message(message, message, func, file, line, false);
}
6 changes: 3 additions & 3 deletions src/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ bool godot_rb_setup_core(void) {
ruby_sysinit(&argc, &argv);
int err = ruby_setup();
if(err) {
printf("Error %i", err);//TODO: Error
godot_rb_error("Ruby ran into a problem while starting.", __func__, __FILE__, __LINE__);
return false;
}
ruby_script("godot_rb.c");
Expand All @@ -24,10 +24,10 @@ bool (*godot_rb_setup_functions[GDEXTENSION_MAX_INITIALIZATION_LEVEL])(void) = {
void godot_rb_setup(__attribute__((unused)) void* userdata, GDExtensionInitializationLevel p_level) {
bool (*func)(void) = godot_rb_setup_functions[p_level];
if(func) {
printf("Godot.rb\tsetting up level %u...\n", p_level);
printf("setting up Godot.rb init level %u...\n", p_level);
if(func()) {
godot_rb_init_levels[p_level] = true;
printf("Godot.rb\tlevel %u set up.\n", p_level);
printf("Godot.rb init level %u set up.\n", p_level);
}
}
}

0 comments on commit 58e634b

Please sign in to comment.