Esempio n. 1
0
        /// <summary>
        /// Gibt den Planeten mit der angegebenen ID zurück
        /// </summary>
        /// <param name="id">Die Planteten-ID des gewünschten Planeten</param>
        /// <returns>Der gewünschte Planet, falls er existiert</returns>
        public IPlanet GetPlanet(int id)
        {
            if (universe == null)
            {
                throw new Exception("No Universe loaded");
            }

            IPlanet planet;

            if (!planets.TryGetValue(id, out planet))
            {
                // Versuch vorhandenen Planeten zu laden
                planet = persistenceManager.LoadPlanet(universe.Id, id);
                if (planet == null)
                {
                    // Keiner da -> neu erzeugen
                    Random        rand       = new Random(universe.Seed + id);
                    var           generators = MapGeneratorManager.GetMapGenerators().ToArray();
                    int           index      = rand.Next(generators.Length - 1);
                    IMapGenerator generator  = generators[index];
                    planet = generator.GeneratePlanet(universe.Id, id, universe.Seed + id);
                    // persistenceManager.SavePlanet(universe.Id, planet);
                }

                planets.Add(id, planet);
            }

            return(planet);
        }
        /// <summary>
        /// Lädt einen Planeten.
        /// </summary>
        /// <param name="universeGuid">Guid des Universums</param>
        /// <param name="planetId">Index des Planeten</param>
        /// <returns></returns>
        public IPlanet LoadPlanet(Guid universeGuid, int planetId)
        {
            string file          = Path.Combine(GetRoot(), universeGuid.ToString(), planetId.ToString(), PlanetFilename);
            string generatorInfo = Path.Combine(GetRoot(), universeGuid.ToString(), planetId.ToString(), PlanetGeneratorInfo);

            if (!File.Exists(generatorInfo) || !File.Exists(file))
            {
                return(null);
            }

            IMapGenerator generator = null;

            using (Stream stream = File.Open(generatorInfo, FileMode.Create, FileAccess.Read))
            {
                using (BinaryReader bw = new BinaryReader(stream))
                {
                    string generatorName = bw.ReadString();
                    generator = MapGeneratorManager.GetMapGenerators().FirstOrDefault(g => g.GetType().FullName.Equals(generatorName));
                }
            }

            if (generator == null)
            {
                throw new Exception("Unknown Generator");
            }


            using (Stream stream = File.Open(file, FileMode.Open, FileAccess.Read))
            {
                using (GZipStream zip = new GZipStream(stream, CompressionMode.Decompress))
                {
                    return(generator.GeneratePlanet(zip));
                }
            }
        }
        private ResourceManager()
        {
            mapGenerator     = MapGeneratorManager.GetMapGenerators().First();
            chunkSerializer  = new ChunkSerializer();
            chunkPersistence = new ChunkDiskPersistence(chunkSerializer);

            _managers = new Dictionary <int, PlanetResourceManager>();
            _planets  = new[] { loadPlanet(0) };

            //planetCache = new Cache<int, IPlanet>(1, loadPlanet, savePlanet);
            //chunkCache = new Cache<PlanetIndex3, IChunk>(CacheSize, loadChunk, saveChunk);

            bool.TryParse(ConfigurationManager.AppSettings["DisablePersistence"], out disablePersistence);
        }