Added GetParsedArrayOfBytes WriteParsedArrayOfBytesNode to handle ArrayOfBytes when converting to/from XML

This commit is contained in:
Carmine 2018-03-01 23:09:15 +01:00
parent 0c558e746c
commit f5b8da104c
2 changed files with 158 additions and 9 deletions

View File

@ -189,15 +189,9 @@ namespace CodeWalker.GameFiles
break; break;
case MetaStructureEntryDataType.ArrayOfBytes: case MetaStructureEntryDataType.ArrayOfBytes:
OpenTag(sb, cind, ename, false);
var byteArrLen = (int)entry.ReferenceKey; WriteParsedArrayOfBytesNode(sb, cind, data, ename, eoffset, entry, arrEntry);
for (int n = 0; n < byteArrLen; n++)
{
var bidx = eoffset + n;
byte b = ((bidx >= 0) && (bidx < data.Length)) ? data[bidx] : (byte)0;
sb.Append(b.ToString("X").PadLeft(2, '0'));
}
CloseTag(sb, 0, ename);
break; break;
case MetaStructureEntryDataType.ArrayOfChars: case MetaStructureEntryDataType.ArrayOfChars:
OpenTag(sb, cind, ename, false); OpenTag(sb, cind, ename, false);
@ -429,6 +423,82 @@ namespace CodeWalker.GameFiles
} }
} }
private static void WriteParsedArrayOfBytesNode(StringBuilder sb, int indent, byte[] data, string ename, int eoffset, MetaStructureEntryInfo_s entry, MetaStructureEntryInfo_s arrEntry)
{
OpenTag(sb, indent, ename, false);
var byteArrLen = ((int)entry.ReferenceKey);
switch (arrEntry.DataType)
{
default:
case MetaStructureEntryDataType.SignedByte:
for (int n = 0; n < byteArrLen; n++)
{
var bidx = eoffset + n;
byte b = ((bidx >= 0) && (bidx < data.Length)) ? data[bidx] : (byte)0;
sb.Append(b.ToString("X").PadLeft(2, '0'));
}
break;
case MetaStructureEntryDataType.UnsignedByte:
for (int n = 0; n < byteArrLen; n++)
{
var bidx = eoffset + n;
byte b = ((bidx >= 0) && (bidx < data.Length)) ? data[bidx] : (byte)0;
sb.Append(b.ToString());
if (n < byteArrLen - 1) sb.Append(" ");
}
break;
case MetaStructureEntryDataType.SignedShort:
for (int n = 0; n < byteArrLen; n++)
{
var bidx = eoffset + n * 2;
short b = ((bidx >= 0) && (bidx < data.Length)) ? BitConverter.ToInt16(data, bidx) : (short)0;
sb.Append(b.ToString());
if (n < byteArrLen - 1) sb.Append(" ");
}
break;
case MetaStructureEntryDataType.UnsignedShort:
for (int n = 0; n < byteArrLen; n++)
{
var bidx = eoffset + n * 2;
ushort b = ((bidx >= 0) && (bidx < data.Length)) ? BitConverter.ToUInt16(data, bidx) : (ushort)0;
sb.Append(b.ToString());
if (n < byteArrLen - 1) sb.Append(" ");
}
break;
case MetaStructureEntryDataType.SignedInt:
for (int n = 0; n < byteArrLen; n++)
{
var bidx = eoffset + n * 4;
int b = ((bidx >= 0) && (bidx < data.Length)) ? BitConverter.ToInt32(data, bidx) : (int)0;
sb.Append(b.ToString());
if (n < byteArrLen - 1) sb.Append(" ");
}
break;
case MetaStructureEntryDataType.UnsignedInt:
for (int n = 0; n < byteArrLen; n++)
{
var bidx = eoffset + n * 4;
uint b = ((bidx >= 0) && (bidx < data.Length)) ? BitConverter.ToUInt32(data, bidx) : (uint)0;
sb.Append(b.ToString());
if (n < byteArrLen - 1) sb.Append(" ");
}
break;
case MetaStructureEntryDataType.Float:
for (int n = 0; n < byteArrLen; n++)
{
var bidx = eoffset + n * 4;
float b = ((bidx >= 0) && (bidx < data.Length)) ? BitConverter.ToSingle(data, bidx) : (float)0;
sb.Append(FloatUtil.ToString(b));
if (n < byteArrLen - 1) sb.Append(" ");
}
break;
}
CloseTag(sb, 0, ename);
}

View File

@ -77,6 +77,11 @@ namespace CodeWalker.GameFiles
} }
case MetaStructureEntryDataType.ArrayOfBytes: case MetaStructureEntryDataType.ArrayOfBytes:
{
GetParsedArrayOfBytes(cnode, data, entry, arrEntry);
break;
}
case MetaStructureEntryDataType.ArrayOfChars: case MetaStructureEntryDataType.ArrayOfChars:
{ {
int offset = entry.DataOffset; int offset = entry.DataOffset;
@ -288,6 +293,80 @@ namespace CodeWalker.GameFiles
return null; return null;
} }
private static void GetParsedArrayOfBytes(XmlNode node, byte[] data, MetaStructureEntryInfo_s entry, MetaStructureEntryInfo_s arrEntry)
{
int offset = entry.DataOffset;
string[] split;
switch (arrEntry.DataType)
{
default:
case MetaStructureEntryDataType.SignedByte:
split = Split(node.InnerText, 2);
for (int j = 0; j < split.Length; j++)
{
byte val = Convert.ToByte(split[j], 16);
data[offset] = val;
offset += sizeof(sbyte);
}
break;
case MetaStructureEntryDataType.UnsignedByte:
split = node.InnerText.Split();
for (int j = 0; j < split.Length; j++)
{
byte val = Convert.ToByte(split[j], 10);
data[offset] = val;
offset += sizeof(byte);
}
break;
case MetaStructureEntryDataType.SignedShort:
split = node.InnerText.Split();
for (int j = 0; j < split.Length; j++)
{
short val = Convert.ToInt16(split[j], 10);
Write(val, data, offset);
offset += sizeof(short);
}
break;
case MetaStructureEntryDataType.UnsignedShort:
split = node.InnerText.Split();
for (int j = 0; j < split.Length; j++)
{
ushort val = Convert.ToUInt16(split[j], 10);
Write(val, data, offset);
offset += sizeof(ushort);
}
break;
case MetaStructureEntryDataType.SignedInt:
split = node.InnerText.Split();
for (int j = 0; j < split.Length; j++)
{
int val = Convert.ToInt32(split[j], 10);
Write(val, data, offset);
offset += sizeof(int);
}
break;
case MetaStructureEntryDataType.UnsignedInt:
split = node.InnerText.Split();
for (int j = 0; j < split.Length; j++)
{
uint val = Convert.ToUInt32(split[j], 10);
Write(val, data, offset);
offset += sizeof(uint);
}
break;
case MetaStructureEntryDataType.Float:
split = node.InnerText.Split();
for (int j = 0; j < split.Length; j++)
{
float val = FloatUtil.Parse(split[j]);
Write(val, data, offset);
offset += sizeof(float);
}
break;
}
}
private static void TraverseArray(XmlNode node, MetaBuilder mb, MetaStructureEntryInfo_s arrEntry, int offset, ArrayResults results) private static void TraverseArray(XmlNode node, MetaBuilder mb, MetaStructureEntryInfo_s arrEntry, int offset, ArrayResults results)
{ {
switch (arrEntry.DataType) switch (arrEntry.DataType)