Last active
June 26, 2017 16:49
-
-
Save notfood/79d1ff61d29f4787a83ae704234f7017 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Text; | |
using System.Xml; | |
using Harmony; | |
using Verse; | |
namespace ZPatches | |
{ | |
[HarmonyPatch (typeof (ModContentPack))] | |
[HarmonyPatch ("LoadDefs")] | |
[HarmonyPatch (new Type [] { typeof (IEnumerable<PatchOperation>) })] | |
public static class ModContentPack_Patch | |
{ | |
static FieldInfo defPackagesField = AccessTools.Field (typeof (ModContentPack), "defPackages"); | |
public static bool Prefix (ModContentPack __instance, IEnumerable<PatchOperation> patches) | |
{ | |
DeepProfiler.Start ("Loading all defs"); | |
var patchedDirectory = __instance.RootDir + "/Defs_Patched/"; | |
var patched = Directory.Exists (patchedDirectory); | |
List<LoadableXmlAsset> list; | |
if (patched && Directory.GetFiles(patchedDirectory).Count() > 0) { | |
list = DirectXmlLoader.XmlAssetsInModFolder (__instance, "Defs_Patched/").ToList<LoadableXmlAsset> (); | |
} else { | |
list = DirectXmlLoader.XmlAssetsInModFolder (__instance, "Defs/").ToList<LoadableXmlAsset> (); | |
foreach (LoadableXmlAsset current in list) { | |
foreach (PatchOperation current2 in patches) { | |
current2.Apply (current.xmlDoc); | |
} | |
} | |
DumpXml (__instance, list, patchedDirectory); | |
} | |
for (int i = 0; i < list.Count; i++) { | |
XmlInheritance.TryRegisterAllFrom (list [i], __instance); | |
} | |
XmlInheritance.Resolve (); | |
for (int j = 0; j < list.Count; j++) { | |
string relFolder = GenFilePaths.FolderPathRelativeToDefsFolder (list [j].fullFolderPath, __instance); | |
DefPackage defPackage = new DefPackage (list [j].name, relFolder); | |
foreach (Def current3 in DirectXmlLoader.AllDefsFromAsset (list [j])) { | |
defPackage.defs.Add (current3); | |
} | |
var defPackages = defPackagesField.GetValue (__instance) as List<DefPackage>; | |
defPackages.Add (defPackage); | |
} | |
DeepProfiler.End (); | |
return false; | |
} | |
public static void DumpXml(ModContentPack mcp, List<LoadableXmlAsset> list, string directoryPath) { | |
var directory = Directory.CreateDirectory (directoryPath); | |
var document = new XmlDocument (); | |
var rootXml = document.CreateElement ("Defs"); | |
var sb = new StringBuilder (); | |
sb.AppendLine (String.Format ("> {0} has {1} assets.", mcp.Name, list.Count)); | |
foreach (var asset in list) { | |
sb.AppendLine (String.Format("\t {0}/{1}", asset.fullFolderPath, asset.name)); | |
var child = document.ImportNode (asset.xmlDoc.DocumentElement.FirstChild, true); | |
rootXml.AppendChild (child); | |
} | |
document.AppendChild (rootXml); | |
Log.Message (sb.ToString()); | |
var writer = new XmlTextWriter (String.Format ("{0}/{1}.xml", directory.FullName, mcp.Name), Encoding.Unicode); | |
writer.Formatting = Formatting.Indented; | |
writer.WriteStartDocument (); | |
document.WriteContentTo (writer); | |
writer.WriteEndDocument (); | |
writer.Close (); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment