Exemple #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);
        }
Exemple #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);
            }
        }
Exemple #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);
            }
        }
Exemple #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);
 }
Exemple #5
0
        public override void Serialize(IOWriter writer)
        {
            base.Serialize(writer);

            // Write tile positions.
            writer.Write(TileX);
            writer.Write(TileY);
            writer.Write(TileZ);
        }
Exemple #6
0
        public override void Update()
        {
            if (Input.KeyDown(Keys.F11))
            {
                Screen.ToggleFullscreen();
            }

            if (Input.KeyPressed(Keys.F))
            {
                for (int i = 0; i < 50; i++)
                {
                    var e = new TestEntity();
                    e.Center   = Input.MouseWorldPos;
                    e.Velocity = Rand.UnitCircle() * Rand.Range(0.25f, 10f) * Tile.SIZE;
                }
            }

            if (Input.KeyDown(Keys.M))
            {
                using (FileStream fs = new FileStream(@"C:\Users\James.000\Desktop\Chunk.txt", FileMode.Create, FileAccess.Write))
                {
                    using (IOWriter w = new IOWriter(fs))
                    {
                        Layer.GetChunk(0, 0).Serialize(w);
                        Debug.Log($"Written {w.Length} bytes.");
                    }
                }
            }

            if (Input.KeyDown(Keys.NumPad0))
            {
                using (FileStream fs = new FileStream(@"C:\Users\James.000\Desktop\Entities.txt", FileMode.Create, FileAccess.Write))
                {
                    using (IOWriter w = new IOWriter(fs))
                    {
                        JEngine.Entities.SerializeAll(w);
                        Debug.Log($"Written {w.Length} bytes for entities.");
                    }
                }
            }

            if (Input.KeyDown(Keys.NumPad1))
            {
                using (FileStream fs = new FileStream(@"C:\Users\James.000\Desktop\Entities.txt", FileMode.Open, FileAccess.Read))
                {
                    using (IOReader w = new IOReader(fs))
                    {
                        JEngine.Entities.DeserializeAllNew(w);
                        Debug.Log($"Read {w.Length} bytes for entities.");
                    }
                }
            }

            if (Input.KeyDown(Keys.N))
            {
                using (FileStream fs = new FileStream(@"C:\Users\James.000\Desktop\Chunk.txt", FileMode.OpenOrCreate, FileAccess.Read))
                {
                    using (IOReader r = new IOReader(fs))
                    {
                        Layer.GetChunk(0, 0).Deserialize(r);
                        Debug.Log($"Read {r.Length} bytes.");
                    }
                }
            }

            if (Input.KeyDown(Keys.Y))
            {
                var e = new TestActive();
                e.Center   = Input.MouseWorldPos;
                e.Velocity = Rand.UnitCircle() * Rand.Range(1f, 5f);
            }

            if (Input.KeyPressed(Keys.L))
            {
                float r = Tile.SIZE * 5;
                foreach (var entity in JEngine.Entities.GetAllInRange(Input.MouseWorldPos, r))
                {
                    entity.Destroy();
                }
            }

            if (Input.KeyDown(Keys.Space))
            {
                System.GC.Collect();
            }

            if (Input.KeyDown(Keys.T))
            {
                var missile = new MissileEntity();
                missile.Position = Input.MouseWorldPos - missile.Size * 0.5f;
            }

            var p = JEngine.TileMap.PixelToTileCoords((int)Input.MouseWorldPos.X, (int)Input.MouseWorldPos.Y);

            if (Input.KeyPressed(Keys.V))
            {
                JEngine.TileMap.SetTile(p.X, p.Y, 1, new Tile(2, ColorCache.EnsureColor(Color.White)));
            }

            if (Input.KeyDown(Keys.B))
            {
                Layer.SetTile(p.X, p.Y, 1, new Tile(4, ColorCache.EnsureColor(Color.White)));
            }

            const int MAX_PER_FRAME = 5;
            int       count         = 0;

            foreach (var chunk in Layer.GetLoadedChunks())
            {
                if (chunk != null)
                {
                    if (chunk.EntityCount != 0)
                    {
                        chunk.FlagAsNeeded();
                    }

                    if (chunk.TimeSinceNeeded > CHUNK_UNLOAD_TIME && chunk.FramesSinceNeeded > 2)
                    {
                        toBin.Add(chunk.ID);
                        continue;
                    }
                    if (chunk.TimeSinceRendered > CHUNK_UNLOAD_TIME && chunk.FramesSinceRendered > 2)
                    {
                        chunk.UnloadGraphics();
                    }
                    chunk.Decay(Time.unscaledDeltaTime);

                    if (chunk.RequiresRedraw && count < MAX_PER_FRAME && chunk.Graphics.Texture != null)
                    {
                        count++;
                        RequestRenderTargetDraw(new TargetRenderRequest(chunk.Graphics.Texture, true)
                        {
                            CustomData = chunk
                        });
                    }
                }
            }
            foreach (var id in toBin)
            {
                Layer.UnloadChunk(id);
            }
            toBin.Clear();
        }
        /// <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; }
        }
 /// <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));
 }
 /// <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; }
 }
Exemple #10
0
        public override void Serialize(IOWriter writer)
        {
            base.Serialize(writer);

            writer.Write(ItemStack);
        }