-
Notifications
You must be signed in to change notification settings - Fork 502
/
wxGameList.cpp
1442 lines (1279 loc) · 42.6 KB
/
wxGameList.cpp
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "gui/components/wxGameList.h"
#include "gui/helpers/wxCustomData.h"
#include "util/helpers/helpers.h"
#include "gui/GameProfileWindow.h"
#include <numeric>
#include <wx/wupdlock.h>
#include <wx/menu.h>
#include <wx/mstream.h>
#include <wx/imaglist.h>
#include <wx/textdlg.h>
#include <wx/stattext.h>
#include <wx/sizer.h>
#include <wx/wfstream.h>
#include <wx/imagpng.h>
#include <wx/string.h>
#include <wx/utils.h>
#include <wx/clipbrd.h>
#include <boost/algorithm/string.hpp>
#include <boost/tokenizer.hpp>
#include "config/ActiveSettings.h"
#include "config/LaunchSettings.h"
#include "Cafe/TitleList/GameInfo.h"
#include "Cafe/TitleList/TitleList.h"
#include "gui/CemuApp.h"
#include "gui/helpers/wxHelpers.h"
#include "gui/MainWindow.h"
#include "../wxHelper.h"
#include "Cafe/IOSU/PDM/iosu_pdm.h" // for last played and play time
#if BOOST_OS_WINDOWS
// for shortcut creation
#include <windows.h>
#include <winnls.h>
#include <shobjidl.h>
#include <objbase.h>
#include <objidl.h>
#include <shlguid.h>
#include <shlobj.h>
#endif
// public events
wxDEFINE_EVENT(wxEVT_OPEN_SETTINGS, wxCommandEvent);
wxDEFINE_EVENT(wxEVT_GAMELIST_BEGIN_UPDATE, wxCommandEvent);
wxDEFINE_EVENT(wxEVT_GAMELIST_END_UPDATE, wxCommandEvent);
wxDEFINE_EVENT(wxEVT_OPEN_GRAPHIC_PACK, wxTitleIdEvent);
// internal events
wxDEFINE_EVENT(wxEVT_GAME_ENTRY_ADDED_OR_REMOVED, wxTitleIdEvent);
constexpr uint64 kDefaultEntryData = 0x1337;
void _stripPathFilename(fs::path& path)
{
if (path.has_extension())
path = path.parent_path();
}
std::list<fs::path> _getCachesPaths(const TitleId& titleId)
{
std::list<fs::path> cachePaths{
ActiveSettings::GetCachePath(L"shaderCache/driver/vk/{:016x}.bin", titleId),
ActiveSettings::GetCachePath(L"shaderCache/precompiled/{:016x}_spirv.bin", titleId),
ActiveSettings::GetCachePath(L"shaderCache/precompiled/{:016x}_gl.bin", titleId),
ActiveSettings::GetCachePath(L"shaderCache/transferable/{:016x}_shaders.bin", titleId),
ActiveSettings::GetCachePath(L"shaderCache/transferable/{:016x}_vkpipeline.bin", titleId)};
cachePaths.remove_if(
[](const fs::path& cachePath)
{
std::error_code ec;
return !fs::exists(cachePath, ec);
});
return cachePaths;
}
wxGameList::wxGameList(wxWindow* parent, wxWindowID id)
: wxListCtrl(parent, id, wxDefaultPosition, wxDefaultSize, GetStyleFlags(Style::kList)), m_style(Style::kList)
{
const auto& config = GetConfig();
InsertColumn(ColumnHiddenName, "", wxLIST_FORMAT_LEFT, 0);
InsertColumn(ColumnIcon, "", wxLIST_FORMAT_LEFT, kListIconWidth);
InsertColumn(ColumnName, _("Game"), wxLIST_FORMAT_LEFT, config.column_width.name);
InsertColumn(ColumnVersion, _("Version"), wxLIST_FORMAT_RIGHT, config.column_width.version);
InsertColumn(ColumnDLC, _("DLC"), wxLIST_FORMAT_RIGHT, config.column_width.dlc);
InsertColumn(ColumnGameTime, _("You've played"), wxLIST_FORMAT_LEFT, config.column_width.game_time);
InsertColumn(ColumnGameStarted, _("Last played"), wxLIST_FORMAT_LEFT, config.column_width.game_started);
InsertColumn(ColumnRegion, _("Region"), wxLIST_FORMAT_LEFT, config.column_width.region);
InsertColumn(ColumnTitleID, _("Title ID"), wxLIST_FORMAT_LEFT, config.column_width.title_id);
const char transparent_bitmap[kIconWidth * kIconWidth * 4] = {0};
wxBitmap blank(transparent_bitmap, kIconWidth, kIconWidth);
blank.UseAlpha(true);
m_image_list = new wxImageList(kIconWidth, kIconWidth);
m_image_list->Add(blank);
wxListCtrl::SetImageList(m_image_list, wxIMAGE_LIST_NORMAL);
m_image_list_small = new wxImageList(kListIconWidth, kListIconWidth);
wxBitmap::Rescale(blank, {kListIconWidth, kListIconWidth});
m_image_list_small->Add(blank);
wxListCtrl::SetImageList(m_image_list_small, wxIMAGE_LIST_SMALL);
m_tooltip_window = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNO_BORDER);
auto* tooltip_sizer = new wxBoxSizer(wxVERTICAL);
tooltip_sizer->Add(new wxStaticText(m_tooltip_window, wxID_ANY, _("This game entry seems to be either an update or the base game was merged with update data\nBroken game dumps cause various problems during emulation and may even stop working at all in future Cemu versions\nPlease make sure the base game is intact and install updates only with the File->Install Update/DLC option")), 0, wxALL, 5);
m_tooltip_window->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK));
m_tooltip_window->SetSizerAndFit(tooltip_sizer);
m_tooltip_window->Hide();
m_tooltip_timer = new wxTimer(this);
Bind(wxEVT_CLOSE_WINDOW, &wxGameList::OnClose, this);
Bind(wxEVT_MOTION, &wxGameList::OnMouseMove, this);
Bind(wxEVT_LIST_KEY_DOWN, &wxGameList::OnKeyDown, this);
Bind(wxEVT_CONTEXT_MENU, &wxGameList::OnContextMenu, this);
Bind(wxEVT_LIST_ITEM_ACTIVATED, &wxGameList::OnItemActivated, this);
Bind(wxEVT_GAME_ENTRY_ADDED_OR_REMOVED, &wxGameList::OnGameEntryUpdatedByTitleId, this);
Bind(wxEVT_TIMER, &wxGameList::OnTimer, this);
Bind(wxEVT_LEAVE_WINDOW, &wxGameList::OnLeaveWindow, this);
Bind(wxEVT_LIST_COL_CLICK, &wxGameList::OnColumnClick, this);
Bind(wxEVT_LIST_COL_BEGIN_DRAG, &wxGameList::OnColumnBeginResize, this);
Bind(wxEVT_LIST_COL_END_DRAG, &wxGameList::OnColumnResize, this);
Bind(wxEVT_LIST_COL_RIGHT_CLICK, &wxGameList::OnColumnRightClick, this);
Bind(wxEVT_SIZE, &wxGameList::OnGameListSize, this);
m_callbackIdTitleList = CafeTitleList::RegisterCallback([](CafeTitleListCallbackEvent* evt, void* ctx) { ((wxGameList*)ctx)->HandleTitleListCallback(evt); }, this);
// start async worker (for icon loading)
m_async_worker_active = true;
m_async_worker_thread = std::thread(&wxGameList::AsyncWorkerThread, this);
}
wxGameList::~wxGameList()
{
CafeTitleList::UnregisterCallback(m_callbackIdTitleList);
m_tooltip_timer->Stop();
// shut down async worker
m_async_worker_active.store(false);
m_async_task_count.increment();
m_async_worker_thread.join();
// destroy image lists
delete m_image_list;
delete m_image_list_small;
// clear image cache
m_icon_cache_mtx.lock();
m_icon_cache.clear();
m_icon_cache_mtx.unlock();
}
void wxGameList::LoadConfig()
{
const auto& config = GetConfig();
SetStyle((Style)config.game_list_style, false);
if (!config.game_list_column_order.empty())
{
wxArrayInt order;
order.reserve(ColumnCounts);
const auto order_string = std::string_view(config.game_list_column_order).substr(1);
const boost::char_separator<char> sep(",");
boost::tokenizer tokens(order_string.begin(), order_string.end(), sep);
for(const auto& token : tokens)
{
order.push_back(ConvertString<int>(token, 10));
}
#ifdef wxHAS_LISTCTRL_COLUMN_ORDER
if(order.GetCount() == ColumnCounts)
SetColumnsOrder(order);
#endif
}
}
void wxGameList::OnGameListSize(wxSizeEvent &event)
{
event.Skip();
// when using a sizer-based layout, do not change the size of the wxComponent in its own wxSizeEvent handler to avoid some UI issues.
int last_col_index = 0;
for(int i = GetColumnCount() - 1; i > 0; i--)
{
#ifdef wxHAS_LISTCTRL_COLUMN_ORDER
if(GetColumnWidth(GetColumnIndexFromOrder(i)) > 0)
{
last_col_index = GetColumnIndexFromOrder(i);
break;
}
#else
if(GetColumnWidth(i) > 0)
{
last_col_index = i;
break;
}
#endif
}
wxListEvent column_resize_event(wxEVT_LIST_COL_END_DRAG);
column_resize_event.SetColumn(last_col_index);
wxPostEvent(this, column_resize_event);
}
void wxGameList::AdjustLastColumnWidth()
{
wxWindowUpdateLocker windowlock(this);
int last_col_index = 0;
int last_col_width = GetClientSize().GetWidth();
for (int i = 1; i < GetColumnCount(); i++)
{
#ifdef wxHAS_LISTCTRL_COLUMN_ORDER
if (GetColumnWidth(GetColumnIndexFromOrder(i)) > 0)
{
last_col_index = GetColumnIndexFromOrder(i);
last_col_width -= GetColumnWidth(last_col_index);
}
#else
if (GetColumnWidth(i) > 0)
{
last_col_index = i;
last_col_width -= GetColumnWidth(i);
}
#endif
}
last_col_width += GetColumnWidth(last_col_index);
if (last_col_width < GetColumnDefaultWidth(last_col_index)) // keep a minimum width
last_col_width = GetColumnDefaultWidth(last_col_index);
SetColumnWidth(last_col_index, last_col_width);
}
// todo: scale all columns using a ratio instead of hardcoding exact widths
int wxGameList::GetColumnDefaultWidth(int column)
{
switch (column)
{
case ColumnIcon:
return kListIconWidth;
case ColumnName:
return DefaultColumnSize::name;
case ColumnVersion:
return DefaultColumnSize::version;
case ColumnDLC:
return DefaultColumnSize::dlc;
case ColumnGameTime:
return DefaultColumnSize::game_time;
case ColumnGameStarted:
return DefaultColumnSize::game_started;
case ColumnRegion:
return DefaultColumnSize::region;
case ColumnTitleID:
return DefaultColumnSize::title_id;
default:
return 80;
}
}
void wxGameList::SaveConfig(bool flush)
{
auto& config = GetConfig();
config.game_list_style = (int)m_style;
#ifdef wxHAS_LISTCTRL_COLUMN_ORDER
config.game_list_column_order = fmt::format("{}", GetColumnsOrder());
#endif
if (flush)
g_config.Save();
}
bool wxGameList::IsVisible(long item) const
{
wxRect itemRect;
GetItemRect(item, itemRect);
const wxRect clientRect = GetClientRect();
bool visible = clientRect.Intersects(itemRect);
return visible;
}
void wxGameList::ReloadGameEntries(bool cached)
{
wxWindowUpdateLocker windowlock(this);
DeleteAllItems();
// tell the game list to rescan
CafeTitleList::Refresh();
// resend notifications for all known titles by re-registering the callback
CafeTitleList::UnregisterCallback(m_callbackIdTitleList);
m_callbackIdTitleList = CafeTitleList::RegisterCallback([](CafeTitleListCallbackEvent* evt, void* ctx) { ((wxGameList*)ctx)->HandleTitleListCallback(evt); }, this);
}
long wxGameList::FindListItemByTitleId(uint64 title_id) const
{
for (int i = 0; i < GetItemCount(); ++i)
{
const auto id = (uint64)GetItemData(i);
if (id == title_id)
return i;
}
return wxNOT_FOUND;
}
// get title name with cache
std::string wxGameList::GetNameByTitleId(uint64 titleId)
{
auto it = m_name_cache.find(titleId);
if (it != m_name_cache.end())
return it->second;
TitleInfo titleInfo;
if (!CafeTitleList::GetFirstByTitleId(titleId, titleInfo))
return "Unknown title";
std::string name;
if (!GetConfig().GetGameListCustomName(titleId, name))
name = titleInfo.GetMetaTitleName();
m_name_cache.emplace(titleId, name);
return name;
}
void wxGameList::SetStyle(Style style, bool save)
{
if (m_style == style)
return;
wxWindowUpdateLocker updatelock(this);
m_style = style;
SetWindowStyleFlag(GetStyleFlags(m_style));
uint64 selected_title_id = 0;
auto selection = GetNextItem(wxNOT_FOUND, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
if (selection != wxNOT_FOUND)
{
selected_title_id = (uint64)GetItemData(selection);
selection = wxNOT_FOUND;
}
switch(style)
{
case Style::kIcons:
wxListCtrl::SetImageList(m_image_list, wxIMAGE_LIST_NORMAL);
break;
case Style::kSmallIcons:
wxListCtrl::SetImageList(m_image_list_small, wxIMAGE_LIST_NORMAL);
break;
}
ReloadGameEntries();
SortEntries();
UpdateItemColors();
if(selection != wxNOT_FOUND)
{
SetItemState(selection, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
EnsureVisible(selection);
}
if(save)
{
GetConfig().game_list_style = (int)m_style;
g_config.Save();
}
if (style == Style::kList)
ApplyGameListColumnWidths();
}
long wxGameList::GetStyleFlags(Style style) const
{
switch (style)
{
case Style::kList:
return (wxLC_SINGLE_SEL | wxLC_REPORT);
case Style::kIcons:
return (wxLC_SINGLE_SEL | wxLC_ICON);
case Style::kSmallIcons:
return (wxLC_SINGLE_SEL | wxLC_ICON);
default:
wxASSERT(false);
return (wxLC_SINGLE_SEL | wxLC_REPORT);
}
}
void wxGameList::UpdateItemColors(sint32 startIndex)
{
wxWindowUpdateLocker lock(this);
wxColour bgColourPrimary = GetBackgroundColour();
wxColour bgColourSecondary = wxHelper::CalculateAccentColour(bgColourPrimary);
for (int i = startIndex; i < GetItemCount(); ++i)
{
const auto titleId = (uint64)GetItemData(i);
if (GetConfig().IsGameListFavorite(titleId))
{
SetItemBackgroundColour(i, kFavoriteColor);
SetItemTextColour(i, 0x000000UL);
}
else if ((i&1) != 0)
{
SetItemBackgroundColour(i, bgColourPrimary);
SetItemTextColour(i, wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
}
else
{
SetItemBackgroundColour(i, bgColourSecondary);
SetItemTextColour(i, wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
}
}
}
static inline int order_to_int(const std::weak_ordering &wo)
{
// no easy conversion seems to exists in C++20
if (wo == std::weak_ordering::less)
return -1;
else if (wo == std::weak_ordering::greater)
return 1;
return 0;
}
int wxGameList::SortComparator(uint64 titleId1, uint64 titleId2, SortData* sortData)
{
const auto isFavoriteA = GetConfig().IsGameListFavorite(titleId1);
const auto isFavoriteB = GetConfig().IsGameListFavorite(titleId2);
const auto& name1 = GetNameByTitleId(titleId1);
const auto& name2 = GetNameByTitleId(titleId2);
if(sortData->dir > 0)
return order_to_int(std::tie(isFavoriteB, name1) <=> std::tie(isFavoriteA, name2));
else
return order_to_int(std::tie(isFavoriteB, name2) <=> std::tie(isFavoriteA, name1));
}
int wxGameList::SortFunction(wxIntPtr item1, wxIntPtr item2, wxIntPtr sortData)
{
const auto sort_data = (SortData*)sortData;
const int dir = sort_data->dir;
return sort_data->thisptr->SortComparator((uint64)item1, (uint64)item2, sort_data);
}
void wxGameList::SortEntries(int column)
{
if (column == -1)
column = s_last_column;
else
{
if (s_last_column == column)
{
s_last_column = 0;
s_direction = -1;
}
else
{
s_last_column = column;
s_direction = 1;
}
}
switch (column)
{
case ColumnName:
case ColumnGameTime:
case ColumnGameStarted:
case ColumnRegion:
{
SortData data{ this, column, s_direction };
SortItems(SortFunction, (wxIntPtr)&data);
break;
}
}
}
void wxGameList::OnKeyDown(wxListEvent& event)
{
event.Skip();
if (m_style != Style::kList)
return;
const auto keycode = std::tolower(event.m_code);
if (keycode == WXK_LEFT)
{
const auto item_count = GetItemCount();
if (item_count > 0)
{
auto selection = (int)GetNextItem(wxNOT_FOUND, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
if (selection == wxNOT_FOUND)
selection = 0;
else
selection = std::max(0, selection - GetCountPerPage());
SetItemState(wxNOT_FOUND, 0, wxLIST_STATE_SELECTED);
SetItemState(selection, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
EnsureVisible(selection);
}
}
else if (keycode == WXK_RIGHT)
{
const auto item_count = GetItemCount();
if (item_count > 0)
{
auto selection = (int)GetNextItem(wxNOT_FOUND, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
if (selection == wxNOT_FOUND)
selection = 0;
selection = std::min(item_count - 1, selection + GetCountPerPage());
SetItemState(wxNOT_FOUND, 0, wxLIST_STATE_SELECTED);
SetItemState(selection, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
EnsureVisible(selection);
}
}
}
enum ContextMenuEntries
{
kContextMenuRefreshGames = wxID_HIGHEST + 1,
kContextMenuStart,
kWikiPage,
kContextMenuFavorite,
kContextMenuEditName,
kContextMenuGameFolder,
kContextMenuSaveFolder,
kContextMenuUpdateFolder,
kContextMenuDLCFolder,
kContextMenuEditGraphicPacks,
kContextMenuEditGameProfile,
kContextMenuRemoveCache,
kContextMenuStyleList,
kContextMenuStyleIcon,
kContextMenuStyleIconSmall,
kContextMenuCreateShortcut,
kContextMenuCopyTitleName,
kContextMenuCopyTitleId,
kContextMenuCopyTitleImage
};
void wxGameList::OnContextMenu(wxContextMenuEvent& event)
{
auto& config = GetConfig();
wxMenu menu;
menu.Bind(wxEVT_COMMAND_MENU_SELECTED, &wxGameList::OnContextMenuSelected, this);
const auto selection = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
if (selection != wxNOT_FOUND)
{
const auto title_id = (uint64)GetItemData(selection);
GameInfo2 gameInfo = CafeTitleList::GetGameInfo(title_id);
if (gameInfo.IsValid())
{
menu.SetClientData((void*)title_id);
menu.Append(kContextMenuStart, _("&Start"));
bool isFavorite = GetConfig().IsGameListFavorite(title_id);
std::error_code ec;
menu.AppendSeparator();
menu.AppendCheckItem(kContextMenuFavorite, _("&Favorite"))->Check(isFavorite);
menu.Append(kContextMenuEditName, _("&Edit name"));
menu.AppendSeparator();
menu.Append(kWikiPage, _("&Wiki page"));
menu.Append(kContextMenuGameFolder, _("&Game directory"));
menu.Append(kContextMenuSaveFolder, _("&Save directory"))->Enable(fs::is_directory(gameInfo.GetSaveFolder(), ec));
menu.Append(kContextMenuUpdateFolder, _("&Update directory"))->Enable(gameInfo.HasUpdate());
menu.Append(kContextMenuDLCFolder, _("&DLC directory"))->Enable(gameInfo.HasAOC());
menu.AppendSeparator();
menu.Append(kContextMenuRemoveCache, _("&Remove shader caches"))->Enable(!_getCachesPaths(gameInfo.GetBaseTitleId()).empty());
menu.AppendSeparator();
menu.Append(kContextMenuEditGraphicPacks, _("&Edit graphic packs"));
menu.Append(kContextMenuEditGameProfile, _("&Edit game profile"));
menu.AppendSeparator();
#if BOOST_OS_LINUX || BOOST_OS_WINDOWS
menu.Append(kContextMenuCreateShortcut, _("&Create shortcut"));
#endif
menu.AppendSeparator();
menu.Append(kContextMenuCopyTitleName, _("&Copy Title Name"));
menu.Append(kContextMenuCopyTitleId, _("&Copy Title ID"));
menu.Append(kContextMenuCopyTitleImage, _("&Copy Title Image"));
menu.AppendSeparator();
}
}
menu.Append(kContextMenuRefreshGames, _("&Refresh game list"))->Enable(!CafeTitleList::IsScanning());
menu.AppendSeparator();
menu.AppendRadioItem(kContextMenuStyleList, _("Style: &List"))->Check(m_style == Style::kList);
menu.AppendRadioItem(kContextMenuStyleIcon, _("Style: &Icons"))->Check(m_style == Style::kIcons);
menu.AppendRadioItem(kContextMenuStyleIconSmall, _("Style: &Small Icons"))->Check(m_style == Style::kSmallIcons);
PopupMenu(&menu);
}
void wxGameList::OnContextMenuSelected(wxCommandEvent& event)
{
const auto title_id = (uint64)((wxMenu*)event.GetEventObject())->GetClientData();
if (title_id)
{
GameInfo2 gameInfo = CafeTitleList::GetGameInfo(title_id);
if (gameInfo.IsValid())
{
switch (event.GetId())
{
case kContextMenuStart:
{
MainWindow::RequestLaunchGame(gameInfo.GetBase().GetPath(), wxLaunchGameEvent::INITIATED_BY::GAME_LIST);
break;
}
case kContextMenuFavorite:
GetConfig().SetGameListFavorite(title_id, !GetConfig().IsGameListFavorite(title_id));
SortEntries();
UpdateItemColors();
//SaveConfig();
break;
case kContextMenuEditName:
{
std::string customName = "";
if (!GetConfig().GetGameListCustomName(title_id, customName))
customName.clear();
wxTextEntryDialog dialog(this, wxEmptyString, _("Enter a custom game title"), wxHelper::FromUtf8(customName));
if(dialog.ShowModal() == wxID_OK)
{
const auto custom_name = dialog.GetValue();
GetConfig().SetGameListCustomName(title_id, custom_name.utf8_string());
m_name_cache.clear();
g_config.Save();
// update list entry
for (int i = 0; i < GetItemCount(); ++i)
{
const auto id = (uint64)GetItemData(i);
if (id == title_id)
{
if (m_style == Style::kList)
SetItem(i, ColumnName, wxHelper::FromUtf8(GetNameByTitleId(title_id)));
break;
}
}
SortEntries();
UpdateItemColors();
}
break;
}
case kContextMenuGameFolder:
{
fs::path path(gameInfo.GetBase().GetPath());
_stripPathFilename(path);
wxLaunchDefaultBrowser(wxHelper::FromUtf8(fmt::format("file:{}", _pathToUtf8(path))));
break;
}
case kWikiPage:
{
// https://wiki.cemu.info/wiki/GameIDs
// WUP-P-ALZP
if (gameInfo.GetBase().ParseXmlInfo())
{
std::string productCode = gameInfo.GetBase().GetMetaInfo()->GetProductCode();
const auto tokens = TokenizeView(productCode, '-');
wxASSERT(!tokens.empty());
const std::string company_code = gameInfo.GetBase().GetMetaInfo()->GetCompanyCode();
wxASSERT(company_code.size() >= 2);
wxLaunchDefaultBrowser(wxHelper::FromUtf8(fmt::format("https://wiki.cemu.info/wiki/{}{}", *tokens.rbegin(), company_code.substr(company_code.size() - 2).c_str())));
}
break;
}
case kContextMenuSaveFolder:
{
wxLaunchDefaultBrowser(wxHelper::FromUtf8(fmt::format("file:{}", _pathToUtf8(gameInfo.GetSaveFolder()))));
break;
}
case kContextMenuUpdateFolder:
{
fs::path path(gameInfo.GetUpdate().GetPath());
_stripPathFilename(path);
wxLaunchDefaultBrowser(wxHelper::FromUtf8(fmt::format("file:{}", _pathToUtf8(path))));
break;
}
case kContextMenuDLCFolder:
{
fs::path path(gameInfo.GetAOC().front().GetPath());
_stripPathFilename(path);
wxLaunchDefaultBrowser(wxHelper::FromUtf8(fmt::format("file:{}", _pathToUtf8(path))));
break;
}
case kContextMenuRemoveCache:
{
RemoveCache(_getCachesPaths(gameInfo.GetBaseTitleId()), gameInfo.GetTitleName());
break;
}
case kContextMenuEditGraphicPacks:
{
wxTitleIdEvent open_event(wxEVT_OPEN_GRAPHIC_PACK, title_id);
ProcessWindowEvent(open_event);
break;
}
case kContextMenuEditGameProfile:
{
(new GameProfileWindow(GetParent(), title_id))->Show();
break;
}
case kContextMenuCreateShortcut:
{
#if BOOST_OS_LINUX || BOOST_OS_WINDOWS
CreateShortcut(gameInfo);
#endif
break;
}
case kContextMenuCopyTitleName:
{
if (wxTheClipboard->Open())
{
wxTheClipboard->SetData(new wxTextDataObject(wxString::FromUTF8(gameInfo.GetTitleName())));
wxTheClipboard->Close();
}
break;
}
case kContextMenuCopyTitleId:
{
if (wxTheClipboard->Open())
{
wxTheClipboard->SetData(new wxTextDataObject(fmt::format("{:016x}", gameInfo.GetBaseTitleId())));
wxTheClipboard->Close();
}
break;
}
case kContextMenuCopyTitleImage:
{
if (wxTheClipboard->Open())
{
int icon_large;
int icon_small;
if (!QueryIconForTitle(title_id, icon_large, icon_small))
break;
auto icon = m_image_list->GetBitmap(icon_large);
wxTheClipboard->SetData(new wxBitmapDataObject(icon));
wxTheClipboard->Close();
}
break;
}
}
}
}
switch (event.GetId())
{
case kContextMenuRefreshGames:
ReloadGameEntries(false);
break;
case kContextMenuStyleList:
SetStyle(Style::kList);
break;
case kContextMenuStyleIcon:
SetStyle(Style::kIcons);
break;
case kContextMenuStyleIconSmall:
SetStyle(Style::kSmallIcons);
break;
}
}
void wxGameList::OnColumnClick(wxListEvent& event)
{
const int column = event.GetColumn();
SortEntries(column);
UpdateItemColors();
event.Skip();
}
void wxGameList::OnColumnRightClick(wxListEvent& event)
{
enum ItemIds
{
ResetWidth = wxID_HIGHEST + 1,
ResetOrder,
ShowName,
ShowVersion,
ShowDlc,
ShowGameTime,
ShowLastPlayed,
ShowRegion,
ShowTitleId
};
const int column = event.GetColumn();
wxMenu menu;
menu.SetClientObject(new wxCustomData(column));
menu.Append(ResetWidth, _("Reset &width"));
menu.Append(ResetOrder, _("Reset &order")) ;
menu.AppendSeparator();
menu.AppendCheckItem(ShowName, _("Show &name"))->Check(GetColumnWidth(ColumnName) > 0);
menu.AppendCheckItem(ShowVersion, _("Show &version"))->Check(GetColumnWidth(ColumnVersion) > 0);
menu.AppendCheckItem(ShowDlc, _("Show &dlc"))->Check(GetColumnWidth(ColumnDLC) > 0);
menu.AppendCheckItem(ShowGameTime, _("Show &game time"))->Check(GetColumnWidth(ColumnGameTime) > 0);
menu.AppendCheckItem(ShowLastPlayed, _("Show &last played"))->Check(GetColumnWidth(ColumnGameStarted) > 0);
menu.AppendCheckItem(ShowRegion, _("Show ®ion"))->Check(GetColumnWidth(ColumnRegion) > 0);
menu.AppendCheckItem(ShowTitleId, _("Show &title ID"))->Check(GetColumnWidth(ColumnTitleID) > 0);
menu.Bind(wxEVT_COMMAND_MENU_SELECTED,
[this](wxCommandEvent& event) {
event.Skip();
const auto menu = dynamic_cast<wxMenu*>(event.GetEventObject());
const int column = dynamic_cast<wxCustomData<int>*>(menu->GetClientObject())->GetData();
auto& config = GetConfig();
switch (event.GetId())
{
case ShowName:
config.column_width.name = menu->IsChecked(ShowName) ? DefaultColumnSize::name : 0;
break;
case ShowVersion:
config.column_width.version = menu->IsChecked(ShowVersion) ? DefaultColumnSize::version : 0;
break;
case ShowDlc:
config.column_width.dlc = menu->IsChecked(ShowDlc) ? DefaultColumnSize::dlc : 0;
break;
case ShowGameTime:
config.column_width.game_time = menu->IsChecked(ShowGameTime) ? DefaultColumnSize::game_time : 0;
break;
case ShowLastPlayed:
config.column_width.game_started = menu->IsChecked(ShowLastPlayed) ? DefaultColumnSize::game_started : 0;
break;
case ShowRegion:
config.column_width.region = menu->IsChecked(ShowRegion) ? DefaultColumnSize::region : 0;
break;
case ShowTitleId:
config.column_width.title_id = menu->IsChecked(ShowTitleId) ? DefaultColumnSize::title_id : 0;
break;
case ResetWidth:
{
switch (column)
{
case ColumnIcon:
break;
case ColumnName:
config.column_width.name = DefaultColumnSize::name;
break;
case ColumnVersion:
config.column_width.version = DefaultColumnSize::version;
break;
case ColumnDLC:
config.column_width.dlc = DefaultColumnSize::dlc;
break;
case ColumnGameTime:
config.column_width.game_time = DefaultColumnSize::game_time;
break;
case ColumnGameStarted:
config.column_width.game_started = DefaultColumnSize::game_started;
break;
case ColumnRegion:
config.column_width.region = DefaultColumnSize::region;
break;
case ColumnTitleID:
config.column_width.title_id = DefaultColumnSize::title_id;
default:
return;
}
break;
}
case ResetOrder:
{
config.game_list_column_order.clear();
wxArrayInt order(ColumnCounts);
std::iota(order.begin(), order.end(), 0);
#ifdef wxHAS_LISTCTRL_COLUMN_ORDER
SetColumnsOrder(order);
#endif
//ApplyGameListColumnWidths();
//Refresh();
//return;
}
}
g_config.Save();
ApplyGameListColumnWidths();
});
PopupMenu(&menu);
event.Skip();
}
void wxGameList::ApplyGameListColumnWidths()
{
const auto& config = GetConfig();
wxWindowUpdateLocker lock(this);
SetColumnWidth(ColumnIcon, kListIconWidth);
SetColumnWidth(ColumnName, config.column_width.name);
SetColumnWidth(ColumnVersion, config.column_width.version);
SetColumnWidth(ColumnDLC, config.column_width.dlc);
SetColumnWidth(ColumnGameTime, config.column_width.game_time);
SetColumnWidth(ColumnGameStarted, config.column_width.game_started);
SetColumnWidth(ColumnRegion, config.column_width.region);
SetColumnWidth(ColumnTitleID, config.column_width.title_id);
AdjustLastColumnWidth();
}
void wxGameList::OnColumnBeginResize(wxListEvent& event)
{
const int column = event.GetColumn();
const int width = GetColumnWidth(column);
int last_col_index = 0;
for(int i = GetColumnCount() - 1; i > 0; i--)
{
#ifdef wxHAS_LISTCTRL_COLUMN_ORDER
if(GetColumnWidth(GetColumnIndexFromOrder(i)) > 0)
{
last_col_index = GetColumnIndexFromOrder(i);
break;
}
#else
if(GetColumnWidth(i) > 0)
{
last_col_index = i;
break;
}
#endif
}
if (width == 0 || column == ColumnIcon || column == last_col_index) // dont resize hidden name, icon, and last column
event.Veto();
else
event.Skip();
}
void wxGameList::OnColumnResize(wxListEvent& event)
{
event.Skip();
if(m_style != Style::kList)
return;
const int column = event.GetColumn();
const int width = GetColumnWidth(column);
auto& config = GetConfig();
switch (column)
{
case ColumnName:
config.column_width.name = width;
break;
case ColumnVersion:
config.column_width.version = width;
break;
case ColumnDLC:
config.column_width.dlc = width;
break;
case ColumnGameTime:
config.column_width.game_time = width;
break;
case ColumnGameStarted:
config.column_width.game_started = width;
break;
case ColumnRegion:
config.column_width.region = width;
break;
default:
break;
}
g_config.Save();
AdjustLastColumnWidth();
}
void wxGameList::OnClose(wxCloseEvent& event)
{
event.Skip();
m_exit = true;
}
int wxGameList::FindInsertPosition(TitleId titleId)
{
SortData data{ this, s_last_column, s_direction };
const auto itemCount = GetItemCount();
if (itemCount == 0)
return 0;
// todo - optimize this with binary search