-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
HtmlExporterPluginSettings.cs
263 lines (230 loc) · 10.9 KB
/
HtmlExporterPluginSettings.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
using Newtonsoft.Json;
using Playnite.SDK;
using Playnite.SDK.Models;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.IO;
using System;
using Playnite.SDK.Data;
namespace HtmlExporterPlugin
{
public class HtmlExporterPluginSettings
{
public string OutputFolder { get; set; } = string.Empty;
public UniqueList<string> ExcludeSources { get; set; } = new UniqueList<string>();
public UniqueList<string> ExcludePlatforms { get; set; } = new UniqueList<string>();
public List<PageObject> Pages { get; set; } = new List<PageObject>();
public bool CopyImages { get; set; } = false;
public bool ExcludeHiddenGames { get; set; } = true;
public bool EraseOutputFolder { get; set; } = false;
public ImageOptions ConvertImageOptions { get; set; } = new ImageOptions();
}
public class HtmlExporterPluginSettingsViewModel : ObservableObject, ISettings
{
private readonly HtmlExporterPlugin plugin;
private HtmlExporterPluginSettings editingClone { get; set; }
private HtmlExporterPluginSettings settings;
public HtmlExporterPluginSettings Settings
{
get => settings;
set
{
settings = value;
OnPropertyChanged();
}
}
public List<string> AvailableTemplateFolders { get; set; } = new List<string>();
public List<string> AvailableSortFields { get; set; } = Constants.AvailableSortFields.AsQueryable().OrderBy(o => Constants.GetNameFromField(o, false)).ToList();
public List<string> AvailableGroupFields { get; set; } = Constants.AvailableGroupFields.AsQueryable().OrderBy(o => Constants.GetNameFromField(o, false)).ToList();
// Playnite serializes settings object to a JSON object and saves it as text file.
// If you want to exclude some property from being saved then use `JsonIgnore` ignore attribute.
// [JsonIgnore]
// public bool OptionThatWontBeSaved { get; set; } = false;
public HtmlExporterPluginSettingsViewModel(HtmlExporterPlugin 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;
foreach (DirectoryInfo dir in plugin.TemplateFolders)
{
AvailableTemplateFolders.Add(dir.Name);
}
AvailableTemplateFolders.Sort();
// Load saved settings.
var savedSettings = plugin.LoadPluginSettings<HtmlExporterPluginSettings>();
// LoadPluginSettings returns null if not saved data is available.
if (savedSettings != null)
{
Settings = savedSettings;
//new option might not exits
if (Settings.ConvertImageOptions is null)
{
Settings.ConvertImageOptions = new ImageOptions();
}
}
else
{
Settings = new HtmlExporterPluginSettings();
DoResetPages();
}
}
private void DoResetPages()
{
foreach (string groupfield in Constants.AvailableGroupFields)
{
if (groupfield == Constants.NotGroupedField)
{
continue;
}
if (Constants.FakeGameFields.Contains(groupfield))
{
Settings.Pages.Add(plugin.CreatePageObject("default list text combobox quicklinks", groupfield, true, Constants.NameField, true, true));
}
else
{
if (Constants.DefaultDescGroupFields.Contains(groupfield))
{
Settings.Pages.Add(plugin.CreatePageObject("default list text combobox quicklinks", groupfield, false, Constants.NameField, true, true));
}
else
{
Settings.Pages.Add(plugin.CreatePageObject("default list text", groupfield, true, Constants.NameField, true, true));
}
}
}
}
public void BeginEdit()
{
// Code executed when settings view is opened and user starts editing values.
editingClone = Serialization.GetClone(Settings);
plugin.SettingsView.ConvertImageOptions = Settings.ConvertImageOptions;
foreach (PageObject page in Settings.Pages)
{
plugin.SettingsView.PagesDataGrid.Items.Add(page);
}
//Credit to felixkmh https://github.com/felixkmh/DuplicateHider/
plugin.SettingsView.SourceComboBox.Items.Dispatcher.Invoke(() =>
{
List<CheckBox> checkBoxes = new List<CheckBox>();
foreach (var source in plugin.PlayniteApi.Database.Sources.AsQueryable().OrderBy(o => (o != null) ? o.Name : null).Concat(new List<GameSource> { null }))
{
string sourceName = source != null ? source.Name : Constants.UndefinedString;
if (Settings.ExcludeSources.Contains(sourceName))
{
var cb = new CheckBox { Content = sourceName, Tag = source };
cb.IsChecked = true;
checkBoxes.Add(cb);
plugin.SettingsView.SourceComboBox.Items.Add(cb);
}
}
foreach (var source in plugin.PlayniteApi.Database.Sources.AsQueryable().OrderBy(o => (o != null) ? o.Name : null).Concat(new List<GameSource> { null }))
{
string sourceName = source != null ? source.Name : Constants.UndefinedString;
if (!Settings.ExcludeSources.Contains(sourceName))
{
var cb = new CheckBox { Content = sourceName, Tag = source };
cb.IsChecked = false;
checkBoxes.Add(cb);
plugin.SettingsView.SourceComboBox.Items.Add(cb);
}
}
plugin.SettingsView.Sources = checkBoxes;
});
plugin.SettingsView.PlatformsComboBox.Items.Dispatcher.Invoke(() =>
{
List<CheckBox> checkBoxes = new List<CheckBox>();
foreach (var platform in plugin.PlayniteApi.Database.Platforms.AsQueryable().OrderBy(o => (o != null) ? o.Name : null).Concat(new List<Platform> { null }))
{
string platformName = platform != null ? platform.Name : Constants.UndefinedString;
if (Settings.ExcludeSources.Contains(platformName))
{
CheckBox cb = new CheckBox { Content = platformName, Tag = platform };
cb.IsChecked = true;
checkBoxes.Add(cb);
plugin.SettingsView.PlatformsComboBox.Items.Add(cb);
}
}
foreach (var platform in plugin.PlayniteApi.Database.Platforms.AsQueryable().OrderBy(o => (o != null) ? o.Name : null).Concat(new List<Platform> { null }))
{
string platformName = platform != null ? platform.Name : Constants.UndefinedString;
if (!Settings.ExcludeSources.Contains(platformName))
{
var cb = new CheckBox { Content = platformName, Tag = platform };
cb.IsChecked = false;
checkBoxes.Add(cb);
plugin.SettingsView.PlatformsComboBox.Items.Add(cb);
}
}
plugin.SettingsView.Platforms = checkBoxes;
});
}
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 Option2.
Settings = editingClone;
}
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 Option2.
Settings.ConvertImageOptions = plugin.SettingsView.ConvertImageOptions;
//Credit to felixkmh https://github.com/felixkmh/DuplicateHider/
plugin.SettingsView.SourceComboBox.Items.Dispatcher.Invoke(() =>
{
foreach (CheckBox cb in plugin.SettingsView.SourceComboBox.Items)
{
string name = cb.Content as string;
if (cb.IsChecked ?? false)
{
Settings.ExcludeSources.AddMissing(name);
}
else
{
Settings.ExcludeSources.Remove(name);
}
}
});
plugin.SettingsView.PlatformsComboBox.Items.Dispatcher.Invoke(() =>
{
foreach (CheckBox cb in plugin.SettingsView.PlatformsComboBox.Items)
{
string name = cb.Content as string;
if (cb.IsChecked ?? false)
{
Settings.ExcludePlatforms.AddMissing(name);
}
else
{
Settings.ExcludePlatforms.Remove(name);
}
}
});
Settings.Pages.Clear();
foreach (PageObject page in plugin.SettingsView.PagesDataGrid.Items)
{
Settings.Pages.Add(page);
}
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.
bool returnvalue = true;
errors = new List<string>();
if (String.IsNullOrEmpty(Settings.OutputFolder) || (!Directory.Exists(Settings.OutputFolder)))
{
returnvalue = false;
errors.Add(Constants.ErrorHTMLExporterNoOutputFolder);
}
if (Settings.EraseOutputFolder && !String.IsNullOrEmpty(Settings.OutputFolder) && Directory.Exists(Settings.OutputFolder) && Utils.IsRootDrive(Settings.OutputFolder))
{
returnvalue = false;
errors.Add(Constants.ErrorHTMLExporterDetectedRootDriveErase);
}
return returnvalue;
}
}
}