Example #1
0
        internal static List <Mod> InstantiateMods(List <LocalMod> modsToLoad)
        {
            var modList = new List <LoadedMod>();

            foreach (var loading in modsToLoad)
            {
                if (!loadedMods.TryGetValue(loading.Name, out LoadedMod mod))
                {
                    mod = loadedMods[loading.Name] = new LoadedMod();
                }

                mod.SetMod(loading);
                modList.Add(mod);
            }

            RecalculateReferences();

            if (Debugger.IsAttached)
            {
                ModLoader.isModder = true;
                foreach (var mod in modList.Where(mod => mod.properties.editAndContinue && mod.CanEaC))
                {
                    mod.EnableEaC();
                }
            }
            if (ModLoader.alwaysLogExceptions)
            {
                ModCompile.ActivateExceptionReporting();
            }

            try
            {
                //load all the assemblies in parallel.
                int i = 0;
                Parallel.ForEach(modList, mod =>
                {
                    Interface.loadMods.SetProgressCompatibility(mod.Name, i++, modsToLoad.Count);
                    mod.LoadAssemblies();
                });

                //Assemblies must be loaded before any instantiation occurs to satisfy dependencies
                return(modList.Select(Instantiate).ToList());
            }
            catch (AggregateException ae)
            {
                ErrorLogger.LogMulti(ae.InnerExceptions.Select(e => new Action(() => {
                    var mod = modList.Single(m => m.Name == (string)e.Data["mod"]);
                    ModLoader.DisableMod(mod.Name);
                    ErrorLogger.LogLoadingError(mod.Name, mod.modFile.tModLoaderVersion, e);
                })));
                return(null);
            }
            catch (Exception e)
            {
                var mod = modList.Single(m => m.Name == (string)e.Data["mod"]);
                ModLoader.DisableMod(mod.Name);
                ErrorLogger.LogLoadingError(mod.Name, mod.modFile.tModLoaderVersion, e);
                return(null);
            }
        }
        internal static List <Mod> InstantiateMods(List <ModLoader.LoadingMod> modsToLoad)
        {
            var modList = new List <LoadedMod>();

            foreach (var loading in modsToLoad)
            {
                LoadedMod mod;
                if (!loadedMods.TryGetValue(loading.Name, out mod))
                {
                    mod = loadedMods[loading.Name] = new LoadedMod();
                }

                mod.SetMod(loading);
                modList.Add(mod);
            }

            RecalculateReferences();

            if (Debugger.IsAttached)
            {
                ModLoader.isModder = true;
                foreach (var mod in modList.Where(mod => mod.properties.editAndContinue && mod.CanEaC))
                {
                    mod.EnableEaC();
                }
            }
            if (ModLoader.alwaysLogExceptions)
            {
                ModCompile.ActivateExceptionReporting();
            }

            var modInstances = new List <Mod>();

            int i = 0;

            foreach (var mod in modList)
            {
                Interface.loadMods.SetProgressCompatibility(mod.Name, i++, modsToLoad.Count);
                try {
                    Interface.loadMods.SetProgressReading(mod.Name, 0, 1);
                    mod.LoadAssemblies();

                    Interface.loadMods.SetProgressReading(mod.Name, 1, 2);
                    Type modType;
                    try
                    {
                        modType = mod.assembly.GetTypes().Single(t => t.IsSubclassOf(typeof(Mod)));
                    }
                    catch (Exception e)
                    {
                        throw new Exception("It looks like this mod doesn't have a class extending Mod. Mods need a Mod class to function.", e)
                              {
                                  HelpLink = "https://github.com/blushiemagic/tModLoader/wiki/Basic-tModLoader-Modding-FAQ#sequence-contains-no-matching-element-error"
                              };
                    }
                    var m = (Mod)Activator.CreateInstance(modType);
                    m.File        = mod.modFile;
                    m.Code        = mod.assembly;
                    m.Side        = mod.properties.side;
                    m.DisplayName = mod.properties.displayName;
                    modInstances.Add(m);
                }
                catch (Exception e) {
                    ModLoader.DisableMod(mod.modFile);
                    ErrorLogger.LogLoadingError(mod.Name, mod.modFile.tModLoaderVersion, e);
                    return(null);
                }
            }

            return(modInstances);
        }