public void Save(string newFilename = null) { //This always saves in little-endian format: //- Metadata will always be little-endian. //- Header will always be big-endian as per the file format. //- Object Data follows this.IsLittleEndian #region Metadata List <byte> metadataSection = new List <byte>(); DataWriter metadataWriter = DataWriter.FromList(metadataSection, true); #region Class Hierarchy Descriptor if (FormatVersion > 7) { metadataWriter.WriteString(UnityVersion); metadataWriter.WriteInt32(RTTIAttributes); } metadataWriter.WriteInt32(BaseClasses.Count); foreach (int baseClass in BaseClasses.Keys) { metadataWriter.WriteInt32(baseClass); BaseClasses[baseClass].Write(metadataWriter); } if (FormatVersion > 7) { metadataWriter.WriteBytes(new byte[] { 0, 0, 0, 0 }); } #endregion metadataWriter.WriteInt32(Objects.Count); int currentOffset = 0; foreach (ObjectData od in Objects) { od.Write(metadataWriter, currentOffset, FormatVersion > 13); metadataWriter.WriteInt64(0); currentOffset += od.Bytes.Length + 8; } metadataWriter.WriteInt32(ExternalFiles.Count); foreach (FileIdentifier f in ExternalFiles) { f.Write(metadataWriter, FormatVersion > 5); } metadataWriter.WriteByte(0); #endregion #region Header and Object Data DataWriter fileWriter = DataWriter.OpenFile(newFilename == null ? filename : newFilename, false); fileWriter.WriteInt32(metadataSection.Count); int headerSize = 16 + (FormatVersion > 8 ? 4 : 0); fileWriter.WriteInt32(metadataSection.Count + currentOffset + headerSize); fileWriter.WriteInt32(FormatVersion); if (FormatVersion > 8) { fileWriter.WriteInt32(headerSize + metadataSection.Count); fileWriter.WriteByte(IsLittleEndian ? (byte)0 : (byte)1); fileWriter.WriteBytes(new byte[] { 0, 0, 0 }); fileWriter.IsLittleEndian = IsLittleEndian; fileWriter.WriteBytes(metadataSection.ToArray()); foreach (ObjectData od in Objects) { fileWriter.WriteBytes(od.Bytes); } } else { fileWriter.WriteInt32(headerSize); fileWriter.IsLittleEndian = IsLittleEndian; foreach (ObjectData od in Objects) { fileWriter.WriteBytes(od.Bytes); } fileWriter.WriteBytes(metadataSection.ToArray()); } #endregion fileWriter.Close(); }