Giter Site home page Giter Site logo

neightools / unitydoorstop Goto Github PK

View Code? Open in Web Editor NEW
402.0 11.0 60.0 502 KB

Doorstop -- run C# before Unity does!

License: GNU Lesser General Public License v2.1

C 83.53% PowerShell 1.26% Shell 11.54% Batchfile 0.06% Lua 3.60%
unity mono in-memory bootstrapper

unitydoorstop's Introduction

Unity Doorstop

GitHub Workflow Status nightly.link artifacts


Doorstop is a tool to execute managed .NET assemblies inside Unity as early as possible.

This is a total rewrite of UnityDoorstop 3. See list of breaking changes for more information.

Features

  • Runs first: Doorstop runs its code before Unity can do so
  • Configurable: An elementary configuration file allows you to specify your assembly to execute
  • Multiplatform: Supports Windows, Linux, macOS
  • Debugger support: Allows to debug managed assemblies in Visual Studio, Rider or dnSpy without modifications to Mono

Unity runtime support

Doorstop supports executing .NET assemblies in both Unity Mono and Il2Cpp runtimes. Depending on the runtime the game uses, Doorstop tries to run your assembly as follows:

  • On Unity Mono, your assembly is executed in the same runtime. As a result
    • You don't need to include your custom Common Language Runtime (CLR); the one bundled with the game is used
    • Your assembly is run alongside other Unity code
    • You can access all Unity API directly
  • On Il2Cpp, your assembly is executed in CoreCLR runtime because Il2Cpp cannot run managed assemblies. As a result:
    • You need to include .NET 6 or newer CoreCLR runtime with your managed assembly
    • Your assembly is run in a runtime that is isolated from Il2Cpp
    • You can access Il2Cpp runtime by interacting with its native il2cpp_ API

Building

Doorstop uses xmake to build the project. To build, run build.bat, build.ps1 or build.sh.

Available build options:

  • -with_logging: build with logging enabled
  • -arch: the architectures to build for, separated by commas (e.g. -arch x86,x64)
  • -debug: build in debug mode (currently only for *nix)

Note: Initial build times are usually slower because the build script automatically downloads and installs xmake.
On Unix, xmake is built directly from the source code.

Minimal injection example

To have Doorstop inject your code, create Entrypoint class into Doorstop namespace. Define a public static Start method in it:

using System.IO;

namespace Doorstop;

class Entrypoint
{
  public static void Start()
  {
      File.WriteAllText("doorstop_hello.log", "Hello from Unity!");
  }
}

You can then define any code you want in Start.

NOTE: On UnityMono, Doorstop bootstraps your assembly with a minimal number of assemblies and minimal configuration. This early execution allows for some interesting tricks, like redirecting the loading of some game assemblies. Bear also in mind that some of the Unity runtime is not initialized at such an early stage, limiting the code you can execute. You might need to appropriately pause the execution of your code until the moment you want to modify the game.

Doorstop environment variables

Doorstop sets some environment variables useful for code execution:

Environment variable Description
DOORSTOP_INITIALIZED Always set to TRUE. Use to determine if your code is run via Doorstop.
DOORSTOP_INVOKE_DLL_PATH Path to the assembly executed by Doorstop relative to the current working directory.
DOORSTOP_PROCESS_PATH Path to the application executable where the injected assembly is run.
DOORSTOP_MANAGED_FOLDER_DIR UnityMono: Path to the game's Managed folder. Il2Cpp: Path to CoreCLR's base class library folder.
DOORSTOP_DLL_SEARCH_DIRS Paths where the runtime searchs assemblies from by default, separated by OS-specific separator (; on windows and : on *nix).
DOORSTOP_MONO_LIB_PATH Only on UnityMono: Full path to the mono runtime library.

Debugging

Doorstop 4 supports debugging the assemblies in the runtime. At the moment, debugging is somewhat limited depending on the runtime.

Debugging in UnityMono

At the moment, debugging is supported in Unity 2017+ games.

To enable debugging, set debug_enabled to true and optionally change the debug server address via debug_address (see configuration options).
After launching the game, you may connect to the debugger using the server address (default is 127.0.0.1:10000).
By default, the game won't wait for the debugger to connect; you may change the behaviour with the debug_suspend option.

If you use dnSpy, you can use the Debug > Start Debugging > Debug engine > Unity option, automatically setting the correct debugging configuration.
Doorstop detects dnSpy and automatically enables debugging without any extra configuration.

Debugging in Il2Cpp

Debugging is automatically enabled in CoreCLR.

To start debugging, compile your DLL in debug mode (with embedded or portable symbols) and start the game with the debugger of your choice.
Alternatively, attach a debugger to the game once it is running. All standard CoreCLR debuggers should detect the CoreCLR runtime in the game.

Moreover, hot reloading is supported for Visual Studio, Rider and other debuggers with .NET 6 hot reloading feature enabled.

Note that you can only debug managed code! Because the game code is unmanaged (i.e. Il2Cpp), you cannot directly debug the actual game code. Consider using native debuggers like GDB and visual debugging tools like IDA or Ghidra to debug actual game code.

Doorstop configuration

Doorstop is highly configurable based on your needs and the environment you want to use. There are two ways to configure Doorstop: via config and CLI arguments.

Via configuration file

Refer to doorstop_config.ini (Windows) or run.sh for all available configuration options.

CLI arguments

The following CLI arguments are available on both *nix, and Windows builds:

All Doorstop arguments start with --doorstop- and always contain an argument. The arguments can be of the following type:

  • bool = true or false
  • string = any sequence of characters and numbers. Wrap into "s if the string contains spaces
Argument Description
--doorstop-enabled bool Enable or disable Doorstop.
--doorstop-redirect-output-log bool Only on Windows: If true Unity's output log is redirected to <current folder>\output_log.txt
--doorstop-target-assembly string Path to the assembly to load and execute.
--doorstop-mono-dll-search-path-override string Overrides default Mono DLL search path
--doorstop-mono-debug-enabled bool If true, Mono debugger server will be enabled
--doorstop-mono-debug-suspend bool Whether to suspend the game execution until the debugger is attached.
--doorstop-mono-debug-address string The address to use for the Mono debugger server.
--doorstop-clr-corlib-dir string Path to coreclr library that contains the CoreCLR runtime
--doorstop-clr-runtime-coreclr-path string Path to the directory containing the managed core libraries for CoreCLR (mscorlib, System, etc.)

License

Doorstop 4 is licensed under LGPLv2.1. You can view the entire license here.
You can still access the source code to the original UnityDoorstop 3 source (licensed under CC0) from the legacy branch.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.