Unity Mod Manager

repository·master·Indexed 19 days ago

https://github.com/newman55/unity-mod-manager

A framework for adding modding capabilities to Unity-based games. It enables the loading and management of mods through automated patching and injection techniques, supporting both Assembly Injection and Doorstop Proxy installation methods.

Tokens
1.6K
Snippets
4
Records
7
Agent score
18%

What's inside unity-mod-manager

  1. Installation methods for Unity Mod Manager

    master

    Unity Mod Manager supports two primary installation methods to hook into a Unity game. The availability of these methods depends on the platform (Unix/Mac vs Windows) and whether a Doorstop proxy is already present:

    1. Assembly Injection (InstallType.Assembly): Modifies the game's entry assembly to inject the manager. This is the standard method for Windows games.
    2. Doorstop Proxy (InstallType.DoorstopProxy): Uses a native DLL (winhttp.dll or architecture-specific variants) to intercept the game process and load the manager. This is often used when assembly injection is not feasible or preferred.

    Note: On Unix-based platforms, the DoorstopProxy method is unavailable, and the system defaults to Assembly injection.

  2. Install Unity Mod Manager via the Install action

    master

    The Actions.Install workflow performs a patch injection into a game assembly to enable Unity Mod Manager.

    Workflow steps:

    1. Backup: Creates backups of the target assembly, library destination paths, and game configuration.
    2. Assembly Patching:
      • Locates the entry point of the target assembly.
      • Injects the UnityModManagerStarter.Start method into the assembly's entry point (either at the beginning or the end of the method body).
      • Writes the modified assembly back to the disk.
    3. Library Deployment: Copies required DLLs from librarySourcePaths to libraryDestPaths.
    4. Configuration: Exports game-specific configuration files using selectedGame.ExportToGame().

    If any step fails, the process attempts to restore the original files from the created backups.

    // Conceptual representation of the Install logic flow
    if (action == Actions.Install)
    {
        Utils.MakeBackup(assemblyPath);
        Utils.MakeBackup(libraryDestPaths);
        // ... Injecting UnityModManagerStarter.Start into assemblyDef ...
        assemblyDef.Write(assemblyPath);
        DoactionLibraries(Actions.Install);
        DoactionGameConfig(Actions.Install);
    }
  3. Uninstall Unity Mod Manager via the Delete action

    master

    The Actions.Delete workflow removes the Unity Mod Manager patch and associated files from the game directory.

    Workflow steps:

    1. Detection: Identifies if the manager was installed using the legacy method (UnityModManager.Start) or the newer method (UnityModManagerStarter.Start).
    2. Instruction Removal: Locates the injected Call instruction at the assembly's entry point and removes it.
    3. Type Removal: Removes the manager's type (e.g., UnityModManagerStarter) from the assembly.
    4. File Cleanup:
      • Deletes files in libraryDestPaths.
      • Deletes any .dll files found in the managerPath directory.
      • Deletes the game configuration file located at GameInfo.filepathInGame.

    If the write flag is enabled, backups are created before modification and restored if an error occurs.

    // Conceptual representation of the Delete logic flow
    if (action == Actions.Delete)
    {
        // 1. Identify injected instruction (UnityModManager.Start or UnityModManagerStarter.Start)
        // 2. Remove instruction from methodDef.Body.Instructions
        // 3. Remove the manager type from assemblyDef.Types
        assemblyDef.Write(assemblyPath);
        DoactionLibraries(Actions.Delete);
        DoactionGameConfig(Actions.Delete);
    }
  4. Reference: Actions and LibIncParam flags

    master

    The console uses bitwise flags to manage available user actions and library inclusion parameters during the installation process.

    [Flags]
    enum Actions {
        Install = 1,
        Update = 2,
        Delete = 4,
        Restore = 8,
        Path = 16
    }
    
    [Flags]
    enum LibIncParam {
        Normal = 0,
        Skip = 1,
        Minimal_lt_0_22 = 2,
        Harmony_2_2 = 4
    }
  5. Manage library files and game configurations

    master

    The CLI uses two internal helper methods to manage the side effects of installation and removal:

    DoactionLibraries(Actions action)

    Handles the movement of DLLs required by the manager.

    • Actions.Install: Iterates through libraryDestPaths and copies files from librarySourcePaths. It skips files if the LastWriteTimeUtc matches the source.
    • Actions.Delete: Deletes files at libraryDestPaths and removes all *.dll files within the managerPath directory.

    DoactionGameConfig(Actions action)

    Handles the game-specific configuration files.

    • Actions.Install: Calls selectedGame.ExportToGame() to generate necessary config files (e.g., Config.xml).
    • Actions.Delete: Deletes the file located at GameInfo.filepathInGame.
  6. Available commands in Unity Mod Manager Console

    master

    The Unity Mod Manager Console provides a text-based interface to manage the installation of the manager into a Unity game. After selecting a game and verifying the path, you can perform the following actions using single-letter keys:

    • I (Install): Installs the manager using the currently selected installation method (Assembly injection or Doorstop proxy).
    • U (Update): Updates the existing installation to the current version of Unity Mod Manager.
    • D (Delete): Removes the Unity Mod Manager files from the game directory.
    • R (Restore): Restores the original game assembly if it was modified via assembly injection (requires the .original_ backup file to exist).
    • P (Path): Resets the game path selection to allow choosing a different game folder.
    I. Install
    U. Update
    D. Delete
    R. Restore
    P. Path