Nexus-Mods / vortex-api

Extension API for vortex
GNU General Public License v3.0
11 stars 4 forks source link

Harmony Patcher #9

Open TanninOne opened 3 years ago

TanninOne commented 3 years ago

Introduction

This functionality is deprecated, please use the BepInEx integration instead

This project aims to enhance Vortex’s modding capabilities by implementing an API/Modding resource which mod authors can use to inject their C# code and assets into game assemblies. This is particularly useful when modding Unity3D games which do not provide their own Mods Loader/Management system.

Implemented in C#, the patcher includes/deploys Andreas Pardeike’s Harmony library to allow mod authors to write prefix/postfix/transpiler methods to easily modify a game’s functionality. Reflection/Cecil can obviously be used directly if needed.

All mods must be written in C# and distributed as Dynamic Link Libraries (DLL), accompanied with a mandatory manifest.json file, and an optional settings.json (persistent settings state storage)

This project is currently windows-only but could be enhanced in the future to cater for other platforms.

Requirements

Although the Harmony library is a great tool which simplifies the modding process significantly, an intermediate understanding of software engineering, programming languages such as C#, and (potentially) JavaScript are required:

It’s important we state the extent of the patcher’s capabilities from the start. Patching games with Harmony relies mostly on the game itself and/or the game engine that it has been developed with, as both may have anti-tampering systems which aim to block users/modders from accessing and modifying the compiled code and/or adding/replacing game assets.

In the case of Unity3D games Harmony will allow mod authors to:

How it works

The project consists of four modules:

Workflow-wise, the extension creator defines the game extension, a game assembly, as well as an appropriate entry point. That entry point should be a class following the singleton design pattern and - preferably - the singleton object should be present throughout the game's life cycle. The game extension should implement both patch injection and removal use cases, either manually for more complex use cases, or automatically using the "harmonypatchermod" mod type which is designed to add/remove the patch upon deployment/purge events. Please note that the automatic patching should only be used in simple use cases where a single assembly needs to be patched More information on the "harmonypatchermod" mod type

Once the patcher function has been injected inside the game assembly, Vortex can be used to deploy/remove mods as usual, previously, the mods path was hard-coded to generate alongside the patched game assembly; e.g. when patching “Untitled Goose Game” the patch method call is injected inside the “../Untitled_data/managed/Assembly-CSharp.dll” assembly; and therefore the mods folder used to be generated inside “../Untitled_data/managed/VortexMods/”. As of Vortex 1.2.0, the mods path is customizable by the game extension.

The mods are loaded and executed at runtime by the game itself at the entry-point defined by the game extension author. The patch function will query the expected mods path and assemble a list of existing DLL files and map them against any matched manifest files; It’s important to include a manifest file for each mod assembly so that the mod loader can identify the mod’s type. Please ensure to respect the following directory structure pattern when creating mods for Vortex’s harmony patcher: The mod needs to have its own folder, e.g. “../VortexMods/ModName/”, inside this folder a DLL and manifest file must be included. e.g.

“../VortexMods/ModName/”
  “../VortexMods/ModName/ModName.dll”
  “../VortexMods/ModName/manifest.json”
  “../VortexMods/ModName/settings.json” (if the mod needs to save state persistently)

Example mods:

Manifest files will be parsed and any valid mod entries will be invoked/injected at game run-time as soon as the injected patch method is called.

As of Vortex 1.2.0, it is possible to create a "load_order.txt" file which can be used to dictate the order in which the Vortex mod loader executes mod patches. This is useful if a mod is written as an extension of another mod and therefore relies on that mod executing its patch first. Please keep in mind that EVERY mod patch will still be executed! Due to this fact, attempting to override mod X with mod Y may produce unexpected results. More on the Load Order Page

A number of Unity3D hooks/delegates are pre-defined for mod authors to subscribe to inside their mod assembly (OnUpdate, OnStart, etc). The mod loader will invoke these when they’re provided.

The Vortex In-Game Overlay (VIGO) is currently a Unity3D exclusive feature and aims to provide users with a convenient way to change mod settings from inside the game. When a Unity3D assembly is identified, the VortexUI library is deployed and its patch method call instruction is injected alongside the regular Vortex patch. The UI can be accessed in-game with the default key combination (CTRL+F12)

Planned Improvements

ElonGaties commented 1 year ago

Very cool tool, helps with many projects 👍