Beispiel #1
0
        /// <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();
            }
        }
Beispiel #2
0
        /// <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);
        }