示例#1
0
        /// <summary>
        /// Deserialisiert die Chunksäule aus dem angegebenen Stream.
        /// </summary>
        /// <param name="stream">Quellstream</param>
        /// <param name="definitionManager">Der verwendete DefinitionManager</param>
        /// <param name="columnIndex">Die Position der Säule</param>
        /// <param name="planetId">Der Index des Planeten</param>
        public void Deserialize(BinaryReader reader)
        {
            var longIndex = reader.ReadByte() > 0;

            // Phase 1 (Column Meta: Heightmap, populated, chunkcount)
            Chunks = new Chunk[reader.ReadByte()]; // Chunk Count

            Populated = reader.ReadBoolean();      // Populated

            Index = new Index2(reader.ReadInt32(), reader.ReadInt32());
            int planetId = reader.ReadInt32();

            var resManager = TypeContainer.Get <IResourceManager>();

            Planet = resManager.GetPlanet(planetId);

            for (var y = 0; y < Chunk.CHUNKSIZE_Y; y++) // Heightmap
            {
                for (var x = 0; x < Chunk.CHUNKSIZE_X; x++)
                {
                    Heights[x, y] = reader.ReadUInt16();
                }
            }


            // Phase 2 (Block Definitionen)
            var types = new List <IDefinition>();
            var map   = new Dictionary <ushort, ushort>();

            int typecount = longIndex ? reader.ReadUInt16() : reader.ReadByte();

            for (var i = 0; i < typecount; i++)
            {
                var           typeName        = reader.ReadString();
                IDefinition[] definitions     = DefinitionManager.GetDefinitions().ToArray();
                IDefinition   blockDefinition = definitions.FirstOrDefault(d => d.GetType().FullName == typeName);
                types.Add(blockDefinition);

                map.Add((ushort)types.Count, (ushort)(Array.IndexOf(definitions, blockDefinition) + 1));
            }

            // Phase 3 (Chunk Infos)
            for (var c = 0; c < Chunks.Length; c++)
            {
                IChunk chunk = Chunks[c] = new Chunk(new Index3(Index, c), Planet);
                chunk.Changed += OnChunkChanged;
                chunk.SetColumn(this);

                for (var i = 0; i < chunk.Blocks.Length; i++)
                {
                    var typeIndex = longIndex ? reader.ReadUInt16() : reader.ReadByte();
                    chunk.MetaData[i] = 0;
                    if (typeIndex > 0)
                    {
                        chunk.Blocks[i] = map[typeIndex];

                        var definition = (IBlockDefinition)DefinitionManager.GetDefinitionByIndex(map[typeIndex]);

                        if (definition.HasMetaData)
                        {
                            chunk.MetaData[i] = reader.ReadInt32();
                        }
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// Serialisiert die Chunksäule in den angegebenen Stream.
        /// </summary>
        /// <param name="writer">Zielschreiber</param>
        /// <param name="definitionManager">Der verwendete DefinitionManager</param>
        public void Serialize(BinaryWriter writer)
        {
            // Definitionen sammeln
            var definitions = new List <IBlockDefinition>();

            for (var c = 0; c < Chunks.Length; c++)
            {
                IChunk chunk = Chunks[c];
                for (var i = 0; i < chunk.Blocks.Length; i++)
                {
                    if (chunk.Blocks[i] != 0)
                    {
                        var definition = (IBlockDefinition)DefinitionManager.GetDefinitionByIndex(chunk.Blocks[i]);
                        if (!definitions.Contains(definition))
                        {
                            definitions.Add(definition);
                        }
                    }
                }
            }

            var longIndex = definitions.Count > 254;

            writer.Write((byte)((longIndex) ? 1 : 0));

            // Schreibe Phase 1 (Column Meta: Heightmap, populated, chunkcount)
            writer.Write((byte)Chunks.Length); // Chunk Count
            writer.Write(Populated);           // Populated
            writer.Write(Index.X);
            writer.Write(Index.Y);
            writer.Write(Planet.Id);

            for (var y = 0; y < Chunk.CHUNKSIZE_Y; y++) // Heightmap
            {
                for (var x = 0; x < Chunk.CHUNKSIZE_X; x++)
                {
                    writer.Write((ushort)Heights[x, y]);
                }
            }

            // Schreibe Phase 2 (Block Definitionen)
            if (longIndex)
            {
                writer.Write((ushort)definitions.Count);
            }
            else
            {
                writer.Write((byte)definitions.Count);
            }

            foreach (IBlockDefinition definition in definitions)
            {
                writer.Write(definition.GetType().FullName);
            }

            // Schreibe Phase 3 (Chunk Infos)
            for (var c = 0; c < Chunks.Length; c++)
            {
                IChunk chunk = Chunks[c];
                for (var i = 0; i < chunk.Blocks.Length; i++)
                {
                    if (chunk.Blocks[i] == 0)
                    {
                        // Definition Index (Air)
                        if (longIndex)
                        {
                            writer.Write((ushort)0);
                        }
                        else
                        {
                            writer.Write((byte)0);
                        }
                    }
                    else
                    {
                        // Definition Index
                        var definition = (IBlockDefinition)DefinitionManager.GetDefinitionByIndex(chunk.Blocks[i]);

                        if (longIndex)
                        {
                            writer.Write((ushort)(definitions.IndexOf(definition) + 1));
                        }
                        else
                        {
                            writer.Write((byte)(definitions.IndexOf(definition) + 1));
                        }

                        // Meta Data
                        if (definition.HasMetaData)
                        {
                            writer.Write(chunk.MetaData[i]);
                        }
                    }
                }
            }
            var resManager = TypeContainer.Get <IResourceManager>();

            using (var lockObj = entitieSemaphore.Wait())
            {
                foreach (var entity in entities)
                {
                    resManager.SaveEntity(entity);
                }
            }
        }