Exemplo n.º 1
0
        public void Write(string path)
        {
            using (var bw = new BinaryWriter(new FileStream(path, FileMode.Create, FileAccess.Write)))
            {
                bw.Write(Encoding.ASCII.GetBytes("GMAD"));

                bw.Write((byte)Header.Version);
                bw.Write(Header.SteamId);
                bw.Write(Convert.ToUInt64((Header.Timestamp - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds));
                
                bw.Write((byte)0); // will be content block

                bw.WriteNullTerminatedString(Header.Name);
                bw.WriteNullTerminatedString(Header.Description);
                bw.WriteNullTerminatedString(Header.Author);
                bw.Write(Header.AddonVersion);

                // File table block
                uint fileNumber = 1;

                foreach (var file in Files)
                {
                    bw.Write(fileNumber);
                    bw.WriteNullTerminatedString(file.StrName);
                    bw.Write(file.Size);
                    bw.Write(file.CRC);

                    fileNumber++;
                }

                // End file table
                bw.Write(0);

                // File data
                foreach (var file in Files)
                {
                    if (file.LocalFile == false)
                    {
                        if(Reader != null)
                            bw.Write(Reader.GetFile(file));
                        else
                            throw new GmadWriterException("Using files from existing gmad file, but no reader has been given.");
                    }
                    else
                    {
                        // read file from disk
                        using (var fs = new FileStream(file.PhysicalPath, FileMode.Open, FileAccess.Read))
                        {
                            var buffer = new byte[fs.Length];
                            fs.Read(buffer, 0, buffer.Length);

                            bw.Write(buffer);
                        }
                    }
                }

            }
        }
Exemplo n.º 2
0
        public void Write(Stream outputStream)
        {
            BinaryWriter writer = new BinaryWriter(outputStream, Encoding.UTF8, true);

            long headerPosition = outputStream.Position;

            outputStream.Position += 24;
            int valuesPosition = (int) outputStream.Position;
            foreach (var entry in Entries)
            {
                entry.Offset = (int) outputStream.Position - valuesPosition;
                writer.Write((short) 0x0001);
                writer.WriteNullTerminatedString(entry.Value);
            }

            writer.AlignWrite(4, 0x00);

            int keysPosition = (int) outputStream.Position;
            foreach (var entry in Entries.OrderBy(e => e.Key).ThenByDescending(e => e.Offset))
            {
                writer.Write(entry.Key);
                writer.Write(entry.Offset);
            }

            long endPosition = outputStream.Position;

            outputStream.Position = headerPosition;
            writer.Write(0x474e414c); // LANG
            writer.Write(3);
            writer.Write(0x0000454c); // LE
            writer.Write(Entries.Count);
            writer.Write(valuesPosition);
            writer.Write(keysPosition);

            outputStream.Position = endPosition;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Compiles the specified addon into the specified stream.
        /// </summary>
        /// <param name="addon">The addon to compile.</param>
        /// <param name="stream">The stream which the result should be written to.</param>
        /// <exception cref="IOExecption">Happens if there is a problem with the specified stream.</exception>
        public static void Create(Addon addon, Stream stream)
        {
            BinaryWriter writer = new BinaryWriter(stream);
            writer.BaseStream.Seek(0, SeekOrigin.Begin);
            writer.BaseStream.SetLength(0);

            // Header (5)
            writer.Write(Addon.Ident.ToCharArray()); // Ident (4)
            writer.Write((char)Addon.Version); // Version (1)
            // SteamID (8) [unused]
            writer.Write((ulong)0);
            // TimeStamp (8)
            writer.Write((ulong)
                (((TimeSpan)(DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0).ToLocalTime())).TotalSeconds));
            // Required content (a list of strings)
            writer.Write((char)0); // signifies nothing
            // Addon Name (n)
            writer.WriteNullTerminatedString(addon.Title);
            // Addon Description (n)
            writer.WriteNullTerminatedString(addon.DescriptionJSON);
            // Addon Author (n) [unused]
            writer.WriteNullTerminatedString("Author Name");
            // Addon version (4) [unused]
            writer.Write((int)1);

            // File list
            uint fileNum = 0;

            foreach (ContentFile f in addon.Files)
            {
                // Remove prefix / from filename
                string file = f.Path.TrimStart('/');

                fileNum++;

                writer.Write(fileNum); // File number (4)
                writer.WriteNullTerminatedString(file.ToLowerInvariant()); // File name (all lower case!) (n)
                writer.Write(f.Size); // File size (8) unsigned long
                writer.Write(f.CRC); // File CRC (4) long long
            }
            writer.Flush();

            // Zero to signify the end of files
            fileNum = 0;
            writer.Write(fileNum);

            // The files
            foreach (ContentFile f in addon.Files)
            {
                writer.Write(f.Content);
                writer.Flush();
            }

            // CRC what we've written (to verify that the download isn't s*****d) (4)
            writer.Seek(0, SeekOrigin.Begin);
            byte[] buffer_whole = new byte[writer.BaseStream.Length];
            writer.BaseStream.Read(buffer_whole, 0, (int)writer.BaseStream.Length);
            ulong addonCRC = System.Cryptography.CRC32.ComputeChecksum(buffer_whole);
            writer.Write(addonCRC);
            writer.Flush();

            writer.BaseStream.Seek(0, SeekOrigin.Begin);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Serializes the current object into a byte array.
        /// </summary>
        public byte[] Serialize()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    foreach (KeyValuePair<long, string> doodadName in this.DoodadNames)
                    {
                        bw.WriteNullTerminatedString(doodadName.Value);
                    }
                }

                return ms.ToArray();
            }
        }
        public byte[] Serialize()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    foreach (string filename in this.Filenames)
                    {
                        bw.WriteNullTerminatedString(filename);
                    }
                }

                return ms.ToArray();
            }
        }
