示例#1
0
        public static bool CanLoadMap(string vmapPath, uint mapID, uint tileX, uint tileY, VMapManager vm)
        {
            string fullname = vmapPath + VMapManager.getMapFileName(mapID);

            if (!File.Exists(fullname))
            {
                return(false);
            }

            using (BinaryReader reader = new BinaryReader(new FileStream(fullname, FileMode.Open, FileAccess.Read)))
            {
                if (reader.ReadStringFromChars(8) != MapConst.VMapMagic)
                {
                    return(false);
                }
            }

            FileStream stream = OpenMapTileFile(vmapPath, mapID, tileX, tileY, vm);

            if (stream == null)
            {
                return(false);
            }

            using (BinaryReader reader = new BinaryReader(stream))
            {
                if (reader.ReadStringFromChars(8) != MapConst.VMapMagic)
                {
                    return(false);
                }
            }

            return(true);
        }
示例#2
0
        public static LoadResult CanLoadMap(string vmapPath, uint mapID, uint tileX, uint tileY, VMapManager vm)
        {
            string fullname = vmapPath + VMapManager.GetMapFileName(mapID);

            if (!File.Exists(fullname))
            {
                return(LoadResult.FileNotFound);
            }

            using (BinaryReader reader = new BinaryReader(new FileStream(fullname, FileMode.Open, FileAccess.Read)))
            {
                if (reader.ReadStringFromChars(8) != MapConst.VMapMagic)
                {
                    return(LoadResult.VersionMismatch);
                }
            }

            FileStream stream = OpenMapTileFile(vmapPath, mapID, tileX, tileY, vm);

            if (stream == null)
            {
                return(LoadResult.FileNotFound);
            }

            using (BinaryReader reader = new BinaryReader(stream))
            {
                if (reader.ReadStringFromChars(8) != MapConst.VMapMagic)
                {
                    return(LoadResult.VersionMismatch);
                }
            }

            return(LoadResult.Success);
        }
