mono_bottom_panel.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*************************************************************************/
  2. /* mono_bottom_panel.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 "mono_bottom_panel.h"
  31. #include "../csharp_script.h"
  32. #include "godotsharp_editor.h"
  33. MonoBottomPanel *MonoBottomPanel::singleton = NULL;
  34. void MonoBottomPanel::_update_build_tabs_list() {
  35. build_tabs_list->clear();
  36. int current_tab = build_tabs->get_current_tab();
  37. bool no_current_tab = current_tab < 0 || current_tab >= build_tabs->get_tab_count();
  38. for (int i = 0; i < build_tabs->get_child_count(); i++) {
  39. MonoBuildTab *tab = Object::cast_to<MonoBuildTab>(build_tabs->get_child(i));
  40. if (tab) {
  41. String item_name = tab->build_info.solution.get_file().get_basename();
  42. item_name += " [" + tab->build_info.configuration + "]";
  43. build_tabs_list->add_item(item_name, tab->get_icon_texture());
  44. String item_tooltip = String("Solution: ") + tab->build_info.solution;
  45. item_tooltip += String("\nConfiguration: ") + tab->build_info.configuration;
  46. item_tooltip += String("\nStatus: ");
  47. if (tab->build_exited) {
  48. item_tooltip += tab->build_result == MonoBuildTab::RESULT_SUCCESS ? "Succeeded" : "Errored";
  49. } else {
  50. item_tooltip += "Running";
  51. }
  52. if (!tab->build_exited || !tab->build_result == MonoBuildTab::RESULT_SUCCESS) {
  53. item_tooltip += "\nErrors: " + itos(tab->error_count);
  54. }
  55. item_tooltip += "\nWarnings: " + itos(tab->warning_count);
  56. build_tabs_list->set_item_tooltip(i, item_tooltip);
  57. if (no_current_tab || current_tab == i) {
  58. build_tabs_list->select(i);
  59. _build_tabs_item_selected(i);
  60. }
  61. }
  62. }
  63. }
  64. void MonoBottomPanel::add_build_tab(MonoBuildTab *p_build_tab) {
  65. build_tabs->add_child(p_build_tab);
  66. raise_build_tab(p_build_tab);
  67. }
  68. void MonoBottomPanel::raise_build_tab(MonoBuildTab *p_build_tab) {
  69. ERR_FAIL_COND(p_build_tab->get_parent() != build_tabs);
  70. build_tabs->move_child(p_build_tab, 0);
  71. _update_build_tabs_list();
  72. }
  73. void MonoBottomPanel::show_build_tab() {
  74. for (int i = 0; i < panel_tabs->get_tab_count(); i++) {
  75. if (panel_tabs->get_tab_control(i) == panel_builds_tab) {
  76. panel_tabs->set_current_tab(i);
  77. editor->make_bottom_panel_item_visible(this);
  78. return;
  79. }
  80. }
  81. ERR_PRINT("Builds tab not found");
  82. }
  83. void MonoBottomPanel::_build_tabs_item_selected(int p_idx) {
  84. ERR_FAIL_INDEX(p_idx, build_tabs->get_tab_count());
  85. build_tabs->set_current_tab(p_idx);
  86. if (!build_tabs->is_visible())
  87. build_tabs->set_visible(true);
  88. warnings_btn->set_visible(true);
  89. errors_btn->set_visible(true);
  90. view_log_btn->set_visible(true);
  91. }
  92. void MonoBottomPanel::_build_tabs_nothing_selected() {
  93. if (build_tabs->get_tab_count() != 0) // just in case
  94. build_tabs->set_visible(false);
  95. warnings_btn->set_visible(false);
  96. errors_btn->set_visible(false);
  97. view_log_btn->set_visible(false);
  98. }
  99. void MonoBottomPanel::_warnings_toggled(bool p_pressed) {
  100. int current_tab = build_tabs->get_current_tab();
  101. ERR_FAIL_INDEX(current_tab, build_tabs->get_tab_count());
  102. MonoBuildTab *build_tab = Object::cast_to<MonoBuildTab>(build_tabs->get_child(current_tab));
  103. build_tab->warnings_visible = p_pressed;
  104. build_tab->_update_issues_list();
  105. }
  106. void MonoBottomPanel::_errors_toggled(bool p_pressed) {
  107. int current_tab = build_tabs->get_current_tab();
  108. ERR_FAIL_INDEX(current_tab, build_tabs->get_tab_count());
  109. MonoBuildTab *build_tab = Object::cast_to<MonoBuildTab>(build_tabs->get_child(current_tab));
  110. build_tab->errors_visible = p_pressed;
  111. build_tab->_update_issues_list();
  112. }
  113. void MonoBottomPanel::_build_project_pressed() {
  114. GodotSharpBuilds::get_singleton()->build_project_blocking("Tools");
  115. MonoReloadNode::get_singleton()->restart_reload_timer();
  116. CSharpLanguage::get_singleton()->reload_assemblies_if_needed(true);
  117. }
  118. void MonoBottomPanel::_view_log_pressed() {
  119. if (build_tabs_list->is_anything_selected()) {
  120. Vector<int> selected_items = build_tabs_list->get_selected_items();
  121. CRASH_COND(selected_items.size() != 1);
  122. int selected_item = selected_items[0];
  123. MonoBuildTab *build_tab = Object::cast_to<MonoBuildTab>(build_tabs->get_tab_control(selected_item));
  124. ERR_FAIL_NULL(build_tab);
  125. String log_dirpath = build_tab->get_build_info().get_log_dirpath();
  126. OS::get_singleton()->shell_open(log_dirpath.plus_file("msbuild_log.txt"));
  127. }
  128. }
  129. void MonoBottomPanel::_notification(int p_what) {
  130. switch (p_what) {
  131. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  132. panel_tabs->add_style_override("panel", editor->get_gui_base()->get_stylebox("DebuggerPanel", "EditorStyles"));
  133. panel_tabs->add_style_override("tab_fg", editor->get_gui_base()->get_stylebox("DebuggerTabFG", "EditorStyles"));
  134. panel_tabs->add_style_override("tab_bg", editor->get_gui_base()->get_stylebox("DebuggerTabBG", "EditorStyles"));
  135. } break;
  136. }
  137. }
  138. void MonoBottomPanel::_bind_methods() {
  139. ClassDB::bind_method(D_METHOD("_build_project_pressed"), &MonoBottomPanel::_build_project_pressed);
  140. ClassDB::bind_method(D_METHOD("_view_log_pressed"), &MonoBottomPanel::_view_log_pressed);
  141. ClassDB::bind_method(D_METHOD("_warnings_toggled", "pressed"), &MonoBottomPanel::_warnings_toggled);
  142. ClassDB::bind_method(D_METHOD("_errors_toggled", "pressed"), &MonoBottomPanel::_errors_toggled);
  143. ClassDB::bind_method(D_METHOD("_build_tabs_item_selected", "idx"), &MonoBottomPanel::_build_tabs_item_selected);
  144. ClassDB::bind_method(D_METHOD("_build_tabs_nothing_selected"), &MonoBottomPanel::_build_tabs_nothing_selected);
  145. }
  146. MonoBottomPanel::MonoBottomPanel(EditorNode *p_editor) {
  147. singleton = this;
  148. editor = p_editor;
  149. set_v_size_flags(SIZE_EXPAND_FILL);
  150. set_anchors_and_margins_preset(Control::PRESET_WIDE);
  151. panel_tabs = memnew(TabContainer);
  152. panel_tabs->set_tab_align(TabContainer::ALIGN_LEFT);
  153. panel_tabs->add_style_override("panel", editor->get_gui_base()->get_stylebox("DebuggerPanel", "EditorStyles"));
  154. panel_tabs->add_style_override("tab_fg", editor->get_gui_base()->get_stylebox("DebuggerTabFG", "EditorStyles"));
  155. panel_tabs->add_style_override("tab_bg", editor->get_gui_base()->get_stylebox("DebuggerTabBG", "EditorStyles"));
  156. panel_tabs->set_custom_minimum_size(Size2(0, 228) * EDSCALE);
  157. panel_tabs->set_v_size_flags(SIZE_EXPAND_FILL);
  158. add_child(panel_tabs);
  159. { // Builds
  160. panel_builds_tab = memnew(VBoxContainer);
  161. panel_builds_tab->set_name(TTR("Builds"));
  162. panel_builds_tab->set_h_size_flags(SIZE_EXPAND_FILL);
  163. panel_tabs->add_child(panel_builds_tab);
  164. HBoxContainer *toolbar_hbc = memnew(HBoxContainer);
  165. toolbar_hbc->set_h_size_flags(SIZE_EXPAND_FILL);
  166. panel_builds_tab->add_child(toolbar_hbc);
  167. Button *build_project_btn = memnew(Button);
  168. build_project_btn->set_text(TTR("Build Project"));
  169. build_project_btn->set_focus_mode(FOCUS_NONE);
  170. build_project_btn->connect("pressed", this, "_build_project_pressed");
  171. toolbar_hbc->add_child(build_project_btn);
  172. toolbar_hbc->add_spacer();
  173. warnings_btn = memnew(ToolButton);
  174. warnings_btn->set_text(TTR("Warnings"));
  175. warnings_btn->set_toggle_mode(true);
  176. warnings_btn->set_pressed(true);
  177. warnings_btn->set_visible(false);
  178. warnings_btn->set_focus_mode(FOCUS_NONE);
  179. warnings_btn->connect("toggled", this, "_warnings_toggled");
  180. toolbar_hbc->add_child(warnings_btn);
  181. errors_btn = memnew(ToolButton);
  182. errors_btn->set_text(TTR("Errors"));
  183. errors_btn->set_toggle_mode(true);
  184. errors_btn->set_pressed(true);
  185. errors_btn->set_visible(false);
  186. errors_btn->set_focus_mode(FOCUS_NONE);
  187. errors_btn->connect("toggled", this, "_errors_toggled");
  188. toolbar_hbc->add_child(errors_btn);
  189. toolbar_hbc->add_spacer();
  190. view_log_btn = memnew(Button);
  191. view_log_btn->set_text(TTR("View log"));
  192. view_log_btn->set_focus_mode(FOCUS_NONE);
  193. view_log_btn->set_visible(false);
  194. view_log_btn->connect("pressed", this, "_view_log_pressed");
  195. toolbar_hbc->add_child(view_log_btn);
  196. HSplitContainer *hsc = memnew(HSplitContainer);
  197. hsc->set_h_size_flags(SIZE_EXPAND_FILL);
  198. hsc->set_v_size_flags(SIZE_EXPAND_FILL);
  199. panel_builds_tab->add_child(hsc);
  200. build_tabs_list = memnew(ItemList);
  201. build_tabs_list->set_h_size_flags(SIZE_EXPAND_FILL);
  202. build_tabs_list->connect("item_selected", this, "_build_tabs_item_selected");
  203. build_tabs_list->connect("nothing_selected", this, "_build_tabs_nothing_selected");
  204. hsc->add_child(build_tabs_list);
  205. build_tabs = memnew(TabContainer);
  206. build_tabs->set_tab_align(TabContainer::ALIGN_LEFT);
  207. build_tabs->set_h_size_flags(SIZE_EXPAND_FILL);
  208. build_tabs->set_tabs_visible(false);
  209. hsc->add_child(build_tabs);
  210. }
  211. }
  212. MonoBottomPanel::~MonoBottomPanel() {
  213. singleton = NULL;
  214. }
  215. void MonoBuildTab::_load_issues_from_file(const String &p_csv_file) {
  216. FileAccessRef f = FileAccess::open(p_csv_file, FileAccess::READ);
  217. if (!f)
  218. return;
  219. while (!f->eof_reached()) {
  220. Vector<String> csv_line = f->get_csv_line();
  221. if (csv_line.size() == 1 && csv_line[0].empty())
  222. return;
  223. ERR_CONTINUE(csv_line.size() != 7);
  224. BuildIssue issue;
  225. issue.warning = csv_line[0] == "warning";
  226. issue.file = csv_line[1];
  227. issue.line = csv_line[2].to_int();
  228. issue.column = csv_line[3].to_int();
  229. issue.code = csv_line[4];
  230. issue.message = csv_line[5];
  231. issue.project_file = csv_line[6];
  232. if (issue.warning)
  233. warning_count += 1;
  234. else
  235. error_count += 1;
  236. issues.push_back(issue);
  237. }
  238. }
  239. void MonoBuildTab::_update_issues_list() {
  240. issues_list->clear();
  241. Ref<Texture> warning_icon = get_icon("Warning", "EditorIcons");
  242. Ref<Texture> error_icon = get_icon("Error", "EditorIcons");
  243. for (int i = 0; i < issues.size(); i++) {
  244. const BuildIssue &issue = issues[i];
  245. if (!(issue.warning ? warnings_visible : errors_visible))
  246. continue;
  247. String tooltip;
  248. tooltip += String("Message: ") + issue.message;
  249. if (issue.code.length()) {
  250. tooltip += String("\nCode: ") + issue.code;
  251. }
  252. tooltip += String("\nType: ") + (issue.warning ? "warning" : "error");
  253. String text;
  254. if (issue.file.length()) {
  255. String sline = String::num_int64(issue.line);
  256. String scolumn = String::num_int64(issue.column);
  257. text += issue.file + "(";
  258. text += sline + ",";
  259. text += scolumn + "): ";
  260. tooltip += "\nFile: " + issue.file;
  261. tooltip += "\nLine: " + sline;
  262. tooltip += "\nColumn: " + scolumn;
  263. }
  264. if (issue.project_file.length()) {
  265. tooltip += "\nProject: " + issue.project_file;
  266. }
  267. text += issue.message;
  268. int line_break_idx = text.find("\n");
  269. issues_list->add_item(line_break_idx == -1 ? text : text.substr(0, line_break_idx),
  270. issue.warning ? warning_icon : error_icon);
  271. int index = issues_list->get_item_count() - 1;
  272. issues_list->set_item_tooltip(index, tooltip);
  273. issues_list->set_item_metadata(index, i);
  274. }
  275. }
  276. Ref<Texture> MonoBuildTab::get_icon_texture() const {
  277. if (build_exited) {
  278. if (build_result == RESULT_ERROR) {
  279. return get_icon("StatusError", "EditorIcons");
  280. } else {
  281. return get_icon("StatusSuccess", "EditorIcons");
  282. }
  283. } else {
  284. return get_icon("Stop", "EditorIcons");
  285. }
  286. }
  287. MonoBuildInfo MonoBuildTab::get_build_info() {
  288. return build_info;
  289. }
  290. void MonoBuildTab::on_build_start() {
  291. build_exited = false;
  292. issues.clear();
  293. warning_count = 0;
  294. error_count = 0;
  295. _update_issues_list();
  296. MonoBottomPanel::get_singleton()->raise_build_tab(this);
  297. }
  298. void MonoBuildTab::on_build_exit(BuildResult result) {
  299. build_exited = true;
  300. build_result = result;
  301. _load_issues_from_file(logs_dir.plus_file("msbuild_issues.csv"));
  302. _update_issues_list();
  303. MonoBottomPanel::get_singleton()->raise_build_tab(this);
  304. }
  305. void MonoBuildTab::on_build_exec_failed(const String &p_cause) {
  306. build_exited = true;
  307. build_result = RESULT_ERROR;
  308. issues_list->clear();
  309. BuildIssue issue;
  310. issue.message = p_cause;
  311. issue.warning = false;
  312. error_count += 1;
  313. issues.push_back(issue);
  314. _update_issues_list();
  315. MonoBottomPanel::get_singleton()->raise_build_tab(this);
  316. }
  317. void MonoBuildTab::restart_build() {
  318. ERR_FAIL_COND(!build_exited);
  319. GodotSharpBuilds::get_singleton()->restart_build(this);
  320. }
  321. void MonoBuildTab::stop_build() {
  322. ERR_FAIL_COND(build_exited);
  323. GodotSharpBuilds::get_singleton()->stop_build(this);
  324. }
  325. void MonoBuildTab::_issue_activated(int p_idx) {
  326. ERR_FAIL_INDEX(p_idx, issues_list->get_item_count());
  327. // Get correct issue idx from issue list
  328. int issue_idx = this->issues_list->get_item_metadata(p_idx);
  329. ERR_FAIL_INDEX(issue_idx, issues.size());
  330. const BuildIssue &issue = issues[issue_idx];
  331. if (issue.project_file.empty() && issue.file.empty())
  332. return;
  333. String project_dir = issue.project_file.length() ? issue.project_file.get_base_dir() : build_info.solution.get_base_dir();
  334. String file = project_dir.simplify_path().plus_file(issue.file.simplify_path());
  335. if (!FileAccess::exists(file))
  336. return;
  337. file = ProjectSettings::get_singleton()->localize_path(file);
  338. if (file.begins_with("res://")) {
  339. Ref<Script> script = ResourceLoader::load(file, CSharpLanguage::get_singleton()->get_type());
  340. if (script.is_valid() && ScriptEditor::get_singleton()->edit(script, issue.line, issue.column)) {
  341. EditorNode::get_singleton()->call("_editor_select", EditorNode::EDITOR_SCRIPT);
  342. }
  343. }
  344. }
  345. void MonoBuildTab::_bind_methods() {
  346. ClassDB::bind_method("_issue_activated", &MonoBuildTab::_issue_activated);
  347. }
  348. MonoBuildTab::MonoBuildTab(const MonoBuildInfo &p_build_info, const String &p_logs_dir) :
  349. build_info(p_build_info),
  350. logs_dir(p_logs_dir),
  351. build_exited(false),
  352. issues_list(memnew(ItemList)),
  353. error_count(0),
  354. warning_count(0),
  355. errors_visible(true),
  356. warnings_visible(true) {
  357. issues_list->set_v_size_flags(SIZE_EXPAND_FILL);
  358. issues_list->connect("item_activated", this, "_issue_activated");
  359. add_child(issues_list);
  360. }