Repository navigation
fix(modloader): restore the stable OrderBy so libraries load before their dependents - #714
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
CheckAndSortDependenciesreturns the mods ordered by dependency, andinstantiateModsthen reorders them with the stable LINQOrderBy(first byRequiresCompilation, later the mod systems byExecuteOrder).CollectModsorders by file name the same way. #493 replaced those threeOrderBycalls withList<T>.Sort.List<T>.Sortis an in-place introsort that is only stable up to 16 elements, so with more mods the equal keys (every mod hasRequiresCompilation == 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
ExecuteOrderwere reordered even without any installed mod.The
Mods, sorted by dependencylog line is written before the sort, so the log looked right while the real order was not.Change
ModLoader.cs.patchrestores the threeOrderBycalls with static key selectors (KeyModRequiresCompilation,KeyModSystemExecuteOrder,KeyModFileName). The selectors keep the transplanted methods free of lambdas.LoadAssembly. Vanilla loads every enabled assembly and marks wrong-side code modsUnusedlater inModContainer.InstantiateModSystems, where it returns early for mods without an assembly. Marking every wrong-side modUnusedearlier 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.ModInfoguards 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
mainand once with this patch, and read vanilla'sInstantiate mod systems for <id>debug line, which is written in the orderinstantiateModsuses.maingamemod loaded 19th, after 12 mods that depend on it, plus two library pairs)Mods, sorted by dependencyorder)The two library pairs are
expandedfoodsbefore its libraryaculinaryartilleryandconfiglibbefore thevsimguiit requires.ModLoaderStableOrderTestspins the three calls and rejectsSorton the mod lists.ModLoader_DotPrefixedAndEmptyModInforeplaces 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 showsOrderBywith the three key selectors ininstantiateMods, the mod system ordering andCollectMods.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.