From 1cbbc1a6e0777459b6413307029956e47ae00387 Mon Sep 17 00:00:00 2001 From: dexy Date: Sun, 27 Mar 2022 14:01:25 +1100 Subject: [PATCH] Refactor XML import into XmlMeta class to be more consistent with XML exporting --- .../GameFiles/MetaTypes/XmlMeta.cs | 288 ++++++++++++++++ CodeWalker/ExploreForm.cs | 311 +----------------- CodeWalker/Forms/MetaForm.cs | 83 +---- 3 files changed, 305 insertions(+), 377 deletions(-) diff --git a/CodeWalker.Core/GameFiles/MetaTypes/XmlMeta.cs b/CodeWalker.Core/GameFiles/MetaTypes/XmlMeta.cs index ddd6ed0..8a86b80 100644 --- a/CodeWalker.Core/GameFiles/MetaTypes/XmlMeta.cs +++ b/CodeWalker.Core/GameFiles/MetaTypes/XmlMeta.cs @@ -9,6 +9,294 @@ namespace CodeWalker.GameFiles { public class XmlMeta { + + + public static byte[] GetData(XmlDocument doc, MetaFormat mformat, string fpathin) + { + switch (mformat) + { + case MetaFormat.RSC: + return GetRSCData(doc); + case MetaFormat.PSO: + return GetPSOData(doc); + case MetaFormat.RBF: + return GetRBFData(doc); + case MetaFormat.AudioRel: + return GetRelData(doc); + case MetaFormat.Ynd: + return GetYndData(doc); + case MetaFormat.Ynv: + return GetYnvData(doc); + case MetaFormat.Ycd: + return GetYcdData(doc); + case MetaFormat.Ybn: + return GetYbnData(doc); + case MetaFormat.Ytd: + return GetYtdData(doc, fpathin); + case MetaFormat.Ydr: + return GetYdrData(doc, fpathin); + case MetaFormat.Ydd: + return GetYddData(doc, fpathin); + case MetaFormat.Yft: + return GetYftData(doc, fpathin); + case MetaFormat.Ypt: + return GetYptData(doc, fpathin); + case MetaFormat.Yld: + return GetYldData(doc, fpathin); + case MetaFormat.Yed: + return GetYedData(doc, fpathin); + case MetaFormat.Ywr: + return GetYwrData(doc, fpathin); + case MetaFormat.Yvr: + return GetYvrData(doc, fpathin); + case MetaFormat.Awc: + return GetAwcData(doc, fpathin); + case MetaFormat.CacheFile: + return GetCacheFileData(doc); + case MetaFormat.Heightmap: + return GetHeightmapData(doc); + } + return null; + } + public static byte[] GetRSCData(XmlDocument doc) + { + var meta = GetMeta(doc); + if ((meta.DataBlocks?.Data == null) || (meta.DataBlocks.Count == 0)) return null; + return ResourceBuilder.Build(meta, 2); //meta is RSC V:2 + } + public static byte[] GetPSOData(XmlDocument doc) + { + var pso = XmlPso.GetPso(doc); + if ((pso.DataSection == null) || (pso.DataMapSection == null) || (pso.SchemaSection == null)) return null; + return pso.Save(); + } + public static byte[] GetRBFData(XmlDocument doc) + { + var rbf = XmlRbf.GetRbf(doc); + if (rbf.current == null) return null; + return rbf.Save(); + } + public static byte[] GetRelData(XmlDocument doc) + { + var rel = XmlRel.GetRel(doc); + if ((rel.RelDatasSorted == null) || (rel.RelDatas == null)) return null; + return rel.Save(); + } + public static byte[] GetYndData(XmlDocument doc) + { + var ynd = XmlYnd.GetYnd(doc); + if (ynd.NodeDictionary == null) return null; + return ynd.Save(); + } + public static byte[] GetYnvData(XmlDocument doc) + { + var ynv = XmlYnv.GetYnv(doc); + if (ynv.Nav == null) return null; + return ynv.Save(); + } + public static byte[] GetYcdData(XmlDocument doc) + { + var ycd = XmlYcd.GetYcd(doc); + if (ycd.ClipDictionary == null) return null; + return ycd.Save(); + } + public static byte[] GetYbnData(XmlDocument doc) + { + var ybn = XmlYbn.GetYbn(doc); + if (ybn.Bounds == null) return null; + return ybn.Save(); + } + public static byte[] GetYtdData(XmlDocument doc, string fpathin) + { + var ytd = XmlYtd.GetYtd(doc, fpathin); + if (ytd.TextureDict == null) return null; + return ytd.Save(); + } + public static byte[] GetYdrData(XmlDocument doc, string fpathin) + { + var ydr = XmlYdr.GetYdr(doc, fpathin); + if (ydr.Drawable == null) return null; + return ydr.Save(); + } + public static byte[] GetYddData(XmlDocument doc, string fpathin) + { + var ydd = XmlYdd.GetYdd(doc, fpathin); + if (ydd.DrawableDict == null) return null; + return ydd.Save(); + } + public static byte[] GetYftData(XmlDocument doc, string fpathin) + { + var yft = XmlYft.GetYft(doc, fpathin); + if (yft.Fragment == null) return null; + return yft.Save(); + } + public static byte[] GetYptData(XmlDocument doc, string fpathin) + { + var ypt = XmlYpt.GetYpt(doc, fpathin); + if (ypt.PtfxList == null) return null; + return ypt.Save(); + } + public static byte[] GetYldData(XmlDocument doc, string fpathin) + { + var yld = XmlYld.GetYld(doc, fpathin); + if (yld.ClothDictionary == null) return null; + return yld.Save(); + } + public static byte[] GetYedData(XmlDocument doc, string fpathin) + { + var yed = XmlYed.GetYed(doc, fpathin); + if (yed.ExpressionDictionary == null) return null; + return yed.Save(); + } + public static byte[] GetYwrData(XmlDocument doc, string fpathin) + { + var ywr = XmlYwr.GetYwr(doc, fpathin); + if (ywr.Waypoints == null) return null; + return ywr.Save(); + } + public static byte[] GetYvrData(XmlDocument doc, string fpathin) + { + var yvr = XmlYvr.GetYvr(doc, fpathin); + if (yvr.Records == null) return null; + return yvr.Save(); + } + public static byte[] GetAwcData(XmlDocument doc, string fpathin) + { + var awc = XmlAwc.GetAwc(doc, fpathin); + if (awc.Streams == null) return null; + return awc.Save(); + } + public static byte[] GetCacheFileData(XmlDocument doc) + { + return null; //TODO!!! + } + public static byte[] GetHeightmapData(XmlDocument doc) + { + var hmf = XmlHmap.GetHeightmap(doc); + if (hmf.MaxHeights == null) return null; + return hmf.Save(); + } + + + public static string GetXMLFormatName(MetaFormat mformat) + { + switch (mformat) + { + case MetaFormat.RSC: return "Meta XML"; + case MetaFormat.PSO: return "PSO XML"; + case MetaFormat.RBF: return "RBF XML"; + case MetaFormat.AudioRel: return "REL XML"; + case MetaFormat.Ynd: return "YND XML"; + case MetaFormat.Ynv: return "YNV XML"; + case MetaFormat.Ycd: return "YCD XML"; + case MetaFormat.Ybn: return "YBN XML"; + case MetaFormat.Ytd: return "YTD XML"; + case MetaFormat.Ydr: return "YDR XML"; + case MetaFormat.Ydd: return "YDD XML"; + case MetaFormat.Yft: return "YFT XML"; + case MetaFormat.Ypt: return "YPT XML"; + case MetaFormat.Yld: return "YLD XML"; + case MetaFormat.Yed: return "YED XML"; + case MetaFormat.Ywr: return "YWR XML"; + case MetaFormat.Yvr: return "YVR XML"; + case MetaFormat.Awc: return "AWC XML"; + case MetaFormat.CacheFile: return "CacheFile XML"; + case MetaFormat.Heightmap: return "Heightmap XML"; + default: return "XML"; + } + } + public static MetaFormat GetXMLFormat(string fnamel, out int trimlength) + { + var mformat = MetaFormat.RSC; + trimlength = 4; + + if (!fnamel.EndsWith(".xml")) + { + mformat = MetaFormat.XML;//not really correct, but have to return something... + } + if (fnamel.EndsWith(".pso.xml")) + { + mformat = MetaFormat.PSO; + trimlength = 8; + } + if (fnamel.EndsWith(".rbf.xml")) + { + mformat = MetaFormat.RBF; + trimlength = 8; + } + if (fnamel.EndsWith(".rel.xml")) + { + mformat = MetaFormat.AudioRel; + } + if (fnamel.EndsWith(".ynd.xml")) + { + mformat = MetaFormat.Ynd; + } + if (fnamel.EndsWith(".ynv.xml")) + { + mformat = MetaFormat.Ynv; + } + if (fnamel.EndsWith(".ycd.xml")) + { + mformat = MetaFormat.Ycd; + } + if (fnamel.EndsWith(".ybn.xml")) + { + mformat = MetaFormat.Ybn; + } + if (fnamel.EndsWith(".ytd.xml")) + { + mformat = MetaFormat.Ytd; + } + if (fnamel.EndsWith(".ydr.xml")) + { + mformat = MetaFormat.Ydr; + } + if (fnamel.EndsWith(".ydd.xml")) + { + mformat = MetaFormat.Ydd; + } + if (fnamel.EndsWith(".yft.xml")) + { + mformat = MetaFormat.Yft; + } + if (fnamel.EndsWith(".ypt.xml")) + { + mformat = MetaFormat.Ypt; + } + if (fnamel.EndsWith(".yld.xml")) + { + mformat = MetaFormat.Yld; + } + if (fnamel.EndsWith(".yed.xml")) + { + mformat = MetaFormat.Yed; + } + if (fnamel.EndsWith(".ywr.xml")) + { + mformat = MetaFormat.Ywr; + } + if (fnamel.EndsWith(".yvr.xml")) + { + mformat = MetaFormat.Yvr; + } + if (fnamel.EndsWith(".awc.xml")) + { + mformat = MetaFormat.Awc; + } + if (fnamel.EndsWith("cache_y.dat.xml")) + { + mformat = MetaFormat.CacheFile; + } + if (fnamel.EndsWith(".dat.xml") && fnamel.StartsWith("heightmap")) + { + mformat = MetaFormat.Heightmap; + } + return mformat; + } + + + public static Meta GetMeta(XmlDocument doc) { MetaBuilder mb = new MetaBuilder(); diff --git a/CodeWalker/ExploreForm.cs b/CodeWalker/ExploreForm.cs index a78c984..07752ec 100644 --- a/CodeWalker/ExploreForm.cs +++ b/CodeWalker/ExploreForm.cs @@ -2703,92 +2703,15 @@ namespace CodeWalker var fname = fi.Name; var fnamel = fname.ToLowerInvariant(); var fpathin = fpath; - var mformat = MetaFormat.RSC; - var trimlength = 4; if (!fnamel.EndsWith(".xml")) { MessageBox.Show(fname + ": Not an XML file!", "Cannot import XML"); continue; } - if (fnamel.EndsWith(".pso.xml")) - { - mformat = MetaFormat.PSO; - trimlength = 8; - } - if (fnamel.EndsWith(".rbf.xml")) - { - mformat = MetaFormat.RBF; - trimlength = 8; - } - if (fnamel.EndsWith(".rel.xml")) - { - mformat = MetaFormat.AudioRel; - } - if (fnamel.EndsWith(".ynd.xml")) - { - mformat = MetaFormat.Ynd; - } - if (fnamel.EndsWith(".ynv.xml")) - { - mformat = MetaFormat.Ynv; - } - if (fnamel.EndsWith(".ycd.xml")) - { - mformat = MetaFormat.Ycd; - } - if (fnamel.EndsWith(".ybn.xml")) - { - mformat = MetaFormat.Ybn; - } - if (fnamel.EndsWith(".ytd.xml")) - { - mformat = MetaFormat.Ytd; - } - if (fnamel.EndsWith(".ydr.xml")) - { - mformat = MetaFormat.Ydr; - } - if (fnamel.EndsWith(".ydd.xml")) - { - mformat = MetaFormat.Ydd; - } - if (fnamel.EndsWith(".yft.xml")) - { - mformat = MetaFormat.Yft; - } - if (fnamel.EndsWith(".ypt.xml")) - { - mformat = MetaFormat.Ypt; - } - if (fnamel.EndsWith(".yld.xml")) - { - mformat = MetaFormat.Yld; - } - if (fnamel.EndsWith(".yed.xml")) - { - mformat = MetaFormat.Yed; - } - if (fnamel.EndsWith(".ywr.xml")) - { - mformat = MetaFormat.Ywr; - } - if (fnamel.EndsWith(".yvr.xml")) - { - mformat = MetaFormat.Yvr; - } - if (fnamel.EndsWith(".awc.xml")) - { - mformat = MetaFormat.Awc; - } - if (fnamel.EndsWith("cache_y.dat.xml")) - { - mformat = MetaFormat.CacheFile; - } - if (fnamel.EndsWith(".dat.xml") && fnamel.StartsWith("heightmap")) - { - mformat = MetaFormat.Heightmap; - } + + var trimlength = 4; + var mformat = XmlMeta.GetXMLFormat(fnamel, out trimlength); fname = fname.Substring(0, fname.Length - trimlength); fnamel = fnamel.Substring(0, fnamel.Length - trimlength); @@ -2802,228 +2725,7 @@ namespace CodeWalker doc.LoadXml(text); } - byte[] data = null; - - switch (mformat) - { - case MetaFormat.RSC: - { - var meta = XmlMeta.GetMeta(doc); - if ((meta.DataBlocks?.Data == null) || (meta.DataBlocks.Count == 0)) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import Meta XML"); - continue; - } - data = ResourceBuilder.Build(meta, 2); //meta is RSC V:2 - break; - } - case MetaFormat.PSO: - { - var pso = XmlPso.GetPso(doc); - if ((pso.DataSection == null) || (pso.DataMapSection == null) || (pso.SchemaSection == null)) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import PSO XML"); - continue; - } - data = pso.Save(); - break; - } - case MetaFormat.RBF: - { - var rbf = XmlRbf.GetRbf(doc); - if (rbf.current == null) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import RBF XML"); - continue; - } - data = rbf.Save(); - break; - } - case MetaFormat.AudioRel: - { - var rel = XmlRel.GetRel(doc); - if ((rel.RelDatasSorted == null) || (rel.RelDatas == null)) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import REL XML"); - continue; - } - data = rel.Save(); - break; - } - case MetaFormat.Ynd: - { - var ynd = XmlYnd.GetYnd(doc); - if (ynd.NodeDictionary == null) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import YND XML"); - continue; - } - data = ynd.Save(); - break; - } - case MetaFormat.Ynv: - { - var ynv = XmlYnv.GetYnv(doc); - if (ynv.Nav == null) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import YNV XML"); - continue; - } - data = ynv.Save(); - break; - } - case MetaFormat.Ycd: - { - var ycd = XmlYcd.GetYcd(doc); - if (ycd.ClipDictionary == null) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import YCD XML"); - continue; - } - data = ycd.Save(); - break; - } - case MetaFormat.Ybn: - { - var ybn = XmlYbn.GetYbn(doc); - if (ybn.Bounds == null) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import YBN XML"); - continue; - } - data = ybn.Save(); - break; - } - case MetaFormat.Ytd: - { - var ytd = XmlYtd.GetYtd(doc, fpathin); - if (ytd.TextureDict == null) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import YTD XML"); - continue; - } - data = ytd.Save(); - break; - } - case MetaFormat.Ydr: - { - var ydr = XmlYdr.GetYdr(doc, fpathin); - if (ydr.Drawable == null) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import YDR XML"); - continue; - } - data = ydr.Save(); - break; - } - case MetaFormat.Ydd: - { - var ydd = XmlYdd.GetYdd(doc, fpathin); - if (ydd.DrawableDict == null) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import YDD XML"); - continue; - } - data = ydd.Save(); - break; - } - case MetaFormat.Yft: - { - var yft = XmlYft.GetYft(doc, fpathin); - if (yft.Fragment == null) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import YFT XML"); - continue; - } - data = yft.Save(); - break; - } - case MetaFormat.Ypt: - { - var ypt = XmlYpt.GetYpt(doc, fpathin); - if (ypt.PtfxList == null) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import YPT XML"); - continue; - } - data = ypt.Save(); - break; - } - case MetaFormat.Yld: - { - var yld = XmlYld.GetYld(doc, fpathin); - if (yld.ClothDictionary == null) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import YLD XML"); - continue; - } - data = yld.Save(); - break; - } - case MetaFormat.Yed: - { - var yed = XmlYed.GetYed(doc, fpathin); - if (yed.ExpressionDictionary == null) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import YED XML"); - continue; - } - data = yed.Save(); - break; - } - case MetaFormat.Ywr: - { - var ywr = XmlYwr.GetYwr(doc, fpathin); - if (ywr.Waypoints == null) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import YWR XML"); - continue; - } - data = ywr.Save(); - break; - } - case MetaFormat.Yvr: - { - var yvr = XmlYvr.GetYvr(doc, fpathin); - if (yvr.Records == null) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import YVR XML"); - continue; - } - data = yvr.Save(); - break; - } - case MetaFormat.Awc: - { - var awc = XmlAwc.GetAwc(doc, fpathin); - if (awc.Streams == null) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import AWC XML"); - continue; - } - data = awc.Save(); - break; - } - case MetaFormat.CacheFile: - { - var cdf = new CacheDatFile(); - //cdf.LoadXml() //TODO!!! - MessageBox.Show(fname + ": CacheFile XML import still TODO!!!", "Cannot import CacheFile XML"); - break; - } - case MetaFormat.Heightmap: - { - var hmf = XmlHmap.GetHeightmap(doc); - if (hmf.MaxHeights == null) - { - MessageBox.Show(fname + ": Schema not supported.", "Cannot import Heightmap XML"); - continue; - } - data = hmf.Save(); - break; - } - } - + byte[] data = XmlMeta.GetData(doc, mformat, fpathin); if (data != null) { @@ -3038,7 +2740,10 @@ namespace CodeWalker CurrentFolder.EnsureFile(outfpath); } } - + else + { + MessageBox.Show(fname + ": Schema not supported.", "Cannot import " + XmlMeta.GetXMLFormatName(mformat)); + } } #if !DEBUG diff --git a/CodeWalker/Forms/MetaForm.cs b/CodeWalker/Forms/MetaForm.cs index 3e188fa..59e2b2a 100644 --- a/CodeWalker/Forms/MetaForm.cs +++ b/CodeWalker/Forms/MetaForm.cs @@ -386,83 +386,23 @@ namespace CodeWalker.Forms if (!(exploreForm?.EditMode ?? false)) return false; + if(metaFormat == MetaFormat.XML) return false;//what are we even doing here? + byte[] data = null; #if !DEBUG try #endif { - switch (metaFormat) + + data = XmlMeta.GetData(doc, metaFormat, string.Empty); + + if (data == null) { - default: - case MetaFormat.XML: return false;//what are we even doing here? - case MetaFormat.RSC: - var meta = XmlMeta.GetMeta(doc); - if ((meta.DataBlocks?.Data == null) || (meta.DataBlocks.Count == 0)) - { - MessageBox.Show("Schema not supported.", "Cannot import Meta XML"); - return false; - } - data = ResourceBuilder.Build(meta, 2); //meta is RSC "Version":2 (it's actually a type identifier, not a version!) - break; - case MetaFormat.PSO: - var pso = XmlPso.GetPso(doc); - if ((pso.DataSection == null) || (pso.DataMapSection == null) || (pso.SchemaSection == null)) - { - MessageBox.Show("Schema not supported.", "Cannot import PSO XML"); - return false; - } - data = pso.Save(); - break; - case MetaFormat.RBF: - var rbf = XmlRbf.GetRbf(doc); - if (rbf.current == null) - { - MessageBox.Show("Schema not supported.", "Cannot import RBF XML"); - return false; - } - data = rbf.Save(); - break; - case MetaFormat.Ynd: - var ynd = XmlYnd.GetYnd(doc); - if (ynd.NodeDictionary == null) - { - MessageBox.Show("Schema not supported.", "Cannot import YND XML"); - return false; - } - data = ynd.Save(); - break; - case MetaFormat.Yld: - var yld = XmlYld.GetYld(doc); - if (yld.ClothDictionary == null) - { - MessageBox.Show("Schema not supported.", "Cannot import YLD XML"); - return false; - } - data = yld.Save(); - break; - case MetaFormat.Yed: - var yed = XmlYed.GetYed(doc); - if (yed.ExpressionDictionary == null) - { - MessageBox.Show("Schema not supported.", "Cannot import YED XML"); - return false; - } - data = yed.Save(); - break; - case MetaFormat.CacheFile: - MessageBox.Show("Sorry, CacheFile import is not supported.", "Cannot import CacheFile XML"); - return false; - case MetaFormat.Heightmap: - var hmap = XmlHmap.GetHeightmap(doc); - if (hmap.MaxHeights == null) - { - MessageBox.Show("Schema not supported.", "Cannot import Heightmap XML"); - return false; - } - data = hmap.Save(); - break; + MessageBox.Show("Schema not supported.", "Cannot import " + XmlMeta.GetXMLFormatName(metaFormat)); + return false; } + } #if !DEBUG catch (Exception ex) @@ -471,11 +411,6 @@ namespace CodeWalker.Forms return false; } #endif - if (data == null) - { - MessageBox.Show("Schema not supported. (Unspecified error - data was null!)", "Cannot convert XML"); - return false; - } if (rpfFileEntry?.Parent != null) {