How does it Really Work?
Info
The following section describes the internal implementation detail of how dependency injection works. It is provided to those that are really interested.
Reloaded makes extensive use of AssemblyLoadContext(s) to provide isolation between mods, such that they may use 3rd library plugins and dependencies of their choice without interfering with other mods. This can make it difficult to share state between a mod and the loader, and even more so between individual mods.
Reloaded overcomes this challenge by sharing instances of Assemblies
(DLLs), loosening up isolation around shared code. Mods can nominate which Assemblies
they wish to share with other mods, and the loader will load them into a shared AssemblyLoadContext
. When the mod is loaded, the Mod Loader will then force it use the shared Assembly
instances from the shared AssemblyLoadContext
.
For publishers its own shared types are automatically shared with itself, for consumers this is determined using the ModDependencies
field in ModConfig.json
.
How is this arrangement Setup?
The whole process for sharing an Assembly
looks something like this:
-
Gathering Dependencies
- Mod Loader loads
Redirector
(Publisher). - Mod Loader loads all shared Assemblies into Shared
AssemblyLoadContext
- Mod Loader unloads
Redirector
(Publisher).
- Mod Loader loads
-
Loading Mods
- Mod Loader loads
Redirector
, sharing its ownAssemblies
with itself (from 1.b). - Mod Loader loads
RedirectorMonitor
(Consumer).RedirectorMonitor
specifiedRedirector
in itsModDependencies
field.- Therefore Mod Loader shares all of
Redirector
's exports withRedirectorMonitor
.
- Mod Loader loads
-
Executing Mods
- Mod Loader rearranges mod load order to account for mod dependencies.
Redirector
code executes. Redirector publishesIRedirectorController
(from Reloaded.Mod.Interfaces.IRedirector.dll) to Mod Loader.Monitor
code executes. Monitor obtainsIRedirectorController
from Mod Loader.