Example #1
0
		public void Serialize(Stream output, Endian endian) {
			output.WriteValueU32(this.Size, endian);
			output.WriteValueEnum<HeaderFlags>(this.Flags, endian);
			output.WriteValueS32(this.Height, endian);
			output.WriteValueS32(this.Width, endian);
			output.WriteValueU32(this.PitchOrLinearSize, endian);
			output.WriteValueU32(this.Depth, endian);
			output.WriteValueU32(this.MipMapCount, endian);
			output.Write(this.Reserved1, 0, this.Reserved1.Length);
			this.PixelFormat.Serialize(output, endian);
			output.WriteValueU32(this.SurfaceFlags, endian);
			output.WriteValueU32(this.CubemapFlags, endian);
			output.Write(this.Reserved2, 0, this.Reserved2.Length);
		}
Example #2
0
        public void Serialize(Stream output)
        {
            if (this.Version < 1 || this.Version > 2)
            {
                throw new InvalidOperationException("unsupported blob version");
            }

            output.WriteValueU32(this.Version);
            output.WriteValueS32(this.Entries.Count);

            var nameLength = this.Version == 2 ? 32 : 14;

            foreach (var entry in this.Entries)
            {
                var name = entry.Name;

                if (name.Length + 1 > nameLength)
                {
                    throw new InvalidOperationException();
                }

                output.WriteString(name.PadRight(nameLength, '\0'));
                output.WriteValueU32((uint)entry.Offset);
                output.WriteValueU32(entry.Size);
            }
        }
Example #3
0
        public void Serialize(Stream output)
        {
            output.WriteValueU32(0x42494748, false);
            output.WriteValueU32(this.TotalFileSize, true);
            output.WriteValueS32(this.Entries.Count, false);
            output.WriteValueS32(16 + (this.Entries.Count * 12) + 8, false);

            foreach (var entry in this.Entries)
            {
                output.WriteValueU32(entry.Offset, false);
                output.WriteValueU32(entry.Size, false);
                output.WriteValueU32(entry.Name, false);
            }

            output.WriteValueU32(0x4C323833, false);
            output.WriteValueU32(0x15050000, false);
        }
