Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions BepInEx.Preloader/BepInEx.Preloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="HarmonyX" Version="2.9.0"/>
<PackageReference Include="HarmonyX" Version="2.16.1"/>
<PackageReference Include="Mono.Cecil" Version="0.10.4"/>
<PackageReference Include="MonoMod.RuntimeDetour" Version="22.1.29.1"/>
<PackageReference Include="MonoMod.Utils" Version="22.1.29.1"/>
<PackageReference Include="MonoMod.RuntimeDetour" Version="25.3.4"/>
<PackageReference Include="MonoMod.Utils" Version="25.0.12"/>
</ItemGroup>

<Target Name="DeletePdbFromPublish" AfterTargets="Publish">
Expand All @@ -27,4 +27,4 @@
</ItemGroup>
<Delete Files="@(FilesToDelete)"/>
</Target>
</Project>
</Project>
8 changes: 3 additions & 5 deletions BepInEx.Preloader/Entrypoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ private static void LoadCriticalAssemblies()

public static void PreloaderPreMain()
{
PlatformUtils.SetPlatform();

string bepinPath = Utility.ParentDirectory(Path.GetFullPath(EnvVars.DOORSTOP_INVOKE_DLL_PATH), 2);

Paths.SetExecutablePath(EnvVars.DOORSTOP_PROCESS_PATH, bepinPath, EnvVars.DOORSTOP_MANAGED_FOLDER_DIR, EnvVars.DOORSTOP_DLL_SEARCH_DIRS);
Expand Down Expand Up @@ -92,8 +90,8 @@ private static Assembly LocalResolve(object sender, ResolveEventArgs args)
return foundAssembly;

if (Utility.TryResolveDllAssembly(assemblyName, Paths.BepInExAssemblyDirectory, out foundAssembly)
|| Utility.TryResolveDllAssembly(assemblyName, Paths.PatcherPluginPath, out foundAssembly)
|| Utility.TryResolveDllAssembly(assemblyName, Paths.PluginPath, out foundAssembly))
|| (Directory.Exists(Paths.PatcherPluginPath) && Utility.TryResolveDllAssembly(assemblyName, Paths.PatcherPluginPath, out foundAssembly))
|| (Directory.Exists(Paths.PluginPath) && Utility.TryResolveDllAssembly(assemblyName, Paths.PluginPath, out foundAssembly)))
return foundAssembly;

return null;
Expand Down Expand Up @@ -158,4 +156,4 @@ internal static Assembly ResolveCurrentDirectory(object sender, ResolveEventArgs
}
}
}
}
}
151 changes: 0 additions & 151 deletions BepInEx.Preloader/Platform.cs

This file was deleted.

6 changes: 3 additions & 3 deletions BepInEx.Preloader/Preloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static void Run()
Logger.LogInfo($"Running under Unity v{GetUnityVersion()}");
Logger.LogInfo($"CLR runtime version: {Environment.Version}");
Logger.LogInfo($"Supports SRE: {Utility.CLRSupportsDynamicAssemblies}");
Logger.LogInfo($"System platform: {PlatformHelper.Current}");
Logger.LogInfo($"System platform: {PlatformDetection.OS} {PlatformDetection.Architecture}");

if (runtimePatchException != null)
Logger.LogWarning($"Failed to apply runtime patches for Mono. See more info in the output log. Error message: {runtimePatchException.Message}");
Expand Down Expand Up @@ -250,7 +250,7 @@ public static void AllocateConsole()

public static string GetUnityVersion()
{
if (PlatformHelper.Is(Platform.Windows))
if (PlatformDetection.OS.Is(OSKind.Windows))
return FileVersionInfo.GetVersionInfo(Paths.ExecutablePath).FileVersion;

return $"Unknown ({(IsPostUnity2017 ? "post" : "pre")}-2017)";
Expand Down Expand Up @@ -318,4 +318,4 @@ private enum MonoModBackend

#endregion
}
}
}
15 changes: 2 additions & 13 deletions BepInEx.Preloader/RuntimeFixes/XTermFix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using System.Linq;
using System.Reflection.Emit;
using HarmonyLib;
using MonoMod.RuntimeDetour;
using MonoMod.RuntimeDetour.Platforms;
using MonoMod.Utils;

namespace BepInEx.Preloader.RuntimeFixes
Expand All @@ -13,7 +11,7 @@ internal static class XTermFix
{
public static void Apply()
{
if (PlatformHelper.Is(Platform.Windows))
if (PlatformDetection.OS.Is(OSKind.Windows))
return;

if (typeof(Console).Assembly.GetType("System.ConsoleDriver") == null)
Expand All @@ -28,13 +26,6 @@ public static void Apply()
return;
}

// Apparently on older Unity versions (4.x), using Process.Start can run Console..cctor
// And since MonoMod's PlatformHelper (used by DetourHelper.Native) runs Process.Start to determine ARM/x86 platform,
// this causes a crash owing to TermInfoReader running before it can be patched and fixed
// Because Doorstop does not support ARM at the moment, we can get away with just forcing x86 detour platform.
// TODO: Figure out a way to detect ARM on Unix without running Process.Start
DetourHelper.Native = new DetourNativeX86Platform();

var harmony = new HarmonyLib.Harmony("com.bepinex.xtermfix");

harmony.Patch(AccessTools.Method("System.TermInfoReader:ReadHeader"),
Expand All @@ -48,8 +39,6 @@ public static void Apply()

harmony.Patch(AccessTools.Method("System.TermInfoReader:GetStringBytes", new []{ AccessTools.TypeByName("System.TermInfoStrings") }),
transpiler: new HarmonyMethod(typeof(XTermFix), nameof(GetTermInfoStringsTranspiler)));

DetourHelper.Native = null;
}

public static int intOffset;
Expand Down Expand Up @@ -133,4 +122,4 @@ public static IEnumerable<CodeInstruction> GetTermInfoStringsTranspiler(IEnumera
return list;
}
}
}
}
8 changes: 4 additions & 4 deletions BepInEx/BepInEx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="HarmonyX" Version="2.9.0"/>
<PackageReference Include="HarmonyX" Version="2.16.1"/>
<PackageReference Include="Mono.Cecil" Version="0.10.4"/>
<PackageReference Include="MonoMod.RuntimeDetour" Version="22.1.29.1"/>
<PackageReference Include="MonoMod.Utils" Version="22.1.29.1"/>
<PackageReference Include="MonoMod.RuntimeDetour" Version="25.3.4"/>
<PackageReference Include="MonoMod.Utils" Version="25.0.12"/>
<PackageReference Include="UnityEngine" Version="5.6.1" IncludeAssets="compile"/>
</ItemGroup>

Expand All @@ -40,4 +40,4 @@
</ItemGroup>
<Delete Files="@(FilesToDelete)"/>
</Target>
</Project>
</Project>
2 changes: 1 addition & 1 deletion BepInEx/Bootstrap/Chainloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static void Initialize(string gameExePath, bool startConsole = true, ICol
consoleLogListener.WriteUnityLogs = false;
}

if (PlatformHelper.Is(Platform.Unix))
if (PlatformDetection.OS.Is(OSKind.Posix))
{
Logger.LogInfo($"Detected Unity version: v{UnityVersion}");
}
Expand Down
4 changes: 2 additions & 2 deletions BepInEx/Console/ConsoleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ static ConsoleManager()

public static void Initialize(bool alreadyActive)
{
if (PlatformHelper.Is(Platform.Unix))
if (PlatformDetection.OS.Is(OSKind.Posix))
Driver = new LinuxConsoleDriver();
else if (PlatformHelper.Is(Platform.Windows))
else if (PlatformDetection.OS.Is(OSKind.Windows))
Driver = new WindowsConsoleDriver();

Driver.Initialize(alreadyActive);
Expand Down
35 changes: 15 additions & 20 deletions BepInEx/Console/Unix/UnixStreamHelper.cs
Original file line number Diff line number Diff line change
@@ -1,60 +1,55 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using MonoMod.Utils;

namespace BepInEx.Unix
{
internal static class UnixStreamHelper
{
private static IntPtr libcHandle;

public delegate int dupDelegate(int fd);
[DynDllImport("libc")]
public static dupDelegate dup;

public delegate IntPtr fdopenDelegate(int fd, string mode);
[DynDllImport("libc")]
public static fdopenDelegate fdopen;

public delegate IntPtr freadDelegate(IntPtr ptr, IntPtr size, IntPtr nmemb, IntPtr stream);
[DynDllImport("libc")]
public static freadDelegate fread;

public delegate int fwriteDelegate(IntPtr ptr, IntPtr size, IntPtr nmemb, IntPtr stream);
[DynDllImport("libc")]
public static fwriteDelegate fwrite;

public delegate int fcloseDelegate(IntPtr stream);
[DynDllImport("libc")]
public static fcloseDelegate fclose;

public delegate int fflushDelegate(IntPtr stream);
[DynDllImport("libc")]
public static fflushDelegate fflush;

public delegate int isattyDelegate(int fd);
[DynDllImport("libc")]
public static isattyDelegate isatty;

static UnixStreamHelper()
{
var libcMapping = new Dictionary<string, List<DynDllMapping>>
{
["libc"] = new List<DynDllMapping>
{
"libc.so.6", // Ubuntu glibc
"libc", // Linux glibc
"/usr/lib/libSystem.dylib", // OSX POSIX
}
};

typeof(UnixStreamHelper).ResolveDynDllImports(libcMapping);
libcHandle = DynDll.OpenLibrary(PlatformDetection.OS.Is(OSKind.OSX) ? "/usr/lib/libSystem.dylib" : "libc");
dup = AsDelegate<dupDelegate>(libcHandle.GetExport("dup"));
fdopen = AsDelegate<fdopenDelegate>(libcHandle.GetExport("fdopen"));
fread = AsDelegate<freadDelegate>(libcHandle.GetExport("fread"));
fwrite = AsDelegate<fwriteDelegate>(libcHandle.GetExport("fwrite"));
fclose = AsDelegate<fcloseDelegate>(libcHandle.GetExport("fclose"));
fflush = AsDelegate<fflushDelegate>(libcHandle.GetExport("fflush"));
isatty = AsDelegate<isattyDelegate>(libcHandle.GetExport("isatty"));
}

private static T AsDelegate<T>(IntPtr symbol) where T : class
=> Marshal.GetDelegateForFunctionPointer(symbol, typeof(T)) as T;

public static Stream CreateDuplicateStream(int fileDescriptor)
{
int newFd = dup(fileDescriptor);

return new UnixStream(newFd, FileAccess.Write);
}
}
}
}
Loading