Exemplo n.º 1
0
        public GridMapLoader(string filePath, int mapID, int x, int y)
        {
            MapID = mapID;
            X     = x;
            Y     = y;

            var file = Path.Combine(filePath, $"{MapID:D3}{y:D2}{x:D2}.map");

            if (!(FileExists = File.Exists(file)))
            {
                return;
            }

            using (var reader = new BinaryReader(File.OpenRead(file)))
            {
                FileHeader = reader.Read <MapFileHeader>();

                // if (header.AreaMapOffset != 0 && !LoadAreaData(reader, header.AreaMapOffset, header.AreaMapSize))
                //     throw new InvalidOperationException();

                // if (FileHeader.LiquidMapOffset != 0 && !LoadLiquidData(reader, FileHeader.LiquidMapOffset, FileHeader.LiquidMapSize))
                //     throw new InvalidOperationException();

                if (FileHeader.HeightMapOffset != 0 && !LoadHeightData(reader, FileHeader.HeightMapOffset, FileHeader.HeightMapSize))
                {
                    throw new InvalidOperationException();
                }

                if (FileHeader.HolesOffset != 0 && !LoadHolesData(reader, FileHeader.HolesOffset, FileHeader.HolesSize))
                {
                    throw new InvalidOperationException();
                }
            }
        }
Exemplo n.º 2
0
        /**
         * Opens the given map file, reads its header data and validates them.
         *
         * @param mapFile
         *            the map file.
         * @return a FileOpenResult containing an error message in case of a failure.
         * @throws ArgumentException
         *             if the given map file is null.
         */
        public void OpenFile(FileInfo mapFile)
        {
            try {
                if (mapFile == null)
                {
                    throw new ArgumentException("mapFile must not be null", "mapFile");
                }

                // make sure to close any previously opened file first
                CloseFile();

                // check if the file exists and is readable
                if (!mapFile.Exists)
                {
                    throw new System.IO.InvalidDataException("file does not exist: " + mapFile);
                }

                // open the file in read only mode
                this.inputFile = mapFile.OpenRead();
                this.fileSize  = this.inputFile.Length;

                this.readBuffer    = new BufferStream(this.inputFile);
                this.mapFileHeader = new MapFileHeader();
                this.mapFileHeader.ReadHeader(this.readBuffer, this.fileSize);
            } catch (IOException e) {
                // make sure that the file is closed
                CloseFile();
                throw new System.IO.InvalidDataException(e.Message);
            }
        }
Exemplo n.º 3
0
 private MapFile()
 {
     // only to create a dummy empty file.
     databaseIndexCache = null;
     fileSize           = 0;
     inputStream        = null;
     mapFileHeader      = null;
     timestamp          = DateTime.Now;
 }
Exemplo n.º 4
0
 private MapFile()
 {
     // only to create a dummy empty file.
     databaseIndexCache = null;
     fileSize           = 0;
     inputFile          = null;
     mapFileHeader      = null;
     readBuffer         = null;
     timestamp          = DateTimeHelperClass.CurrentUnixTimeMillis();
 }
Exemplo n.º 5
0
        private MapFile GenerateMapFile(CachedTagInstance scenarioTag, Blf mapInfo = null)
        {
            MapFile  map    = new MapFile();
            var      header = new MapFileHeader();
            Scenario scnr;

            using (var stream = CacheContext.OpenTagCacheRead())
            {
                var deserializer = new TagDeserializer(CacheContext.Version);
                scnr = (Scenario)CacheContext.Deserialize(stream, scenarioTag);
            }

            map.Version      = CacheContext.Version;
            map.EndianFormat = EndianFormat.LittleEndian;
            map.MapVersion   = MapFileVersion.HaloOnline;

            header.HeadTag = new Tag("head");
            header.FootTag = new Tag("foot");
            header.Version = (int)map.MapVersion;
            header.Build   = CacheVersionDetection.GetBuildName(CacheContext.Version);

            switch (scnr.MapType)
            {
            case ScenarioMapType.MainMenu:
                header.CacheType = CacheFileType.MainMenu;
                break;

            case ScenarioMapType.SinglePlayer:
                header.CacheType = CacheFileType.Campaign;
                break;

            case ScenarioMapType.Multiplayer:
                header.CacheType = CacheFileType.Multiplayer;
                break;
            }
            header.SharedType = CacheFileSharedType.None;

            header.MapId            = scnr.MapId;
            header.ScenarioTagIndex = scenarioTag.Index;
            header.Name             = scenarioTag.Name.Split('\\').Last();
            header.ScenarioPath     = scenarioTag.Name;

            map.Header = header;

            header.FileLength = 0x3390;

            if (mapInfo != null)
            {
                if (mapInfo.ContentFlags.HasFlag(BlfFileContentFlags.StartOfFile) && mapInfo.ContentFlags.HasFlag(BlfFileContentFlags.EndOfFile) && mapInfo.ContentFlags.HasFlag(BlfFileContentFlags.Scenario))
                {
                    map.MapFileBlf = mapInfo;
                }
            }
            return(map);
        }
