50 lines
1.9 KiB
C#
50 lines
1.9 KiB
C#
|
|
using System.IO;
|
||
|
|
using System.Linq;
|
||
|
|
using HarmonyLib;
|
||
|
|
using HybridCLR.Editor;
|
||
|
|
using HybridCLR.Editor.Template;
|
||
|
|
using Obfuz.Settings;
|
||
|
|
using UnityEditor;
|
||
|
|
|
||
|
|
public static class OpCodeShuffleCommand
|
||
|
|
{
|
||
|
|
private static string TemplatePath => "Assets/Editor/KedrExtension/Templates";
|
||
|
|
|
||
|
|
[InitializeOnLoadMethod]
|
||
|
|
private static void Init()
|
||
|
|
{
|
||
|
|
var harmony = new Harmony("kedr.polymorphic.il");
|
||
|
|
harmony.PatchAll();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void GenerateOpCodes()
|
||
|
|
{
|
||
|
|
var opcodeMapping = Enumerable.Range(0, 248).Select(x => (byte)x).ToArray();
|
||
|
|
|
||
|
|
if (ObfuzSettings.Instance.polymorphicDllSettings.enable && KedrExtensionSettings.Instance.enableOpcodeShuffle)
|
||
|
|
{
|
||
|
|
var seed = HashUtil.Hash(ObfuzSettings.Instance.polymorphicDllSettings.codeGenerationSecretKey);
|
||
|
|
var rng = new System.Random((int)seed);
|
||
|
|
RandomUtil.Shuffle(opcodeMapping, rng);
|
||
|
|
}
|
||
|
|
|
||
|
|
var reverseOpcodeMapping = new byte[opcodeMapping.Length];
|
||
|
|
for (byte i = 0; i < opcodeMapping.Length; i++) reverseOpcodeMapping[opcodeMapping[i]] = i;
|
||
|
|
|
||
|
|
{
|
||
|
|
var frr = new FileRegionReplace(File.ReadAllText($"{TemplatePath}/Opcodes.h.tpl"));
|
||
|
|
var lines = Opcode.List.Select((op, i) => $"{op.Name} = {opcodeMapping[i]},");
|
||
|
|
frr.Replace("OPCODE_VALUE", string.Join('\n', lines));
|
||
|
|
var opcodesPath = $"{SettingsUtil.LocalIl2CppDir}/libil2cpp/hybridclr/metadata/Opcodes.h";
|
||
|
|
frr.Commit(opcodesPath);
|
||
|
|
}
|
||
|
|
|
||
|
|
{
|
||
|
|
var frr = new FileRegionReplace(File.ReadAllText($"{TemplatePath}/Opcodes.cpp.tpl"));
|
||
|
|
var lines = reverseOpcodeMapping.Select(i => Opcode.List[i].Info);
|
||
|
|
frr.Replace("OPCODE_INFO", string.Join('\n', lines));
|
||
|
|
var opcodesPath = $"{SettingsUtil.LocalIl2CppDir}/libil2cpp/hybridclr/metadata/Opcodes.cpp";
|
||
|
|
frr.Commit(opcodesPath);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|