Skip to content

Commit

Permalink
Ignore patched files
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyw15 committed May 6, 2024
1 parent bfb7300 commit e24a74d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
19 changes: 15 additions & 4 deletions AssemblyPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -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) }))),
Expand All @@ -64,13 +65,23 @@ 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]);
}

// write
module.Write(TARGET_FILE);

return true;
}

public static void DeleteNgen(string cevioInstallPath)
Expand Down Expand Up @@ -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\"";
Expand Down
16 changes: 11 additions & 5 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit e24a74d

Please sign in to comment.