-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathImportEmulatedGamesDialog.vala
651 lines (522 loc) · 17.5 KB
/
ImportEmulatedGamesDialog.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
This file is part of GameHub.
Copyright (C) 2018-2019 Anatoliy Kashkin
GameHub is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GameHub is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GameHub. If not, see <https://www.gnu.org/licenses/>.
*/
using Gtk;
using Gdk;
using Granite;
using GLib;
using Gee;
using GameHub.Utils;
using GameHub.UI.Widgets;
using GameHub.Data;
using GameHub.Data.DB;
using GameHub.Data.Compat;
using GameHub.Data.Sources.User;
namespace GameHub.UI.Dialogs
{
public class ImportEmulatedGamesDialog: Dialog
{
private const string[] LIBRETRO_IGNORED_CORES = { "3dengine", "ffmpeg", "dosbox", "dosbox_svn", "dosbox_svn_glide" };
private const string[] LIBRETRO_IGNORED_FILES = { "bin", "dat", "exe", "zip", "7z", "gz" };
private const int RESPONSE_IMPORT = 10;
public signal void game_added(UserGame game);
private HashMap<string, EmulatedGame> detected_games;
private Box content;
private FileChooserEntry dir_chooser;
private Label status_label;
private Spinner status_spinner;
private ScrolledWindow found_list_scroll;
private ListBox found_list;
private CheckButton select_all;
private Button import_btn;
public ImportEmulatedGamesDialog()
{
Object(transient_for: Windows.MainWindow.instance, resizable: false, title: _("Import emulated games"));
}
construct
{
get_style_context().add_class("rounded");
get_style_context().add_class(Gtk.STYLE_CLASS_FLAT);
modal = true;
content = new Box(Orientation.VERTICAL, 4);
content.set_size_request(640, 480);
content.margin = 4;
var dir_hbox = new Box(Orientation.HORIZONTAL, 12);
dir_hbox.margin_start = dir_hbox.margin_end = 8;
dir_chooser = new FileChooserEntry(_("Select directory with emulated games"), FileChooserAction.SELECT_FOLDER);
dir_chooser.hexpand = true;
dir_chooser.file_set.connect(start_search);
dir_hbox.add(new HeaderLabel(_("Directory")));
dir_hbox.add(dir_chooser);
var header_label = new HeaderLabel(_("Detected games"));
header_label.margin_start = header_label.margin_end = 8;
header_label.xalign = 0;
header_label.hexpand = true;
found_list_scroll = new ScrolledWindow(null, null);
found_list_scroll.expand = true;
found_list_scroll.get_style_context().add_class(Gtk.STYLE_CLASS_FRAME);
found_list = new ListBox();
found_list.selection_mode = SelectionMode.NONE;
found_list.sensitive = false;
found_list.set_sort_func((row, row2) => ((EmulatedGameRow) row).sort((EmulatedGameRow) row2));
found_list.set_header_func((row, prev) => ((EmulatedGameRow) row).header((EmulatedGameRow) prev));
found_list_scroll.add(found_list);
content.add(dir_hbox);
content.add(header_label);
content.add(found_list_scroll);
var status_hbox = new Box(Orientation.HORIZONTAL, 10);
status_hbox.margin_start = 6;
select_all = new CheckButton.with_label(_("Select all"));
select_all.margin_start = 2;
select_all.active = true;
select_all.no_show_all = true;
select_all.visible = false;
select_all.xalign = 0.5f;
select_all.toggled.connect(select_all_toggled);
status_label = new Label(_("Select directory to import"));
status_label.get_style_context().add_class(Gtk.STYLE_CLASS_DIM_LABEL);
status_label.margin_start = 2;
status_label.xalign = 0;
status_label.hexpand = true;
status_spinner = new Spinner();
status_spinner.halign = Align.END;
status_spinner.no_show_all = true;
status_spinner.visible = false;
status_hbox.add(select_all);
status_hbox.add(status_spinner);
status_hbox.add(status_label);
import_btn = (Button) add_button(_("Import"), RESPONSE_IMPORT);
import_btn.get_style_context().add_class(Gtk.STYLE_CLASS_SUGGESTED_ACTION);
import_btn.sensitive = false;
var bbox = (ButtonBox) import_btn.get_parent();
bbox.margin_end = 8;
bbox.add(status_hbox);
bbox.set_child_secondary(status_hbox, true);
bbox.set_child_non_homogeneous(status_hbox, true);
response.connect((source, response_id) => {
switch(response_id)
{
case RESPONSE_IMPORT:
found_list.foreach(r => {
var row = (EmulatedGameRow) r;
row.import_as_usergame();
});
destroy();
break;
}
});
delete_event.connect(() => {
destroy();
});
get_content_area().add(content);
show_all();
}
private bool is_toggling_selection = false;
private bool is_updating_selection = false;
private void select_all_toggled()
{
if(is_toggling_selection || is_updating_selection) return;
is_toggling_selection = true;
select_all.inconsistent = false;
found_list.foreach(r => {
var row = (EmulatedGameRow) r;
row.import.active = select_all.active;
});
is_toggling_selection = false;
update_selection();
}
private void update_selection()
{
if(is_updating_selection || is_toggling_selection) return;
is_updating_selection = true;
bool all_selected = true;
bool none_selected = true;
found_list.foreach(r => {
var row = (EmulatedGameRow) r;
all_selected = all_selected && row.import.active;
none_selected = none_selected && !row.import.active;
});
select_all.active = all_selected;
select_all.inconsistent = !all_selected && !none_selected;
import_btn.sensitive = !none_selected;
is_updating_selection = false;
}
private void add_row(EmulatedGameRow row)
{
found_list.add(row);
row.game_added.connect(g => game_added(g));
row.import.toggled.connect(update_selection);
}
private void start_search()
{
if(!dir_chooser.sensitive || dir_chooser.file == null) return;
dir_chooser.sensitive = false;
select_all.visible = false;
found_list.sensitive = false;
status_spinner.visible = true;
status_spinner.start();
detected_games = new HashMap<string, EmulatedGame>();
found_list.foreach(r => r.destroy());
Utils.thread("ImportEmulatedGamesDialogSearch", () => {
debug("[ImportEmulatedGamesDialog] Starting search in '%s'", dir_chooser.file.get_path());
search_emulators();
search_retroarch();
Idle.add(() => {
select_all.visible = true;
found_list.sensitive = true;
status_spinner.visible = false;
status_spinner.stop();
status_label.label = null;
foreach(var game in detected_games.values)
{
add_row(new EmulatedGameRow(game));
}
update_selection();
return Source.REMOVE;
});
});
}
private void search_emulators()
{
var root = dir_chooser.file;
var emulators = Tables.Emulators.get_all();
foreach(var emu in emulators)
{
var pattern = emu.game_executable_pattern;
if(pattern != null) pattern = pattern.strip();
if(pattern.length > 0)
{
var subpatterns = pattern.split("|");
foreach(var sp in subpatterns)
{
sp = sp.strip();
bool is_dir_based = sp.has_prefix("./");
if(is_dir_based)
{
if(sp == "./") continue;
sp = sp.substring(sp.index_of_nth_char(2));
}
var files_list = Utils.run({"find", root.get_path(), "-path", "*/" + sp}, null, null, false, true, false);
var files = files_list.split("\n");
foreach(var file_path in files)
{
var file = FSUtils.file(file_path);
if(file.query_file_type(FileQueryInfoFlags.NONE) == FileType.DIRECTORY) continue;
var dir = file.get_parent();
var name = file.get_basename();
if(is_dir_based)
{
if("/" in sp)
{
var sp_parts = sp.split("/");
for(int i = 0; i < sp_parts.length - 1; i++)
{
dir = dir.get_parent();
}
}
name = dir.get_basename();
}
else
{
var ext_index = name.last_index_of_char('.');
if(ext_index > 0)
{
name = name.substring(0, ext_index);
}
}
debug("[search_emulators: %s] '%s': %s [%s]", emu.name, name, file.get_path(), dir.get_path());
var tool = new EmulatedGame.Tool(emu);
var game = detected_games.has_key(file_path) ? detected_games.get(file_path) : new EmulatedGame(name, file, dir, is_dir_based ? dir.get_parent() : dir);
game.tools.add(tool);
detected_games.set(file_path, game);
}
}
}
}
}
private void search_retroarch()
{
var root = dir_chooser.file;
var retroarch = RetroArch.instance;
if(!retroarch.installed)
{
warning("[search_retroarch] RetroArch is not installed");
return;
}
if(!retroarch.has_cores)
{
warning("[search_retroarch] No libretro cores found");
return;
}
var core_info_dir = FSUtils.file(FSUtils.Paths.Settings.instance.libretro_core_info_dir);
if(core_info_dir == null || !core_info_dir.query_exists())
{
warning("[search_retroarch] libretro core info dir does not exist");
return;
}
foreach(var core in retroarch.cores)
{
if(core in LIBRETRO_IGNORED_CORES) continue;
var info = core_info_dir.get_child(core + RetroArch.LIBRETRO_CORE_INFO_SUFFIX);
if(info == null || !info.query_exists()) continue;
status_label.label = "RetroArch: " + core;
try
{
string full_info;
FileUtils.get_contents(info.get_path(), out full_info);
if(!("supported_extensions = \"" in full_info)) continue;
string? core_name = null;
string? display_name = null;
string? supported_extensions = null;
var lines = full_info.split("\n");
foreach(var line in lines)
{
if("corename = \"" in line)
{
core_name = line.replace("corename = \"", "").replace("\"", "").strip();
}
else if("display_name = \"" in line)
{
display_name = line.replace("display_name = \"", "").replace("\"", "").strip();
status_label.label = "RetroArch: " + display_name;
}
else if("supported_extensions = \"" in line)
{
supported_extensions = line.replace("supported_extensions = \"", "").replace("\"", "").strip();
}
}
if(supported_extensions != null && supported_extensions.length > 0)
{
var exts = supported_extensions.split("|");
foreach(var ext in exts)
{
ext = ext.strip();
if(ext in LIBRETRO_IGNORED_FILES) continue;
var files_list = Utils.run({"find", root.get_path(), "-path", "*/*." + ext}, null, null, false, true, false);
var files = files_list.split("\n");
foreach(var file_path in files)
{
var file = FSUtils.file(file_path);
if(file.query_file_type(FileQueryInfoFlags.NONE) == FileType.DIRECTORY) continue;
var dir = file.get_parent();
var name = file.get_basename();
var ext_index = name.last_index_of_char('.');
if(ext_index > 0)
{
name = name.substring(0, ext_index);
}
debug("[search_retroarch: %s] '%s': %s [%s]", core, name, file.get_path(), dir.get_path());
var tool = new EmulatedGame.Tool.libretro(core, core_name, display_name);
var game = detected_games.has_key(file_path) ? detected_games.get(file_path) : new EmulatedGame(name, file, dir, dir);
game.tools.add(tool);
detected_games.set(file_path, game);
}
}
}
}
catch(Error e)
{
warning("[search_retroarch] Error while reading core info: %s", e.message);
}
}
}
private class EmulatedGame: Object
{
public string name;
public File file;
public File directory;
public File parent_directory;
public ArrayList<Tool> tools;
public EmulatedGame(string name, File file, File directory, File parent_directory)
{
this.name = name;
this.file = file;
this.directory = directory;
this.parent_directory = parent_directory;
this.tools = new ArrayList<Tool>();
}
public class Tool: Object
{
public string short_name;
public string name;
public Emulator? emulator = null;
public string? libretro_core = null;
public bool uses_libretro = false;
public Tool(Emulator emulator)
{
this.short_name = this.name = emulator.name;
this.emulator = emulator;
this.uses_libretro = false;
}
public Tool.libretro(string core, string? core_name, string? display_name)
{
this.short_name = "RetroArch: " + (core_name != null ? core_name : core);
this.name = "RetroArch: " + (display_name != null ? display_name : (core_name != null ? core_name : core));
this.libretro_core = core;
this.uses_libretro = true;
}
}
}
private class EmulatedGameRow: ListBoxRow
{
public signal void game_added(UserGame game);
public EmulatedGame game;
public CheckButton import;
public Entry title;
private Gtk.ListStore tools_model;
private int tools_model_size = 0;
private Gtk.TreeIter tools_iter;
public ComboBox tools;
public EmulatedGame.Tool selected_tool;
public EmulatedGameRow(EmulatedGame game)
{
this.game = game;
var grid = new Grid();
grid.column_spacing = 8;
grid.margin_start = grid.margin_end = 8;
grid.margin_top = grid.margin_bottom = 4;
import = new CheckButton();
import.active = true;
var title_hbox = new Box(Orientation.HORIZONTAL, 0);
title_hbox.expand = true;
title_hbox.get_style_context().add_class(Gtk.STYLE_CLASS_LINKED);
title = new Entry();
title.expand = true;
title.xalign = 0;
title_hbox.add(title);
tools_model = new Gtk.ListStore(2, typeof(string), typeof(EmulatedGame.Tool));
foreach(var tool in game.tools)
{
tools_model.append(out tools_iter);
tools_model.set(tools_iter, 0, tool.short_name);
tools_model.set(tools_iter, 1, tool);
tools_model_size++;
}
tools = new ComboBox.with_model(tools_model);
tools.set_size_request(128, -1);
tools.popup_fixed_width = false;
CellRendererText r_name = new CellRendererText();
r_name.ellipsize = Pango.EllipsizeMode.END;
r_name.width_chars = r_name.max_width_chars = 20;
r_name.xpad = 4;
tools.pack_start(r_name, true);
tools.add_attribute(r_name, "text", 0);
tools.changed.connect(() => {
if(tools_model_size == 0) return;
Value v;
tools.get_active_iter(out tools_iter);
tools_model.get_value(tools_iter, 1, out v);
selected_tool = v as EmulatedGame.Tool;
tools.tooltip_text = selected_tool.name;
});
tools.active = 0;
tools.sensitive = tools_model_size > 1;
title_hbox.add(tools);
title.text = game.name;
tooltip_text = game.file.get_path();
import.toggled.connect(() => {
title_hbox.sensitive = import.active;
});
grid.attach(import, 0, 0);
grid.attach(title_hbox, 1, 0);
child = grid;
show_all();
}
public int sort(EmulatedGameRow other)
{
var dir = game.parent_directory.get_path();
var other_dir = other.game.parent_directory.get_path();
return strcmp(dir.collate_key_for_filename(), other_dir.collate_key_for_filename());
}
public void header(EmulatedGameRow? prev)
{
if(prev == null || prev.game.parent_directory.get_path() != game.parent_directory.get_path())
{
var header = new HeaderLabel(game.parent_directory.get_path());
header.expand = true;
header.show_all();
set_header(header);
}
else
{
set_header(null);
}
}
public void import_as_usergame()
{
if(!import.active) return;
var g = new UserGame(title.text.strip(), game.directory, game.file, "", false);
if(selected_tool.uses_libretro)
{
g.compat_tool = "retroarch";
g.compat_tool_settings = "{\"compat_options_saved\":true,\"force_compat\":true,\"retroarch\":{\"options\":{\"core\":\"" + selected_tool.libretro_core + "\"}}}";
}
else
{
g.compat_tool = "emulator";
g.compat_tool_settings = "{\"compat_options_saved\":true,\"force_compat\":true,\"emulator\":{\"options\":{\"emulator\":\"" + selected_tool.emulator.name + "\"}}}";
var basename = game.file.get_basename();
var ext_index = basename.last_index_of_char('.');
if(ext_index > 0)
{
basename = basename.substring(0, ext_index);
}
var image = find_by_pattern(selected_tool.emulator.game_image_pattern, game.directory, basename);
var icon = find_by_pattern(selected_tool.emulator.game_icon_pattern, game.directory, basename);
if(image != null)
{
g.image = image.get_uri();
}
if(icon != null)
{
g.icon = icon.get_uri();
}
}
g.platforms.clear();
g.platforms.add(Platform.EMULATED);
g.save();
User.instance.add_game(g);
game_added(g);
}
private File? find_by_pattern(string? pattern, File directory, string basename)
{
if(pattern != null)
{
var subpatterns = pattern.split("|");
foreach(var sp in subpatterns)
{
sp = sp.strip();
if(sp.has_prefix("./"))
{
if(sp == "./") continue;
sp = sp.substring(sp.index_of_nth_char(2));
}
sp = sp.replace("${basename}", basename).replace("$basename", basename);
var files_list = Utils.run({"find", directory.get_path(), "-path", "*/" + sp}, null, null, false, true, false);
var files = files_list.split("\n");
foreach(var file_path in files)
{
var file = FSUtils.file(file_path);
if(file != null && file.query_exists())
{
return file;
}
}
}
}
return null;
}
}
}
}