Exemplo n.º 1
0
 public void Reset()
 {
     _FileLength = 0;
     _Version    = SR1_File.Version.Jun01;
     _IsLevel    = false;
     _Structures.Clear();
     _Primatives.Clear();
     _MigrationStructures.Clear();
     _Pointers.Clear();
     _TextureIDs.Clear();
     _IntroNames.Clear();
     _IntroIDs.Clear();
     _ObjectNames.Clear();
     _ImportErrors.GetStringBuilder().Clear();
     _Scripts.GetStringBuilder().Clear();
     _LastPrimative = null;
     _Overrides     = null;
 }
Exemplo n.º 2
0
        private void Export(Stream outputStream, Version targetVersion, MigrateFlags migrateFlags, Overrides overrides)
        {
            _Overrides = overrides;

            MemoryStream dataStream = new MemoryStream();

            BinaryWriter outputWriter = new BinaryWriter(outputStream, System.Text.Encoding.UTF8, true);
            SR1_Writer   dataWriter   = new SR1_Writer(this, dataStream, true);

            _Primatives.Clear();
            _MigrationStructures.Clear();
            _Pointers.Clear();
            _TextureIDs.Clear();
            _IntroNames.Clear();
            _IntroIDs.Clear();
            _ObjectNames.Clear();

            _LastPrimative = null;

            SR1_Structure[] structures = new SR1_Structure[_Structures.Values.Count];
            _Structures.Values.CopyTo(structures, 0);
            foreach (SR1_Structure structure in structures)
            {
                structure.MigrateVersion(this, targetVersion, migrateFlags);
            }

            _Version = targetVersion;

            foreach (KeyValuePair <uint, SR1_Structure> entry in _Structures)
            {
                entry.Value.Write(dataWriter);
            }

            List <uint> sortedPrimativeKeys = new List <uint>(_Primatives.Keys);
            List <uint> sortedStructureKeys = new List <uint>(_Structures.Keys);

            outputWriter.Write(_Pointers.Count);

            foreach (SR1_PointerBase pointer in _Pointers)
            {
                if (pointer.Offset != 0)
                {
                    outputWriter.Write(pointer.NewStart);
                }
            }

            uint dataStart = (uint)outputWriter.BaseStream.Position;
            uint mod       = dataStart % 0x800;

            if (mod > 0)
            {
                uint padding = 0x800 - mod;
                dataStart += padding;
            }

            while (outputWriter.BaseStream.Position < dataStart)
            {
                outputWriter.Write((sbyte)0);
            }

            foreach (SR1_PointerBase pointer in _Pointers)
            {
                if (_MigrationStructures.ContainsKey(pointer.Offset))
                {
                    dataWriter.BaseStream.Position = pointer.NewStart;
                    dataWriter.Write(_MigrationStructures[pointer.Offset].NewStart);
                    continue;
                }

                if (pointer.Offset == _LastPrimative.End)
                {
                    uint newOffset = _LastPrimative.NewEnd + (pointer.Offset - _LastPrimative.End);
                    dataWriter.BaseStream.Position = pointer.NewStart;
                    dataWriter.Write(newOffset);
                }
                else
                {
                    int index = sortedPrimativeKeys.BinarySearch(pointer.Offset);
                    if (index < 0)
                    {
                        index = (~index - 1);
                    }

                    if (index >= 0)
                    {
                        uint newOffset = 0;
                        SR1_PrimativeBase primative = _Primatives[sortedPrimativeKeys[index]];
                        if (pointer.Offset >= primative.Start && pointer.Offset < primative.End)
                        {
                            newOffset = primative.NewStart + (pointer.Offset - primative.Start);
                        }
                        else
                        {
                            index = sortedStructureKeys.BinarySearch(pointer.Offset);
                            if (index >= 0)
                            {
                                SR1_Structure structure = _Structures[sortedStructureKeys[index]];
                                if (pointer.Offset == structure.Start)
                                {
                                    newOffset = structure.NewStart;
                                }
                            }
                        }

                        dataWriter.BaseStream.Position = pointer.NewStart;
                        dataWriter.Write(newOffset);
                    }
                }
            }

            dataStream.WriteTo(outputStream);
            outputStream.Flush();

            dataWriter.Dispose();
            outputWriter.Dispose();

            dataStream.Close();

            outputStream.Position = 0;

            _Overrides = null;
        }