|
12 | 12 | #include <godot_cpp/godot.hpp> |
13 | 13 |
|
14 | 14 | #include "example.h" |
| 15 | +#include "godot_cpp/classes/dir_access.hpp" |
| 16 | +#include "godot_cpp/classes/editor_interface.hpp" |
| 17 | +#include "godot_cpp/classes/engine.hpp" |
| 18 | +#include "godot_cpp/classes/file_access.hpp" |
| 19 | +#include "godot_cpp/classes/gd_extension_manager.hpp" |
15 | 20 | #include "tests.h" |
16 | 21 |
|
17 | 22 | using namespace godot; |
18 | 23 |
|
19 | | -void initialize_example_module(ModuleInitializationLevel p_level) { |
20 | | - if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { |
21 | | - return; |
| 24 | +// Global flag to control the timer thread |
| 25 | +std::atomic keep_running(true); |
| 26 | + |
| 27 | +// Function to simulate extension reload |
| 28 | +void trigger_extension_reload() { |
| 29 | + if (Engine::get_singleton()->is_editor_hint()) { |
| 30 | + if (const EditorInterface *ei = EditorInterface::get_singleton(); ei->get_edited_scene_root()) { |
| 31 | + UtilityFunctions::print("Editor has a loaded scene: ", ei->get_edited_scene_root()->get_name()); |
| 32 | + } else { |
| 33 | + return; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + keep_running = false; |
| 38 | + const auto gdextension_path = String{ "res://example.gdextension" }; |
| 39 | + UtilityFunctions::print("Simulating GDExtension reload..."); |
| 40 | + |
| 41 | + // Reload all extensions |
| 42 | + if (GDExtensionManager *ext_manager = GDExtensionManager::get_singleton()) { |
| 43 | + ext_manager->call_deferred("reload_extension", gdextension_path); |
| 44 | + UtilityFunctions::print("GDExtensionManager::reload_extension(...) called successfully."); |
| 45 | + } else { |
| 46 | + UtilityFunctions::print("Error: GDExtensionManager singleton not available."); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// Timer thread function |
| 51 | +void timer_thread_function(const float interval_seconds) { |
| 52 | + // Single cycle for testing: sleep once, trigger reload, then exit (keep_running=false in trigger). |
| 53 | + while (keep_running) { |
| 54 | + std::this_thread::sleep_for(std::chrono::seconds(static_cast<int>(interval_seconds))); |
| 55 | + trigger_extension_reload(); |
22 | 56 | } |
| 57 | +} |
23 | 58 |
|
24 | | - GDREGISTER_CLASS(ExampleRef); |
25 | | - GDREGISTER_CLASS(ExampleMin); |
26 | | - GDREGISTER_CLASS(Example); |
27 | | - GDREGISTER_VIRTUAL_CLASS(ExampleVirtual); |
28 | | - GDREGISTER_ABSTRACT_CLASS(ExampleAbstractBase); |
29 | | - GDREGISTER_CLASS(ExampleConcrete); |
30 | | - GDREGISTER_CLASS(ExampleBase); |
31 | | - GDREGISTER_CLASS(ExampleChild); |
32 | | - GDREGISTER_RUNTIME_CLASS(ExampleRuntime); |
33 | | - GDREGISTER_CLASS(ExamplePrzykład); |
34 | | - GDREGISTER_INTERNAL_CLASS(ExampleInternal); |
| 59 | +void initialize_example_module(ModuleInitializationLevel p_level) { |
| 60 | + if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) { |
| 61 | + UtilityFunctions::print("Initializing Integration Testing Extension"); |
| 62 | + GDREGISTER_CLASS(ExampleRef); |
| 63 | + GDREGISTER_CLASS(ExampleMin); |
| 64 | + GDREGISTER_CLASS(Example); |
| 65 | + GDREGISTER_VIRTUAL_CLASS(ExampleVirtual); |
| 66 | + GDREGISTER_ABSTRACT_CLASS(ExampleAbstractBase); |
| 67 | + GDREGISTER_CLASS(ExampleConcrete); |
| 68 | + GDREGISTER_CLASS(ExampleBase); |
| 69 | + GDREGISTER_CLASS(ExampleChild); |
| 70 | + GDREGISTER_RUNTIME_CLASS(ExampleRuntime); |
| 71 | + GDREGISTER_CLASS(ExamplePrzykład); |
| 72 | + GDREGISTER_INTERNAL_CLASS(ExampleInternal); |
| 73 | + } else if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) { |
| 74 | + const String lock_path = "user://extension_timer_started"; |
| 75 | + for (const auto &arg : OS::get_singleton()->get_cmdline_args()) { |
| 76 | + if (arg.begins_with("reload") && !FileAccess::file_exists(lock_path)) { |
| 77 | + constexpr float reload_interval = 3.0f; |
| 78 | + UtilityFunctions::print("No lock file found; starting timer thread for hot-reload test."); |
| 79 | + std::thread timer_thread(timer_thread_function, reload_interval); |
| 80 | + timer_thread.detach(); // Detach the thread to run independently |
| 81 | + |
| 82 | + // Create the lock file to prevent future inits from starting duplicates. |
| 83 | + if (const Ref<FileAccess> lock_file = FileAccess::open(lock_path, FileAccess::WRITE); lock_file.is_valid()) { |
| 84 | + lock_file->close(); |
| 85 | + } else { |
| 86 | + UtilityFunctions::print("Warning: Failed to create lock file."); |
| 87 | + } |
| 88 | + } else { |
| 89 | + UtilityFunctions::print("Lock file exists; skipping timer thread start (already triggered reload)."); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
35 | 93 | } |
36 | 94 |
|
37 | 95 | void uninitialize_example_module(ModuleInitializationLevel p_level) { |
38 | | - if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { |
39 | | - return; |
| 96 | + if (p_level == MODULE_INITIALIZATION_LEVEL_SCENE) { |
| 97 | + UtilityFunctions::print("Uninitializing Integration Testing Extension"); |
| 98 | + } else if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) { |
| 99 | + // Stop the timer thread when deinitializing |
| 100 | + keep_running = false; |
40 | 101 | } |
41 | 102 | } |
42 | 103 |
|
43 | 104 | extern "C" { |
44 | 105 | // Initialization. |
45 | 106 | GDExtensionBool GDE_EXPORT example_library_init(GDExtensionInterfaceGetProcAddress p_get_proc_address, GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) { |
46 | | - godot::GDExtensionBinding::InitObject init_obj(p_get_proc_address, p_library, r_initialization); |
| 107 | + GDExtensionBinding::InitObject init_obj(p_get_proc_address, p_library, r_initialization); |
47 | 108 |
|
48 | 109 | init_obj.register_initializer(initialize_example_module); |
49 | 110 | init_obj.register_terminator(uninitialize_example_module); |
|
0 commit comments