-
Notifications
You must be signed in to change notification settings - Fork 0
/
RiotGamesLibrarySettings.cs
272 lines (247 loc) · 11.7 KB
/
RiotGamesLibrarySettings.cs
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
using Playnite.SDK;
using Playnite.SDK.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Microsoft.Win32;
using Shell32;
namespace RiotGamesLibrary
{
public class CompanionApp
{
public string ExePath { get; set; }
public string LaunchArgs { get; set; }
public bool CloseWithGame { get; set; } = true;
public bool CompanionEnabled { get; set; } = true;
public string ExeName { get; set; }
public bool GenerateAction { get; set; } = true;
}
public class RiotGamesLibrarySettings : ObservableObject
{
private bool _CloseRiotClient = true;
private string _RiotClientPath = RiotClient.InstallationPath;
private string _LeaguePath = RiotGame.IsInstalled("rg-leagueoflegends") ? RiotGame.InstallPath("rg-leagueoflegends") : ResourceProvider.GetString("LOCRiotGamesNotInstalled");
private bool _LeaguePBE = false;
private string _ValorantPath = RiotGame.IsInstalled("rg-valorant") ? RiotGame.InstallPath("rg-valorant") : ResourceProvider.GetString("LOCRiotGamesNotInstalled");
private string _LORPath = RiotGame.IsInstalled("rg-legendsofruneterra") ? RiotGame.InstallPath("rg-legendsofruneterra") : ResourceProvider.GetString("LOCRiotGamesNotInstalled");
private bool _LeagueUseShortCompName = true;
private bool _ValUseShortCompName = true;
private int _VersionNum = 1;
public bool CloseRiotClient { get => _CloseRiotClient; set => SetValue(ref _CloseRiotClient, value); }
public string RiotClientPath { get => _RiotClientPath; set => SetValue(ref _RiotClientPath, value); }
public string LeaguePath { get => _LeaguePath; set => SetValue(ref _LeaguePath, value); }
public bool LeaguePBE { get => _LeaguePBE; set => SetValue(ref _LeaguePBE, value); }
public string ValorantPath { get => _ValorantPath; set => SetValue(ref _ValorantPath, value); }
public string LORPath { get => _LORPath; set => SetValue(ref _LORPath, value); }
public ObservableCollection<CompanionApp> LeagueCompanions { get; set; } = new ObservableCollection<CompanionApp>();
public ObservableCollection<CompanionApp> ValorantCompanions { get; set; } = new ObservableCollection<CompanionApp>();
public bool LeagueUseShortCompName { get => _LeagueUseShortCompName; set => SetValue(ref _LeagueUseShortCompName, value); }
public bool ValUseShortCompName { get => _ValUseShortCompName; set => SetValue(ref _ValUseShortCompName, value); }
public int VersionNum { get => _VersionNum; set => SetValue(ref _VersionNum, value); }
}
public class RiotGamesLibrarySettingsViewModel : ObservableObject, ISettings
{
private readonly ILogger logger = LogManager.GetLogger();
private readonly RiotGamesLibrary plugin;
private RiotGamesLibrarySettings editingClone { get; set; }
private RiotGamesLibrarySettings settings;
public RiotGamesLibrarySettings Settings
{
get => settings;
set
{
settings = value;
OnPropertyChanged();
}
}
public RelayCommand<string> AddCompCommand
{
get => new RelayCommand<string>((a) =>
{
Tuple<string, string, string> companion = SelectApp();
if (companion != null)
{
var compsList = (a == "rg-leagueoflegends") ? Settings.LeagueCompanions : Settings.ValorantCompanions;
foreach (var comp in compsList)
{
if (companion.Item1 == comp.ExePath)
{
Playnite.SDK.API.Instance.Dialogs.ShowErrorMessage(ResourceProvider.GetString("LOCRiotGamesCompAlreadyAdded"), ResourceProvider.GetString("LOCRiotGamesCompAddError"));
return;
}
}
CompanionApp lc = new CompanionApp();
lc.ExePath = companion.Item1;
lc.LaunchArgs = companion.Item2;
if (companion.Item1.ToLower().Contains("overwolf"))
{
lc.ExeName = $"{Path.GetFileNameWithoutExtension(companion.Item3)} (Overwolf)";
}
else
{
lc.ExeName = Path.GetFileNameWithoutExtension(companion.Item1);
}
compsList.Add(lc);
}
});
}
public RelayCommand<CompanionApp> RemoveLeagueCompCommand
{
get => new RelayCommand<CompanionApp>((a) =>
{
RemoveCompanion(a, GamesEnums.League);
});
}
public RelayCommand<CompanionApp> RemoveValorantCompCommand
{
get => new RelayCommand<CompanionApp>((a) =>
{
RemoveCompanion(a, GamesEnums.Valorant);
});
}
private void RemoveCompanion(CompanionApp comp, GamesEnums rgame)
{
if (comp == null) { return; }
string gameId = (rgame == GamesEnums.League) ? "rg-leagueoflegends" : "rg-valorant";
var comps = (rgame == GamesEnums.League) ? Settings.LeagueCompanions : Settings.ValorantCompanions;
if (comp.ExePath != string.Empty)
{
foreach (var game in Playnite.SDK.API.Instance.Database.Games)
{
if (game.PluginId != plugin.Id || game.GameId != gameId)
{
continue;
}
if (game.GameActions == null) { break; }
for (int i = 0; i < game.GameActions.Count; i++)
{
if (game.GameActions[i].Name == $"Open {comp.ExeName}")
{
game.GameActions.Remove(game.GameActions[i]);
Playnite.SDK.API.Instance.Database.Games.Update(game);
break;
}
}
break;
}
}
comps.Remove(comp);
}
private Tuple<string, string, string> SelectApp()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.DereferenceLinks = false;
openFileDialog.Filter = "Executable file (.exe)|*.exe";
Nullable<bool> result = openFileDialog.ShowDialog();
// Process open file dialog box results
if (result == true)
{
string lnkName = string.Empty;
string targetname = openFileDialog.FileName;
string args = string.Empty;
if (openFileDialog.FileName.Contains(".lnk"))
{
//logger.Debug("File chosen is a shortcut, trying to extract target and args");
// Open document
lnkName = openFileDialog.FileName;
string pathOnly = System.IO.Path.GetDirectoryName(openFileDialog.FileName);
string filenameOnly = System.IO.Path.GetFileName(openFileDialog.FileName);
Shell shell = new Shell();
Shell32.Folder folder = shell.NameSpace(pathOnly);
FolderItem folderItem = folder.ParseName(filenameOnly);
if (folderItem != null)
{
Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
targetname = link.Target.Path; // <-- main difference
args = link.Arguments;
if (targetname.StartsWith("{"))
{ // it is prefixed with {54A35DE2-guid-for-program-files-x86-QZ32BP4}
int endguid = targetname.IndexOf("}");
if (endguid > 0)
{
targetname = "C:\\program files (x86)" + targetname.Substring(endguid + 1);
}
}
//string file = LnkToFile(openFileDialog.FileName);
}
}
return new Tuple<string, string, string>(targetname, args, lnkName);
}
return null;
}
public RiotGamesLibrarySettingsViewModel(RiotGamesLibrary plugin)
{
// Injecting your plugin instance is required for Save/Load method because Playnite saves data to a location based on what plugin requested the operation.
this.plugin = plugin;
// Load saved settings.
var savedSettings = plugin.LoadPluginSettings<RiotGamesLibrarySettings>();
// LoadPluginSettings returns null if no saved data is available.
if (savedSettings != null)
{
Settings = savedSettings;
}
else
{
Settings = new RiotGamesLibrarySettings();
}
}
public void BeginEdit()
{
// Code executed when settings view is opened and user starts editing values.
editingClone = Serialization.GetClone(Settings);
}
public void CancelEdit()
{
// Code executed when user decides to cancel any changes made since BeginEdit was called.
// This method should revert any changes made to Option1 and CloseRiotClient.
Settings = editingClone;
plugin.UpdateCompanionActions();
}
public void EndEdit()
{
// Code executed when user decides to confirm changes made since BeginEdit was called.
// This method should save settings made to Option1 and CloseRiotClient.
plugin.UpdateCompanionActions();
plugin.SavePluginSettings(Settings);
}
public bool VerifySettings(out List<string> errors)
{
// Code execute when user decides to confirm changes made since BeginEdit was called.
// Executed before EndEdit is called and EndEdit is not called if false is returned.
// List of errors is presented to user if verification fails.
errors = new List<string>();
foreach (var comp in Settings.LeagueCompanions)
{
if (comp.ExePath != string.Empty && (!File.Exists(comp.ExePath) || Path.GetExtension(comp.ExePath) != ".exe"))
{
errors.Add($"{ResourceProvider.GetString("LOCRiotGamesLeagueCompanion")} {comp.ExePath} {ResourceProvider.GetString("LOCRiotGamesInvalidExe")}");
return false;
}
if (comp.ExePath == string.Empty)
{
errors.Add(ResourceProvider.GetString("LOCRiotGamesEmptyPath"));
return false;
}
}
foreach (var comp in Settings.ValorantCompanions)
{
if (comp.ExePath != string.Empty && (!File.Exists(comp.ExePath) || Path.GetExtension(comp.ExePath) != ".exe"))
{
errors.Add($"{ResourceProvider.GetString("LOCRiotGamesValorantCompanion")} {comp.ExePath} {ResourceProvider.GetString("LOCRiotGamesInvalidExe")}");
return false;
}
if (comp.ExePath == string.Empty)
{
errors.Add(ResourceProvider.GetString("LOCRiotGamesEmptyPath"));
return false;
}
}
return true;
}
}
}