/// <summary> /// Loads a structure layout based upon an XML container's children. /// </summary> /// <param name="layoutTag">The collection of structure field tags to parse.</param> /// <returns>The structure layout that was loaded.</returns> public static StructureLayout LoadLayout(XContainer layoutTag) { StructureLayout layout = new StructureLayout(); foreach (XElement element in layoutTag.Elements()) HandleElement(layout, element); return layout; }
private IReader _reader; // The stream to read from #endregion Fields #region Constructors /// <summary> /// (private) Constructs a new StructureReader. /// </summary> /// <param name="reader">The IReader to read from.</param> /// <param name="layout">The structure layout to follow.</param> private StructureReader(IReader reader, StructureLayout layout) { _reader = reader; _layout = layout; _baseOffset = reader.Position; _offset = _baseOffset; _collection = new StructureValueCollection(); }
public ThirdGenLanguage(ThirdGenLanguageGlobals languageGlobals, StructureValueCollection values, IndexOffsetConverter converter, BuildInformation buildInfo) { _languageGlobals = languageGlobals; _pointerLayout = buildInfo.GetLayout("locale index table entry"); _encryptionKey = buildInfo.LocaleKey; _symbols = buildInfo.LocaleSymbols; _converter = converter; _pageSize = buildInfo.LocaleAlignment; Load(values, converter); }
/// <summary> /// Adds a layout to the collection, associating it with a certain name. /// </summary> /// <param name="name">The name to associate the layout with.</param> /// <param name="layout">The StructureLayout to add.</param> public void AddLayout(string name, StructureLayout layout) { _layouts[name] = layout; }
/// <summary> /// Parses an XML element representing an array field 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 static void HandleArrayElement(StructureLayout layout, XElement element, string name, int offset) { int count = GetNumericAttribute(element, "count"); int entrySize = GetNumericAttribute(element, "entrySize"); layout.AddArrayField(name, offset, count, entrySize, XMLLayoutLoader.LoadLayout(element)); }
/// <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 static void HandleRawElement(StructureLayout layout, XElement element, string name, int offset) { int size = GetNumericAttribute(element, "size"); layout.AddRawField(name, offset, size); }
/// <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 = GetStringAttribute(element, "name"); int offset = 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> /// Parses an XML element representing a basic structure field 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 static void HandleBasicElement(StructureLayout layout, XElement element, string name, int offset) { StructureValueType type = IdentifyValueType(element.Name.LocalName); layout.AddBasicField(name, type, offset); }
private List<IScript> LoadScripts(StructureValueCollection values, IReader reader, MetaAddressConverter addrConverter, IStringIDSource stringIDs, ExpressionTable expressions, StructureLayout entryLayout, BuildInformation buildInfo) { int script = (int)values.GetNumber("number of scripts"); ScriptsLocation = new Pointer(values.GetNumber("script table address"), addrConverter); // Read all of the script entries first, then go back and create the objects // ThirdGenScript reads parameters from its constructor - this may or may not need cleaning up to make this more obvious reader.SeekTo(ScriptsLocation.AsOffset()); List<StructureValueCollection> scriptData = new List<StructureValueCollection>(); for (int i = 0; i < script; i++) scriptData.Add(StructureReader.ReadStructure(reader, entryLayout)); List<IScript> result = new List<IScript>(); foreach (StructureValueCollection scriptValues in scriptData) result.Add(new ThirdGenScript(reader, scriptValues, addrConverter, stringIDs, expressions, buildInfo)); return result; }
private List<IGlobalObject> LoadScriptObjects(StructureValueCollection values, IReader reader, MetaAddressConverter addrConverter, IStringIDSource stringIDs, StructureLayout entryLayout) { int objectsCount = (int)values.GetNumber("number of script objects"); ScriptObjectsLocation = new Pointer(values.GetNumber("script object table address"), addrConverter); List<IGlobalObject> result = new List<IGlobalObject>(); reader.SeekTo(ScriptObjectsLocation.AsOffset()); for (int i = 0; i < objectsCount; i++) { StructureValueCollection objValues = StructureReader.ReadStructure(reader, entryLayout); result.Add(new ThirdGenGlobalObject(objValues, stringIDs)); } return result; }
private List<IGlobal> LoadScriptGlobals(StructureValueCollection values, IReader reader, MetaAddressConverter addrConverter, ExpressionTable expressions, StructureLayout entryLayout) { int globalsCount = (int)values.GetNumber("number of script globals"); ScriptGlobalsLocation = new Pointer(values.GetNumber("script global table address"), addrConverter); List<IGlobal> result = new List<IGlobal>(); reader.SeekTo(ScriptGlobalsLocation.AsOffset()); for (int i = 0; i < globalsCount; i++) { StructureValueCollection globalValues = StructureReader.ReadStructure(reader, entryLayout); result.Add(new ThirdGenGlobal(globalValues, expressions)); } return result; }
private void FindLanguageTable(BuildInformation buildInfo, out ITag tag, out StructureLayout layout) { // Check for a PATG tag, and if one isn't found, then use MATG tag = null; layout = null; if (buildInfo.HasLayout("patg")) { tag = FindTagByClass(PatgMagic); layout = buildInfo.GetLayout("patg"); } if (tag == null) { tag = FindTagByClass(MatgMagic); layout = buildInfo.GetLayout("matg"); } if (tag == null || layout == null) throw new InvalidOperationException("The cache file is missing locale information."); }
/// <summary> /// Reads an array of values from the stream and adds it to the value /// collection which is currently being built. /// </summary> /// <param name="name">The name to store the array under.</param> /// <param name="offset">The array's offset (in bytes) from the beginning of the structure.</param> /// <param name="count">The number of elements to read into the array.</param> /// <param name="entrySize">The size (in bytes) of each element in the array.</param> /// <param name="entryLayout">The layout to follow for each entry in the array.</param> public void VisitArrayField(string name, int offset, int count, int entrySize, StructureLayout entryLayout) { StructureValueCollection[] arrayValue = new StructureValueCollection[count]; for (int i = 0; i < count; i++) { _reader.SeekTo(_baseOffset + offset + i * entrySize); arrayValue[i] = ReadStructure(_reader, entryLayout); } _collection.SetArray(name, arrayValue); }
/// <summary> /// Reads a structure from a stream by following a predefined structure layout. /// </summary> /// <param name="reader">The IReader to read the structure from.</param> /// <param name="layout">The structure layout to follow.</param> /// <returns>A collection of the values that were read.</returns> /// <seealso cref="StructureLayout"/> public static StructureValueCollection ReadStructure(IReader reader, StructureLayout layout) { StructureReader structReader = new StructureReader(reader, layout); layout.Accept(structReader); return structReader._collection; }
/// <summary> /// Constructs a new array field. /// </summary> /// <param name="name">The name of the array.</param> /// <param name="offset">The offset (in bytes) of the array from the beginning of the structure.</param> /// <param name="count">The number of entries in the array.</param> /// <param name="entrySize">The size of each entry in the array.</param> /// <param name="entryLayout">The layout of each entry in the array.</param> public ArrayField(string name, int offset, int count, int entrySize, StructureLayout entryLayout) { _name = name; _offset = offset; _count = count; _entrySize = entrySize; _subLayout = entryLayout; }
/// <summary> /// Adds an array field to the structure layout. /// </summary> /// <param name="name">The name of the array.</param> /// <param name="offset">The offset (in bytes) of the array from the beginning of the structure.</param> /// <param name="count">The number of entries in the array.</param> /// <param name="entrySize">The size of each entry in the array.</param> /// <param name="entryLayout">The layout of each entry in the array.</param> public void AddArrayField(string name, int offset, int count, int entrySize, StructureLayout entryLayout) { _fields.Add(new ArrayField(name, offset, count, entrySize, entryLayout)); }
private ExpressionTable LoadScriptExpressions(StructureValueCollection values, IReader reader, MetaAddressConverter addrConverter, StringTableReader stringReader, StructureLayout entryLayout) { int exprCount = (int)values.GetNumber("number of script expressions"); ScriptExpressionsLocation = new Pointer(values.GetNumber("script expression table address"), addrConverter); ExpressionTable result = new ExpressionTable(); reader.SeekTo(ScriptExpressionsLocation.AsOffset()); for (int i = 0; i < exprCount; i++) { StructureValueCollection exprValues = StructureReader.ReadStructure(reader, entryLayout); result.AddExpression(new ThirdGenExpression(exprValues, (ushort)i, stringReader)); } foreach (IExpression expr in result) { // FIXME: hax if (expr != null) ((ThirdGenExpression)expr).ResolveReferences(result); } return result; }