@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
package/dist/project.js
ADDED
|
@@ -0,0 +1,732 @@
|
|
|
1
|
+
import { getDefaultsForNewSound, isValidSoundName, isValidSpriteName, } from '@bscotch/stitch-config';
|
|
2
|
+
import { SoundChannel, SpriteOrigin, SpriteType, yyObjectEventSchema, yyRoomInstanceLayerSchema, yyRoomInstanceSchema, yyRoomSchema, yySpriteSchema, } from '@bscotch/yy';
|
|
3
|
+
import { randomBytes } from 'node:crypto';
|
|
4
|
+
import fs from 'node:fs/promises';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { instanceLayers, } from "./asset.js";
|
|
7
|
+
import { getEvent, getEventFromFilename, getEventFromLabel, } from "./objectEvents.js";
|
|
8
|
+
import { ProjectState } from "./project.state.js";
|
|
9
|
+
import { referencesPath, replaceRefs } from "./refs.js";
|
|
10
|
+
import { fshDefault, vshDefault } from "./shaderDefaults.js";
|
|
11
|
+
import { assert, assertIsValidIdentifier, exists, findCaseInsensitive, folderPathToPosix, folderPathToYy, getPngSize, key, listDir, listFilesRecursively, splitPath, toPosix, } from "./util.js";
|
|
12
|
+
/**
|
|
13
|
+
* A GameMaker project: its assets, folders, and settings, kept in sync with
|
|
14
|
+
* the files on disk.
|
|
15
|
+
*
|
|
16
|
+
* - **Disk is the source of truth.** Every change is made in a transaction
|
|
17
|
+
* that re-reads the files it changes, so external changes (GameMaker, git)
|
|
18
|
+
* are never overwritten with stale data.
|
|
19
|
+
* - **Changes are serialized.** Changes made through this class, and the
|
|
20
|
+
* processing of external changes, happen one at a time, in order.
|
|
21
|
+
* - **Changes are observable.** Use {@link on} to listen for assets being
|
|
22
|
+
* added, removed or changed — whether by this class or by something else.
|
|
23
|
+
*
|
|
24
|
+
* This class knows nothing about GML semantics. Updating code that refers to
|
|
25
|
+
* a renamed asset is the job of a higher layer.
|
|
26
|
+
*/
|
|
27
|
+
export class GameMakerProject extends ProjectState {
|
|
28
|
+
/**
|
|
29
|
+
* Load a project.
|
|
30
|
+
* @param yypPathOrDir The path to the yyp file, or to the folder containing it.
|
|
31
|
+
*/
|
|
32
|
+
static async open(yypPathOrDir, options = {}) {
|
|
33
|
+
const project = new GameMakerProject(await ProjectState.findYyp(yypPathOrDir), options);
|
|
34
|
+
await project.load();
|
|
35
|
+
return project;
|
|
36
|
+
}
|
|
37
|
+
// --- Project settings ---
|
|
38
|
+
/**
|
|
39
|
+
* Set the GameMaker IDE version listed in the yyp. (The IDE overwrites
|
|
40
|
+
* this with its own version when it saves the project.)
|
|
41
|
+
*/
|
|
42
|
+
async setIdeVersion(version) {
|
|
43
|
+
assert(/^\d+\.\d+\.\d+\.\d+$/.test(version), `Invalid version string: ${version}`);
|
|
44
|
+
await this.transaction(async (tx) => {
|
|
45
|
+
(await tx.yyp()).MetaData.IDEVersion = version;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Make the yyp's "Included Files" list match the files in the `datafiles`
|
|
50
|
+
* folder. Existing entries (and their settings) are kept.
|
|
51
|
+
*/
|
|
52
|
+
async syncIncludedFiles() {
|
|
53
|
+
await this.transaction(async (tx) => {
|
|
54
|
+
const yyp = await tx.yyp();
|
|
55
|
+
const root = path.join(this.dir, 'datafiles');
|
|
56
|
+
const existing = new Map(yyp.IncludedFiles.map((f) => [key(`${f.filePath}/${f.name}`), f]));
|
|
57
|
+
yyp.IncludedFiles = (await listFilesRecursively(root)).map((file) => {
|
|
58
|
+
const relative = toPosix(path.relative(this.dir, file));
|
|
59
|
+
const filePath = path.posix.dirname(relative);
|
|
60
|
+
const name = path.posix.basename(relative);
|
|
61
|
+
// Missing fields are filled in by the schema when the yyp is written
|
|
62
|
+
return (existing.get(key(relative)) ||
|
|
63
|
+
{ filePath, name });
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
// --- Folders ---
|
|
68
|
+
/**
|
|
69
|
+
* Create a folder in the asset tree (and any missing parent folders).
|
|
70
|
+
* Returns its POSIX path.
|
|
71
|
+
*/
|
|
72
|
+
async createFolder(folder) {
|
|
73
|
+
return await this.transaction(async (tx) => {
|
|
74
|
+
const parent = await tx.ensureFolder(folder);
|
|
75
|
+
return folderPathToPosix(parent.path);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/** Delete a folder and its subfolders. They must not contain any assets. */
|
|
79
|
+
async deleteFolder(folder) {
|
|
80
|
+
const posix = splitPath(folder).join('/');
|
|
81
|
+
assert(posix, 'Cannot delete the root folder');
|
|
82
|
+
await this.transaction(async (tx) => {
|
|
83
|
+
assert(!this.listAssetsInFolder(posix, { recursive: true }).length, `Cannot delete folder "${posix}": it contains assets`);
|
|
84
|
+
const yyp = await tx.yyp();
|
|
85
|
+
yyp.Folders = yyp.Folders.filter((f) => !isSameOrSubfolder(f.folderPath, posix));
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Rename or move a folder, along with its subfolders and assets. If the
|
|
90
|
+
* new folder already exists, the folders are merged.
|
|
91
|
+
*/
|
|
92
|
+
async renameFolder(from, to) {
|
|
93
|
+
const fromPosix = splitPath(from).join('/');
|
|
94
|
+
const toParts = splitPath(to);
|
|
95
|
+
const toPosixPath = toParts.join('/');
|
|
96
|
+
assert(fromPosix && toPosixPath, 'Cannot rename the root folder');
|
|
97
|
+
assert(!isSameOrSubfolder(folderPathToYy(toPosixPath), fromPosix), `Cannot move folder "${fromPosix}" into itself`);
|
|
98
|
+
return await this.transaction(async (tx) => {
|
|
99
|
+
const toMove = this.listAssetsInFolder(fromPosix, { recursive: true });
|
|
100
|
+
const yyp = await tx.yyp();
|
|
101
|
+
const movedFolders = [];
|
|
102
|
+
const fromPrefix = `folders/${fromPosix}/`;
|
|
103
|
+
const toPrefix = `folders/${toPosixPath}/`;
|
|
104
|
+
// Move the subfolders and remove the old folder
|
|
105
|
+
yyp.Folders = yyp.Folders.filter((f) => {
|
|
106
|
+
if (key(f.folderPath) === key(folderPathToYy(fromPosix)))
|
|
107
|
+
return false;
|
|
108
|
+
if (key(f.folderPath).startsWith(key(fromPrefix))) {
|
|
109
|
+
f.folderPath = toPrefix + f.folderPath.slice(fromPrefix.length);
|
|
110
|
+
movedFolders.push(folderPathToPosix(f.folderPath));
|
|
111
|
+
}
|
|
112
|
+
return true;
|
|
113
|
+
});
|
|
114
|
+
// Dedupe in case of merges
|
|
115
|
+
const seen = new Set();
|
|
116
|
+
yyp.Folders = yyp.Folders.filter((f) => {
|
|
117
|
+
const k = key(f.folderPath);
|
|
118
|
+
if (seen.has(k))
|
|
119
|
+
return false;
|
|
120
|
+
seen.add(k);
|
|
121
|
+
return true;
|
|
122
|
+
});
|
|
123
|
+
const target = await tx.ensureFolder(toParts);
|
|
124
|
+
for (const asset of toMove) {
|
|
125
|
+
const yy = (await tx.yy(asset));
|
|
126
|
+
const current = folderPathToPosix(yy.parent.path);
|
|
127
|
+
if (key(current) === key(fromPosix)) {
|
|
128
|
+
yy.parent = { ...target };
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
const rest = current.slice(fromPosix.length + 1);
|
|
132
|
+
yy.parent = {
|
|
133
|
+
name: yy.parent.name,
|
|
134
|
+
path: folderPathToYy(`${toPosixPath}/${rest}`),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
tx.stageYy(asset.yyPath, asset.kind, yy);
|
|
138
|
+
}
|
|
139
|
+
return { movedFolders, movedAssets: toMove };
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
/** Move an asset to a folder (created if it doesn't exist) */
|
|
143
|
+
async moveAsset(asset, folder) {
|
|
144
|
+
const target = this.requireAsset(asset);
|
|
145
|
+
await this.transaction(async (tx) => {
|
|
146
|
+
const yy = (await tx.yy(target));
|
|
147
|
+
yy.parent = await tx.ensureFolder(folder);
|
|
148
|
+
tx.stageYy(target.yyPath, target.kind, yy);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
// --- Creating assets ---
|
|
152
|
+
/**
|
|
153
|
+
* Create a script.
|
|
154
|
+
* @param assetPath The asset-tree path, ending with the script's name: `my/folder/scr_name`
|
|
155
|
+
*/
|
|
156
|
+
async createScript(assetPath, content = '') {
|
|
157
|
+
const { name, folder } = this.parseNewAssetPath(assetPath);
|
|
158
|
+
return await this.createAsset('scripts', name, async (tx, dir) => {
|
|
159
|
+
await tx.writeFile(path.join(dir, `${name}.gml`), content);
|
|
160
|
+
return { name, parent: await tx.ensureFolder(folder) };
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Create an object, with a Create event.
|
|
165
|
+
* @param assetPath The asset-tree path, ending with the object's name.
|
|
166
|
+
*/
|
|
167
|
+
async createObject(assetPath, options) {
|
|
168
|
+
const { name, folder } = this.parseNewAssetPath(assetPath);
|
|
169
|
+
const parent = options?.parent
|
|
170
|
+
? this.requireAsset(options.parent, 'objects')
|
|
171
|
+
: undefined;
|
|
172
|
+
const sprite = options?.sprite
|
|
173
|
+
? this.requireAsset(options.sprite, 'sprites')
|
|
174
|
+
: undefined;
|
|
175
|
+
return await this.createAsset('objects', name, async (tx, dir) => {
|
|
176
|
+
await tx.writeFile(path.join(dir, 'Create_0.gml'), '');
|
|
177
|
+
return {
|
|
178
|
+
name,
|
|
179
|
+
parent: await tx.ensureFolder(folder),
|
|
180
|
+
eventList: [yyObjectEventSchema.parse({ eventType: 0, eventNum: 0 })],
|
|
181
|
+
parentObjectId: parent ? { ...parent.id } : null,
|
|
182
|
+
spriteId: sprite ? { ...sprite.id } : null,
|
|
183
|
+
};
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Create a sprite from a PNG image, with its origin in the middle.
|
|
188
|
+
* @param assetPath The asset-tree path, ending with the sprite's name.
|
|
189
|
+
*/
|
|
190
|
+
async createSprite(assetPath, fromImage) {
|
|
191
|
+
const { name, folder } = this.parseNewAssetPath(assetPath);
|
|
192
|
+
assert(isValidSpriteName(name, this.config), `Sprite name "${name}" does not match the allowed patterns`);
|
|
193
|
+
const { width, height } = await getPngSize(fromImage);
|
|
194
|
+
return await this.createAsset('sprites', name, async (tx, dir) => {
|
|
195
|
+
const yy = yySpriteSchema.parse({
|
|
196
|
+
name,
|
|
197
|
+
parent: await tx.ensureFolder(folder),
|
|
198
|
+
type: SpriteType.Default,
|
|
199
|
+
origin: SpriteOrigin.MiddleCenter,
|
|
200
|
+
width,
|
|
201
|
+
height,
|
|
202
|
+
sequence: {
|
|
203
|
+
xorigin: Math.floor(width / 2),
|
|
204
|
+
yorigin: Math.floor(height / 2),
|
|
205
|
+
},
|
|
206
|
+
frames: [{}],
|
|
207
|
+
});
|
|
208
|
+
await tx.copyFile(fromImage, path.join(dir, `${yy.frames[0].name}.png`));
|
|
209
|
+
return yy;
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Create a sound from an audio file. Defaults from the Stitch config's
|
|
214
|
+
* `newSoundRules` are applied.
|
|
215
|
+
* @param assetPath The asset-tree path, ending with the sound's name.
|
|
216
|
+
*/
|
|
217
|
+
async createSound(assetPath, fromFile) {
|
|
218
|
+
const { name, folder } = this.parseNewAssetPath(assetPath);
|
|
219
|
+
assert(isValidSoundName(name, this.config), `Sound name "${name}" does not match the allowed patterns`);
|
|
220
|
+
const defaults = getDefaultsForNewSound(name, this.config);
|
|
221
|
+
return await this.createAsset('sounds', name, async (tx, dir) => {
|
|
222
|
+
const soundFile = `${name}${path.extname(fromFile)}`;
|
|
223
|
+
await tx.copyFile(fromFile, path.join(dir, soundFile));
|
|
224
|
+
return {
|
|
225
|
+
name,
|
|
226
|
+
parent: await tx.ensureFolder(folder),
|
|
227
|
+
type: defaults?.mono ? SoundChannel.Mono : SoundChannel.Stereo,
|
|
228
|
+
soundFile,
|
|
229
|
+
// Sound yy files have a different format in older projects
|
|
230
|
+
resourceVersion: '$GMProject' in (await tx.yyp()) ? '2.0' : '1.0',
|
|
231
|
+
};
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Create an empty room, and add it to the end of the room order.
|
|
236
|
+
* @param assetPath The asset-tree path, ending with the room's name.
|
|
237
|
+
*/
|
|
238
|
+
async createRoom(assetPath) {
|
|
239
|
+
const { name, folder } = this.parseNewAssetPath(assetPath);
|
|
240
|
+
return await this.createAsset('rooms', name, async (tx) => {
|
|
241
|
+
const yy = yyRoomSchema.parse({
|
|
242
|
+
name,
|
|
243
|
+
parent: await tx.ensureFolder(folder),
|
|
244
|
+
layers: [{ resourceType: 'GMRBackgroundLayer' }],
|
|
245
|
+
views: Array.from({ length: 8 }, () => ({})),
|
|
246
|
+
});
|
|
247
|
+
yy.views[0].visible = true;
|
|
248
|
+
return yy;
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Create a shader with GameMaker's default pass-through source.
|
|
253
|
+
* @param assetPath The asset-tree path, ending with the shader's name.
|
|
254
|
+
*/
|
|
255
|
+
async createShader(assetPath) {
|
|
256
|
+
const { name, folder } = this.parseNewAssetPath(assetPath);
|
|
257
|
+
return await this.createAsset('shaders', name, async (tx, dir) => {
|
|
258
|
+
await tx.writeFile(path.join(dir, `${name}.fsh`), fshDefault);
|
|
259
|
+
await tx.writeFile(path.join(dir, `${name}.vsh`), vshDefault);
|
|
260
|
+
return { name, parent: await tx.ensureFolder(folder) };
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Add an asset whose files already exist in the project folder (e.g. written
|
|
265
|
+
* by a sprite importer) to the yyp, and put it in a folder. Its yy file must
|
|
266
|
+
* be at the standard location: `<kind>/<name>/<name>.yy`.
|
|
267
|
+
* @param assetPath The asset-tree path, ending with the asset's name.
|
|
268
|
+
*/
|
|
269
|
+
async adoptAsset(kind, assetPath) {
|
|
270
|
+
const { name, folder } = this.parseNewAssetPath(assetPath);
|
|
271
|
+
await this.transaction(async (tx) => {
|
|
272
|
+
const yyp = await tx.yyp();
|
|
273
|
+
assert(!yyp.resources.some((r) => key(r.id.name) === key(name)), `An asset named "${name}" already exists`);
|
|
274
|
+
const id = assetId(kind, name);
|
|
275
|
+
const yyPath = path.join(this.dir, id.path);
|
|
276
|
+
assert(await exists(yyPath), `No yy file found at ${yyPath}`);
|
|
277
|
+
const yy = await tx.yyAt(yyPath, kind);
|
|
278
|
+
yy.parent = await tx.ensureFolder(folder);
|
|
279
|
+
tx.stageYy(yyPath, kind, yy);
|
|
280
|
+
yyp.resources.push({ id });
|
|
281
|
+
if (kind === 'rooms')
|
|
282
|
+
yyp.RoomOrderNodes.push({ roomId: { ...id } });
|
|
283
|
+
});
|
|
284
|
+
return this.requireAsset(name, kind);
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Create a copy of an asset.
|
|
288
|
+
* @param newAssetPath The asset-tree path for the copy, ending with its
|
|
289
|
+
* name. If there's no folder, the copy goes in the source's folder.
|
|
290
|
+
*/
|
|
291
|
+
async duplicateAsset(source, newAssetPath) {
|
|
292
|
+
const original = this.requireAsset(source);
|
|
293
|
+
const parts = splitPath(newAssetPath);
|
|
294
|
+
const { name } = this.parseNewAssetPath(newAssetPath);
|
|
295
|
+
const folder = parts.length > 1 ? parts.slice(0, -1) : splitPath(original.folder);
|
|
296
|
+
const newId = assetId(original.kind, name);
|
|
297
|
+
return (await this.createAsset(original.kind, name, async (tx, dir) => {
|
|
298
|
+
const renamer = fileRenamer(original.name, name);
|
|
299
|
+
await tx.copyDir(original.dir, dir, renamer);
|
|
300
|
+
const yy = structuredClone(await tx.yy(original));
|
|
301
|
+
renameInOwnYy(yy, original.kind, original.id, newId, renamer);
|
|
302
|
+
yy.parent = await tx.ensureFolder(folder);
|
|
303
|
+
if (original.isKind('rooms')) {
|
|
304
|
+
await this.renewRoomInstanceIds(tx, yy, dir);
|
|
305
|
+
}
|
|
306
|
+
return yy;
|
|
307
|
+
}));
|
|
308
|
+
}
|
|
309
|
+
// --- Renaming & deleting assets ---
|
|
310
|
+
/**
|
|
311
|
+
* Rename an asset, updating every reference to it in the project's yy
|
|
312
|
+
* files (parent objects, object sprites, room instances, collision events,
|
|
313
|
+
* the room order, ...).
|
|
314
|
+
*
|
|
315
|
+
* GML code is not updated: that's the job of a code-aware layer.
|
|
316
|
+
*
|
|
317
|
+
* The asset is replaced by a new {@link Asset}, which is returned.
|
|
318
|
+
*/
|
|
319
|
+
async renameAsset(from, to) {
|
|
320
|
+
const asset = this.requireAsset(from);
|
|
321
|
+
assertIsValidIdentifier(to);
|
|
322
|
+
if (to === asset.name)
|
|
323
|
+
return asset;
|
|
324
|
+
const caseOnly = key(to) === key(asset.name);
|
|
325
|
+
const oldId = { ...asset.id };
|
|
326
|
+
const newId = assetId(asset.kind, to);
|
|
327
|
+
await this.transaction(async (tx) => {
|
|
328
|
+
// (Inside the transaction, so that pending external changes are included)
|
|
329
|
+
const referrers = this.listAssets().filter((a) => a !== asset && referencesPath(a.yy, oldId.path));
|
|
330
|
+
if (!caseOnly)
|
|
331
|
+
await this.assertNameAvailable(tx, to, asset.kind);
|
|
332
|
+
const yyp = await tx.yyp();
|
|
333
|
+
assert(yyp.resources.some((r) => key(r.id.name) === key(asset.name)), `Asset "${asset.name}" is not listed in the yyp`);
|
|
334
|
+
const renamer = fileRenamer(asset.name, to);
|
|
335
|
+
const yy = structuredClone(await tx.yy(asset));
|
|
336
|
+
const newDir = path.join(path.dirname(asset.dir), to);
|
|
337
|
+
if (caseOnly) {
|
|
338
|
+
// Move in place, via a temporary name (needed on case-insensitive
|
|
339
|
+
// file systems)
|
|
340
|
+
const temp = path.join(path.dirname(asset.dir), `${to}.stitch-${randomBytes(4).toString('hex')}`);
|
|
341
|
+
await tx.rename(asset.dir, temp);
|
|
342
|
+
for (const file of await listFilesRecursively(temp)) {
|
|
343
|
+
const relative = toPosix(path.relative(temp, file));
|
|
344
|
+
const renamed = renamer(relative);
|
|
345
|
+
if (renamed !== relative) {
|
|
346
|
+
await tx.rename(file, path.join(temp, renamed));
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
await tx.rename(temp, newDir);
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
await tx.copyDir(asset.dir, newDir, renamer);
|
|
353
|
+
tx.deleteOnCommit(asset.dir);
|
|
354
|
+
}
|
|
355
|
+
renameInOwnYy(yy, asset.kind, oldId, newId, renamer);
|
|
356
|
+
tx.stageYy(path.join(newDir, `${to}.yy`), asset.kind, yy);
|
|
357
|
+
// Update references elsewhere
|
|
358
|
+
replaceRefs(yyp, oldId, newId);
|
|
359
|
+
for (const other of referrers) {
|
|
360
|
+
const data = await tx.yy(other);
|
|
361
|
+
replaceRefs(data, oldId, newId);
|
|
362
|
+
tx.stageYy(other.yyPath, other.kind, data);
|
|
363
|
+
if (other.isKind('objects') && asset.isKind('objects')) {
|
|
364
|
+
// Collision events are named after the other object
|
|
365
|
+
const file = await findCaseInsensitive(other.dir, `Collision_${oldId.name}.gml`);
|
|
366
|
+
if (file) {
|
|
367
|
+
await tx.rename(file, path.join(other.dir, `Collision_${to}.gml`));
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
return this.requireAsset(to);
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Delete an asset and its files.
|
|
376
|
+
*
|
|
377
|
+
* References to it are cleaned up where that's safe: object parents and
|
|
378
|
+
* sprites are unset, collision events with it are removed, its instances
|
|
379
|
+
* are removed from rooms, and it is removed from the room order. Any
|
|
380
|
+
* remaining references are reported.
|
|
381
|
+
*/
|
|
382
|
+
async deleteAsset(ref) {
|
|
383
|
+
const asset = this.requireAsset(ref);
|
|
384
|
+
const id = { ...asset.id };
|
|
385
|
+
return await this.transaction(async (tx) => {
|
|
386
|
+
const referrers = this.listAssets().filter((a) => a !== asset && referencesPath(a.yy, id.path));
|
|
387
|
+
const yyp = await tx.yyp();
|
|
388
|
+
yyp.resources = yyp.resources.filter((r) => key(r.id.name) !== key(asset.name));
|
|
389
|
+
yyp.RoomOrderNodes = yyp.RoomOrderNodes.filter((n) => key(n.roomId.path) !== key(id.path));
|
|
390
|
+
const danglingReferences = [];
|
|
391
|
+
for (const other of referrers) {
|
|
392
|
+
const yy = (await tx.yy(other));
|
|
393
|
+
// Unset top-level references (parentObjectId, spriteId, parentRoom, ...)
|
|
394
|
+
for (const [field, value] of Object.entries(yy)) {
|
|
395
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
396
|
+
if (key(value.path ?? '') === key(id.path))
|
|
397
|
+
yy[field] = null;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
if (other.isKind('objects')) {
|
|
401
|
+
const object = yy;
|
|
402
|
+
const before = object.eventList.length;
|
|
403
|
+
object.eventList = object.eventList.filter((e) => key(e.collisionObjectId?.path ?? '') !== key(id.path));
|
|
404
|
+
const file = object.eventList.length < before &&
|
|
405
|
+
(await findCaseInsensitive(other.dir, `Collision_${id.name}.gml`));
|
|
406
|
+
if (file)
|
|
407
|
+
tx.deleteOnCommit(file);
|
|
408
|
+
}
|
|
409
|
+
else if (other.isKind('rooms')) {
|
|
410
|
+
const room = yy;
|
|
411
|
+
const removed = new Set();
|
|
412
|
+
for (const layer of instanceLayers(room)) {
|
|
413
|
+
layer.instances = layer.instances.filter((i) => {
|
|
414
|
+
if (key(i.objectId.path) !== key(id.path))
|
|
415
|
+
return true;
|
|
416
|
+
removed.add(i.name);
|
|
417
|
+
return false;
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
room.instanceCreationOrder = room.instanceCreationOrder.filter((i) => !removed.has(i.name));
|
|
421
|
+
for (const name of removed) {
|
|
422
|
+
await this.deleteInstanceCreationCode(tx, other, name);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
tx.stageYy(other.yyPath, other.kind, yy);
|
|
426
|
+
if (referencesPath(yy, id.path))
|
|
427
|
+
danglingReferences.push(other.name);
|
|
428
|
+
}
|
|
429
|
+
tx.deleteOnCommit(asset.dir);
|
|
430
|
+
return { danglingReferences };
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
// --- Objects ---
|
|
434
|
+
/** Set (or unset) an object's parent */
|
|
435
|
+
async setObjectParent(object, parent) {
|
|
436
|
+
const obj = this.requireAsset(object, 'objects');
|
|
437
|
+
const newParent = parent ? this.requireAsset(parent, 'objects') : undefined;
|
|
438
|
+
if (newParent) {
|
|
439
|
+
assert(newParent !== obj && !this.getAncestors(newParent).includes(obj), `Cannot make "${newParent.name}" the parent of "${obj.name}": that would create a cycle`);
|
|
440
|
+
}
|
|
441
|
+
await this.updateYy(obj, (yy) => {
|
|
442
|
+
yy.parentObjectId = newParent ? { ...newParent.id } : null;
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
/** Set (or unset) an object's sprite */
|
|
446
|
+
async setObjectSprite(object, sprite) {
|
|
447
|
+
const obj = this.requireAsset(object, 'objects');
|
|
448
|
+
const newSprite = sprite ? this.requireAsset(sprite, 'sprites') : undefined;
|
|
449
|
+
await this.updateYy(obj, (yy) => {
|
|
450
|
+
yy.spriteId = newSprite ? { ...newSprite.id } : null;
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Add an event to an object, creating its GML file if needed. Returns the
|
|
455
|
+
* path to the event's GML file.
|
|
456
|
+
* @param event The event, or its file name (`Step_0`) or label (`Step`).
|
|
457
|
+
*/
|
|
458
|
+
async createObjectEvent(object, event) {
|
|
459
|
+
const obj = this.requireAsset(object, 'objects');
|
|
460
|
+
const info = resolveEvent(event);
|
|
461
|
+
const collisionWith = info.collisionObject
|
|
462
|
+
? this.requireAsset(info.collisionObject, 'objects')
|
|
463
|
+
: undefined;
|
|
464
|
+
return await this.transaction(async (tx) => {
|
|
465
|
+
const yy = await tx.yy(obj);
|
|
466
|
+
const file = (await findCaseInsensitive(obj.dir, `${info.name}.gml`)) ||
|
|
467
|
+
path.join(obj.dir, `${info.name}.gml`);
|
|
468
|
+
if (!yy.eventList.some((e) => isSameEvent(e, info))) {
|
|
469
|
+
yy.eventList.push(yyObjectEventSchema.parse({
|
|
470
|
+
eventType: info.eventType,
|
|
471
|
+
eventNum: info.eventNum,
|
|
472
|
+
collisionObjectId: collisionWith ? { ...collisionWith.id } : null,
|
|
473
|
+
}));
|
|
474
|
+
tx.stageYy(obj.yyPath, obj.kind, yy);
|
|
475
|
+
}
|
|
476
|
+
if (!(await exists(file)))
|
|
477
|
+
await tx.writeFile(file, '');
|
|
478
|
+
return file;
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
/** Remove an event from an object, deleting its GML file */
|
|
482
|
+
async deleteObjectEvent(object, event) {
|
|
483
|
+
const obj = this.requireAsset(object, 'objects');
|
|
484
|
+
const info = resolveEvent(event);
|
|
485
|
+
await this.transaction(async (tx) => {
|
|
486
|
+
const yy = await tx.yy(obj);
|
|
487
|
+
yy.eventList = yy.eventList.filter((e) => !isSameEvent(e, info));
|
|
488
|
+
tx.stageYy(obj.yyPath, obj.kind, yy);
|
|
489
|
+
const file = await findCaseInsensitive(obj.dir, `${info.name}.gml`);
|
|
490
|
+
if (file)
|
|
491
|
+
tx.deleteOnCommit(file);
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
// --- Rooms ---
|
|
495
|
+
/**
|
|
496
|
+
* Add an instance of an object to a room, at the end of the creation
|
|
497
|
+
* order. Returns the new instance's ID.
|
|
498
|
+
* @param options.layer The instance layer to add it to (default: the first one, created if needed)
|
|
499
|
+
*/
|
|
500
|
+
async addRoomInstance(room, object, options) {
|
|
501
|
+
const target = this.requireAsset(room, 'rooms');
|
|
502
|
+
const obj = this.requireAsset(object, 'objects');
|
|
503
|
+
return await this.transaction(async (tx) => {
|
|
504
|
+
const yy = await tx.yy(target);
|
|
505
|
+
const layers = instanceLayers(yy);
|
|
506
|
+
let layer = options?.layer
|
|
507
|
+
? layers.find((l) => key(l.name) === key(options.layer))
|
|
508
|
+
: layers[0];
|
|
509
|
+
if (!layer) {
|
|
510
|
+
layer = yyRoomInstanceLayerSchema.parse(options?.layer ? { name: options.layer } : {});
|
|
511
|
+
yy.layers.unshift(layer);
|
|
512
|
+
}
|
|
513
|
+
const instance = yyRoomInstanceSchema.parse({
|
|
514
|
+
objectId: { ...obj.id },
|
|
515
|
+
x: options?.x ?? 0,
|
|
516
|
+
y: options?.y ?? 0,
|
|
517
|
+
});
|
|
518
|
+
layer.instances.push(instance);
|
|
519
|
+
yy.instanceCreationOrder.push({
|
|
520
|
+
name: instance.name,
|
|
521
|
+
path: target.id.path,
|
|
522
|
+
});
|
|
523
|
+
tx.stageYy(target.yyPath, target.kind, yy);
|
|
524
|
+
return instance.name;
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
/** Remove an instance from a room (and its creation code, if any) */
|
|
528
|
+
async removeRoomInstance(room, instanceId) {
|
|
529
|
+
const target = this.requireAsset(room, 'rooms');
|
|
530
|
+
await this.transaction(async (tx) => {
|
|
531
|
+
const yy = await tx.yy(target);
|
|
532
|
+
for (const layer of instanceLayers(yy)) {
|
|
533
|
+
layer.instances = layer.instances.filter((i) => i.name !== instanceId);
|
|
534
|
+
}
|
|
535
|
+
yy.instanceCreationOrder = yy.instanceCreationOrder.filter((i) => i.name !== instanceId);
|
|
536
|
+
tx.stageYy(target.yyPath, target.kind, yy);
|
|
537
|
+
await this.deleteInstanceCreationCode(tx, target, instanceId);
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
/** Set a room's instance creation order. Must list every instance exactly once. */
|
|
541
|
+
async reorderRoomInstances(room, instanceIds) {
|
|
542
|
+
const target = this.requireAsset(room, 'rooms');
|
|
543
|
+
await this.transaction(async (tx) => {
|
|
544
|
+
const yy = await tx.yy(target);
|
|
545
|
+
const current = new Set(yy.instanceCreationOrder.map((i) => i.name));
|
|
546
|
+
const requested = new Set(instanceIds);
|
|
547
|
+
assert(requested.size === instanceIds.length, 'Instance IDs must be unique');
|
|
548
|
+
assert(requested.size === current.size &&
|
|
549
|
+
instanceIds.every((i) => current.has(i)), 'The new order must list every instance in the room');
|
|
550
|
+
yy.instanceCreationOrder = instanceIds.map((name) => ({
|
|
551
|
+
name,
|
|
552
|
+
path: target.id.path,
|
|
553
|
+
}));
|
|
554
|
+
tx.stageYy(target.yyPath, target.kind, yy);
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
// --- Sprites ---
|
|
558
|
+
/** Add PNG images as new frames at the end of a (non-Spine) sprite */
|
|
559
|
+
async addSpriteFrames(sprite, images) {
|
|
560
|
+
const target = this.requireSpriteForFrames(sprite);
|
|
561
|
+
if (!images.length)
|
|
562
|
+
return;
|
|
563
|
+
const sizes = await Promise.all(images.map((i) => getPngSize(i)));
|
|
564
|
+
await this.transaction(async (tx) => {
|
|
565
|
+
let yy = await tx.yy(target);
|
|
566
|
+
const expected = yy.frames.length
|
|
567
|
+
? { width: yy.width, height: yy.height }
|
|
568
|
+
: sizes[0];
|
|
569
|
+
assert(sizes.every((s) => s.width === expected.width && s.height === expected.height), `All frames must be ${expected.width}x${expected.height}`);
|
|
570
|
+
const start = yy.frames.length;
|
|
571
|
+
yy.frames.push(...images.map(() => ({})));
|
|
572
|
+
yy = yySpriteSchema.parse({ ...yy, ...expected });
|
|
573
|
+
for (const [i, image] of images.entries()) {
|
|
574
|
+
const frameId = yy.frames[start + i].name;
|
|
575
|
+
await tx.copyFile(image, path.join(target.dir, `${frameId}.png`));
|
|
576
|
+
}
|
|
577
|
+
tx.stageYy(target.yyPath, target.kind, yy);
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
/** Delete frames from a (non-Spine) sprite, along with their images */
|
|
581
|
+
async deleteSpriteFrames(sprite, frameIds) {
|
|
582
|
+
const target = this.requireSpriteForFrames(sprite);
|
|
583
|
+
const remaining = target.frameIds.filter((id) => !frameIds.includes(id));
|
|
584
|
+
await this.reorderSpriteFrames(target, remaining);
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Set the order of a (non-Spine) sprite's frames. Frames not listed are
|
|
588
|
+
* deleted, along with their images.
|
|
589
|
+
*/
|
|
590
|
+
async reorderSpriteFrames(sprite, frameIds) {
|
|
591
|
+
const target = this.requireSpriteForFrames(sprite);
|
|
592
|
+
await this.transaction(async (tx) => {
|
|
593
|
+
const yy = await tx.yy(target);
|
|
594
|
+
const byId = new Map(yy.frames.map((f) => [f.name, f]));
|
|
595
|
+
assert(frameIds.every((id) => byId.has(id)), "Can't reorder frames that don't exist");
|
|
596
|
+
assert(new Set(frameIds).size === frameIds.length, 'Frame IDs must be unique');
|
|
597
|
+
const removed = yy.frames.filter((f) => !frameIds.includes(f.name));
|
|
598
|
+
yy.frames = frameIds.map((id) => byId.get(id));
|
|
599
|
+
tx.stageYy(target.yyPath, target.kind, yySpriteSchema.parse(yy));
|
|
600
|
+
for (const frame of removed) {
|
|
601
|
+
tx.deleteOnCommit(path.join(target.dir, `${frame.name}.png`));
|
|
602
|
+
tx.deleteOnCommit(path.join(target.dir, 'layers', frame.name));
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
// --- Helpers ---
|
|
607
|
+
/**
|
|
608
|
+
* Change an asset's yy data, in a transaction. `change` gets the data as
|
|
609
|
+
* read fresh from disk, and mutates it.
|
|
610
|
+
*/
|
|
611
|
+
async updateYy(asset, change) {
|
|
612
|
+
await this.transaction(async (tx) => {
|
|
613
|
+
const yy = await tx.yy(asset);
|
|
614
|
+
change(yy);
|
|
615
|
+
tx.stageYy(asset.yyPath, asset.kind, yy);
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
requireSpriteForFrames(sprite) {
|
|
619
|
+
const target = this.requireAsset(sprite, 'sprites');
|
|
620
|
+
assert(!target.isSpineSprite, `"${target.name}" is a Spine sprite, so its frames can't be edited`);
|
|
621
|
+
return target;
|
|
622
|
+
}
|
|
623
|
+
/** Split an asset-tree path into the asset name and its folder */
|
|
624
|
+
parseNewAssetPath(assetPath) {
|
|
625
|
+
const parts = splitPath(assetPath);
|
|
626
|
+
const name = parts.pop();
|
|
627
|
+
assert(name, `No asset name in path "${assetPath}"`);
|
|
628
|
+
assertIsValidIdentifier(name);
|
|
629
|
+
assert(!this.getAsset(name), `An asset named "${name}" already exists`);
|
|
630
|
+
return { name, folder: parts };
|
|
631
|
+
}
|
|
632
|
+
/** Throw if the name is taken (checking disk, not just memory) */
|
|
633
|
+
async assertNameAvailable(tx, name, kind) {
|
|
634
|
+
assert(!this.getAsset(name), `An asset named "${name}" already exists`);
|
|
635
|
+
const yyp = await tx.yyp();
|
|
636
|
+
assert(!yyp.resources.some((r) => key(r.id.name) === key(name)), `An asset named "${name}" already exists`);
|
|
637
|
+
const dir = await findCaseInsensitive(path.join(this.dir, kind), name);
|
|
638
|
+
assert(!dir || !(await listDir(dir)).length, `The folder for "${name}" already exists and isn't empty: ${dir}`);
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Create a new asset: `build` writes any extra files and returns the yy
|
|
642
|
+
* data, which is then written and added to the yyp.
|
|
643
|
+
*/
|
|
644
|
+
async createAsset(kind, name, build) {
|
|
645
|
+
await this.transaction(async (tx) => {
|
|
646
|
+
await this.assertNameAvailable(tx, name, kind);
|
|
647
|
+
const id = assetId(kind, name);
|
|
648
|
+
const dir = path.join(this.dir, kind, name);
|
|
649
|
+
tx.onRollback(() => fs.rm(dir, { recursive: true, force: true }));
|
|
650
|
+
const yy = (await build(tx, dir));
|
|
651
|
+
tx.stageYy(path.join(this.dir, id.path), kind, yy);
|
|
652
|
+
const yyp = await tx.yyp();
|
|
653
|
+
yyp.resources.push({ id });
|
|
654
|
+
if (kind === 'rooms')
|
|
655
|
+
yyp.RoomOrderNodes.push({ roomId: { ...id } });
|
|
656
|
+
});
|
|
657
|
+
return this.requireAsset(name, kind);
|
|
658
|
+
}
|
|
659
|
+
async deleteInstanceCreationCode(tx, room, instanceId) {
|
|
660
|
+
const file = await findCaseInsensitive(room.dir, `InstanceCreationCode_${instanceId}.gml`);
|
|
661
|
+
if (file)
|
|
662
|
+
tx.deleteOnCommit(file);
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Give a room's instances new IDs (instance IDs must be unique across the
|
|
666
|
+
* project), renaming their creation-code files to match.
|
|
667
|
+
*/
|
|
668
|
+
async renewRoomInstanceIds(tx, room, dir) {
|
|
669
|
+
const renamed = new Map();
|
|
670
|
+
for (const layer of instanceLayers(room)) {
|
|
671
|
+
for (const instance of layer.instances) {
|
|
672
|
+
const newId = `inst_${randomBytes(4).toString('hex').toUpperCase()}`;
|
|
673
|
+
renamed.set(instance.name, newId);
|
|
674
|
+
instance.name = newId;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
for (const entry of room.instanceCreationOrder) {
|
|
678
|
+
entry.name = renamed.get(entry.name) ?? entry.name;
|
|
679
|
+
}
|
|
680
|
+
for (const [oldId, newId] of renamed) {
|
|
681
|
+
const file = await findCaseInsensitive(dir, `InstanceCreationCode_${oldId}.gml`);
|
|
682
|
+
if (file) {
|
|
683
|
+
await tx.rename(file, path.join(dir, `InstanceCreationCode_${newId}.gml`));
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
function assetId(kind, name) {
|
|
689
|
+
return { name, path: `${kind}/${name}/${name}.yy` };
|
|
690
|
+
}
|
|
691
|
+
function isSameOrSubfolder(folderPathYy, posix) {
|
|
692
|
+
const k = key(folderPathYy);
|
|
693
|
+
return (k === key(folderPathToYy(posix)) || k.startsWith(key(`folders/${posix}/`)));
|
|
694
|
+
}
|
|
695
|
+
function escapeRegex(text) {
|
|
696
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* A function that renames the parts of a relative path that are named after
|
|
700
|
+
* an asset (e.g. `obj_old.yy`, `snd_old.wav`, `Collision_obj_old.gml`).
|
|
701
|
+
*/
|
|
702
|
+
function fileRenamer(from, to) {
|
|
703
|
+
const pattern = new RegExp(`(?<![a-zA-Z0-9])${escapeRegex(from)}(?![a-zA-Z0-9_])`, 'gi');
|
|
704
|
+
return (relative) => relative.replace(pattern, to);
|
|
705
|
+
}
|
|
706
|
+
/** Update an asset's own yy data for a new name */
|
|
707
|
+
function renameInOwnYy(yy, kind, from, to, renamer) {
|
|
708
|
+
replaceRefs(yy, from, to);
|
|
709
|
+
yy.name = to.name;
|
|
710
|
+
if ('%Name' in yy)
|
|
711
|
+
yy['%Name'] = to.name;
|
|
712
|
+
if (kind === 'sounds' && typeof yy.soundFile === 'string') {
|
|
713
|
+
yy.soundFile = renamer(yy.soundFile);
|
|
714
|
+
}
|
|
715
|
+
if (kind === 'sprites' && yy.sequence && typeof yy.sequence === 'object') {
|
|
716
|
+
yy.sequence.name = to.name;
|
|
717
|
+
if ('%Name' in yy.sequence)
|
|
718
|
+
yy.sequence['%Name'] = to.name;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
function resolveEvent(event) {
|
|
722
|
+
if (typeof event !== 'string')
|
|
723
|
+
return event;
|
|
724
|
+
const info = getEventFromFilename(event) || getEventFromLabel(event);
|
|
725
|
+
assert(info, `Unknown object event: ${event}`);
|
|
726
|
+
return info;
|
|
727
|
+
}
|
|
728
|
+
function isSameEvent(entry, event) {
|
|
729
|
+
const info = getEvent(entry.eventType, entry.eventNum, entry.collisionObjectId?.name);
|
|
730
|
+
return !!info && key(info.name) === key(event.name);
|
|
731
|
+
}
|
|
732
|
+
//# sourceMappingURL=project.js.map
|