Unity - Managed Code Unstripping
Scenario:
You're messing around with a Unity game- modding, hacking, snooping around, etc… But no matter what you do, your code keeps generating errors. You're getting MissingMethodException
's or TargetInvocationException
's, and for some reason mscorlib.dll
really doesn' like you.
Fear not! It could just be a stripped library.
Managed code stripping
During the build process, Unity removes unused or unreachable code through a process called managed code stripping, which can significantly decrease your application's final size. Managed code stripping removes code from managed assemblies, including assemblies built from the C# scripts in your project, assemblies that are part of packages and plugins, and assemblies in .NET Framework.
Let's confirm.
Confirming The Diagnosis
The main giveaway of Unity's managed code stripping will be the filesize of our main core libraries.
While size varies version to version, below is an approximation of what to expect.
Are your core libraries on a diet? If so, they've been stripped!
Unstripping The Stripped
The first step in solving our problem is to locate the Unity version of the game.
If you don't know the version number, here is a great article on how to find it. (Or if you are using MelonLoader or BepinEx, they both show the version number in the console)
Great! We know our hypothetical game uses Unity 2021.3.8
.
Option #1 - Download Libs from Online
The beautiful people at BepinEx created a libs repository for all things Unity. Simply download the appropriate .zips.
https://unity.bepinex.dev/libraries/<unity version>.zip
https://unity.bepinex.dev/corlibs/<unity version>.zip
Example:
https://unity.bepinex.dev/libraries/2021.3.8.zip
https://unity.bepinex.dev/corlibs/2021.3.8.zip
Once downloaded, extract the files from each .zip and move them into a temp folder.
Option #2 - Generate your own Libs with UnityHub
Really? Well, if you absolutely wanted to generate your own libs, it can be done.
You'll need to first install UnityHub. Once installed, navigate to the Installs
tab and click the big blue Install Editor
button.
From there, pick the correct Unity Version (in our example Unity 2021.3.8
and install).
Once installed, create a new project in UnityHub. Select the correct editor version (in our example Unity 2021.3.8
), select 3D or 2D according to your game, name your project, pick a location and finally, click the Create Project
button.
Unity will automatically open. Once opened, click File > Build And Run
or CTRL + B
and then pick a build folder location. Remember this location!
Now we wait patiently until the build completes.
Once the build is done, our game will launch. You can Alt + F4
to quit out quickly. We've generated our libraries!!
Hey.. remember that build folder location? Our libraries are there!
C:\Build\Location\Path\<Project Name>_Data\Managed
Back to Business
The rest is history. Simply replace the stripped libraries with the libraries you got from Options #1 or #2 above.
If you are modifying your game standalone or with MelonLoader, drop these files into:
C:\Path\To\Game\Folder\<Game Name>_Data\Managed
If you are modifying your game with BepinEx, create a folder in your games folder called unstripped_libs
and move your unstripped libraries there. Next, edit doorstop_config.ini
and find or create the option dllSearchPathOverride
and set that to unstripped_libs
.
Now your code will run error free! 🥳