Esempio n. 1
0
        /// <summary>
        /// Writes velocity and mass.
        /// </summary>
        /// <param name="writer"></param>
        public override void Serialize(IOWriter writer)
        {
            base.Serialize(writer);

            writer.Write(Velocity);
            writer.Write(Mass);
        }
Esempio n. 2
0
        public void Serialize(IOWriter writer)
        {
            // Don't save the terrain layer, there is no point.
            // Save Z layers, using run length encoding (TODO)

            // Compresses using run length encoding (obviously has to be lossless).
            // Compressed in vertical strips due to data structure indexing.
            Tile[] toSave = new Tile[tiles.Length - SIZE * SIZE];
            Array.Copy(tiles, SIZE * SIZE, toSave, 0, toSave.Length);
            var compressed = RLE.Compress <Tile>(toSave, (current, next) =>
            {
                return(current.ID == next.ID && current.ColorRef == next.ColorRef && next.EntityID == 0);
            }, out int squased);

            Debug.Log($"Squshed {squased} of {tiles.Length} tiles, that's {((float)squased / (tiles.Length - SIZE * SIZE))*100f:F1}%. Segments: {compressed.Count}.");

            // Write total segment count.
            writer.Write((ushort)compressed.Count);

            // Write compressed data.
            foreach (var pair in compressed)
            {
                writer.Write((ushort)pair.count);
                writer.Write(pair.data.ID);
                writer.Write(pair.data.ColorRef);
                writer.Write(pair.data.EntityID);
            }
        }
Esempio n. 3
0
        public void SerializeAll(IOWriter writer)
        {
            // First of all, compress names to ushorts using a map.
            Dictionary <Type, (string name, ushort id)> map = new Dictionary <Type, (string, ushort)>();
            ushort id = 0;

            foreach (var e in allEntities)
            {
                Type t = e.GetType();
                if (!map.ContainsKey(t))
                {
                    map.Add(t, (t.AssemblyQualifiedName, id));
                    id++;
                }
            }

            // Write map...
            writer.Write(map.Count);
            foreach (var pair in map)
            {
                writer.Write(pair.Value.name);
            }

            writer.Write(allEntities.Count);
            foreach (var e in allEntities)
            {
                writer.Write(map[e.GetType()].id);
                writer.Write(e);
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Called when the entity is being saved to disk.
 /// Override this to write all data that you want to save. Data must be written and read in the
 /// same order.
 /// This default implementation writes name and bounds, so it is important to still call it when overriding in a custom class.
 /// </summary>
 /// <param name="writer">The BinaryWriter to write data with. Import Engine.IO for many useful extension methods (such as writing Vector2).</param>
 public virtual void Serialize(IOWriter writer)
 {
     if (SerializeName)
     {
         writer.Write(Name);
     }
     writer.Write(Bounds);
 }
Esempio n. 5
0
        public override void Serialize(IOWriter writer)
        {
            base.Serialize(writer);

            // Write tile positions.
            writer.Write(TileX);
            writer.Write(TileY);
            writer.Write(TileZ);
        }
Esempio n. 6
0
        /// <summary>
        /// Saves the current zip file
        /// </summary>
        /// <param name="output">location to save</param>
        /// <returns>outcome of operation</returns>
        public bool Save(string output,bool getFileData)
        {
            try
            {
                CentralDirectoryRecordEOF eof = new CentralDirectoryRecordEOF();
                eof.TotalCDirEntries = (ushort)this.m_lZipDirectory.Count;

                /// Get all file data
                if (getFileData)
                    for (int i = 0; i < this.m_lZipDirectory.Count; i++)
                        this.m_lZipDirectory[i].ChildEntry.FileData = GetFileData(i);
                using (IOWriter writer = new IOWriter(File.Create(output)))
                {
                    /// Write local file directories
                    foreach (ZipFile file in this.m_lZipDirectory)
                        writer.Write(file.CreateHeader((int)writer.BaseStream.Position).Serialize());

                    /// Pad the beginning of the central directory
                    int cdirStart = (int)writer.BaseStream.Position.Align(0x800);
                    writer.Write(new byte[cdirStart - (int)writer.BaseStream.Position]);
                    eof.StartOfCDir = (uint)cdirStart;

                    /// Write Central Directory entires
                    foreach (ZipFile file in this.m_lZipDirectory)
                        writer.Write(file.ChildEntry.Serialize());

                    /// Pad out the end of the central directory
                    int cdirEnd = (int)writer.BaseStream.Position.Align(0x800);
                    writer.Write(new byte[cdirEnd - (int)writer.BaseStream.Position]);
                    eof.SizeOfCDir = (ushort)(cdirEnd - cdirStart);
                    /// Pad zip
                    if (writer.BaseStream.Length < (int)this.m_gameName)
                        writer.Write(new byte[((int)this.m_gameName - writer.BaseStream.Length) - 54]);
                    /// Write EOF
                    writer.Write(eof.Serialize());
                }

                return true;
            }
            catch { return false; }
        }
Esempio n. 7
0
 /// <summary>
 /// Extracts a file
 /// </summary>
 /// <param name="index">index in zip</param>
 /// <param name="fileName">save file name</param>
 public void ExtractFile(int index, string fileName)
 {
     this.m_zipReader.Seek(this.m_lZipDirectory[index].Offset, SeekOrigin.Begin);
     using (IOWriter writer = new IOWriter(File.Create(fileName)))
         writer.Write(this.m_zipReader.ReadBytes(this.m_lZipDirectory[index].FileSize));
 }
Esempio n. 8
0
 /// <summary>
 /// Extracts all files from the zip
 /// </summary>
 /// <param name="folder">output folder</param>
 /// <returns>operation outcome</returns>
 public bool ExtractAllFiles(string folder)
 {
     try
     {
         foreach (ZipFile entry in this.m_lZipDirectory)
         {
             this.m_zipReader.Seek(entry.Offset, SeekOrigin.Begin);
             byte[] buffer = this.m_zipReader.ReadBytes((int)entry.FileSize);
             string path = folder + @"\" + entry.FileName.Substring(0, entry.FileName.LastIndexOfAny(new char[] { '\\', '/' }) + 1);
             string fileName = Path.GetFileName(entry.FileName);
             Console.WriteLine("Extracting {0} to /output {1} bytes", entry.FileName, entry.FileSize);
             if (!Directory.Exists(path))
             {
                 Directory.CreateDirectory(path);
             }
             using(FileStream output = new FileStream(path + fileName, FileMode.Create))
             using (IOWriter writer = new IOWriter(output))
             {
                 writer.Write(buffer);
             }
         }
         return true;
     }
     catch { return false; }
 }
Esempio n. 9
0
        public override void Serialize(IOWriter writer)
        {
            base.Serialize(writer);

            writer.Write(ItemStack);
        }