editor_internal_calls.cpp 12 KB

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