private void RegisterGlobals(XContainer root, OpcodeLookup lookup) { foreach (XElement element in root.Element("globals").Descendants("global")) { string name = XMLUtil.GetStringAttribute(element, "name"); if (name == "") { continue; } ushort opcode = (ushort)XMLUtil.GetNumericAttribute(element, "opcode"); string returnType = XMLUtil.GetStringAttribute(element, "type"); bool isNull = XMLUtil.GetBoolAttribute(element, "null", false); var info = new GlobalInfo(name, opcode, returnType, !isNull); lookup.RegisterGlobal(info); } }
private void RegisterValueTypes(XContainer root, OpcodeLookup lookup) { foreach (XElement element in root.Element("valueTypes").Descendants("type")) { string name = XMLUtil.GetStringAttribute(element, "name"); var opcode = (ushort)XMLUtil.GetNumericAttribute(element, "opcode"); int size = (int)XMLUtil.GetNumericAttribute(element, "size"); bool quoted = XMLUtil.GetBoolAttribute(element, "quoted", false); string tag = XMLUtil.GetStringAttribute(element, "tag", null); bool obj = XMLUtil.GetBoolAttribute(element, "object", false); var valueType = new ScriptValueType(name, opcode, size, quoted, tag, obj); foreach (XElement option in element.Descendants("enum")) { valueType.AddEnumValue(option.Value); } lookup.RegisterValueType(valueType); } }
private void RegisterFunctions(XContainer root, OpcodeLookup lookup) { foreach (XElement element in root.Element("functions").Descendants("function")) { string name = XMLUtil.GetStringAttribute(element, "name"); if (name == "") { continue; } var opcode = (ushort)XMLUtil.GetNumericAttribute(element, "opcode"); string returnType = XMLUtil.GetStringAttribute(element, "returnType", "void"); var flags = (uint)XMLUtil.GetNumericAttribute(element, "flags", 0); string[] parameterTypes = element.Descendants("arg").Select(e => XMLUtil.GetStringAttribute(e, "type")).ToArray(); var info = new ScriptFunctionInfo(name, opcode, returnType, flags, parameterTypes); lookup.RegisterFunction(info); } }
/// <summary> /// Parses an XML element and adds the field that it represents to a /// structure layout. /// </summary> /// <param name="layout">The layout to add the parsed field to.</param> /// <param name="element">The element to parse.</param> private static void HandleElement(StructureLayout layout, XElement element) { // Every structure field at least has a name and an offset string name = XMLUtil.GetStringAttribute(element, "name"); int offset = XMLUtil.GetNumericAttribute(element, "offset"); if (IsArrayElement(element)) { HandleArrayElement(layout, element, name, offset); } else if (IsRawElement(element)) { HandleRawElement(layout, element, name, offset); } else { HandleBasicElement(layout, element, name, offset); } }
/// <summary> /// Loads all of the structure layouts defined in an XML document. /// </summary> /// <param name="layoutDocument">The XML document to load structure layouts from.</param> /// <returns>The layouts that were loaded.</returns> private StructureLayoutCollection LoadLayouts(XDocument layoutDocument) { // Make sure there is a root <layouts> tag XContainer layoutContainer = layoutDocument.Element("layouts"); if (layoutContainer == null) { throw new ArgumentException("Invalid layout document"); } // Layout tags have the format: // <layout for="(layout's purpose)">(structure fields)</layout> var result = new StructureLayoutCollection(); foreach (XElement layout in layoutContainer.Elements("layout")) { string name = XMLUtil.GetStringAttribute(layout, "for"); int size = (int)XMLUtil.GetNumericAttribute(layout, "size", 0); result.AddLayout(name, LoadLayout(layout, size)); } return(result); }
/// <summary> /// Parses an XML element and adds the field that it represents to a /// structure layout. /// </summary> /// <param name="layout">The layout to add the parsed field to.</param> /// <param name="element">The element to parse.</param> private void HandleElement(StructureLayout layout, XElement element) { // Every structure field at least has a name and an offset string name = XMLUtil.GetStringAttribute(element, "name"); int offset = (int)XMLUtil.GetNumericAttribute(element, "offset"); switch (element.Name.LocalName) { case "array": HandleArrayElement(layout, element, name, offset); break; case "raw": HandleRawElement(layout, element, name, offset); break; case "struct": HandleStructElement(layout, element, name, offset); break; case "tagblock": HandleTagBlockElement(layout, element, name, offset); break; case "stringId": HandleStringIDElement(layout, element, name, offset); break; case "tagRef": HandleTagReferenceElement(layout, element, name, offset); break; default: HandleBasicElement(layout, element, name, offset); break; } }
/// <summary> /// Parses an XML element representing an raw byte array and adds the /// field information to a structure layout. /// </summary> /// <param name="layout">The structure layout to add the field's information to.</param> /// <param name="element">The XML element to parse.</param> /// <param name="name">The name of the field to add.</param> /// <param name="offset">The offset (in bytes) of the field from the beginning of the structure.</param> private void HandleRawElement(StructureLayout layout, XElement element, string name, int offset) { int size = (int)XMLUtil.GetNumericAttribute(element, "size"); layout.AddRawField(name, offset, size); }
private void HandleTagBlockElement(StructureLayout layout, XElement element, string name, int offset) { int entrySize = (int)XMLUtil.GetNumericAttribute(element, "entrySize"); layout.AddTagBlockField(name, offset, LoadLayout(element, entrySize)); }