From fa81605c96edfdd7f94cbd0c1306510418daec5b Mon Sep 17 00:00:00 2001 From: aevitas Date: Wed, 29 Apr 2020 23:19:43 +0200 Subject: [PATCH] Add initial options file implementation --- Patcher/Patcher.csproj | 6 ++++++ Patcher/PatcherOptions.cs | 15 +++++++++++++++ Patcher/Program.cs | 25 +++++++++++++++---------- Patcher/options.json | 7 +++++++ 4 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 Patcher/PatcherOptions.cs create mode 100644 Patcher/options.json diff --git a/Patcher/Patcher.csproj b/Patcher/Patcher.csproj index 4f25ece..2b8bed9 100644 --- a/Patcher/Patcher.csproj +++ b/Patcher/Patcher.csproj @@ -10,4 +10,10 @@ + + + PreserveNewest + + + diff --git a/Patcher/PatcherOptions.cs b/Patcher/PatcherOptions.cs new file mode 100644 index 0000000..85896be --- /dev/null +++ b/Patcher/PatcherOptions.cs @@ -0,0 +1,15 @@ +namespace Patcher +{ + public class PatcherOptions + { + public string ThemeName { get; set; } + + public string FileLocation { get; set; } + + public string Version { get; set; } + + public OperatingSystem? OperatingSystem { get; set; } + + public bool? Force { get; set; } + } +} diff --git a/Patcher/Program.cs b/Patcher/Program.cs index d38e007..eb4ebf9 100644 --- a/Patcher/Program.cs +++ b/Patcher/Program.cs @@ -4,6 +4,7 @@ using System.Linq; using NDesk.Options; using System.Runtime.InteropServices; +using System.Text.Json; namespace Patcher { @@ -11,12 +12,17 @@ internal static class Program { internal static void Main(string[] args) { - var themeName = string.Empty; + var options = new PatcherOptions(); + var optionsFile = new FileInfo("options.json"); + if (optionsFile.Exists) + options = JsonSerializer.Deserialize(File.ReadAllText(optionsFile.FullName)); + + var themeName = options.ThemeName ?? string.Empty; var help = false; - var fileLocation = string.Empty; - var os = OperatingSystem.Unknown; - var version = string.Empty; - var force = false; + var fileLocation = options.FileLocation ?? string.Empty; + var os = options.OperatingSystem ?? OperatingSystem.Unknown; + var version = options.Version ?? string.Empty; + var force = options.Force ?? false; var optionSet = new OptionSet { @@ -186,10 +192,9 @@ private static void CreateBackup(FileSystemInfo fileInfo, MemoryStream ms) backupFileInfo.Delete(); using var backupWriteStream = backupFileInfo.OpenWrite(); - - backupWriteStream.Write(ms.ToArray(), 0, (int)ms.Length); - + backupWriteStream.Write(ms.ToArray(), 0, (int)ms.Length); + if (backupFileInfo.Exists) Console.WriteLine($"Backup '{backupFileInfo.Name}' created."); } @@ -230,13 +235,13 @@ private static void PatchExecutable(MemoryStream ms, FileStream fs, PatchInfo pa Console.WriteLine($"Patching to {themeName}..."); foreach (var offset in offsets) - + for (var i = 0; i < themeBytes.Length; i++) { fs.Position = offset + i; fs.WriteByte(themeBytes[i]); } - + Console.WriteLine("Unity was successfully patched. Enjoy!"); } diff --git a/Patcher/options.json b/Patcher/options.json new file mode 100644 index 0000000..e5b4248 --- /dev/null +++ b/Patcher/options.json @@ -0,0 +1,7 @@ +{ + "ThemeName": "dark", + "FileLocation": "", + "Version": "", + "OperatingSystem": null, + "Force": false +} \ No newline at end of file