Skip to content

fix(modloader): restore the stable OrderBy so libraries load before their dependents - #714

Merged
Zaldaryon merged 1 commit into
mainfrom
fix/modloader-stable-dependency-order
Nov 20, 2026
Merged

Zaldaryon merged 1 commit into
mainfrom
fix/modloader-stable-dependency-order

Conversation

@Zaldaryon

@Zaldaryon Zaldaryon commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A user reported that Optimum loads mods out of order, with libraries loading after the mods that depend on them. The cause is in ModLoader.

Root cause

CheckAndSortDependencies returns the mods ordered by dependency, and instantiateMods then reorders them with the stable LINQ OrderBy (first by RequiresCompilation, later the mod systems by ExecuteOrder). CollectMods orders by file name the same way. #493 replaced those three OrderBy calls with List<T>.Sort. List<T>.Sort is an in-place introsort that is only stable up to 16 elements, so with more mods the equal keys (every mod has RequiresCompilation == false) were shuffled and the dependency order was lost.

The mod system ordering is hit more often than the mod ordering: the base game alone has more than 16 systems, so systems sharing the default ExecuteOrder were reordered even without any installed mod.

The Mods, sorted by dependency log line is written before the sort, so the log looked right while the real order was not.

Change

  • ModLoader.cs.patch restores the three OrderBy calls with static key selectors (KeyModRequiresCompilation, KeyModSystemExecuteOrder, KeyModFileName). The selectors keep the transplanted methods free of lambdas.
  • It also drops the Side check that fix(modloader): ignore dot-prefixed folders, guard null ModInfo, and check Side before LoadAssembly #493 added before LoadAssembly. Vanilla loads every enabled assembly and marks wrong-side code mods Unused later in ModContainer.InstantiateModSystems, where it returns early for mods without an assembly. Marking every wrong-side mod Unused earlier removed content-only mods of the other side from the loaded mods and the asset origins, and skipped loading or compiling their assemblies. Review of this PR flagged it as a vanilla deviation that is unrelated to the ordering fix, so it goes in the same change instead of staying half reverted.
  • The dot-prefixed directory filter and the null ModInfo guards from fix(modloader): ignore dot-prefixed folders, guard null ModInfo, and check Side before LoadAssembly #493 stay.

Evidence

I ran the same singleplayer world twice with 25 mods (21 from the ModDB, the three game mods and one local mod), once with the current main and once with this patch, and read vanilla's Instantiate mod systems for <id> debug line, which is written in the order instantiateMods uses.

Build Mods instantiated Mods instantiated before one of their dependencies
main 25 14 (the game mod loaded 19th, after 12 mods that depend on it, plus two library pairs)
this patch 25 0 (instantiation order equals the Mods, sorted by dependency order)

The two library pairs are expandedfoods before its library aculinaryartillery and configlib before the vsimgui it requires.

ModLoaderStableOrderTests pins the three calls and rejects Sort on the mod lists. ModLoader_DotPrefixedAndEmptyModInfo replaces the assertion for the removed Side check and the renamed helper. These tests read the patch text, so the in-game run above is the behavioral evidence. A decompile of the regenerated runtime cache shows OrderBy with the three key selectors in instantiateMods, the mod system ordering and CollectMods.

Not covered

The run is one machine and one mod set. The dependency sort itself is vanilla and unchanged. Source code mods still sort after every compiled mod, as in vanilla.

…heir dependents

instantiateMods receives the mods already sorted by dependency and vanilla
reorders them with the stable LINQ OrderBy. The Optimum patch used
List<T>.Sort instead, an unstable in-place introsort above 16 elements, in
three places: the RequiresCompilation grouping, the ModSystem ExecuteOrder
ordering and the CollectMods file name ordering. With enough mods a library
loaded after the mods that depend on it.

Restore OrderBy with static key selectors, which keeps the transplanted
methods free of lambdas. With 25 mods the game instantiated 14 of them before
one of their dependencies (the game mod itself loaded 18th); with this change
the instantiation order equals the dependency order.

Also drop the Side check that ran before LoadAssembly. Vanilla loads every
enabled assembly and marks wrong-side code mods Unused later, in
ModContainer.InstantiateModSystems. Marking every wrong-side mod Unused
earlier removed content-only mods of the other side from the loaded mods and
the asset origins, and skipped loading or compiling their assemblies.
@Zaldaryon
Zaldaryon merged commit 8ec9056 into main Nov 20, 2026
39 checks passed
@Zaldaryon
Zaldaryon deleted the fix/modloader-stable-dependency-order branch November 20, 2026 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant