/// <summary> /// Find and load all ModBases in the given assembly. /// </summary> /// <param name="meta">The mod metadata, preferably from the mod metadata.yaml file.</param> /// <param name="asm">The mod assembly, preferably relinked.</param> public static void LoadModAssembly(ModMetadata meta, Assembly asm) { if (meta != null) { ModContentManager.Crawl(new AssemblyModContent(asm)); } Type[] types; try { types = asm.GetTypes(); } catch (Exception e) { Logger.Log(LogLevel.Warn, "loader", $"Failed reading assembly: {e}"); e.LogDetailed(); return; } for (int i = 0; i < types.Length; i++) { Type type = types[i]; if (!typeof(ModBase).IsAssignableFrom(type) || type.IsAbstract) { continue; } ModBase mod = (ModBase)type.GetConstructor(ModManager._EmptyTypeArray).Invoke(ModManager._EmptyObjectArray); if (meta != null) { mod.Metadata = meta; } mod.Register(); } }
/// <summary> /// Deserialize this asset's matching .meta asset. Uses TryDeserialize internally. /// </summary> /// <typeparam name="T">The target meta type.</typeparam> /// <param name="meta">The requested meta object.</param> /// <returns>True if deserializing the meta asset succeeded, false otherwise.</returns> public bool TryGetMeta <T>(out T meta) { ModAsset metaAsset; if (ModContentManager.TryGet(PathVirtual + ".meta", out metaAsset) && metaAsset.TryDeserialize(out meta) ) { return(true); } meta = default(T); return(false); }
protected override void Crawl() { string[] resourceNames = Assembly.GetManifestResourceNames(); for (int i = 0; i < resourceNames.Length; i++) { string name = resourceNames[i]; int indexOfContent = name.IndexOf("Content"); if (indexOfContent < 0) { continue; } name = name.Substring(indexOfContent + 8); ModContentManager.Add(name, new AssemblyModAsset(Assembly, resourceNames[i])); } }
protected virtual void Crawl(string dir, string root = null) { if (root == null) { root = dir; } string[] files = Directory.GetFiles(dir); for (int i = 0; i < files.Length; i++) { string file = files[i]; ModContentManager.Add(file.Substring(root.Length + 1), new FileSystemModAsset(file)); } files = Directory.GetDirectories(dir); for (int i = 0; i < files.Length; i++) { string file = files[i]; Crawl(file, root); } }
protected void Add(string path, ModAsset asset) { asset = ModContentManager.Add(path, asset); List.Add(asset); Map[asset.PathVirtual] = asset; }
/// <summary> /// Load a mod from a directory at runtime. /// </summary> /// <param name="dir">The path to the mod directory.</param> public static void DefaultLoadDir(string dir) { if (!Directory.Exists(dir)) // Relative path? { dir = Path.Combine(PathMods, dir); } if (!Directory.Exists(dir)) // It just doesn't exist. { return; } Logger.Log(LogLevel.Verbose, "loader", $"Loading mod directory: {dir}"); ModMetadata meta = null; ModMetadata[] multimetas = null; string metaPath = Path.Combine(dir, "metadata.yaml"); if (File.Exists(metaPath)) { using (StreamReader reader = new StreamReader(metaPath)) { try { meta = YamlHelper.Deserializer.Deserialize <DirectoryModMetadata>(reader); meta.Container = dir; meta.PostParse(); } catch (Exception e) { Logger.Log(LogLevel.Warn, "loader", $"Failed parsing metadata.yaml in {dir}: {e}"); } } } metaPath = Path.Combine(dir, "multimetadata.yaml"); if (File.Exists(metaPath)) { using (StreamReader reader = new StreamReader(metaPath)) { try { multimetas = YamlHelper.Deserializer.Deserialize <DirectoryModMetadata[]>(reader); foreach (ModMetadata multimeta in multimetas) { multimeta.Container = dir; multimeta.PostParse(); } } catch (Exception e) { Logger.Log(LogLevel.Warn, "loader", $"Failed parsing multimetadata.yaml in {dir}: {e}"); } } } ModContentSource contentMeta = new DirectoryModContent(dir); Action contentCrawl = () => { if (contentMeta == null) { return; } ModContentManager.Crawl(contentMeta); contentMeta = null; }; if (multimetas != null) { foreach (ModMetadata multimeta in multimetas) { LoadModDelayed(multimeta, contentCrawl); } } LoadModDelayed(meta, contentCrawl); }