From 4c1ca52082e0e44b76bb2dd268621cd2c8452700 Mon Sep 17 00:00:00 2001 From: artman41 Date: Thu, 17 May 2018 00:57:01 +0100 Subject: [PATCH] refactor to use regions instead of comments --- IPA/Program.cs | 224 +++++++++++++++++++++++-------------------------- 1 file changed, 105 insertions(+), 119 deletions(-) diff --git a/IPA/Program.cs b/IPA/Program.cs index 6a4da9d8..51242724 100644 --- a/IPA/Program.cs +++ b/IPA/Program.cs @@ -10,11 +10,8 @@ using System.Text; using System.Text.RegularExpressions; using System.Windows.Forms; -namespace IPA -{ - - public class Program - { +namespace IPA { + public class Program { public enum Architecture { x86, x64, @@ -25,57 +22,53 @@ namespace IPA static void Main(string[] args) { PatchContext context; - - if(args.Length < 1 || !args[0].EndsWith(".exe")) - { + + if (args.Length < 1 || !args[0].EndsWith(".exe")) { //Fail("Drag an (executable) file on the exe!"); - context = PatchContext.Create(new [] {new DirectoryInfo(Directory.GetCurrentDirectory()).GetFiles().First(o => o.FullName.EndsWith(".exe")) - .FullName}); + context = PatchContext.Create(new[] { + new DirectoryInfo(Directory.GetCurrentDirectory()).GetFiles() + .First(o => o.FullName.EndsWith(".exe")) + .FullName + }); } else { context = PatchContext.Create(args); } - try - { - + try { bool isRevert = args.Contains("--revert") || Keyboard.IsKeyDown(Keys.LMenu); // Sanitizing Validate(context); - if (isRevert) - { + if (isRevert) { Revert(context); } - else - { + else { Install(context); StartIfNeedBe(context); } - } catch(Exception e) - { + } + catch (Exception e) { Fail(e.Message); } } - private static void Validate(PatchContext c) - { - if (!File.Exists(c.LauncherPathSrc)) Fail("Couldn't find DLLs! Make sure you extracted all contents of the release archive."); - if (!Directory.Exists(c.DataPathDst) || !File.Exists(c.EngineFile)) - { + private static void Validate(PatchContext c) { + if (!File.Exists(c.LauncherPathSrc)) + Fail("Couldn't find DLLs! Make sure you extracted all contents of the release archive."); + if (!Directory.Exists(c.DataPathDst) || !File.Exists(c.EngineFile)) { Fail("Game does not seem to be a Unity project. Could not find the libraries to patch."); Console.WriteLine($"DataPath: {c.DataPathDst}"); Console.WriteLine($"EngineFile: {c.EngineFile}"); } } - private static void Install(PatchContext context) - { - try - { + private static void Install(PatchContext context) { + try { var backup = new BackupUnit(context); - - //new version check + + #region Patch Version Check + var patchedModule = PatchedModule.Load(context.EngineFile); var isCurrentNewer = Version.CompareTo(patchedModule.Data.Version) > 0; if (isCurrentNewer) { @@ -87,7 +80,8 @@ namespace IPA Console.ResetColor(); - // Copying + #region File Copying + Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine("Updating files... "); var nativePluginFolder = Path.Combine(context.DataPathDst, "Plugins"); @@ -105,6 +99,8 @@ namespace IPA architecture)); Console.WriteLine("Successfully updated files!"); + + #endregion } else { Console.ForegroundColor = ConsoleColor.Red; @@ -112,16 +108,22 @@ namespace IPA Console.ResetColor(); } - if (!Directory.Exists(context.PluginsFolder)) - { + #endregion + + #region Create Plugin Folder + + if (!Directory.Exists(context.PluginsFolder)) { + Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine("Creating plugins folder... "); Directory.CreateDirectory(context.PluginsFolder); + Console.ResetColor(); } - Console.ResetColor(); - // Patching - if (!patchedModule.Data.IsPatched || isCurrentNewer) - { + #endregion + + #region Patching + + if (!patchedModule.Data.IsPatched || isCurrentNewer) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"Patching UnityEngine.dll with Version {Application.ProductVersion}... "); backup.Add(context.EngineFile); @@ -130,9 +132,11 @@ namespace IPA Console.ResetColor(); } - // Virtualizing - if (File.Exists(context.AssemblyFile)) - { + #endregion + + #region Virtualizing + + if (File.Exists(context.AssemblyFile)) { var virtualizedModule = VirtualizedModule.Load(context.AssemblyFile); if (!virtualizedModule.IsVirtualized) { Console.ForegroundColor = ConsoleColor.Blue; @@ -144,7 +148,9 @@ namespace IPA } } - // Creating shortcut + #endregion + + #region Creating shortcut /*if(!File.Exists(context.ShortcutPath)) { Console.Write("Creating shortcut to IPA ({0})... ", context.IPA); @@ -165,9 +171,9 @@ namespace IPA Console.Error.WriteLine("Failed to create shortcut, but game was patched!"); } }*/ + #endregion } - catch (Exception e) - { + catch (Exception e) { Fail("Oops! This should not have happened.\n\n" + e); } @@ -181,19 +187,17 @@ namespace IPA private static void Revert(PatchContext context, string[] args = null) { Console.ForegroundColor = ConsoleColor.Cyan; bool isNewVersion = (args != null && args.Contains("newVersion")); - + Console.Write("Restoring backup... "); - if(BackupManager.Restore(context)) - { + if (BackupManager.Restore(context)) { Console.WriteLine("Done!"); - } else - { + } + else { Console.WriteLine("Already vanilla!"); } - - if (File.Exists(context.ShortcutPath)) - { + + if (File.Exists(context.ShortcutPath)) { Console.WriteLine("Deleting shortcut..."); File.Delete(context.ShortcutPath); } @@ -201,86 +205,80 @@ namespace IPA Console.WriteLine(""); Console.WriteLine("--- Done reverting ---"); - if (!Environment.CommandLine.Contains("--nowait") && !isNewVersion) - { + if (!Environment.CommandLine.Contains("--nowait") && !isNewVersion) { Console.WriteLine("\n\n[Press any key to quit]"); Console.ReadKey(); } + Console.ResetColor(); } - private static void StartIfNeedBe(PatchContext context) - { + private static void StartIfNeedBe(PatchContext context) { var argList = context.Args.ToList(); bool launch = argList.Remove("--launch"); argList.RemoveAt(0); - if(launch) - { + if (launch) { Process.Start(context.Executable, Args(argList.ToArray())); } } - public static IEnumerable NativePluginInterceptor(FileInfo from, FileInfo to, DirectoryInfo nativePluginFolder, bool isFlat, Architecture preferredArchitecture) - { - if (to.FullName.StartsWith(nativePluginFolder.FullName)) - { + public static IEnumerable NativePluginInterceptor(FileInfo from, FileInfo to, + DirectoryInfo nativePluginFolder, bool isFlat, Architecture preferredArchitecture) { + if (to.FullName.StartsWith(nativePluginFolder.FullName)) { var relevantBit = to.FullName.Substring(nativePluginFolder.FullName.Length + 1); // Goes into the plugin folder! bool isFileFlat = !relevantBit.StartsWith("x86"); - if (isFlat && !isFileFlat) - { + if (isFlat && !isFileFlat) { // Flatten structure bool is64Bit = relevantBit.StartsWith("x86_64"); - if (!is64Bit && preferredArchitecture == Architecture.x86) - { + if (!is64Bit && preferredArchitecture == Architecture.x86) { // 32 bit - yield return new FileInfo(Path.Combine(nativePluginFolder.FullName, relevantBit.Substring("x86".Length + 1))); + yield return new FileInfo(Path.Combine(nativePluginFolder.FullName, + relevantBit.Substring("x86".Length + 1))); } - else if(is64Bit && (preferredArchitecture == Architecture.x64 || preferredArchitecture == Architecture.Unknown)) - { + else if (is64Bit && (preferredArchitecture == Architecture.x64 || + preferredArchitecture == Architecture.Unknown)) { // 64 bit - yield return new FileInfo(Path.Combine(nativePluginFolder.FullName, relevantBit.Substring("x86_64".Length + 1))); - } else { + yield return new FileInfo(Path.Combine(nativePluginFolder.FullName, + relevantBit.Substring("x86_64".Length + 1))); + } + else { // Throw away yield break; } } - else if (!isFlat && isFileFlat) - { + else if (!isFlat && isFileFlat) { // Deepen structure - yield return new FileInfo(Path.Combine(Path.Combine(nativePluginFolder.FullName, "x86"), relevantBit)); - yield return new FileInfo(Path.Combine(Path.Combine(nativePluginFolder.FullName, "x86_64"), relevantBit)); + yield return new FileInfo(Path.Combine(Path.Combine(nativePluginFolder.FullName, "x86"), + relevantBit)); + yield return new FileInfo(Path.Combine(Path.Combine(nativePluginFolder.FullName, "x86_64"), + relevantBit)); } - else - { + else { yield return to; } } - else - { + else { yield return to; } } - private static IEnumerable PassThroughInterceptor(FileInfo from, FileInfo to) - { + + private static IEnumerable PassThroughInterceptor(FileInfo from, FileInfo to) { yield return to; } - public static void CopyAll(DirectoryInfo source, DirectoryInfo target, bool aggressive, BackupUnit backup, Func> interceptor = null) - { - if(interceptor == null) - { + public static void CopyAll(DirectoryInfo source, DirectoryInfo target, bool aggressive, BackupUnit backup, + Func> interceptor = null) { + if (interceptor == null) { interceptor = PassThroughInterceptor; } // Copy each file into the new directory. - foreach (FileInfo fi in source.GetFiles()) - { - foreach(var targetFile in interceptor(fi, new FileInfo(Path.Combine(target.FullName, fi.Name)))) { - if (!targetFile.Exists || targetFile.LastWriteTimeUtc < fi.LastWriteTimeUtc || aggressive) - { + foreach (FileInfo fi in source.GetFiles()) { + foreach (var targetFile in interceptor(fi, new FileInfo(Path.Combine(target.FullName, fi.Name)))) { + if (!targetFile.Exists || targetFile.LastWriteTimeUtc < fi.LastWriteTimeUtc || aggressive) { targetFile.Directory.Create(); Console.WriteLine(@"Copying {0}", targetFile.FullName); @@ -291,27 +289,24 @@ namespace IPA } // Copy each subdirectory using recursion. - foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) - { + foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) { DirectoryInfo nextTargetSubDir = new DirectoryInfo(Path.Combine(target.FullName, diSourceSubDir.Name)); CopyAll(diSourceSubDir, nextTargetSubDir, aggressive, backup, interceptor); } } - static void Fail(string message) - { + static void Fail(string message) { Console.Error.Write("ERROR: " + message); - if (!Environment.CommandLine.Contains("--nowait")) - { + if (!Environment.CommandLine.Contains("--nowait")) { Console.WriteLine("\n\n[Press any key to quit]"); Console.ReadKey(); } + Environment.Exit(1); } - public static string Args(params string[] args) - { + public static string Args(params string[] args) { return string.Join(" ", args.Select(EncodeParameterArgument).ToArray()); } @@ -321,8 +316,7 @@ namespace IPA /// The value that should be received by the program /// The value which needs to be passed to the program for the original value /// to come through - public static string EncodeParameterArgument(string original) - { + public static string EncodeParameterArgument(string original) { if (string.IsNullOrEmpty(original)) return original; string value = Regex.Replace(original, @"(\\*)" + "\"", @"$1\$0"); @@ -330,13 +324,10 @@ namespace IPA return value; } - public static Architecture DetectArchitecture(string assembly) - { - using (var reader = new BinaryReader(File.OpenRead(assembly))) - { + public static Architecture DetectArchitecture(string assembly) { + using (var reader = new BinaryReader(File.OpenRead(assembly))) { var header = reader.ReadUInt16(); - if(header == 0x5a4d) - { + if (header == 0x5a4d) { reader.BaseStream.Seek(60, SeekOrigin.Begin); // this location contains the offset for the PE header var peOffset = reader.ReadUInt32(); @@ -351,19 +342,17 @@ namespace IPA return Architecture.x64; else return Architecture.Unknown; - } else - { + } + else { // Not a supported binary return Architecture.Unknown; } } } - public abstract class Keyboard - { + public abstract class Keyboard { [Flags] - private enum KeyStates - { + private enum KeyStates { None = 0, Down = 1, Toggled = 2 @@ -372,11 +361,10 @@ namespace IPA [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] private static extern short GetKeyState(int keyCode); - private static KeyStates GetKeyState(Keys key) - { + private static KeyStates GetKeyState(Keys key) { KeyStates state = KeyStates.None; - short retVal = GetKeyState((int)key); + short retVal = GetKeyState((int) key); //If the high-order bit is 1, the key is down //otherwise, it is up. @@ -390,15 +378,13 @@ namespace IPA return state; } - public static bool IsKeyDown(Keys key) - { + public static bool IsKeyDown(Keys key) { return KeyStates.Down == (GetKeyState(key) & KeyStates.Down); } - public static bool IsKeyToggled(Keys key) - { + public static bool IsKeyToggled(Keys key) { return KeyStates.Toggled == (GetKeyState(key) & KeyStates.Toggled); } } } -} +} \ No newline at end of file