|
| 1 | +#if UNITY_EDITOR |
| 2 | +using System; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using UnityEditor; |
| 7 | +using UnityEditor.Build; |
| 8 | +using UnityEditor.Build.Reporting; |
| 9 | +using Debug = UnityEngine.Debug; |
| 10 | + |
| 11 | +namespace BitMono.Unity.Editor |
| 12 | +{ |
| 13 | + // Post-build half of #276: after an IL2CPP Windows player is built, encrypt its global-metadata.dat in |
| 14 | + // place so static dumpers (Il2CppDumper, Cpp2IL) can't parse it. The native decryptor shipped as a source |
| 15 | + // plugin (Plugins/BitMono/global_metadata_decrypt.cpp) is compiled into GameAssembly.dll and restores the |
| 16 | + // metadata in memory at startup. Opt-in via BitMonoConfig.EncryptIl2CppMetadata. |
| 17 | + public class BitMonoMetadataProtection : IPostprocessBuildWithReport |
| 18 | + { |
| 19 | + // "BMIL2CPP" little-endian - the marker GlobalMetadataEncryptor writes; lets us stay idempotent. |
| 20 | + private const ulong EncryptedSanity = 0x505043324C494D42UL; |
| 21 | + |
| 22 | + public int callbackOrder => 1000; // run last, once the player (and its metadata) is fully written |
| 23 | + |
| 24 | + public void OnPostprocessBuild(BuildReport report) |
| 25 | + { |
| 26 | + var config = LoadConfig(); |
| 27 | + if (config == null || !config.EncryptIl2CppMetadata) |
| 28 | + { |
| 29 | + return; |
| 30 | + } |
| 31 | + // Don't gate on result == Succeeded: OnPostprocessBuild runs before the report is finalized, so the |
| 32 | + // result is usually BuildResult.Unknown here. Only bail on an explicit failure/cancel. |
| 33 | + if (report.summary.result == BuildResult.Failed || report.summary.result == BuildResult.Cancelled) |
| 34 | + { |
| 35 | + return; |
| 36 | + } |
| 37 | + // The native decryptor is Windows x64 only (CreateFileW hook); only encrypt where it can decrypt. |
| 38 | + if (report.summary.platform != BuildTarget.StandaloneWindows64) |
| 39 | + { |
| 40 | + Debug.LogWarning("[BitMono] Encrypt IL2CPP Metadata is on, but it currently supports Windows x64 " + |
| 41 | + "only. Leaving global-metadata.dat unencrypted for this platform."); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + var metadata = FindMetadata(report.summary.outputPath); |
| 46 | + if (metadata == null) |
| 47 | + { |
| 48 | + // No metadata = not an IL2CPP build (Mono backend). Nothing to do. |
| 49 | + return; |
| 50 | + } |
| 51 | + if (IsEncrypted(metadata)) |
| 52 | + { |
| 53 | + return; // already done (idempotent for incremental builds) |
| 54 | + } |
| 55 | + |
| 56 | + var cli = FindCli(); |
| 57 | + if (cli == null) |
| 58 | + { |
| 59 | + Debug.LogError("[BitMono] Encrypt IL2CPP Metadata is on but BitMono.CLI.exe wasn't found. " + |
| 60 | + "global-metadata.dat is left UNENCRYPTED."); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + try |
| 65 | + { |
| 66 | + // --encrypt-metadata writes <path>.enc and self-verifies the round-trip. |
| 67 | + var psi = new ProcessStartInfo |
| 68 | + { |
| 69 | + FileName = cli, |
| 70 | + Arguments = $"--encrypt-metadata \"{metadata}\"", |
| 71 | + UseShellExecute = false, |
| 72 | + RedirectStandardOutput = true, |
| 73 | + RedirectStandardError = true, |
| 74 | + CreateNoWindow = true, |
| 75 | + }; |
| 76 | + using var process = Process.Start(psi); |
| 77 | + var stdout = process.StandardOutput.ReadToEnd(); |
| 78 | + var stderr = process.StandardError.ReadToEnd(); |
| 79 | + process.WaitForExit(); |
| 80 | + if (process.ExitCode != 0) |
| 81 | + { |
| 82 | + Debug.LogError($"[BitMono] --encrypt-metadata failed (exit {process.ExitCode}); " + |
| 83 | + $"global-metadata.dat left UNENCRYPTED.\n{stdout}\n{stderr}"); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + var enc = metadata + ".enc"; |
| 88 | + if (!File.Exists(enc)) |
| 89 | + { |
| 90 | + Debug.LogError("[BitMono] --encrypt-metadata reported success but produced no .enc file; " + |
| 91 | + "global-metadata.dat left UNENCRYPTED."); |
| 92 | + return; |
| 93 | + } |
| 94 | + File.Copy(enc, metadata, overwrite: true); |
| 95 | + File.Delete(enc); |
| 96 | + Debug.Log("[BitMono] Encrypted IL2CPP global-metadata.dat (#276): static dumpers are blocked; " + |
| 97 | + "GameAssembly.dll decrypts it at startup."); |
| 98 | + } |
| 99 | + catch (Exception ex) |
| 100 | + { |
| 101 | + Debug.LogError($"[BitMono] IL2CPP metadata encryption failed: {ex}. " + |
| 102 | + "global-metadata.dat left UNENCRYPTED."); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // outputPath is the .exe; the metadata sits at <Name>_Data/il2cpp_data/Metadata/global-metadata.dat. |
| 107 | + private static string FindMetadata(string outputPath) |
| 108 | + { |
| 109 | + var dir = Path.GetDirectoryName(outputPath); |
| 110 | + if (string.IsNullOrEmpty(dir)) |
| 111 | + { |
| 112 | + return null; |
| 113 | + } |
| 114 | + var name = Path.GetFileNameWithoutExtension(outputPath); |
| 115 | + var expected = Path.Combine(dir, name + "_Data", "il2cpp_data", "Metadata", "global-metadata.dat"); |
| 116 | + if (File.Exists(expected)) |
| 117 | + { |
| 118 | + return expected; |
| 119 | + } |
| 120 | + // Fallback for unusual output layouts. |
| 121 | + try |
| 122 | + { |
| 123 | + return Directory.GetFiles(dir, "global-metadata.dat", SearchOption.AllDirectories).FirstOrDefault(); |
| 124 | + } |
| 125 | + catch |
| 126 | + { |
| 127 | + return null; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private static bool IsEncrypted(string path) |
| 132 | + { |
| 133 | + try |
| 134 | + { |
| 135 | + using var fs = File.OpenRead(path); |
| 136 | + var head = new byte[8]; |
| 137 | + return fs.Read(head, 0, 8) == 8 && BitConverter.ToUInt64(head, 0) == EncryptedSanity; |
| 138 | + } |
| 139 | + catch |
| 140 | + { |
| 141 | + return false; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private static string FindCli() |
| 146 | + { |
| 147 | + var dataPath = UnityEngine.Application.dataPath; |
| 148 | + var paths = new[] |
| 149 | + { |
| 150 | + // BitMono.CLI~ is Unity-ignored (~) so its DLLs never ship into the player; the .exe still runs. |
| 151 | + Path.Combine(dataPath, "BitMono.Unity", "BitMono.CLI~", "BitMono.CLI.exe"), |
| 152 | + Path.Combine(dataPath, "BitMono.Unity", "BitMono.CLI", "BitMono.CLI.exe"), |
| 153 | + Path.Combine(dataPath, "..", "BitMono.CLI", "BitMono.CLI.exe"), |
| 154 | + Path.Combine(dataPath, "..", "..", "src", "BitMono.CLI", "bin", "Release", "net462", "BitMono.CLI.exe"), |
| 155 | + }; |
| 156 | + return paths.FirstOrDefault(File.Exists); |
| 157 | + } |
| 158 | + |
| 159 | + private static BitMonoConfig LoadConfig() |
| 160 | + { |
| 161 | + var guids = AssetDatabase.FindAssets("t:BitMonoConfig"); |
| 162 | + if (guids.Length == 0) |
| 163 | + { |
| 164 | + return null; |
| 165 | + } |
| 166 | + return AssetDatabase.LoadAssetAtPath<BitMonoConfig>(AssetDatabase.GUIDToAssetPath(guids[0])); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | +#endif |
0 commit comments