editor_internal_calls.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*************************************************************************/
  2. /* editor_internal_calls.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "editor_internal_calls.h"
  31. #ifdef UNIX_ENABLED
  32. #include <unistd.h> // access
  33. #endif
  34. #include "core/config/project_settings.h"
  35. #include "core/os/os.h"
  36. #include "core/version.h"
  37. #include "editor/debugger/editor_debugger_node.h"
  38. #include "editor/editor_node.h"
  39. #include "editor/editor_paths.h"
  40. #include "editor/editor_scale.h"
  41. #include "editor/editor_settings.h"
  42. #include "editor/plugins/script_editor_plugin.h"
  43. #include "main/main.h"
  44. #include "../csharp_script.h"
  45. #include "../godotsharp_dirs.h"
  46. #include "../utils/macos_utils.h"
  47. #include "code_completion.h"
  48. #include "../interop_types.h"
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. void godot_icall_GodotSharpDirs_ResMetadataDir(godot_string *r_dest) {
  53. memnew_placement(r_dest, String(GodotSharpDirs::get_res_metadata_dir()));
  54. }
  55. void godot_icall_GodotSharpDirs_MonoUserDir(godot_string *r_dest) {
  56. memnew_placement(r_dest, String(GodotSharpDirs::get_mono_user_dir()));
  57. }
  58. void godot_icall_GodotSharpDirs_BuildLogsDirs(godot_string *r_dest) {
  59. #ifdef TOOLS_ENABLED
  60. memnew_placement(r_dest, String(GodotSharpDirs::get_build_logs_dir()));
  61. #else
  62. return nullptr;
  63. #endif
  64. }
  65. void godot_icall_GodotSharpDirs_DataEditorToolsDir(godot_string *r_dest) {
  66. #ifdef TOOLS_ENABLED
  67. memnew_placement(r_dest, String(GodotSharpDirs::get_data_editor_tools_dir()));
  68. #else
  69. return nullptr;
  70. #endif
  71. }
  72. void godot_icall_EditorProgress_Create(const godot_string *p_task, const godot_string *p_label, int32_t p_amount, bool p_can_cancel) {
  73. String task = *reinterpret_cast<const String *>(p_task);
  74. String label = *reinterpret_cast<const String *>(p_label);
  75. EditorNode::progress_add_task(task, label, p_amount, (bool)p_can_cancel);
  76. }
  77. void godot_icall_EditorProgress_Dispose(const godot_string *p_task) {
  78. String task = *reinterpret_cast<const String *>(p_task);
  79. EditorNode::progress_end_task(task);
  80. }
  81. bool godot_icall_EditorProgress_Step(const godot_string *p_task, const godot_string *p_state, int32_t p_step, bool p_force_refresh) {
  82. String task = *reinterpret_cast<const String *>(p_task);
  83. String state = *reinterpret_cast<const String *>(p_state);
  84. return EditorNode::progress_task_step(task, state, p_step, (bool)p_force_refresh);
  85. }
  86. void godot_icall_Internal_FullExportTemplatesDir(godot_string *r_dest) {
  87. String full_templates_dir = EditorPaths::get_singleton()->get_export_templates_dir().path_join(VERSION_FULL_CONFIG);
  88. memnew_placement(r_dest, String(full_templates_dir));
  89. }
  90. bool godot_icall_Internal_IsMacOSAppBundleInstalled(const godot_string *p_bundle_id) {
  91. #ifdef MACOS_ENABLED
  92. String bundle_id = *reinterpret_cast<const String *>(p_bundle_id);
  93. return (bool)macos_is_app_bundle_installed(bundle_id);
  94. #else
  95. (void)p_bundle_id; // UNUSED
  96. return (bool)false;
  97. #endif
  98. }
  99. bool godot_icall_Internal_GodotIs32Bits() {
  100. return sizeof(void *) == 4;
  101. }
  102. bool godot_icall_Internal_GodotIsRealTDouble() {
  103. #ifdef REAL_T_IS_DOUBLE
  104. return (bool)true;
  105. #else
  106. return (bool)false;
  107. #endif
  108. }
  109. void godot_icall_Internal_GodotMainIteration() {
  110. Main::iteration();
  111. }
  112. bool godot_icall_Internal_IsAssembliesReloadingNeeded() {
  113. #ifdef GD_MONO_HOT_RELOAD
  114. return (bool)CSharpLanguage::get_singleton()->is_assembly_reloading_needed();
  115. #else
  116. return (bool)false;
  117. #endif
  118. }
  119. void godot_icall_Internal_ReloadAssemblies(bool p_soft_reload) {
  120. #ifdef GD_MONO_HOT_RELOAD
  121. mono_bind::GodotSharp::get_singleton()->call_deferred(SNAME("_reload_assemblies"), (bool)p_soft_reload);
  122. #endif
  123. }
  124. void godot_icall_Internal_EditorDebuggerNodeReloadScripts() {
  125. EditorDebuggerNode::get_singleton()->reload_scripts();
  126. }
  127. bool godot_icall_Internal_ScriptEditorEdit(Resource *p_resource, int32_t p_line, int32_t p_col, bool p_grab_focus) {
  128. Ref<Resource> resource = p_resource;
  129. return (bool)ScriptEditor::get_singleton()->edit(resource, p_line, p_col, (bool)p_grab_focus);
  130. }
  131. void godot_icall_Internal_EditorNodeShowScriptScreen() {
  132. EditorNode::get_singleton()->call("_editor_select", EditorNode::EDITOR_SCRIPT);
  133. }
  134. void godot_icall_Internal_EditorRunPlay() {
  135. EditorNode::get_singleton()->run_play();
  136. }
  137. void godot_icall_Internal_EditorRunStop() {
  138. EditorNode::get_singleton()->run_stop();
  139. }
  140. void godot_icall_Internal_ScriptEditorDebugger_ReloadScripts() {
  141. EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
  142. if (ed) {
  143. ed->reload_scripts();
  144. }
  145. }
  146. void godot_icall_Internal_CodeCompletionRequest(int32_t p_kind, const godot_string *p_script_file, godot_packed_array *r_ret) {
  147. String script_file = *reinterpret_cast<const String *>(p_script_file);
  148. PackedStringArray suggestions = gdmono::get_code_completion((gdmono::CompletionKind)p_kind, script_file);
  149. memnew_placement(r_ret, PackedStringArray(suggestions));
  150. }
  151. float godot_icall_Globals_EditorScale() {
  152. return EDSCALE;
  153. }
  154. void godot_icall_Globals_GlobalDef(const godot_string *p_setting, const godot_variant *p_default_value, bool p_restart_if_changed, godot_variant *r_result) {
  155. String setting = *reinterpret_cast<const String *>(p_setting);
  156. Variant default_value = *reinterpret_cast<const Variant *>(p_default_value);
  157. Variant result = _GLOBAL_DEF(setting, default_value, (bool)p_restart_if_changed);
  158. memnew_placement(r_result, Variant(result));
  159. }
  160. void godot_icall_Globals_EditorDef(const godot_string *p_setting, const godot_variant *p_default_value, bool p_restart_if_changed, godot_variant *r_result) {
  161. String setting = *reinterpret_cast<const String *>(p_setting);
  162. Variant default_value = *reinterpret_cast<const Variant *>(p_default_value);
  163. Variant result = _EDITOR_DEF(setting, default_value, (bool)p_restart_if_changed);
  164. memnew_placement(r_result, Variant(result));
  165. }
  166. void godot_icall_Globals_EditorShortcut(const godot_string *p_setting, godot_variant *r_result) {
  167. String setting = *reinterpret_cast<const String *>(p_setting);
  168. Ref<Shortcut> result = ED_GET_SHORTCUT(setting);
  169. memnew_placement(r_result, Variant(result));
  170. }
  171. void godot_icall_Globals_TTR(const godot_string *p_text, godot_string *r_dest) {
  172. String text = *reinterpret_cast<const String *>(p_text);
  173. memnew_placement(r_dest, String(TTR(text)));
  174. }
  175. void godot_icall_Utils_OS_GetPlatformName(godot_string *r_dest) {
  176. String os_name = OS::get_singleton()->get_name();
  177. memnew_placement(r_dest, String(os_name));
  178. }
  179. bool godot_icall_Utils_OS_UnixFileHasExecutableAccess(const godot_string *p_file_path) {
  180. #ifdef UNIX_ENABLED
  181. String file_path = *reinterpret_cast<const String *>(p_file_path);
  182. return access(file_path.utf8().get_data(), X_OK) == 0;
  183. #else
  184. ERR_FAIL_V(false);
  185. #endif
  186. }
  187. #ifdef __cplusplus
  188. }
  189. #endif
  190. // The order in this array must match the declaration order of
  191. // the methods in 'GodotTools/Internals/Internal.cs'.
  192. static const void *unmanaged_callbacks[]{
  193. (void *)godot_icall_GodotSharpDirs_ResMetadataDir,
  194. (void *)godot_icall_GodotSharpDirs_MonoUserDir,
  195. (void *)godot_icall_GodotSharpDirs_BuildLogsDirs,
  196. (void *)godot_icall_GodotSharpDirs_DataEditorToolsDir,
  197. (void *)godot_icall_EditorProgress_Create,
  198. (void *)godot_icall_EditorProgress_Dispose,
  199. (void *)godot_icall_EditorProgress_Step,
  200. (void *)godot_icall_Internal_FullExportTemplatesDir,
  201. (void *)godot_icall_Internal_IsMacOSAppBundleInstalled,
  202. (void *)godot_icall_Internal_GodotIs32Bits,
  203. (void *)godot_icall_Internal_GodotIsRealTDouble,
  204. (void *)godot_icall_Internal_GodotMainIteration,
  205. (void *)godot_icall_Internal_IsAssembliesReloadingNeeded,
  206. (void *)godot_icall_Internal_ReloadAssemblies,
  207. (void *)godot_icall_Internal_EditorDebuggerNodeReloadScripts,
  208. (void *)godot_icall_Internal_ScriptEditorEdit,
  209. (void *)godot_icall_Internal_EditorNodeShowScriptScreen,
  210. (void *)godot_icall_Internal_EditorRunPlay,
  211. (void *)godot_icall_Internal_EditorRunStop,
  212. (void *)godot_icall_Internal_ScriptEditorDebugger_ReloadScripts,
  213. (void *)godot_icall_Internal_CodeCompletionRequest,
  214. (void *)godot_icall_Globals_EditorScale,
  215. (void *)godot_icall_Globals_GlobalDef,
  216. (void *)godot_icall_Globals_EditorDef,
  217. (void *)godot_icall_Globals_EditorShortcut,
  218. (void *)godot_icall_Globals_TTR,
  219. (void *)godot_icall_Utils_OS_GetPlatformName,
  220. (void *)godot_icall_Utils_OS_UnixFileHasExecutableAccess,
  221. };
  222. const void **godotsharp::get_editor_interop_funcs(int32_t &r_size) {
  223. r_size = sizeof(unmanaged_callbacks);
  224. return unmanaged_callbacks;
  225. }