@threenative/runtime-native 0.3.1 → 0.3.3
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/README.md +236 -1
- package/android/app/build.gradle.kts +37 -2
- package/android/app/src/main/java/com/mystral/engine/MystralActivity.java +62 -0
- package/android/build.gradle.kts +1 -1
- package/android/gradle/wrapper/gradle-wrapper.properties +2 -2
- package/package.json +15 -4
- package/scripts/asset-manifest.mjs +190 -0
- package/scripts/asset-preflight.mjs +137 -6
- package/scripts/bundle.mjs +25 -5
- package/scripts/check-android-16kb-alignment.mjs +413 -0
- package/scripts/desktop-distribution.mjs +1084 -0
- package/scripts/inspect-container-brand.mjs +748 -0
- package/scripts/install-prebuilt.mjs +56 -10
- package/scripts/package-android.mjs +478 -32
- package/scripts/package-desktop.mjs +191 -15
- package/scripts/package-ios.mjs +53 -10
- package/scripts/production-evidence.mjs +7 -3
- package/scripts/profile-production.mjs +109 -28
- package/scripts/verify-starter-consumer.mjs +732 -0
- package/scripts/verify-starter-desktop-base.mjs +448 -0
- package/scripts/verify-starter-desktop.mjs +27 -220
- package/scripts/verify-windows-installer.mjs +63 -0
- package/scripts/windows-installer.mjs +181 -0
- package/scripts/windows-installer.nsi +114 -0
- package/scripts/xvfb.sh +48 -1
- package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
|
@@ -0,0 +1,1084 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OS container and resource operations for desktop release artifacts.
|
|
3
|
+
*
|
|
4
|
+
* `packageDesktop` delegates here for `--mode release`; the raw-binary debug path stays exactly
|
|
5
|
+
* as it was. Nothing in this file decides how a game looks — it wraps the produced executable,
|
|
6
|
+
* its UI bundle and the runtime dependencies discovered from that executable into one relocatable
|
|
7
|
+
* container per native host, and writes the OS metadata that names the game.
|
|
8
|
+
*
|
|
9
|
+
* Signing and notarization are optional OS-tool operations. A release archive replaces the
|
|
10
|
+
* previous output only after every requested packaging/signing/notarization stage succeeds.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { spawnSync } from 'node:child_process';
|
|
14
|
+
import { createHash } from 'node:crypto';
|
|
15
|
+
import {
|
|
16
|
+
chmodSync,
|
|
17
|
+
closeSync,
|
|
18
|
+
copyFileSync,
|
|
19
|
+
existsSync,
|
|
20
|
+
mkdirSync,
|
|
21
|
+
mkdtempSync,
|
|
22
|
+
openSync,
|
|
23
|
+
readFileSync,
|
|
24
|
+
readSync,
|
|
25
|
+
readdirSync,
|
|
26
|
+
realpathSync,
|
|
27
|
+
renameSync,
|
|
28
|
+
rmSync,
|
|
29
|
+
statSync,
|
|
30
|
+
writeFileSync,
|
|
31
|
+
} from 'node:fs';
|
|
32
|
+
import { tmpdir } from 'node:os';
|
|
33
|
+
import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
34
|
+
import { buildWindowsInstaller } from './windows-installer.mjs';
|
|
35
|
+
|
|
36
|
+
export const CONTAINER_MANIFEST = 'threenative-container.json';
|
|
37
|
+
export const CONTAINER_SCHEMA_VERSION = 1;
|
|
38
|
+
export const DESKTOP_TARGETS = ['linux-x64', 'linux-arm64', 'darwin-x64', 'darwin-arm64', 'win32-x64'];
|
|
39
|
+
|
|
40
|
+
// The loader's own directories. A library resolved from one of these is the player's machine to
|
|
41
|
+
// provide; everything else the runtime actually needs travels inside the container, or the
|
|
42
|
+
// artifact is not relocatable. Matched against the resolved path, because a bundled library can
|
|
43
|
+
// carry any soname.
|
|
44
|
+
const SYSTEM_LIBRARY_DIRECTORIES = [
|
|
45
|
+
'/lib/',
|
|
46
|
+
'/lib64/',
|
|
47
|
+
'/usr/lib/',
|
|
48
|
+
'/usr/lib64/',
|
|
49
|
+
'/usr/libexec/',
|
|
50
|
+
'/usr/lib/system/',
|
|
51
|
+
'/System/Library/',
|
|
52
|
+
'/usr/local/lib/system/',
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
// Libraries the platform itself owns, by name, whether or not the tool resolved a path. The GTK,
|
|
56
|
+
// WebKit, X11 and Wayland stacks are the documented Linux player prerequisite (PRD-365 phase 2),
|
|
57
|
+
// not something an unsigned tarball re-ships.
|
|
58
|
+
const SYSTEM_LIBRARY_NAMES = new Set([
|
|
59
|
+
'libc.so.6', 'libm.so.6', 'libdl.so.2', 'libpthread.so.0', 'librt.so.1', 'libgcc_s.so.1',
|
|
60
|
+
'libstdc++.so.6', 'libatomic.so.1', 'libnsl.so.1', 'libutil.so.1', 'libresolv.so.2',
|
|
61
|
+
'libX11.so.6', 'libXext.so.6', 'libXrandr.so.2', 'libXi.so.6', 'libXcursor.so.1',
|
|
62
|
+
'libXinerama.so.1', 'libxkbcommon.so.0', 'libwayland-client.so.0', 'libwayland-server.so.0',
|
|
63
|
+
'libwayland-cursor.so.0', 'libwayland-egl.so.1', 'libepoxy.so.0', 'libgbm.so.1', 'libdrm.so.2',
|
|
64
|
+
'libGL.so.1', 'libEGL.so.1', 'libGLESv2.so.2', 'libvulkan.so.1', 'libasound.so.2',
|
|
65
|
+
'libpulse.so.0', 'libdbus-1.so.3', 'libudev.so.1', 'libsystemd.so.0',
|
|
66
|
+
'libgobject-2.0.so.0', 'libglib-2.0.so.0', 'libgio-2.0.so.0', 'libgtk-3.so.0',
|
|
67
|
+
'libgdk-3.so.0', 'libwebkit2gtk-4.1.so.0', 'libjavascriptcoregtk-4.1.so.0', 'libsoup-3.0.so.0',
|
|
68
|
+
'libz.so.1', 'libexpat.so.1', 'libfontconfig.so.1', 'libfreetype.so.6', 'libpng16.so.16',
|
|
69
|
+
'libharfbuzz.so.0', 'libpango-1.0.so.0', 'libcairo.so.2', 'libcairo-gobject.so.2',
|
|
70
|
+
'libgdk_pixbuf-2.0.so.0', 'libatk-1.0.so.0', 'libatk-bridge-2.0.so.0', 'libcurl.so.4',
|
|
71
|
+
]);
|
|
72
|
+
|
|
73
|
+
const SYSTEM_WINDOWS_DLLS = new Set([
|
|
74
|
+
'kernel32.dll', 'user32.dll', 'gdi32.dll', 'advapi32.dll', 'shell32.dll', 'ole32.dll',
|
|
75
|
+
'oleaut32.dll', 'ws2_32.dll', 'opengl32.dll', 'winmm.dll', 'userenv.dll', 'version.dll',
|
|
76
|
+
'setupapi.dll', 'cfgmgr32.dll', 'bcrypt.dll', 'crypt32.dll', 'dwmapi.dll', 'shlwapi.dll',
|
|
77
|
+
'comdlg32.dll', 'imm32.dll', 'uxtheme.dll', 'd3d11.dll', 'dxgi.dll', 'd3dcompiler_47.dll',
|
|
78
|
+
]);
|
|
79
|
+
|
|
80
|
+
function exec(command, args, options = {}) {
|
|
81
|
+
const result = spawnSync(command, args, { encoding: 'utf8', ...options });
|
|
82
|
+
return {
|
|
83
|
+
error: result.error,
|
|
84
|
+
signal: result.signal,
|
|
85
|
+
status: result.status,
|
|
86
|
+
stderr: result.stderr ?? '',
|
|
87
|
+
stdout: result.stdout ?? '',
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function sha256(data) {
|
|
92
|
+
return createHash('sha256').update(data).digest('hex');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function sha256File(path) {
|
|
96
|
+
return sha256(readFileSync(path));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function missing(path, label) {
|
|
100
|
+
return `TN_DESKTOP_INPUT_MISSING: ${label} does not exist: ${path}`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function assertDirectory(path, label) {
|
|
104
|
+
if (!path || !existsSync(path) || !statSync(path).isDirectory()) {
|
|
105
|
+
throw new Error(missing(path ?? '(not provided)', label));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function assertFile(path, label) {
|
|
110
|
+
if (!path || !existsSync(path) || !statSync(path).isFile()) {
|
|
111
|
+
throw new Error(missing(path ?? '(not provided)', label));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function desktopTargetKey(platform = process.platform, arch = process.arch) {
|
|
116
|
+
const key = `${platform}-${arch}`;
|
|
117
|
+
if (!DESKTOP_TARGETS.includes(key)) {
|
|
118
|
+
throw new Error(`Unsupported desktop platform '${key}'.`);
|
|
119
|
+
}
|
|
120
|
+
return key;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function desktopContainerFormat(platform = process.platform) {
|
|
124
|
+
if (platform === 'linux') return 'tar.gz';
|
|
125
|
+
if (platform === 'darwin' || platform === 'win32') return 'zip';
|
|
126
|
+
throw new Error(`TN_DESKTOP_CONTAINER_UNSUPPORTED: no container format for '${platform}'.`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function containerArchivePath(output, platform = process.platform) {
|
|
130
|
+
const extension = desktopContainerFormat(platform) === 'tar.gz' ? '.tar.gz' : '.zip';
|
|
131
|
+
return output.endsWith(extension) ? output : `${output}${extension}`;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** A filesystem-safe project name; the game's display name stays in the OS metadata. */
|
|
135
|
+
export function containerSlug(appName) {
|
|
136
|
+
return String(appName).trim().replace(/[^A-Za-z0-9._-]+/gu, '-').replace(/^-+|-+$/gu, '') || 'game';
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The single top-level directory an archive extracts to, so a container is one relocatable unit. */
|
|
140
|
+
export function containerRootFolder(platform, appName) {
|
|
141
|
+
return platform === 'darwin' ? `${appName}.app` : containerSlug(appName);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function listFiles(root) {
|
|
145
|
+
const files = [];
|
|
146
|
+
const walk = (directory, prefix) => {
|
|
147
|
+
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
|
148
|
+
const absolute = join(directory, entry.name);
|
|
149
|
+
const relativePath = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
150
|
+
if (entry.isDirectory()) walk(absolute, relativePath);
|
|
151
|
+
else if (entry.isFile()) files.push({ absolute, relative: relativePath });
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
walk(root, '');
|
|
155
|
+
return files;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Parse the dependency tool every native host already ships. Kept pure so the contract can be
|
|
160
|
+
* pinned without a binary that links anything.
|
|
161
|
+
*/
|
|
162
|
+
export function parseLinkedLibraries(output, platform) {
|
|
163
|
+
const libraries = [];
|
|
164
|
+
if (platform === 'linux') {
|
|
165
|
+
for (const line of output.split('\n')) {
|
|
166
|
+
const resolved = /^\s*(\S+)\s+=>\s+(.+?)\s+\(0x[0-9a-f]+\)\s*$/u.exec(line);
|
|
167
|
+
if (resolved) {
|
|
168
|
+
libraries.push({ name: resolved[1], path: resolved[2] });
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
// `linux-vdso.so.1` and `/lib64/ld-linux-x86-64.so.2` have no `=>`; they are the kernel and
|
|
172
|
+
// loader and are never bundled. A bare `=> not found` has no path and fails closed later.
|
|
173
|
+
const unresolved = /^\s*(\S+)\s+=>\s+not found\s*$/u.exec(line);
|
|
174
|
+
if (unresolved) libraries.push({ missing: true, name: unresolved[1] });
|
|
175
|
+
}
|
|
176
|
+
return libraries;
|
|
177
|
+
}
|
|
178
|
+
if (platform === 'darwin') {
|
|
179
|
+
for (const line of output.split('\n').slice(1)) {
|
|
180
|
+
const resolved = /^\s*(.+?)\s+\(compatibility version/u.exec(line);
|
|
181
|
+
if (resolved) libraries.push({ name: basename(resolved[1]), path: resolved[1] });
|
|
182
|
+
}
|
|
183
|
+
return libraries;
|
|
184
|
+
}
|
|
185
|
+
if (platform === 'win32') {
|
|
186
|
+
for (const line of output.split('\n')) {
|
|
187
|
+
const resolved = /^\s*(\S+\.dll)\s*$/iu.exec(line);
|
|
188
|
+
if (resolved) libraries.push({ name: resolved[1], path: undefined });
|
|
189
|
+
}
|
|
190
|
+
return libraries;
|
|
191
|
+
}
|
|
192
|
+
throw new Error(`TN_DESKTOP_CONTAINER_UNSUPPORTED: no dependency tool for '${platform}'.`);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Inspect the produced executable and return every shared library it loads. */
|
|
196
|
+
export function discoverRuntimeDependencies(binary, { platform = process.platform, run = exec } = {}) {
|
|
197
|
+
assertFile(binary, 'desktop executable');
|
|
198
|
+
const tool = platform === 'linux'
|
|
199
|
+
? { args: [binary], command: 'ldd' }
|
|
200
|
+
: platform === 'darwin'
|
|
201
|
+
? { args: ['-L', binary], command: 'otool' }
|
|
202
|
+
: { args: ['/DEPENDENTS', binary], command: 'dumpbin' };
|
|
203
|
+
const result = run(tool.command, tool.args, { stdio: ['ignore', 'pipe', 'pipe'] });
|
|
204
|
+
if (result.error) {
|
|
205
|
+
throw new Error(
|
|
206
|
+
`TN_DESKTOP_DEPENDENCY_TOOL_MISSING: '${tool.command}' is required to resolve native dependencies (${result.error.message}).`,
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
if (result.status !== 0) {
|
|
210
|
+
throw new Error(
|
|
211
|
+
`TN_DESKTOP_DEPENDENCY_RESOLUTION_FAILED: '${tool.command}' exited ${result.status ?? 'unknown'} for ${binary}.\n${result.stderr}`,
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
const libraries = parseLinkedLibraries(result.stdout, platform);
|
|
215
|
+
const unresolved = libraries.filter((library) => library.missing);
|
|
216
|
+
if (unresolved.length > 0) {
|
|
217
|
+
throw new Error(
|
|
218
|
+
`TN_DESKTOP_DEPENDENCY_MISSING: ${unresolved.map((library) => library.name).join(', ')} could not be resolved for ${binary}.`,
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
return libraries;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function isSystemLibrary(library, platform, systemRoot) {
|
|
225
|
+
if (platform === 'win32') {
|
|
226
|
+
const name = library.name.toLowerCase();
|
|
227
|
+
// api-ms-win-* and ext-ms-win-* are API Set contracts: virtual names the loader redirects to a
|
|
228
|
+
// real implementation. They are never files on disk and always come from the OS, and dumpbin
|
|
229
|
+
// reports them for anything linked against the UCRT, so no allowlist of real DLL names can
|
|
230
|
+
// enumerate them.
|
|
231
|
+
if (name.startsWith('api-ms-win-') || name.startsWith('ext-ms-win-')) return true;
|
|
232
|
+
if (SYSTEM_WINDOWS_DLLS.has(name)) return true;
|
|
233
|
+
// `dumpbin /DEPENDENTS` prints names and no paths, so an allowlist is the only thing standing
|
|
234
|
+
// between a real system DLL and a refusal. Ask the system directory the loader itself searches
|
|
235
|
+
// instead of guessing which names Microsoft ships; the allowlist above stays a fast path and
|
|
236
|
+
// keeps this decidable on a non-Windows host, where there is no system directory to consult.
|
|
237
|
+
return systemRoot !== undefined && existsSync(join(systemRoot, 'System32', library.name));
|
|
238
|
+
}
|
|
239
|
+
if (!library.path) return false;
|
|
240
|
+
if (platform === 'darwin') {
|
|
241
|
+
return library.path.startsWith('/usr/lib/') || library.path.startsWith('/System/');
|
|
242
|
+
}
|
|
243
|
+
return SYSTEM_LIBRARY_NAMES.has(library.name) ||
|
|
244
|
+
SYSTEM_LIBRARY_DIRECTORIES.some((directory) => library.path.startsWith(directory));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Split the executable's libraries into the ones the player machine provides (recorded as
|
|
249
|
+
* prerequisites) and the ones the container must carry. A library that is neither system nor
|
|
250
|
+
* locatable fails closed rather than shipping an artifact that only launches here.
|
|
251
|
+
*/
|
|
252
|
+
export function classifyDependencies(
|
|
253
|
+
libraries,
|
|
254
|
+
{ platform = process.platform, systemRoot = process.env.SystemRoot } = {},
|
|
255
|
+
) {
|
|
256
|
+
const bundled = [];
|
|
257
|
+
const prerequisites = [];
|
|
258
|
+
for (const library of libraries) {
|
|
259
|
+
if (isSystemLibrary(library, platform, systemRoot)) {
|
|
260
|
+
prerequisites.push({ name: library.name });
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
if (!library.path) {
|
|
264
|
+
throw new Error(`TN_DESKTOP_DEPENDENCY_UNLOCATABLE: ${library.name} is not a system library and has no path to copy.`);
|
|
265
|
+
}
|
|
266
|
+
assertFile(library.path, `native dependency ${library.name}`);
|
|
267
|
+
bundled.push({ name: basename(library.path), source: library.path });
|
|
268
|
+
}
|
|
269
|
+
return { bundled, prerequisites };
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* The loading sequence the consumer config declared, recorded so a distributed container can be
|
|
274
|
+
* checked against the config that built it.
|
|
275
|
+
*
|
|
276
|
+
* The splash is drawn by the game's own generated `src/render/loading.ts` from assets already
|
|
277
|
+
* inside the bundle, so this records identity rather than a second copy: the authored colour and
|
|
278
|
+
* the authored image's hash. A game with no `bootSplash` records that it has none — writing
|
|
279
|
+
* nothing would leave a consumer unable to tell "no splash configured" from "evidence missing",
|
|
280
|
+
* which is the difference between a pass and a false pass.
|
|
281
|
+
*/
|
|
282
|
+
export function containerLoading(config) {
|
|
283
|
+
const bootSplash = config?.bootSplash;
|
|
284
|
+
if (bootSplash === undefined || bootSplash === null) return { bootSplash: null };
|
|
285
|
+
let imageSha256 = null;
|
|
286
|
+
if (bootSplash.image !== undefined) {
|
|
287
|
+
if (!bootSplash.image || !existsSync(bootSplash.image) || !statSync(bootSplash.image).isFile()) {
|
|
288
|
+
throw new Error(
|
|
289
|
+
`TN_DESKTOP_SPLASH_IMAGE_MISSING: bootSplash.image does not exist: ${bootSplash.image ?? '(not provided)'}`,
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
imageSha256 = sha256File(bootSplash.image);
|
|
293
|
+
}
|
|
294
|
+
return {
|
|
295
|
+
bootSplash: {
|
|
296
|
+
...(bootSplash.backgroundColor === undefined
|
|
297
|
+
? {}
|
|
298
|
+
: { backgroundColor: bootSplash.backgroundColor }),
|
|
299
|
+
imageSha256,
|
|
300
|
+
},
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export function containerMetadata({ platform = process.platform, config }) {
|
|
305
|
+
const app = config?.app ?? {};
|
|
306
|
+
const id = app.id ?? 'com.threenative.game';
|
|
307
|
+
const name = app.name ?? 'ThreeNative';
|
|
308
|
+
const version = app.version ?? '0.1.0';
|
|
309
|
+
const build = String(app.build ?? 1);
|
|
310
|
+
const slug = containerSlug(name);
|
|
311
|
+
const xml = (value) => String(value).replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>');
|
|
312
|
+
if (platform === 'linux') {
|
|
313
|
+
return {
|
|
314
|
+
[`share/applications/${id}.desktop`]: [
|
|
315
|
+
'[Desktop Entry]',
|
|
316
|
+
'Type=Application',
|
|
317
|
+
`Name=${name}`,
|
|
318
|
+
`Exec=${slug}`,
|
|
319
|
+
`TryExec=${slug}`,
|
|
320
|
+
`Icon=${id}`,
|
|
321
|
+
'Terminal=false',
|
|
322
|
+
'Categories=Game;',
|
|
323
|
+
'StartupNotify=true',
|
|
324
|
+
'',
|
|
325
|
+
].join('\n'),
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
if (platform === 'darwin') {
|
|
329
|
+
return {
|
|
330
|
+
'Contents/Info.plist': [
|
|
331
|
+
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
332
|
+
'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">',
|
|
333
|
+
'<plist version="1.0">',
|
|
334
|
+
'<dict>',
|
|
335
|
+
' <key>CFBundleDevelopmentRegion</key><string>en</string>',
|
|
336
|
+
` <key>CFBundleExecutable</key><string>${xml(slug)}</string>`,
|
|
337
|
+
` <key>CFBundleIdentifier</key><string>${xml(id)}</string>`,
|
|
338
|
+
' <key>CFBundleInfoDictionaryVersion</key><string>6.0</string>',
|
|
339
|
+
` <key>CFBundleName</key><string>${xml(name)}</string>`,
|
|
340
|
+
` <key>CFBundleDisplayName</key><string>${xml(name)}</string>`,
|
|
341
|
+
' <key>CFBundlePackageType</key><string>APPL</string>',
|
|
342
|
+
` <key>CFBundleShortVersionString</key><string>${xml(version)}</string>`,
|
|
343
|
+
` <key>CFBundleVersion</key><string>${xml(build)}</string>`,
|
|
344
|
+
` <key>CFBundleIconFile</key><string>${xml(slug)}</string>`,
|
|
345
|
+
' <key>LSMinimumSystemVersion</key><string>11.0</string>',
|
|
346
|
+
' <key>NSHighResolutionCapable</key><true/>',
|
|
347
|
+
'</dict>',
|
|
348
|
+
'</plist>',
|
|
349
|
+
'',
|
|
350
|
+
].join('\n'),
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
if (platform === 'win32') {
|
|
354
|
+
// Windows identity lives in the executable's PE resource section, written by `rcedit`.
|
|
355
|
+
return {};
|
|
356
|
+
}
|
|
357
|
+
throw new Error(`TN_DESKTOP_CONTAINER_UNSUPPORTED: no metadata for '${platform}'.`);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** Root-relative paths, one per platform, so every consumer resolves from the container root. */
|
|
361
|
+
function layout(platform, { appName, executableName, iconName }) {
|
|
362
|
+
const slug = containerSlug(appName);
|
|
363
|
+
if (platform === 'linux') {
|
|
364
|
+
return {
|
|
365
|
+
// `findExternalBundle` looks beside the executable on Linux and Windows, and in
|
|
366
|
+
// Contents/Resources for a macOS .app. Stage it where the loader already searches.
|
|
367
|
+
bundle: 'game.bundle',
|
|
368
|
+
executable: slug,
|
|
369
|
+
icon: `share/icons/hicolor/256x256/apps/${iconName}.png`,
|
|
370
|
+
manifest: CONTAINER_MANIFEST,
|
|
371
|
+
ui: 'ui',
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
if (platform === 'darwin') {
|
|
375
|
+
return {
|
|
376
|
+
bundle: 'Contents/Resources/game.bundle',
|
|
377
|
+
executable: `Contents/MacOS/${slug}`,
|
|
378
|
+
icon: `Contents/Resources/${slug}.icns`,
|
|
379
|
+
manifest: `Contents/Resources/${CONTAINER_MANIFEST}`,
|
|
380
|
+
// SDL_GetBasePath() resolves to Contents/Resources/ for a bundled macOS app, and that is
|
|
381
|
+
// what src/cli/main.cpp joins a relative ui root against. Staging beside the executable
|
|
382
|
+
// launches the container with no HUD.
|
|
383
|
+
ui: 'Contents/Resources/ui',
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
return {
|
|
387
|
+
bundle: 'game.bundle',
|
|
388
|
+
executable: executableName,
|
|
389
|
+
icon: `${iconName}.png`,
|
|
390
|
+
manifest: CONTAINER_MANIFEST,
|
|
391
|
+
ui: 'ui',
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function dependencyRelativePath(platform, name) {
|
|
396
|
+
if (platform === 'darwin') return `Contents/Frameworks/${name}`;
|
|
397
|
+
if (platform === 'linux') return `lib/${name}`;
|
|
398
|
+
return name;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Wrap a PNG in a single-image Windows .ico. rcedit's `--set-icon` needs an icon Windows can parse
|
|
403
|
+
* and refuses a bare PNG, and Vista onwards reads a PNG-compressed icon entry directly, so the
|
|
404
|
+
* container needs no image tool on the packaging machine and no new player prerequisite.
|
|
405
|
+
*/
|
|
406
|
+
export function pngToIco(source, destination) {
|
|
407
|
+
const png = readFileSync(source);
|
|
408
|
+
// 8-byte signature, then the IHDR length and tag, then width and height as big-endian uint32.
|
|
409
|
+
if (png.length < 24 || png.readUInt32BE(0) !== 0x89504e47 || png.readUInt32BE(12) !== 0x49484452) {
|
|
410
|
+
throw new Error(`TN_DESKTOP_RESOURCE_FAILED: ${source} is not a PNG, so it cannot become a Windows icon.`);
|
|
411
|
+
}
|
|
412
|
+
const width = png.readUInt32BE(16);
|
|
413
|
+
const height = png.readUInt32BE(20);
|
|
414
|
+
const header = Buffer.alloc(22);
|
|
415
|
+
header.writeUInt16LE(0, 0);
|
|
416
|
+
header.writeUInt16LE(1, 2);
|
|
417
|
+
header.writeUInt16LE(1, 4);
|
|
418
|
+
// 0 means 256 in an icon directory entry; anything larger cannot be described at all.
|
|
419
|
+
header.writeUInt8(width >= 256 ? 0 : width, 6);
|
|
420
|
+
header.writeUInt8(height >= 256 ? 0 : height, 7);
|
|
421
|
+
header.writeUInt16LE(1, 10);
|
|
422
|
+
header.writeUInt16LE(32, 12);
|
|
423
|
+
header.writeUInt32LE(png.length, 14);
|
|
424
|
+
header.writeUInt32LE(header.length, 18);
|
|
425
|
+
writeFileSync(destination, Buffer.concat([header, png]));
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function buildIcon(icon, destination, { platform, run }) {
|
|
429
|
+
if (platform === 'linux') {
|
|
430
|
+
copyFileSync(icon, destination);
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
if (platform === 'darwin') {
|
|
434
|
+
if (icon.endsWith('.icns')) {
|
|
435
|
+
copyFileSync(icon, destination);
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
// .icns is what Finder and Dock read; it is built with the OS's own tools, never hand-rolled.
|
|
439
|
+
const iconsetDirectory = mkdtempSync(join(tmpdir(), 'threenative-iconset-'));
|
|
440
|
+
const iconset = join(iconsetDirectory, 'app.iconset');
|
|
441
|
+
try {
|
|
442
|
+
mkdirSync(iconset);
|
|
443
|
+
for (const size of [16, 32, 128, 256, 512]) {
|
|
444
|
+
for (const scale of [1, 2]) {
|
|
445
|
+
const pixels = String(size * scale);
|
|
446
|
+
const name = `icon_${size}x${size}${scale === 2 ? '@2x' : ''}.png`;
|
|
447
|
+
const sips = run('sips', ['-z', pixels, pixels, icon, '--out', join(iconset, name)], {});
|
|
448
|
+
if (sips.error) throw new Error(`TN_DESKTOP_RESOURCE_TOOL_MISSING: 'sips' is required to build an .icns (${sips.error.message}).`);
|
|
449
|
+
if (sips.status !== 0) throw new Error(`TN_DESKTOP_RESOURCE_FAILED: sips exited ${sips.status ?? 'unknown'}.`);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
const built = run('iconutil', ['-c', 'icns', iconset, '-o', destination], {});
|
|
453
|
+
if (built.error) throw new Error(`TN_DESKTOP_RESOURCE_TOOL_MISSING: 'iconutil' is required to build an .icns (${built.error.message}).`);
|
|
454
|
+
if (built.status !== 0) throw new Error(`TN_DESKTOP_RESOURCE_FAILED: iconutil exited ${built.status ?? 'unknown'}.`);
|
|
455
|
+
} finally {
|
|
456
|
+
rmSync(iconsetDirectory, { force: true, recursive: true });
|
|
457
|
+
}
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
throw new Error(`TN_DESKTOP_CONTAINER_UNSUPPORTED: no icon builder for '${platform}'.`);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/** The local file header every zip starts with, and an empty archive's end-of-central-directory. */
|
|
464
|
+
function isZipArchive(path) {
|
|
465
|
+
const header = Buffer.alloc(4);
|
|
466
|
+
let handle;
|
|
467
|
+
try {
|
|
468
|
+
handle = openSync(path, 'r');
|
|
469
|
+
if (readSync(handle, header, 0, 4, 0) !== 4) return false;
|
|
470
|
+
} catch {
|
|
471
|
+
return false;
|
|
472
|
+
} finally {
|
|
473
|
+
if (handle !== undefined) closeSync(handle);
|
|
474
|
+
}
|
|
475
|
+
return header.toString('latin1') === 'PK\u0003\u0004' || header.toString('latin1') === 'PK\u0005\u0006';
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Tools that write a real `.zip`, best first.
|
|
480
|
+
*
|
|
481
|
+
* tar.gz has one universal writer; .zip does not. `zip` is absent from a stock Arch and from
|
|
482
|
+
* Windows entirely — where packaging a Windows container therefore failed on the one tool it
|
|
483
|
+
* needed, on the OS the container is for. libarchive fills the gap: `bsdtar` on Arch and Homebrew,
|
|
484
|
+
* and plain `tar` on macOS and Windows 10 1803 onwards, where `-a` picks the format from the .zip
|
|
485
|
+
* suffix. Both carry the Unix mode bits, so a macOS `.app` still has its executable bit after a
|
|
486
|
+
* round trip; `7z` is deliberately not on this list, because its zip writer drops them and every
|
|
487
|
+
* platform that needs a zip already ships one of these three.
|
|
488
|
+
*/
|
|
489
|
+
function zipWriters(candidate, staging, rootFolder) {
|
|
490
|
+
return [
|
|
491
|
+
{ args: ['-r', '-q', candidate, rootFolder], name: 'zip', options: { cwd: staging } },
|
|
492
|
+
{ args: ['-a', '-c', '-f', candidate, rootFolder], name: 'bsdtar', options: { cwd: staging } },
|
|
493
|
+
{ args: ['-a', '-c', '-f', candidate, rootFolder], name: 'tar', options: { cwd: staging } },
|
|
494
|
+
];
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function archiveContainer({ platform, staging, rootFolder, output, run }) {
|
|
498
|
+
// zip updates existing archives in place, retaining removed files. Build a fresh candidate on
|
|
499
|
+
// the output filesystem and replace the old artifact only after the archiver succeeds.
|
|
500
|
+
const archiveDirectory = mkdtempSync(join(dirname(output), '.threenative-archive-'));
|
|
501
|
+
const candidate = join(archiveDirectory, basename(output));
|
|
502
|
+
try {
|
|
503
|
+
// tar.gz has one universal writer; .zip does not. `zip` is not installed on a stock Arch or
|
|
504
|
+
// slim container image, and has never existed on Windows — where packaging a Windows container
|
|
505
|
+
// therefore failed on the one tool it needed, on the OS the container is for. Each candidate
|
|
506
|
+
// below writes a .zip that every player's OS unpacks natively, and the first one installed
|
|
507
|
+
// wins, so a machine with any of them can package a release.
|
|
508
|
+
const candidates = platform === 'linux'
|
|
509
|
+
? [{ args: ['-czf', candidate, '-C', staging, rootFolder], name: 'tar' }]
|
|
510
|
+
: zipWriters(candidate, staging, rootFolder);
|
|
511
|
+
const wantsZip = desktopContainerFormat(platform) === 'zip';
|
|
512
|
+
let used;
|
|
513
|
+
let result;
|
|
514
|
+
let mislabelled;
|
|
515
|
+
for (const command of candidates) {
|
|
516
|
+
// zip updates in place and a rejected candidate leaves its own bytes behind, so every
|
|
517
|
+
// attempt starts from nothing.
|
|
518
|
+
rmSync(candidate, { force: true });
|
|
519
|
+
result = run(command.name, command.args, command.options ?? {});
|
|
520
|
+
// Only a missing tool is worth another candidate. A tool that ran and failed is a real
|
|
521
|
+
// failure, and retrying past it would hide it behind whichever archiver came next.
|
|
522
|
+
if (result.error) continue;
|
|
523
|
+
// An archiver is trusted to run, not to have written the format its suffix promises: GNU
|
|
524
|
+
// tar answers to libarchive's name and compresses by suffix, which it does not know for
|
|
525
|
+
// .zip, so it leaves a gzip wearing a .zip extension. That is this candidate failing rather
|
|
526
|
+
// than the build failing — the next one may be installed — but it must never ship.
|
|
527
|
+
if (result.status === 0 && wantsZip && command.name !== 'zip' && !isZipArchive(candidate)) {
|
|
528
|
+
mislabelled = command.name;
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
531
|
+
used = command;
|
|
532
|
+
break;
|
|
533
|
+
}
|
|
534
|
+
if (used === undefined) {
|
|
535
|
+
rmSync(candidate, { force: true });
|
|
536
|
+
const names = candidates.map((entry) => entry.name).join(', ');
|
|
537
|
+
throw new Error(
|
|
538
|
+
`TN_DESKTOP_ARCHIVE_TOOL_MISSING: one of ${names} is required to write ${output} ` +
|
|
539
|
+
(mislabelled === undefined
|
|
540
|
+
? `(${result.error.message}).`
|
|
541
|
+
: `('${mislabelled}' ran but wrote no zip archive).`),
|
|
542
|
+
);
|
|
543
|
+
}
|
|
544
|
+
if (result.status !== 0) {
|
|
545
|
+
throw new Error(
|
|
546
|
+
`TN_DESKTOP_ARCHIVE_FAILED: '${used.name}' exited ${result.status ?? 'unknown'} for ${output}.\n${result.stderr}`,
|
|
547
|
+
);
|
|
548
|
+
}
|
|
549
|
+
renameSync(candidate, output);
|
|
550
|
+
} finally {
|
|
551
|
+
rmSync(archiveDirectory, { force: true, recursive: true });
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/** Extract a release archive so a verifier can resolve its payload outside the build tree. */
|
|
556
|
+
export function extractContainer(archive, destination, { platform = process.platform, run = exec } = {}) {
|
|
557
|
+
assertFile(archive, 'desktop container archive');
|
|
558
|
+
mkdirSync(destination, { recursive: true });
|
|
559
|
+
// Same portability gap as writing, on the other side of it: `unzip` is absent from a stock Arch
|
|
560
|
+
// and from Windows, so a host that can now write a container could not unpack its own. libarchive
|
|
561
|
+
// reads zip through `tar -xf` on both, and all three preserve the Unix mode the launcher needs.
|
|
562
|
+
const candidates = desktopContainerFormat(platform) === 'tar.gz'
|
|
563
|
+
? [{ args: ['-xzf', archive, '-C', destination], name: 'tar' }]
|
|
564
|
+
: [
|
|
565
|
+
{ args: ['-q', archive, '-d', destination], name: 'unzip' },
|
|
566
|
+
{ args: ['-x', '-f', archive, '-C', destination], name: 'bsdtar' },
|
|
567
|
+
{ args: ['-x', '-f', archive, '-C', destination], name: 'tar' },
|
|
568
|
+
];
|
|
569
|
+
let used;
|
|
570
|
+
let result;
|
|
571
|
+
for (const command of candidates) {
|
|
572
|
+
result = run(command.name, command.args, {});
|
|
573
|
+
// A tool that ran and failed is a real failure; only a missing one is worth the next candidate.
|
|
574
|
+
if (result.error) continue;
|
|
575
|
+
used = command;
|
|
576
|
+
break;
|
|
577
|
+
}
|
|
578
|
+
if (used === undefined) {
|
|
579
|
+
throw new Error(
|
|
580
|
+
`TN_DESKTOP_EXTRACT_TOOL_MISSING: one of ${candidates.map((entry) => entry.name).join(', ')} is ` +
|
|
581
|
+
`required to unpack ${archive} (${result.error.message}).`,
|
|
582
|
+
);
|
|
583
|
+
}
|
|
584
|
+
if (result.status !== 0) {
|
|
585
|
+
throw new Error(`TN_DESKTOP_EXTRACT_FAILED: '${used.name}' exited ${result.status ?? 'unknown'} for ${archive}.`);
|
|
586
|
+
}
|
|
587
|
+
return join(destination, locateContainerRoot(destination));
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/** The single top-level directory an archive extracted to. */
|
|
591
|
+
export function locateContainerRoot(extractDirectory) {
|
|
592
|
+
const directories = readdirSync(extractDirectory, { withFileTypes: true })
|
|
593
|
+
.filter((entry) => entry.isDirectory())
|
|
594
|
+
.map((entry) => entry.name);
|
|
595
|
+
if (directories.length !== 1) {
|
|
596
|
+
throw new Error(
|
|
597
|
+
`TN_DESKTOP_CONTAINER_LAYOUT_INVALID: expected one top-level directory in ${extractDirectory}, found ${directories.length}.`,
|
|
598
|
+
);
|
|
599
|
+
}
|
|
600
|
+
return directories[0];
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Resolve a container from wherever it was unpacked. Every recorded resource is addressed
|
|
605
|
+
* relative to the container root, so a move is legal; a resource absent or whose bytes changed
|
|
606
|
+
* is a container that must not launch, not a warning.
|
|
607
|
+
*/
|
|
608
|
+
export function resolveContainer(root, { platform = process.platform, run = exec } = {}) {
|
|
609
|
+
const resolvedRoot = resolve(root);
|
|
610
|
+
assertDirectory(resolvedRoot, 'desktop container root');
|
|
611
|
+
const candidates = platform === 'darwin'
|
|
612
|
+
? [join('Contents', 'Resources', CONTAINER_MANIFEST), CONTAINER_MANIFEST]
|
|
613
|
+
: [CONTAINER_MANIFEST, join('Contents', 'Resources', CONTAINER_MANIFEST)];
|
|
614
|
+
const manifestRelative = candidates.find((candidate) => existsSync(join(resolvedRoot, candidate)));
|
|
615
|
+
if (!manifestRelative) {
|
|
616
|
+
throw new Error(`TN_DESKTOP_CONTAINER_MANIFEST_MISSING: no ${CONTAINER_MANIFEST} under ${resolvedRoot}`);
|
|
617
|
+
}
|
|
618
|
+
let manifest;
|
|
619
|
+
try {
|
|
620
|
+
manifest = JSON.parse(readFileSync(join(resolvedRoot, manifestRelative), 'utf8'));
|
|
621
|
+
} catch (error) {
|
|
622
|
+
throw new Error(`TN_DESKTOP_CONTAINER_MANIFEST_INVALID: ${error instanceof Error ? error.message : String(error)}`);
|
|
623
|
+
}
|
|
624
|
+
if (!manifest || typeof manifest !== 'object' || Array.isArray(manifest) ||
|
|
625
|
+
manifest.schemaVersion !== CONTAINER_SCHEMA_VERSION ||
|
|
626
|
+
!manifest.resources || typeof manifest.resources !== 'object' || Array.isArray(manifest.resources) ||
|
|
627
|
+
!Array.isArray(manifest.dependencies) ||
|
|
628
|
+
(manifest.ui !== null && (!manifest.ui || typeof manifest.ui !== 'object' || Array.isArray(manifest.ui)))) {
|
|
629
|
+
throw new Error('TN_DESKTOP_CONTAINER_MANIFEST_INVALID: unsupported schema or missing payload inventory.');
|
|
630
|
+
}
|
|
631
|
+
// Without the bundle the executable is a bare runtime that prints CLI usage instead of the game,
|
|
632
|
+
// which is exactly the failure a released container must never reach a player with. Name that
|
|
633
|
+
// case rather than letting it fall through as a required resource called 'undefined': a container
|
|
634
|
+
// built before the game moved beside the executable has no `bundle` at all.
|
|
635
|
+
if (typeof manifest.bundle !== 'string' || !manifest.bundle) {
|
|
636
|
+
throw new Error(
|
|
637
|
+
'TN_DESKTOP_CONTAINER_MANIFEST_INVALID: the manifest names no game bundle, so this container ' +
|
|
638
|
+
'predates the sidecar layout and its executable would launch the runtime CLI. Rebuild it.',
|
|
639
|
+
);
|
|
640
|
+
}
|
|
641
|
+
const required = [
|
|
642
|
+
manifest.executable,
|
|
643
|
+
manifest.bundle,
|
|
644
|
+
...manifest.dependencies.map((dependency) => dependency?.path),
|
|
645
|
+
...(manifest.ui === null ? [] : [manifest.ui.entry]),
|
|
646
|
+
...(manifest.app?.icon === undefined ? [] : [manifest.app.icon]),
|
|
647
|
+
];
|
|
648
|
+
for (const path of required) {
|
|
649
|
+
if (typeof path !== 'string' || !path || !Object.hasOwn(manifest.resources, path)) {
|
|
650
|
+
throw new Error(`TN_DESKTOP_CONTAINER_MANIFEST_INVALID: required resource '${path}' has no integrity record.`);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
const signedMac = /^darwin-(?:x64|arm64)$/u.test(manifest.platform) &&
|
|
654
|
+
manifest.signed === true && manifest.signingScheme === 'codesign';
|
|
655
|
+
if (signedMac && (manifestRelative !== join('Contents', 'Resources', CONTAINER_MANIFEST) ||
|
|
656
|
+
!/^Contents\/MacOS\/[^/\\]+$/u.test(manifest.executable) ||
|
|
657
|
+
manifest.resources[manifest.executable]?.signature !== 'codesign')) {
|
|
658
|
+
throw new Error('TN_DESKTOP_CONTAINER_MANIFEST_INVALID: a signed macOS app requires a sealed internal manifest and a codesign integrity record for its main executable.');
|
|
659
|
+
}
|
|
660
|
+
const physicalRoot = realpathSync(resolvedRoot);
|
|
661
|
+
for (const [relativePath, expected] of Object.entries(manifest.resources)) {
|
|
662
|
+
const signature = signedMac && relativePath === manifest.executable && expected?.signature === 'codesign';
|
|
663
|
+
const hash = typeof expected?.sha256 === 'string' && /^[a-f0-9]{64}$/u.test(expected.sha256);
|
|
664
|
+
if (!relativePath || !expected || Array.isArray(expected) || Object.keys(expected).length !== 1 || (!signature && !hash)) {
|
|
665
|
+
throw new Error(`TN_DESKTOP_CONTAINER_MANIFEST_INVALID: invalid integrity record for '${relativePath}'.`);
|
|
666
|
+
}
|
|
667
|
+
const absolute = resolve(resolvedRoot, relativePath);
|
|
668
|
+
const within = relative(resolvedRoot, absolute);
|
|
669
|
+
if (isAbsolute(relativePath) || within === '..' || within.startsWith(`..${sep}`) || isAbsolute(within)) {
|
|
670
|
+
throw new Error(
|
|
671
|
+
`TN_DESKTOP_CONTAINER_MANIFEST_INVALID: ${relativePath} escapes the container root.`,
|
|
672
|
+
);
|
|
673
|
+
}
|
|
674
|
+
if (!existsSync(absolute) || !statSync(absolute).isFile()) {
|
|
675
|
+
throw new Error(
|
|
676
|
+
`TN_DESKTOP_CONTAINER_INCOMPLETE: ${relativePath} is named by the container manifest but is not in the payload.`,
|
|
677
|
+
);
|
|
678
|
+
}
|
|
679
|
+
const physicalPath = relative(physicalRoot, realpathSync(absolute));
|
|
680
|
+
if (physicalPath === '..' || physicalPath.startsWith(`..${sep}`) || isAbsolute(physicalPath)) {
|
|
681
|
+
throw new Error(`TN_DESKTOP_CONTAINER_MANIFEST_INVALID: ${relativePath} resolves outside the container root.`);
|
|
682
|
+
}
|
|
683
|
+
if (signature) continue;
|
|
684
|
+
const actual = sha256File(absolute);
|
|
685
|
+
if (actual !== expected.sha256) {
|
|
686
|
+
throw new Error(
|
|
687
|
+
`TN_DESKTOP_CONTAINER_TAMPERED: ${relativePath} hashes ${actual}, not the recorded ${expected.sha256}.`,
|
|
688
|
+
);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
if (signedMac) {
|
|
692
|
+
if (platform !== 'darwin') {
|
|
693
|
+
throw new Error('TN_DESKTOP_CODESIGN_HOST_REQUIRED: verify this signed application on macOS with codesign.');
|
|
694
|
+
}
|
|
695
|
+
signingTool(run, 'codesign', ['--verify', '--strict', '--deep', resolvedRoot], 'TN_DESKTOP_CODESIGN_VERIFY');
|
|
696
|
+
}
|
|
697
|
+
return manifest;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/** The game identity the container carries must equal the one the author configured. */
|
|
701
|
+
export function assertContainerIdentity(manifest, config, { icon } = {}) {
|
|
702
|
+
const app = config?.app ?? {};
|
|
703
|
+
for (const field of ['id', 'name', 'version']) {
|
|
704
|
+
if (manifest.app?.[field] !== app[field]) {
|
|
705
|
+
throw new Error(
|
|
706
|
+
`TN_DESKTOP_BRAND_MISMATCH: container app.${field} is '${manifest.app?.[field]}', config says '${app[field]}'.`,
|
|
707
|
+
);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
if (icon !== undefined) {
|
|
711
|
+
assertFile(icon, 'app icon');
|
|
712
|
+
const expected = sha256File(icon);
|
|
713
|
+
if (manifest.app?.iconSha256 !== expected) {
|
|
714
|
+
throw new Error('TN_DESKTOP_BRAND_MISMATCH: the container did not embed the configured app icon.');
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
return manifest;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
/**
|
|
721
|
+
* Signing is OS tooling run outside the game runtime. Non-secret identity/options come from
|
|
722
|
+
* validated build inputs; secrets stay in the OS keychain. Linux has no Authenticode or
|
|
723
|
+
* notarization, so a Linux container is never described as signed.
|
|
724
|
+
*/
|
|
725
|
+
|
|
726
|
+
/** Notarization evidence must name the artifact it notarized and report Apple's acceptance. */
|
|
727
|
+
export function assertNotaryEvidence({ artifactSha256, evidence }) {
|
|
728
|
+
if (!evidence || typeof evidence.artifactSha256 !== 'string') {
|
|
729
|
+
throw new Error('TN_DESKTOP_NOTARY_MISSING: notarization evidence carries no artifact sha256.');
|
|
730
|
+
}
|
|
731
|
+
if (evidence.artifactSha256 !== artifactSha256) {
|
|
732
|
+
throw new Error(
|
|
733
|
+
`TN_DESKTOP_NOTARY_MISMATCH: the notarization evidence is for ${evidence.artifactSha256}, not the produced artifact ${artifactSha256}.`,
|
|
734
|
+
);
|
|
735
|
+
}
|
|
736
|
+
if (evidence.status !== 'Accepted') {
|
|
737
|
+
throw new Error(`TN_DESKTOP_NOTARY_FAILED: notarization returned '${evidence.status}'.`);
|
|
738
|
+
}
|
|
739
|
+
return evidence;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
function signingTool(run, name, args, code) {
|
|
743
|
+
const result = run(name, args, {});
|
|
744
|
+
if (result.error) {
|
|
745
|
+
throw new Error(`${code}_TOOL_MISSING: '${name}' is required to sign the desktop artifact (${result.error.message}).`);
|
|
746
|
+
}
|
|
747
|
+
if (result.status !== 0) {
|
|
748
|
+
throw new Error(`${code}_FAILED: '${name}' exited ${result.status ?? 'unknown'}.\n${result.stderr ?? ''}`);
|
|
749
|
+
}
|
|
750
|
+
return result;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
/**
|
|
754
|
+
* Sign the staged artifact with the platform's own tool. Throws instead of returning when signing
|
|
755
|
+
* fails, so no archive is written for a release that only claims to be signed.
|
|
756
|
+
*/
|
|
757
|
+
export function signDesktopArtifact({ platform = process.platform, target, signing, run = exec } = {}) {
|
|
758
|
+
if (platform === 'linux') {
|
|
759
|
+
return { scheme: 'none', signed: false, reason: 'Linux has no Authenticode/notarization; the archive carries integrity metadata.' };
|
|
760
|
+
}
|
|
761
|
+
if (!target || !existsSync(target)) throw new Error(missing(target ?? '(not provided)', 'artifact to sign'));
|
|
762
|
+
if (platform === 'darwin') {
|
|
763
|
+
if (!signing?.identity) {
|
|
764
|
+
throw new Error('TN_DESKTOP_SIGNING_CREDENTIALS_MISSING: macOS signing needs a Developer ID identity; unsigned preparation can proceed without signing.');
|
|
765
|
+
}
|
|
766
|
+
// Hardened runtime otherwise denies the MAP_JIT memory V8/JavaScriptCore execute from.
|
|
767
|
+
// Entitlements belong to the application executable, never its nested dynamic libraries.
|
|
768
|
+
const temporary = statSync(target).isDirectory() ? mkdtempSync(join(tmpdir(), 'threenative-sign-')) : undefined;
|
|
769
|
+
try {
|
|
770
|
+
const entitlements = temporary === undefined ? [] : ['--entitlements', join(temporary, 'jit.plist')];
|
|
771
|
+
if (temporary !== undefined) {
|
|
772
|
+
writeFileSync(entitlements[1], '<?xml version="1.0" encoding="UTF-8"?>\n<plist version="1.0"><dict><key>com.apple.security.cs.allow-jit</key><true/></dict></plist>\n');
|
|
773
|
+
}
|
|
774
|
+
signingTool(run, 'codesign', ['--force', '--options', 'runtime', ...entitlements, '--sign', signing.identity, target], 'TN_DESKTOP_CODESIGN');
|
|
775
|
+
signingTool(run, 'codesign', ['--verify', '--strict', '--deep', target], 'TN_DESKTOP_CODESIGN_VERIFY');
|
|
776
|
+
} finally {
|
|
777
|
+
if (temporary !== undefined) rmSync(temporary, { force: true, recursive: true });
|
|
778
|
+
}
|
|
779
|
+
// `target` is the `.app` directory, which has no single file hash; notarization evidence binds
|
|
780
|
+
// to the archive's bytes instead.
|
|
781
|
+
return { scheme: 'codesign', signed: true };
|
|
782
|
+
}
|
|
783
|
+
if (platform === 'win32') {
|
|
784
|
+
if (signing?.certificate && signing?.subject) {
|
|
785
|
+
throw new Error('TN_DESKTOP_SIGNING_CREDENTIALS_AMBIGUOUS: Windows signing takes a certificate file or a store subject, not both; one of them would be silently ignored.');
|
|
786
|
+
}
|
|
787
|
+
if (!signing?.certificate && !signing?.subject) {
|
|
788
|
+
throw new Error('TN_DESKTOP_SIGNING_CREDENTIALS_MISSING: Windows signing needs a code-signing certificate file or a certificate store subject; unsigned preparation can proceed without signing.');
|
|
789
|
+
}
|
|
790
|
+
const timestamp = signing.timestampUrl ? ['/tr', signing.timestampUrl, '/td', 'sha256'] : [];
|
|
791
|
+
// `/f` reads a PFX and takes a `/p` password this contract deliberately does not carry, so it
|
|
792
|
+
// only ever works for a password-less file - which is not what a CA issues. `/n` signs from the
|
|
793
|
+
// Windows certificate store, keeping the private key in the OS keychain as the README promises.
|
|
794
|
+
const credential = signing.subject ? ['/n', signing.subject] : ['/f', signing.certificate];
|
|
795
|
+
signingTool(run, 'signtool', ['sign', '/fd', 'sha256', ...timestamp, ...credential, target], 'TN_DESKTOP_SIGNTOOL');
|
|
796
|
+
signingTool(run, 'signtool', ['verify', '/pa', target], 'TN_DESKTOP_SIGNTOOL_VERIFY');
|
|
797
|
+
return { artifactSha256: sha256File(target), scheme: 'signtool', signed: true };
|
|
798
|
+
}
|
|
799
|
+
throw new Error(`TN_DESKTOP_CONTAINER_UNSUPPORTED: no signing tool for '${platform}'.`);
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
/** Submit the archive to Apple and return evidence bound to that exact archive's bytes. */
|
|
803
|
+
export function notarizeArchive({ archive, signing, run = exec } = {}) {
|
|
804
|
+
if (!signing?.keychainProfile) {
|
|
805
|
+
throw new Error('TN_DESKTOP_SIGNING_CREDENTIALS_MISSING: macOS notarization needs a notarytool keychain profile.');
|
|
806
|
+
}
|
|
807
|
+
assertFile(archive, 'desktop container archive');
|
|
808
|
+
const submittedSha256 = sha256File(archive);
|
|
809
|
+
const result = signingTool(
|
|
810
|
+
run,
|
|
811
|
+
'xcrun',
|
|
812
|
+
['notarytool', 'submit', archive, '--keychain-profile', signing.keychainProfile, '--wait', '--output-format', 'json'],
|
|
813
|
+
'TN_DESKTOP_NOTARYTOOL',
|
|
814
|
+
);
|
|
815
|
+
let payload;
|
|
816
|
+
try {
|
|
817
|
+
payload = JSON.parse(result.stdout);
|
|
818
|
+
} catch {
|
|
819
|
+
throw new Error(`TN_DESKTOP_NOTARY_FAILED: notarytool returned unreadable JSON: ${result.stdout}`);
|
|
820
|
+
}
|
|
821
|
+
const artifactSha256 = sha256File(archive);
|
|
822
|
+
return assertNotaryEvidence({
|
|
823
|
+
artifactSha256,
|
|
824
|
+
evidence: { artifactSha256: submittedSha256, id: payload.id, status: payload.status },
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
/**
|
|
829
|
+
* Build one complete desktop container, unsigned unless signing inputs are supplied.
|
|
830
|
+
*
|
|
831
|
+
* The caller supplies the executable the runtime compiler produced, the UI bundle when the game
|
|
832
|
+
* uses the web renderer, and the dependencies discovered from that executable. Everything is
|
|
833
|
+
* addressed relative to a single top-level directory, so the archive can be moved anywhere.
|
|
834
|
+
*/
|
|
835
|
+
export function packageDesktopContainer({
|
|
836
|
+
platform = process.platform,
|
|
837
|
+
arch = process.arch,
|
|
838
|
+
bundle,
|
|
839
|
+
executable,
|
|
840
|
+
executableName,
|
|
841
|
+
uiDirectory,
|
|
842
|
+
uiRenderer = 'native',
|
|
843
|
+
dependencies = [],
|
|
844
|
+
prerequisites = [],
|
|
845
|
+
config,
|
|
846
|
+
icon,
|
|
847
|
+
output,
|
|
848
|
+
run = exec,
|
|
849
|
+
signing,
|
|
850
|
+
} = {}) {
|
|
851
|
+
const key = desktopTargetKey(platform, arch);
|
|
852
|
+
const format = desktopContainerFormat(platform);
|
|
853
|
+
if (!output) throw new Error('TN_DESKTOP_OUTPUT_MISSING: packageDesktopContainer needs an output path.');
|
|
854
|
+
assertFile(executable, 'desktop executable');
|
|
855
|
+
if (uiRenderer === 'web') {
|
|
856
|
+
assertDirectory(uiDirectory, 'built UI bundle');
|
|
857
|
+
if (!existsSync(join(uiDirectory, 'index.html'))) {
|
|
858
|
+
throw new Error(`TN_UI_BUNDLE_MISSING: ${uiDirectory} has no index.html, which is the page the overlay loads.`);
|
|
859
|
+
}
|
|
860
|
+
} else if (uiDirectory) {
|
|
861
|
+
throw new Error(`TN_UI_BUNDLE_UNEXPECTED: a UI bundle was staged for a game whose ui.renderer is '${uiRenderer}'.`);
|
|
862
|
+
}
|
|
863
|
+
const app = config?.app ?? {};
|
|
864
|
+
const loading = containerLoading(config);
|
|
865
|
+
const appName = app.name ?? 'ThreeNative';
|
|
866
|
+
const slug = containerSlug(appName);
|
|
867
|
+
const executableName2 = executableName ?? (platform === 'win32' ? `${slug}.exe` : slug);
|
|
868
|
+
const iconName = app.id ?? slug;
|
|
869
|
+
const rootFolder = containerRootFolder(platform, appName);
|
|
870
|
+
const archive = containerArchivePath(resolve(output), platform);
|
|
871
|
+
const paths = layout(platform, { appName, executableName: executableName2, iconName });
|
|
872
|
+
// The game is as required as the executable: a container without it launches a bare runtime that
|
|
873
|
+
// prints CLI usage. Check it with the other preconditions, before anything is staged.
|
|
874
|
+
assertFile(bundle, 'game bundle');
|
|
875
|
+
const staging = mkdtempSync(join(tmpdir(), 'threenative-container-'));
|
|
876
|
+
const resources = {};
|
|
877
|
+
const stage = (relativePath) => join(staging, rootFolder, relativePath);
|
|
878
|
+
const record = (relativePath) => {
|
|
879
|
+
resources[relativePath] = { sha256: sha256File(stage(relativePath)) };
|
|
880
|
+
};
|
|
881
|
+
try {
|
|
882
|
+
mkdirSync(dirname(stage(paths.executable)), { recursive: true });
|
|
883
|
+
copyFileSync(executable, stage(paths.executable));
|
|
884
|
+
if (platform !== 'win32') chmodSync(stage(paths.executable), 0o755);
|
|
885
|
+
|
|
886
|
+
// The game travels beside the executable, never appended to it: rcedit and the signing tools
|
|
887
|
+
// rewrite the binary, and anything past the end of the image does not survive that.
|
|
888
|
+
mkdirSync(dirname(stage(paths.bundle)), { recursive: true });
|
|
889
|
+
copyFileSync(bundle, stage(paths.bundle));
|
|
890
|
+
record(paths.bundle);
|
|
891
|
+
|
|
892
|
+
let ui = null;
|
|
893
|
+
if (uiRenderer === 'web') {
|
|
894
|
+
for (const file of listFiles(uiDirectory)) {
|
|
895
|
+
const destination = `${paths.ui}/${file.relative}`;
|
|
896
|
+
mkdirSync(dirname(stage(destination)), { recursive: true });
|
|
897
|
+
copyFileSync(file.absolute, stage(destination));
|
|
898
|
+
record(destination);
|
|
899
|
+
}
|
|
900
|
+
ui = { directory: paths.ui, entry: `${paths.ui}/index.html` };
|
|
901
|
+
if (!existsSync(stage(ui.entry))) throw new Error('TN_UI_BUNDLE_MISSING: no index.html staged for the web renderer.');
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
const bundled = [];
|
|
905
|
+
for (const dependency of dependencies) {
|
|
906
|
+
assertFile(dependency.source, `native dependency ${dependency.name}`);
|
|
907
|
+
const destination = dependencyRelativePath(platform, dependency.name);
|
|
908
|
+
mkdirSync(dirname(stage(destination)), { recursive: true });
|
|
909
|
+
copyFileSync(dependency.source, stage(destination));
|
|
910
|
+
if (platform !== 'win32') chmodSync(stage(destination), 0o755);
|
|
911
|
+
bundled.push({ name: dependency.name, path: destination });
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
let iconRecord;
|
|
915
|
+
if (icon !== undefined) {
|
|
916
|
+
assertFile(icon, 'app icon');
|
|
917
|
+
if (platform === 'win32') {
|
|
918
|
+
// Identity for Windows is the executable's PE resource section, so rcedit runs after the
|
|
919
|
+
// binary is staged; the game icon also travels as a sidecar for the manifest record.
|
|
920
|
+
const version = String(app.version ?? '0.0.0');
|
|
921
|
+
// rcedit parses --set-icon as a Windows icon and exits 1 on the authored PNG. macOS converts
|
|
922
|
+
// to .icns and Linux copies the PNG the .desktop entry wants; Windows needs an .ico, built
|
|
923
|
+
// outside the staging directory so it never becomes an unrecorded container resource.
|
|
924
|
+
let windowsIcon = icon;
|
|
925
|
+
let iconDirectory;
|
|
926
|
+
try {
|
|
927
|
+
if (!icon.toLowerCase().endsWith('.ico')) {
|
|
928
|
+
iconDirectory = mkdtempSync(join(tmpdir(), 'threenative-ico-'));
|
|
929
|
+
windowsIcon = join(iconDirectory, `${basename(icon, extname(icon))}.ico`);
|
|
930
|
+
pngToIco(icon, windowsIcon);
|
|
931
|
+
}
|
|
932
|
+
const rcedit = run('rcedit', [
|
|
933
|
+
stage(paths.executable),
|
|
934
|
+
'--set-icon', windowsIcon,
|
|
935
|
+
'--set-file-version', version,
|
|
936
|
+
'--set-product-version', version,
|
|
937
|
+
'--set-version-string', 'ProductName', appName,
|
|
938
|
+
'--set-version-string', 'FileDescription', appName,
|
|
939
|
+
], {});
|
|
940
|
+
if (rcedit.error) {
|
|
941
|
+
throw new Error(`TN_DESKTOP_RESOURCE_TOOL_MISSING: 'rcedit' is required to embed the executable icon and version (${rcedit.error.message}).`);
|
|
942
|
+
}
|
|
943
|
+
if (rcedit.status !== 0) throw new Error(`TN_DESKTOP_RESOURCE_FAILED: rcedit exited ${rcedit.status ?? 'unknown'}.`);
|
|
944
|
+
} finally {
|
|
945
|
+
if (iconDirectory) rmSync(iconDirectory, { force: true, recursive: true });
|
|
946
|
+
}
|
|
947
|
+
copyFileSync(icon, stage(paths.icon));
|
|
948
|
+
iconRecord = { path: paths.icon, sha256: sha256File(icon) };
|
|
949
|
+
record(paths.icon);
|
|
950
|
+
} else {
|
|
951
|
+
mkdirSync(dirname(stage(paths.icon)), { recursive: true });
|
|
952
|
+
buildIcon(icon, stage(paths.icon), { platform, run });
|
|
953
|
+
record(paths.icon);
|
|
954
|
+
iconRecord = { path: paths.icon, sha256: sha256File(icon) };
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
for (const [relativePath, content] of Object.entries(containerMetadata({ config, platform }))) {
|
|
959
|
+
const destination = relativePath;
|
|
960
|
+
mkdirSync(dirname(stage(destination)), { recursive: true });
|
|
961
|
+
writeFileSync(stage(destination), content);
|
|
962
|
+
record(destination);
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
// Sign nested macOS code first. The outer signature seals the manifest itself, so the
|
|
966
|
+
// executable's integrity is verified by codesign rather than a circular whole-file hash.
|
|
967
|
+
const signMac = platform === 'darwin' && signing !== undefined && signing !== null;
|
|
968
|
+
if (signMac) {
|
|
969
|
+
for (const dependency of bundled) {
|
|
970
|
+
signDesktopArtifact({ platform, run, signing, target: stage(dependency.path) });
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
const signedArtifact = signMac
|
|
974
|
+
? { scheme: 'codesign', signed: true }
|
|
975
|
+
: signing === undefined || signing === null
|
|
976
|
+
? { scheme: 'none', signed: false }
|
|
977
|
+
: signDesktopArtifact({
|
|
978
|
+
platform,
|
|
979
|
+
run,
|
|
980
|
+
signing,
|
|
981
|
+
target: stage(paths.executable),
|
|
982
|
+
});
|
|
983
|
+
|
|
984
|
+
// Windows signing and all nested signing have finished; these hashes now describe final bytes.
|
|
985
|
+
if (signMac) resources[paths.executable] = { signature: 'codesign' };
|
|
986
|
+
else record(paths.executable);
|
|
987
|
+
for (const dependency of bundled) {
|
|
988
|
+
record(dependency.path);
|
|
989
|
+
dependency.sha256 = resources[dependency.path].sha256;
|
|
990
|
+
}
|
|
991
|
+
const manifest = {
|
|
992
|
+
app: {
|
|
993
|
+
id: app.id ?? 'com.threenative.game',
|
|
994
|
+
name: appName,
|
|
995
|
+
version: app.version ?? '0.1.0',
|
|
996
|
+
build: app.build ?? 1,
|
|
997
|
+
...(iconRecord === undefined ? {} : { icon: iconRecord.path, iconSha256: iconRecord.sha256 }),
|
|
998
|
+
},
|
|
999
|
+
bundle: paths.bundle,
|
|
1000
|
+
dependencies: bundled,
|
|
1001
|
+
executable: paths.executable,
|
|
1002
|
+
format,
|
|
1003
|
+
loading,
|
|
1004
|
+
platform: key,
|
|
1005
|
+
prerequisites,
|
|
1006
|
+
resources,
|
|
1007
|
+
schemaVersion: CONTAINER_SCHEMA_VERSION,
|
|
1008
|
+
signed: signedArtifact.signed,
|
|
1009
|
+
...(signedArtifact.scheme === 'none' ? {} : { signingScheme: signedArtifact.scheme }),
|
|
1010
|
+
ui,
|
|
1011
|
+
};
|
|
1012
|
+
mkdirSync(dirname(stage(paths.manifest)), { recursive: true });
|
|
1013
|
+
writeFileSync(stage(paths.manifest), `${JSON.stringify(manifest, null, 2)}\n`);
|
|
1014
|
+
|
|
1015
|
+
if (signMac) {
|
|
1016
|
+
signDesktopArtifact({ platform, run, signing, target: join(staging, rootFolder) });
|
|
1017
|
+
resolveContainer(join(staging, rootFolder), { platform, run });
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
mkdirSync(dirname(archive), { recursive: true });
|
|
1021
|
+
// Keep the public destination untouched until the entire release transaction succeeds,
|
|
1022
|
+
// including notarization, stapling and re-archiving. The candidate lives on the destination
|
|
1023
|
+
// filesystem so the final rename never needs a cross-device copy.
|
|
1024
|
+
const candidateDirectory = mkdtempSync(join(dirname(archive), '.threenative-release-'));
|
|
1025
|
+
const candidateArchive = join(candidateDirectory, basename(archive));
|
|
1026
|
+
const installer = platform === 'win32' ? archive.replace(/\.zip$/iu, '-setup.exe') : undefined;
|
|
1027
|
+
let retainCandidates = false;
|
|
1028
|
+
try {
|
|
1029
|
+
archiveContainer({ output: candidateArchive, platform, rootFolder, run, staging });
|
|
1030
|
+
if (platform === 'darwin' && signing?.notarize) {
|
|
1031
|
+
notarizeArchive({ archive: candidateArchive, run, signing });
|
|
1032
|
+
signingTool(run, 'xcrun', ['stapler', 'staple', join(staging, rootFolder)], 'TN_DESKTOP_NOTARY_STAPLE');
|
|
1033
|
+
resolveContainer(join(staging, rootFolder), { platform, run });
|
|
1034
|
+
archiveContainer({ output: candidateArchive, platform, rootFolder, run, staging });
|
|
1035
|
+
}
|
|
1036
|
+
const artifacts = [[candidateArchive, archive]];
|
|
1037
|
+
if (installer) {
|
|
1038
|
+
const candidateInstaller = join(candidateDirectory, basename(installer));
|
|
1039
|
+
let installerIcon = icon;
|
|
1040
|
+
if (icon && !icon.toLowerCase().endsWith('.ico')) {
|
|
1041
|
+
installerIcon = join(candidateDirectory, 'installer.ico');
|
|
1042
|
+
pngToIco(icon, installerIcon);
|
|
1043
|
+
}
|
|
1044
|
+
buildWindowsInstaller({
|
|
1045
|
+
root: join(staging, rootFolder), output: candidateInstaller, manifest,
|
|
1046
|
+
files: [...Object.keys(resources), paths.manifest].sort(),
|
|
1047
|
+
icon: installerIcon, run, signing,
|
|
1048
|
+
});
|
|
1049
|
+
if (signing) signDesktopArtifact({ platform, run, signing, target: candidateInstaller });
|
|
1050
|
+
artifacts.push([candidateInstaller, installer]);
|
|
1051
|
+
}
|
|
1052
|
+
const backups = [];
|
|
1053
|
+
const published = [];
|
|
1054
|
+
try {
|
|
1055
|
+
for (const [, destination] of artifacts) {
|
|
1056
|
+
if (!existsSync(destination)) continue;
|
|
1057
|
+
if (!statSync(destination).isFile()) throw new Error('TN_DESKTOP_OUTPUT_NOT_FILE: ' + destination);
|
|
1058
|
+
const backup = join(candidateDirectory, 'previous-' + basename(destination));
|
|
1059
|
+
renameSync(destination, backup);
|
|
1060
|
+
backups.push([backup, destination]);
|
|
1061
|
+
}
|
|
1062
|
+
for (const [candidate, destination] of artifacts) {
|
|
1063
|
+
renameSync(candidate, destination);
|
|
1064
|
+
published.push(destination);
|
|
1065
|
+
}
|
|
1066
|
+
} catch (error) {
|
|
1067
|
+
try {
|
|
1068
|
+
for (const destination of published.reverse()) rmSync(destination);
|
|
1069
|
+
for (const [backup, destination] of backups.reverse()) renameSync(backup, destination);
|
|
1070
|
+
} catch (recoveryError) {
|
|
1071
|
+
retainCandidates = true;
|
|
1072
|
+
throw new AggregateError([error, recoveryError],
|
|
1073
|
+
'TN_DESKTOP_RELEASE_RECOVERY_REQUIRED: previous outputs retained at ' + candidateDirectory);
|
|
1074
|
+
}
|
|
1075
|
+
throw error;
|
|
1076
|
+
}
|
|
1077
|
+
} finally {
|
|
1078
|
+
if (!retainCandidates) rmSync(candidateDirectory, { force: true, recursive: true });
|
|
1079
|
+
}
|
|
1080
|
+
return { archive, ...(installer ? { installer } : {}), manifest, rootFolder, signed: signedArtifact.signed };
|
|
1081
|
+
} finally {
|
|
1082
|
+
rmSync(staging, { force: true, recursive: true });
|
|
1083
|
+
}
|
|
1084
|
+
}
|