示例#3
0
        public void UnloadMapTile(uint tileX, uint tileY, VMapManager vm)
        {
            uint tileID = PackTileID(tileX, tileY);
            var  tile   = iLoadedTiles.LookupByKey(tileID);

            if (!iLoadedTiles.ContainsKey(tileID))
            {
                Log.outError(LogFilter.Server, "StaticMapTree.UnloadMapTile() : trying to unload non-loaded tile - Map:{0} X:{1} Y:{2}", iMapID, tileX, tileY);
                return;
            }
            if (tile) // file associated with tile
            {
                FileStream stream = OpenMapTileFile(VMapManager.VMapPath, iMapID, tileX, tileY, vm);
                if (stream != null)
                {
                    using (BinaryReader reader = new BinaryReader(stream))
                    {
                        bool result = true;
                        if (reader.ReadStringFromChars(8) != MapConst.VMapMagic)
                        {
                            result = false;
                        }

                        uint numSpawns = reader.ReadUInt32();
                        for (uint i = 0; i < numSpawns && result; ++i)
                        {
                            // read model spawns
                            ModelSpawn spawn;
                            result = ModelSpawn.ReadFromFile(reader, out spawn);
                            if (result)
                            {
                                // release model instance
                                vm.ReleaseModelInstance(spawn.name);

                                // update tree
                                if (!iSpawnIndices.ContainsKey(spawn.Id))
                                {
                                    result = false;
                                }
                                else
                                {
                                    uint referencedNode = iSpawnIndices[spawn.Id];
                                    if (!iLoadedSpawns.ContainsKey(referencedNode))
                                    {
                                        Log.outError(LogFilter.Server, "StaticMapTree.UnloadMapTile() : trying to unload non-referenced model '{0}' (ID:{1})", spawn.name, spawn.Id);
                                    }
                                    else if (--iLoadedSpawns[referencedNode] == 0)
                                    {
                                        iTreeValues[referencedNode].SetUnloaded();
                                        iLoadedSpawns.Remove(referencedNode);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            iLoadedTiles.Remove(tileID);
        }
示例#4
0
 public void UnloadMap(VMapManager vm)
 {
     foreach (var id in iLoadedSpawns)
     {
         iTreeValues[id.Key].SetUnloaded();
         for (uint refCount = 0; refCount < id.Key; ++refCount)
         {
             vm.ReleaseModelInstance(iTreeValues[id.Key].name);
         }
     }
     iLoadedSpawns.Clear();
     iLoadedTiles.Clear();
 }
示例#5
0
        public bool InitMap(string fname, VMapManager vm)
        {
            Log.outDebug(LogFilter.Maps, "StaticMapTree.InitMap() : initializing StaticMapTree '{0}'", fname);
            bool success = false;

            if (!File.Exists(fname))
            {
                return(false);
            }
            char tiled = '0';

            using (BinaryReader reader = new BinaryReader(new FileStream(fname, FileMode.Open, FileAccess.Read)))
            {
                var magic = reader.ReadStringFromChars(8);
                tiled = reader.ReadChar();
                var node = reader.ReadStringFromChars(4);

                if (magic == MapConst.VMapMagic && node == "NODE" && iTree.readFromFile(reader))
                {
                    iNTreeValues = iTree.primCount();
                    iTreeValues  = new ModelInstance[iNTreeValues];
                    success      = reader.ReadStringFromChars(4) == "GOBJ";
                }

                iIsTiled = (tiled == 1);

                // global model spawns
                // only non-tiled maps have them, and if so exactly one (so far at least...)
                ModelSpawn spawn;
                if (!iIsTiled && ModelSpawn.readFromFile(reader, out spawn))
                {
                    WorldModel model = vm.acquireModelInstance(spawn.name);
                    Log.outDebug(LogFilter.Maps, "StaticMapTree.InitMap() : loading {0}", spawn.name);
                    if (model != null)
                    {
                        // assume that global model always is the first and only tree value (could be improved...)
                        iTreeValues[0]   = new ModelInstance(spawn, model);
                        iLoadedSpawns[0] = 1;
                    }
                    else
                    {
                        success = false;
                        Log.outError(LogFilter.Server, "StaticMapTree.InitMap() : could not acquire WorldModel for '{0}'", spawn.name);
                    }
                }
            }
            return(success);
        }
示例#6
0
        public static bool CanLoadMap(string vmapPath, uint mapID, uint tileX, uint tileY)
        {
            string fullname = vmapPath + VMapManager.getMapFileName(mapID);
            bool   success  = true;

            if (!File.Exists(fullname))
            {
                return(false);
            }

            using (BinaryReader reader = new BinaryReader(new FileStream(fullname, FileMode.Open, FileAccess.Read)))
            {
                if (reader.ReadStringFromChars(8) != MapConst.VMapMagic)
                {
                    return(false);
                }
                char tiled = reader.ReadChar();
                if (tiled == 1)
                {
                    string tilefile = vmapPath + getTileFileName(mapID, tileX, tileY);
                    if (!File.Exists(tilefile))
                    {
                        return(false);
                    }

                    using (BinaryReader reader1 = new BinaryReader(new FileStream(tilefile, FileMode.Open, FileAccess.Read)))
                    {
                        if (reader1.ReadStringFromChars(8) != MapConst.VMapMagic)
                        {
                            success = false;
                        }
                    }
                }
            }
            return(success);
        }
示例#7
0
        static FileStream OpenMapTileFile(string vmapPath, uint mapID, uint tileX, uint tileY, VMapManager vm)
        {
            string tilefile = vmapPath + GetTileFileName(mapID, tileX, tileY);

            if (!File.Exists(tilefile))
            {
                int parentMapId = vm.GetParentMapId(mapID);
                while (parentMapId != -1)
                {
                    tilefile = vmapPath + GetTileFileName((uint)parentMapId, tileX, tileY);
                    if (File.Exists(tilefile))
                    {
                        break;
                    }

                    parentMapId = vm.GetParentMapId((uint)parentMapId);
                }
            }

            if (!File.Exists(tilefile))
            {
                return(null);
            }

            return(new FileStream(tilefile, FileMode.Open, FileAccess.Read));
        }
示例#8
0
        public bool LoadMapTile(uint tileX, uint tileY, VMapManager vm)
        {
            if (iTreeValues == null)
            {
                Log.outError(LogFilter.Server, "StaticMapTree.LoadMapTile() : tree has not been initialized [{0}, {1}]", tileX, tileY);
                return(false);
            }
            bool result = true;

            FileStream stream = OpenMapTileFile(VMapManager.VMapPath, iMapID, tileX, tileY, vm);

            if (stream == null)
            {
                iLoadedTiles[PackTileID(tileX, tileY)] = false;
            }
            else
            {
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    if (reader.ReadStringFromChars(8) != MapConst.VMapMagic)
                    {
                        return(false);
                    }

                    uint numSpawns = reader.ReadUInt32();

                    for (uint i = 0; i < numSpawns && result; ++i)
                    {
                        // read model spawns
                        ModelSpawn spawn;
                        result = ModelSpawn.ReadFromFile(reader, out spawn);
                        if (result)
                        {
                            // acquire model instance
                            WorldModel model = vm.AcquireModelInstance(spawn.name, spawn.flags);
                            if (model == null)
                            {
                                Log.outError(LogFilter.Server, "StaticMapTree.LoadMapTile() : could not acquire WorldModel [{0}, {1}]", tileX, tileY);
                            }

                            // update tree
                            if (iSpawnIndices.ContainsKey(spawn.Id))
                            {
                                uint referencedVal = iSpawnIndices[spawn.Id];
                                if (!iLoadedSpawns.ContainsKey(referencedVal))
                                {
                                    if (referencedVal >= iNTreeValues)
                                    {
                                        Log.outError(LogFilter.Maps, "StaticMapTree.LoadMapTile() : invalid tree element ({0}/{1}) referenced in tile {2}", referencedVal, iNTreeValues, stream.Name);
                                        continue;
                                    }

                                    iTreeValues[referencedVal]   = new ModelInstance(spawn, model);
                                    iLoadedSpawns[referencedVal] = 1;
                                }
                                else
                                {
                                    ++iLoadedSpawns[referencedVal];
                                }
                            }
                            else
                            {
                                result = false;
                            }
                        }
                    }
                }
                iLoadedTiles[PackTileID(tileX, tileY)] = true;
            }
            return(result);
        }
示例#9
0
        public bool LoadMapTile(uint tileX, uint tileY, VMapManager vm)
        {
            if (!iIsTiled)
            {
                // currently, core creates grids for all maps, whether it has terrain tiles or not
                // so we need "fake" tile loads to know when we can unload map geometry
                iLoadedTiles[packTileID(tileX, tileY)] = false;
                return(true);
            }
            if (iTreeValues == null)
            {
                Log.outError(LogFilter.Server, "StaticMapTree.LoadMapTile() : tree has not been initialized [{0}, {1}]", tileX, tileY);
                return(false);
            }
            bool result = true;

            string tilefile = VMapManager.VMapPath + getTileFileName(iMapID, tileX, tileY);

            if (!File.Exists(tilefile))
            {
                iLoadedTiles[packTileID(tileX, tileY)] = false;
            }
            else
            {
                using (BinaryReader reader = new BinaryReader(new FileStream(tilefile, FileMode.Open, FileAccess.Read)))
                {
                    if (reader.ReadStringFromChars(8) != MapConst.VMapMagic)
                    {
                        return(false);
                    }

                    uint numSpawns = reader.ReadUInt32();

                    for (uint i = 0; i < numSpawns && result; ++i)
                    {
                        // read model spawns
                        ModelSpawn spawn;
                        result = ModelSpawn.readFromFile(reader, out spawn);
                        if (result)
                        {
                            // acquire model instance
                            WorldModel model = vm.acquireModelInstance(spawn.name);
                            if (model == null)
                            {
                                Log.outError(LogFilter.Server, "StaticMapTree.LoadMapTile() : could not acquire WorldModel [{0}, {1}]", tileX, tileY);
                            }

                            // update tree
                            uint referencedVal = reader.ReadUInt32();
                            if (!iLoadedSpawns.ContainsKey(referencedVal))
                            {
                                iTreeValues[referencedVal]   = new ModelInstance(spawn, model);
                                iLoadedSpawns[referencedVal] = 1;
                            }
                            else
                            {
                                ++iLoadedSpawns[referencedVal];
                            }
                        }
                        else
                        {
                            result = false;
                        }
                    }
                }
                iLoadedTiles[packTileID(tileX, tileY)] = true;
            }
            return(result);
        }
示例#10
0
        static TileFileOpenResult OpenMapTileFile(string vmapPath, uint mapID, uint tileX, uint tileY, VMapManager vm)
        {
            TileFileOpenResult result = new TileFileOpenResult();

            result.Name = vmapPath + GetTileFileName(mapID, tileX, tileY);

            if (File.Exists(result.Name))
            {
                result.UsedMapId = mapID;
                result.File      = new FileStream(result.Name, FileMode.Open, FileAccess.Read);
                return(result);
            }

            int parentMapId = vm.GetParentMapId(mapID);

            while (parentMapId != -1)
            {
                result.Name = vmapPath + GetTileFileName((uint)parentMapId, tileX, tileY);
                if (File.Exists(result.Name))
                {
                    result.File      = new FileStream(result.Name, FileMode.Open, FileAccess.Read);
                    result.UsedMapId = (uint)parentMapId;
                    return(result);
                }

                parentMapId = vm.GetParentMapId((uint)parentMapId);
            }

            return(result);
        }
示例#11
0
        public LoadResult LoadMapTile(uint tileX, uint tileY, VMapManager vm)
        {
            if (iTreeValues == null)
            {
                Log.outError(LogFilter.Server, "StaticMapTree.LoadMapTile() : tree has not been initialized [{0}, {1}]", tileX, tileY);
                return(LoadResult.ReadFromFileFailed);
            }

            LoadResult result = LoadResult.FileNotFound;

            TileFileOpenResult fileResult = OpenMapTileFile(VMapManager.VMapPath, iMapID, tileX, tileY, vm);

            if (fileResult.File != null)
            {
                result = LoadResult.Success;
                using (BinaryReader reader = new BinaryReader(fileResult.File))
                {
                    if (reader.ReadStringFromChars(8) != MapConst.VMapMagic)
                    {
                        result = LoadResult.VersionMismatch;
                    }

                    if (result == LoadResult.Success)
                    {
                        uint numSpawns = reader.ReadUInt32();
                        for (uint i = 0; i < numSpawns && result == LoadResult.Success; ++i)
                        {
                            // read model spawns
                            if (ModelSpawn.ReadFromFile(reader, out ModelSpawn spawn))
                            {
                                // acquire model instance
                                WorldModel model = vm.AcquireModelInstance(spawn.name, spawn.flags);
                                if (model == null)
                                {
                                    Log.outError(LogFilter.Server, "StaticMapTree.LoadMapTile() : could not acquire WorldModel [{0}, {1}]", tileX, tileY);
                                }

                                // update tree
                                if (iSpawnIndices.ContainsKey(spawn.Id))
                                {
                                    uint referencedVal = iSpawnIndices[spawn.Id];
                                    if (!iLoadedSpawns.ContainsKey(referencedVal))
                                    {
                                        if (referencedVal >= iNTreeValues)
                                        {
                                            Log.outError(LogFilter.Maps, "StaticMapTree.LoadMapTile() : invalid tree element ({0}/{1}) referenced in tile {2}", referencedVal, iNTreeValues, fileResult.Name);
                                            continue;
                                        }

                                        iTreeValues[referencedVal]   = new ModelInstance(spawn, model);
                                        iLoadedSpawns[referencedVal] = 1;
                                    }
                                    else
                                    {
                                        ++iLoadedSpawns[referencedVal];
                                    }
                                }
                                else if (iMapID == fileResult.UsedMapId)
                                {
                                    // unknown parent spawn might appear in because it overlaps multiple tiles
                                    // in case the original tile is swapped but its neighbour is now (adding this spawn)
                                    // we want to not mark it as loading error and just skip that model
                                    Log.outError(LogFilter.Maps, $"StaticMapTree.LoadMapTile() : invalid tree element (spawn {spawn.Id}) referenced in tile fileResult.Name{fileResult.Name} by map {iMapID}");
                                    result = LoadResult.ReadFromFileFailed;
                                }
                            }
                            else
                            {
                                Log.outError(LogFilter.Maps, $"StaticMapTree.LoadMapTile() : cannot read model from file (spawn index {i}) referenced in tile {fileResult.Name} by map {iMapID}");
                                result = LoadResult.ReadFromFileFailed;
                            }
                        }
                    }
                    iLoadedTiles[PackTileID(tileX, tileY)] = true;
                }
            }
            else
            {
                iLoadedTiles[PackTileID(tileX, tileY)] = false;
            }

            return(result);
        }