77 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
3448ef75c2 Log Unity stack traces on IL2CPP (#1424)
UnityLogCallback ignored its second parameter, so Unity exceptions and
errors logged only their message. Append the trace for Exception and Error
when Unity gives one. The Mono source already does this for Exception. Log
and Warning stay as they are, since in the game I tested Unity passed a
trace for most of those.

Fixes #660

Co-authored-by: mirfalltnixein <22786734+mirfalltnixein@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 19:33:09 +01:00
Arrowmaster 4e98359b51 Upgrade UnityDoorstop to version 4.6.0 (#1418) 2026-10-20 20:51:05 +02:00
Joshua TraverandCursor 5b766a3b7f Follow PE export jmp thunks before applying a native detour (#1387)
* Follow PE export jmp thunks before applying a native detour.

GetProcAddress of il2cpp_runtime_invoke can be a jmp rel32 stub. Dobby aborting on that stub is BepInEx#1188-class. Resolve E9 and FF 25 in INativeDetour.Create so Default/Dobby and Funchook all patch the prologue. No-op when the pointer is already a function body.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Use IntPtr.Size when following FF 25 thunks.

nint.Size does not exist on the net6.0 BepInEx IL2CPP target.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-01 07:03:47 +02:00
Drew MochakandManlyMarco 7cb1246fcc Survive stripped UnityEngine.Application members during chainloader init (#1391)
Two related failures on aggressively stripped Unity Mono builds:

1. InitializeLoggers reads Application.unityVersion, which the managed
   code stripper can remove. The version is only used for an
   informational log line, but a MissingMethodException there aborts the
   rest of chainloader initialisation. Read it defensively instead.

2. UnityChainloader.Initialize caught every exception, logged it as
   Fatal, and returned normally. StaticStart then called Execute() on a
   chainloader that was never initialised, so BepInEx logged
   "Chainloader startup complete" while every plugin silently never ran.
   Rethrow so a failed init cannot be mistaken for a successful one.

Verified on Beacon Pines (Unity 2021.3.45f1, Mono, x64), which strips
both Application.unityVersion and the log-callback members.

Co-authored-by: ManlyMarco <39247311+ManlyMarco@users.noreply.github.com>
2026-09-01 06:58:43 +02:00
Drew Mochak 6d82b2e5fc Don't kill the chainloader when Unity log callbacks are stripped (#1389)
UnityLogSource's static constructor falls back to
Application.RegisterLogCallback when Application.logMessageReceived is
absent. Unity's managed code stripper can remove both, in which case
GetMethod returns null and the unguarded Invoke throws a
NullReferenceException out of the static constructor.

That exception propagates through chainloader initialisation, so the
chainloader never starts and no plugins load. The failure is close to
undiagnosable: LogOutput.log ends at "Preloader finished" with no error,
and the exception only appears on stdout.

Unity log forwarding is optional, so warn and continue instead of taking
the whole loader down.

Seen on Beacon Pines (Unity 2021.3.45f1, Mono), whose
UnityEngine.Application has neither member.
2026-09-01 06:56:05 +02:00
ManlyMarco 6abdba47ee Revert Cpp2IL back to development.1452 because of regressions 2026-06-28 18:07:00 +02:00
ManlyMarco 0523d6f4e9 Downgrade Samboy063.Cpp2IL.Core to 2022.1.0-pre-release.20
Version 21 includes a reference to Disarm version 99 which results in builds to fail unless author's custom nuget feed is referenced.

Details: https://github.com/SamboyCoding/Disarm/issues/45
2026-06-24 15:26:40 +02:00
ManlyMarco 80dde081fd Bump Cpp2IL to 2022.1.0.21 and AssetRipper.Primitives to 3.2.0 (#1362) 2026-06-21 19:05:18 +02:00
ManlyMarco 0643c2bceb Bump Il2CppInterop to 1.5.3 (#1361) 2026-06-21 12:49:28 +02:00
ManlyMarco 4aab2920fc Make UnityInput and KeyboardShortcut available in IL2CPP (#998) 2026-06-21 12:22:08 +02:00
HetCreep a06688e85b Degrade instead of crashing when the IL2CPP interop assemblies are missing (#1338)
* Degrade instead of crashing when the interop assemblies are missing

OnInvokeMethod references interop types (UnityEngine.Application/LogType) in its body. The CLR JIT-compiles the whole method before running any of it, so when the interop assemblies cannot be resolved the method throws FileNotFoundException at JIT time -- before its own try is entered -- and the exception escapes into the native runtime-invoke detour and crashes the process with nothing logged.

Move the interop-referencing call into its own SetupUnityLogging method called inside the try. OnInvokeMethod then JITs without the interop assemblies present, the missing-interop failure is caught, and the loader degrades (logs and continues) so the game runs unmodded. Set unhook at the top of the try so the detour fires exactly once even on failure.

* Condense the interop-degrade comments and reword the fatal log

Per review: trim the JIT-bypass comments to one line each and change
the fatal message to "Unable to execute IL2CPP chainloader, no plugins
will be loaded".
2026-06-11 11:54:22 +02:00
HetCreep f8ba0a41af Give actionable error when base-libs download fails (#1332)
When the Unity base-libraries download fails (commonly a 404 for a
Unity version not yet hosted on unity.bepinex.dev), the uncaught
HttpRequestException surfaced only as a cryptic "Failed to generate
Il2Cpp interop assemblies" and the game would not launch -- even
though a local fallback (the unity-libs directory) already exists.

Catch the download failure and throw a clear message naming the Unity
version, the URL and the two workarounds: place a matching
base-libraries .zip in the unity-libs directory, or point the
[IL2CPP] UnityBaseLibrariesSource config at a valid URL.

Refs #1295, #1084, #986, #941, #515
2026-06-08 14:36:17 +02:00
tobey 3fab71a191 Improved support for IL2CPP metadata v23-106 (Unity 6+) (#1284)
chore(deps): update Cpp2IL and Il2CppInterop
2026-03-07 14:10:33 +01:00
Arrowmaster c0386134c6 Upgrade Doorstop to version 4.5.0 (#1260) 2026-02-24 17:05:40 +01:00
ManlyMarco dd0655fe9d Cache downloaded base libraries in the unity-libs directory (#1213) 2025-11-16 19:49:19 +01:00
ManlyMarco 88b4838fef Fix assembly preloading overriding preloader patches (#1212) 2025-11-14 12:23:18 +01:00
ManlyMarco af0cba71fb v6 - Bump Il2CppInterop to 1.5.0-ci.620 (#1106) 2025-06-21 02:59:18 +02:00
ManlyMarco 5fef3570f2 Bump Cpp2IL to pre-release.19 (#1103) 2025-05-09 19:09:55 +02:00
Howard Lam 28559fe708 Checks if target Unity binaries archive exists before attempting download in case game's network conenction is blocked (#1024) 2025-01-11 22:51:47 +01:00
itectori 35f6b1b541 include DeobfuscationMap.csv.gz in ComputeHash (#1020) 2024-12-02 19:48:56 +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
aldelaro5andManlyMarco d9b95a0ac7 Add input abstraction layer to support games with the Input class disabled
Originally pull request #444

Co-authored-by: ManlyMarco <39247311+manlymarco@users.noreply.github.com>
2024-09-17 18:50:35 +02:00
aldelaro5andManlyMarco 8597ce7ebf Add TomlTypeConverter for UnityEngine.Rect
Originally pull request #510

Co-authored-by: ManlyMarco <39247311+manlymarco@users.noreply.github.com>
2024-09-17 18:50:35 +02:00
aldelaro5andghorsington 6e846fc70a Preload Harmony and MonoMod
Originally commit a1bb117c86

Co-authored-by: ghorsington <neigh@coder.horse>
2024-09-17 18:50:35 +02:00
aldelaro5andghorsington a5b7dabc1d Add error when using *nix build with Windows executable
Originally commit 8c7dcf265c

Co-authored-by: ghorsington <neigh@coder.horse>
2024-09-17 18:50:35 +02:00
ManlyMarco f800cbcc43 Add GlobalMetadataPath setting (#978) 2024-09-08 16:09:44 +02:00
ManlyMarco cbd70eb2f0 Automatically load all interop assemblies before loading plugins (#777)
Assemblies are often loaded on the il2cpp side but not on the mono side, causing confusion when using reflection.
This will load all interop assemblies right before plugins are loaded, which should mostly avoid these issues at a low time cost of around 40ms.
2024-09-08 15:45:19 +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 be1beb5e13 Address code review round 5 2024-09-03 20:34:17 -04:00
ManlyMarco 6b38cee127 Fix failing to initialize if game's Data folder is just 'Data' (#975)
Any Unity game can have its _Data folder renamed to just Data and continue working just fine.
2024-09-02 13:26:49 +02:00
aldelaro5 d44b93ba93 Use the IL2CPPChainLoader instance for il2cpp to preserve compatibility 2024-09-01 22:56:30 -04:00
aldelaro5 e9337e9982 Revert the removal of the il2cpp chainloader instance 2024-09-01 17:35:53 -04:00
aldelaro5 0a4f1d6d73 Accomodate breaking changes in preparation for api refactors 2024-09-01 17:14:59 -04:00
thegu5 5a324246f1 Bump Cpp2IL (#967)
The currently used version doesn't have support for metadata version 29.2 (Unity 2023.2+) and 31 (2022.3.33+)
2024-09-01 19:09:38 +02: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 e2396d202b address code review round 3 2024-08-29 12:50:46 -04:00
aldelaro5 b729aee545 Address code review round 3 (first part) 2024-08-28 01:14:00 -04:00
aldelaro5 bfc539bd74 Address code review round 2 2024-08-25 12:03:33 -04:00
aldelaro5 b688bfa978 Add a Source property to PluginInfo
Also abstract an interface among the base plugins classes to make this easier
2024-08-23 23:06:54 -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
aldelaro5 851521cc12 Upgrade Doorstop to 4.3.0 (#901) 2024-06-11 18:00:31 +02:00
deca 3ba398f269 Upgrade AssetRipper.Primitives to avoid implying Json 8.0.1 (#873) 2024-05-21 15:29:34 +03:00
aldelaro5 36d130f00d Upgrade doorstop to version 4.2.0 (#865) 2024-05-16 22:42:10 +02:00
Kasuromi a23612f4fe Bump Cpp2IL & Il2CppInterop (#869) 2024-05-16 23:29:50 +03:00
Oleg Oshmyan 49015217f3 run_bepinex*.sh: support older Steam and paths that contain spaces 2024-02-09 19:52:51 +01:00
Oleg Oshmyan c82fc4e686 run_bepinex*.sh: use exec when relaunching self 2024-02-09 19:52:51 +01:00
ManlyMarco 910844a982 Fix manager GameObject not having a name (#735) 2023-12-25 00:31:59 +01:00
Kokaiinum f0c0adddff Reimplement UnhollowedAssembliesPath config option as IL2CPPInteropAssembliesPath (#734) 2023-12-23 11:18:55 +01:00