Add assembly framework and architecture checks for xna launcher

This commit is contained in:
Bepis committed 2022-01-23 01:56:18 +11:00
1 parent e21677f440
commit 3f172dbc95
4 files changed
+173 -3

No files matched your search

+142
View File
@@ -0,0 +1,142 @@
using System;
using System.Linq;
using Mono.Cecil;
namespace BepInEx.Preloader.Core
{
public class AssemblyBuildInfo
{
public Version NetFrameworkVersion { get; private set; }
public bool IsAnyCpu { get; set; }
public bool Is64Bit { get; set; }
private static Version GetNet4Version(AssemblyDefinition assemblyDefinition)
{
var targetFrameworkAttribute = assemblyDefinition.CustomAttributes.FirstOrDefault(x =>
x.AttributeType.FullName == "System.Runtime.Versioning.TargetFrameworkAttribute");
if (targetFrameworkAttribute == null)
return null;
if (targetFrameworkAttribute.ConstructorArguments.Count < 1)
return null;
if (targetFrameworkAttribute.ConstructorArguments[0].Type.Name != "String")
return null;
string versionInfo = (string)targetFrameworkAttribute.ConstructorArguments[0].Value;
var values = versionInfo.Split(',');
foreach (var value in values)
{
if (!value.StartsWith("Version=v"))
continue;
try
{
return new Version(value.Substring("Version=v".Length));
}
catch {}
}
return null;
}
public static AssemblyBuildInfo DetermineInfo(AssemblyDefinition assemblyDefinition)
{
var buildInfo = new AssemblyBuildInfo();
// framework version
var runtime = assemblyDefinition.MainModule.Runtime;
if (runtime == TargetRuntime.Net_1_0)
{
buildInfo.NetFrameworkVersion = new Version(1, 0);
}
else if (runtime == TargetRuntime.Net_1_1)
{
buildInfo.NetFrameworkVersion = new Version(1, 1);
}
else if (runtime == TargetRuntime.Net_2_0)
{
// 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
buildInfo.NetFrameworkVersion = new Version(3, 5);
}
else
{
buildInfo.NetFrameworkVersion = GetNet4Version(assemblyDefinition) ?? new Version(4, 0);
}
// bitness
/*
AnyCPU 64-bit preferred
MainModule.Architecture: I386
MainModule.Attributes: ILOnly
AnyCPU 32-bit preferred
MainModule.Architecture: I386
MainModule.Attributes: ILOnly, Required32Bit, Preferred32Bit
x86
MainModule.Architecture: I386
MainModule.Attributes: ILOnly, Required32Bit
x64
MainModule.Architecture: AMD64
MainModule.Attributes: ILOnly
*/
var architecture = assemblyDefinition.MainModule.Architecture;
var attributes = assemblyDefinition.MainModule.Attributes;
if (architecture == TargetArchitecture.AMD64)
{
buildInfo.Is64Bit = true;
buildInfo.IsAnyCpu = false;
}
else if (architecture == TargetArchitecture.I386 && HasFlag(attributes, ModuleAttributes.Preferred32Bit | ModuleAttributes.Required32Bit))
{
buildInfo.Is64Bit = false;
buildInfo.IsAnyCpu = true;
}
else if (architecture == TargetArchitecture.I386 && HasFlag(attributes, ModuleAttributes.Required32Bit))
{
buildInfo.Is64Bit = false;
buildInfo.IsAnyCpu = false;
}
else if (architecture == TargetArchitecture.I386)
{
buildInfo.Is64Bit = true;
buildInfo.IsAnyCpu = true;
}
else
{
throw new Exception("Unable to determine assembly architecture");
}
return buildInfo;
}
private static bool HasFlag(ModuleAttributes value, ModuleAttributes flag)
{
return (value & flag) == flag;
}
/// <inheritdoc />
public override string ToString()
{
if (IsAnyCpu)
{
return $".NET Framework {NetFrameworkVersion}, AnyCPU ({(Is64Bit ? "64" : "32")}-bit preferred)";
}
return $".NET Framework {NetFrameworkVersion}, {(Is64Bit ? "x64" : "x86")}";
}
}
}
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using BepInEx.Logging;
using BepInEx.Shared;
using MonoMod.Utils;
@@ -17,7 +18,7 @@ public static class ChainloaderLogHelper
//See BuildInfoAttribute for more information about this section.
var attributes = typeof(BuildInfoAttribute).Assembly.GetCustomAttributes(typeof(BuildInfoAttribute), false);
if (attributes.Length > 0)
{
var attribute = (BuildInfoAttribute) attributes[0];
@@ -25,6 +26,7 @@ public static class ChainloaderLogHelper
}
Logger.Log(LogLevel.Info, $"System platform: {PlatformHelper.Current}");
Logger.Log(LogLevel.Info, $"Process bitness: {(PlatformUtils.ProcessIs64Bit ? "64-bit (x64)" : "32-bit (x86)")}");
}
public static void RewritePreloaderLogs()
+3 -1
View File
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
@@ -16,6 +16,8 @@ internal static class PlatformUtils
CharSet = CharSet.Ansi)]
private static extern IntPtr uname_osx(ref utsname_osx utsname);
public static readonly bool ProcessIs64Bit = IntPtr.Size >= 8;
/// <summary>
/// Recreation of MonoMod's PlatformHelper.DeterminePlatform method, but with libc calls instead of creating processes.
/// </summary>