godotsharp_editor.cpp 18 KB

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