Exemple #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(Stream stream, IDefinitionManager definitionManager, int planetId, Index2 columnIndex)
        {
            using (BinaryReader br = new BinaryReader(stream))
            {
                bool longIndex = br.ReadByte() > 0;

                // Phase 1 (Column Meta: Heightmap, populated, chunkcount)
                Chunks = new Chunk[br.ReadByte()]; // Chunk Count
                Planet = planetId;
                Index = columnIndex;

                Populated = br.ReadBoolean(); // Populated
                for (int y = 0; y < Chunk.CHUNKSIZE_Y; y++) // Heightmap
                    for (int x = 0; x < Chunk.CHUNKSIZE_X; x++)
                        Heights[x, y] = br.ReadUInt16();

                int[] counter = new int[Chunks.Length];
                for (int i = 0; i < Chunks.Length; i++) // ChangeCounter
                    counter[i] = br.ReadInt32();

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

                int typecount = longIndex ? br.ReadUInt16() : br.ReadByte();
                for (int i = 0; i < typecount; i++)
                {
                    string typeName = br.ReadString();
                    IBlockDefinition[] definitions = definitionManager.GetBlockDefinitions().ToArray();
                    var 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 (int c = 0; c < Chunks.Length; c++)
                {
                    IChunk chunk = Chunks[c] = new Chunk(new Index3(columnIndex, c), planetId);
                    for (int i = 0; i < chunk.Blocks.Length; i++)
                    {
                        ushort typeIndex = longIndex ? br.ReadUInt16() : br.ReadByte();
                        chunk.MetaData[i] = 0;
                        if (typeIndex > 0)
                        {
                            chunk.Blocks[i] = map[typeIndex];

                            var definition = definitionManager.GetBlockDefinitionByIndex(map[typeIndex]);
                            if (definition.HasMetaData)
                                chunk.MetaData[i] = br.ReadInt32();
                        }
                    }
                    chunk.ChangeCounter = counter[c];
                }
            }
        }
Exemple #2
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, IDefinitionManager definitionManager)
        {
            bool 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());
            Planet = reader.ReadInt32();

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

            int[] counter = new int[Chunks.Length];

            for (int i = 0; i < Chunks.Length; i++) // ChangeCounter
            {
                counter[i] = reader.ReadInt32();
            }

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

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

            for (int i = 0; i < typecount; i++)
            {
                string        typeName        = reader.ReadString();
                IDefinition[] definitions     = definitionManager.GetDefinitions().ToArray();
                var           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 (int c = 0; c < Chunks.Length; c++)
            {
                IChunk chunk = Chunks[c] = new Chunk(new Index3(Index, c), Planet);
                chunk.Changed += OnChunkChanged;
                chunk.SetColumn(this);

                for (int i = 0; i < chunk.Blocks.Length; i++)
                {
                    ushort 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();
                        }
                    }
                }
                chunk.ChangeCounter = counter[c];
            }

            //Entities lesen
            var count = reader.ReadInt32();

            for (int i = 0; i < count; i++)
            {
                var name   = reader.ReadString();
                var length = reader.ReadInt32();

                byte[] buffer = new byte[length];
                reader.Read(buffer, 0, length);

                try
                {
                    var type = Type.GetType(name);

                    if (type == null)
                    {
                        continue;
                    }

                    Entity entity = (Entity)Activator.CreateInstance(type);

                    using (MemoryStream memorystream = new MemoryStream(buffer))
                    {
                        using (BinaryReader componentbinarystream = new BinaryReader(memorystream))
                        {
                            entity.Deserialize(componentbinarystream, definitionManager);
                        }
                    }

                    Entities.Add(entity);
                }
                catch (Exception)
                {
                }
            }
        }