/// <summary> /// Try to create a protomod from an embedded modinfo json in a DLL /// </summary> /// <param name="dllFile">Path to the DLL file to process</param> /// <param name="mod">The parsed <see cref="ProtoMod"/>, or null</param> /// <returns>Whether the parsing was successful</returns> public static bool TryParseFromDLL(String dllFile, out ProtoMod mod) { var assembly = Assembly.LoadFile(dllFile); mod = new ProtoMod(); mod.isFromJSON = false; mod.path = Path.GetDirectoryName(dllFile); mod.entryFile = dllFile; if (assembly.GetManifestResourceNames().FirstOrDefault((x) => x.EndsWith("modinfo.json")) is string fileName) { using (var reader = new StreamReader(assembly.GetManifestResourceStream(fileName))) { mod = ParseFromJson(reader.ReadToEnd(), dllFile); mod.isFromJSON = false; } } else { return(false); } return(true); }
static void AddMod(ProtoMod modInfo, Type entryType) { ModEntryPoint entryPoint = (ModEntryPoint)Activator.CreateInstance(entryType); var newmod = new SRMod(modInfo.ToModInfo(), entryPoint, modInfo.path); Mods.Add(modInfo.id, newmod); }
public static void LoadMods() { FileSystem.CheckDirectory(FileSystem.ModPath); HashSet <ProtoMod> foundMods = new HashSet <ProtoMod>(new ProtoMod.Comparer()); foreach (var jsonFile in Directory.GetFiles(FileSystem.ModPath, ModJson, SearchOption.AllDirectories)) { var mod = ProtoMod.ParseFromJson(jsonFile); if (!foundMods.Add(mod)) { throw new Exception("Found mod with duplicate id '" + mod.id + "' in " + jsonFile + "!"); } } DependencyChecker.CheckDependencies(foundMods); DiscoverAndLoadAssemblies(foundMods); }
/// <summary> /// Searches for valid mods and their assemblies, and decides the load order based on their settings /// </summary> internal static void InitializeMods() { FileSystem.CheckDirectory(FileSystem.ModPath); HashSet <ProtoMod> foundMods = new HashSet <ProtoMod>(new ProtoMod.Comparer()); // process mods without embedded modinfo.jsons foreach (var jsonFile in Directory.GetFiles(FileSystem.ModPath, ModJson, SearchOption.AllDirectories)) { var mod = ProtoMod.ParseFromJson(jsonFile); if (!foundMods.Add(mod)) { throw new Exception("Found mod with duplicate id '" + mod.id + "' in " + jsonFile + "!"); } } // process mods with embedded modinfo.jsons foreach (var dllFile in Directory.GetFiles(FileSystem.ModPath, "*.dll", SearchOption.AllDirectories)) { if (!ProtoMod.TryParseFromDLL(dllFile, out var mod) || mod.id == null) { continue; } if (!foundMods.Add(mod)) { throw new Exception("Found mod with duplicate id '" + mod.id + "' in " + dllFile + "!"); } } // Make sure all dependencies are in order, otherwise throw an exception from checkdependencies DependencyChecker.CheckDependencies(foundMods); DependencyChecker.CalculateLoadOrder(foundMods, loadOrder); // Start loading the assemblies DiscoverAndLoadAssemblies(foundMods); }
public AssemblyInfo(AssemblyName name, String path, ProtoMod mod) { AssemblyName = name; Path = path; this.mod = mod; }