Skip to content

Commit

Permalink
Support YFD XML import/export
Browse files Browse the repository at this point in the history
  • Loading branch information
alexguirre committed Jun 16, 2023
1 parent 9d76f2c commit 071251e
Show file tree
Hide file tree
Showing 7 changed files with 470 additions and 37 deletions.
44 changes: 44 additions & 0 deletions CodeWalker.Core/GameFiles/FileTypes/YfdFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CodeWalker.GameFiles
{
Expand Down Expand Up @@ -49,5 +50,48 @@ public void Load(byte[] data, RpfFileEntry entry)
FrameFilterDictionary = rd?.ReadBlock<FrameFilterDictionary>();

}

public byte[] Save()
{
byte[] data = ResourceBuilder.Build(FrameFilterDictionary, 4); //yfd is version 4...

return data;
}
}


public class YfdXml : MetaXmlBase
{
public static string GetXml(YfdFile yfd)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(XmlHeader);

if (yfd?.FrameFilterDictionary != null)
{
FrameFilterDictionary.WriteXmlNode(yfd.FrameFilterDictionary, sb, 0);
}

return sb.ToString();
}

}

public class XmlYfd
{
public static YfdFile GetYfd(string xml)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
return GetYfd(doc);
}

public static YfdFile GetYfd(XmlDocument doc)
{
YfdFile yfd = new YfdFile();
yfd.FrameFilterDictionary = new FrameFilterDictionary();
yfd.FrameFilterDictionary.ReadXml(doc.DocumentElement);
return yfd;
}
}
}
49 changes: 49 additions & 0 deletions CodeWalker.Core/GameFiles/GameFileCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ public void Init(Action<string> updateStatus, Action<string> errorLog)
//TestYwrs();
//TestYmaps();
//TestYpdbs();
//TestYfds();
//TestMrfs();
//TestFxcs();
//TestPlacements();
Expand Down Expand Up @@ -4517,6 +4518,54 @@ public void TestYpdbs()
}
}
}
public void TestYfds()
{
foreach (RpfFile file in AllRpfs)
{
foreach (RpfEntry entry in file.AllEntries)
{
var rfe = entry as RpfFileEntry;
if (rfe == null) continue;

try
{
if (rfe.NameLower.EndsWith(".yfd"))
{
UpdateStatus(string.Format(entry.Path));
YfdFile yfd = RpfMan.GetFile<YfdFile>(entry);
if (yfd != null)
{
if (yfd.FrameFilterDictionary != null)
{
// check that all signatures can be re-calculated
foreach (var f in yfd.FrameFilterDictionary.Filters.data_items)
{
if (f.Signature != f.CalculateSignature())
{ }
}
}

var xml = YfdXml.GetXml(yfd);
var yfd2 = XmlYfd.GetYfd(xml);
var data2 = yfd2.Save();
var yfd3 = new YfdFile();
RpfFile.LoadResourceFile(yfd3, data2, 4);//full roundtrip
var xml2 = YfdXml.GetXml(yfd3);
if (xml != xml2)
{ }
}
else
{ }
}
}
catch (Exception ex)
{
UpdateStatus("Error! " + ex.ToString());
}

}
}
}
public void TestMrfs()
{
foreach (RpfFile file in AllRpfs)
Expand Down
12 changes: 12 additions & 0 deletions CodeWalker.Core/GameFiles/MetaTypes/MetaXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public static string GetXml(RpfFileEntry e, byte[] data, out string filename, st
YpdbFile ypdb = RpfFile.GetFile<YpdbFile>(e, data);
return GetXml(ypdb, out filename);
}
else if (fnl.EndsWith(".yfd"))
{
YfdFile yfd = RpfFile.GetFile<YfdFile>(e, data);
return GetXml(yfd, out filename);
}
else if (fnl.EndsWith(".awc"))
{
AwcFile awc = RpfFile.GetFile<AwcFile>(e, data);
Expand Down Expand Up @@ -295,6 +300,12 @@ public static string GetXml(YpdbFile ypdb, out string filename)
filename = fn + ".xml";
return YpdbXml.GetXml(ypdb);
}
public static string GetXml(YfdFile yfd, out string filename)
{
var fn = (yfd?.Name) ?? "";
filename = fn + ".xml";
return YfdXml.GetXml(yfd);
}
public static string GetXml(AwcFile awc, out string filename, string outputfolder)
{
var fn = (awc?.Name) ?? "";
Expand Down Expand Up @@ -2243,6 +2254,7 @@ public enum MetaFormat
Heightmap = 21,
Ypdb = 22,
Mrf = 23,
Yfd = 24,
}

}
13 changes: 13 additions & 0 deletions CodeWalker.Core/GameFiles/MetaTypes/XmlMeta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public static byte[] GetData(XmlDocument doc, MetaFormat mformat, string fpathin
return GetHeightmapData(doc);
case MetaFormat.Ypdb:
return GetYpdbData(doc);
case MetaFormat.Yfd:
return GetYfdData(doc);
case MetaFormat.Mrf:
return GetMrfData(doc);
}
Expand Down Expand Up @@ -196,6 +198,12 @@ public static byte[] GetYpdbData(XmlDocument doc)
if (ypdb.WeightSet == null) return null;
return ypdb.Save();
}
public static byte[] GetYfdData(XmlDocument doc)
{
var yfd = XmlYfd.GetYfd(doc);
if (yfd.FrameFilterDictionary == null) return null;
return yfd.Save();
}
public static byte[] GetMrfData(XmlDocument doc)
{
var mrf = XmlMrf.GetMrf(doc);
Expand Down Expand Up @@ -231,6 +239,7 @@ public static string GetXMLFormatName(MetaFormat mformat)
case MetaFormat.Heightmap: return "Heightmap XML";
case MetaFormat.Ypdb: return "YPDB XML";
case MetaFormat.Mrf: return "MRF XML";
case MetaFormat.Yfd: return "YFD XML";
default: return "XML";
}
}
Expand Down Expand Up @@ -329,6 +338,10 @@ public static MetaFormat GetXMLFormat(string fnamel, out int trimlength)
{
mformat = MetaFormat.Ypdb;
}
if (fnamel.EndsWith(".yfd.xml"))
{
mformat = MetaFormat.Yfd;
}
if (fnamel.EndsWith(".mrf.xml"))
{
mformat = MetaFormat.Mrf;
Expand Down
Loading

0 comments on commit 071251e

Please sign in to comment.