Example #4
0
        public void Serialize(Stream output)
        {
            var endian = this.Endian;

            const uint headerSize = 32;
            output.WriteValueU32(0x42424947, endian);
            output.WriteValueU32(this.Version, endian);

            var keys = new List<string>() {""};

            int maxValueLength = 0;
            var blob = new StringBuilder();
            foreach (var file in this.Files)
            {
                keys.Add(file.Name);
                foreach (var section in file.Sections)
                {
                    keys.Add(section.Key);
                    foreach (var value in section.Value)
                    {
                        keys.Add(value.Key);
                        foreach (var item in value.Value)
                        {
                            if (item.Value != null)
                            {
                                blob.Append(item.Value + '\0');
                                maxValueLength = Math.Max(maxValueLength, item.Value.Length);
                            }
                        }
                    }
                }
            }

            var huffmanEncoder = new Huffman.Encoder();
            huffmanEncoder.Build(blob.ToString());

            keys = keys.Distinct().OrderBy(k => k.HashCrc32()).ToList();
            int maxKeyLength = keys.Max(k => k.Length);

            uint stringTableSize;
            using (var data = new MemoryStream())
            {
                data.Position = 4;
                data.WriteValueS32(keys.Count, endian);

                data.Position = 4 + 4 + (8 * keys.Count);
                var offsets = new List<KeyValuePair<uint, uint>>();
                foreach (var key in keys)
                {
                    var offset = (uint)data.Position;
                    data.WriteValueU16((ushort)key.Length, endian);
                    data.WriteString(key, Encoding.UTF8);
                    offsets.Add(new KeyValuePair<uint, uint>(key.HashCrc32(), offset));
                }

                data.Position = 8;
                foreach (var kv in offsets)
                {
                    data.WriteValueU32(kv.Key, endian);
                    data.WriteValueU32(kv.Value - 8, endian);
                }

                data.Position = 0;
                data.WriteValueU32((uint)data.Length, endian);

                data.Position = 0;
                stringTableSize = (uint)data.Length;

                output.Seek(headerSize, SeekOrigin.Begin);
                output.WriteFromStream(data, data.Length);
            }

            uint huffmanSize;
            using (var data = new MemoryStream())
            {
                var pairs = huffmanEncoder.GetPairs();
                data.WriteValueU16((ushort)pairs.Length, endian);
                foreach (var pair in pairs)
                {
                    data.WriteValueS32(pair.Left, endian);
                    data.WriteValueS32(pair.Right, endian);
                }

                data.Position = 0;
                huffmanSize = (uint)data.Length;

                output.Seek(headerSize + stringTableSize, SeekOrigin.Begin);
                output.WriteFromStream(data, data.Length);
            }

            var bits = new BitArray(huffmanEncoder.TotalBits);
            var bitOffset = 0;

            uint indexSize;
            using (var index = new MemoryStream())
            {
                var fileDataOffset = 2 + (this.Files.Count * 6);

                var files = new List<KeyValuePair<ushort, int>>();
                foreach (var file in this.Files.OrderBy(f => keys.IndexOf(f.Name)))
                {
                    files.Add(new KeyValuePair<ushort, int>(
                        (ushort)keys.IndexOf(file.Name), fileDataOffset));

                    var sectionDataOffset = 2 + (file.Sections.Count * 6);

                    var sections = new List<KeyValuePair<ushort, int>>();
                    foreach (var section in file.Sections.OrderBy(s => keys.IndexOf(s.Key)))
                    {
                        sections.Add(new KeyValuePair<ushort, int>(
                            (ushort)keys.IndexOf(section.Key), sectionDataOffset));

                        var valueDataOffset = 2 + (section.Value.Count * 6);

                        var values = new List<KeyValuePair<ushort, int>>();
                        foreach (var value in section.Value.OrderBy(v => keys.IndexOf(v.Key)))
                        {
                            index.Position = fileDataOffset + sectionDataOffset + valueDataOffset;

                            values.Add(new KeyValuePair<ushort, int>(
                                (ushort)keys.IndexOf(value.Key), valueDataOffset));

                            index.WriteValueU16((ushort)value.Value.Count, endian);
                            valueDataOffset += 2;

                            foreach (var item in value.Value)
                            {
                                if (item.Type == 1)
                                {
                                    index.WriteValueS32((1 << 29) | bitOffset, endian);
                                }
                                else if (item.Type == 0 || item.Type == 2 || item.Type == 3 || item.Type == 4)
                                {
                                    index.WriteValueS32((item.Type << 29) | bitOffset, endian);
                                    bitOffset += huffmanEncoder.Encode((item.Value ?? "") + '\0', bits, bitOffset);
                                }
                                valueDataOffset += 4;
                            }
                        }

                        index.Position = fileDataOffset + sectionDataOffset;
                        
                        index.WriteValueU16((ushort)values.Count, endian);
                        sectionDataOffset += 2;

                        foreach (var value in values)
                        {
                            index.WriteValueU16(value.Key, endian);
                            index.WriteValueS32(value.Value, endian);
                            sectionDataOffset += 6;
                        }

                        sectionDataOffset += valueDataOffset;
                    }

                    index.Position = fileDataOffset;

                    index.WriteValueU16((ushort)sections.Count, endian);
                    fileDataOffset += 2;

                    foreach (var section in sections)
                    {
                        index.WriteValueU16(section.Key, endian);
                        index.WriteValueS32(section.Value, endian);
                        fileDataOffset += 6;
                    }

                    fileDataOffset += sectionDataOffset;
                }

                index.Position = 0;

                index.WriteValueU16((ushort)files.Count, endian);
                foreach (var file in files)
                {
                    index.WriteValueU16(file.Key, endian);
                    index.WriteValueS32(file.Value, endian);
                }

                index.Position = 0;
                indexSize = (uint)index.Length;

                output.Seek(headerSize + stringTableSize + huffmanSize, SeekOrigin.Begin);
                output.WriteFromStream(index, index.Length);
            }

            output.Seek(headerSize + stringTableSize + huffmanSize + indexSize, SeekOrigin.Begin);
            output.WriteValueS32(bits.Length, endian);
            var bytes = new byte[(bits.Length - 1) / 8 + 1];
            bits.CopyTo(bytes, 0);
            output.WriteBytes(bytes);

            output.Seek(8, SeekOrigin.Begin);
            output.WriteValueS32(maxKeyLength, endian);
            output.WriteValueS32(maxValueLength, endian);
            output.WriteValueU32(stringTableSize, endian);
            output.WriteValueU32(huffmanSize, endian);
            output.WriteValueU32(indexSize, endian);
            output.WriteValueS32(bytes.Length, endian);

            output.Seek(0, SeekOrigin.Begin);
            output.WriteValueU32(0x666D726D, endian);
        }
Example #5
0
 public static void Serialize(Stream output, FieldType type, object value, bool littleEndian)
 {
     switch (type)
     {
         case FieldType.UInt8: output.WriteValueU8((byte)value); break;
         case FieldType.Int8: output.WriteValueS8((sbyte)value); break;
         case FieldType.UInt16: output.WriteValueU16((ushort)value); break;
         case FieldType.Int16: output.WriteValueS16((short)value); break;
         case FieldType.UInt32: output.WriteValueU32((uint)value); break;
         case FieldType.Int32: output.WriteValueS32((int)value); break;
         case FieldType.UInt64: output.WriteValueU64((ulong)value); break;
         case FieldType.Int64: output.WriteValueS64((long)value); break;
         case FieldType.Single: output.WriteValueF32((float)value); break;
         case FieldType.Double: output.WriteValueF64((double)value); break;
         case FieldType.Vector3:
         {
             if (value == null)
             {
                 throw new ArgumentNullException();
             }
             ((Builtins.Vector3)value).Serialize(output, littleEndian);
             break;
         }
         case FieldType.Vector4:
         {
             if (value == null)
             {
                 throw new ArgumentNullException();
             }
             ((Builtins.Vector4)value).Serialize(output, littleEndian);
             break;
         }
         case FieldType.Quaternion:
         {
             if (value == null)
             {
                 throw new ArgumentNullException();
             }
             ((Builtins.Quaternion)value).Serialize(output, littleEndian);
             break;
         }
         case FieldType.String:
         {
             throw new NotSupportedException("cannot serialize strings via Builtin");
         }
         case FieldType.Color:
         {
             if (value == null)
             {
                 throw new ArgumentNullException();
             }
             ((Builtins.Color)value).Serialize(output, littleEndian);
             break;
         }
         case FieldType.Matrix4x4:
         {
             if (value == null)
             {
                 throw new ArgumentNullException();
             }
             ((Builtins.Matrix4x4)value).Serialize(output, littleEndian);
             break;
         }
         default:
         {
             throw new NotSupportedException("unsupported builtin type");
         }
     }
 }