Exemplo n.º 6
0
        /**
         * Closes the map file and destroys all internal caches. Has no effect if no map file is currently opened.
         */
        public void CloseFile()
        {
            this.mapFileHeader = null;

            if (this.databaseIndexCache != null)
            {
                this.databaseIndexCache = null;
            }

            if (this.inputFile != null)
            {
                this.inputFile.Close();
                this.inputFile = null;
            }

            this.readBuffer = null;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Opens the given map file, reads its header data and validates them.
        /// </summary>
        /// <param name="mapFile"> the map file. </param>
        /// <param name="language"> the language to use (may be null). </param>
        /// <exception cref="MapFileException"> if the given map file is null or invalid. </exception>
        public MapFile(IFile mapFile, string language) : base(language)
        {
            if (mapFile == null)
            {
                throw new MapFileException("mapFile must not be null");
            }
            try
            {
                // open the file in read only mode
                try
                {
                    this.inputFile = mapFile.OpenAsync(FileAccess.Read).Result;

                    if (this.inputFile == null)
                    {
                        throw new MapFileException("cannot read file: " + mapFile);
                    }
                }
                catch
                {
                    throw new MapFileException("cannot read file: " + mapFile);
                }

                this.fileSize = this.inputFile.Length;

                this.readBuffer    = new ReadBuffer(this.inputFile);
                this.mapFileHeader = new MapFileHeader();
                this.mapFileHeader.ReadHeader(this.readBuffer, this.fileSize);
                this.databaseIndexCache = new IndexCache(this.inputFile, INDEX_CACHE_SIZE);

                // TODO: Find the creation date of file
                this.timestamp = DateTimeHelperClass.CurrentUnixTimeMillis();                  // mapFile.lastModified();
            }
            catch (Exception e)
            {
                LOGGER.Log(LogLevel.Fatal, e.Message, e);
                // make sure that the file is closed
                CloseFile();
                throw new MapFileException(e.Message);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Opens the given map file, reads its header data and validates them.
        /// </summary>
        /// <param name="stream"> the map file. </param>
        /// <param name="language"> the language to use (may be null). </param>
        /// <exception cref="MapFileException"> if the given map file is null or invalid. </exception>
        public MapFile(Stream stream, string language)
        {
            this.preferredLanguage = language;

            if (stream == null)
            {
                throw new MapFileException("map file must not be null");
            }

            this.inputStream = stream;

            this.fileSize = this.inputStream.Length;
            ReadBuffer readBuffer = new ReadBuffer(this.inputStream);

            this.mapFileHeader = new MapFileHeader();
            this.mapFileHeader.ReadHeader(readBuffer, this.fileSize);
            this.databaseIndexCache = new IndexCache(this.inputStream, INDEX_CACHE_SIZE);

            // TODO: Find the creation date of file
            this.timestamp = DateTime.Now;  // mapFile.lastModified();
        }
Exemplo n.º 9
0
        public void GetTile()
        {
            // arrange
            var names           = Assembly.GetExecutingAssembly().GetManifestResourceNames();
            var mapsforgeStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("mapsforge_tests.testdata.140.map");

            var readBuffer    = new ReadBuffer(mapsforgeStream);
            var mapFileHeader = new MapFileHeader();

            mapFileHeader.ReadHeader(readBuffer, mapsforgeStream.Length);

            // assert
            Assert.IsTrue(mapsforgeStream != null);
            Assert.IsTrue(mapFileHeader.MapFileInfo.FileVersion == 3);
            Assert.IsTrue(mapFileHeader.MapFileInfo.PoiTags.Length == 4);
            Assert.IsTrue(mapFileHeader.MapFileInfo.PoiTags[0] == "place=village");
            Assert.IsTrue(mapFileHeader.MapFileInfo.WayTags.Length == 18);
            Assert.IsTrue(mapFileHeader.MapFileInfo.WayTags[0] == "natural=coastline");

            // todo: read poi's and ways
        }