Skip to content

Commit

Permalink
Auto Enable Dependency Mods on Reload (#3726)
Browse files Browse the repository at this point in the history
* Rename variable for clarity & re-add diff style EnableDeps

* Fix avoidable call of EnsureDepsExist

* Eliminate duplicate code
  • Loading branch information
Solxanich committed Aug 25, 2023
1 parent 08050d1 commit 73dc090
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions patches/tModLoader/Terraria/ModLoader/Core/ModOrganizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@ private static IEnumerable<string> GetTemporaryFiles()

internal static bool LoadSide(ModSide side) => side != (Main.dedServ ? ModSide.Client : ModSide.Server);

internal static List<LocalMod> SelectAndSortMods(IEnumerable<LocalMod> mods, CancellationToken token)
internal static List<LocalMod> SelectAndSortMods(IEnumerable<LocalMod> availableMods, CancellationToken token)
{
var missing = ModLoader.EnabledMods.Except(mods.Select(mod => mod.Name)).ToList();
var missing = ModLoader.EnabledMods.Except(availableMods.Select(mod => mod.Name)).ToList();
if (missing.Any()) {
Logging.tML.Info("Missing previously enabled mods: " + string.Join(", ", missing));
foreach (var name in missing)
Expand All @@ -316,18 +316,21 @@ internal static List<LocalMod> SelectAndSortMods(IEnumerable<LocalMod> mods, Can
return new();
}

CommandLineModPackOverride(mods);
CommandLineModPackOverride(availableMods);

// Alternate fix for updating enabled mods
//foreach (string fileName in Directory.GetFiles(modPath, "*.tmod.update", SearchOption.TopDirectoryOnly)) {
// File.Copy(fileName, Path.GetFileNameWithoutExtension(fileName), true);
// File.Delete(fileName);
//}
Interface.loadMods.SetLoadStage("tModLoader.MSFinding");
var modsToLoad = mods.Where(mod => mod.Enabled && LoadSide(mod.properties.side)).ToList();

VerifyNames(modsToLoad);
foreach (var mod in GetModsToLoad(availableMods)) {
EnableWithDeps(mod, availableMods);
}
SaveEnabledMods();

var modsToLoad = GetModsToLoad(availableMods);
try {
EnsureDependenciesExist(modsToLoad, false);
EnsureTargetVersionsMet(modsToLoad);
Expand All @@ -340,6 +343,13 @@ internal static List<LocalMod> SelectAndSortMods(IEnumerable<LocalMod> mods, Can
}
}

private static List<LocalMod> GetModsToLoad(IEnumerable<LocalMod> availableMods)
{
var modsToLoad = availableMods.Where(mod => mod.Enabled && LoadSide(mod.properties.side)).ToList();
VerifyNames(modsToLoad);
return modsToLoad;
}

private static void CommandLineModPackOverride(IEnumerable<LocalMod> mods)
{
if (string.IsNullOrWhiteSpace(commandLineModPack))
Expand Down Expand Up @@ -367,6 +377,17 @@ private static void CommandLineModPackOverride(IEnumerable<LocalMod> mods)
}
}

//TODO: This duplicates some of the logic in UIModItem
internal static void EnableWithDeps(LocalMod mod, IEnumerable<LocalMod> availableMods)
{
mod.Enabled = true;

foreach (var depName in mod.properties.RefNames(includeWeak: false)) {
if (availableMods.SingleOrDefault(m => m.Name == depName) is LocalMod { Enabled: false } dep)
EnableWithDeps(dep, availableMods);
}
}

private static void VerifyNames(List<LocalMod> mods)
{
var names = new HashSet<string>();
Expand Down

0 comments on commit 73dc090

Please sign in to comment.