Exemplo n.º 6
0
        public byte[] Serialize()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    // Each block begins with two empty strings
                    bw.Write('\0');
                    bw.Write('\0');

                    // Then the actual data
                    for (int i = 0; i < this.GroupNames.Count; ++i)
                    {
                        bw.WriteNullTerminatedString(this.GroupNames.ElementAt(i).Value);
                    }

                    // Then zero padding to an even 4-byte boundary at the end
                    long count = 4 - (ms.Position % 4);
                    if (count < 4)
                    {
                        for (long i = 0; i < count; ++i)
                        {
                            bw.Write('\0');
                        }
                    }
                }

                return ms.ToArray();
            }
        }
Exemplo n.º 7
0
 public void WriteString(Stream output)
 {
     BinaryWriter writer = new BinaryWriter(output, Encoding.Default, true);
     StringOffset = (int) output.Position;
     string value = ValueEncrypted ? Encoding.Default.GetString(EncryptedValue) : Value;
     StringLength = value.Length;
     writer.WriteNullTerminatedString(value);
 }
Exemplo n.º 8
0
        public byte[] Serialize()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    foreach (KeyValuePair<long, string> texture in this.Textures)
                    {
                        if (ms.Position % 4 == 0)
                        {
                            bw.WriteNullTerminatedString(texture.Value);
                        }
                        else
                        {
                            // Pad with nulls, then write
                            long stringPadCount = 4 - (ms.Position % 4);
                            for (int i = 0; i < stringPadCount; ++i)
                            {
                                bw.Write('\0');
                            }

                            bw.WriteNullTerminatedString(texture.Value);
                        }
                    }

                    // Finally, pad until the next alignment
                    long padCount = 4 - (ms.Position % 4);
                    for (int i = 0; i < padCount; ++i)
                    {
                        bw.Write('\0');
                    }
                }

                return ms.ToArray();
            }
        }
Exemplo n.º 9
0
        public byte[] Serialize()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    if (string.IsNullOrEmpty(this.SkyboxName))
                    {
                        bw.Write(new byte[4]);
                    }
                    else
                    {
                        bw.WriteNullTerminatedString(this.SkyboxName);
                    }
                }

                return ms.ToArray();
            }
        }