-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsViewModel.cs
More file actions
270 lines (244 loc) · 10.9 KB
/
SettingsViewModel.cs
File metadata and controls
270 lines (244 loc) · 10.9 KB
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
using Playnite.SDK;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Input;
namespace GameSnapPlugin
{
public class SettingsViewModel : ISettings, INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string? name = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
private readonly GameSnapPlugin _plugin;
private GameSnapSettings _settings;
private GameSnapSettings? _editingClone;
public GameSnapSettings Settings
{
get => _settings;
set
{
_settings = value;
OnPropertyChanged();
// Force all bound fields to refresh when settings object changes
OnPropertyChanged(nameof(ImageExtensionsText));
OnPropertyChanged(nameof(VideoExtensionsText));
OnPropertyChanged(nameof(AdditionalSourcesText));
OnPropertyChanged(nameof(CustomEmulatorFoldersText));
}
}
// Extensions text bindings
public string ImageExtensionsText
{
get => string.Join(", ", _settings.ImageExtensions);
set
{
_settings.ImageExtensions = new List<string>(
value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim().ToLowerInvariant())
.Where(s => s.StartsWith(".")));
OnPropertyChanged();
}
}
public string VideoExtensionsText
{
get => string.Join(", ", _settings.VideoExtensions);
set
{
_settings.VideoExtensions = new List<string>(
value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim().ToLowerInvariant())
.Where(s => s.StartsWith(".")));
OnPropertyChanged();
}
}
// Additional sources text binding (one per line)
public string CustomEmulatorFoldersText
{
get => string.Join(Environment.NewLine, _settings.CustomEmulatorFolders);
set
{
_settings.CustomEmulatorFolders = new List<string>(
value.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim())
.Where(s => !string.IsNullOrEmpty(s)));
OnPropertyChanged();
}
}
public string AdditionalSourcesText
{
get => string.Join(Environment.NewLine, _settings.AdditionalSourceFolders);
set
{
_settings.AdditionalSourceFolders = new List<string>(
value.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim())
.Where(s => !string.IsNullOrEmpty(s)));
OnPropertyChanged();
}
}
// Backup folder binding
public string BackupFolder
{
get => _settings.BackupFolder;
set { _settings.BackupFolder = value; OnPropertyChanged(); }
}
// Commands
public ICommand BrowseSourceCommand { get; }
public ICommand BrowseDestinationCommand { get; }
public ICommand BrowseBackupCommand { get; }
public ICommand BrowseSteamCommand { get; }
public ICommand OpenDictionaryCommand { get; }
public ICommand OpenLogCommand { get; }
public SettingsViewModel(GameSnapPlugin plugin)
{
_plugin = plugin;
_settings = plugin.LoadSettings();
BrowseSourceCommand = new RelayCommand(BrowseSource);
BrowseDestinationCommand = new RelayCommand(BrowseDestination);
BrowseBackupCommand = new RelayCommand(BrowseBackup);
BrowseSteamCommand = new RelayCommand(BrowseSteam);
OpenDictionaryCommand = new RelayCommand(OpenDictionary);
OpenLogCommand = new RelayCommand(OpenLog);
}
public void BeginEdit()
{
// Always reload from disk when opening settings
// This ensures the UI reflects what was actually saved
_settings = _plugin.LoadSettings();
_editingClone = CloneSettings(_settings);
OnPropertyChanged(nameof(Settings));
OnPropertyChanged(nameof(ImageExtensionsText));
OnPropertyChanged(nameof(VideoExtensionsText));
OnPropertyChanged(nameof(AdditionalSourcesText));
OnPropertyChanged(nameof(CustomEmulatorFoldersText));
OnPropertyChanged(nameof(BackupFolder));
}
public void CancelEdit()
{
if (_editingClone != null)
Settings = _editingClone;
}
public void EndEdit()
{
try
{
// Sync all text-bound fields back to settings object before saving
_settings.ImageExtensions = ParseExtensions(ImageExtensionsText);
_settings.VideoExtensions = ParseExtensions(VideoExtensionsText);
_settings.AdditionalSourceFolders = ParseLines(AdditionalSourcesText);
_settings.CustomEmulatorFolders = ParseLines(CustomEmulatorFoldersText);
_plugin.SaveSettings(_settings);
_plugin.ApplySettings(_settings);
}
catch (Exception ex)
{
_plugin.PlayniteApi.Dialogs.ShowErrorMessage(
$"GameSnap failed to save settings:\n{ex.Message}", "GameSnap");
}
}
private static System.Collections.Generic.List<string> ParseExtensions(string text)
{
return new System.Collections.Generic.List<string>(
text.Split(new char[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim().ToLowerInvariant())
.Where(s => s.StartsWith(".")));
}
private static System.Collections.Generic.List<string> ParseLines(string text)
{
return new System.Collections.Generic.List<string>(
text.Split(new char[] { '\r', '\n' }, System.StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim())
.Where(s => !string.IsNullOrEmpty(s)));
}
public bool VerifySettings(out List<string> errors)
{
errors = new List<string>();
if (string.IsNullOrWhiteSpace(_settings.SourceFolder))
errors.Add("Source folder is required.");
if (string.IsNullOrWhiteSpace(_settings.DestinationBase))
errors.Add("Destination folder is required.");
return errors.Count == 0;
}
private void BrowseSource()
{
var path = _plugin.PlayniteApi.Dialogs.SelectFolder();
if (path != null) { _settings.SourceFolder = path; OnPropertyChanged(nameof(Settings)); }
}
private void BrowseDestination()
{
var path = _plugin.PlayniteApi.Dialogs.SelectFolder();
if (path != null) { _settings.DestinationBase = path; OnPropertyChanged(nameof(Settings)); }
}
private void BrowseBackup()
{
var path = _plugin.PlayniteApi.Dialogs.SelectFolder();
if (path != null) { _settings.BackupFolder = path; OnPropertyChanged(nameof(Settings)); }
}
private void BrowseSteam()
{
var path = _plugin.PlayniteApi.Dialogs.SelectFolder();
if (path != null) { _settings.SteamPath = path; OnPropertyChanged(nameof(Settings)); }
}
private void OpenDictionary()
{
var path = Path.Combine(_plugin.GetPluginUserDataPath(), "dictionary.txt");
if (!File.Exists(path))
File.WriteAllText(path, "# Format:\n# [Game Name]\n# alias1\n");
var psi = new System.Diagnostics.ProcessStartInfo("notepad.exe", path)
{
UseShellExecute = true
};
System.Diagnostics.Process.Start(psi);
}
private void OpenLog()
{
var path = Path.Combine(_plugin.GetPluginUserDataPath(), "gamesnap.log");
if (File.Exists(path))
{
var psi = new System.Diagnostics.ProcessStartInfo("notepad.exe", path)
{
UseShellExecute = true
};
System.Diagnostics.Process.Start(psi);
}
else
_plugin.PlayniteApi.Dialogs.ShowMessage("No log file yet.", "GameSnap");
}
private static GameSnapSettings CloneSettings(GameSnapSettings src) => new GameSnapSettings
{
SourceFolder = src.SourceFolder,
AdditionalSourceFolders = new List<string>(src.AdditionalSourceFolders),
DestinationBase = src.DestinationBase,
PollingIntervalSeconds = src.PollingIntervalSeconds,
UsePlayniteDetection = src.UsePlayniteDetection,
UseWindowFallback = src.UseWindowFallback,
AutoCreateFolders = src.AutoCreateFolders,
MoveUnmatchedToFolder = src.MoveUnmatchedToFolder,
UnmatchedFolderName = src.UnmatchedFolderName,
ShowNotifications = src.ShowNotifications,
RenamePattern = src.RenamePattern,
EnableBackup = src.EnableBackup,
BackupFolder = src.BackupFolder,
EnableSteamSupport = src.EnableSteamSupport,
SteamPath = src.SteamPath,
EnableLocalProviderIntegration = src.EnableLocalProviderIntegration,
EnableEmulatorSupport = src.EnableEmulatorSupport,
CustomEmulatorFolders = new List<string>(src.CustomEmulatorFolders),
ImageExtensions = new List<string>(src.ImageExtensions),
VideoExtensions = new List<string>(src.VideoExtensions),
WindowBlacklist = new List<string>(src.WindowBlacklist),
};
}
public class RelayCommand : ICommand
{
private readonly Action _execute;
public RelayCommand(Action execute) => _execute = execute;
public event EventHandler? CanExecuteChanged;
public bool CanExecute(object? parameter) => true;
public void Execute(object? parameter) => _execute();
}
}