godotsharp_editor.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*************************************************************************/
  2. /* godotsharp_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "godotsharp_editor.h"
  31. #include "core/os/os.h"
  32. #include "core/project_settings.h"
  33. #include "scene/gui/control.h"
  34. #include "scene/main/node.h"
  35. #include "../csharp_script.h"
  36. #include "../godotsharp_dirs.h"
  37. #include "../mono_gd/gd_mono.h"
  38. #include "../mono_gd/gd_mono_marshal.h"
  39. #include "../utils/path_utils.h"
  40. #include "bindings_generator.h"
  41. #include "csharp_project.h"
  42. #include "godotsharp_export.h"
  43. #include "net_solution.h"
  44. #ifdef OSX_ENABLED
  45. #include "../utils/osx_utils.h"
  46. #endif
  47. #ifdef WINDOWS_ENABLED
  48. #include "../utils/mono_reg_utils.h"
  49. #endif
  50. GodotSharpEditor *GodotSharpEditor::singleton = NULL;
  51. bool GodotSharpEditor::_create_project_solution() {
  52. EditorProgress pr("create_csharp_solution", TTR("Generating solution..."), 2);
  53. pr.step(TTR("Generating C# project..."));
  54. String path = OS::get_singleton()->get_resource_dir();
  55. String name = ProjectSettings::get_singleton()->get("application/config/name");
  56. if (name.empty()) {
  57. name = "UnnamedProject";
  58. }
  59. String guid = CSharpProject::generate_game_project(path, name);
  60. if (guid.length()) {
  61. NETSolution solution(name);
  62. if (!solution.set_path(path)) {
  63. show_error_dialog(TTR("Failed to create solution."));
  64. return false;
  65. }
  66. Vector<String> extra_configs;
  67. extra_configs.push_back("Tools");
  68. solution.add_new_project(name, guid, extra_configs);
  69. Error sln_error = solution.save();
  70. if (sln_error != OK) {
  71. show_error_dialog(TTR("Failed to save solution."));
  72. return false;
  73. }
  74. if (!GodotSharpBuilds::make_api_sln(APIAssembly::API_CORE))
  75. return false;
  76. if (!GodotSharpBuilds::make_api_sln(APIAssembly::API_EDITOR))
  77. return false;
  78. pr.step(TTR("Done"));
  79. // Here, after all calls to progress_task_step
  80. call_deferred("_remove_create_sln_menu_option");
  81. } else {
  82. show_error_dialog(TTR("Failed to create C# project."));
  83. }
  84. return true;
  85. }
  86. void GodotSharpEditor::_remove_create_sln_menu_option() {
  87. menu_popup->remove_item(menu_popup->get_item_index(MENU_CREATE_SLN));
  88. if (menu_popup->get_item_count() == 0)
  89. menu_button->hide();
  90. bottom_panel_btn->show();
  91. }
  92. void GodotSharpEditor::_show_about_dialog() {
  93. bool show_on_start = EDITOR_GET("mono/editor/show_info_on_start");
  94. about_dialog_checkbox->set_pressed(show_on_start);
  95. about_dialog->popup_centered_minsize();
  96. }
  97. void GodotSharpEditor::_toggle_about_dialog_on_start(bool p_enabled) {
  98. bool show_on_start = EDITOR_GET("mono/editor/show_info_on_start");
  99. if (show_on_start != p_enabled) {
  100. EditorSettings::get_singleton()->set_setting("mono/editor/show_info_on_start", p_enabled);
  101. }
  102. }
  103. void GodotSharpEditor::_menu_option_pressed(int p_id) {
  104. switch (p_id) {
  105. case MENU_CREATE_SLN: {
  106. _create_project_solution();
  107. } break;
  108. case MENU_ABOUT_CSHARP: {
  109. _show_about_dialog();
  110. } break;
  111. default:
  112. ERR_FAIL();
  113. }
  114. }
  115. void GodotSharpEditor::_notification(int p_notification) {
  116. switch (p_notification) {
  117. case NOTIFICATION_READY: {
  118. bool show_info_dialog = EDITOR_GET("mono/editor/show_info_on_start");
  119. if (show_info_dialog) {
  120. about_dialog->set_exclusive(true);
  121. _show_about_dialog();
  122. // Once shown a first time, it can be seen again via the Mono menu - it doesn't have to be exclusive then.
  123. about_dialog->set_exclusive(false);
  124. }
  125. }
  126. }
  127. }
  128. void GodotSharpEditor::_bind_methods() {
  129. ClassDB::bind_method(D_METHOD("_create_project_solution"), &GodotSharpEditor::_create_project_solution);
  130. ClassDB::bind_method(D_METHOD("_remove_create_sln_menu_option"), &GodotSharpEditor::_remove_create_sln_menu_option);
  131. ClassDB::bind_method(D_METHOD("_toggle_about_dialog_on_start"), &GodotSharpEditor::_toggle_about_dialog_on_start);
  132. ClassDB::bind_method(D_METHOD("_menu_option_pressed", "id"), &GodotSharpEditor::_menu_option_pressed);
  133. }
  134. MonoBoolean godot_icall_MonoDevelopInstance_IsApplicationBundleInstalled(MonoString *p_bundle_id) {
  135. #ifdef OSX_ENABLED
  136. return (MonoBoolean)osx_is_app_bundle_installed(GDMonoMarshal::mono_string_to_godot(p_bundle_id));
  137. #else
  138. (void)p_bundle_id; // UNUSED
  139. ERR_FAIL_V(false);
  140. #endif
  141. }
  142. MonoString *godot_icall_Utils_OS_GetPlatformName() {
  143. return GDMonoMarshal::mono_string_from_godot(OS::get_singleton()->get_name());
  144. }
  145. void GodotSharpEditor::register_internal_calls() {
  146. static bool registered = false;
  147. ERR_FAIL_COND(registered);
  148. registered = true;
  149. mono_add_internal_call("GodotSharpTools.Editor.MonoDevelopInstance::IsApplicationBundleInstalled", (void *)godot_icall_MonoDevelopInstance_IsApplicationBundleInstalled);
  150. mono_add_internal_call("GodotSharpTools.Utils.OS::GetPlatformName", (void *)godot_icall_Utils_OS_GetPlatformName);
  151. GodotSharpBuilds::register_internal_calls();
  152. GodotSharpExport::register_internal_calls();
  153. }
  154. void GodotSharpEditor::show_error_dialog(const String &p_message, const String &p_title) {
  155. error_dialog->set_title(p_title);
  156. error_dialog->set_text(p_message);
  157. error_dialog->popup_centered_minsize();
  158. }
  159. Error GodotSharpEditor::open_in_external_editor(const Ref<Script> &p_script, int p_line, int p_col) {
  160. ExternalEditor editor = ExternalEditor(int(EditorSettings::get_singleton()->get("mono/editor/external_editor")));
  161. switch (editor) {
  162. case EDITOR_VSCODE: {
  163. static String vscode_path;
  164. if (vscode_path.empty() || !FileAccess::exists(vscode_path)) {
  165. // Try to search it again if it wasn't found last time or if it was removed from its location
  166. vscode_path = path_which("code");
  167. }
  168. List<String> args;
  169. #ifdef OSX_ENABLED
  170. // The package path is '/Applications/Visual Studio Code.app'
  171. static const String vscode_bundle_id = "com.microsoft.VSCode";
  172. static bool osx_app_bundle_installed = osx_is_app_bundle_installed(vscode_bundle_id);
  173. if (osx_app_bundle_installed) {
  174. args.push_back("-b");
  175. args.push_back(vscode_bundle_id);
  176. // The reusing of existing windows made by the 'open' command might not choose a wubdiw that is
  177. // editing our folder. It's better to ask for a new window and let VSCode do the window management.
  178. args.push_back("-n");
  179. // The open process must wait until the application finishes (which is instant in VSCode's case)
  180. args.push_back("--wait-apps");
  181. args.push_back("--args");
  182. }
  183. #endif
  184. args.push_back(ProjectSettings::get_singleton()->get_resource_path());
  185. String script_path = ProjectSettings::get_singleton()->globalize_path(p_script->get_path());
  186. if (p_line >= 0) {
  187. args.push_back("-g");
  188. args.push_back(script_path + ":" + itos(p_line + 1) + ":" + itos(p_col));
  189. } else {
  190. args.push_back(script_path);
  191. }
  192. #ifdef OSX_ENABLED
  193. ERR_EXPLAIN("Cannot find code editor: VSCode");
  194. ERR_FAIL_COND_V(!osx_app_bundle_installed && vscode_path.empty(), ERR_FILE_NOT_FOUND);
  195. String command = osx_app_bundle_installed ? "/usr/bin/open" : vscode_path;
  196. #else
  197. ERR_EXPLAIN("Cannot find code editor: VSCode");
  198. ERR_FAIL_COND_V(vscode_path.empty(), ERR_FILE_NOT_FOUND);
  199. String command = vscode_path;
  200. #endif
  201. Error err = OS::get_singleton()->execute(command, args, false);
  202. if (err != OK) {
  203. ERR_PRINT("Error when trying to execute code editor: VSCode");
  204. return err;
  205. }
  206. } break;
  207. #ifdef OSX_ENABLED
  208. case EDITOR_VISUALSTUDIO_MAC:
  209. // [[fallthrough]];
  210. #endif
  211. case EDITOR_MONODEVELOP: {
  212. #ifdef OSX_ENABLED
  213. bool is_visualstudio = editor == EDITOR_VISUALSTUDIO_MAC;
  214. MonoDevelopInstance **instance = is_visualstudio ?
  215. &visualstudio_mac_instance :
  216. &monodevelop_instance;
  217. MonoDevelopInstance::EditorId editor_id = is_visualstudio ?
  218. MonoDevelopInstance::VISUALSTUDIO_FOR_MAC :
  219. MonoDevelopInstance::MONODEVELOP;
  220. #else
  221. MonoDevelopInstance **instance = &monodevelop_instance;
  222. MonoDevelopInstance::EditorId editor_id = MonoDevelopInstance::MONODEVELOP;
  223. #endif
  224. if (!*instance)
  225. *instance = memnew(MonoDevelopInstance(GodotSharpDirs::get_project_sln_path(), editor_id));
  226. String script_path = ProjectSettings::get_singleton()->globalize_path(p_script->get_path());
  227. if (p_line >= 0) {
  228. script_path += ";" + itos(p_line + 1) + ";" + itos(p_col);
  229. }
  230. (*instance)->execute(script_path);
  231. } break;
  232. default:
  233. return ERR_UNAVAILABLE;
  234. }
  235. return OK;
  236. }
  237. bool GodotSharpEditor::overrides_external_editor() {
  238. return ExternalEditor(int(EditorSettings::get_singleton()->get("mono/editor/external_editor"))) != EDITOR_NONE;
  239. }
  240. GodotSharpEditor::GodotSharpEditor(EditorNode *p_editor) {
  241. singleton = this;
  242. monodevelop_instance = NULL;
  243. #ifdef OSX_ENABLED
  244. visualstudio_mac_instance = NULL;
  245. #endif
  246. editor = p_editor;
  247. error_dialog = memnew(AcceptDialog);
  248. editor->get_gui_base()->add_child(error_dialog);
  249. bottom_panel_btn = editor->add_bottom_panel_item(TTR("Mono"), memnew(MonoBottomPanel(editor)));
  250. godotsharp_builds = memnew(GodotSharpBuilds);
  251. editor->add_child(memnew(MonoReloadNode));
  252. menu_button = memnew(MenuButton);
  253. menu_button->set_text(TTR("Mono"));
  254. menu_popup = menu_button->get_popup();
  255. // TODO: Remove or edit this info dialog once Mono support is no longer in alpha
  256. {
  257. menu_popup->add_item(TTR("About C# support"), MENU_ABOUT_CSHARP);
  258. about_dialog = memnew(AcceptDialog);
  259. editor->get_gui_base()->add_child(about_dialog);
  260. about_dialog->set_title("Important: C# support is not feature-complete");
  261. // We don't use set_text() as the default AcceptDialog Label doesn't play well with the TextureRect and CheckBox
  262. // we'll add. Instead we add containers and a new autowrapped Label inside.
  263. // Main VBoxContainer (icon + label on top, checkbox at bottom)
  264. VBoxContainer *about_vbc = memnew(VBoxContainer);
  265. about_dialog->add_child(about_vbc);
  266. // HBoxContainer for icon + label
  267. HBoxContainer *about_hbc = memnew(HBoxContainer);
  268. about_vbc->add_child(about_hbc);
  269. TextureRect *about_icon = memnew(TextureRect);
  270. about_hbc->add_child(about_icon);
  271. Ref<Texture> about_icon_tex = about_icon->get_icon("NodeWarning", "EditorIcons");
  272. about_icon->set_texture(about_icon_tex);
  273. Label *about_label = memnew(Label);
  274. about_hbc->add_child(about_label);
  275. about_label->set_custom_minimum_size(Size2(600, 150) * EDSCALE);
  276. about_label->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  277. about_label->set_autowrap(true);
  278. String about_text =
  279. String("C# support in Godot Engine is a brand new feature and a work in progress.\n") +
  280. "It is currently in an alpha stage and is not suitable for use in production.\n\n" +
  281. "As of Godot 3.1, C# support is not feature-complete and may crash in some situations. " +
  282. "Bugs and usability issues will be addressed gradually over future 3.x releases, " +
  283. "including compatibility breaking changes as new features are implemented for a better overall C# experience.\n\n" +
  284. "If you experience issues with this Mono build, please report them on Godot's issue tracker with details about your system, Mono version, IDE, etc:\n\n" +
  285. " https://github.com/godotengine/godot/issues\n\n" +
  286. "Your critical feedback at this stage will play a great role in shaping the C# support in future releases, so thank you!";
  287. about_label->set_text(about_text);
  288. EDITOR_DEF("mono/editor/show_info_on_start", true);
  289. // CheckBox in main container
  290. about_dialog_checkbox = memnew(CheckBox);
  291. about_vbc->add_child(about_dialog_checkbox);
  292. about_dialog_checkbox->set_text("Show this warning when starting the editor");
  293. about_dialog_checkbox->connect("toggled", this, "_toggle_about_dialog_on_start");
  294. }
  295. String sln_path = GodotSharpDirs::get_project_sln_path();
  296. String csproj_path = GodotSharpDirs::get_project_csproj_path();
  297. if (!FileAccess::exists(sln_path) || !FileAccess::exists(csproj_path)) {
  298. bottom_panel_btn->hide();
  299. menu_popup->add_item(TTR("Create C# solution"), MENU_CREATE_SLN);
  300. }
  301. menu_popup->connect("id_pressed", this, "_menu_option_pressed");
  302. if (menu_popup->get_item_count() == 0)
  303. menu_button->hide();
  304. editor->get_menu_hb()->add_child(menu_button);
  305. // External editor settings
  306. EditorSettings *ed_settings = EditorSettings::get_singleton();
  307. EDITOR_DEF("mono/editor/external_editor", EDITOR_NONE);
  308. String settings_hint_str = "None";
  309. #ifdef WINDOWS_ENABLED
  310. settings_hint_str += ",MonoDevelop,Visual Studio Code";
  311. #elif OSX_ENABLED
  312. settings_hint_str += ",Visual Studio,MonoDevelop,Visual Studio Code";
  313. #elif UNIX_ENABLED
  314. settings_hint_str += ",MonoDevelop,Visual Studio Code";
  315. #endif
  316. ed_settings->add_property_hint(PropertyInfo(Variant::INT, "mono/editor/external_editor", PROPERTY_HINT_ENUM, settings_hint_str));
  317. // Export plugin
  318. Ref<GodotSharpExport> godotsharp_export;
  319. godotsharp_export.instance();
  320. EditorExport::get_singleton()->add_export_plugin(godotsharp_export);
  321. }
  322. GodotSharpEditor::~GodotSharpEditor() {
  323. singleton = NULL;
  324. memdelete(godotsharp_builds);
  325. if (monodevelop_instance) {
  326. memdelete(monodevelop_instance);
  327. monodevelop_instance = NULL;
  328. }
  329. }
  330. MonoReloadNode *MonoReloadNode::singleton = NULL;
  331. void MonoReloadNode::_reload_timer_timeout() {
  332. CSharpLanguage::get_singleton()->reload_assemblies_if_needed(false);
  333. }
  334. void MonoReloadNode::restart_reload_timer() {
  335. reload_timer->stop();
  336. reload_timer->start();
  337. }
  338. void MonoReloadNode::_bind_methods() {
  339. ClassDB::bind_method(D_METHOD("_reload_timer_timeout"), &MonoReloadNode::_reload_timer_timeout);
  340. }
  341. void MonoReloadNode::_notification(int p_what) {
  342. switch (p_what) {
  343. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  344. restart_reload_timer();
  345. CSharpLanguage::get_singleton()->reload_assemblies_if_needed(true);
  346. } break;
  347. default: {
  348. } break;
  349. };
  350. }
  351. MonoReloadNode::MonoReloadNode() {
  352. singleton = this;
  353. reload_timer = memnew(Timer);
  354. add_child(reload_timer);
  355. reload_timer->set_one_shot(false);
  356. reload_timer->set_wait_time(EDITOR_DEF("mono/assembly_watch_interval_sec", 0.5));
  357. reload_timer->connect("timeout", this, "_reload_timer_timeout");
  358. reload_timer->start();
  359. }
  360. MonoReloadNode::~MonoReloadNode() {
  361. singleton = NULL;
  362. }