run_instances_dialog.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /**************************************************************************/
  2. /* run_instances_dialog.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 "run_instances_dialog.h"
  31. #include "core/config/project_settings.h"
  32. #include "editor/settings/editor_settings.h"
  33. #include "editor/themes/editor_scale.h"
  34. #include "scene/gui/check_box.h"
  35. #include "scene/gui/grid_container.h"
  36. #include "scene/gui/label.h"
  37. #include "scene/gui/line_edit.h"
  38. #include "scene/gui/margin_container.h"
  39. #include "scene/gui/popup_menu.h"
  40. #include "scene/gui/separator.h"
  41. #include "scene/gui/spin_box.h"
  42. #include "scene/gui/tree.h"
  43. #include "scene/main/timer.h"
  44. void RunInstancesDialog::_fetch_main_args() {
  45. if (!main_args_edit->has_focus()) { // Only set the text if the user is not currently editing it.
  46. main_args_edit->set_text(GLOBAL_GET("editor/run/main_run_args"));
  47. }
  48. }
  49. void RunInstancesDialog::_start_main_timer() {
  50. main_apply_timer->start();
  51. }
  52. void RunInstancesDialog::_start_instance_timer() {
  53. instance_apply_timer->start();
  54. }
  55. void RunInstancesDialog::_refresh_argument_count() {
  56. instance_tree->clear();
  57. instance_tree->create_item(); // Root.
  58. while (instance_count->get_value() > stored_data.size()) {
  59. stored_data.append(Dictionary());
  60. }
  61. instances_data.resize(instance_count->get_value());
  62. InstanceData *instances_write = instances_data.ptrw();
  63. for (int i = 0; i < instances_data.size(); i++) {
  64. InstanceData instance;
  65. const Dictionary &instance_data = stored_data[i];
  66. _create_instance(instance, instance_data, i + 1);
  67. instances_write[i] = instance;
  68. }
  69. }
  70. void RunInstancesDialog::_create_instance(InstanceData &p_instance, const Dictionary &p_data, int p_idx) {
  71. TreeItem *instance_item = instance_tree->create_item();
  72. p_instance.item = instance_item;
  73. instance_item->set_cell_mode(COLUMN_OVERRIDE_ARGS, TreeItem::CELL_MODE_CHECK);
  74. instance_item->set_editable(COLUMN_OVERRIDE_ARGS, true);
  75. instance_item->set_text(COLUMN_OVERRIDE_ARGS, TTR("Enabled"));
  76. instance_item->set_checked(COLUMN_OVERRIDE_ARGS, p_data.get("override_args", false));
  77. instance_item->set_editable(COLUMN_LAUNCH_ARGUMENTS, true);
  78. instance_item->set_text(COLUMN_LAUNCH_ARGUMENTS, p_data.get("arguments", String()));
  79. instance_item->set_cell_mode(COLUMN_OVERRIDE_FEATURES, TreeItem::CELL_MODE_CHECK);
  80. instance_item->set_editable(COLUMN_OVERRIDE_FEATURES, true);
  81. instance_item->set_text(COLUMN_OVERRIDE_FEATURES, TTR("Enabled"));
  82. instance_item->set_checked(COLUMN_OVERRIDE_FEATURES, p_data.get("override_features", false));
  83. instance_item->set_editable(COLUMN_FEATURE_TAGS, true);
  84. instance_item->set_text(COLUMN_FEATURE_TAGS, p_data.get("features", String()));
  85. }
  86. void RunInstancesDialog::_save_main_args() {
  87. ProjectSettings::get_singleton()->set_setting("editor/run/main_run_args", main_args_edit->get_text());
  88. ProjectSettings::get_singleton()->save();
  89. EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_main_feature_tags", main_features_edit->get_text());
  90. EditorSettings::get_singleton()->set_project_metadata("debug_options", "multiple_instances_enabled", enable_multiple_instances_checkbox->is_pressed());
  91. }
  92. void RunInstancesDialog::_save_arguments() {
  93. for (int i = 0; i < instances_data.size(); i++) {
  94. const InstanceData &instance = instances_data[i];
  95. Dictionary dict;
  96. dict["override_args"] = instance.overrides_run_args();
  97. dict["arguments"] = instance.get_launch_arguments();
  98. dict["override_features"] = instance.overrides_features();
  99. dict["features"] = instance.get_feature_tags();
  100. stored_data[i] = dict;
  101. }
  102. EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_instances_config", stored_data);
  103. EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_instance_count", instance_count->get_value());
  104. }
  105. Vector<String> RunInstancesDialog::_split_cmdline_args(const String &p_arg_string) const {
  106. Vector<String> split_args;
  107. int arg_start = 0;
  108. bool is_quoted = false;
  109. char32_t quote_char = '-';
  110. char32_t arg_char;
  111. int arg_length;
  112. for (int i = 0; i < p_arg_string.length(); i++) {
  113. arg_char = p_arg_string[i];
  114. if (arg_char == '\"' || arg_char == '\'') {
  115. if (i == 0 || p_arg_string[i - 1] != '\\') {
  116. if (is_quoted) {
  117. if (arg_char == quote_char) {
  118. is_quoted = false;
  119. quote_char = '-';
  120. }
  121. } else {
  122. is_quoted = true;
  123. quote_char = arg_char;
  124. }
  125. }
  126. } else if (!is_quoted && arg_char == ' ') {
  127. arg_length = i - arg_start;
  128. if (arg_length > 0) {
  129. split_args.push_back(p_arg_string.substr(arg_start, arg_length));
  130. }
  131. arg_start = i + 1;
  132. }
  133. }
  134. arg_length = p_arg_string.length() - arg_start;
  135. if (arg_length > 0) {
  136. split_args.push_back(p_arg_string.substr(arg_start, arg_length));
  137. }
  138. return split_args;
  139. }
  140. void RunInstancesDialog::_instance_menu_id_pressed(int p_option) {
  141. switch (p_option) {
  142. case CLEAR_ITEM: {
  143. int item_to_clear = popup_menu->get_item_metadata(0);
  144. if (item_to_clear >= 0 && item_to_clear < stored_data.size()) {
  145. stored_data[item_to_clear] = Dictionary();
  146. }
  147. } break;
  148. case CLEAR_ALL: {
  149. stored_data.clear();
  150. stored_data.resize(instance_count->get_value());
  151. } break;
  152. }
  153. _start_instance_timer();
  154. _refresh_argument_count();
  155. }
  156. void RunInstancesDialog::_instance_tree_rmb(const Vector2 &p_pos, MouseButton p_button) {
  157. if (p_button != MouseButton::RIGHT) {
  158. return;
  159. }
  160. popup_menu->clear();
  161. popup_menu->add_item(TTR("Clear"), CLEAR_ITEM);
  162. TreeItem *item = instance_tree->get_item_at_position(p_pos);
  163. if (item) {
  164. popup_menu->set_item_metadata(0, item->get_index());
  165. } else {
  166. popup_menu->set_item_disabled(0, true);
  167. }
  168. popup_menu->add_item(TTR("Clear All"), CLEAR_ALL);
  169. popup_menu->set_position(instance_tree->get_screen_position() + p_pos);
  170. popup_menu->reset_size();
  171. popup_menu->popup();
  172. }
  173. void RunInstancesDialog::popup_dialog() {
  174. popup_centered_clamped(Size2(1200, 600) * EDSCALE, 0.8);
  175. }
  176. int RunInstancesDialog::get_instance_count() const {
  177. if (enable_multiple_instances_checkbox->is_pressed()) {
  178. return instance_count->get_value();
  179. } else {
  180. return 1;
  181. }
  182. }
  183. void RunInstancesDialog::get_argument_list_for_instance(int p_idx, List<String> &r_list) const {
  184. bool override_args = instances_data[p_idx].overrides_run_args();
  185. bool use_multiple_instances = enable_multiple_instances_checkbox->is_pressed();
  186. String raw_custom_args;
  187. if (use_multiple_instances) {
  188. if (override_args) {
  189. raw_custom_args = instances_data[p_idx].get_launch_arguments();
  190. } else {
  191. raw_custom_args = main_args_edit->get_text() + " " + instances_data[p_idx].get_launch_arguments();
  192. }
  193. } else {
  194. raw_custom_args = main_args_edit->get_text();
  195. }
  196. String exec = OS::get_singleton()->get_executable_path();
  197. if (!raw_custom_args.is_empty()) {
  198. // Allow the user to specify a command to run, similar to Steam's launch options.
  199. // In this case, Godot will no longer be run directly; it's up to the underlying command
  200. // to run it. For instance, this can be used on Linux to force a running project
  201. // to use Optimus using `prime-run` or similar.
  202. // Example: `prime-run %command% --time-scale 0.5`
  203. const int placeholder_pos = raw_custom_args.find("%command%");
  204. Vector<String> custom_args;
  205. if (placeholder_pos != -1) {
  206. // Prepend executable-specific custom arguments.
  207. // If nothing is placed before `%command%`, behave as if no placeholder was specified.
  208. Vector<String> exec_args = _split_cmdline_args(raw_custom_args.substr(0, placeholder_pos));
  209. if (exec_args.size() > 0) {
  210. exec = exec_args[0];
  211. exec_args.remove_at(0);
  212. // Append the Godot executable name before we append executable arguments
  213. // (since the order is reversed when using `push_front()`).
  214. r_list.push_front(OS::get_singleton()->get_executable_path());
  215. }
  216. for (int i = exec_args.size() - 1; i >= 0; i--) {
  217. // Iterate backwards as we're pushing items in the reverse order.
  218. r_list.push_front(exec_args[i].replace(" ", "%20"));
  219. }
  220. // Append Godot-specific custom arguments.
  221. custom_args = _split_cmdline_args(raw_custom_args.substr(placeholder_pos + String("%command%").size()));
  222. for (int i = 0; i < custom_args.size(); i++) {
  223. r_list.push_back(custom_args[i].replace(" ", "%20"));
  224. }
  225. } else {
  226. // Append Godot-specific custom arguments.
  227. custom_args = _split_cmdline_args(raw_custom_args);
  228. for (int i = 0; i < custom_args.size(); i++) {
  229. r_list.push_back(custom_args[i].replace(" ", "%20"));
  230. }
  231. }
  232. }
  233. }
  234. void RunInstancesDialog::apply_custom_features(int p_instance_idx) {
  235. const InstanceData &instance = instances_data[p_instance_idx];
  236. String raw_text;
  237. if (enable_multiple_instances_checkbox->is_pressed()) {
  238. if (instance.overrides_features()) {
  239. raw_text = instance.get_feature_tags();
  240. } else {
  241. raw_text = main_features_edit->get_text() + "," + instance.get_feature_tags();
  242. }
  243. } else {
  244. raw_text = main_features_edit->get_text();
  245. }
  246. const Vector<String> raw_list = raw_text.split(",");
  247. Vector<String> stripped_features;
  248. for (int i = 0; i < raw_list.size(); i++) {
  249. String f = raw_list[i].strip_edges();
  250. if (!f.is_empty()) {
  251. stripped_features.push_back(f);
  252. }
  253. }
  254. OS::get_singleton()->set_environment("GODOT_EDITOR_CUSTOM_FEATURES", String(",").join(stripped_features));
  255. }
  256. RunInstancesDialog::RunInstancesDialog() {
  257. singleton = this;
  258. set_title(TTR("Run Instances"));
  259. main_apply_timer = memnew(Timer);
  260. main_apply_timer->set_wait_time(0.5);
  261. main_apply_timer->set_one_shot(true);
  262. add_child(main_apply_timer);
  263. main_apply_timer->connect("timeout", callable_mp(this, &RunInstancesDialog::_save_main_args));
  264. instance_apply_timer = memnew(Timer);
  265. instance_apply_timer->set_wait_time(0.5);
  266. instance_apply_timer->set_one_shot(true);
  267. add_child(instance_apply_timer);
  268. instance_apply_timer->connect("timeout", callable_mp(this, &RunInstancesDialog::_save_arguments));
  269. VBoxContainer *main_vb = memnew(VBoxContainer);
  270. add_child(main_vb);
  271. GridContainer *main_gc = memnew(GridContainer);
  272. main_gc->set_columns(2);
  273. main_vb->add_child(main_gc);
  274. {
  275. Label *l = memnew(Label);
  276. l->set_text(TTR("Main Run Args:"));
  277. main_gc->add_child(l);
  278. }
  279. {
  280. Label *l = memnew(Label);
  281. l->set_text(TTR("Main Feature Tags:"));
  282. main_gc->add_child(l);
  283. }
  284. stored_data = TypedArray<Dictionary>(EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_instances_config", TypedArray<Dictionary>()));
  285. main_args_edit = memnew(LineEdit);
  286. main_args_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  287. main_args_edit->set_placeholder(TTR("Space-separated arguments, example: host player1 blue"));
  288. main_args_edit->set_accessibility_name(TTRC("Main Run Args:"));
  289. main_gc->add_child(main_args_edit);
  290. _fetch_main_args();
  291. ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &RunInstancesDialog::_fetch_main_args));
  292. main_args_edit->connect(SceneStringName(text_changed), callable_mp(this, &RunInstancesDialog::_start_main_timer).unbind(1));
  293. main_features_edit = memnew(LineEdit);
  294. main_features_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  295. main_features_edit->set_placeholder(TTR("Comma-separated tags, example: demo, steam, event"));
  296. main_features_edit->set_text(EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_main_feature_tags", ""));
  297. main_features_edit->set_accessibility_name(TTRC("Main Feature Tags:"));
  298. main_gc->add_child(main_features_edit);
  299. main_features_edit->connect(SceneStringName(text_changed), callable_mp(this, &RunInstancesDialog::_start_main_timer).unbind(1));
  300. main_vb->add_child(memnew(HSeparator));
  301. HBoxContainer *instance_hb = memnew(HBoxContainer);
  302. instance_hb->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  303. main_vb->add_child(instance_hb);
  304. enable_multiple_instances_checkbox = memnew(CheckBox);
  305. enable_multiple_instances_checkbox->set_text(TTRC("Enable Multiple Instances"));
  306. enable_multiple_instances_checkbox->set_pressed(EditorSettings::get_singleton()->get_project_metadata("debug_options", "multiple_instances_enabled", false));
  307. instance_hb->add_child(enable_multiple_instances_checkbox);
  308. enable_multiple_instances_checkbox->connect(SceneStringName(pressed), callable_mp(this, &RunInstancesDialog::_start_main_timer));
  309. instance_count = memnew(SpinBox);
  310. instance_count->set_min(1);
  311. instance_count->set_max(20);
  312. instance_count->set_value(EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_instance_count", stored_data.size()));
  313. instance_count->set_accessibility_name(TTRC("Number of Instances"));
  314. instance_hb->add_child(instance_count);
  315. instance_count->connect(SceneStringName(value_changed), callable_mp(this, &RunInstancesDialog::_start_instance_timer).unbind(1));
  316. instance_count->connect(SceneStringName(value_changed), callable_mp(this, &RunInstancesDialog::_refresh_argument_count).unbind(1));
  317. enable_multiple_instances_checkbox->connect(SceneStringName(toggled), callable_mp(instance_count, &SpinBox::set_editable));
  318. instance_count->set_editable(enable_multiple_instances_checkbox->is_pressed());
  319. MarginContainer *mc = memnew(MarginContainer);
  320. mc->set_theme_type_variation("NoBorderHorizontalWindow");
  321. mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  322. main_vb->add_child(mc);
  323. instance_tree = memnew(Tree);
  324. instance_tree->set_h_scroll_enabled(false);
  325. instance_tree->set_theme_type_variation("TreeTable");
  326. instance_tree->set_hide_folding(true);
  327. instance_tree->set_columns(4);
  328. instance_tree->set_column_titles_visible(true);
  329. instance_tree->set_column_title(COLUMN_OVERRIDE_ARGS, TTR("Override Main Run Args"));
  330. instance_tree->set_column_expand(COLUMN_OVERRIDE_ARGS, false);
  331. instance_tree->set_column_title(COLUMN_LAUNCH_ARGUMENTS, TTR("Launch Arguments"));
  332. instance_tree->set_column_title(COLUMN_OVERRIDE_FEATURES, TTR("Override Main Tags"));
  333. instance_tree->set_column_expand(COLUMN_OVERRIDE_FEATURES, false);
  334. instance_tree->set_column_title(COLUMN_FEATURE_TAGS, TTR("Feature Tags"));
  335. instance_tree->set_hide_root(true);
  336. instance_tree->set_allow_rmb_select(true);
  337. instance_tree->set_scroll_hint_mode(Tree::SCROLL_HINT_MODE_BOTTOM);
  338. popup_menu = memnew(PopupMenu);
  339. popup_menu->connect(SceneStringName(id_pressed), callable_mp(this, &RunInstancesDialog::_instance_menu_id_pressed));
  340. instance_tree->add_child(popup_menu);
  341. instance_tree->connect("item_mouse_selected", callable_mp(this, &RunInstancesDialog::_instance_tree_rmb));
  342. instance_tree->connect("empty_clicked", callable_mp(this, &RunInstancesDialog::_instance_tree_rmb));
  343. mc->add_child(instance_tree);
  344. _refresh_argument_count();
  345. instance_tree->connect("item_edited", callable_mp(this, &RunInstancesDialog::_start_instance_timer));
  346. }
  347. bool RunInstancesDialog::InstanceData::overrides_run_args() const {
  348. return item->is_checked(COLUMN_OVERRIDE_ARGS);
  349. }
  350. String RunInstancesDialog::InstanceData::get_launch_arguments() const {
  351. return item->get_text(COLUMN_LAUNCH_ARGUMENTS);
  352. }
  353. bool RunInstancesDialog::InstanceData::overrides_features() const {
  354. return item->is_checked(COLUMN_OVERRIDE_FEATURES);
  355. }
  356. String RunInstancesDialog::InstanceData::get_feature_tags() const {
  357. return item->get_text(COLUMN_FEATURE_TAGS);
  358. }