@bscotch/gamemaker-project 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +29 -0
- package/README.md +138 -2
- package/dist/asset.d.ts +114 -0
- package/dist/asset.d.ts.map +1 -0
- package/dist/asset.js +259 -0
- package/dist/asset.js.map +1 -0
- package/dist/discovery.d.ts +25 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +33 -0
- package/dist/discovery.js.map +1 -0
- package/dist/import.d.ts +90 -0
- package/dist/import.d.ts.map +1 -0
- package/dist/import.js +197 -0
- package/dist/import.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/objectEvents.d.ts +36 -0
- package/dist/objectEvents.d.ts.map +1 -0
- package/dist/objectEvents.js +140 -0
- package/dist/objectEvents.js.map +1 -0
- package/dist/project.d.ts +187 -0
- package/dist/project.d.ts.map +1 -0
- package/dist/project.js +732 -0
- package/dist/project.js.map +1 -0
- package/dist/project.state.d.ts +220 -0
- package/dist/project.state.d.ts.map +1 -0
- package/dist/project.state.js +718 -0
- package/dist/project.state.js.map +1 -0
- package/dist/refs.d.ts +20 -0
- package/dist/refs.d.ts.map +1 -0
- package/dist/refs.js +53 -0
- package/dist/refs.js.map +1 -0
- package/dist/shaderDefaults.d.ts +3 -0
- package/dist/shaderDefaults.d.ts.map +1 -0
- package/dist/shaderDefaults.js +32 -0
- package/dist/shaderDefaults.js.map +1 -0
- package/dist/spine.d.ts +20 -0
- package/dist/spine.d.ts.map +1 -0
- package/dist/spine.js +47 -0
- package/dist/spine.js.map +1 -0
- package/dist/spine.types.d.ts +89 -0
- package/dist/spine.types.d.ts.map +1 -0
- package/dist/spine.types.js +2 -0
- package/dist/spine.types.js.map +1 -0
- package/dist/transaction.d.ts +91 -0
- package/dist/transaction.d.ts.map +1 -0
- package/dist/transaction.js +203 -0
- package/dist/transaction.js.map +1 -0
- package/dist/util.d.ts +56 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +163 -0
- package/dist/util.js.map +1 -0
- package/package.json +45 -3
|
@@ -0,0 +1,718 @@
|
|
|
1
|
+
import { stitchConfigFilename, stitchConfigSchema, } from '@bscotch/stitch-config';
|
|
2
|
+
import { Yy, yySchemas, } from '@bscotch/yy';
|
|
3
|
+
import { watch as fsWatch } from 'node:fs';
|
|
4
|
+
import fs from 'node:fs/promises';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { Asset } from "./asset.js";
|
|
7
|
+
import { contentHash, ProjectTransaction } from "./transaction.js";
|
|
8
|
+
import { assert, exists, findCaseInsensitive, folderPathToPosix, GameMakerProjectError, hash, key, listDir, mapConcurrent, SerialQueue, sleep, toPosix, } from "./util.js";
|
|
9
|
+
const SELF_WRITE_TTL_MS = 10_000;
|
|
10
|
+
/**
|
|
11
|
+
* The state of a GameMaker project, kept in sync with what's on disk.
|
|
12
|
+
*
|
|
13
|
+
* @internal Use {@link GameMakerProject}, which adds methods for changing it.
|
|
14
|
+
*/
|
|
15
|
+
export class ProjectState {
|
|
16
|
+
/** Absolute path to the yyp file */
|
|
17
|
+
yypPath;
|
|
18
|
+
/** Absolute path to the project's folder */
|
|
19
|
+
dir;
|
|
20
|
+
options;
|
|
21
|
+
_yyp;
|
|
22
|
+
yypHash = '';
|
|
23
|
+
yypMetaHash = '';
|
|
24
|
+
_config = stitchConfigSchema.parse({});
|
|
25
|
+
configHash = '';
|
|
26
|
+
assetsByName = new Map();
|
|
27
|
+
assetsByDir = new Map();
|
|
28
|
+
/** Resources listed in the yyp whose yy files could not be loaded */
|
|
29
|
+
unloaded = new Map();
|
|
30
|
+
queue = new SerialQueue();
|
|
31
|
+
listeners = new Map();
|
|
32
|
+
selfWrites = new Map();
|
|
33
|
+
gitDir;
|
|
34
|
+
/** Files that couldn't be read (e.g. mid-write), and how often we retried */
|
|
35
|
+
retries = new Map();
|
|
36
|
+
// Change coalescing
|
|
37
|
+
pending = new Set();
|
|
38
|
+
debounceTimer;
|
|
39
|
+
firstPendingAt = 0;
|
|
40
|
+
flushing;
|
|
41
|
+
constructor(yypPath, options) {
|
|
42
|
+
this.yypPath = path.resolve(yypPath);
|
|
43
|
+
this.dir = path.dirname(this.yypPath);
|
|
44
|
+
this.options = {
|
|
45
|
+
gitLockTimeoutMs: 10_000,
|
|
46
|
+
changeDebounceMs: 100,
|
|
47
|
+
changeMaxWaitMs: 2000,
|
|
48
|
+
...options,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/** Find the yyp file, given its path or its project's folder */
|
|
52
|
+
static async findYyp(yypPathOrDir) {
|
|
53
|
+
const resolved = path.resolve(yypPathOrDir);
|
|
54
|
+
if (resolved.toLowerCase().endsWith('.yyp')) {
|
|
55
|
+
assert(await exists(resolved), `Project file not found: ${resolved}`);
|
|
56
|
+
return resolved;
|
|
57
|
+
}
|
|
58
|
+
const yyps = (await listDir(resolved)).filter((f) => f.toLowerCase().endsWith('.yyp'));
|
|
59
|
+
assert(yyps.length, `No .yyp file found in ${resolved}`);
|
|
60
|
+
assert(yyps.length === 1, `Multiple .yyp files found in ${resolved}`);
|
|
61
|
+
return path.join(resolved, yyps[0]);
|
|
62
|
+
}
|
|
63
|
+
/** Read everything from disk. */
|
|
64
|
+
async load() {
|
|
65
|
+
const [yypText] = await Promise.all([
|
|
66
|
+
fs.readFile(this.yypPath, 'utf8'),
|
|
67
|
+
this.readConfig(),
|
|
68
|
+
this.findGitDir().then((d) => (this.gitDir = d)),
|
|
69
|
+
]);
|
|
70
|
+
this.setYyp(yypText);
|
|
71
|
+
const loaded = await mapConcurrent(this._yyp.resources, 64, (r) => this.loadAsset(r.id));
|
|
72
|
+
for (const asset of loaded)
|
|
73
|
+
if (asset)
|
|
74
|
+
this.registerAsset(asset);
|
|
75
|
+
}
|
|
76
|
+
// --- Project data ---
|
|
77
|
+
/** The yyp data, as read from disk. Do not mutate! */
|
|
78
|
+
get yyp() {
|
|
79
|
+
return this._yyp;
|
|
80
|
+
}
|
|
81
|
+
get name() {
|
|
82
|
+
return this._yyp.name;
|
|
83
|
+
}
|
|
84
|
+
/** The GameMaker IDE version that last saved the project */
|
|
85
|
+
get ideVersion() {
|
|
86
|
+
return this._yyp.MetaData.IDEVersion;
|
|
87
|
+
}
|
|
88
|
+
/** The project's Stitch config (`stitch.config.json`) */
|
|
89
|
+
get config() {
|
|
90
|
+
return this._config;
|
|
91
|
+
}
|
|
92
|
+
/** The names of the project's build configurations */
|
|
93
|
+
get configs() {
|
|
94
|
+
const names = [];
|
|
95
|
+
const visit = (config) => {
|
|
96
|
+
names.push(config.name);
|
|
97
|
+
config.children?.forEach(visit);
|
|
98
|
+
};
|
|
99
|
+
visit(this._yyp.configs);
|
|
100
|
+
return names;
|
|
101
|
+
}
|
|
102
|
+
/** The project's asset-tree folders, in POSIX form (`my/folder`) */
|
|
103
|
+
get folders() {
|
|
104
|
+
return this._yyp.Folders.map((f) => folderPathToPosix(f.folderPath));
|
|
105
|
+
}
|
|
106
|
+
/** The project's "Included Files", as listed in the yyp */
|
|
107
|
+
get includedFiles() {
|
|
108
|
+
return this._yyp.IncludedFiles;
|
|
109
|
+
}
|
|
110
|
+
get audioGroups() {
|
|
111
|
+
return this._yyp.AudioGroups;
|
|
112
|
+
}
|
|
113
|
+
get textureGroups() {
|
|
114
|
+
return this._yyp.TextureGroups;
|
|
115
|
+
}
|
|
116
|
+
/** The display name set in the project's Windows options, if any */
|
|
117
|
+
async getWindowsName() {
|
|
118
|
+
const file = path.join(this.dir, 'options/windows/options_windows.yy');
|
|
119
|
+
try {
|
|
120
|
+
const data = Yy.parse(await fs.readFile(file, 'utf8'));
|
|
121
|
+
return data.option_windows_display_name;
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
// --- Assets ---
|
|
128
|
+
/** All assets, in no particular order */
|
|
129
|
+
get assets() {
|
|
130
|
+
return this.assetsByName.values();
|
|
131
|
+
}
|
|
132
|
+
get assetCount() {
|
|
133
|
+
return this.assetsByName.size;
|
|
134
|
+
}
|
|
135
|
+
listAssets(kind) {
|
|
136
|
+
const all = [...this.assetsByName.values()];
|
|
137
|
+
return (kind ? all.filter((a) => a.kind === kind) : all);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Resources listed in the yyp whose yy files could not be read (e.g.
|
|
141
|
+
* because they are missing).
|
|
142
|
+
*/
|
|
143
|
+
get unloadedResources() {
|
|
144
|
+
return [...this.unloaded.values()];
|
|
145
|
+
}
|
|
146
|
+
/** Get an asset by name (case-insensitive) */
|
|
147
|
+
getAsset(ref) {
|
|
148
|
+
if (!ref)
|
|
149
|
+
return undefined;
|
|
150
|
+
if (typeof ref !== 'string') {
|
|
151
|
+
return this.assetsByName.get(key(ref.name)) === ref ? ref : undefined;
|
|
152
|
+
}
|
|
153
|
+
return this.assetsByName.get(key(ref));
|
|
154
|
+
}
|
|
155
|
+
/** Get an asset by name, throwing if it doesn't exist */
|
|
156
|
+
requireAsset(ref, kind) {
|
|
157
|
+
const asset = this.getAsset(ref);
|
|
158
|
+
const name = typeof ref === 'string' ? ref : ref.name;
|
|
159
|
+
assert(asset, `Asset "${name}" does not exist`);
|
|
160
|
+
assert(!kind || asset.kind === kind, `Asset "${name}" is not of kind "${kind}"`);
|
|
161
|
+
return asset;
|
|
162
|
+
}
|
|
163
|
+
/** Get the asset that a file belongs to (its yy file, GML files, etc.) */
|
|
164
|
+
getAssetByPath(file) {
|
|
165
|
+
const parts = toPosix(path.relative(this.dir, path.resolve(file))).split('/');
|
|
166
|
+
if (parts.length < 2 || parts[0] === '..')
|
|
167
|
+
return undefined;
|
|
168
|
+
return this.assetsByDir.get(key(`${parts[0]}/${parts[1]}`));
|
|
169
|
+
}
|
|
170
|
+
/** For an object, its parent object */
|
|
171
|
+
getParent(object) {
|
|
172
|
+
const parent = this.getAsset(this.getAsset(object)?.parentName);
|
|
173
|
+
return parent?.isKind('objects') ? parent : undefined;
|
|
174
|
+
}
|
|
175
|
+
/** For an object, its ancestors, starting with its parent */
|
|
176
|
+
getAncestors(object) {
|
|
177
|
+
const ancestors = [];
|
|
178
|
+
const seen = new Set();
|
|
179
|
+
let current = this.getParent(object);
|
|
180
|
+
while (current && !seen.has(current)) {
|
|
181
|
+
ancestors.push(current);
|
|
182
|
+
seen.add(current);
|
|
183
|
+
current = this.getParent(current);
|
|
184
|
+
}
|
|
185
|
+
return ancestors;
|
|
186
|
+
}
|
|
187
|
+
/** For an object, the objects whose parent it is */
|
|
188
|
+
getChildren(object) {
|
|
189
|
+
const name = key(typeof object === 'string' ? object : object.name);
|
|
190
|
+
return this.listAssets('objects').filter((o) => o.parentName && key(o.parentName) === name);
|
|
191
|
+
}
|
|
192
|
+
/** List the assets in a folder (`my/folder`) */
|
|
193
|
+
listAssetsInFolder(folder, options) {
|
|
194
|
+
const target = key(folderPathToPosix(folder));
|
|
195
|
+
return this.listAssets().filter((a) => options?.recursive ? a.isInFolder(target) : key(a.folder) === target);
|
|
196
|
+
}
|
|
197
|
+
// --- Events ---
|
|
198
|
+
/** Listen for an event. Returns a function that removes the listener. */
|
|
199
|
+
on(event, listener) {
|
|
200
|
+
let set = this.listeners.get(event);
|
|
201
|
+
if (!set)
|
|
202
|
+
this.listeners.set(event, (set = new Set()));
|
|
203
|
+
set.add(listener);
|
|
204
|
+
return () => set.delete(listener);
|
|
205
|
+
}
|
|
206
|
+
emit(events) {
|
|
207
|
+
for (const [name, ...args] of events) {
|
|
208
|
+
for (const listener of this.listeners.get(name) || []) {
|
|
209
|
+
try {
|
|
210
|
+
listener(...args);
|
|
211
|
+
}
|
|
212
|
+
catch (err) {
|
|
213
|
+
if (name !== 'error')
|
|
214
|
+
this.emitError(err);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
emitError(err) {
|
|
220
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
221
|
+
if (!this.listeners.get('error')?.size)
|
|
222
|
+
return;
|
|
223
|
+
this.emit([['error', error]]);
|
|
224
|
+
}
|
|
225
|
+
// --- Changes from outside ---
|
|
226
|
+
/**
|
|
227
|
+
* Tell the project that files have changed on disk (e.g. from an editor's
|
|
228
|
+
* file watcher). Changes are collected for a moment, then processed
|
|
229
|
+
* together: the project compares disk with what it has in memory, and
|
|
230
|
+
* emits events for whatever actually changed.
|
|
231
|
+
*
|
|
232
|
+
* Changes made by the project itself are recognized and ignored.
|
|
233
|
+
*/
|
|
234
|
+
notifyChanged(files) {
|
|
235
|
+
for (const file of typeof files === 'string' ? [files] : files) {
|
|
236
|
+
this.pending.add(path.resolve(file));
|
|
237
|
+
}
|
|
238
|
+
if (!this.pending.size)
|
|
239
|
+
return;
|
|
240
|
+
const now = Date.now();
|
|
241
|
+
if (!this.debounceTimer)
|
|
242
|
+
this.firstPendingAt = now;
|
|
243
|
+
clearTimeout(this.debounceTimer);
|
|
244
|
+
const wait = Math.min(this.options.changeDebounceMs, Math.max(0, this.firstPendingAt + this.options.changeMaxWaitMs - now));
|
|
245
|
+
this.debounceTimer = setTimeout(() => {
|
|
246
|
+
this.debounceTimer = undefined;
|
|
247
|
+
this.flushing = this.queue
|
|
248
|
+
.run(() => this.reconcilePending())
|
|
249
|
+
.catch((err) => this.emitError(err))
|
|
250
|
+
.finally(() => (this.flushing = undefined));
|
|
251
|
+
}, wait);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Watch the project folder for changes, using Node's file watcher.
|
|
255
|
+
* (Editors with their own file watchers can call {@link notifyChanged}
|
|
256
|
+
* instead.) Returns a function that stops watching.
|
|
257
|
+
*/
|
|
258
|
+
watch() {
|
|
259
|
+
const watcher = fsWatch(this.dir, { recursive: true }, (_event, filename) => {
|
|
260
|
+
if (!filename)
|
|
261
|
+
return;
|
|
262
|
+
const first = toPosix(filename.toString()).split('/')[0];
|
|
263
|
+
if (first.startsWith('.') || first === 'node_modules')
|
|
264
|
+
return;
|
|
265
|
+
this.notifyChanged(path.join(this.dir, filename.toString()));
|
|
266
|
+
});
|
|
267
|
+
watcher.on('error', (err) => this.emitError(err));
|
|
268
|
+
return () => watcher.close();
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Compare disk with memory and emit events for any differences. With no
|
|
272
|
+
* arguments, everything is checked. Waits for queued changes to finish.
|
|
273
|
+
*/
|
|
274
|
+
async reconcile(files) {
|
|
275
|
+
await this.queue.run(async () => {
|
|
276
|
+
await this.waitForGit();
|
|
277
|
+
if (files) {
|
|
278
|
+
for (const file of files)
|
|
279
|
+
this.pending.add(path.resolve(file));
|
|
280
|
+
await this.reconcilePending();
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
this.pending.clear();
|
|
284
|
+
await this.reconcileNow([], { full: true });
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
/** Resolves once all pending changes and transactions have been processed */
|
|
289
|
+
async idle() {
|
|
290
|
+
while (this.debounceTimer || this.flushing || this.queue.busy) {
|
|
291
|
+
if (this.debounceTimer)
|
|
292
|
+
await sleep(this.options.changeDebounceMs);
|
|
293
|
+
await this.flushing;
|
|
294
|
+
await this.queue.idle();
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
async reconcilePending() {
|
|
298
|
+
await this.waitForGit();
|
|
299
|
+
const files = [...this.pending];
|
|
300
|
+
this.pending.clear();
|
|
301
|
+
if (files.length)
|
|
302
|
+
await this.reconcileNow(files, {});
|
|
303
|
+
}
|
|
304
|
+
// --- Transactions ---
|
|
305
|
+
/**
|
|
306
|
+
* Run a set of changes as a transaction. Transactions (and the processing
|
|
307
|
+
* of external changes) run one at a time, in order.
|
|
308
|
+
*
|
|
309
|
+
* Before the transaction runs, pending external changes are processed and
|
|
310
|
+
* any in-progress git operation is waited for. Afterwards, the project's
|
|
311
|
+
* state is updated from disk and events are emitted.
|
|
312
|
+
*
|
|
313
|
+
* If `fn` throws, what can be undone is undone, and the error is rethrown.
|
|
314
|
+
*/
|
|
315
|
+
async transaction(fn) {
|
|
316
|
+
return await this.queue.run(async () => {
|
|
317
|
+
await this.reconcilePending();
|
|
318
|
+
const tx = new ProjectTransaction({
|
|
319
|
+
dir: this.dir,
|
|
320
|
+
yypPath: this.yypPath,
|
|
321
|
+
readYypFresh: () => this.readYypFresh(),
|
|
322
|
+
parseYy: (text, kind) => this.parseYy(text, kind),
|
|
323
|
+
recordSelfWrite: (file, content) => this.recordSelfWrite(file, content),
|
|
324
|
+
});
|
|
325
|
+
let result;
|
|
326
|
+
try {
|
|
327
|
+
result = await fn(tx);
|
|
328
|
+
const { errors } = await tx.commit();
|
|
329
|
+
for (const err of errors)
|
|
330
|
+
this.emitError(err);
|
|
331
|
+
}
|
|
332
|
+
catch (err) {
|
|
333
|
+
const { errors } = await tx.rollback();
|
|
334
|
+
for (const e of errors)
|
|
335
|
+
this.emitError(e);
|
|
336
|
+
await this.reconcileNow(tx.touched, { yyp: true, own: true });
|
|
337
|
+
throw err;
|
|
338
|
+
}
|
|
339
|
+
await this.reconcileNow(tx.touched, { yyp: tx.usesYyp, own: true });
|
|
340
|
+
return result;
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
async readYypFresh() {
|
|
344
|
+
const text = await fs.readFile(this.yypPath, 'utf8');
|
|
345
|
+
return hash(text) === this.yypHash
|
|
346
|
+
? structuredClone(this._yyp)
|
|
347
|
+
: Yy.parse(text, 'project');
|
|
348
|
+
}
|
|
349
|
+
parseYy(text, kind) {
|
|
350
|
+
return Yy.parse(text, kind in yySchemas ? kind : undefined);
|
|
351
|
+
}
|
|
352
|
+
recordSelfWrite(file, content) {
|
|
353
|
+
this.selfWrites.set(key(path.resolve(file)), {
|
|
354
|
+
hash: contentHash(content),
|
|
355
|
+
at: Date.now(),
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* If a reported change to `file` is one the project made itself (and the
|
|
360
|
+
* file hasn't changed since), forget it and return `true`.
|
|
361
|
+
*/
|
|
362
|
+
async isOwnWrite(file) {
|
|
363
|
+
const k = key(file);
|
|
364
|
+
const record = this.selfWrites.get(k);
|
|
365
|
+
if (!record)
|
|
366
|
+
return false;
|
|
367
|
+
if (Date.now() - record.at > SELF_WRITE_TTL_MS) {
|
|
368
|
+
this.selfWrites.delete(k);
|
|
369
|
+
return false;
|
|
370
|
+
}
|
|
371
|
+
let current;
|
|
372
|
+
try {
|
|
373
|
+
current = hash(await fs.readFile(file));
|
|
374
|
+
}
|
|
375
|
+
catch {
|
|
376
|
+
current = null;
|
|
377
|
+
}
|
|
378
|
+
if (current !== record.hash)
|
|
379
|
+
return false;
|
|
380
|
+
this.selfWrites.delete(k);
|
|
381
|
+
return true;
|
|
382
|
+
}
|
|
383
|
+
// --- Git ---
|
|
384
|
+
async findGitDir() {
|
|
385
|
+
let dir = this.dir;
|
|
386
|
+
while (true) {
|
|
387
|
+
const candidate = path.join(dir, '.git');
|
|
388
|
+
try {
|
|
389
|
+
const stat = await fs.stat(candidate);
|
|
390
|
+
if (stat.isDirectory())
|
|
391
|
+
return candidate;
|
|
392
|
+
// Worktrees and submodules have a `.git` file pointing to the real dir
|
|
393
|
+
const content = await fs.readFile(candidate, 'utf8');
|
|
394
|
+
const match = content.match(/^gitdir:\s*(.+)$/m);
|
|
395
|
+
if (match)
|
|
396
|
+
return path.resolve(dir, match[1].trim());
|
|
397
|
+
}
|
|
398
|
+
catch {
|
|
399
|
+
// Not here
|
|
400
|
+
}
|
|
401
|
+
const parent = path.dirname(dir);
|
|
402
|
+
if (parent === dir)
|
|
403
|
+
return undefined;
|
|
404
|
+
dir = parent;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
/** Wait while a git operation (e.g. a pull) is in progress */
|
|
408
|
+
async waitForGit() {
|
|
409
|
+
if (!this.gitDir)
|
|
410
|
+
return;
|
|
411
|
+
const lock = path.join(this.gitDir, 'index.lock');
|
|
412
|
+
const start = Date.now();
|
|
413
|
+
while (await exists(lock)) {
|
|
414
|
+
if (Date.now() - start > this.options.gitLockTimeoutMs)
|
|
415
|
+
return;
|
|
416
|
+
await sleep(50);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
// --- Reconciling memory with disk ---
|
|
420
|
+
setYyp(text) {
|
|
421
|
+
this._yyp = Yy.parse(text, 'project');
|
|
422
|
+
this.yypHash = hash(text);
|
|
423
|
+
this.yypMetaHash = hash(Yy.stringify({ ...this._yyp, resources: [] }));
|
|
424
|
+
}
|
|
425
|
+
async readConfig() {
|
|
426
|
+
const file = path.join(this.dir, stitchConfigFilename);
|
|
427
|
+
let text = '';
|
|
428
|
+
try {
|
|
429
|
+
text = await fs.readFile(file, 'utf8');
|
|
430
|
+
}
|
|
431
|
+
catch {
|
|
432
|
+
// No config
|
|
433
|
+
}
|
|
434
|
+
const h = hash(text);
|
|
435
|
+
if (h === this.configHash)
|
|
436
|
+
return false;
|
|
437
|
+
this.configHash = h;
|
|
438
|
+
if (!text) {
|
|
439
|
+
this._config = stitchConfigSchema.parse({});
|
|
440
|
+
return true;
|
|
441
|
+
}
|
|
442
|
+
try {
|
|
443
|
+
this._config = stitchConfigSchema.parse(JSON.parse(text));
|
|
444
|
+
}
|
|
445
|
+
catch (err) {
|
|
446
|
+
this.emitError(new GameMakerProjectError(`Invalid Stitch config: ${file}`, {
|
|
447
|
+
cause: err,
|
|
448
|
+
}));
|
|
449
|
+
this._config = stitchConfigSchema.parse({});
|
|
450
|
+
}
|
|
451
|
+
return true;
|
|
452
|
+
}
|
|
453
|
+
registerAsset(asset) {
|
|
454
|
+
this.assetsByName.set(key(asset.name), asset);
|
|
455
|
+
this.assetsByDir.set(key(this.relativeDir(asset)), asset);
|
|
456
|
+
this.unloaded.delete(key(asset.name));
|
|
457
|
+
}
|
|
458
|
+
unregisterAsset(asset) {
|
|
459
|
+
if (this.assetsByName.get(key(asset.name)) === asset) {
|
|
460
|
+
this.assetsByName.delete(key(asset.name));
|
|
461
|
+
}
|
|
462
|
+
const dirKey = key(this.relativeDir(asset));
|
|
463
|
+
if (this.assetsByDir.get(dirKey) === asset)
|
|
464
|
+
this.assetsByDir.delete(dirKey);
|
|
465
|
+
}
|
|
466
|
+
relativeDir(asset) {
|
|
467
|
+
return toPosix(path.relative(this.dir, asset.dir));
|
|
468
|
+
}
|
|
469
|
+
/** Find a resource's yy file (case-insensitively, if needed) */
|
|
470
|
+
async findYy(id) {
|
|
471
|
+
const expected = path.join(this.dir, id.path);
|
|
472
|
+
if (await exists(expected))
|
|
473
|
+
return expected;
|
|
474
|
+
const [kind] = id.path.split('/');
|
|
475
|
+
const dir = await findCaseInsensitive(path.join(this.dir, kind), id.name);
|
|
476
|
+
return dir && (await findCaseInsensitive(dir, `${id.name}.yy`));
|
|
477
|
+
}
|
|
478
|
+
async loadAsset(id) {
|
|
479
|
+
const kind = id.path.split('/')[0];
|
|
480
|
+
try {
|
|
481
|
+
const yyPath = await this.findYy(id);
|
|
482
|
+
assert(yyPath, `Could not find the yy file for "${id.path}"`);
|
|
483
|
+
const text = await fs.readFile(yyPath, 'utf8');
|
|
484
|
+
const asset = new Asset({
|
|
485
|
+
kind,
|
|
486
|
+
id: { name: id.name, path: id.path },
|
|
487
|
+
yyPath,
|
|
488
|
+
yy: this.parseYy(text, kind),
|
|
489
|
+
yyHash: hash(text),
|
|
490
|
+
dirEntries: await listDir(path.dirname(yyPath)),
|
|
491
|
+
});
|
|
492
|
+
return asset;
|
|
493
|
+
}
|
|
494
|
+
catch (err) {
|
|
495
|
+
this.unloaded.set(key(id.name), { name: id.name, path: id.path });
|
|
496
|
+
this.emitError(new GameMakerProjectError(`Could not load asset "${id.name}"`, {
|
|
497
|
+
cause: err,
|
|
498
|
+
}));
|
|
499
|
+
return undefined;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Update memory from disk for the given changed files, emitting events
|
|
504
|
+
* for whatever actually changed.
|
|
505
|
+
*/
|
|
506
|
+
async reconcileNow(files, options) {
|
|
507
|
+
const events = [];
|
|
508
|
+
const changes = new Map();
|
|
509
|
+
const change = (asset) => {
|
|
510
|
+
let c = changes.get(asset);
|
|
511
|
+
if (!c) {
|
|
512
|
+
c = {
|
|
513
|
+
asset,
|
|
514
|
+
yy: false,
|
|
515
|
+
relist: false,
|
|
516
|
+
gml: new Set(),
|
|
517
|
+
files: new Set(),
|
|
518
|
+
};
|
|
519
|
+
changes.set(asset, c);
|
|
520
|
+
}
|
|
521
|
+
return c;
|
|
522
|
+
};
|
|
523
|
+
const list = [...files].map((f) => path.resolve(f));
|
|
524
|
+
const yypKey = key(this.yypPath);
|
|
525
|
+
if (options.yyp || options.full || list.some((f) => key(f) === yypKey)) {
|
|
526
|
+
await this.reconcileYyp(events, options);
|
|
527
|
+
}
|
|
528
|
+
const addedNow = new Set(events.flatMap((e) => (e[0] === 'assetAdded' ? [e[1]] : [])));
|
|
529
|
+
let datafiles = false;
|
|
530
|
+
for (const file of list) {
|
|
531
|
+
const relative = toPosix(path.relative(this.dir, file));
|
|
532
|
+
const parts = relative.split('/');
|
|
533
|
+
if (parts[0] === '..' || key(file) === yypKey)
|
|
534
|
+
continue;
|
|
535
|
+
if (key(relative) === key(stitchConfigFilename)) {
|
|
536
|
+
if (await this.readConfig()) {
|
|
537
|
+
events.push(['configChanged', this._config]);
|
|
538
|
+
}
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
if (key(parts[0]) === 'datafiles') {
|
|
542
|
+
datafiles = true;
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
if (parts.length < 2)
|
|
546
|
+
continue;
|
|
547
|
+
const asset = this.assetsByDir.get(key(`${parts[0]}/${parts[1]}`));
|
|
548
|
+
if (asset && addedNow.has(asset))
|
|
549
|
+
continue; // Just loaded fresh
|
|
550
|
+
if (!asset) {
|
|
551
|
+
// Maybe an asset that couldn't be loaded before
|
|
552
|
+
await this.retryUnloaded(parts[1], events);
|
|
553
|
+
continue;
|
|
554
|
+
}
|
|
555
|
+
if (parts.length === 2) {
|
|
556
|
+
// The asset's folder itself changed (e.g. it was replaced)
|
|
557
|
+
const c = change(asset);
|
|
558
|
+
c.yy = true;
|
|
559
|
+
c.relist = true;
|
|
560
|
+
}
|
|
561
|
+
else if (key(file) === key(asset.yyPath)) {
|
|
562
|
+
change(asset).yy = true;
|
|
563
|
+
}
|
|
564
|
+
else if (!options.own && (await this.isOwnWrite(file))) {
|
|
565
|
+
continue;
|
|
566
|
+
}
|
|
567
|
+
else if (file.toLowerCase().endsWith('.gml')) {
|
|
568
|
+
change(asset).gml.add(file);
|
|
569
|
+
}
|
|
570
|
+
else {
|
|
571
|
+
change(asset).files.add(file);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
if (options.full) {
|
|
575
|
+
for (const asset of this.assetsByName.values()) {
|
|
576
|
+
const c = change(asset);
|
|
577
|
+
c.yy = true;
|
|
578
|
+
c.relist = true;
|
|
579
|
+
}
|
|
580
|
+
if (await this.readConfig())
|
|
581
|
+
events.push(['configChanged', this._config]);
|
|
582
|
+
}
|
|
583
|
+
if (datafiles)
|
|
584
|
+
events.push(['includedFilesChanged']);
|
|
585
|
+
await mapConcurrent([...changes.values()], 32, (c) => this.reconcileAsset(c, events));
|
|
586
|
+
this.emit(events);
|
|
587
|
+
}
|
|
588
|
+
/** Check a file again soon (a few times at most), e.g. after a partial read */
|
|
589
|
+
retryLater(file) {
|
|
590
|
+
const k = key(file);
|
|
591
|
+
const count = this.retries.get(k) ?? 0;
|
|
592
|
+
if (count >= 3) {
|
|
593
|
+
this.retries.delete(k);
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
this.retries.set(k, count + 1);
|
|
597
|
+
this.notifyChanged(file);
|
|
598
|
+
}
|
|
599
|
+
async retryUnloaded(dirName, events) {
|
|
600
|
+
const id = this.unloaded.get(key(dirName));
|
|
601
|
+
if (!id || this.assetsByName.has(key(id.name)))
|
|
602
|
+
return;
|
|
603
|
+
const asset = await this.loadAsset(id);
|
|
604
|
+
if (asset) {
|
|
605
|
+
this.registerAsset(asset);
|
|
606
|
+
events.push(['assetAdded', asset]);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
async reconcileYyp(events, options) {
|
|
610
|
+
let text;
|
|
611
|
+
try {
|
|
612
|
+
text = await fs.readFile(this.yypPath, 'utf8');
|
|
613
|
+
}
|
|
614
|
+
catch (err) {
|
|
615
|
+
this.emitError(err);
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
if (hash(text) === this.yypHash && !options.full)
|
|
619
|
+
return;
|
|
620
|
+
const oldMeta = this.yypMetaHash;
|
|
621
|
+
try {
|
|
622
|
+
this.setYyp(text);
|
|
623
|
+
}
|
|
624
|
+
catch (err) {
|
|
625
|
+
// Probably mid-write. Try again shortly.
|
|
626
|
+
this.emitError(new GameMakerProjectError('Could not parse the yyp file', {
|
|
627
|
+
cause: err,
|
|
628
|
+
}));
|
|
629
|
+
this.retryLater(this.yypPath);
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
this.retries.delete(key(this.yypPath));
|
|
633
|
+
if (this.yypMetaHash !== oldMeta)
|
|
634
|
+
events.push(['yypChanged']);
|
|
635
|
+
// Find added and removed resources
|
|
636
|
+
const listed = new Map();
|
|
637
|
+
for (const { id } of this._yyp.resources)
|
|
638
|
+
listed.set(key(id.name), id);
|
|
639
|
+
for (const asset of [...this.assetsByName.values()]) {
|
|
640
|
+
const id = listed.get(key(asset.name));
|
|
641
|
+
if (!id ||
|
|
642
|
+
key(id.path) !== key(asset.id.path) ||
|
|
643
|
+
id.name !== asset.name) {
|
|
644
|
+
this.unregisterAsset(asset);
|
|
645
|
+
events.push(['assetRemoved', asset]);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
for (const name of [...this.unloaded.keys()]) {
|
|
649
|
+
if (!listed.has(name))
|
|
650
|
+
this.unloaded.delete(name);
|
|
651
|
+
}
|
|
652
|
+
const added = [...listed.values()].filter((id) => !this.assetsByName.has(key(id.name)));
|
|
653
|
+
const loaded = await mapConcurrent(added, 64, (id) => this.loadAsset(id));
|
|
654
|
+
for (const asset of loaded) {
|
|
655
|
+
if (!asset)
|
|
656
|
+
continue;
|
|
657
|
+
this.registerAsset(asset);
|
|
658
|
+
events.push(['assetAdded', asset]);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
async reconcileAsset(c, events) {
|
|
662
|
+
const { asset } = c;
|
|
663
|
+
if (!this.assetsByName.has(key(asset.name)))
|
|
664
|
+
return; // Removed meanwhile
|
|
665
|
+
const change = {
|
|
666
|
+
yy: false,
|
|
667
|
+
gmlFilesAdded: [],
|
|
668
|
+
gmlFilesRemoved: [],
|
|
669
|
+
files: [...c.files],
|
|
670
|
+
};
|
|
671
|
+
const oldFiles = [...asset.gmlFiles];
|
|
672
|
+
const oldGml = new Set(oldFiles.map(key));
|
|
673
|
+
let relist = c.relist || c.gml.size > 0;
|
|
674
|
+
if (c.yy) {
|
|
675
|
+
try {
|
|
676
|
+
const text = await fs.readFile(asset.yyPath, 'utf8');
|
|
677
|
+
const h = hash(text);
|
|
678
|
+
if (h !== asset.yyHash) {
|
|
679
|
+
asset.update({
|
|
680
|
+
yy: this.parseYy(text, asset.kind),
|
|
681
|
+
yyHash: h,
|
|
682
|
+
dirEntries: await listDir(asset.dir),
|
|
683
|
+
});
|
|
684
|
+
change.yy = true;
|
|
685
|
+
relist = false;
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
catch (err) {
|
|
689
|
+
// Missing (e.g. mid-delete) or mid-write: the yyp or a later
|
|
690
|
+
// change event will sort it out.
|
|
691
|
+
if (await exists(asset.yyPath)) {
|
|
692
|
+
this.emitError(new GameMakerProjectError(`Could not read ${asset.yyPath}`, {
|
|
693
|
+
cause: err,
|
|
694
|
+
}));
|
|
695
|
+
this.retryLater(asset.yyPath);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
if (relist)
|
|
700
|
+
asset.updateGmlFiles(await listDir(asset.dir));
|
|
701
|
+
const newGml = new Set(asset.gmlFiles.map(key));
|
|
702
|
+
change.gmlFilesAdded = asset.gmlFiles.filter((f) => !oldGml.has(key(f)));
|
|
703
|
+
change.gmlFilesRemoved = oldFiles.filter((f) => !newGml.has(key(f)));
|
|
704
|
+
if (change.yy ||
|
|
705
|
+
change.gmlFilesAdded.length ||
|
|
706
|
+
change.gmlFilesRemoved.length ||
|
|
707
|
+
change.files.length) {
|
|
708
|
+
events.push(['assetChanged', asset, change]);
|
|
709
|
+
}
|
|
710
|
+
for (const file of c.gml) {
|
|
711
|
+
const k = key(file);
|
|
712
|
+
if (newGml.has(k) && oldGml.has(k)) {
|
|
713
|
+
events.push(['gmlChanged', asset, file]);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
//# sourceMappingURL=project.state.js.map
|