Skip to content

Commit

Permalink
Enabled the XML to XMB and improved the performance of Xmb.Save more.
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo committed Oct 16, 2018
1 parent c29dfe5 commit 7ca307d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
29 changes: 20 additions & 9 deletions FFXV.Services/Xmb.Write.cs
Expand Up @@ -21,6 +21,9 @@ private class StringEntry
}

private Header header;
public Dictionary<int, int> dicElements = new Dictionary<int, int>();
public Dictionary<int, int> dicAttributes = new Dictionary<int, int>();
public Dictionary<int, int> dicVariants = new Dictionary<int, int>();
private List<Element> elements = new List<Element>();
private List<Attribute> attributes = new List<Attribute>();
private List<Variant> variants = new List<Variant>();
Expand Down Expand Up @@ -63,7 +66,7 @@ public int ReadElement(XElement xmlElement)
elementStrType = xmlAttribute.Value;
}

attributeIndexes.Add(AddComparable(attributes, attribute));
attributeIndexes.Add(AddComparable(dicAttributes, attributes, attribute));
element.AttributeCount++;
}
}
Expand All @@ -88,7 +91,7 @@ public int ReadElement(XElement xmlElement)

if (OptimizeSize)
{
return AddComparable(elements, element);
return AddComparable(dicElements, elements, element);
}

elements.Add(element);
Expand Down Expand Up @@ -246,7 +249,7 @@ public void Compare(Xmb xmb)

private int GetOrAddVariantIndex(ValueType type, string value)
{
return AddComparable(variants, new Variant()
return AddComparable(dicVariants, variants, new Variant()
{
Type = type,
NameStringOffset = AddString(value),
Expand All @@ -272,17 +275,25 @@ private int AddString(string str)
return value;
}

private static int AddComparable<T>(List<T> collection, T item)
private static int AddComparable<T>(Dictionary<int, int> dictionary, List<T> collection, T item)
where T : IComparable<T>
{
for (int i = 0; i < collection.Count; i++)
AddComparable(dictionary, collection, item, out var result);
return result;
}

private static bool AddComparable<T>(Dictionary<int, int> dictionary, List<T> collection, T item, out int index)
where T : IComparable<T>
{
var hash = item.GetHashCode();
if (!dictionary.TryGetValue(hash, out index))
{
if (collection[i].CompareTo(item) == 0)
return i;
dictionary[index] = index = collection.Count;
collection.Add(item);
return true;
}

collection.Add(item);
return collection.Count - 1;
return false;
}

public static ValueType GetTypeFromAttribute(string type, string value)
Expand Down
36 changes: 24 additions & 12 deletions FFXV.Services/Xmb.cs
Expand Up @@ -101,6 +101,15 @@ public class Element : IComparable<Element>

public override string ToString() => Name;

public override int GetHashCode()
{
return AttributeTableIndex ^
(AttributeCount * 433) ^ (ElementCount * 65729) ^
(ElementTableIndex << 13 | (ElementTableIndex >> 19)) ^
(NameStringOffset << 26 | (NameStringOffset >> 6)) ^
(VariantOffset << 7 | (VariantOffset >> 25));
}

public static Element Read(BinaryReader reader)
{
return new Element()
Expand Down Expand Up @@ -128,10 +137,9 @@ public void Write(BinaryWriter writer)

public int CompareTo(Element other)
{
return Reserved == other.Reserved &&
AttributeTableIndex == other.AttributeTableIndex &&
AttributeCount == other.AttributeCount &&
return AttributeTableIndex == other.AttributeTableIndex &&
ElementTableIndex == other.ElementTableIndex &&
AttributeCount == other.AttributeCount &&
ElementCount == other.ElementCount &&
NameStringOffset == other.NameStringOffset &&
VariantOffset == other.VariantOffset ?
Expand All @@ -149,6 +157,11 @@ public class Attribute : IComparable<Attribute>

public override string ToString() => Name;

public override int GetHashCode()
{
return NameStringOffset | (VariantOffset << 16);
}

public static Attribute Read(BinaryReader reader)
{
return new Attribute()
Expand All @@ -168,8 +181,7 @@ public void Write(BinaryWriter writer)

public int CompareTo(Attribute other)
{
return Reserved == other.Reserved &&
NameStringOffset == other.NameStringOffset &&
return NameStringOffset == other.NameStringOffset &&
VariantOffset == other.VariantOffset ?
0 : 1;
}
Expand All @@ -188,6 +200,11 @@ public class Variant : IComparable<Variant>

public override string ToString() => Name;

public override int GetHashCode()
{
return NameStringOffset | ((int)Type << 24);
}

public static Variant Read(BinaryReader reader)
{
return new Variant()
Expand All @@ -213,13 +230,8 @@ public void Write(BinaryWriter writer)

public int CompareTo(Variant other)
{
return Type == other.Type &&
NameStringOffset == other.NameStringOffset &&
Value1 == other.Value1 &&
Value2 == other.Value2 &&
Value3 == other.Value3 &&
Value4 == other.Value4 &&
Name == other.Name ?
return NameStringOffset == other.NameStringOffset &&
Type == other.Type ?
0 : 1;
}
}
Expand Down
12 changes: 9 additions & 3 deletions FFXV.Tools.Xmb/Program.cs
Expand Up @@ -47,11 +47,17 @@ private void OnExecute()
{
if (IsDirectory)
{
Export(Input);
if (IsExport)
Export(Input);
else
Import(Input);
}
else
{
Export(Input, Output);
if (IsExport)
Export(Input, Output);
else
Import(Input, Output);
}
}

Expand Down Expand Up @@ -116,7 +122,7 @@ private void Import(string input, string output)
{
using (var outStream = File.Open(output, FileMode.Create))
{
Xmb.Save(inStream, XDocument.Load(inStream).Root);
Xmb.Save(outStream, XDocument.Load(inStream).Root);
}
}
}
Expand Down

0 comments on commit 7ca307d

Please sign in to comment.