public BinaryReader BeginDeserialize(TagStructureInfo info) { var data = _cache.ExtractTagRaw(_stream, Tag); var reader = new BinaryReader(new MemoryStream(data)); reader.BaseStream.Position = Tag.MainStructOffset; return reader; }
public void EndSerialize(TagStructureInfo info, byte[] data, uint mainStructOffset) { _data.MainStructOffset = mainStructOffset; _data.Data = data; _cache.SetTagData(_stream, Tag, _data); _data = null; }
public void EndSerialize(TagStructureInfo info, byte[] data, uint mainStructOffset) { _resource.DefinitionFixups.Clear(); _resource.D3DObjectFixups.Clear(); _resource.DefinitionFixups.AddRange(_fixups); _resource.D3DObjectFixups.AddRange(_d3dFixups); _resource.DefinitionData = data; _resource.DefinitionAddress = new ResourceAddress(ResourceAddressType.Definition, (int)mainStructOffset); }
public void BeginSerialize(TagStructureInfo info) { _dataFixups.Clear(); _resourceFixups.Clear(); _dependencies.Clear(); Tag.GroupTag = info.GroupTag; Tag.ParentGroupTag = info.ParentGroupTag; Tag.GrandparentGroupTag = info.GrandparentGroupTag; Tag.GroupName = (info.Structure.Name != null) ? _stringIds.GetStringId(info.Structure.Name) : StringId.Null; }
public ListFieldsCommand(OpenTagCache info, TagStructureInfo structure, object value) : base(CommandFlags.Inherit, "ListFields", $"Lists the fields in the current {structure.Types[0].Name} definition.", "ListFields", $"Lists the fields in the current {structure.Types[0].Name} definition.") { Info = info; Structure = structure; Value = value; }
public void BeginSerialize(TagStructureInfo info) { _data = new TagData { Group = new TagGroup ( tag: info.GroupTag, parentTag: info.ParentGroupTag, grandparentTag: info.GrandparentGroupTag, name: (info.Structure.Name != null) ? _stringIds.GetStringId(info.Structure.Name) : StringId.Null ), }; }
public SetFieldCommand(CommandContextStack stack, OpenTagCache info, TagInstance tag, TagStructureInfo structure, object owner) : base(CommandFlags.Inherit, "SetField", $"Sets the value of a specific field in the current {structure.Types[0].Name} definition.", "SetField <field name> <field value>", $"Sets the value of a specific field in the current {structure.Types[0].Name} definition.") { Stack = stack; Info = info; Tag = tag; Structure = structure; Owner = owner; }
public EditBlockCommand(CommandContextStack stack, OpenTagCache info, TagInstance tag, object value) : base(CommandFlags.Inherit, "Edit", "Edit the fields of a particular block element.", "Edit <block name> [block index (if block)]", "Edit the fields of a particular block element.") { Info = info; Stack = stack; Tag = tag; Structure = new TagStructureInfo(value.GetType(), Info.Version); Owner = value; }
public PasteElementsCommand(CommandContextStack stack, OpenTagCache info, TagInstance tag, TagStructureInfo structure, object owner) : base(CommandFlags.Inherit, "PasteElements", $"Pastes block element(s) to a specific tag block in the current {structure.Types[0].Name} definition.", "PasteElements <tag block name> [index = *]", $"Pastes block element(s) to a specific tag block in the current {structure.Types[0].Name} definition.") { Stack = stack; Info = info; Tag = tag; Structure = structure; Owner = owner; }
public AddToBlockCommand(CommandContextStack stack, OpenTagCache info, TagInstance tag, TagStructureInfo structure, object owner) : base(CommandFlags.Inherit, "AddTo", $"Adds block element(s) to the end of a specific tag block in the current {structure.Types[0].Name} definition.", "AddTo <tag block name> [amount = 1]", $"Adds block element(s) to the end of a specific tag block in the current {structure.Types[0].Name} definition.") { Stack = stack; Info = info; Tag = tag; Structure = structure; Owner = owner; }
public RemoveFromBlockCommand(CommandContextStack stack, OpenTagCache info, TagInstance tag, TagStructureInfo structure, object owner) : base(CommandFlags.Inherit, "RemoveFrom", $"Removes block element(s) from a specified index of a specific tag block in the current {structure.Types[0].Name} definition.", "RemoveFrom <tag block name> [* | <tag block index> [* | amount = 1]]", $"Removes block element(s) from a specified index of a specific tag block in the current {structure.Types[0].Name} definition.") { Stack = stack; Info = info; Tag = tag; Structure = structure; Owner = owner; }
/// <summary> /// Serializes a tag structure into a context. /// </summary> /// <param name="context">The serialization context to use.</param> /// <param name="tagStructure">The tag structure.</param> public void Serialize(ISerializationContext context, object tagStructure) { // Serialize the structure to a data block var info = new TagStructureInfo(tagStructure.GetType(), _version); context.BeginSerialize(info); var tagStream = new MemoryStream(); var structBlock = context.CreateBlock(); SerializeStruct(context, tagStream, structBlock, info, tagStructure); // Finalize the block and write all of the tag data out var mainStructOffset = structBlock.Finalize(tagStream); var data = tagStream.ToArray(); context.EndSerialize(info, data, mainStructOffset); }
/// <summary> /// Serializes a structure into a temporary memory block. /// </summary> /// <param name="context">The serialization context to use.</param> /// <param name="tagStream">The stream to write completed blocks of tag data to.</param> /// <param name="block">The temporary block to write incomplete tag data to.</param> /// <param name="info">Information about the tag structure type.</param> /// <param name="structure">The structure to serialize.</param> /// <exception cref="System.InvalidOperationException">Structure type must have TagStructureAttribute</exception> private void SerializeStruct(ISerializationContext context, MemoryStream tagStream, IDataBlock block, TagStructureInfo info, object structure) { var baseOffset = block.Stream.Position; var enumerator = new TagFieldEnumerator(info); while (enumerator.Next()) SerializeProperty(context, tagStream, block, structure, enumerator, baseOffset); // Honor the struct size if it's defined if (info.TotalSize > 0) { block.Stream.Position = baseOffset + info.TotalSize; if (block.Stream.Position > block.Stream.Length) block.Stream.SetLength(block.Stream.Position); } // Honor alignment if (info.Structure.Align > 0) block.SuggestAlignment(info.Structure.Align); }
public BinaryReader BeginDeserialize(TagStructureInfo info) { if (_resource.DefinitionAddress.Value == 0 || _resource.DefinitionAddress.Type != ResourceAddressType.Definition) throw new InvalidOperationException("Invalid resource definition address"); // Create a stream with a copy of the resource definition data var stream = new MemoryStream(_resource.DefinitionData.Length); stream.Write(_resource.DefinitionData, 0, _resource.DefinitionData.Length); // Apply fixups var writer = new BinaryWriter(stream); foreach (var fixup in _resource.DefinitionFixups) { stream.Position = fixup.DefinitionDataOffset; writer.Write(fixup.Address.Value); } stream.Position = _resource.DefinitionAddress.Offset; return new BinaryReader(stream); }
/// <summary> /// Constructs an enumerator over a tag structure. /// </summary> /// <param name="info">The info for the structure. Only fields which match the version used to create the info will be enumerated over.</param> public TagFieldEnumerator(TagStructureInfo info) { Info = info; Begin(); }
/// <summary> /// Deserializes a structure. /// </summary> /// <param name="reader">The reader.</param> /// <param name="context">The serialization context to use.</param> /// <param name="info">Information about the structure to deserialize.</param> /// <returns>The deserialized structure.</returns> /// <exception cref="System.InvalidOperationException">Target type must have TagStructureAttribute</exception> private object DeserializeStruct(BinaryReader reader, ISerializationContext context, TagStructureInfo info) { var baseOffset = reader.BaseStream.Position; var instance = Activator.CreateInstance(info.Types[0]); var enumerator = new TagFieldEnumerator(info); while (enumerator.Next()) { DeserializeProperty(reader, context, instance, enumerator, baseOffset); } if (enumerator.Info.TotalSize > 0) { reader.BaseStream.Position = baseOffset + enumerator.Info.TotalSize; } return(instance); }
public void EndDeserialize(TagStructureInfo info, object obj) { }
public void BeginSerialize(TagStructureInfo info) { _fixups.Clear(); _d3dFixups.Clear(); }
public static CommandContext Create(CommandContextStack stack, OpenTagCache info, TagInstance tag) { var groupName = info.StringIds.GetString(tag.Group.Name); var context = new CommandContext(stack.Context, string.Format("0x{0:X4}.{1}", tag.Index, groupName)); switch (tag.Group.Tag.ToString()) { case "vfsl": // vfiles_list EditVFilesList(context, info, tag); break; case "unic": // multilingual_unicode_string_list EditMultilingualUnicodeStringList(context, info, tag); break; case "bitm": // bitmap EditBitmap(context, info, tag); break; case "hlmt": // model EditModel(context, info, tag); break; case "mode": // render_model EditRenderModel(context, info, tag); break; case "rm ": // render_method case "rmsh": // shader case "rmd ": // shader_decal case "rmfl": // shader_foliage case "rmhg": // shader_halogram case "rmss": // shader_screen case "rmtr": // shader_terrain case "rmw ": // shader_water case "rmzo": // shader_zonly case "rmcs": // shader_custom EditRenderMethod(context, info, tag); break; case "scnr": EditScenario(context, info, tag); break; } object value = null; using (var stream = info.OpenCacheRead()) value = info.Deserializer.Deserialize( new TagSerializationContext(stream, info.Cache, info.StringIds, tag), TagStructureTypes.FindByGroupTag(tag.Group.Tag)); var structure = new TagStructureInfo( TagStructureTypes.FindByGroupTag(tag.Group.Tag)); context.AddCommand(new ListFieldsCommand(info, structure, value)); context.AddCommand(new SetFieldCommand(stack, info, tag, structure, value)); context.AddCommand(new EditBlockCommand(stack, info, tag, value)); context.AddCommand(new AddToBlockCommand(stack, info, tag, structure, value)); context.AddCommand(new RemoveFromBlockCommand(stack, info, tag, structure, value)); context.AddCommand(new SaveChangesCommand(info, tag, value)); context.AddCommand(new ExitToCommand(stack)); return context; }
/// <summary> /// Serializes a structure into a temporary memory block. /// </summary> /// <param name="context">The serialization context to use.</param> /// <param name="tagStream">The stream to write completed blocks of tag data to.</param> /// <param name="block">The temporary block to write incomplete tag data to.</param> /// <param name="info">Information about the tag structure type.</param> /// <param name="structure">The structure to serialize.</param> /// <exception cref="System.InvalidOperationException">Structure type must have TagStructureAttribute</exception> private void SerializeStruct(ISerializationContext context, MemoryStream tagStream, IDataBlock block, TagStructureInfo info, object structure) { var baseOffset = block.Stream.Position; var enumerator = new TagFieldEnumerator(info); while (enumerator.Next()) { SerializeProperty(context, tagStream, block, structure, enumerator, baseOffset); } // Honor the struct size if it's defined if (info.TotalSize > 0) { block.Stream.Position = baseOffset + info.TotalSize; if (block.Stream.Position > block.Stream.Length) { block.Stream.SetLength(block.Stream.Position); } } // Honor alignment if (info.Structure.Align > 0) { block.SuggestAlignment(info.Structure.Align); } }
public override bool Execute(List<string> args) { if (args.Count < 1 || args.Count > 2) return false; var blockName = args[0]; var ownerType = Owner.GetType(); var enumerator = new TagFieldEnumerator(Structure); var deferredNames = new List<string>(); var deferredArgs = new List<string>(); if (blockName.Contains(".")) { deferredNames.AddRange(blockName.Split('.')); blockName = deferredNames[0]; deferredNames = deferredNames.Skip(1).ToList(); deferredArgs.AddRange(args.Skip(1)); args = new List<string> { blockName }; } if (blockName.Contains("]")) { var openBracketIndex = blockName.IndexOf('['); var closeBracketIndex = blockName.IndexOf(']'); var name = blockName.Substring(0, openBracketIndex); var index = blockName.Substring(openBracketIndex + 1, (closeBracketIndex - openBracketIndex) - 1); blockName = name; args = new List<string> { name, index }; } var blockNameLow = blockName.ToLower(); var field = enumerator.Find(f => f.Name == blockName || f.Name.ToLower() == blockNameLow); if (field == null) { Console.WriteLine("{0} does not contain a block named \"{1}\"", ownerType.Name, blockName); return false; } var contextName = ""; object blockValue = null; var structureAttribute = field.FieldType.CustomAttributes.ToList().Find(a => a.AttributeType == typeof(TagStructureAttribute)); if (structureAttribute != null) { if (args.Count != 1) return false; blockValue = field.GetValue(Owner); contextName = $"{blockName}"; } else { if (args.Count != 2) return false; IList fieldValue = null; if (field.FieldType.GetInterface("IList") == null || (fieldValue = (IList)field.GetValue(Owner)) == null) { Console.WriteLine("{0} does not contain a block named \"{1}\"", ownerType.Name, blockName); return false; } int blockIndex = 0; if (args[1] == "*") blockIndex = fieldValue.Count - 1; else if (!int.TryParse(args[1], out blockIndex)) { Console.WriteLine("Invalid index requested from block {0}: {1}", blockName, blockIndex); return false; } if (blockIndex >= fieldValue.Count || blockIndex < 0) { Console.WriteLine("Invalid index requested from block {0}: {1}", blockName, blockIndex); return false; } blockValue = fieldValue[blockIndex]; contextName = $"{blockName}[{blockIndex}]"; } var blockStructure = new TagStructureInfo(blockValue.GetType()); var blockContext = new CommandContext(Stack.Context, contextName); blockContext.AddCommand(new ListFieldsCommand(Info, blockStructure, blockValue)); blockContext.AddCommand(new SetFieldCommand(Stack, Info, Tag, blockStructure, blockValue)); blockContext.AddCommand(new EditBlockCommand(Stack, Info, Tag, blockValue)); blockContext.AddCommand(new AddToBlockCommand(Stack, Info, Tag, blockStructure, blockValue)); blockContext.AddCommand(new RemoveFromBlockCommand(Stack, Info, Tag, blockStructure, blockValue)); blockContext.AddCommand(new CopyElementsCommand(Stack, Info, Tag, blockStructure, blockValue)); blockContext.AddCommand(new PasteElementsCommand(Stack, Info, Tag, blockStructure, blockValue)); blockContext.AddCommand(new ExitToCommand(Stack)); Stack.Push(blockContext); if (deferredNames.Count != 0) { var name = deferredNames[0]; deferredNames = deferredNames.Skip(1).ToList(); foreach (var deferredName in deferredNames) name += '.' + deferredName; args = new List<string> { name }; args.AddRange(deferredArgs); var command = new EditBlockCommand(Stack, Info, Tag, blockValue); return command.Execute(args); } return true; }
public void EndSerialize(TagStructureInfo info, byte[] data, uint mainStructOffset) { // Set up the tag description Tag.DataFixups.Clear(); Tag.ResourceFixups.Clear(); Tag.Dependencies.Clear(); Tag.DataFixups.AddRange(_dataFixups); Tag.ResourceFixups.AddRange(_resourceFixups); Tag.Dependencies.UnionWith(_dependencies); Tag.MainStructOffset = mainStructOffset; _cache.OverwriteTagData(_stream, Tag, data); }