コード例 #1
0
ファイル: Map.cs プロジェクト: TheDireMaster/fCraft
        static Map LoadFCM(World _world, string fileName)
        {
            FileStream fs  = null;
            Map        map = new Map(_world);

            try {
                fs = File.OpenRead(fileName);
                if (map.ReadHeader(fs))
                {
                    map.ReadMetadata(fs);
                    map.ReadBlocks(fs);
                    if (!map.ValidateBlockTypes())
                    {
                        throw new Exception("Invalid block types detected. File is possibly corrupt.");
                    }
                    _world.log.Log("Loaded map succesfully from {0}", LogType.SystemActivity, fileName);
                    return(map);
                }
                else
                {
                    return(null);
                }
            } catch (EndOfStreamException) {
                _world.log.Log("Map.Load: Unexpected end of file - possible corruption!", LogType.Error);
                return(null);
            } catch (Exception ex) {
                _world.log.Log("Map.Load: Error trying to read from \"{0}\": {1}", LogType.Error, fileName, ex.Message);
                return(null);
            } finally {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
コード例 #2
0
ファイル: MapFCMv2.cs プロジェクト: asiekierka/afCraft
        public Map Load( Stream MapStream )
        {
            // Reset the seeker to the front of the stream
            // This should probably be done differently.
            MapStream.Seek( 0, SeekOrigin.Begin );

            Map map = new Map();

            BinaryReader reader = new BinaryReader( MapStream );

            // Read in the magic number
            if ( reader.ReadUInt32() != Identifier ) {
                throw new FormatException();
            }

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

            // Read in the spawn location
            map.spawn.x = reader.ReadInt16();
            map.spawn.y = reader.ReadInt16();
            map.spawn.h = reader.ReadInt16();

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

            // Read the metadata
            map.ReadMetadata( reader );

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

            // Read in the map data
            map.blocks = new Byte[map.GetBlockCount()];
            using ( GZipStream decompressor = new GZipStream( MapStream, CompressionMode.Decompress, true ) ) {
                decompressor.Read( map.blocks, 0, map.blocks.Length );
            }

            return map;
        }