editor_internal_calls.cpp 12 KB

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