run_instances_dialog.cpp 15 KB

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