Skip to content

Commit

Permalink
Catch up newer worlds on migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
stwlam committed Mar 7, 2021
1 parent 0817a52 commit d3f2953
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ declare global {
}

interface WorldSettingsStorage {
getItem(setting: 'pf2e.worldSchemaVersion'): string;
get(setting: 'pf2e.worldSchemaVersion'): string | undefined;
getItem(setting: 'pf2e.worldSchemaVersion'): string | null;
}

const BUILD_MODE: 'development' | 'production';
Expand Down
21 changes: 16 additions & 5 deletions src/scripts/hooks/render-settings.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { MigrationRunner } from '../../module/migration-runner';
import { Migrations } from '../../module/migrations';

export function listen(): void {
// Save the current world schema version if hasn't before.
Hooks.once('renderSettings', () => {
Hooks.once('renderSettings', async () => {
const storedSettings = game.settings.storage.get('world');
const storedSchemaVersion = storedSettings.getItem('pf2e.worldSchemaVersion');
if (game.user.isGM && !storedSchemaVersion) {
const currentVersion = game.settings.get('pf2e', 'worldSchemaVersion');
game.settings.set('pf2e', 'worldSchemaVersion', currentVersion);
const storedSchemaVersion = storedSettings.get('pf2e.worldSchemaVersion');
if (game.user.isGM && storedSchemaVersion === undefined) {
// If there is no stored schema version, the world is either new or wasn't receiving migrations since
// schema version 0.595
if ([game.actors.entities, game.items.entities].some((entities) => entities.length > 0)) {
await game.settings.set('pf2e', 'worldSchemaVersion', 0.595);
const migrationRunner = new MigrationRunner(Migrations.constructAll());
await migrationRunner.runMigration();
} else {
const currentVersion = game.settings.get('pf2e', 'worldSchemaVersion');
game.settings.set('pf2e', 'worldSchemaVersion', currentVersion);
}
}
});
}
2 changes: 1 addition & 1 deletion types/foundry-pc-types/types/core/settings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ declare class ClientSettings {
/**
* A simple interface for World settings storage which imitates the API provided by localStorage
*/
declare class WorldSettingsStorage {
declare class WorldSettingsStorage extends Map<string, unknown> {
constructor(settings: object);

getItem(key: string): unknown;
Expand Down

0 comments on commit d3f2953

Please sign in to comment.