From 634a38347b8bde1d3f95dc8bd45704ef46748b0b Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Sun, 23 Sep 2018 18:09:57 -0500 Subject: [PATCH] Moved Assembly-CSharp and UnityEngine backup to the loader, updates the backup on game update --- IPA.Injector/Backups/BackupManager.cs | 27 +++++++ IPA.Injector/Backups/BackupUnit.cs | 104 ++++++++++++++++++++++++++ IPA.Injector/IPA.Injector.csproj | 2 + IPA.Injector/Injector.cs | 10 ++- IPA/Program.cs | 4 +- 5 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 IPA.Injector/Backups/BackupManager.cs create mode 100644 IPA.Injector/Backups/BackupUnit.cs diff --git a/IPA.Injector/Backups/BackupManager.cs b/IPA.Injector/Backups/BackupManager.cs new file mode 100644 index 00000000..368a50a4 --- /dev/null +++ b/IPA.Injector/Backups/BackupManager.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace IPA.Injector.Backups +{ + public class BackupManager + { + public static BackupUnit FindLatestBackup(string dir) + { + new DirectoryInfo(dir).Create(); + return new DirectoryInfo(dir) + .GetDirectories() + .OrderByDescending(p => p.Name) + .Select(p => BackupUnit.FromDirectory(p, dir)) + .FirstOrDefault(); + } + + public static bool HasBackup(string dir) + { + return FindLatestBackup(dir) != null; + } + } +} diff --git a/IPA.Injector/Backups/BackupUnit.cs b/IPA.Injector/Backups/BackupUnit.cs new file mode 100644 index 00000000..ad7c6e5f --- /dev/null +++ b/IPA.Injector/Backups/BackupUnit.cs @@ -0,0 +1,104 @@ +using IPA.Logging; +using IPA.Utilities; +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.IO; +using System.Linq; +using System.Text; + +namespace IPA.Injector.Backups +{ + /// + /// A unit for backup. WIP. + /// + public class BackupUnit + { + public string Name { get; private set; } + + private DirectoryInfo _BackupPath; + private HashSet _Files = new HashSet(); + private FileInfo _ManifestFile; + private static string _ManifestFileName = "$manifest$.txt"; + + public BackupUnit(string dir) : this(dir, DateTime.Now.ToString("yyyy-MM-dd_h-mm-ss")) + { + } + + private BackupUnit(string dir, string name) + { + Name = name; + _BackupPath = new DirectoryInfo(Path.Combine(dir, Name)); + _ManifestFile = new FileInfo(Path.Combine(_BackupPath.FullName, _ManifestFileName)); + } + + public static BackupUnit FromDirectory(DirectoryInfo directory, string dir) + { + var unit = new BackupUnit(dir, directory.Name); + + // Read Manifest + if (unit._ManifestFile.Exists) + { + string manifest = File.ReadAllText(unit._ManifestFile.FullName); + foreach (var line in manifest.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)) + unit._Files.Add(line); + } + else + { + foreach (var file in directory.GetFiles("*", SearchOption.AllDirectories)) + { + if (file.Name == _ManifestFileName) continue; + var relativePath = file.FullName.Substring(directory.FullName.Length + 1); + unit._Files.Add(relativePath); + } + } + + return unit; + } + + public void Add(string file) + { + Add(new FileInfo(file)); + } + + internal void Delete() + { + _BackupPath.Delete(true); + } + + /// + /// Adds a file to the list of changed files and backups it. + /// + /// + public void Add(FileInfo file) + { + var relativePath = LoneFunctions.GetRelativePath(file.FullName, Environment.CurrentDirectory); + var backupPath = new FileInfo(Path.Combine(_BackupPath.FullName, relativePath)); + + // Copy over + backupPath.Directory.Create(); + if (file.Exists) + { + file.CopyTo(backupPath.FullName); + } + else + { + // Make empty file + backupPath.Create().Close(); + } + + if (!_Files.Contains(relativePath)) + { + if (!File.Exists(_ManifestFile.FullName)) + _ManifestFile.Create().Close(); + var stream = _ManifestFile.AppendText(); + stream.WriteLine(relativePath); + stream.Close(); + + // Add to list + _Files.Add(relativePath); + } + } + + } +} diff --git a/IPA.Injector/IPA.Injector.csproj b/IPA.Injector/IPA.Injector.csproj index 4cdf91ab..8b93e097 100644 --- a/IPA.Injector/IPA.Injector.csproj +++ b/IPA.Injector/IPA.Injector.csproj @@ -52,6 +52,8 @@ + + diff --git a/IPA.Injector/Injector.cs b/IPA.Injector/Injector.cs index 4c0d7fe0..1b3e2a73 100644 --- a/IPA.Injector/Injector.cs +++ b/IPA.Injector/Injector.cs @@ -1,4 +1,5 @@ using Harmony; +using IPA.Injector.Backups; using IPA.Loader; using IPA.Logging; using Mono.Cecil; @@ -50,6 +51,12 @@ namespace IPA.Injector { var cAsmName = Assembly.GetExecutingAssembly().GetName(); + loader.Debug("Finding backup"); + var backupPath = Path.Combine(Environment.CurrentDirectory, "IPA","Backups","Beat Saber"); + var bkp = BackupManager.FindLatestBackup(backupPath); + if (bkp == null) + loader.Warn("No backup found! Was BSIPA installed using the installer?"); + loader.Debug("Ensuring patch on UnityEngine.CoreModule exists"); #region Insert patch into UnityEngine.CoreModule.dll var unityPath = Path.Combine(Environment.CurrentDirectory, "Beat Saber_Data", "Managed", "UnityEngine.CoreModule.dll"); @@ -110,6 +117,7 @@ namespace IPA.Injector if (modified) { + bkp?.Add(unityPath); unityAsmDef.Write(unityPath); } #endregion @@ -119,7 +127,7 @@ namespace IPA.Injector var ascPath = Path.Combine(Environment.CurrentDirectory, "Beat Saber_Data", "Managed", "Assembly-CSharp.dll"); var ascModule = VirtualizedModule.Load(ascPath); - ascModule.Virtualize(cAsmName); + ascModule.Virtualize(cAsmName, () => bkp?.Add(ascPath)); #endregion } diff --git a/IPA/Program.cs b/IPA/Program.cs index e6edc75b..69f4d81e 100644 --- a/IPA/Program.cs +++ b/IPA/Program.cs @@ -255,8 +255,8 @@ namespace IPA backup, null, false); - backup.Add(context.AssemblyFile); - backup.Add(context.EngineFile); + //backup.Add(context.AssemblyFile); + //backup.Add(context.EngineFile); } #region Create Plugin Folder