private static void LoadFinalMods() { var registeredMods = ModRegistry.GetRegisteredItems(); foreach (var mod in registeredMods.Where(n => n.ModState == ModState.Unloaded)) { Log.Verbose($"Loading mod: {mod.Name} by {mod.Author}"); try { ApiEvents.InvokeModPreLoad(mod); mod.OnBeforeLoaded(); if (mod.HasContent) { mod.LoadContent(); } if (mod.HasDll) { mod.LoadModDll(); } mod.ModState = ModState.Loaded; mod.OnAfterLoaded(); Log.Success($"Loaded Mod: {mod.Name} v{mod.Version} by {mod.Author}"); ApiEvents.InvokeModPostLoad(mod); } catch (Exception ex) { mod.ModState = ModState.Errored; Log.Exception($"Error loading mod {mod.Name} by {mod.Author}", ex); ApiEvents.InvokeModLoadError(mod); } } }
private static void LoadFinalMods() { var registeredMods = ModRegistry.GetRegisteredItems(); foreach (var mod in registeredMods.Where(n => n.ModState == ModState.Unloaded)) { Log.Verbose($"Loading mod: {mod.Name} by {mod.Author}"); try { ApiEvents.InvokeModPreLoad(mod); mod.OnBeforeLoaded(); if (mod.HasContent) { mod.LoadContent(); } if (mod.HasDll) { mod.LoadModDll(); } mod.ModState = ModState.Loaded; mod.OnAfterLoaded(); Log.Success($"Loaded Mod: {mod.Name} v{mod.Version} by {mod.Author}"); ApiEvents.InvokeModPostLoad(mod); } catch (Exception ex) { mod.ModState = ModState.Errored; Log.Exception($"Error loading mod {mod.Name} by {mod.Author}", ex); ApiEvents.InvokeModLoadError(mod); } } // See ReferenceFix.Data.BuildXnaTypeCache() // Since mod loading is done we don't need this anymore. // There are a lot of types, so might as well save the memory. ReferenceHelper.XnaTypes.Clear(); }
private static void LoadFinalMods() { Func <ModManifest[], List <ModManifest>, List <ModManifest>, List <ModManifest>, ModManifest[]> getModsForThisPass = (mods, modsLoaded, modsErrored, modsProcessed) => { Func <ModDependency, bool> isDependencyLoaded = dependency => { if (dependency.IsRequired) { return(modsLoaded.Any(ld => ld.UniqueId.Equals(dependency.UniqueId))); } var dependentMod = mods.FirstOrDefault(n => n.UniqueId.Equals(dependency.UniqueId)); return(dependentMod?.ModState != ModState.Unloaded); }; return (mods.Where( n => n.ModState == ModState.Unloaded && (n.Dependencies == null || n.Dependencies.All(d => isDependencyLoaded(d)))) .ToArray()); }; var modsToLoad = ModRegistry.GetRegisteredItems().Where(n => n.IsFarmhandMod).Cast <ModManifest>().ToArray(); var loadedMods = new List <ModManifest>(); var erroredMods = new List <ModManifest>(); var processedMods = new List <ModManifest>(); var modsThisPass = getModsForThisPass(modsToLoad, loadedMods, erroredMods, processedMods); while (modsThisPass.Any()) { foreach (var mod in modsThisPass) { processedMods.Add(mod); if (mod.ModState == ModState.DependencyLoadError) { erroredMods.Add(mod); continue; } Log.Verbose($"Loading mod: {mod.Name} by {mod.Author}"); try { ApiEvents.InvokeModPreLoad(mod); mod.OnBeforeLoaded(); if (mod.HasContent) { mod.LoadContent(); } if (mod.HasDll) { if (!mod.LoadModDll()) { mod.ModState = ModState.Errored; ApiEvents.InvokeModLoadError(mod); erroredMods.Add(mod); BubbleDependencyLoadError(mod); continue; } } mod.ModState = ModState.Loaded; mod.OnAfterLoaded(); Log.Success($"Loaded Mod: {mod.Name} v{mod.Version} by {mod.Author}"); ApiEvents.InvokeModPostLoad(mod); loadedMods.Add(mod); } catch (Exception ex) { mod.ModState = ModState.Errored; Log.Exception($"Error loading mod {mod.Name} by {mod.Author}", ex); ApiEvents.InvokeModLoadError(mod); erroredMods.Add(mod); BubbleDependencyLoadError(mod); break; } } modsThisPass = getModsForThisPass(modsToLoad, loadedMods, erroredMods, processedMods); } // See ReferenceFix.Data.BuildXnaTypeCache() // Since mod loading is done we don't need this anymore. // There are a lot of types, so might as well save the memory. ReferenceHelper.XnaTypes.Clear(); }