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