run_instances_dialog.cpp 16 KB

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