forked from foundryvtt/pf2e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.ts
239 lines (200 loc) · 8.08 KB
/
base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { ActorSourcePF2e } from "@actor/data/index.ts";
import { ItemSourcePF2e } from "@item/base/data/index.ts";
import { itemIsOfType } from "@item/helpers.ts";
import { MigrationRecord } from "@module/data.ts";
import { MigrationBase } from "@module/migration/base.ts";
import type { ScenePF2e, TokenDocumentPF2e } from "@scene";
interface CollectionDiff<T extends foundry.documents.ActiveEffectSource | ItemSourcePF2e> {
inserted: T[];
deleted: string[];
updated: T[];
}
export class MigrationRunnerBase {
migrations: MigrationBase[];
static LATEST_SCHEMA_VERSION = 0.911;
static MINIMUM_SAFE_VERSION = 0.634;
static RECOMMENDED_SAFE_VERSION = 0.781;
/** The minimum schema version for the foundry version number */
static FOUNDRY_SCHEMA_VERSIONS = {
0.8: 0.634,
9: 0.7,
10: 0.781,
11: 0.841,
};
constructor(migrations: MigrationBase[] = []) {
this.migrations = migrations.sort((a, b) => a.version - b.version);
}
needsMigration(currentVersion: number): boolean {
return currentVersion < (this.constructor as typeof MigrationRunnerBase).LATEST_SCHEMA_VERSION;
}
diffCollection(orig: ItemSourcePF2e[], updated: ItemSourcePF2e[]): CollectionDiff<ItemSourcePF2e> {
const diffs: CollectionDiff<ItemSourcePF2e> = {
inserted: [],
deleted: [],
updated: [],
};
const origSources: Map<string, ItemSourcePF2e> = new Map();
for (const source of orig) {
origSources.set(source._id!, source);
}
for (const source of updated) {
const origSource = origSources.get(source._id!);
if (origSource) {
// check to see if anything changed
if (JSON.stringify(origSource) !== JSON.stringify(source)) {
diffs.updated.push(source);
}
origSources.delete(source._id!);
} else {
// it's new
diffs.inserted.push(source);
}
}
// since we've been deleting them as we process, the ones remaining need to be deleted
for (const source of origSources.values()) {
diffs.deleted.push(source._id!);
}
return diffs;
}
async getUpdatedActor(actor: ActorSourcePF2e, migrations: MigrationBase[]): Promise<ActorSourcePF2e> {
const currentActor = fu.deepClone(actor);
for (const migration of migrations) {
for (const currentItem of currentActor.items) {
await migration.preUpdateItem?.(currentItem, currentActor);
if (currentItem.type === "consumable" && currentItem.system.spell) {
await migration.preUpdateItem?.(currentItem.system.spell);
}
if (itemIsOfType(currentItem, "armor", "equipment", "shield", "weapon")) {
for (const embed of currentItem.system.subitems) {
migration.preUpdateItem?.(embed);
}
}
}
}
for (const migration of migrations) {
await migration.updateActor?.(currentActor);
for (const currentItem of currentActor.items) {
await migration.updateItem?.(currentItem, currentActor);
// Handle embedded items
if (currentItem.type === "consumable" && currentItem.system.spell) {
await migration.updateItem?.(currentItem.system.spell, currentActor);
}
if (itemIsOfType(currentItem, "armor", "equipment", "shield", "weapon")) {
for (const embed of currentItem.system.subitems) {
migration.preUpdateItem?.(embed);
}
}
}
}
// Don't set schema record on compendium JSON
if ("game" in globalThis) {
const latestMigration = migrations.slice(-1)[0];
currentActor.system._migration ??= { version: null, previous: null };
this.#updateMigrationRecord(currentActor.system._migration, latestMigration);
for (const itemSource of currentActor.items) {
itemSource.system._migration ??= { version: null, previous: null };
this.#updateMigrationRecord(itemSource.system._migration, latestMigration);
}
}
return currentActor;
}
async getUpdatedItem(item: ItemSourcePF2e, migrations: MigrationBase[]): Promise<ItemSourcePF2e> {
const current = fu.deepClone(item);
for (const migration of migrations) {
await migration.preUpdateItem?.(current);
if (current.type === "consumable" && current.system.spell) {
await migration.preUpdateItem?.(current.system.spell);
}
if (itemIsOfType(current, "armor", "equipment", "shield", "weapon")) {
for (const embed of current.system.subitems) {
migration.preUpdateItem?.(embed);
}
}
}
for (const migration of migrations) {
await migration.updateItem?.(current);
// Handle embedded spells
if (current.type === "consumable" && current.system.spell) {
await migration.updateItem?.(current.system.spell);
}
}
if (migrations.length > 0) this.#updateMigrationRecord(current.system._migration, migrations.slice(-1)[0]);
return current;
}
async getUpdatedTable(
tableSource: foundry.documents.RollTableSource,
migrations: MigrationBase[],
): Promise<foundry.documents.RollTableSource> {
const current = fu.deepClone(tableSource);
for (const migration of migrations) {
try {
await migration.updateTable?.(current);
} catch (err) {
console.error(err);
}
}
return current;
}
async getUpdatedMacro(
macroSource: foundry.documents.MacroSource,
migrations: MigrationBase[],
): Promise<foundry.documents.MacroSource> {
const current = fu.deepClone(macroSource);
for (const migration of migrations) {
try {
await migration.updateMacro?.(current);
} catch (err) {
console.error(err);
}
}
return current;
}
async getUpdatedJournalEntry(
source: foundry.documents.JournalEntrySource,
migrations: MigrationBase[],
): Promise<foundry.documents.JournalEntrySource> {
const clone = fu.deepClone(source);
for (const migration of migrations) {
try {
await migration.updateJournalEntry?.(clone);
} catch (err) {
console.error(err);
}
}
return clone;
}
async getUpdatedToken(
token: TokenDocumentPF2e<ScenePF2e>,
migrations: MigrationBase[],
): Promise<foundry.documents.TokenSource> {
const current = token.toObject();
for (const migration of migrations) {
await migration.updateToken?.(current, token.actor, token.scene);
}
return current;
}
async getUpdatedUser(
userData: foundry.documents.UserSource,
migrations: MigrationBase[],
): Promise<foundry.documents.UserSource> {
const current = fu.deepClone(userData);
for (const migration of migrations) {
try {
await migration.updateUser?.(current);
} catch (err) {
console.error(err);
}
}
return current;
}
#updateMigrationRecord(migrations: MigrationRecord, latestMigration: MigrationBase): void {
if (!("game" in globalThis && latestMigration)) return;
const fromVersion = typeof migrations.version === "number" ? migrations.version : null;
migrations.version = latestMigration.version;
migrations.previous = {
schema: fromVersion,
foundry: game.version,
system: game.system.version,
};
}
}