|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using Mono.Cecil; |
| 4 | + |
| 5 | +namespace BepInEx.Preloader.Core |
| 6 | +{ |
| 7 | + public class AssemblyBuildInfo |
| 8 | + { |
| 9 | + public Version NetFrameworkVersion { get; private set; } |
| 10 | + |
| 11 | + public bool IsAnyCpu { get; set; } |
| 12 | + |
| 13 | + public bool Is64Bit { get; set; } |
| 14 | + |
| 15 | + private static Version GetNet4Version(AssemblyDefinition assemblyDefinition) |
| 16 | + { |
| 17 | + var targetFrameworkAttribute = assemblyDefinition.CustomAttributes.FirstOrDefault(x => |
| 18 | + x.AttributeType.FullName == "System.Runtime.Versioning.TargetFrameworkAttribute"); |
| 19 | + |
| 20 | + if (targetFrameworkAttribute == null) |
| 21 | + return null; |
| 22 | + |
| 23 | + if (targetFrameworkAttribute.ConstructorArguments.Count < 1) |
| 24 | + return null; |
| 25 | + |
| 26 | + if (targetFrameworkAttribute.ConstructorArguments[0].Type.Name != "String") |
| 27 | + return null; |
| 28 | + |
| 29 | + string versionInfo = (string)targetFrameworkAttribute.ConstructorArguments[0].Value; |
| 30 | + |
| 31 | + var values = versionInfo.Split(','); |
| 32 | + |
| 33 | + foreach (var value in values) |
| 34 | + { |
| 35 | + if (!value.StartsWith("Version=v")) |
| 36 | + continue; |
| 37 | + |
| 38 | + try |
| 39 | + { |
| 40 | + return new Version(value.Substring("Version=v".Length)); |
| 41 | + } |
| 42 | + catch {} |
| 43 | + } |
| 44 | + |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + public static AssemblyBuildInfo DetermineInfo(AssemblyDefinition assemblyDefinition) |
| 49 | + { |
| 50 | + var buildInfo = new AssemblyBuildInfo(); |
| 51 | + |
| 52 | + // framework version |
| 53 | + |
| 54 | + var runtime = assemblyDefinition.MainModule.Runtime; |
| 55 | + |
| 56 | + if (runtime == TargetRuntime.Net_1_0) |
| 57 | + { |
| 58 | + buildInfo.NetFrameworkVersion = new Version(1, 0); |
| 59 | + } |
| 60 | + else if (runtime == TargetRuntime.Net_1_1) |
| 61 | + { |
| 62 | + buildInfo.NetFrameworkVersion = new Version(1, 1); |
| 63 | + } |
| 64 | + else if (runtime == TargetRuntime.Net_2_0) |
| 65 | + { |
| 66 | + // Assume 3.5 here. The code to determine versions between 2.0 and 3.5 is not worth the amount of non-unity games that use it, if any |
| 67 | + |
| 68 | + buildInfo.NetFrameworkVersion = new Version(3, 5); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + buildInfo.NetFrameworkVersion = GetNet4Version(assemblyDefinition) ?? new Version(4, 0); |
| 73 | + } |
| 74 | + |
| 75 | + // bitness |
| 76 | + |
| 77 | + /* |
| 78 | + AnyCPU 64-bit preferred |
| 79 | + MainModule.Architecture: I386 |
| 80 | + MainModule.Attributes: ILOnly |
| 81 | +
|
| 82 | + AnyCPU 32-bit preferred |
| 83 | + MainModule.Architecture: I386 |
| 84 | + MainModule.Attributes: ILOnly, Required32Bit, Preferred32Bit |
| 85 | +
|
| 86 | + x86 |
| 87 | + MainModule.Architecture: I386 |
| 88 | + MainModule.Attributes: ILOnly, Required32Bit |
| 89 | +
|
| 90 | + x64 |
| 91 | + MainModule.Architecture: AMD64 |
| 92 | + MainModule.Attributes: ILOnly |
| 93 | + */ |
| 94 | + |
| 95 | + var architecture = assemblyDefinition.MainModule.Architecture; |
| 96 | + var attributes = assemblyDefinition.MainModule.Attributes; |
| 97 | + |
| 98 | + if (architecture == TargetArchitecture.AMD64) |
| 99 | + { |
| 100 | + buildInfo.Is64Bit = true; |
| 101 | + buildInfo.IsAnyCpu = false; |
| 102 | + } |
| 103 | + else if (architecture == TargetArchitecture.I386 && HasFlag(attributes, ModuleAttributes.Preferred32Bit | ModuleAttributes.Required32Bit)) |
| 104 | + { |
| 105 | + buildInfo.Is64Bit = false; |
| 106 | + buildInfo.IsAnyCpu = true; |
| 107 | + } |
| 108 | + else if (architecture == TargetArchitecture.I386 && HasFlag(attributes, ModuleAttributes.Required32Bit)) |
| 109 | + { |
| 110 | + buildInfo.Is64Bit = false; |
| 111 | + buildInfo.IsAnyCpu = false; |
| 112 | + } |
| 113 | + else if (architecture == TargetArchitecture.I386) |
| 114 | + { |
| 115 | + buildInfo.Is64Bit = true; |
| 116 | + buildInfo.IsAnyCpu = true; |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + throw new Exception("Unable to determine assembly architecture"); |
| 121 | + } |
| 122 | + |
| 123 | + return buildInfo; |
| 124 | + } |
| 125 | + |
| 126 | + private static bool HasFlag(ModuleAttributes value, ModuleAttributes flag) |
| 127 | + { |
| 128 | + return (value & flag) == flag; |
| 129 | + } |
| 130 | + |
| 131 | + /// <inheritdoc /> |
| 132 | + public override string ToString() |
| 133 | + { |
| 134 | + if (IsAnyCpu) |
| 135 | + { |
| 136 | + return $".NET Framework {NetFrameworkVersion}, AnyCPU ({(Is64Bit ? "64" : "32")}-bit preferred)"; |
| 137 | + } |
| 138 | + |
| 139 | + return $".NET Framework {NetFrameworkVersion}, {(Is64Bit ? "x64" : "x86")}"; |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments