Guest User

Untitled

a guest
Nov 20th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Xml;
  5.  
  6. using Verse;
  7.  
  8. namespace LoadOnDemand
  9. {
  10. public class PatchOperationLoadOnDemand : PatchOperation
  11. {
  12. bool required;
  13. bool tested;
  14.  
  15. public List<string> mods;
  16. public List<string> folders;
  17.  
  18. protected override bool ApplyWorker (XmlDocument xml)
  19. {
  20. if (tested) return false;
  21.  
  22. tested = true;
  23.  
  24. if (mods.NullOrEmpty () || folders.NullOrEmpty()) {
  25. return false;
  26. }
  27.  
  28. required = ModLister.AllInstalledMods.Select (mod => mod.Name).Any (name => mods.Contains (name));
  29.  
  30. return true;
  31. }
  32.  
  33. public override void Complete (string modIdentifier)
  34. {
  35. if (required) {
  36. foreach (var folder in folders) {
  37. Log.Message (modIdentifier + " :: Loading " + folder + " on demand");
  38.  
  39. LoadDefs (LoadedModManager.GetMod<LoadOnDemand> ().Content, folder);
  40. }
  41. }
  42.  
  43. base.Complete (modIdentifier);
  44. }
  45.  
  46. // Adapted from ModContentPack.LoadDefs
  47. public void LoadDefs (ModContentPack content, string folder)
  48. {
  49. string path = Path.Combine (Path.Combine (content.RootDir, "DefsOnDemand/"), folder);
  50. if (!Directory.Exists (path)) {
  51. Log.Warning (string.Format("{0} is trying to load non-existant folder DefsOnDemand/{1}", content.Identifier, folder));
  52.  
  53. return;
  54. }
  55.  
  56. var list = DirectXmlLoader.XmlAssetsInModFolder (content, "DefsOnDemand/" + folder).ToList<LoadableXmlAsset> ();
  57. foreach (var xmlAsset in list) {
  58. foreach (var patch in content.Patches) {
  59. patch.Apply (xmlAsset.xmlDoc);
  60. }
  61. }
  62. for (int i = 0; i < list.Count; i++) {
  63. if (list [i] == null || list [i].xmlDoc == null || list [i].xmlDoc.DocumentElement == null) {
  64. Log.Error (string.Format ("{0}: unknown parse failure", list [i].fullFolderPath + "/" + list [i].name));
  65. } else if (list [i].xmlDoc.DocumentElement.Name != "Defs") {
  66. Log.Error (string.Format ("{0}: root element named {1}; should be named Defs", list [i].fullFolderPath + "/" + list [i].name, list [i].xmlDoc.DocumentElement.Name));
  67. }
  68. XmlInheritance.TryRegisterAllFrom (list [i], content);
  69. }
  70. XmlInheritance.Resolve ();
  71.  
  72. for (int j = 0; j < list.Count; j++) {
  73. var defPackage = new DefPackage (list [j].name, folder);
  74. foreach (var def in DirectXmlLoader.AllDefsFromAsset (list [j])) {
  75. defPackage.defs.Add (def);
  76. }
  77. content.AddDefPackage (defPackage);
  78. }
  79. }
  80. }
  81. }
Add Comment
Please, Sign In to add comment