100 Commits
Author SHA1 Message Date
Chris Dobbyn dbc82f05b8 v6: Fix plugin loading on native arm64 macOS (#1405)
* fix(macos): load plugins when the game runs as native arm64

This is the v6 port of #1402, which fixed the same failure on v5-lts.

MonoMod picks DetourNativeMonoPosixPlatform on Mono, whose MakeWritable and
MakeExecutable call mprotect with PROT_READ|PROT_WRITE|PROT_EXEC. macOS on
Apple Silicon answers EACCES, because a process without the
com.apple.security.cs.allow-unsigned-executable-memory entitlement can never
hold a page that is writable and executable at once. Unity ships
com.apple.security.cs.allow-jit instead, so Mono's code lives in a MAP_JIT
region, which also rejects mach_vm_write with KERN_INVALID_ADDRESS.

The EACCES escapes DetourRuntimeILPlatform's constructor, which runs inside
DetourHelper.Runtime's initialiser after _RuntimeInit is already set, so every
later read of DetourHelper.Runtime returns null and every Harmony patch dies on
a NullReferenceException in DetourHelper.GetIdentifiable. On v6 the first
casualty is ConsoleSetOutFix.Apply, which is not wrapped in Utility.TryDo, so
the whole preloader dies before it opens LogOutput.log. The only trace is a
preloader_*.log beside the executable.

v6 reaches this more easily than v5 does: run_bepinex_mono.sh sets
ARCHPREFERENCE="arm64,x86_64" unconditionally on Apple Silicon, so a universal
game runs as native arm64 by default and no opt-in is needed to hit the bug.

AppleSiliconDetourFix installs a detour platform that keeps
DetourNativeARMPlatform's AArch64 encoding and routes every code write through
doorstop_jit_memcpy, which toggles pthread_jit_write_protect_np around its own
memcpy. The toggle cannot be done from managed code: it drops execute
permission for the calling thread, and the caller is itself running
JIT-compiled code.

XTermFix forced DetourNativeX86Platform on every non-Windows target. On Apple
Silicon that encoded x86 branches into arm64 method bodies and reinstated the
failing mprotect, so it is now skipped when the new fix applies. XTermFix also
cleared DetourHelper.Native back to null on the way out, which would discard
the platform installed here, so it now restores what it found.

doorstop_jit_memcpy landed in UnityDoorstop via NeighTools/UnityDoorstop#114
and is not in the pinned 4.5.0 release. Until DoorstopVersion is bumped, this
fix reports its absence as a warning through the preloader instead of failing
silently, which is what happens today.

Verified on an M4 Pro against Valheim 6000.0.75f1 (Unity Mono), with a probe
plugin that Harmony-patches an already-JIT-compiled method:

  arm64,   unpatched build: preloader dies, no LogOutput.log
  arm64,   this build:      plugin loads, Harmony patch applies
  x86_64,  this build:      plugin loads, Harmony patch applies

* Move AppleSiliconDetourFix to BepInEx.Preloader.Core

The IL2CPP preloader needs the same class, so keep one copy where both
preloaders already reference it rather than a copy per runtime.

* Cut the comments back to one line each
2026-11-05 19:50:19 +01:00
HetCreep 5f39645992 Allow multiple [TargetType] and [TargetAssembly] attributes (#1343)
Patch-method discovery already reads both with the plural
MetadataHelper.GetAttributes<T>(), and the docs say additional target
attributes can be specified, but the attributes lacked
AllowMultiple = true, so the compiler rejected a second one (CS0579).
Set AllowMultiple = true on both.
2026-06-11 14:52:43 +02:00
js6pak bd467c9af6 Expose a DisplayBepInExVersion (#556) 2026-06-11 12:25:33 +02:00
The0x539 0d275a4df3 Avoid accidentally (indirectly) reading PlatformHelper.Current before setting it (#1254) 2026-01-27 15:26:39 +01:00
aldelaro5 544f909a80 Revert "Merge pull request #960 from aldelaro5/metaplugins" (#995)
This reverts commit 2a61b61497, reversing
changes made to 6b38cee127.
2024-09-24 20:25:56 +02:00
aldelaro5andghorsington 92f7d25698 Print file write data in console
Originally commit 6c5766d5ab

Co-authored-by: ghorsington <neigh@coder.horse>
2024-09-17 18:50:35 +02:00
Bepis 2a61b61497 Merge pull request #960 from aldelaro5/metaplugins
Implement patcher and plugin providers
2024-09-08 23:19:59 +10:00
aldelaro5 24cf635e65 Ensure providers and plugins / patchers folders are created if they don't exist 2024-09-01 09:29:35 -04:00
aldelaro5 ba3fac94da Upgrade MonoMod to 22.7.31.1 to fix old mono preloader crashes (#973) 2024-08-31 11:07:51 +02:00
aldelaro5 40643ffdef Offload the load context caching to bepinex 2024-08-29 21:12:07 -04:00
aldelaro5 d8dd404e52 Address code review 2024-08-23 15:40:22 -04:00
aldelaro5 4d47c3cd39 Address code review 2024-08-20 15:13:10 -04:00
aldelaro5 952c74f6fe Implement Patcher and plugin providers 2024-08-19 19:57:52 -04:00
aldelaro5 53625800b8 Upgrade Harmonyx, Iced and AssetRipper.Primitives packages (#953)
* Upgrade Harmonyx to 2.10.2

* Upgrade Iced to 1.21.0

* Upgrade AssetRipper.Primitives to 3.1.3
2024-08-12 21:54:02 +02:00
js6pak 472e950179 Don't try to parse the wine version (#555) 2023-07-26 20:13:30 +02:00
ghorsington 5796f53564 Bump Il2CppInterop, HarmonyX 2022-11-13 20:02:15 +02:00
ghorsington 684959d840 Bump Cpp2IL and Il2CppInterop 2022-09-30 20:33:50 +03:00
ghorsington f7857c7994 Restructure project names and folder layout 2022-08-14 17:19:09 +03:00
ghorsington f27cfd7215 Fix MonoAssemblyHelper not working on Unity 5 and older
In older Unity (ones that embed UnityPlayer into the EXE), __Internal will map to the EXE which causes wrong DllImport resolving. Instead, use Doorstop's env var to resolve correct functions
2022-08-14 17:19:08 +03:00
ghorsington 828ef20996 UnityMono: Resolve assemblies into memory using mono API
This fixes several issues with loading assemblies. In turn, this commit also removes old Location/CodeBase fixes since now assemblies are resolved automatically correctly.

Additionally, integrate the mono loader into preloading which fixes assembly location resolving
2022-08-14 17:19:08 +03:00
ghorsington f400445ba1 Refactor Unity -> UnityMono
Use runtime names for BepInEx modules instead of just the game engine
2022-08-14 17:19:08 +03:00
ghorsington 46d9dd4f62 Fix Wine check not working on UnityMono version
DynDll relies on PlatformHelper in net35 version which can cause early loading
2022-08-14 17:19:08 +03:00
ghorsington f658a4fbdf Adjust hash info message 2022-08-14 17:19:08 +03:00
ghorsington d190b069a1 Restructure versioning and project configuration
To simplify versioning and configuration:

* Removed BepInEx.Shared
* Removed BuildInfoAttribute
* Moved shared configuration to Directory.Build.props
* Refactored to use VersionSuffix and SourceRevisionId for embedding BE and commit hash info
* Enabled debug info embedding
2022-08-14 17:19:06 +03:00
ghorsington 60147c73dc Add platform check for Wine, report Wine version 2022-08-14 17:18:59 +03:00
Bepis 0d7ba3a5cb Fix BepInEx 6 for Unity 4.x games and below 2022-08-04 16:20:16 +10:00
ghorsington 16a75479bb Bump package versions 2022-04-17 18:37:36 +03:00
ghorsington b7a05a61e1 Fix C# 10 CS8983 regression 2022-02-24 22:32:17 +02:00
ghorsington 1504d80c06 Bump HarmonyX and MonoMod 2022-02-03 19:06:36 +02:00
Bepis 5686f6a003 Create .NET Core platform project 2022-01-25 21:11:55 +11:00
Bepis a02c330295 Implement better platform identification and descriptions in preloader log 2022-01-24 18:05:22 +11:00
Bepis 3f172dbc95 Add assembly framework and architecture checks for xna launcher 2022-01-23 01:56:18 +11:00
ghorsington 59c5adc982 Bump libraries
* Updated HarmonyX
* Updated MonoMod
* Updated Cpp2IL
2022-01-11 17:27:04 +02:00
ghorsington b7b8dd2d84 Add HarmonyBackend config to Il2Cpp as well 2022-01-01 13:37:48 +02:00
ghorsington 3fd5d68e3b ManualLogSource: Add string interpolation for Log* overloads; clean up logging 2021-12-21 19:40:15 +02:00
ghorsington 4bb48c502e Reformat project; apply code style 2021-12-21 18:55:05 +02:00
ghorsington 81141fcfc4 Add LogInterpolatedStringHandler overload for default logger 2021-12-21 18:49:07 +02:00
ghorsington 325e0df14b Use C# 10, reformat project 2021-12-21 16:09:39 +02:00
ghorsington e3e944d042 Bump package versions 2021-12-21 10:41:53 +02:00
ghorsington 7562726305 Bump package versions 2021-12-07 22:16:30 +02:00
ghorsington ad0e751b4d Bump packages 2021-10-24 17:26:34 +03:00
ghorsington 7e904152b9 Fix random access denied on solution build 2021-10-03 18:15:57 +03:00
ghorsington e613bbec32 Include netstandard2.0 to BepInEx.Unity for NuGet package 2021-09-26 00:19:41 +03:00
ghorsington b219c162de Use process name for preloader assembly dump path; fixes #303 2021-09-25 19:43:43 +03:00
ghorsington 639dbb475c Bump NuGet packages 2021-09-25 19:22:12 +03:00
ghorsington 02952f5ab1 Use Microsoft.NETFramework.ReferenceAssemblies 2021-08-25 20:51:07 +03:00
ghorsington bc1572a17f Generate NuGet packages on release build for certain libs 2021-08-25 20:48:14 +03:00
Bepis 9cc0211a78 Revamp AssemblyPatcher and patcher plugins 2021-07-29 10:38:36 +10:00
js6pak 0461ab4f39 Make stacktraces Error instead of Debug 2021-07-27 16:33:31 +02:00
ghorsington 086d2f877d Update HarmonyX and MonoMod.RuntimeDetour 2021-04-25 21:08:16 +03:00