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