@threenative/runtime-native 0.2.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 +21 -0
- package/NOTICE +10 -0
- package/android/README.md +155 -0
- package/android/app/build.gradle.kts +298 -0
- package/android/app/proguard-rules.pro +12 -0
- package/android/app/src/main/AndroidManifest.xml +45 -0
- package/android/app/src/main/java/com/mystral/engine/MystralActivity.java +88 -0
- package/android/app/src/main/res/values/strings.xml +5 -0
- package/android/app/src/main/res/values/themes.xml +8 -0
- package/android/build.gradle.kts +5 -0
- package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/android/gradle/wrapper/gradle-wrapper.properties +6 -0
- package/android/gradle.properties +13 -0
- package/android/gradlew +185 -0
- package/android/settings.gradle.kts +17 -0
- package/package.json +56 -0
- package/scripts/bundle.mjs +343 -0
- package/scripts/install-prebuilt.mjs +102 -0
- package/scripts/package-android.mjs +412 -0
- package/scripts/package-desktop.mjs +123 -0
- package/scripts/package-ios.mjs +385 -0
- package/scripts/production-evidence.mjs +286 -0
- package/scripts/profile-production.mjs +1525 -0
- package/scripts/verify-starter-desktop.mjs +103 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
5
|
+
import { dirname, join, resolve } from 'node:path';
|
|
6
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
7
|
+
|
|
8
|
+
const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
9
|
+
const packageVersion = JSON.parse(readFileSync(join(packageRoot, 'package.json'), 'utf8')).version;
|
|
10
|
+
const supported = new Set([
|
|
11
|
+
'darwin-arm64',
|
|
12
|
+
'linux-x64',
|
|
13
|
+
'win32-x64',
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
export function platformKey(platform = process.platform, arch = process.arch) {
|
|
17
|
+
const key = `${platform}-${arch}`;
|
|
18
|
+
if (!supported.has(key)) throw new Error(`Unsupported native runtime platform '${key}'.`);
|
|
19
|
+
return key;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function sha256(contents) {
|
|
23
|
+
return createHash('sha256').update(contents).digest('hex');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function verifyChecksum(contents, expected, key) {
|
|
27
|
+
const actual = sha256(contents);
|
|
28
|
+
if (!/^[a-f0-9]{64}$/u.test(expected) || actual !== expected) {
|
|
29
|
+
throw new Error(`Checksum verification failed for '${key}': expected ${expected}, received ${actual}.`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function releaseFromManifest(manifest, key) {
|
|
34
|
+
const release = manifest.artifacts?.[key];
|
|
35
|
+
if (!release?.url || !release?.sha256) {
|
|
36
|
+
throw new Error(`No prebuilt release asset is recorded for '${key}'.`);
|
|
37
|
+
}
|
|
38
|
+
const url = new URL(release.url);
|
|
39
|
+
if (url.protocol !== 'https:' && process.env.THREENATIVE_ALLOW_INSECURE_PREBUILT !== '1') {
|
|
40
|
+
throw new Error(`Prebuilt release URL for '${key}' must use HTTPS.`);
|
|
41
|
+
}
|
|
42
|
+
return release;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function readRelease(manifestPath, key) {
|
|
46
|
+
if (!existsSync(manifestPath)) {
|
|
47
|
+
throw new Error(`No prebuilt release manifest exists for '${key}'; this target remains OPEN.`);
|
|
48
|
+
}
|
|
49
|
+
return releaseFromManifest(JSON.parse(readFileSync(manifestPath, 'utf8')), key);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function releaseManifestUrl(version = packageVersion) {
|
|
53
|
+
return `https://github.com/jonit-dev/threenative/releases/download/runtime-native-v${encodeURIComponent(version)}/prebuilt-lock.json`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function fetchRelease(manifestUrl, key) {
|
|
57
|
+
const url = new URL(manifestUrl);
|
|
58
|
+
if (url.protocol !== 'https:' && process.env.THREENATIVE_ALLOW_INSECURE_PREBUILT !== '1') {
|
|
59
|
+
throw new Error(`Prebuilt release manifest for '${key}' must use HTTPS.`);
|
|
60
|
+
}
|
|
61
|
+
const response = await fetch(url);
|
|
62
|
+
if (!response.ok) {
|
|
63
|
+
throw new Error(`Prebuilt release manifest fetch failed for '${key}': HTTP ${response.status}.`);
|
|
64
|
+
}
|
|
65
|
+
return releaseFromManifest(await response.json(), key);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function downloadReleaseArtifact(key, options = {}) {
|
|
69
|
+
const manifestPath = options.manifestPath ?? process.env.THREENATIVE_PREBUILT_MANIFEST;
|
|
70
|
+
const release = manifestPath
|
|
71
|
+
? readRelease(resolve(manifestPath), key)
|
|
72
|
+
: await fetchRelease(options.manifestUrl ?? releaseManifestUrl(), key);
|
|
73
|
+
const response = await fetch(release.url);
|
|
74
|
+
if (!response.ok) throw new Error(`Prebuilt release fetch failed for '${key}': HTTP ${response.status}.`);
|
|
75
|
+
const contents = Buffer.from(await response.arrayBuffer());
|
|
76
|
+
verifyChecksum(contents, release.sha256, key);
|
|
77
|
+
return contents;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export async function installPrebuilt(options = {}) {
|
|
81
|
+
const key = platformKey(options.platform, options.arch);
|
|
82
|
+
const contents = await downloadReleaseArtifact(key, options);
|
|
83
|
+
const filename = process.platform === 'win32' ? 'threenative-runtime.exe' : 'threenative-runtime';
|
|
84
|
+
const output = resolve(options.output ?? join(packageRoot, 'prebuilt', key, filename));
|
|
85
|
+
mkdirSync(dirname(output), { recursive: true });
|
|
86
|
+
writeFileSync(output, contents);
|
|
87
|
+
if (process.platform !== 'win32') chmodSync(output, 0o755);
|
|
88
|
+
console.log(`Installed verified ThreeNative runtime for '${key}'.`);
|
|
89
|
+
return output;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
|
|
93
|
+
const sourceCheckout = existsSync(join(packageRoot, 'src')) && existsSync(join(packageRoot, 'CMakeLists.txt'));
|
|
94
|
+
if (sourceCheckout) {
|
|
95
|
+
console.log('ThreeNative runtime source checkout detected; prebuilt install is deferred to package testing.');
|
|
96
|
+
} else {
|
|
97
|
+
installPrebuilt().catch((error) => {
|
|
98
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
99
|
+
process.exitCode = 1;
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { spawnSync } from 'node:child_process';
|
|
4
|
+
import {
|
|
5
|
+
copyFileSync,
|
|
6
|
+
cpSync,
|
|
7
|
+
existsSync,
|
|
8
|
+
mkdirSync,
|
|
9
|
+
readFileSync,
|
|
10
|
+
readdirSync,
|
|
11
|
+
rmSync,
|
|
12
|
+
statSync,
|
|
13
|
+
writeFileSync,
|
|
14
|
+
} from 'node:fs';
|
|
15
|
+
import { dirname, join, posix, resolve } from 'node:path';
|
|
16
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
17
|
+
import { downloadReleaseArtifact, verifyChecksum } from './install-prebuilt.mjs';
|
|
18
|
+
|
|
19
|
+
const runtimeRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
|
20
|
+
export const GRADLE_WRAPPER_URL =
|
|
21
|
+
'https://raw.githubusercontent.com/gradle/gradle/v8.5.0/gradle/wrapper/gradle-wrapper.jar';
|
|
22
|
+
export const GRADLE_WRAPPER_SHA256 =
|
|
23
|
+
'd3b261c2820e9e3d8d639ed084900f11f4a86050a8f83342ade7b6bc9b0d2bdd';
|
|
24
|
+
|
|
25
|
+
export const ANDROID_PREBUILT_ASSETS = {
|
|
26
|
+
'android-arm64-v8a-runtime': 'jniLibs/arm64-v8a/libmystral-runtime.so',
|
|
27
|
+
'android-arm64-v8a-sdl3': 'jniLibs/arm64-v8a/libSDL3.so',
|
|
28
|
+
'android-sdl3-aar': 'SDL3-3.2.8.aar',
|
|
29
|
+
'android-x86_64-runtime': 'jniLibs/x86_64/libmystral-runtime.so',
|
|
30
|
+
'android-x86_64-sdl3': 'jniLibs/x86_64/libSDL3.so',
|
|
31
|
+
};
|
|
32
|
+
export const NATIVE_ORIENTATIONS = ['landscape', 'portrait', 'sensor'];
|
|
33
|
+
|
|
34
|
+
function androidPaths(root = runtimeRoot) {
|
|
35
|
+
return {
|
|
36
|
+
androidRoot: join(root, 'android'),
|
|
37
|
+
androidManifest: join(root, 'android', 'app', 'src', 'main', 'AndroidManifest.xml'),
|
|
38
|
+
androidStrings: join(root, 'android', 'app', 'src', 'main', 'res', 'values', 'strings.xml'),
|
|
39
|
+
androidTheme: join(root, 'android', 'app', 'src', 'main', 'res', 'values', 'themes.xml'),
|
|
40
|
+
androidGradle: join(root, 'android', 'app', 'build.gradle.kts'),
|
|
41
|
+
androidIcon: join(
|
|
42
|
+
root,
|
|
43
|
+
'android',
|
|
44
|
+
'app',
|
|
45
|
+
'src',
|
|
46
|
+
'main',
|
|
47
|
+
'res',
|
|
48
|
+
'mipmap-xxxhdpi',
|
|
49
|
+
'ic_launcher.png',
|
|
50
|
+
),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const DEFAULT_ANDROID_CONFIG = {
|
|
55
|
+
app: { id: 'com.threenative.game', name: 'ThreeNative', version: '0.1.0', build: 1 },
|
|
56
|
+
display: { orientation: 'landscape', fullscreen: true, keepScreenOn: false },
|
|
57
|
+
window: { title: 'ThreeNative', width: 1280, height: 720, resizable: true },
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
function configValue(value, orientation) {
|
|
61
|
+
const source = value && typeof value === 'object' ? value : {};
|
|
62
|
+
const app = source.app && typeof source.app === 'object' ? source.app : {};
|
|
63
|
+
const display = source.display && typeof source.display === 'object' ? source.display : {};
|
|
64
|
+
const window = source.window && typeof source.window === 'object' ? source.window : {};
|
|
65
|
+
return {
|
|
66
|
+
app: { ...DEFAULT_ANDROID_CONFIG.app, ...app },
|
|
67
|
+
display: {
|
|
68
|
+
...DEFAULT_ANDROID_CONFIG.display,
|
|
69
|
+
...display,
|
|
70
|
+
orientation: orientation ?? display.orientation ?? DEFAULT_ANDROID_CONFIG.display.orientation,
|
|
71
|
+
},
|
|
72
|
+
window: { ...DEFAULT_ANDROID_CONFIG.window, ...window },
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function readAndroidConfig(configPath) {
|
|
77
|
+
if (configPath === undefined) return configValue();
|
|
78
|
+
try {
|
|
79
|
+
return configValue(JSON.parse(readFileSync(configPath, 'utf8')));
|
|
80
|
+
} catch (error) {
|
|
81
|
+
throw new Error(`TN_CONFIG_FILE_INVALID: ${error instanceof Error ? error.message : String(error)}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function xmlEscape(value) {
|
|
86
|
+
return String(value).replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"').replaceAll("'", ''');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function setXmlAttribute(tag, name, value) {
|
|
90
|
+
const attribute = `android:${name}`;
|
|
91
|
+
const pattern = new RegExp(`\\s${attribute}="[^"]*"`, 'u');
|
|
92
|
+
if (value === undefined) return tag.replace(pattern, '');
|
|
93
|
+
const rendered = ` ${attribute}="${xmlEscape(value)}"`;
|
|
94
|
+
return pattern.test(tag) ? tag.replace(pattern, rendered) : tag.replace(/>$/u, `${rendered}>`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function upsertApplicationMetadata(source, name, value) {
|
|
98
|
+
const rendered = ` <meta-data android:name="${name}" android:value="${xmlEscape(value)}" />`;
|
|
99
|
+
const existing = new RegExp(`\\n[ \\t]*<meta-data\\b[^>]*android:name="${name}"[^>]*/>`, 'gu');
|
|
100
|
+
const withoutExisting = source.replace(existing, '');
|
|
101
|
+
return withoutExisting.replace(/\s*<\/application>/u, `\n${rendered}\n </application>`);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function orientationValue(value = 'landscape') {
|
|
105
|
+
if (typeof value === 'string' && NATIVE_ORIENTATIONS.includes(value)) return value;
|
|
106
|
+
throw new Error(
|
|
107
|
+
'TN_NATIVE_ORIENTATION_INVALID: display.orientation must be landscape, portrait, or sensor.',
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function renderAndroidManifest(source, orientation = 'landscape') {
|
|
112
|
+
const config = configValue(typeof orientation === 'string' ? undefined : orientation, typeof orientation === 'string' ? orientation : undefined);
|
|
113
|
+
const value = orientationValue(config.display.orientation);
|
|
114
|
+
const application = /<application\b[^>]*>/u.exec(source);
|
|
115
|
+
if (!application) throw new Error('TN_ANDROID_MANIFEST_APPLICATION_MISSING: no application was found.');
|
|
116
|
+
const activity = /<activity\b[^>]*>/u.exec(source);
|
|
117
|
+
if (!activity) throw new Error('TN_ANDROID_MANIFEST_ACTIVITY_MISSING: no activity was found.');
|
|
118
|
+
let rendered = source.replace(
|
|
119
|
+
application[0],
|
|
120
|
+
setXmlAttribute(application[0], 'icon', config.app.icon === undefined ? undefined : '@mipmap/ic_launcher'),
|
|
121
|
+
);
|
|
122
|
+
const renderedActivity = setXmlAttribute(
|
|
123
|
+
activity[0],
|
|
124
|
+
'screenOrientation',
|
|
125
|
+
value,
|
|
126
|
+
);
|
|
127
|
+
rendered = rendered.replace(activity[0], renderedActivity);
|
|
128
|
+
rendered = upsertApplicationMetadata(rendered, 'TN_KEEP_SCREEN_ON', String(config.display.keepScreenOn));
|
|
129
|
+
rendered = upsertApplicationMetadata(rendered, 'TN_WINDOW_TITLE', '@string/window_title');
|
|
130
|
+
return upsertApplicationMetadata(rendered, 'TN_FULLSCREEN', String(config.display.fullscreen));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function renderAndroidStrings(source, config) {
|
|
134
|
+
const value = configValue(config);
|
|
135
|
+
let rendered = source.replace(/(<string\s+name="app_name">)[\s\S]*?(<\/string>)/u, `$1${xmlEscape(value.app.name)}$2`);
|
|
136
|
+
if (!/<string\s+name="window_title">/u.test(rendered)) {
|
|
137
|
+
rendered = rendered.replace(/\s*<\/resources>/u, `\n <string name="window_title">${xmlEscape(value.window.title)}</string>\n</resources>`);
|
|
138
|
+
} else {
|
|
139
|
+
rendered = rendered.replace(/(<string\s+name="window_title">)[\s\S]*?(<\/string>)/u, `$1${xmlEscape(value.window.title)}$2`);
|
|
140
|
+
}
|
|
141
|
+
return rendered;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function renderAndroidTheme(source, config) {
|
|
145
|
+
const value = configValue(config);
|
|
146
|
+
const fullscreen = String(value.display.fullscreen);
|
|
147
|
+
const parent = value.display.fullscreen ? 'android:Theme.NoTitleBar.Fullscreen' : 'android:Theme.NoTitleBar';
|
|
148
|
+
let rendered = source.replace(/(parent=")[^"]*(")/u, `$1${parent}$2`);
|
|
149
|
+
const item = /<item\s+name="android:windowFullscreen">[\s\S]*?<\/item>/u;
|
|
150
|
+
if (item.test(rendered)) return rendered.replace(item, `<item name="android:windowFullscreen">${fullscreen}</item>`);
|
|
151
|
+
return rendered.replace(/\s*<\/style>/u, `\n <item name="android:windowFullscreen">${fullscreen}</item>\n </style>`);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function renderAndroidBuildGradle(source, config) {
|
|
155
|
+
const value = configValue(config);
|
|
156
|
+
let rendered = source.replace(/(namespace\s*=\s*")[^"]*(")/u, `$1${value.app.id}$2`);
|
|
157
|
+
rendered = rendered.replace(/(applicationId\s*=\s*")[^"]*(")/u, `$1${value.app.id}$2`);
|
|
158
|
+
rendered = rendered.replace(/(versionCode\s*=\s*)\d+/u, `$1${value.app.build}`);
|
|
159
|
+
return rendered.replace(/(versionName\s*=\s*")[^"]*(")/u, `$1${value.app.version}$2`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function restoreFile(file, original) {
|
|
163
|
+
if (original === undefined) rmSync(file, { force: true });
|
|
164
|
+
else writeFileSync(file, original);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function installAndroidFiles(config, root = runtimeRoot) {
|
|
168
|
+
const {
|
|
169
|
+
androidManifest,
|
|
170
|
+
androidStrings,
|
|
171
|
+
androidTheme,
|
|
172
|
+
androidGradle,
|
|
173
|
+
androidIcon,
|
|
174
|
+
} = androidPaths(root);
|
|
175
|
+
if (config.app.icon !== undefined && (!existsSync(config.app.icon) || !statSync(config.app.icon).isFile())) {
|
|
176
|
+
throw new Error(`TN_CONFIG_ICON_MISSING: app.icon does not exist: ${config.app.icon}`);
|
|
177
|
+
}
|
|
178
|
+
const originals = new Map([
|
|
179
|
+
[androidManifest, readFileSync(androidManifest)],
|
|
180
|
+
[androidStrings, readFileSync(androidStrings)],
|
|
181
|
+
[androidTheme, readFileSync(androidTheme)],
|
|
182
|
+
[androidGradle, readFileSync(androidGradle)],
|
|
183
|
+
]);
|
|
184
|
+
const iconOriginal = existsSync(androidIcon) ? readFileSync(androidIcon) : undefined;
|
|
185
|
+
writeFileSync(androidManifest, renderAndroidManifest(originals.get(androidManifest).toString('utf8'), config));
|
|
186
|
+
writeFileSync(androidStrings, renderAndroidStrings(originals.get(androidStrings).toString('utf8'), config));
|
|
187
|
+
writeFileSync(androidTheme, renderAndroidTheme(originals.get(androidTheme).toString('utf8'), config));
|
|
188
|
+
writeFileSync(androidGradle, renderAndroidBuildGradle(originals.get(androidGradle).toString('utf8'), config));
|
|
189
|
+
if (config.app.icon !== undefined) {
|
|
190
|
+
mkdirSync(dirname(androidIcon), { recursive: true });
|
|
191
|
+
copyFileSync(config.app.icon, androidIcon);
|
|
192
|
+
} else {
|
|
193
|
+
rmSync(androidIcon, { force: true });
|
|
194
|
+
}
|
|
195
|
+
return () => {
|
|
196
|
+
for (const [file, original] of originals) restoreFile(file, original);
|
|
197
|
+
restoreFile(androidIcon, iconOriginal);
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export async function prepareAndroidPrebuilts(options = {}) {
|
|
202
|
+
const downloads = await Promise.all(
|
|
203
|
+
Object.keys(ANDROID_PREBUILT_ASSETS).map(async (key) => [
|
|
204
|
+
key,
|
|
205
|
+
await downloadReleaseArtifact(key, options),
|
|
206
|
+
]),
|
|
207
|
+
);
|
|
208
|
+
const prebuiltRoot = resolve(options.outputRoot ?? join(runtimeRoot, 'android', 'prebuilt'));
|
|
209
|
+
for (const [key, contents] of downloads) {
|
|
210
|
+
const output = join(prebuiltRoot, ANDROID_PREBUILT_ASSETS[key]);
|
|
211
|
+
mkdirSync(dirname(output), { recursive: true });
|
|
212
|
+
writeFileSync(output, contents);
|
|
213
|
+
}
|
|
214
|
+
return prebuiltRoot;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export async function ensureGradleWrapper(options = {}) {
|
|
218
|
+
const output = resolve(
|
|
219
|
+
options.output ?? join(runtimeRoot, 'android', 'gradle', 'wrapper', 'gradle-wrapper.jar'),
|
|
220
|
+
);
|
|
221
|
+
const expected = options.sha256 ?? GRADLE_WRAPPER_SHA256;
|
|
222
|
+
if (existsSync(output)) {
|
|
223
|
+
verifyChecksum(readFileSync(output), expected, 'gradle-wrapper');
|
|
224
|
+
return output;
|
|
225
|
+
}
|
|
226
|
+
const url = new URL(options.url ?? GRADLE_WRAPPER_URL);
|
|
227
|
+
if (url.protocol !== 'https:' && process.env.THREENATIVE_ALLOW_INSECURE_PREBUILT !== '1') {
|
|
228
|
+
throw new Error('Gradle wrapper URL must use HTTPS.');
|
|
229
|
+
}
|
|
230
|
+
const response = await fetch(url);
|
|
231
|
+
if (!response.ok) throw new Error(`Gradle wrapper fetch failed: HTTP ${response.status}.`);
|
|
232
|
+
const contents = Buffer.from(await response.arrayBuffer());
|
|
233
|
+
verifyChecksum(contents, expected, 'gradle-wrapper');
|
|
234
|
+
mkdirSync(dirname(output), { recursive: true });
|
|
235
|
+
writeFileSync(output, contents);
|
|
236
|
+
return output;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function listFiles(directory, relative = '') {
|
|
240
|
+
const files = [];
|
|
241
|
+
for (const entry of readdirSync(join(directory, relative), { withFileTypes: true })) {
|
|
242
|
+
const path = relative ? posix.join(relative, entry.name) : entry.name;
|
|
243
|
+
if (entry.isDirectory()) files.push(...listFiles(directory, path));
|
|
244
|
+
else if (entry.isFile()) files.push(path);
|
|
245
|
+
else throw new Error(`Unsupported Android asset entry: ${join(directory, path)}`);
|
|
246
|
+
}
|
|
247
|
+
return files.sort();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function stageAndroidAssets(
|
|
251
|
+
assets,
|
|
252
|
+
destination = join(runtimeRoot, 'android', 'app', 'build', 'generated', 'threenative', 'assets', 'game'),
|
|
253
|
+
) {
|
|
254
|
+
rmSync(destination, { force: true, recursive: true });
|
|
255
|
+
mkdirSync(destination, { recursive: true });
|
|
256
|
+
if (!assets || !existsSync(assets)) return [];
|
|
257
|
+
if (!statSync(assets).isDirectory()) {
|
|
258
|
+
throw new Error(`Android assets path is not a directory: ${assets}`);
|
|
259
|
+
}
|
|
260
|
+
const files = listFiles(assets);
|
|
261
|
+
for (const file of files) {
|
|
262
|
+
const output = join(destination, file);
|
|
263
|
+
mkdirSync(dirname(output), { recursive: true });
|
|
264
|
+
cpSync(join(assets, file), output);
|
|
265
|
+
}
|
|
266
|
+
return files;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export async function packageAndroid(
|
|
270
|
+
bundle,
|
|
271
|
+
requestedOutput,
|
|
272
|
+
assets,
|
|
273
|
+
orientation = undefined,
|
|
274
|
+
config = undefined,
|
|
275
|
+
options = {},
|
|
276
|
+
) {
|
|
277
|
+
const packageRoot = resolve(options.runtimeRoot ?? runtimeRoot);
|
|
278
|
+
const { androidRoot } = androidPaths(packageRoot);
|
|
279
|
+
const declared = configValue(config, orientation);
|
|
280
|
+
orientationValue(declared.display.orientation);
|
|
281
|
+
const gradlew = join(androidRoot, process.platform === 'win32' ? 'gradlew.bat' : 'gradlew');
|
|
282
|
+
if (!existsSync(gradlew)) throw new Error(`Android Gradle wrapper is missing: ${gradlew}`);
|
|
283
|
+
if (!existsSync(bundle)) throw new Error(`Missing native bundle: ${bundle}`);
|
|
284
|
+
const ensureWrapper =
|
|
285
|
+
options.ensureGradleWrapper ??
|
|
286
|
+
(() => ensureGradleWrapper({ output: join(androidRoot, 'gradle', 'wrapper', 'gradle-wrapper.jar') }));
|
|
287
|
+
await ensureWrapper();
|
|
288
|
+
const sourceCheckout =
|
|
289
|
+
existsSync(join(packageRoot, 'CMakeLists.txt')) &&
|
|
290
|
+
existsSync(join(packageRoot, 'third_party', 'sdl3-android', 'SDL3-3.2.8.aar'));
|
|
291
|
+
if (!sourceCheckout) {
|
|
292
|
+
const preparePrebuilts =
|
|
293
|
+
options.prepareAndroidPrebuilts ??
|
|
294
|
+
(() => prepareAndroidPrebuilts({ outputRoot: join(packageRoot, 'android', 'prebuilt') }));
|
|
295
|
+
await preparePrebuilts();
|
|
296
|
+
}
|
|
297
|
+
const generatedAssets = join(
|
|
298
|
+
packageRoot,
|
|
299
|
+
'android',
|
|
300
|
+
'app',
|
|
301
|
+
'build',
|
|
302
|
+
'generated',
|
|
303
|
+
'threenative',
|
|
304
|
+
'assets',
|
|
305
|
+
);
|
|
306
|
+
rmSync(generatedAssets, { force: true, recursive: true });
|
|
307
|
+
const assetBundle = join(
|
|
308
|
+
generatedAssets,
|
|
309
|
+
'scripts',
|
|
310
|
+
'main.js',
|
|
311
|
+
);
|
|
312
|
+
mkdirSync(dirname(assetBundle), { recursive: true });
|
|
313
|
+
copyFileSync(bundle, assetBundle);
|
|
314
|
+
stageAndroidAssets(assets, join(generatedAssets, 'game'));
|
|
315
|
+
const restoreFiles = installAndroidFiles(declared, packageRoot);
|
|
316
|
+
try {
|
|
317
|
+
const command = process.platform === 'win32' ? gradlew : 'sh';
|
|
318
|
+
// Build variants the app already understands — `-PthreenativeJsEngine=v8`,
|
|
319
|
+
// `-PthreenativeVsync=false` — are only reachable if something can pass them through. Without
|
|
320
|
+
// this the properties exist in `build.gradle.kts` and no caller can ever set them.
|
|
321
|
+
const extraGradleArgs = (process.env.THREENATIVE_GRADLE_ARGS ?? '')
|
|
322
|
+
.split(' ')
|
|
323
|
+
.filter((entry) => entry.length > 0);
|
|
324
|
+
const baseArgs = ['assembleDebug', '-x', 'buildAndroidFirstProofBundle', ...extraGradleArgs];
|
|
325
|
+
const args = process.platform === 'win32' ? baseArgs : [gradlew, ...baseArgs];
|
|
326
|
+
const spawn = options.spawnSync ?? spawnSync;
|
|
327
|
+
const result = spawn(command, args, {
|
|
328
|
+
cwd: androidRoot,
|
|
329
|
+
encoding: 'utf8',
|
|
330
|
+
stdio: 'inherit',
|
|
331
|
+
});
|
|
332
|
+
if (result.error) throw result.error;
|
|
333
|
+
if (result.status !== 0)
|
|
334
|
+
throw new Error(`Gradle exited with code ${result.status ?? 'unknown'}.`);
|
|
335
|
+
const apk = join(
|
|
336
|
+
packageRoot,
|
|
337
|
+
'android',
|
|
338
|
+
'app',
|
|
339
|
+
'build',
|
|
340
|
+
'outputs',
|
|
341
|
+
'apk',
|
|
342
|
+
'debug',
|
|
343
|
+
'app-debug.apk',
|
|
344
|
+
);
|
|
345
|
+
if (!existsSync(apk)) throw new Error(`Gradle did not produce the expected APK: ${apk}`);
|
|
346
|
+
const output = requestedOutput ? resolve(requestedOutput) : apk;
|
|
347
|
+
if (output !== apk) {
|
|
348
|
+
mkdirSync(dirname(output), { recursive: true });
|
|
349
|
+
copyFileSync(apk, output);
|
|
350
|
+
}
|
|
351
|
+
console.log(`ThreeNative Android APK: ${output}`);
|
|
352
|
+
return output;
|
|
353
|
+
} finally {
|
|
354
|
+
restoreFiles();
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
|
|
359
|
+
const bundleIndex = process.argv.indexOf('--bundle');
|
|
360
|
+
const outputIndex = process.argv.indexOf('--output');
|
|
361
|
+
const assetsIndex = process.argv.indexOf('--assets');
|
|
362
|
+
const orientationIndex = process.argv.indexOf('--orientation');
|
|
363
|
+
const configIndex = process.argv.indexOf('--config');
|
|
364
|
+
if (
|
|
365
|
+
bundleIndex === -1 ||
|
|
366
|
+
!process.argv[bundleIndex + 1] ||
|
|
367
|
+
process.argv[bundleIndex + 1].startsWith('--')
|
|
368
|
+
) {
|
|
369
|
+
console.error(
|
|
370
|
+
'Usage: package-android.mjs --bundle FILE [--output FILE] [--assets DIR] [--orientation landscape|portrait|sensor] [--config FILE]',
|
|
371
|
+
);
|
|
372
|
+
process.exitCode = 1;
|
|
373
|
+
} else if (
|
|
374
|
+
assetsIndex !== -1 &&
|
|
375
|
+
(!process.argv[assetsIndex + 1] || process.argv[assetsIndex + 1].startsWith('--'))
|
|
376
|
+
) {
|
|
377
|
+
console.error('--assets requires a value.');
|
|
378
|
+
process.exitCode = 1;
|
|
379
|
+
} else if (
|
|
380
|
+
outputIndex !== -1 &&
|
|
381
|
+
(!process.argv[outputIndex + 1] || process.argv[outputIndex + 1].startsWith('--'))
|
|
382
|
+
) {
|
|
383
|
+
console.error('--output requires a value.');
|
|
384
|
+
process.exitCode = 1;
|
|
385
|
+
} else if (
|
|
386
|
+
orientationIndex !== -1 &&
|
|
387
|
+
(!process.argv[orientationIndex + 1] || process.argv[orientationIndex + 1].startsWith('--'))
|
|
388
|
+
) {
|
|
389
|
+
console.error('--orientation requires a value.');
|
|
390
|
+
process.exitCode = 1;
|
|
391
|
+
} else if (
|
|
392
|
+
configIndex !== -1 &&
|
|
393
|
+
(!process.argv[configIndex + 1] || process.argv[configIndex + 1].startsWith('--'))
|
|
394
|
+
) {
|
|
395
|
+
console.error('--config requires a value.');
|
|
396
|
+
process.exitCode = 1;
|
|
397
|
+
} else {
|
|
398
|
+
try {
|
|
399
|
+
const configPath = configIndex === -1 ? undefined : resolve(process.argv[configIndex + 1]);
|
|
400
|
+
await packageAndroid(
|
|
401
|
+
resolve(process.argv[bundleIndex + 1]),
|
|
402
|
+
outputIndex === -1 ? undefined : process.argv[outputIndex + 1],
|
|
403
|
+
assetsIndex === -1 ? undefined : resolve(process.argv[assetsIndex + 1]),
|
|
404
|
+
orientationIndex === -1 ? undefined : process.argv[orientationIndex + 1],
|
|
405
|
+
readAndroidConfig(configPath),
|
|
406
|
+
);
|
|
407
|
+
} catch (error) {
|
|
408
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
409
|
+
process.exitCode = 1;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { spawnSync } from 'node:child_process';
|
|
4
|
+
import {
|
|
5
|
+
chmodSync,
|
|
6
|
+
copyFileSync,
|
|
7
|
+
cpSync,
|
|
8
|
+
existsSync,
|
|
9
|
+
mkdirSync,
|
|
10
|
+
mkdtempSync,
|
|
11
|
+
readFileSync,
|
|
12
|
+
readdirSync,
|
|
13
|
+
rmSync,
|
|
14
|
+
statSync,
|
|
15
|
+
writeFileSync,
|
|
16
|
+
} from 'node:fs';
|
|
17
|
+
import { tmpdir } from 'node:os';
|
|
18
|
+
import { dirname, join, resolve } from 'node:path';
|
|
19
|
+
import { pathToFileURL } from 'node:url';
|
|
20
|
+
|
|
21
|
+
export const DEFAULT_DESKTOP_CONFIG = {
|
|
22
|
+
app: { id: 'com.threenative.game', name: 'ThreeNative', version: '0.1.0', build: 1 },
|
|
23
|
+
display: { orientation: 'landscape', fullscreen: true, keepScreenOn: false },
|
|
24
|
+
window: { title: 'ThreeNative', width: 1280, height: 720, resizable: true },
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function readConfig(configPath) {
|
|
28
|
+
if (configPath === undefined) return DEFAULT_DESKTOP_CONFIG;
|
|
29
|
+
try {
|
|
30
|
+
return JSON.parse(readFileSync(configPath, 'utf8'));
|
|
31
|
+
} catch (error) {
|
|
32
|
+
throw new Error(`TN_CONFIG_FILE_INVALID: ${error instanceof Error ? error.message : String(error)}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function parseArgs(args) {
|
|
37
|
+
const options = {};
|
|
38
|
+
for (let index = 0; index < args.length; index += 2) {
|
|
39
|
+
const flag = args[index];
|
|
40
|
+
const value = args[index + 1];
|
|
41
|
+
if (!['--assets', '--bundle', '--config', '--output', '--runtime'].includes(flag) || !value) {
|
|
42
|
+
throw new Error('Usage: package-desktop.mjs --bundle FILE --runtime FILE --output FILE [--assets DIR] [--config FILE]');
|
|
43
|
+
}
|
|
44
|
+
options[flag.slice(2)] = resolve(value);
|
|
45
|
+
}
|
|
46
|
+
for (const required of ['bundle', 'output', 'runtime']) {
|
|
47
|
+
if (!options[required]) throw new Error(`Missing --${required}.`);
|
|
48
|
+
}
|
|
49
|
+
return options;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function packageDesktop(options) {
|
|
53
|
+
const key = `${process.platform}-${process.arch}`;
|
|
54
|
+
if (!['linux-x64', 'linux-arm64', 'darwin-x64', 'darwin-arm64', 'win32-x64'].includes(key)) {
|
|
55
|
+
throw new Error(`Unsupported desktop platform '${key}'.`);
|
|
56
|
+
}
|
|
57
|
+
for (const [label, file] of [['native bundle', options.bundle], ['prebuilt runtime', options.runtime]]) {
|
|
58
|
+
if (!existsSync(file)) throw new Error(`Missing ${label} for '${key}': ${file}`);
|
|
59
|
+
}
|
|
60
|
+
const output = process.platform === 'win32' && !options.output.endsWith('.exe')
|
|
61
|
+
? `${options.output}.exe`
|
|
62
|
+
: options.output;
|
|
63
|
+
mkdirSync(dirname(output), { recursive: true });
|
|
64
|
+
const staging = mkdtempSync(join(tmpdir(), 'threenative-desktop-'));
|
|
65
|
+
try {
|
|
66
|
+
const stagedEntry = stageDesktopFiles(
|
|
67
|
+
options.bundle,
|
|
68
|
+
options.assets,
|
|
69
|
+
staging,
|
|
70
|
+
options.config === undefined ? undefined : readConfig(options.config),
|
|
71
|
+
);
|
|
72
|
+
const args = [
|
|
73
|
+
'compile',
|
|
74
|
+
stagedEntry,
|
|
75
|
+
'--root',
|
|
76
|
+
staging,
|
|
77
|
+
'--include',
|
|
78
|
+
staging,
|
|
79
|
+
'--out',
|
|
80
|
+
output,
|
|
81
|
+
];
|
|
82
|
+
const result = spawnSync(options.runtime, args, { encoding: 'utf8', stdio: 'inherit' });
|
|
83
|
+
if (result.error) throw result.error;
|
|
84
|
+
if (result.status !== 0)
|
|
85
|
+
throw new Error(`Runtime packager exited with code ${result.status ?? 'unknown'}.`);
|
|
86
|
+
} finally {
|
|
87
|
+
rmSync(staging, { force: true, recursive: true });
|
|
88
|
+
}
|
|
89
|
+
if (process.platform !== 'win32') chmodSync(output, 0o755);
|
|
90
|
+
console.log(`ThreeNative desktop artifact: ${output}`);
|
|
91
|
+
return output;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function stageDesktopFiles(bundle, assets, staging, config = undefined) {
|
|
95
|
+
mkdirSync(staging, { recursive: true });
|
|
96
|
+
if (assets && existsSync(assets)) {
|
|
97
|
+
if (!statSync(assets).isDirectory()) {
|
|
98
|
+
throw new Error(`Desktop assets path is not a directory: ${assets}`);
|
|
99
|
+
}
|
|
100
|
+
for (const entry of readdirSync(assets)) {
|
|
101
|
+
if (entry === '.threenative') {
|
|
102
|
+
throw new Error('TN_NATIVE_ASSET_RESERVED_PATH: public/.threenative is reserved.');
|
|
103
|
+
}
|
|
104
|
+
cpSync(join(assets, entry), join(staging, entry), { recursive: true });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const entry = join(staging, '.threenative', 'game.js');
|
|
108
|
+
mkdirSync(dirname(entry), { recursive: true });
|
|
109
|
+
copyFileSync(bundle, entry);
|
|
110
|
+
if (config !== undefined) {
|
|
111
|
+
writeFileSync(join(staging, '.threenative', 'config.json'), `${JSON.stringify(config, null, 2)}\n`);
|
|
112
|
+
}
|
|
113
|
+
return entry;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
|
|
117
|
+
try {
|
|
118
|
+
packageDesktop(parseArgs(process.argv.slice(2)));
|
|
119
|
+
} catch (error) {
|
|
120
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
121
|
+
process.exitCode = 1;
|
|
122
|
+
}
|
|
123
|
+
}
|