Example #1
0
        public static DlmMap ReadFromStream(IDataReader givenReader, DlmReader dlmReader)
        {
            var reader = givenReader;
            var map    = new DlmMap();

            map.Version = reader.ReadByte();
            map.Id      = reader.ReadInt();

            if (map.Version >= 7)
            {
                map.Encrypted         = reader.ReadBoolean();
                map.EncryptionVersion = reader.ReadByte();

                var len = reader.ReadInt();

                if (map.Encrypted)
                {
                    var key = dlmReader.DecryptionKey;

                    if (key == null && dlmReader.DecryptionKeyProvider != null)
                    {
                        key = dlmReader.DecryptionKeyProvider(map.Id);
                    }

                    if (key == null)
                    {
                        throw new InvalidOperationException(string.Format("Cannot decrypt the map {0} without decryption key", map.Id));
                    }

                    var data       = reader.ReadBytes(len);
                    var encodedKey = Encoding.Default.GetBytes(key);

                    if (key.Length > 0)
                    {
                        for (int i = 0; i < data.Length; i++)
                        {
                            data[i] = (byte)(data[i] ^ encodedKey[i % key.Length]);
                        }

                        reader = new FastBigEndianReader(data);
                    }
                }
            }

            map.RelativeId = reader.ReadUInt();
            map.MapType    = reader.ReadByte();

            // temp, just to know if the result is coherent
            if (map.MapType < 0 || map.MapType > 1)
            {
                throw new Exception("Invalid decryption key");
            }

            map.SubAreaId             = reader.ReadInt();
            map.TopNeighbourId        = reader.ReadInt();
            map.BottomNeighbourId     = reader.ReadInt();
            map.LeftNeighbourId       = reader.ReadInt();
            map.RightNeighbourId      = reader.ReadInt();
            map.ShadowBonusOnEntities = reader.ReadInt();

            if (map.Version >= 3)
            {
                map.BackgroundColor = Color.FromArgb(reader.ReadByte(), reader.ReadByte(), reader.ReadByte());
            }

            if (map.Version >= 4)
            {
                map.ZoomScale   = reader.ReadUShort();
                map.ZoomOffsetX = reader.ReadShort();
                map.ZoomOffsetY = reader.ReadShort();
            }

            map.UseLowPassFilter = reader.ReadByte() == 1;
            map.UseReverb        = reader.ReadByte() == 1;

            if (map.UseReverb)
            {
                map.PresetId = reader.ReadInt();
            }
            {
                map.PresetId = -1;
            }

            map.BackgroudFixtures = new DlmFixture[reader.ReadByte()];
            for (int i = 0; i < map.BackgroudFixtures.Length; i++)
            {
                map.BackgroudFixtures[i] = DlmFixture.ReadFromStream(map, reader);
            }

            map.ForegroundFixtures = new DlmFixture[reader.ReadByte()];
            for (int i = 0; i < map.ForegroundFixtures.Length; i++)
            {
                map.ForegroundFixtures[i] = DlmFixture.ReadFromStream(map, reader);
            }

            reader.ReadInt();
            map.GroundCRC = reader.ReadInt();

            map.Layers = new DlmLayer[reader.ReadByte()];
            for (int i = 0; i < map.Layers.Length; i++)
            {
                map.Layers[i] = DlmLayer.ReadFromStream(map, reader);
            }

            map.Cells = new DlmCellData[CellCount];
            int?lastMoveZone = null;

            for (short i = 0; i < map.Cells.Length; i++)
            {
                map.Cells[i] = DlmCellData.ReadFromStream(i, map.Version, reader);
                if (!lastMoveZone.HasValue)
                {
                    lastMoveZone = map.Cells[i].MoveZone;
                }
                else if (lastMoveZone != map.Cells[i].MoveZone) // if a cell is different the new system is used
                {
                    map.UsingNewMovementSystem = true;
                }
            }

            return(map);
        }
Example #2
0
        public static DlmMap ReadFromStream(BigEndianReader givenReader, DlmReader dlmReader)
        {
            var reader = givenReader;
            var map = new DlmMap();

            map.Version = reader.ReadByte();
            map.Id = reader.ReadInt();

            if (map.Version >= 7)
            {
                map.Encrypted = reader.ReadBoolean();
                map.EncryptionVersion = reader.ReadByte();

                var len = reader.ReadInt();

                if (map.Encrypted)
                {
                    var key = dlmReader.DecryptionKey;

                    if (key == null && dlmReader.DecryptionKeyProvider != null)
                        key = dlmReader.DecryptionKeyProvider(map.Id);

                    if (key == null)
                    {
                        throw new InvalidOperationException(string.Format("Cannot decrypt the map {0} without decryption key", map.Id));
                    }

                    var data = reader.ReadBytes(len);
                    var encodedKey = Encoding.Default.GetBytes(key);

                    if (key.Length > 0)
                    {
                        for (int i = 0; i < data.Length; i++)
                        {
                            data[i] = (byte)( data[i] ^ encodedKey[i % key.Length] );
                        }

                        reader = new BigEndianReader(new MemoryStream(data));
                    }
                }
            }

            map.RelativeId = reader.ReadUInt();
            map.MapType = reader.ReadByte();
            map.SubAreaId = reader.ReadInt();
            map.TopNeighbourId = reader.ReadInt();
            map.BottomNeighbourId = reader.ReadInt();
            map.LeftNeighbourId = reader.ReadInt();
            map.RightNeighbourId = reader.ReadInt();
            map.ShadowBonusOnEntities = reader.ReadInt();

            if (map.Version >= 3)
            {
                map.BackgroundColor = Color.FromArgb(reader.ReadByte(), reader.ReadByte(), reader.ReadByte());
            }

            if (map.Version >= 4)
            {
                map.ZoomScale = reader.ReadUShort();
                map.ZoomOffsetX = reader.ReadShort();
                map.ZoomOffsetY = reader.ReadShort();
            }

            map.UseLowPassFilter = reader.ReadByte() == 1;
            map.UseReverb = reader.ReadByte() == 1;

            if (map.UseReverb)
            {
                map.PresetId = reader.ReadInt();
            }
            {
                map.PresetId = -1;
            }

            map.BackgroudFixtures = new DlmFixture[reader.ReadByte()];
            for (int i = 0; i < map.BackgroudFixtures.Length; i++)
            {
                map.BackgroudFixtures[i] = DlmFixture.ReadFromStream(map, reader);
            }

            map.ForegroundFixtures = new DlmFixture[reader.ReadByte()];
            for (int i = 0; i < map.ForegroundFixtures.Length; i++)
            {
                map.ForegroundFixtures[i] = DlmFixture.ReadFromStream(map, reader);
            }

            reader.ReadInt();
            map.GroundCRC = reader.ReadInt();

            map.Layers = new DlmLayer[reader.ReadByte()];
            for (int i = 0; i < map.Layers.Length; i++)
            {
                map.Layers[i] = DlmLayer.ReadFromStream(map, reader);
            }

            map.Cells = new DlmCellData[CellCount];
            int? lastMoveZone = null;
            for (short i = 0; i < map.Cells.Length; i++)
            {
                map.Cells[i] = DlmCellData.ReadFromStream(map, i, reader);
                if (!lastMoveZone.HasValue)
                    lastMoveZone = map.Cells[i].MoveZone;
                else if (lastMoveZone != map.Cells[i].MoveZone) // if a cell is different the new system is used
                    map.UsingNewMovementSystem = true;
            }

            return map;
        }