godot.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**************************************************************************/
  2. /* godot.hpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include <godot_cpp/core/gdextension_interface_loader.hpp>
  32. namespace godot {
  33. namespace gdextension_interface {
  34. extern "C" GDExtensionInterfaceGetProcAddress get_proc_address;
  35. extern "C" GDExtensionClassLibraryPtr library;
  36. extern "C" void *token;
  37. extern "C" GDExtensionGodotVersion2 godot_version;
  38. } // namespace gdextension_interface
  39. namespace internal {
  40. class DocDataRegistration {
  41. public:
  42. DocDataRegistration(const char *p_hash, int p_uncompressed_size, int p_compressed_size, const unsigned char *p_data);
  43. };
  44. } // namespace internal
  45. enum ModuleInitializationLevel {
  46. MODULE_INITIALIZATION_LEVEL_CORE = GDEXTENSION_INITIALIZATION_CORE,
  47. MODULE_INITIALIZATION_LEVEL_SERVERS = GDEXTENSION_INITIALIZATION_SERVERS,
  48. MODULE_INITIALIZATION_LEVEL_SCENE = GDEXTENSION_INITIALIZATION_SCENE,
  49. MODULE_INITIALIZATION_LEVEL_EDITOR = GDEXTENSION_INITIALIZATION_EDITOR,
  50. MODULE_INITIALIZATION_LEVEL_MAX
  51. };
  52. class GDExtensionBinding {
  53. public:
  54. using Callback = void (*)(ModuleInitializationLevel p_level);
  55. struct InitData {
  56. GDExtensionInitializationLevel minimum_initialization_level = GDEXTENSION_INITIALIZATION_CORE;
  57. Callback init_callback = nullptr;
  58. Callback terminate_callback = nullptr;
  59. GDExtensionMainLoopCallbacks main_loop_callbacks = {};
  60. inline bool has_main_loop_callbacks() const {
  61. return main_loop_callbacks.frame_func || main_loop_callbacks.startup_func || main_loop_callbacks.shutdown_func;
  62. }
  63. };
  64. class InitDataList {
  65. int data_count = 0;
  66. int data_capacity = 0;
  67. InitData **data = nullptr;
  68. public:
  69. void add(InitData *p_cb);
  70. ~InitDataList();
  71. };
  72. static bool api_initialized;
  73. static int level_initialized[MODULE_INITIALIZATION_LEVEL_MAX];
  74. static InitDataList initdata;
  75. static GDExtensionBool init(GDExtensionInterfaceGetProcAddress p_get_proc_address, GDExtensionClassLibraryPtr p_library, InitData *p_init_data, GDExtensionInitialization *r_initialization);
  76. public:
  77. static void initialize_level(void *p_userdata, GDExtensionInitializationLevel p_level);
  78. static void deinitialize_level(void *p_userdata, GDExtensionInitializationLevel p_level);
  79. class InitObject {
  80. GDExtensionInterfaceGetProcAddress get_proc_address;
  81. GDExtensionClassLibraryPtr library;
  82. GDExtensionInitialization *initialization;
  83. mutable InitData *init_data = nullptr;
  84. public:
  85. InitObject(GDExtensionInterfaceGetProcAddress p_get_proc_address, GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization);
  86. void register_initializer(Callback p_init) const;
  87. void register_terminator(Callback p_init) const;
  88. void set_minimum_library_initialization_level(ModuleInitializationLevel p_level) const;
  89. // Register a callback that is called after all initialization levels when Godot is fully initialized.
  90. void register_startup_callback(GDExtensionMainLoopStartupCallback p_callback) const;
  91. // Register a callback that is called for every process frame. This will run after all `_process()` methods on Node, and before `ScriptServer::frame()`.
  92. void register_frame_callback(GDExtensionMainLoopFrameCallback p_callback) const;
  93. // Register a callback that is called before Godot is shutdown when it is still fully initialized.
  94. void register_shutdown_callback(GDExtensionMainLoopShutdownCallback p_callback) const;
  95. GDExtensionBool init() const;
  96. };
  97. };
  98. } // namespace godot