Path: blob/master/editor/run/run_instances_dialog.cpp
9896 views
/**************************************************************************/1/* run_instances_dialog.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "run_instances_dialog.h"3132#include "core/config/project_settings.h"33#include "editor/settings/editor_settings.h"34#include "editor/themes/editor_scale.h"35#include "scene/gui/check_box.h"36#include "scene/gui/grid_container.h"37#include "scene/gui/label.h"38#include "scene/gui/line_edit.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"4445void RunInstancesDialog::_fetch_main_args() {46if (!main_args_edit->has_focus()) { // Only set the text if the user is not currently editing it.47main_args_edit->set_text(GLOBAL_GET("editor/run/main_run_args"));48}49}5051void RunInstancesDialog::_start_main_timer() {52main_apply_timer->start();53}5455void RunInstancesDialog::_start_instance_timer() {56instance_apply_timer->start();57}5859void RunInstancesDialog::_refresh_argument_count() {60instance_tree->clear();61instance_tree->create_item(); // Root.6263while (instance_count->get_value() > stored_data.size()) {64stored_data.append(Dictionary());65}6667instances_data.resize(instance_count->get_value());68InstanceData *instances_write = instances_data.ptrw();6970for (int i = 0; i < instances_data.size(); i++) {71InstanceData instance;72const Dictionary &instance_data = stored_data[i];7374_create_instance(instance, instance_data, i + 1);75instances_write[i] = instance;76}77}7879void RunInstancesDialog::_create_instance(InstanceData &p_instance, const Dictionary &p_data, int p_idx) {80TreeItem *instance_item = instance_tree->create_item();81p_instance.item = instance_item;8283instance_item->set_cell_mode(COLUMN_OVERRIDE_ARGS, TreeItem::CELL_MODE_CHECK);84instance_item->set_editable(COLUMN_OVERRIDE_ARGS, true);85instance_item->set_text(COLUMN_OVERRIDE_ARGS, TTR("Enabled"));86instance_item->set_checked(COLUMN_OVERRIDE_ARGS, p_data.get("override_args", false));8788instance_item->set_editable(COLUMN_LAUNCH_ARGUMENTS, true);89instance_item->set_text(COLUMN_LAUNCH_ARGUMENTS, p_data.get("arguments", String()));9091instance_item->set_cell_mode(COLUMN_OVERRIDE_FEATURES, TreeItem::CELL_MODE_CHECK);92instance_item->set_editable(COLUMN_OVERRIDE_FEATURES, true);93instance_item->set_text(COLUMN_OVERRIDE_FEATURES, TTR("Enabled"));94instance_item->set_checked(COLUMN_OVERRIDE_FEATURES, p_data.get("override_features", false));9596instance_item->set_editable(COLUMN_FEATURE_TAGS, true);97instance_item->set_text(COLUMN_FEATURE_TAGS, p_data.get("features", String()));98}99100void RunInstancesDialog::_save_main_args() {101ProjectSettings::get_singleton()->set_setting("editor/run/main_run_args", main_args_edit->get_text());102ProjectSettings::get_singleton()->save();103EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_main_feature_tags", main_features_edit->get_text());104EditorSettings::get_singleton()->set_project_metadata("debug_options", "multiple_instances_enabled", enable_multiple_instances_checkbox->is_pressed());105}106107void RunInstancesDialog::_save_arguments() {108for (int i = 0; i < instances_data.size(); i++) {109const InstanceData &instance = instances_data[i];110Dictionary dict;111dict["override_args"] = instance.overrides_run_args();112dict["arguments"] = instance.get_launch_arguments();113dict["override_features"] = instance.overrides_features();114dict["features"] = instance.get_feature_tags();115stored_data[i] = dict;116}117EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_instances_config", stored_data);118EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_instance_count", instance_count->get_value());119}120121Vector<String> RunInstancesDialog::_split_cmdline_args(const String &p_arg_string) const {122Vector<String> split_args;123int arg_start = 0;124bool is_quoted = false;125char32_t quote_char = '-';126char32_t arg_char;127int arg_length;128for (int i = 0; i < p_arg_string.length(); i++) {129arg_char = p_arg_string[i];130if (arg_char == '\"' || arg_char == '\'') {131if (i == 0 || p_arg_string[i - 1] != '\\') {132if (is_quoted) {133if (arg_char == quote_char) {134is_quoted = false;135quote_char = '-';136}137} else {138is_quoted = true;139quote_char = arg_char;140}141}142} else if (!is_quoted && arg_char == ' ') {143arg_length = i - arg_start;144if (arg_length > 0) {145split_args.push_back(p_arg_string.substr(arg_start, arg_length));146}147arg_start = i + 1;148}149}150arg_length = p_arg_string.length() - arg_start;151if (arg_length > 0) {152split_args.push_back(p_arg_string.substr(arg_start, arg_length));153}154return split_args;155}156157void RunInstancesDialog::_instance_menu_id_pressed(int p_option) {158switch (p_option) {159case CLEAR_ITEM: {160int item_to_clear = popup_menu->get_item_metadata(0);161if (item_to_clear >= 0 && item_to_clear < stored_data.size()) {162stored_data[item_to_clear] = Dictionary();163}164} break;165case CLEAR_ALL: {166stored_data.clear();167stored_data.resize(instance_count->get_value());168} break;169}170171_start_instance_timer();172_refresh_argument_count();173}174175void RunInstancesDialog::_instance_tree_rmb(const Vector2 &p_pos, MouseButton p_button) {176if (p_button != MouseButton::RIGHT) {177return;178}179180popup_menu->clear();181popup_menu->add_item(TTR("Clear"), CLEAR_ITEM);182183TreeItem *item = instance_tree->get_item_at_position(p_pos);184if (item) {185popup_menu->set_item_metadata(0, item->get_index());186} else {187popup_menu->set_item_disabled(0, true);188}189190popup_menu->add_item(TTR("Clear All"), CLEAR_ALL);191popup_menu->set_position(instance_tree->get_screen_position() + p_pos);192popup_menu->reset_size();193popup_menu->popup();194}195196void RunInstancesDialog::popup_dialog() {197popup_centered_clamped(Size2(1200, 600) * EDSCALE, 0.8);198}199200int RunInstancesDialog::get_instance_count() const {201if (enable_multiple_instances_checkbox->is_pressed()) {202return instance_count->get_value();203} else {204return 1;205}206}207208void RunInstancesDialog::get_argument_list_for_instance(int p_idx, List<String> &r_list) const {209bool override_args = instances_data[p_idx].overrides_run_args();210bool use_multiple_instances = enable_multiple_instances_checkbox->is_pressed();211String raw_custom_args;212213if (use_multiple_instances) {214if (override_args) {215raw_custom_args = instances_data[p_idx].get_launch_arguments();216} else {217raw_custom_args = main_args_edit->get_text() + " " + instances_data[p_idx].get_launch_arguments();218}219} else {220raw_custom_args = main_args_edit->get_text();221}222223String exec = OS::get_singleton()->get_executable_path();224225if (!raw_custom_args.is_empty()) {226// Allow the user to specify a command to run, similar to Steam's launch options.227// In this case, Godot will no longer be run directly; it's up to the underlying command228// to run it. For instance, this can be used on Linux to force a running project229// to use Optimus using `prime-run` or similar.230// Example: `prime-run %command% --time-scale 0.5`231const int placeholder_pos = raw_custom_args.find("%command%");232233Vector<String> custom_args;234235if (placeholder_pos != -1) {236// Prepend executable-specific custom arguments.237// If nothing is placed before `%command%`, behave as if no placeholder was specified.238Vector<String> exec_args = _split_cmdline_args(raw_custom_args.substr(0, placeholder_pos));239if (exec_args.size() > 0) {240exec = exec_args[0];241exec_args.remove_at(0);242243// Append the Godot executable name before we append executable arguments244// (since the order is reversed when using `push_front()`).245r_list.push_front(OS::get_singleton()->get_executable_path());246}247248for (int i = exec_args.size() - 1; i >= 0; i--) {249// Iterate backwards as we're pushing items in the reverse order.250r_list.push_front(exec_args[i].replace(" ", "%20"));251}252253// Append Godot-specific custom arguments.254custom_args = _split_cmdline_args(raw_custom_args.substr(placeholder_pos + String("%command%").size()));255for (int i = 0; i < custom_args.size(); i++) {256r_list.push_back(custom_args[i].replace(" ", "%20"));257}258} else {259// Append Godot-specific custom arguments.260custom_args = _split_cmdline_args(raw_custom_args);261for (int i = 0; i < custom_args.size(); i++) {262r_list.push_back(custom_args[i].replace(" ", "%20"));263}264}265}266}267268void RunInstancesDialog::apply_custom_features(int p_instance_idx) {269const InstanceData &instance = instances_data[p_instance_idx];270271String raw_text;272if (enable_multiple_instances_checkbox->is_pressed()) {273if (instance.overrides_features()) {274raw_text = instance.get_feature_tags();275} else {276raw_text = main_features_edit->get_text() + "," + instance.get_feature_tags();277}278} else {279raw_text = main_features_edit->get_text();280}281282const Vector<String> raw_list = raw_text.split(",");283Vector<String> stripped_features;284285for (int i = 0; i < raw_list.size(); i++) {286String f = raw_list[i].strip_edges();287if (!f.is_empty()) {288stripped_features.push_back(f);289}290}291OS::get_singleton()->set_environment("GODOT_EDITOR_CUSTOM_FEATURES", String(",").join(stripped_features));292}293294RunInstancesDialog::RunInstancesDialog() {295singleton = this;296set_title(TTR("Run Instances"));297298main_apply_timer = memnew(Timer);299main_apply_timer->set_wait_time(0.5);300main_apply_timer->set_one_shot(true);301add_child(main_apply_timer);302main_apply_timer->connect("timeout", callable_mp(this, &RunInstancesDialog::_save_main_args));303304instance_apply_timer = memnew(Timer);305instance_apply_timer->set_wait_time(0.5);306instance_apply_timer->set_one_shot(true);307add_child(instance_apply_timer);308instance_apply_timer->connect("timeout", callable_mp(this, &RunInstancesDialog::_save_arguments));309310VBoxContainer *main_vb = memnew(VBoxContainer);311add_child(main_vb);312313GridContainer *main_gc = memnew(GridContainer);314main_gc->set_columns(2);315main_vb->add_child(main_gc);316317{318Label *l = memnew(Label);319l->set_text(TTR("Main Run Args:"));320main_gc->add_child(l);321}322323{324Label *l = memnew(Label);325l->set_text(TTR("Main Feature Tags:"));326main_gc->add_child(l);327}328329stored_data = TypedArray<Dictionary>(EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_instances_config", TypedArray<Dictionary>()));330331main_args_edit = memnew(LineEdit);332main_args_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);333main_args_edit->set_placeholder(TTR("Space-separated arguments, example: host player1 blue"));334main_args_edit->set_accessibility_name(TTRC("Main Run Args:"));335main_gc->add_child(main_args_edit);336_fetch_main_args();337ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &RunInstancesDialog::_fetch_main_args));338main_args_edit->connect(SceneStringName(text_changed), callable_mp(this, &RunInstancesDialog::_start_main_timer).unbind(1));339340main_features_edit = memnew(LineEdit);341main_features_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);342main_features_edit->set_placeholder(TTR("Comma-separated tags, example: demo, steam, event"));343main_features_edit->set_text(EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_main_feature_tags", ""));344main_features_edit->set_accessibility_name(TTRC("Main Feature Tags:"));345main_gc->add_child(main_features_edit);346main_features_edit->connect(SceneStringName(text_changed), callable_mp(this, &RunInstancesDialog::_start_main_timer).unbind(1));347348main_vb->add_child(memnew(HSeparator));349350HBoxContainer *instance_hb = memnew(HBoxContainer);351instance_hb->set_alignment(BoxContainer::ALIGNMENT_CENTER);352main_vb->add_child(instance_hb);353354enable_multiple_instances_checkbox = memnew(CheckBox);355enable_multiple_instances_checkbox->set_text(TTRC("Enable Multiple Instances"));356enable_multiple_instances_checkbox->set_pressed(EditorSettings::get_singleton()->get_project_metadata("debug_options", "multiple_instances_enabled", false));357instance_hb->add_child(enable_multiple_instances_checkbox);358enable_multiple_instances_checkbox->connect(SceneStringName(pressed), callable_mp(this, &RunInstancesDialog::_start_main_timer));359360instance_count = memnew(SpinBox);361instance_count->set_min(1);362instance_count->set_max(20);363instance_count->set_value(EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_instance_count", stored_data.size()));364instance_count->set_accessibility_name(TTRC("Number of Instances"));365366instance_hb->add_child(instance_count);367instance_count->connect(SceneStringName(value_changed), callable_mp(this, &RunInstancesDialog::_start_instance_timer).unbind(1));368instance_count->connect(SceneStringName(value_changed), callable_mp(this, &RunInstancesDialog::_refresh_argument_count).unbind(1));369enable_multiple_instances_checkbox->connect(SceneStringName(toggled), callable_mp(instance_count, &SpinBox::set_editable));370instance_count->set_editable(enable_multiple_instances_checkbox->is_pressed());371372instance_tree = memnew(Tree);373instance_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);374instance_tree->set_h_scroll_enabled(false);375instance_tree->set_columns(4);376instance_tree->set_column_titles_visible(true);377instance_tree->set_column_title(COLUMN_OVERRIDE_ARGS, TTR("Override Main Run Args"));378instance_tree->set_column_expand(COLUMN_OVERRIDE_ARGS, false);379instance_tree->set_column_title(COLUMN_LAUNCH_ARGUMENTS, TTR("Launch Arguments"));380instance_tree->set_column_title(COLUMN_OVERRIDE_FEATURES, TTR("Override Main Tags"));381instance_tree->set_column_expand(COLUMN_OVERRIDE_FEATURES, false);382instance_tree->set_column_title(COLUMN_FEATURE_TAGS, TTR("Feature Tags"));383instance_tree->set_hide_root(true);384instance_tree->set_allow_rmb_select(true);385386popup_menu = memnew(PopupMenu);387popup_menu->connect(SceneStringName(id_pressed), callable_mp(this, &RunInstancesDialog::_instance_menu_id_pressed));388instance_tree->add_child(popup_menu);389390instance_tree->connect("item_mouse_selected", callable_mp(this, &RunInstancesDialog::_instance_tree_rmb));391instance_tree->connect("empty_clicked", callable_mp(this, &RunInstancesDialog::_instance_tree_rmb));392main_vb->add_child(instance_tree);393394_refresh_argument_count();395instance_tree->connect("item_edited", callable_mp(this, &RunInstancesDialog::_start_instance_timer));396}397398bool RunInstancesDialog::InstanceData::overrides_run_args() const {399return item->is_checked(COLUMN_OVERRIDE_ARGS);400}401402String RunInstancesDialog::InstanceData::get_launch_arguments() const {403return item->get_text(COLUMN_LAUNCH_ARGUMENTS);404}405406bool RunInstancesDialog::InstanceData::overrides_features() const {407return item->is_checked(COLUMN_OVERRIDE_FEATURES);408}409410String RunInstancesDialog::InstanceData::get_feature_tags() const {411return item->get_text(COLUMN_FEATURE_TAGS);412}413414415