Skip to content

Commit

Permalink
Linter pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauriethefish committed Oct 26, 2023
1 parent 4beb235 commit f410d7f
Show file tree
Hide file tree
Showing 74 changed files with 469 additions and 483 deletions.
18 changes: 9 additions & 9 deletions QuestPatcher.Axml/AxmlAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ public AxmlAttribute(string name, Uri? ns, int? resourceId, object value)

internal void PreparePooling(SavingContext ctx)
{
if(ResourceId != null)
if (ResourceId != null)
{
ctx.ResourceMap.Add(Name, (int) ResourceId);
}
else
{
ctx.StringPool.Add(Name);
}
if(Namespace != null)

if (Namespace != null)
{
ctx.StringPool.Add(Namespace.ToString());
}
Expand All @@ -113,8 +113,8 @@ internal void PreparePooling(SavingContext ctx)
{
ctx.StringPool.Add(wrappedValue.RawValue);
}
}
else if(Value is string asString)
}
else if (Value is string asString)
{
ctx.StringPool.Add(asString);
}
Expand All @@ -124,7 +124,7 @@ internal void Save(SavingContext ctx)
{
ctx.Writer.Write(Namespace == null ? -1 : ctx.StringPool.GetIndex(Namespace.ToString()));

if(ResourceId != null)
if (ResourceId != null)
{
int resourceIdIdx = ctx.ResourceMap.GetIndex(Name, (int) ResourceId);
ctx.Writer.Write(resourceIdIdx);
Expand All @@ -133,9 +133,9 @@ internal void Save(SavingContext ctx)
{
ctx.Writer.Write(ctx.StringPool.GetIndex(Name));
}

int rawStringIndex = -1;
int type = _valueType == null ? -1 : ((int)_valueType << 24) | 0x000008;
int type = _valueType == null ? -1 : ((int) _valueType << 24) | 0x000008;
int rawValue;
if (Value is WrappedValue wrappedValue)
{
Expand All @@ -156,7 +156,7 @@ internal void Save(SavingContext ctx)
}
else
{
rawValue = (int)Value;
rawValue = (int) Value;
}

ctx.Writer.Write(rawStringIndex);
Expand Down
32 changes: 16 additions & 16 deletions QuestPatcher.Axml/AxmlElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class AxmlElement
/// TODO: Automatically set this to a sensible value upon saving?
/// </summary>
public int OpeningTextLineNumber { get; set; }

/// <summary>
/// Axml text line number for the closing tag read from the parser.
/// </summary>
Expand Down Expand Up @@ -70,13 +70,13 @@ public AxmlElement(string name, Uri? namespaceUri = null, int openingTextLineNum
internal void PreparePooling(SavingContext ctx)
{
// First we need to write the namespaces declared within this element
foreach (KeyValuePair<string, Uri> pair in DeclaredNamespaces)
foreach (var pair in DeclaredNamespaces)
{
ctx.StringPool.Add(pair.Key);
ctx.StringPool.Add(pair.Value.ToString());
}

if(NamespaceUri != null)
if (NamespaceUri != null)
{
ctx.StringPool.Add(NamespaceUri.ToString());
}
Expand All @@ -88,12 +88,12 @@ internal void PreparePooling(SavingContext ctx)
Attributes.Sort((a, b) =>
{
int resourceIdDiff = (a.ResourceId ?? -1) - (b.ResourceId ?? -1);
if(resourceIdDiff != 0)
if (resourceIdDiff != 0)
{
return resourceIdDiff;
}

if(a.Namespace == null)
if (a.Namespace == null)
{
return b.Namespace == null ? 0 : -1;
}
Expand All @@ -102,13 +102,13 @@ internal void PreparePooling(SavingContext ctx)
return b.Namespace == null ? 1 : String.CompareOrdinal(a.Namespace.ToString(), b.Namespace.ToString());
}
});
foreach(AxmlAttribute attribute in Attributes)

foreach (var attribute in Attributes)
{
attribute.PreparePooling(ctx);
}

foreach (AxmlElement element in Children)
foreach (var element in Children)
{
element.PreparePooling(ctx);
}
Expand All @@ -117,7 +117,7 @@ internal void PreparePooling(SavingContext ctx)
internal void Save(SavingContext ctx)
{
// First we need to write the namespaces declared within this element
foreach (KeyValuePair<string, Uri> pair in DeclaredNamespaces)
foreach (var pair in DeclaredNamespaces)
{
ctx.Writer.WriteChunkHeader(ResourceType.XmlStartNamespace, 16); // Each namespace tag is 3 integers, so 3 * 4 = 12 bytes
ctx.Writer.Write(OpeningTextLineNumber);
Expand All @@ -139,8 +139,8 @@ internal void Save(SavingContext ctx)
short styleAttributeIndex = -1;
for (short i = 0; i < Attributes.Count; i++)
{
WrappedValue? wrappedValue = Attributes[i].Value as WrappedValue;
if(wrappedValue == null) { continue; }
var wrappedValue = Attributes[i].Value as WrappedValue;
if (wrappedValue == null) { continue; }

// Make sure to prevent multiple of these attributes, as this will save incorrectly
switch (wrappedValue.Type)
Expand All @@ -159,18 +159,18 @@ internal void Save(SavingContext ctx)
break;
}
}

ctx.Writer.Write((short) Attributes.Count);
// Stored indices are one above the actual ones
ctx.Writer.Write((short) (idAttributeIndex + 1));
ctx.Writer.Write((short) (classAttributeIndex + 1));
ctx.Writer.Write((short) (styleAttributeIndex + 1));
foreach(AxmlAttribute attribute in Attributes)
foreach (var attribute in Attributes)
{
attribute.Save(ctx);
}

foreach (AxmlElement child in Children)
foreach (var child in Children)
{
child.Save(ctx);
}
Expand All @@ -179,9 +179,9 @@ internal void Save(SavingContext ctx)
ctx.Writer.Write(0xFFFFFFFF);
ctx.Writer.Write(NamespaceUri == null ? -1 : ctx.StringPool.GetIndex(NamespaceUri.ToString()));
ctx.Writer.Write(ctx.StringPool.GetIndex(Name));

// End the namespaces stated by this element, as we have exited it
foreach (KeyValuePair<string, Uri> pair in DeclaredNamespaces.Reverse())
foreach (var pair in DeclaredNamespaces.Reverse())
{
ctx.Writer.WriteChunkHeader(ResourceType.XmlEndNamespace, 16); // Each namespace tag is 3 integers, so 3 * 4 = 12 bytes
ctx.Writer.Write(ClosingTextLineNumber);
Expand Down
51 changes: 27 additions & 24 deletions QuestPatcher.Axml/AxmlLoader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;

namespace QuestPatcher.Axml
{
Expand All @@ -21,7 +21,7 @@ public QueuedNamespace(string? prefix, Uri uri)
Uri = uri;
}
}

/// <summary>
/// Loads an AXML document from the given stream.
/// The stream must be seekable.
Expand All @@ -37,7 +37,7 @@ public static AxmlElement LoadDocument(Stream stream)
throw new ArgumentException("Cannot read axml from non-seekable stream");
}

BinaryReader input = new BinaryReader(stream);
var input = new BinaryReader(stream);
if (input.ReadResourceType() != ResourceType.Xml)
{
throw new AxmlParseException("Initial tag was not xml");
Expand All @@ -48,14 +48,14 @@ public static AxmlElement LoadDocument(Stream stream)
string[]? stringPool = null;
int[]? resourceMap = null;

Stack<AxmlElement> elementStack = new Stack<AxmlElement>();
List<QueuedNamespace> queuedNamespaces = new List<QueuedNamespace>();
var elementStack = new Stack<AxmlElement>();
var queuedNamespaces = new List<QueuedNamespace>();
AxmlElement? rootElement = null;

int preChunkPosition = 8; // Already gone past two ints for initial XML tag and file size
while(preChunkPosition < fileSize)
while (preChunkPosition < fileSize)
{
ResourceType chunkType = input.ReadResourceType();
var chunkType = input.ReadResourceType();
int chunkLength = input.ReadInt32();

if (stringPool == null && chunkType != ResourceType.StringPool)
Expand Down Expand Up @@ -93,8 +93,8 @@ public static AxmlElement LoadDocument(Stream stream)
string? prefix = prefixId == -1 ? null : stringPool[prefixId];

string uriString = stringPool[input.ReadInt32()];
Uri uri = ParseNamespaceUri(uriString);
var uri = ParseNamespaceUri(uriString);

queuedNamespaces.Add(new QueuedNamespace(prefix, uri));
break;
case ResourceType.XmlEndNamespace:
Expand All @@ -113,8 +113,8 @@ public static AxmlElement LoadDocument(Stream stream)
{
throw new AxmlParseException("Expected 0x00140014");
}
AxmlElement childElement = new AxmlElement(elementName, namespaceId == -1 ? null : ParseNamespaceUri(stringPool[namespaceId]), currentLineNumber);

var childElement = new AxmlElement(elementName, namespaceId == -1 ? null : ParseNamespaceUri(stringPool[namespaceId]), currentLineNumber);

int numAttributes = input.ReadInt16();
int idAttributeIndex = input.ReadInt16() - 1;
Expand All @@ -123,13 +123,13 @@ public static AxmlElement LoadDocument(Stream stream)
for (int i = 0; i < numAttributes; i++)
{
int attrNamespaceId = input.ReadInt32();
Uri? attrNamespace = attrNamespaceId == -1 ? null : ParseNamespaceUri(stringPool[attrNamespaceId]);
var attrNamespace = attrNamespaceId == -1 ? null : ParseNamespaceUri(stringPool[attrNamespaceId]);

int attrNameAndResourceIdIndex = input.ReadInt32();

string attrName = stringPool[attrNameAndResourceIdIndex];
int? attrResourceId = null;

if (resourceMap == null)
{
throw new AxmlParseException(
Expand All @@ -141,28 +141,31 @@ public static AxmlElement LoadDocument(Stream stream)
}

int attrRawStringIndex = input.ReadInt32();
AttributeType attrType = (AttributeType) (input.ReadInt32() >> 24); // The first byte contains the actual type, so we shift this to the right
var attrType = (AttributeType) (input.ReadInt32() >> 24); // The first byte contains the actual type, so we shift this to the right
int attrRawValue = input.ReadInt32();

object value;
if (i == idAttributeIndex)
{
value = new WrappedValue(WrappedValueType.Id, stringPool[attrRawStringIndex], attrRawValue);
}
else if(i == classAttributeIndex)
else if (i == classAttributeIndex)
{
value = new WrappedValue(WrappedValueType.Class, stringPool[attrRawStringIndex], attrRawValue);
} else if (i == styleAttributeIndex)
}
else if (i == styleAttributeIndex)
{
value = new WrappedValue(WrappedValueType.Style, stringPool[attrRawStringIndex], attrRawValue);
} else if (attrType == AttributeType.Reference)
}
else if (attrType == AttributeType.Reference)
{
value = new WrappedValue(WrappedValueType.Reference, null, attrRawValue);
}
else if(attrType == AttributeType.String)
else if (attrType == AttributeType.String)
{
value = stringPool[attrRawValue];
} else if (attrType == AttributeType.Boolean)
}
else if (attrType == AttributeType.Boolean)
{
value = attrRawValue != 0;
}
Expand All @@ -175,7 +178,7 @@ public static AxmlElement LoadDocument(Stream stream)
}

// Add the namespaces of any parent StartNamespace resources to this element
foreach (QueuedNamespace ns in queuedNamespaces)
foreach (var ns in queuedNamespaces)
{
if (ns.Prefix == null) // No prefix means that this is a default namespace
{
Expand Down Expand Up @@ -203,8 +206,8 @@ public static AxmlElement LoadDocument(Stream stream)
}
// Set this element as the bottom-most element
elementStack.Push(childElement);


break;
case ResourceType.XmlEndElement:
int lineNumber = input.ReadInt32();
Expand All @@ -218,7 +221,7 @@ public static AxmlElement LoadDocument(Stream stream)
}

input.ReadInt32(); // TODO: ID of "text" value. Currently unused

// TODO: Unused bytes. (figure out what they are)
input.ReadInt32();
input.ReadInt32();
Expand Down
16 changes: 8 additions & 8 deletions QuestPatcher.Axml/AxmlSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,41 @@ public static class AxmlSaver
/// <param name="rootElement">Root element of the document</param>
public static void SaveDocument(Stream stream, AxmlElement rootElement)
{
BinaryWriter mainOutput = new BinaryWriter(stream);
var mainOutput = new BinaryWriter(stream);

// Write the main elements chunk of the file to a MemoryStream first
SavingContext ctx = new SavingContext();
var ctx = new SavingContext();
rootElement.PreparePooling(ctx);
string[] stringPool = ctx.StringPool.PrepareForSavePhase(ctx.ResourceMap);

rootElement.Save(ctx);
MemoryStream mainChunkStream = (MemoryStream) ctx.Writer.BaseStream;
var mainChunkStream = (MemoryStream) ctx.Writer.BaseStream;


int[] resourcePool = ctx.ResourceMap.Save();

int stringPoolLength = StringPoolSerializer.CalculatePoolLength(stringPool);
int stringPoolPadding = (4 - stringPoolLength % 4) % 4;
stringPoolLength += stringPoolPadding; // Add padding to four bytes

int resourcePoolLength = resourcePool.Length * 4; // Each pool item is an integer

// The length of the main xml tag is that of the whole file, so also including the string pool, resource pool, and main chunk. (+ extra 8 + 8 = 16 bytes for string pool and resource pool header)
mainOutput.WriteChunkHeader(ResourceType.Xml, stringPoolLength + resourcePoolLength + (int) mainChunkStream.Position + 16);

mainOutput.WriteChunkHeader(ResourceType.StringPool, stringPoolLength);
StringPoolSerializer.SaveStringPool(stringPool, mainOutput);
for (int i = 0; i < stringPoolPadding; i++)
{
mainOutput.Write((byte) 0);
}

mainOutput.WriteChunkHeader(ResourceType.XmlResourceMap, resourcePoolLength);
foreach (int resource in resourcePool)
{
mainOutput.Write(resource);
}

// Save the main chunk of the file
mainChunkStream.Position = 0;
mainChunkStream.CopyTo(stream);
Expand Down
Loading

0 comments on commit f410d7f

Please sign in to comment.