Ejemplo n.º 1
0
        private void BlockField_CreateDataBlock(BlockField field, long baseOffset, ref long offset)
        {
            //Create a condensed block
            if (field.BlockList.Count > 0)
            {
                //Prepare
                ITagBlock block       = field.BlockList[0];
                int       localOffset = (int)(field.BlockAddress - baseOffset);

                //Create
                using (VirtualStream vs = new VirtualStream(offset))
                    using (BinaryWriter writer = new BinaryWriter(vs))
                    {
                        //Align
                        vs.Align(block.Alignment);

                        //Write
                        foreach (ITagBlock child in field.BlockList)
                        {
                            child.Write(writer);
                        }

                        //Add
                        tagBlockView.Blocks.Add(localOffset, (int)vs.Length, block.BlockName);
                        offset = field.BlockAddress + vs.Length;
                    }
            }

            //Create child blocks
            foreach (ITagBlock block in field.BlockList)
            {
                TagBlock_CreateChildBlocks(block, baseOffset, ref offset);
            }
        }
Ejemplo n.º 2
0
        private int TagGroup_CalculateChecksum(ITagGroup tagGroup)
        {
            int checksum = 0;

            using (VirtualStream tagStream = new VirtualStream(TagGroupHeader.Size))
                using (BinaryWriter writer = new BinaryWriter(tagStream))
                {
                    tagGroup.Write(writer);
                    tagStream.Align(4);

                    if (tagStream.Length == 0)
                    {
                        return(0);
                    }

                    byte[] tagGroupBuffer = tagStream.ToArray();
                    checksum = BitConverter.ToInt32(tagGroupBuffer, 0);
                    for (int i = 1; i < tagGroupBuffer.Length / 4; i++)
                    {
                        checksum ^= BitConverter.ToInt32(tagGroupBuffer, i * 4);
                    }
                }

            return(checksum);
        }
Ejemplo n.º 3
0
        private void TagBlock_CreateDataBlock(ITagBlock block, long baseOffset, ref long offset)
        {
            //Preapre
            int localOffset = (int)(offset - baseOffset);

            //Create
            using (VirtualStream vs = new VirtualStream(offset))
                using (BinaryWriter writer = new BinaryWriter(vs))
                {
                    //Align
                    vs.Align(block.Alignment);

                    //Writer
                    block.Write(writer);

                    //Add
                    tagBlockView.Blocks.Add(localOffset, (int)vs.Length, block.BlockName);
                    offset += vs.Length;
                }
        }
Ejemplo n.º 4
0
        private void FixSystemLinkButton_Click(object sender, EventArgs e)
        {
            /*
             * Heh I guess Entity is good for something :P
             */

            //Prepare
            ITagGroup         tagGroup = null;
            ITagGroup         scenario = new Scenario();
            ITagBlock         scenarioBlock = null, simulationDefinitionTableElementBlock = null;
            BlockField        simulationDefinitionTableField = null;
            List <IndexEntry> simulationDefinitionEntries = new List <IndexEntry>();
            bool success = false;

            //Build table
            foreach (IndexEntry entry in Map.IndexEntries)
            {
                switch (entry.Root)
                {
                case "bipd":
                case "bloc":
                case "ctrl":
                case "jpt!":
                case "mach":
                case "scen":
                case "ssce":
                case "vehi":
                    simulationDefinitionEntries.Add(entry);
                    break;

                case "eqip":
                case "garb":
                case "proj":
                    simulationDefinitionEntries.Add(entry);
                    simulationDefinitionEntries.Add(entry);
                    break;

                case "weap":
                    simulationDefinitionEntries.Add(entry);
                    simulationDefinitionEntries.Add(entry);
                    simulationDefinitionEntries.Add(entry);
                    break;
                }
            }

            //Read scenario
            using (BinaryReader reader = Map.Scenario.TagData.CreateReader())
            {
                reader.BaseStream.Seek((uint)Map.Scenario.PostProcessedOffset, SeekOrigin.Begin);
                scenario.Read(reader);
            }

            //Re-create simulation definition table
            scenarioBlock = scenario[0];
            simulationDefinitionTableField = (BlockField)scenarioBlock[143];
            simulationDefinitionTableField.BlockList.Clear();
            foreach (IndexEntry entry in simulationDefinitionEntries)
            {
                //Attempt to add tag block
                simulationDefinitionTableElementBlock = simulationDefinitionTableField.Add(out success);
                if (success)
                {
                    simulationDefinitionTableElementBlock[0].Value = entry.Id;
                }
            }

            //Rebuild map
            using (VirtualStream tagDataStream = new VirtualStream(Map.TagDataStream.MemoryAddress))
                using (BinaryWriter writer = tagDataStream.CreateWriter())
                    using (BinaryReader reader = Map.TagDataStream.CreateReader())
                    {
                        //Loop
                        foreach (IndexEntry entry in Map.IndexEntries.Where(ie => ie.Offset > 0 && ie.Size > 0))
                        {
                            //Read (unless it's our modified scenario)
                            if (entry != Map.Scenario)
                            {
                                tagGroup = TagLookup.CreateTagGroup(entry.Root);
                                reader.BaseStream.Seek(entry.Offset, SeekOrigin.Begin);
                                tagGroup.Read(reader);
                            }
                            else
                            {
                                tagGroup = scenario;
                            }

                            //Create buffer
                            using (VirtualStream stream = new VirtualStream(tagDataStream.Position))
                                using (BinaryWriter tagWriter = stream.CreateWriter())
                                    using (BinaryReader tagReader = stream.CreateReader())
                                    {
                                        //Write
                                        tagGroup.Write(tagWriter);

                                        //Recalculate raw addresses
                                        Helper.RecalculateRawAddresses(entry.Raws, entry.Root, stream, tagReader, tagWriter);

                                        //Setup tag
                                        entry.Offset = (uint)stream.MemoryAddress;
                                        entry.Size   = (int)stream.Length;

                                        //Write to tag data stream
                                        writer.Write(stream.ToArray());
                                    }
                        }

                        //Align
                        tagDataStream.Align(4096);

                        //Swap
                        Map.SwapTagBuffer(tagDataStream.ToArray(), tagDataStream.MemoryAddress);
                    }
        }