fCraft.Map.ValidateHeader C# (CSharp) Method

ValidateHeader() public method

public ValidateHeader ( ) : bool
return bool
        public bool ValidateHeader()
        {
            if ( !IsValidDimension( Width ) ) {
                Logger.Log( LogType.Error,
                            "Map.ValidateHeader: Unsupported map width: {0}.", Width );
                return false;
            }

            if ( !IsValidDimension( Length ) ) {
                Logger.Log( LogType.Error,
                            "Map.ValidateHeader: Unsupported map length: {0}.", Length );
                return false;
            }

            if ( !IsValidDimension( Height ) ) {
                Logger.Log( LogType.Error,
                            "Map.ValidateHeader: Unsupported map height: {0}.", Height );
                return false;
            }

            if ( Spawn.X > Width * 32 || Spawn.Y > Length * 32 || Spawn.Z > Height * 32 || Spawn.X < 0 || Spawn.Y < 0 || Spawn.Z < 0 ) {
                Logger.Log( LogType.Warning,
                            "Map.ValidateHeader: Spawn coordinates are outside the valid range! Using center of the map instead." );
                ResetSpawn();
            }

            return true;
        }

Usage Example

Example #1
0
        public Map Load( System.IO.Stream MapStream )
        {
            // Reset the seeker to the front of the stream
            // This should probably be done differently.
            MapStream.Seek( 0, SeekOrigin.Begin );

            // Setup a GZipStream to decompress and read the map file
            GZipStream gs = new GZipStream( MapStream, CompressionMode.Decompress, true );
            BinaryReader bs = new BinaryReader( gs );

            Map map = new Map();

            // Read in the magic number
            if ( bs.ReadUInt16() != 0x752 ) {
                throw new FormatException();
            }

            // Read in the map dimesions
            map.widthX = bs.ReadInt16();
            map.widthY = bs.ReadInt16();
            map.height = bs.ReadInt16();

            // Read in the spawn location
            map.spawn.x = (short)(bs.ReadInt16() * 32);
            map.spawn.y = (short)(bs.ReadInt16() * 32);
            map.spawn.h = (short)(bs.ReadInt16() * 32);

            // Read in the spawn orientation
            map.spawn.r = bs.ReadByte();
            map.spawn.l = bs.ReadByte();

            // Skip over the VisitPermission and BuildPermission bytes
            bs.ReadByte();
            bs.ReadByte();

            if( !map.ValidateHeader() ) {
                throw new Exception( "One or more of the map dimensions are invalid." );
            }

            // Read in the map data
            map.blocks = bs.ReadBytes( map.GetBlockCount() );

            for( int i = 0; i < map.blocks.Length; i++ ) {
                if( map.blocks[i] > 49 ) {
                    map.blocks[i] = mapping[map.blocks[i]];
                }
            }

            return map;
        }
All Usage Examples Of fCraft.Map::ValidateHeader