Example #6
0
        public void Serialize(Stream output)
        {
            output.WriteValueU32(this.FileAlignment, this.LittleEndian);
            output.WriteString(this.BasePath, 64, Encoding.ASCII);
            output.WriteValueS32(this.Entries.Count, this.LittleEndian);

            var entries = this.Entries
                .OrderBy(e => e.UncompressedSize)
                .OrderBy(e => e.NameHash);

            foreach (var entry in entries)
            {
                output.WriteValueU32(entry.NameHash, this.LittleEndian);
            }

            foreach (var entry in entries)
            {
                output.WriteValueU32(entry.UncompressedSize, this.LittleEndian);
                output.WriteValueU32(entry.Offset, this.LittleEndian);
                output.WriteValueU32(entry.Locale, this.LittleEndian);
                output.WriteValueU32(entry.CompressedSize, this.LittleEndian);
            }
        }
        public void Serialize(Stream output)
        {
            if (this.Version == 1)
            {
                throw new NotSupportedException();
            }
            else if (this.Version == 2)
            {
                throw new NotSupportedException();
            }
            else if (this.Version == 3)
            {
                output.WriteString("ERF V3.0", Encoding.Unicode);

                var strings = new List<string>();
                foreach (var entry in this.Entries)
                {
                    if (entry.Name != null &&
                        strings.Contains(entry.Name) == false)
                    {
                        strings.Add(entry.Name);
                    }
                }
                strings.Sort();

                var stringOffsets = new Dictionary<string, uint>();
                var stringTable = new MemoryStream();

                foreach (var str in strings)
                {
                    stringOffsets[str] = (uint)stringTable.Length;
                    stringTable.WriteStringZ(str, Encoding.UTF8);
                }
                stringTable.SetLength(stringTable.Length.Align(16));
                stringTable.Position = 0;

                output.WriteValueU32((uint)stringTable.Length);
                output.WriteValueS32(this.Entries.Count);

                if (this.Encryption != EncryptionScheme.None ||
                    this.Compression != CompressionScheme.None)
                {
                    throw new NotSupportedException();
                }

                uint flags = 0;
                flags |= ((uint)this.Encryption) << 4;
                flags |= ((uint)this.Compression) << 29;
                output.WriteValueU32(flags);

                output.WriteValueU32(this.ContentId);
                if (this.PasswordDigest == null)
                {
                    output.Write(new byte[16], 0, 16);
                }
                else
                {
                    output.Write(this.PasswordDigest, 0, 16);
                }

                output.WriteFromStream(stringTable, stringTable.Length, 0x00100000);

                foreach (var entry in this.Entries)
                {
                    if (entry.Name != null)
                    {
                        entry.CalculateHashes();
                        output.WriteValueU32(stringOffsets[entry.Name]);
                    }
                    else
                    {
                        output.WriteValueU32(0xFFFFFFFF);
                    }

                    output.WriteValueU64(entry.NameHash);
                    output.WriteValueU32(entry.TypeHash);
                    output.WriteValueU32((uint)entry.Offset);
                    output.WriteValueU32(entry.CompressedSize);
                    output.WriteValueU32(entry.UncompressedSize);
                }
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
Example #8
0
        public void Serialize(Stream output)
        {
            output.WriteValueU32(0x54414653, this.LittleEndian);
            output.WriteValueU32(this.Unknown04, this.LittleEndian);
            output.WriteValueU32(this.NumberOfFiles, this.LittleEndian);
            output.WriteValueS32(this.Entries.Count, this.LittleEndian);
            output.WriteValueU32(this.Unknown10, this.LittleEndian);
            output.WriteString(this.BasePath, 32, Encoding.ASCII);

            foreach (var e in this.Entries.OrderBy(e => e.NameHash))
            {
                output.WriteValueU32(e.NameHash, this.LittleEndian);
                output.WriteValueU32(e.Locale, this.LittleEndian);
                output.WriteValueU32(e.Size, this.LittleEndian);
                output.WriteValueU32(e.File | e.Offset, this.LittleEndian);
            }
        }
Example #9
0
        public void Serialize(Stream output)
        {
            output.WriteValueU32(0x47464620u, false);
            output.WriteValueU32(0x56342E30u + this.FileVersion, false);
            output.WriteValueEnum<GFF.FilePlatform>(this.FilePlatform, false);
            output.WriteValueEnum<GFF.FormatType>(this.FormatType, false);
            output.WriteValueU32(this.FormatVersion, false);

            var littleEndian = this.FilePlatform == GFF.FilePlatform.PC;

            output.WriteValueS32(this.Structures.Count, littleEndian);

            int structuresOffset = (int)output.Position + 4;
            int fieldsOffset = structuresOffset + (this.Structures.Count * 16);
            int dataOffset = fieldsOffset + (this.Structures.Sum(s => s.Fields.Count) * 12);
            dataOffset = dataOffset.Align(16);
            output.WriteValueS32(dataOffset, littleEndian);

            int runningFieldCount = 0;
            foreach (var structDef in this.Structures)
            {
                output.WriteValueU32(structDef.Id, false);
                output.WriteValueS32(structDef.Fields.Count, littleEndian);
                output.WriteValueS32(fieldsOffset +
                    (runningFieldCount * 12), littleEndian);
                output.WriteValueU32(structDef.DataSize, littleEndian);
                runningFieldCount += structDef.Fields.Count;
            }

            foreach (var structDef in this.Structures)
            {
                foreach (var fieldDef in structDef.Fields)
                {
                    output.WriteValueS32(fieldDef.Id, littleEndian);

                    var flags = fieldDef.Flags;
                    ushort type;

                    if (fieldDef.Type == GFF.FieldType.Structure)
                    {
                        flags |= GFF.FieldFlags.IsStructure;
                        type = fieldDef.StructureId;
                    }
                    else
                    {
                        type = (ushort)fieldDef.Type;
                    }

                    uint rawFlags = 0;
                    rawFlags |= type;
                    rawFlags |= (uint)flags << 16;

                    output.WriteValueU32(rawFlags, littleEndian);
                    output.WriteValueU32(fieldDef.Offset, littleEndian);
                }
            }

            this.Data.Position = 0;
            output.Seek(dataOffset, SeekOrigin.Begin);
            output.WriteFromStream(this.Data, this.Data.Length);
        }