Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Xml;
- using Verse;
- namespace LoadOnDemand
- {
- public class PatchOperationLoadOnDemand : PatchOperation
- {
- bool required;
- bool tested;
- public List<string> mods;
- public List<string> folders;
- protected override bool ApplyWorker (XmlDocument xml)
- {
- if (tested) return false;
- tested = true;
- if (mods.NullOrEmpty () || folders.NullOrEmpty()) {
- return false;
- }
- required = ModLister.AllInstalledMods.Select (mod => mod.Name).Any (name => mods.Contains (name));
- return true;
- }
- public override void Complete (string modIdentifier)
- {
- if (required) {
- foreach (var folder in folders) {
- Log.Message (modIdentifier + " :: Loading " + folder + " on demand");
- LoadDefs (LoadedModManager.GetMod<LoadOnDemand> ().Content, folder);
- }
- }
- base.Complete (modIdentifier);
- }
- // Adapted from ModContentPack.LoadDefs
- public void LoadDefs (ModContentPack content, string folder)
- {
- string path = Path.Combine (Path.Combine (content.RootDir, "DefsOnDemand/"), folder);
- if (!Directory.Exists (path)) {
- Log.Warning (string.Format("{0} is trying to load non-existant folder DefsOnDemand/{1}", content.Identifier, folder));
- return;
- }
- var list = DirectXmlLoader.XmlAssetsInModFolder (content, "DefsOnDemand/" + folder).ToList<LoadableXmlAsset> ();
- foreach (var xmlAsset in list) {
- foreach (var patch in content.Patches) {
- patch.Apply (xmlAsset.xmlDoc);
- }
- }
- for (int i = 0; i < list.Count; i++) {
- if (list [i] == null || list [i].xmlDoc == null || list [i].xmlDoc.DocumentElement == null) {
- Log.Error (string.Format ("{0}: unknown parse failure", list [i].fullFolderPath + "/" + list [i].name));
- } else if (list [i].xmlDoc.DocumentElement.Name != "Defs") {
- Log.Error (string.Format ("{0}: root element named {1}; should be named Defs", list [i].fullFolderPath + "/" + list [i].name, list [i].xmlDoc.DocumentElement.Name));
- }
- XmlInheritance.TryRegisterAllFrom (list [i], content);
- }
- XmlInheritance.Resolve ();
- for (int j = 0; j < list.Count; j++) {
- var defPackage = new DefPackage (list [j].name, folder);
- foreach (var def in DirectXmlLoader.AllDefsFromAsset (list [j])) {
- defPackage.defs.Add (def);
- }
- content.AddDefPackage (defPackage);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment