editor_internal_calls.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/export/lipo.h"
  44. #include "editor/file_system/editor_paths.h"
  45. #include "editor/run/editor_run_bar.h"
  46. #include "editor/script/script_editor_plugin.h"
  47. #include "editor/settings/editor_settings.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. memnew_placement(r_dest, String(GodotSharpDirs::get_build_logs_dir()));
  64. }
  65. void godot_icall_GodotSharpDirs_DataEditorToolsDir(godot_string *r_dest) {
  66. memnew_placement(r_dest, String(GodotSharpDirs::get_data_editor_tools_dir()));
  67. }
  68. void godot_icall_GodotSharpDirs_CSharpProjectName(godot_string *r_dest) {
  69. memnew_placement(r_dest, String(Path::get_csharp_project_name()));
  70. }
  71. void godot_icall_EditorProgress_Create(const godot_string *p_task, const godot_string *p_label, int32_t p_amount, bool p_can_cancel) {
  72. String task = *reinterpret_cast<const String *>(p_task);
  73. String label = *reinterpret_cast<const String *>(p_label);
  74. EditorNode::progress_add_task(task, label, p_amount, (bool)p_can_cancel);
  75. }
  76. void godot_icall_EditorProgress_Dispose(const godot_string *p_task) {
  77. String task = *reinterpret_cast<const String *>(p_task);
  78. EditorNode::progress_end_task(task);
  79. }
  80. bool godot_icall_EditorProgress_Step(const godot_string *p_task, const godot_string *p_state, int32_t p_step, bool p_force_refresh) {
  81. String task = *reinterpret_cast<const String *>(p_task);
  82. String state = *reinterpret_cast<const String *>(p_state);
  83. return EditorNode::progress_task_step(task, state, p_step, (bool)p_force_refresh);
  84. }
  85. void godot_icall_Internal_FullExportTemplatesDir(godot_string *r_dest) {
  86. String full_templates_dir = EditorPaths::get_singleton()->get_export_templates_dir().path_join(GODOT_VERSION_FULL_CONFIG);
  87. memnew_placement(r_dest, String(full_templates_dir));
  88. }
  89. bool godot_icall_Internal_IsMacOSAppBundleInstalled(const godot_string *p_bundle_id) {
  90. #ifdef MACOS_ENABLED
  91. String bundle_id = *reinterpret_cast<const String *>(p_bundle_id);
  92. return (bool)macos_is_app_bundle_installed(bundle_id);
  93. #else
  94. (void)p_bundle_id; // UNUSED
  95. return (bool)false;
  96. #endif
  97. }
  98. bool godot_icall_Internal_LipOCreateFile(const godot_string *p_output_path, const godot_packed_array *p_files) {
  99. String output_path = *reinterpret_cast<const String *>(p_output_path);
  100. PackedStringArray files = *reinterpret_cast<const PackedStringArray *>(p_files);
  101. LipO lip;
  102. return lip.create_file(output_path, files);
  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. callable_mp(MonoBind::GodotSharp::get_singleton(), &MonoBind::GodotSharp::reload_assemblies).call_deferred(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_editor_main_screen()->select(EditorMainScreen::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_LipOCreateFile,
  223. (void *)godot_icall_Internal_GodotIs32Bits,
  224. (void *)godot_icall_Internal_GodotIsRealTDouble,
  225. (void *)godot_icall_Internal_GodotMainIteration,
  226. (void *)godot_icall_Internal_IsAssembliesReloadingNeeded,
  227. (void *)godot_icall_Internal_ReloadAssemblies,
  228. (void *)godot_icall_Internal_EditorDebuggerNodeReloadScripts,
  229. (void *)godot_icall_Internal_ScriptEditorEdit,
  230. (void *)godot_icall_Internal_EditorNodeShowScriptScreen,
  231. (void *)godot_icall_Internal_EditorRunPlay,
  232. (void *)godot_icall_Internal_EditorRunStop,
  233. (void *)godot_icall_Internal_EditorPlugin_AddControlToEditorRunBar,
  234. (void *)godot_icall_Internal_ScriptEditorDebugger_ReloadScripts,
  235. (void *)godot_icall_Internal_CodeCompletionRequest,
  236. (void *)godot_icall_Globals_EditorScale,
  237. (void *)godot_icall_Globals_GlobalDef,
  238. (void *)godot_icall_Globals_EditorDef,
  239. (void *)godot_icall_Globals_EditorDefShortcut,
  240. (void *)godot_icall_Globals_EditorGetShortcut,
  241. (void *)godot_icall_Globals_EditorShortcutOverride,
  242. (void *)godot_icall_Globals_TTR,
  243. (void *)godot_icall_Utils_OS_GetPlatformName,
  244. (void *)godot_icall_Utils_OS_UnixFileHasExecutableAccess,
  245. };
  246. const void **godotsharp::get_editor_interop_funcs(int32_t &r_size) {
  247. r_size = sizeof(unmanaged_callbacks);
  248. return unmanaged_callbacks;
  249. }