Repository navigation
Expand file tree
/
Copy pathBackupManagerViewModel.cs
More file actions
128 lines (112 loc) · 4.35 KB
/
Copy pathBackupManagerViewModel.cs
File metadata and controls
128 lines (112 loc) · 4.35 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
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using CUE4Parse.FileProvider.Objects;
using FModel.Framework;
using FModel.Services;
using FModel.Settings;
using FModel.ViewModels.ApiEndpoints.Models;
using FModel.Views.Resources.Controls;
using K4os.Compression.LZ4;
using K4os.Compression.LZ4.Streams;
using Serilog;
namespace FModel.ViewModels;
public class BackupManagerViewModel : ViewModel
{
public const uint FBKP_MAGIC = 0x504B4246;
private ThreadWorkerViewModel _threadWorkerView => ApplicationService.ThreadWorkerView;
private ApiEndpointViewModel _apiEndpointView => ApplicationService.ApiEndpointView;
private ApplicationViewModel _applicationView => ApplicationService.ApplicationView;
private readonly string _gameName;
private Backup _selectedBackup;
public Backup SelectedBackup
{
get => _selectedBackup;
set => SetProperty(ref _selectedBackup, value);
}
public ObservableCollection<Backup> Backups { get; }
public ICollectionView BackupsView { get; }
public BackupManagerViewModel(string gameName)
{
_gameName = gameName;
Backups = new ObservableCollection<Backup>();
BackupsView = new ListCollectionView(Backups) { SortDescriptions = { new SortDescription("FileName", ListSortDirection.Ascending) } };
}
public async Task Initialize()
{
await _threadWorkerView.Begin(cancellationToken =>
{
var backups = _apiEndpointView.DillyApi.GetBackups(cancellationToken);
if (backups == null) return;
Application.Current.Dispatcher.Invoke(() =>
{
foreach (var backup in backups) Backups.Add(backup);
SelectedBackup = Backups.FirstOrDefault();
});
});
}
public async Task CreateBackup()
{
await _threadWorkerView.Begin(_ =>
{
var backupFolder = Path.Combine(UserSettings.Default.OutputDirectory, "Backups");
var fileName = $"{_gameName}_{DateTime.Now:MM'_'dd'_'yyyy}.fbkp";
var fullPath = Path.Combine(backupFolder, fileName);
var func = new Func<GameFile, bool>(x => !x.IsUePackagePayload);
using var fileStream = new FileStream(fullPath, FileMode.Create);
using var compressedStream = LZ4Stream.Encode(fileStream, LZ4Level.L00_FAST);
using var writer = new BinaryWriter(compressedStream);
writer.Write(FBKP_MAGIC);
writer.Write((byte) EBackupVersion.Latest);
writer.Write(_applicationView.CUE4Parse.Provider.Files.Values.Count(func));
foreach (var asset in _applicationView.CUE4Parse.Provider.Files.Values)
{
if (!func(asset)) continue;
writer.Write(asset.Size);
writer.Write(asset.IsEncrypted);
writer.Write(asset.Path);
}
SaveCheck(fullPath, fileName, "created", "create");
});
}
public async Task Download()
{
if (SelectedBackup == null) return;
await _threadWorkerView.Begin(_ =>
{
var fullPath = Path.Combine(Path.Combine(UserSettings.Default.OutputDirectory, "Backups"), SelectedBackup.FileName);
_apiEndpointView.DownloadFile(SelectedBackup.Url, fullPath);
SaveCheck(fullPath, SelectedBackup.FileName, "downloaded", "download");
});
}
private void SaveCheck(string fullPath, string fileName, string type1, string type2)
{
if (new FileInfo(fullPath).Length > 0)
{
Log.Information("{FileName} successfully {Type}", fileName, type1);
FLogger.Append(ELog.Information, () =>
{
FLogger.Text($"Successfully {type1} ", Constants.WHITE);
FLogger.Link(fileName, fullPath, true);
});
}
else
{
Log.Error("{FileName} could not be {Type}", fileName, type1);
FLogger.Append(ELog.Error, () => FLogger.Text($"Could not {type2} '{fileName}'", Constants.WHITE, true));
}
}
}
public enum EBackupVersion : byte
{
BeforeVersionWasAdded = 0,
Initial,
PerfectPath, // no more leading slash and ToLower
LatestPlusOne,
Latest = LatestPlusOne - 1
}