internal static IEnumerable <LoaderError> Load(ModDef mod) { var errors = new List <LoaderError>(); foreach (var f in Directory.EnumerateFiles(mod.Info.ModPath, "*.*", SearchOption.AllDirectories)) { var nf = NormalizeResourceFilePath(f, mod.Info.ModPath); string fn = Path.GetFileName(f); if (fn == PrismApi.JsonManifestFileName || fn == NormalizeResourceFilePath(mod.Info.AssemblyFileName) || mod.Info.References.Where(r => r is AssemblyReference).Select(r => NormalizeResourceFilePath(((AssemblyReference)r).Name)).Any(s => Path.GetFileName(s) == fn)) { continue; // shouldn't be used, obviously } try { mod.ContentHandler.resources.Add(nf, File.OpenRead(f)); } catch (Exception e) { errors.Add(new LoaderError(mod.Info, "Error reading resource: '" + f + "'.", e)); } } return(errors); }
/// <summary> /// Loads a mod and returns all <see cref="LoaderError"/>s encountered. /// </summary> /// <param name="mod">The mod to load.</param> /// <returns>Enumerable list of LoaderErrors encountered while loading the mod.</returns> internal static IEnumerable <LoaderError> Load(ModDef mod) { var ret = new List <LoaderError>(); var ch = mod.ContentHandler; mod.BuffDefs = SetEntityModDefs <BuffDef, BuffBehaviour>(mod, ch.GetBuffDefsInternally()); mod.ItemDefs = SetEntityModDefs <ItemDef, ItemBehaviour>(mod, ch.GetItemDefsInternally()); mod.MountDefs = SetEntityModDefs <MountDef, MountBehaviour>(mod, ch.GetMountDefsInternally()); mod.NpcDefs = SetEntityModDefs <NpcDef, NpcBehaviour>(mod, ch.GetNpcDefsInternally()); mod.ProjectileDefs = SetEntityModDefs <ProjectileDef, ProjectileBehaviour>(mod, ch.GetProjDefsInternally()); mod.TileDefs = SetEntityModDefs <TileDef, TileBehaviour>(mod, ch.GetTileDefsInternally()); mod.RecipeDefs = RecipeDefHandler.SetRecipeModDefs(mod, ch.GetRecipeDefsInternally()); ret.AddRange(Handler.BuffDef.Load(mod.BuffDefs)); ret.AddRange(Handler.ItemDef.Load(mod.ItemDefs)); ret.AddRange(Handler.MountDef.Load(mod.MountDefs)); ret.AddRange(Handler.NpcDef.Load(mod.NpcDefs)); ret.AddRange(Handler.ProjDef.Load(mod.ProjectileDefs)); ret.AddRange(Handler.TileDef.Load(mod.TileDefs)); ret.AddRange(Handler.RecipeDef.Load(mod.RecipeDefs)); return(ret); }
internal static List <RecipeDef> SetRecipeModDefs(ModDef mod, IEnumerable <RecipeDef> defs) { return(defs.Select(d => { d.Mod = mod.Info; return d; }).ToList()); }
public override void OnInspectorGUI() { ModDef m = target as ModDef; if (!m.HasCreated) { EditorGUILayout.HelpBox("You need to give a mod ID. This ID cannot change after the mod is created.", MessageType.Info); idInput = idInput.Trim().ToLower(); idInput = GUILayout.TextField(idInput); bool valid = IsValidID(idInput, out string error); if (valid && GUILayout.Button("Create mod files")) { // Assign id. m.ID = idInput; // Create a folder next to the mod def file that has the same name as the mod def file. string modFolder = "Assets/Mods/" + m.ID; AssetDatabase.CreateFolder("Assets/Mods", m.ID); AssetDatabase.CreateFolder(modFolder, "Scripts"); AssetDatabase.CreateFolder(modFolder, "Content"); CreateAssemblyDef(m.ID); m.HasCreated = true; } else if (!valid) { EditorGUILayout.HelpBox(error, MessageType.Error); } return; } GUI.enabled = false; GUILayout.Label($"ID: {m.ID}"); GUI.enabled = true; DrawDefaultInspector(); EditorGUILayout.Space(); bool canBuild = true; if (canBuild) { GUILayout.BeginHorizontal(); zip = GUILayout.Toggle(zip, "Zip Compress", GUILayout.ExpandWidth(false)); bool build = GUILayout.Button("Build Mod", GUILayout.ExpandWidth(true)); GUILayout.EndHorizontal(); if (build) { m.Build(zip); } } else { GUILayout.Label("Fix errors to build the mod."); } }
internal static IEnumerable <LoaderError> Load(ModDef mod) { var ret = new List <LoaderError>(); mod.gameBehaviour = mod.ContentHandler.CreateGameBInternally(); mod.BgmEntries = SetEntryModDefs <BgmEntry, BgmRef>(mod, mod.ContentHandler.GetBgmsInternally()); mod.SfxEntries = SetEntryModDefs <SfxEntry, SfxRef>(mod, mod.ContentHandler.GetSfxesInternally()); return(ret); }
/// <summary> /// Sets each entity def's <see cref="EntityDef{TBehaviour, TEntity}.InternalName"/> /// and <see cref="EntityDef{TBehaviour, TEntity}.Mod"/> fields to the def's key /// in the dictionary and this <see cref="ModDef"/>, respectively. /// </summary> /// <typeparam name="TEntityDef"></typeparam> /// <param name="def"></param> /// <param name="dict"></param> /// <returns></returns> static Dictionary <string, TEntityDef> SetEntityModDefs <TEntityDef>(ModDef def, Dictionary <string, TEntityDef> dict) where TEntityDef : ObjectDef { foreach (var kvp in dict) { kvp.Value.InternalName = kvp.Key; kvp.Value.Mod = def.Info; } return(dict); }
static Dictionary <string, TEntry> SetEntryModDefs <TEntry, TRef>(ModDef def, Dictionary <string, TEntry> dict) where TEntry : AudioEntry <TEntry, TRef> where TRef : EntityRef <TEntry> { foreach (var kvp in dict) { kvp.Value.InternalName = kvp.Key; kvp.Value.Mod = def.Info; } return(dict); }
public IModCollection LoadCollectionFromPath(string battleTechPath, string modsPath, string name) { this.ModCollection.Name = name; this.ModCollection.Path = modsPath; if (!DirectoryUtils.Exists(modsPath)) { throw new Exception($@"The specified Mods directory [{modsPath}] does not exist."); } var gameDirectoryValid = true; if (!DirectoryUtils.Exists(battleTechPath)) { this.logger.Warn($"The Battle Tech Path [{battleTechPath}] could not be found."); gameDirectoryValid = false; } if (gameDirectoryValid) { var gameDirectoryInfo = new DirectoryInfo(battleTechPath); this.logger.Info($"Processing BattleTech from [{gameDirectoryInfo.FullName}]"); var battleTechMod = this.TryLoadFromPath(gameDirectoryInfo.FullName, true); this.ModCollection.AddModToCollection(battleTechMod); } // find all sub-directories that have a mod.json file var modDirectories = Directory.GetDirectories(modsPath).Where(x => File.Exists(Path.Combine(x, "mod.json"))).ToArray(); // create ModDef objects for each mod.json file HashSet <string> FailedToLoadMods = new HashSet <string>(); Dictionary <string, ModDef> ModDefs = new Dictionary <string, ModDef>(); foreach (var modDirectory in modDirectories) { ModDef modDef; var modDefPath = Path.Combine(modDirectory, "mod.json"); try { modDef = ModDef.CreateFromPath(modDefPath); } catch (Exception e) { FailedToLoadMods.Add(modDefPath); continue; } if (!modDef.ShouldTryLoad(ModDefs.Keys.ToList(), out var reason)) { if (!modDef.IgnoreLoadFailure) { FailedToLoadMods.Add(modDef.Name); } continue; } ModDefs.Add(modDef.Name, modDef); } // get a load order and remove mods that won't be loaded var ModLoadOrder = LoadOrder.CreateLoadOrder(ModDefs, out var notLoaded, new List <string>(), this.logger); var modsDirectoryInfo = new DirectoryInfo(modsPath); this.logger.Info($"Processing mods from [{modsDirectoryInfo.FullName}]"); /*modsDirectoryInfo.GetDirectories() * //.AsParallel().ForAll( * .ToList().ForEach( * sub => * { * this.logger.Debug("."); * var mod = this.TryLoadFromPath(sub.FullName, false); * this.ModCollection.AddModToCollection(mod); * });*/ ModLoadOrder.ForEach( s => { var def = ModDefs.First(pair => pair.Key == s).Value; var mod = this.TryLoadFromPath(def.Directory, false); this.ModCollection.AddModToCollection(mod); }); /*this.ModCollection.Mods.Sort( * (mod, mod1) => mod.IsBattleTech ? 1 : string.Compare(mod.Name, mod1.Name, StringComparison.OrdinalIgnoreCase));*/ this.OnPropertyChanged(nameof(this.ModCollection)); return(this.ModCollection); }