From d5e186f3e0926ce6e2d1dddfe06dd2aa3a448fcc Mon Sep 17 00:00:00 2001 From: Fusion86 Date: Mon, 29 Jan 2024 22:47:22 +0100 Subject: [PATCH] Add .mhwslot support and add backup feature --- src/MHWSaveTransfer/FodyWeavers.xml | 1 + src/MHWSaveTransfer/FodyWeavers.xsd | 116 ++++++++++++++++++ src/MHWSaveTransfer/Helpers/BackupHelper.cs | 103 ++++++++++++++++ src/MHWSaveTransfer/Helpers/SteamUtility.cs | 2 - src/MHWSaveTransfer/MHWSaveTransfer.csproj | 17 +-- src/MHWSaveTransfer/MainWindow.xaml | 6 +- .../PublishProfiles/FolderProfile.pubxml | 17 --- .../ViewModels/MainWindowViewModel.cs | 35 ++++-- .../ViewModels/SaveSlotViewModel.cs | 16 +++ 9 files changed, 277 insertions(+), 36 deletions(-) create mode 100644 src/MHWSaveTransfer/Helpers/BackupHelper.cs delete mode 100644 src/MHWSaveTransfer/Properties/PublishProfiles/FolderProfile.pubxml diff --git a/src/MHWSaveTransfer/FodyWeavers.xml b/src/MHWSaveTransfer/FodyWeavers.xml index d5abfed..379374d 100644 --- a/src/MHWSaveTransfer/FodyWeavers.xml +++ b/src/MHWSaveTransfer/FodyWeavers.xml @@ -1,3 +1,4 @@  + \ No newline at end of file diff --git a/src/MHWSaveTransfer/FodyWeavers.xsd b/src/MHWSaveTransfer/FodyWeavers.xsd index 69dbe48..73fc4e9 100644 --- a/src/MHWSaveTransfer/FodyWeavers.xsd +++ b/src/MHWSaveTransfer/FodyWeavers.xsd @@ -53,6 +53,122 @@ + + + + + + A list of assembly names to exclude from the default action of "embed all Copy Local references", delimited with line breaks + + + + + A list of assembly names to include from the default action of "embed all Copy Local references", delimited with line breaks. + + + + + A list of runtime assembly names to exclude from the default action of "embed all Copy Local references", delimited with line breaks + + + + + A list of runtime assembly names to include from the default action of "embed all Copy Local references", delimited with line breaks. + + + + + A list of unmanaged 32 bit assembly names to include, delimited with line breaks. + + + + + A list of unmanaged 64 bit assembly names to include, delimited with line breaks. + + + + + The order of preloaded assemblies, delimited with line breaks. + + + + + + This will copy embedded files to disk before loading them into memory. This is helpful for some scenarios that expected an assembly to be loaded from a physical file. + + + + + Controls if .pdbs for reference assemblies are also embedded. + + + + + Controls if runtime assemblies are also embedded. + + + + + Controls whether the runtime assemblies are embedded with their full path or only with their assembly name. + + + + + Embedded assemblies are compressed by default, and uncompressed when they are loaded. You can turn compression off with this option. + + + + + As part of Costura, embedded assemblies are no longer included as part of the build. This cleanup can be turned off. + + + + + Costura by default will load as part of the module initialization. This flag disables that behavior. Make sure you call CosturaUtility.Initialize() somewhere in your code. + + + + + Costura will by default use assemblies with a name like 'resources.dll' as a satellite resource and prepend the output path. This flag disables that behavior. + + + + + A list of assembly names to exclude from the default action of "embed all Copy Local references", delimited with | + + + + + A list of assembly names to include from the default action of "embed all Copy Local references", delimited with |. + + + + + A list of runtime assembly names to exclude from the default action of "embed all Copy Local references", delimited with | + + + + + A list of runtime assembly names to include from the default action of "embed all Copy Local references", delimited with |. + + + + + A list of unmanaged 32 bit assembly names to include, delimited with |. + + + + + A list of unmanaged 64 bit assembly names to include, delimited with |. + + + + + The order of preloaded assemblies, delimited with |. + + + + diff --git a/src/MHWSaveTransfer/Helpers/BackupHelper.cs b/src/MHWSaveTransfer/Helpers/BackupHelper.cs new file mode 100644 index 0000000..6c541ba --- /dev/null +++ b/src/MHWSaveTransfer/Helpers/BackupHelper.cs @@ -0,0 +1,103 @@ +using Cirilla.Core.Models; +using Serilog; +using System; +using System.IO; +using System.Linq; + +namespace MHWSaveTransfer.Helpers +{ + // Mostly copy-pasted from https://github.com/Fusion86/MHWAppearanceEditor/blob/master/src/MHWAppearanceEditor/Services/BackupService.cs + internal class BackupHelper + { + private static readonly int MaxBackupCount = 10; + private static readonly ILogger log = Log.ForContext(); + + private readonly string backupDir; + + public BackupHelper() + { + backupDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MHWSaveTransfer", "backup"); + + try + { + if (!Directory.Exists(backupDir)) + { + Directory.CreateDirectory(backupDir); + + string readme = "MHWSaveTransfer uses this folder for its automated backup feature.\n"; + readme += "You can disable this feature by setting ' \"MaxBackupCount\": 0 ' in ../config.json\n\n"; + readme += "Do NOT manually add files to this folder, MHWSaveTransfer may delete them at any time to make more space!"; + File.WriteAllText(Path.Combine(backupDir, "README.txt"), readme); + } + } + catch (Exception ex) + { + log.Error(ex.Message); + } + } + + public void CreateBackup(SaveData saveData) + { + if (MaxBackupCount == 0) return; + + string fileName = "SAVEDATA_" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); + string backupPath = Path.Combine(backupDir, fileName); + string infoPath = Path.Combine(backupDir, fileName + ".txt"); + + // Write SAVEDATA + File.Copy(saveData.Filepath, backupPath); + + // Write info + string info = ""; + for (int i = 0; i < saveData.SaveSlots.Length; i++) + { + info += $"SaveSlot {i + 1}:\n"; + + var timespan = TimeSpan.FromSeconds(saveData.SaveSlots[i].PlayTime); + string timeStr = (int)timespan.TotalHours + ":" + timespan.ToString(@"mm\:ss"); + + // TODO: This used the **edited** huntername, rank, etc but the actual backup contains the **unedited** values. + info += $"TimePlayed: {timeStr}\n"; + info += $"HunterName: {saveData.SaveSlots[i].HunterName}\n"; + info += $"HunterRank: {saveData.SaveSlots[i].HunterRank}\n"; + info += $"MasterRank: {saveData.SaveSlots[i].MasterRank}\n"; + info += $"HunterXp: {saveData.SaveSlots[i].HunterXp}\n"; + info += $"MasterXp: {saveData.SaveSlots[i].MasterXp}\n"; + info += $"PalicoName: {saveData.SaveSlots[i].PalicoName}\n\n"; + } + + File.WriteAllText(infoPath, info); + + log.Information($"Created a backup at '{backupPath}'"); + + RemoveExcessiveBackups(); + } + + public void RemoveExcessiveBackups() + { + var files = Directory.GetFiles(backupDir, "*") + .Where(x => !x.EndsWith(".txt")) + .OrderByDescending(x => x) + .Skip(MaxBackupCount) + .ToList(); + + if (files.Count > 0) + { + log.Information($"Deleting {files.Count} backup(s) because they exceed the MaxBackupCount"); + + foreach (var file in files) + { + try + { + File.Delete(file); + File.Delete(file + ".txt"); + } + catch (Exception ex) + { + log.Error("Couldn't delete backup: " + ex.Message); + } + } + } + } + } +} diff --git a/src/MHWSaveTransfer/Helpers/SteamUtility.cs b/src/MHWSaveTransfer/Helpers/SteamUtility.cs index b163fdb..f0017a1 100644 --- a/src/MHWSaveTransfer/Helpers/SteamUtility.cs +++ b/src/MHWSaveTransfer/Helpers/SteamUtility.cs @@ -7,8 +7,6 @@ using System.IO; using System.Linq; using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; namespace MHWSaveTransfer.Helpers { diff --git a/src/MHWSaveTransfer/MHWSaveTransfer.csproj b/src/MHWSaveTransfer/MHWSaveTransfer.csproj index c780038..69fc02b 100644 --- a/src/MHWSaveTransfer/MHWSaveTransfer.csproj +++ b/src/MHWSaveTransfer/MHWSaveTransfer.csproj @@ -2,9 +2,9 @@ WinExe - net5.0-windows - true - 0.3.0 + net48 + true + 0.4.0 enable 8.0 icon.ico @@ -12,17 +12,20 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - - + - + all - + all diff --git a/src/MHWSaveTransfer/MainWindow.xaml b/src/MHWSaveTransfer/MainWindow.xaml index aefa923..13c3027 100644 --- a/src/MHWSaveTransfer/MainWindow.xaml +++ b/src/MHWSaveTransfer/MainWindow.xaml @@ -70,8 +70,8 @@ - + + @@ -112,7 +112,7 @@ -