forked from Reloaded-Project/Reloaded-II
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPublishModDialogViewModel.cs
More file actions
331 lines (282 loc) · 12.5 KB
/
Copy pathPublishModDialogViewModel.cs
File metadata and controls
331 lines (282 loc) · 12.5 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
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
using static Reloaded.Mod.Loader.Update.Packaging.Publisher;
using CompressionLevel = SevenZip.CompressionLevel;
using IOEx = Reloaded.Mod.Loader.IO.Utility.IOEx;
using Paths = Sewer56.DeltaPatchGenerator.Lib.Utility.Paths;
namespace Reloaded.Mod.Launcher.Lib.Models.ViewModel.Dialog;
/// <summary>
/// ViewModel used to publish a singular mod to the masses.
/// </summary>
public class PublishModDialogViewModel : ObservableObject
{
private PathTuple<ModConfig> _modTuple;
/// <summary>
/// Target for publishing the package to.
/// </summary>
public PublishTarget PublishTarget { get; set; } = PublishTarget.Default;
/// <summary>
/// The folder where the published mod should be saved.
/// </summary>
public string OutputFolder { get; set; }
/// <summary>
/// Paths to older versions of the same mods.
/// </summary>
public ObservableCollection<StringWrapper> OlderVersionFolders { get; set; } = new ObservableCollection<StringWrapper>();
/// <summary>
/// The currently selected <see cref="OlderVersionFolders"/> item.
/// </summary>
public StringWrapper? SelectedOlderVersionFolder { get; set; } = null;
/// <summary>
/// Regexes of files to make sure are ignored.
/// </summary>
public ObservableCollection<StringWrapper> IgnoreRegexes { get; set; }
/// <summary>
/// The currently selected <see cref="IgnoreRegexes"/> item.
/// </summary>
public StringWrapper? SelectedIgnoreRegex { get; set; } = null;
/// <summary>
/// Name of the generated 7z file.
/// </summary>
public string? PackageName { get; set; } = null;
/// <summary>
/// Regexes of files to make sure are not ignored.
/// </summary>
public ObservableCollection<StringWrapper> IncludeRegexes { get; set; }
/// <summary>
/// The currently selected <see cref="IncludeRegexes"/> item.
/// </summary>
public StringWrapper? SelectedIncludeRegex { get; set; } = null;
/// <summary>
/// The progress of the build operation.
/// </summary>
public double BuildProgress { get; set; }
/// <summary>
/// True if currently building, else false.
/// </summary>
public bool CanBuild { get; set; } = true;
/// <summary>
/// True to show last version's UI items, else false.
/// </summary>
public bool ShowLastVersionUiItems { get; set; } = true;
/// <summary>
/// The amount by which the mod should be compressed.
/// </summary>
public CompressionLevel CompressionLevel { get; set; } = CompressionLevel.Ultra;
/// <summary>
/// The method with which the archive should be compressed by.
/// </summary>
public CompressionMethod CompressionMethod { get; set; } = CompressionMethod.Lzma2;
/// <summary>
/// Automatically creates a delta package when building a new release.
/// </summary>
public bool AutomaticDelta { get; set; }
/// <summary>
/// Path to the changelog file.
/// </summary>
public string? ChangelogPath { get; set; }
/// <summary>
/// Path to the readme for the file.
/// </summary>
public string? ReadmePath { get; set; }
/// <summary/>
public PublishModDialogViewModel(PathTuple<ModConfig> modTuple)
{
_modTuple = modTuple;
PackageName = IOEx.ForceValidFilePath(_modTuple.Config.ModName.Replace(' ', '_'));
OutputFolder = Path.Combine(Path.GetTempPath(), $"{IOEx.ForceValidFilePath(_modTuple.Config.ModId)}.Publish");
IgnoreRegexes = new ObservableCollection<StringWrapper>(
_modTuple.Config.IgnoreRegexes.Select(x => new StringWrapper { Value = x })
);
foreach (StringWrapper item in IgnoreRegexes)
item.PropertyChanged += (_, __) => UpdateConfig(_modTuple.Config.IgnoreRegexes, IgnoreRegexes);
IgnoreRegexes.CollectionChanged += (s, e) =>
{
if (e.NewItems != null)
foreach (StringWrapper item in e.NewItems)
item.PropertyChanged += (_, __) => UpdateConfig(_modTuple.Config.IgnoreRegexes, IgnoreRegexes);
if (e.OldItems != null)
foreach (StringWrapper item in e.OldItems)
item.PropertyChanged -= (_, __) => UpdateConfig(_modTuple.Config.IgnoreRegexes, IgnoreRegexes);
_modTuple.Config.IgnoreRegexes.Clear();
_modTuple.Config.IgnoreRegexes.AddRange(IgnoreRegexes.Select(x => x.Value));
_modTuple.SaveAsync();
};
IncludeRegexes = new ObservableCollection<StringWrapper>(
_modTuple.Config.IncludeRegexes.Select(x => new StringWrapper { Value = x })
);
foreach (StringWrapper item in IncludeRegexes)
item.PropertyChanged += (_, __) => UpdateConfig(_modTuple.Config.IncludeRegexes, IncludeRegexes);
IncludeRegexes.CollectionChanged += (s, e) =>
{
if (e.NewItems != null)
foreach (StringWrapper item in e.NewItems)
item.PropertyChanged += (_, __) => UpdateConfig(_modTuple.Config.IncludeRegexes, IncludeRegexes);
if (e.OldItems != null)
foreach (StringWrapper item in e.OldItems)
item.PropertyChanged -= (_, __) => UpdateConfig(_modTuple.Config.IncludeRegexes, IncludeRegexes);
_modTuple.Config.IncludeRegexes.Clear();
_modTuple.Config.IncludeRegexes.AddRange(IncludeRegexes.Select(x => x.Value));
_modTuple.SaveAsync();
};
}
void UpdateConfig(List<string> list, ObservableCollection<StringWrapper> collection)
{
list.Clear();
list.AddRange(collection.Select(x => x.Value));
_modTuple.SaveAsync();
}
/// <summary>
/// Builds a new release.
/// </summary>
/// <param name="cancellationToken">Used to cancel the current build.</param>
/// <returns>True if a build has started and the operation completed, else false.</returns>
public async Task<bool> BuildAsync(CancellationToken cancellationToken = default)
{
// Check if Auto Delta can be performed.
if (AutomaticDelta && !Singleton<ReleaseMetadata>.Instance.CanReadFromDirectory(OutputFolder, null, out _, out _))
{
var description = string.Format(Resources.PublishAutoDeltaWarningDescriptionFormat.Get(), Singleton<ReleaseMetadata>.Instance.GetDefaultFileName());
Actions.DisplayMessagebox(Resources.PublishAutoDeltaWarningTitle.Get(), description);
return false;
}
// Perform Build
CanBuild = false;
try
{
await Task.Run(async () =>
{
await PublishAsync(new PublishArgs()
{
PublishTarget = PublishTarget,
OutputFolder = OutputFolder,
ModTuple = _modTuple,
IgnoreRegexes = _modTuple.Config.IgnoreRegexes,
IncludeRegexes = _modTuple.Config.IncludeRegexes,
Progress = new Progress<double>(d => BuildProgress = d * 100),
AutomaticDelta = AutomaticDelta,
CompressionLevel = CompressionLevel,
CompressionMethod = CompressionMethod,
OlderVersionFolders = OlderVersionFolders.Select(x => x.Value).ToList(),
PackageName = PackageName,
MetadataFileName = _modTuple.Config.ReleaseMetadataFileName,
ChangelogPath = ChangelogPath,
ReadmePath = ReadmePath
});
});
ProcessExtensions.OpenFileWithExplorer(OutputFolder);
}
catch (Exception ex)
{
Errors.HandleException(ex);
return false;
}
finally
{
CanBuild = true;
}
return true;
}
/// <summary>
/// Adds a folder for another version of the mod.
/// </summary>
public void AddNewVersionFolder()
{
var configFilePath = FileSelectors.SelectModConfigFile();
if (string.IsNullOrEmpty(configFilePath))
return;
// Try parse.
ModConfig config;
try { config = IConfig<ModConfig>.FromPath(configFilePath); }
catch (Exception)
{
Actions.DisplayMessagebox(Resources.ErrorInvalidSemanticVersionTitle.Get(), Resources.ErrorInvalidSemanticVersionDescription.Get());
return;
}
// Check ID
if (config.ModId != _modTuple.Config.ModId)
{
Actions.DisplayMessagebox(Resources.ErrorPublishModIdMismatch.Get(), Resources.ErrorPublishModIdDescription.Get());
return;
}
// Check Version
if (!NuGetVersion.TryParse(config.ModVersion, out var version))
{
Actions.DisplayMessagebox(Resources.ErrorInvalidSemanticVersionTitle.Get(), Resources.ErrorInvalidSemanticVersionDescription.Get());
return;
}
// Check Version Value
if (new NuGetVersion(_modTuple.Config.ModVersion) <= new NuGetVersion(config.ModVersion))
{
Actions.DisplayMessagebox(Resources.ErrorNewerModConfigVersionTitle.Get(), Resources.ErrorNewerModConfigVersionDescription.Get());
return;
}
OlderVersionFolders.Add(Path.GetDirectoryName(configFilePath)!);
}
/// <summary>
/// Removes the folder for last known version.
/// </summary>
public void RemoveSelectedVersionFolder() => RemoveSelectedOrLastItem(SelectedOlderVersionFolder, OlderVersionFolders);
/// <summary>
/// Removes the selected ignore regex.
/// </summary>
public void RemoveSelectedIgnoreRegex() => RemoveSelectedOrLastItem(SelectedIgnoreRegex, IgnoreRegexes);
/// <summary>
/// Removes the selected include regex.
/// </summary>
public void RemoveSelectedIncludeRegex() => RemoveSelectedOrLastItem(SelectedIncludeRegex, IncludeRegexes);
/// <summary>
/// Adds a regular expression for ignoring files.
/// </summary>
public void AddIgnoreRegex() => IgnoreRegexes.Add("New Ignore Regex");
/// <summary>
/// Adds a regular expression for including files.
/// </summary>
public void AddIncludeRegex() => IncludeRegexes.Add("New Include Regex");
/// <summary>
/// Calculates all files that will be removed from the final archive and displays them to the user.
/// </summary>
public void ShowExcludedFiles()
{
var compiledIgnoreRegexes = IgnoreRegexes.Select(x => new Regex(x, RegexOptions.Compiled));
var compiledIncludeRegexes = IncludeRegexes.Select(x => new Regex(x, RegexOptions.Compiled));
var allFiles = Directory.GetFiles(GetModFolder(), "*.*", SearchOption.AllDirectories).Select(x => Paths.GetRelativePath(x, GetModFolder()));
var ignoredFiles = allFiles.Where(path => path.TryMatchAnyRegex(compiledIgnoreRegexes) && !path.TryMatchAnyRegex(compiledIncludeRegexes)).ToHashSet();
Actions.DisplayMessagebox.Invoke(Resources.PublishModRegexDialogTitle.Get(), string.Join('\n', ignoredFiles));
}
/// <summary>
/// Sets the output folder for the build operation.
/// </summary>
/// <exception cref="NotImplementedException"></exception>
public void SetOutputFolder()
{
var dialog = new VistaFolderBrowserDialog();
dialog.Multiselect = false;
dialog.SelectedPath = OutputFolder;
if ((bool)dialog.ShowDialog()!)
OutputFolder = dialog.SelectedPath;
}
/// <summary>
/// Directs user to the page showing them how to publish mods.
/// </summary>
public void ShowPublishTutorial() => ProcessExtensions.OpenFileWithDefaultProgram("https://reloaded-project.github.io/Reloaded-II/CreatingRelease");
/// <summary>
/// Lets the user select a new changelog path.
/// </summary>
public void SetChangelogPath() => ChangelogPath = FileSelectors.SelectMarkdownFile();
/// <summary>
/// Lets the user select a new readme path.
/// </summary>
public void SetReadmePath() => ReadmePath = FileSelectors.SelectMarkdownFile();
private string GetModFolder() => Path.GetDirectoryName(_modTuple.Path)!;
private void RemoveSelectedOrLastItem(StringWrapper? item, ObservableCollection<StringWrapper> allItems)
{
if (item != null)
allItems.Remove(item);
else if (allItems.Count > 0)
allItems.RemoveAt(allItems.Count - 1);
}
private void ChangeUiVisbilityOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(PublishTarget))
ShowLastVersionUiItems = PublishTarget != PublishTarget.NuGet;
}
}