public static void LoadMod(GameModMetadata meta, Assembly asm) { ModContent.Crawl(null, asm); Type[] types; try { types = asm.GetTypes(); } catch (Exception e) { e.LogDetailed(); ModLogger.Log("loader", $"Failed reading assembly: {e}"); return; } for (int i = 0; i < types.Length; i++) { Type type = types[i]; if (!typeof(GameMod).IsAssignableFrom(type) || type.IsAbstract) { continue; } GameMod mod = (GameMod)type.GetConstructor(_EmptyTypeArray).Invoke(_EmptyObjectArray); mod.Metadata = meta; Mods.Add(mod); _ModuleTypes.Add(type); _ModuleMethods.Add(new FastDictionary <string, MethodInfo>()); } ModLogger.Log("loader", $"Mod {meta} initialized."); }
public static void LoadModDir(string dir) { if (!Directory.Exists(dir)) { // Probably a mod in the mod directory dir = Path.Combine(ModsDirectory, dir); } if (!Directory.Exists(dir)) { // It just doesn't exist. return; } ModLogger.Log("loader", $"Loading mod directory: {dir}"); GameModMetadata meta = null; Assembly asm = null; // First read the metadata, ... string metaPath = Path.Combine(dir, "metadata.yaml"); if (File.Exists(metaPath)) { using (StreamReader reader = new StreamReader(metaPath)) meta = GameModMetadata.Parse("", dir, reader); } if (meta != null) { // ... then check if the mod runs on this profile ... if (meta.ProfileID > ModAPI.Profile.Id) { ModLogger.Log("loader", "Mod meant for an in-dev YLMAPI version!"); return; } // ... then check if the dependencies are loaded ... foreach (GameModMetadata dep in meta.Dependencies) { if (!DependencyLoaded(dep)) { ModLogger.Log("loader", $"Dependency {dep} of mod {meta} not loaded!"); return; } } // ... then add an AssemblyResolve handler for all the .zip-ped libraries AppDomain.CurrentDomain.AssemblyResolve += meta._GenerateModAssemblyResolver(); } // ... then everything else ModContent.Crawl(null, dir); if (meta == null || !File.Exists(meta.DLL)) { return; } if (!string.IsNullOrEmpty(meta.PatchDLL) && File.Exists(meta.PatchDLL)) { using (Stream stream = File.OpenRead(meta.PatchDLL)) ModRuntimePatcher.LoadPatch(stream); } if (meta.Prelinked) { asm = Assembly.LoadFrom(meta.DLL); } else { using (FileStream fs = File.OpenRead(meta.DLL)) asm = meta.GetRelinkedAssembly(fs); } if (asm != null) { LoadMod(meta, asm); } }