Example #1
0
        public void Init()
        {
            pathToDb       = Path.Combine(OutputPath, "chunks.sqlite");
            pathToDbUpdate = Path.Combine(OutputPath, "chunks-update.sqlite");
            pathToDbBackup = Path.Combine(OutputPath, "chunks-backup.sqlite");

            pathToMapUpdate = Path.Combine(OutputPath, "update", "dim" + Dimension + (string.IsNullOrEmpty(Profile) ? "" : $"_{Profile}"));
            pathToMap       = Path.Combine(OutputPath, "map", "dim" + Dimension + (string.IsNullOrEmpty(Profile) ? "" : $"_{Profile}"));

            isUpdate = File.Exists(pathToDb);

            NewInitialZoomLevel = 20;
            NewLastZoomLevel    = NewInitialZoomLevel - InitialZoomLevel;

            if (isUpdate)
            {
                Console.WriteLine("Found chunks.sqlite, this must be an update of the map");

                if (File.Exists(pathToDbUpdate))
                {
                    Console.WriteLine($"Deleting {pathToDbUpdate} old update database file");
                    File.Delete(pathToDbUpdate);
                    File.Delete(pathToDbUpdate + "-wal");
                    File.Delete(pathToDbUpdate + "-shm");
                }

                File.Copy(pathToDb, pathToDbUpdate);

                if (Directory.Exists(pathToMapUpdate) && DeleteExistingUpdateFolder)
                {
                    Console.WriteLine("Deleting old update in {0}", pathToMapUpdate);
                    DirectoryInfo di        = new DirectoryInfo(pathToMapUpdate);
                    var           files     = di.EnumerateFiles("*.*", SearchOption.AllDirectories);
                    var           fileInfos = files.ToList();
                    if (fileInfos.Any(x => x.Extension != "." + FileFormat))
                    {
                        Console.WriteLine("Can not delete the update folder, because there are files in it not generated by PapyrusCs");
                        foreach (var f in fileInfos.Where(x => x.Extension != "." + FileFormat))
                        {
                            Console.WriteLine("Unknown file {0}", f.FullName);
                        }
                        throw new InvalidOperationException("Can not delete the update folder, because there are files in it not generated by PapyrusCs");
                    }

                    foreach (var f in fileInfos)
                    {
                        Console.WriteLine("Deleting update file {0}", f.FullName);
                        f.Delete();
                    }
                }
            }

            var c = new DbCreator();

            db = c.CreateDbContext(pathToDbUpdate, true);
            db.Database.Migrate();

            var settings = db.Settings.FirstOrDefault(x => x.Dimension == Dimension && x.Profile == Profile);

            if (settings != null)
            {
                this.FileFormat         = settings.Format;
                this.FileQuality        = settings.Quality;
                this.ChunksPerDimension = settings.ChunksPerDimension;
                Console.WriteLine("Overriding settings with: Format {0}, Quality {1} ChunksPerDimension {2}", FileFormat, FileQuality, ChunksPerDimension);

                settings.MaxZoom = NewInitialZoomLevel;
                settings.MinZoom = NewLastZoomLevel;
                Console.WriteLine("Setting Zoom levels to {0} down to {1}", NewInitialZoomLevel, NewLastZoomLevel);
                db.SaveChanges();
            }
            else
            {
                settings = new Settings()
                {
                    Dimension          = Dimension,
                    Profile            = Profile,
                    Quality            = FileQuality,
                    Format             = FileFormat,
                    MaxZoom            = this.NewInitialZoomLevel,
                    MinZoom            = this.NewLastZoomLevel,
                    ChunksPerDimension = db.Settings.FirstOrDefault()?.ChunksPerDimension ?? this.ChunksPerDimension
                };
                db.Add(settings);
                db.SaveChanges();
            }

            renderedSubchunks = db.Checksums.Where(x => x.Profile == Profile).ToImmutableDictionary(
                x => new LevelDbWorldKey2(x.LevelDbKey), x => new KeyAndCrc(x.Id, x.Crc32));
            Console.WriteLine($"Found {renderedSubchunks.Count} subchunks which are already rendered");
        }