From e24a74d6546365d066de79192578a9e0a7f3b3b8 Mon Sep 17 00:00:00 2001 From: wangyw15 Date: Tue, 7 May 2024 02:12:03 +0800 Subject: [PATCH] Ignore patched files --- AssemblyPatcher.cs | 19 +++++++++++++++---- Program.cs | 16 +++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/AssemblyPatcher.cs b/AssemblyPatcher.cs index 6f68038..5c328c2 100644 --- a/AssemblyPatcher.cs +++ b/AssemblyPatcher.cs @@ -12,6 +12,7 @@ namespace CeVIOActivator public static class AssemblyPatcher { private const string TARGET_FILE = "CeVIO.ToolBarControl.dll"; + private const string TARGET_CLASS = "CeVIO.Editor.MissionAssistant.Authorizer"; [Obsolete("Directly patch executable will make it not work. Use PatchFile instead.")] public static void PatchExecutable(string cevioExecutablePath) @@ -33,7 +34,7 @@ public static void PatchExecutable(string cevioExecutablePath) asm.Write("CeVIO AI.exe"); } - public static void PatchFile(string cevioInstallPath) + public static bool PatchFile(string cevioInstallPath) { // System.Void CeVIO.ToolBarControl.ToolBarControl::.cctor() // System.Reflection.Assembly.GetEntryAssembly().GetType("CeVIO.Editor.MissionAssistant.Authorizer").GetProperty("HasAuthorized").SetValue(null, true); @@ -49,12 +50,12 @@ public static void PatchFile(string cevioInstallPath) var type = module.GetType("CeVIO.ToolBarControl.ToolBarControl"); var method = type.Methods.First(m => m.Name == ".cctor"); - // patch + // generate instructions var processor = method.Body.GetILProcessor(); var instructions = new Instruction[] { processor.Create(OpCodes.Call, module.ImportReference(typeof(Assembly).GetMethod("GetEntryAssembly"))), - processor.Create(OpCodes.Ldstr, "CeVIO.Editor.MissionAssistant.Authorizer"), + processor.Create(OpCodes.Ldstr, TARGET_CLASS), processor.Create(OpCodes.Callvirt, module.ImportReference(typeof(Assembly).GetMethod("GetType", new Type[] { typeof(string) }))), processor.Create(OpCodes.Ldstr, "HasAuthorized"), processor.Create(OpCodes.Callvirt, module.ImportReference(typeof(Type).GetMethod("GetProperty", new Type[] { typeof(string) }))), @@ -64,6 +65,14 @@ public static void PatchFile(string cevioInstallPath) processor.Create(OpCodes.Callvirt, module.ImportReference(typeof(PropertyInfo).GetMethod("SetValue", new Type[] { typeof(object), typeof(object) }))) }; + // detect if patched + if (method.Body.Instructions.Any(x => x.Operand as string == TARGET_CLASS)) + { + Console.WriteLine("Already patched, skip"); + return false; + } + + // patch for (var i = instructions.Length - 1; i >= 0; i--) { processor.InsertBefore(method.Body.Instructions[0], instructions[i]); @@ -71,6 +80,8 @@ public static void PatchFile(string cevioInstallPath) // write module.Write(TARGET_FILE); + + return true; } public static void DeleteNgen(string cevioInstallPath) @@ -99,7 +110,7 @@ public static void ReplaceFile(string cevioInstallPath) var sourcePath = Path.GetFullPath(TARGET_FILE); var targetPath = Path.Combine(cevioInstallPath, TARGET_FILE); // backup unmodified file - File.Copy(sourcePath, sourcePath + ".bak", true); + File.Copy(targetPath, targetPath + ".bak", true); var process = new Process(); process.StartInfo.FileName = "cmd.exe"; process.StartInfo.Arguments = $"/c \"timeout 1 /nobreak & copy /y \"{targetPath}\" \"{targetPath}.bak\" & copy /y \"{sourcePath}\" \"{targetPath}\" & del \"{sourcePath}\" & echo Completed & pause\""; diff --git a/Program.cs b/Program.cs index 19555c4..8077923 100644 --- a/Program.cs +++ b/Program.cs @@ -27,15 +27,21 @@ static void Main(string[] args) var installFolder = GetCeVIOInstallFolder(); Console.WriteLine("Patching CeVIO.ToolBarControl.dll"); - AssemblyPatcher.PatchFile(installFolder); + var thisTimePatched = AssemblyPatcher.PatchFile(installFolder); - Console.WriteLine("Deleting Ngen"); - AssemblyPatcher.DeleteNgen(installFolder); + if (thisTimePatched) + { + Console.WriteLine("Deleting Ngen"); + AssemblyPatcher.DeleteNgen(installFolder); + } Console.WriteLine("You should reactivate CeVIO AI before " + DateTime.Now.AddYears(100).ToLongDateString()); - Console.WriteLine("Replace the file to enable offline export"); - AssemblyPatcher.ReplaceFile(installFolder); + if (thisTimePatched) + { + Console.WriteLine("Replace the file to enable offline export"); + AssemblyPatcher.ReplaceFile(installFolder); + } } private static string GetCeVIOInstallFolder()