@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,13 @@
|
|
|
1
|
+
# Project-wide Gradle settings.
|
|
2
|
+
|
|
3
|
+
# JVM arguments for Gradle daemon
|
|
4
|
+
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
|
|
5
|
+
|
|
6
|
+
# Enable AndroidX
|
|
7
|
+
android.useAndroidX=true
|
|
8
|
+
|
|
9
|
+
# Enable build cache
|
|
10
|
+
org.gradle.caching=true
|
|
11
|
+
|
|
12
|
+
# NDK version - matches the SDK-installed NDK
|
|
13
|
+
android.ndkVersion=27.1.12297006
|
package/android/gradlew
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
# Copyright © 2015-2021 the original authors.
|
|
5
|
+
#
|
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
# you may not use this file except in compliance with the License.
|
|
8
|
+
# You may obtain a copy of the License at
|
|
9
|
+
#
|
|
10
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
#
|
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
# See the License for the specific language governing permissions and
|
|
16
|
+
# limitations under the License.
|
|
17
|
+
#
|
|
18
|
+
|
|
19
|
+
##############################################################################
|
|
20
|
+
#
|
|
21
|
+
# Gradle start up script for POSIX generated by Gradle.
|
|
22
|
+
#
|
|
23
|
+
##############################################################################
|
|
24
|
+
|
|
25
|
+
# Attempt to set APP_HOME
|
|
26
|
+
|
|
27
|
+
# Resolve links: $0 may be a link
|
|
28
|
+
app_path=$0
|
|
29
|
+
|
|
30
|
+
# Need this for daisy-chained symlinks.
|
|
31
|
+
while
|
|
32
|
+
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
|
|
33
|
+
[ -h "$app_path" ]
|
|
34
|
+
do
|
|
35
|
+
ls=$( ls -ld "$app_path" )
|
|
36
|
+
link=${ls#*' -> '}
|
|
37
|
+
case $link in #(
|
|
38
|
+
/*) app_path=$link ;; #(
|
|
39
|
+
*) app_path=$APP_HOME$link ;;
|
|
40
|
+
esac
|
|
41
|
+
done
|
|
42
|
+
|
|
43
|
+
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
|
|
44
|
+
|
|
45
|
+
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
|
46
|
+
MAX_FD=maximum
|
|
47
|
+
|
|
48
|
+
warn () {
|
|
49
|
+
echo "$*"
|
|
50
|
+
} >&2
|
|
51
|
+
|
|
52
|
+
die () {
|
|
53
|
+
echo
|
|
54
|
+
echo "$*"
|
|
55
|
+
echo
|
|
56
|
+
exit 1
|
|
57
|
+
} >&2
|
|
58
|
+
|
|
59
|
+
# OS specific support (must be 'true' or 'false').
|
|
60
|
+
cygwin=false
|
|
61
|
+
msys=false
|
|
62
|
+
darwin=false
|
|
63
|
+
nonstop=false
|
|
64
|
+
case "$( uname )" in #(
|
|
65
|
+
CYGWIN* ) cygwin=true ;; #(
|
|
66
|
+
Darwin* ) darwin=true ;; #(
|
|
67
|
+
MSYS* | MINGW* ) msys=true ;; #(
|
|
68
|
+
NONSTOP* ) nonstop=true ;;
|
|
69
|
+
esac
|
|
70
|
+
|
|
71
|
+
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
|
72
|
+
|
|
73
|
+
# Determine the Java command to use to start the JVM.
|
|
74
|
+
if [ -n "$JAVA_HOME" ] ; then
|
|
75
|
+
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
|
76
|
+
# IBM's JDK on AIX uses strange locations for the executables
|
|
77
|
+
JAVACMD=$JAVA_HOME/jre/sh/java
|
|
78
|
+
else
|
|
79
|
+
JAVACMD=$JAVA_HOME/bin/java
|
|
80
|
+
fi
|
|
81
|
+
if [ ! -x "$JAVACMD" ] ; then
|
|
82
|
+
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
|
83
|
+
|
|
84
|
+
Please set the JAVA_HOME variable in your environment to match the
|
|
85
|
+
location of your Java installation."
|
|
86
|
+
fi
|
|
87
|
+
else
|
|
88
|
+
JAVACMD=java
|
|
89
|
+
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
|
90
|
+
|
|
91
|
+
Please set the JAVA_HOME variable in your environment to match the
|
|
92
|
+
location of your Java installation."
|
|
93
|
+
fi
|
|
94
|
+
|
|
95
|
+
# Increase the maximum file descriptors if we can.
|
|
96
|
+
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
|
97
|
+
case $MAX_FD in #(
|
|
98
|
+
max*)
|
|
99
|
+
MAX_FD=$( ulimit -H -n ) ||
|
|
100
|
+
warn "Could not query maximum file descriptor limit"
|
|
101
|
+
esac
|
|
102
|
+
case $MAX_FD in #(
|
|
103
|
+
'' | soft) :;; #(
|
|
104
|
+
*)
|
|
105
|
+
ulimit -n "$MAX_FD" ||
|
|
106
|
+
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
|
107
|
+
esac
|
|
108
|
+
fi
|
|
109
|
+
|
|
110
|
+
# Collect all arguments for the java command, stacking in reverse order:
|
|
111
|
+
# * args from the command line
|
|
112
|
+
# * the main class name
|
|
113
|
+
# * -classpath
|
|
114
|
+
# * -D...appname settings
|
|
115
|
+
# * --module-path (only if needed)
|
|
116
|
+
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
|
|
117
|
+
|
|
118
|
+
# For Cygwin or MSYS, switch paths to Windows format before running java
|
|
119
|
+
if "$cygwin" || "$msys" ; then
|
|
120
|
+
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
|
|
121
|
+
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
|
|
122
|
+
|
|
123
|
+
JAVACMD=$( cygpath --unix "$JAVACMD" )
|
|
124
|
+
|
|
125
|
+
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
|
126
|
+
for arg do
|
|
127
|
+
if
|
|
128
|
+
case $arg in #(
|
|
129
|
+
-*) false ;; # don't mess with options #(
|
|
130
|
+
/?*) t=${arg#)}; t=/${t%%/*} # looks like a POSIX filepath
|
|
131
|
+
[ -e "$t" ] ;; #(
|
|
132
|
+
*) false ;;
|
|
133
|
+
esac
|
|
134
|
+
then
|
|
135
|
+
arg=$( cygpath --path --ignore --mixed "$arg" )
|
|
136
|
+
fi
|
|
137
|
+
# Roll the args list around exactly as many times as the number of
|
|
138
|
+
# temporary args needed so that the command of args rolls back to the
|
|
139
|
+
# temporary storage.
|
|
140
|
+
shift # remove old arg
|
|
141
|
+
set -- "$@" "$arg" # push replacement arg
|
|
142
|
+
done
|
|
143
|
+
fi
|
|
144
|
+
|
|
145
|
+
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
|
146
|
+
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
|
147
|
+
|
|
148
|
+
# Collect all arguments for the java command;
|
|
149
|
+
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
|
|
150
|
+
# temporary storage but also just white space, so put them in double quotes
|
|
151
|
+
# to preserve white space, and allow for variable interpolation.
|
|
152
|
+
# * $APP_HOME is a path, so wrap it in double quotes.
|
|
153
|
+
|
|
154
|
+
set -- \
|
|
155
|
+
-classpath "$CLASSPATH" \
|
|
156
|
+
org.gradle.wrapper.GradleWrapperMain \
|
|
157
|
+
"$@"
|
|
158
|
+
|
|
159
|
+
# Stop when "xargs" is not available.
|
|
160
|
+
if ! command -v xargs >/dev/null 2>&1
|
|
161
|
+
then
|
|
162
|
+
die "xargs is not available"
|
|
163
|
+
fi
|
|
164
|
+
|
|
165
|
+
# Use "xargs" to parse quoted args.
|
|
166
|
+
#
|
|
167
|
+
# With -n://github, every N argumen ts (default 1) are passed to the command at a time.
|
|
168
|
+
# We assume args contains at least 1 argument which is the gradle command itself
|
|
169
|
+
# like "build".
|
|
170
|
+
#
|
|
171
|
+
# Read arguments from "-D" options to "xargs":
|
|
172
|
+
#
|
|
173
|
+
# xargs -t -r --> only run if there are arguments
|
|
174
|
+
# -n 1 --> invoke 1 argument at a time
|
|
175
|
+
# $@ >/dev/null --> redirect output
|
|
176
|
+
#
|
|
177
|
+
|
|
178
|
+
eval "set -- $(
|
|
179
|
+
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
|
|
180
|
+
xargs -n1 |
|
|
181
|
+
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
|
|
182
|
+
tr '\n' ' '
|
|
183
|
+
)" '"$@"'
|
|
184
|
+
|
|
185
|
+
exec "$JAVACMD" "$@"
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
pluginManagement {
|
|
2
|
+
repositories {
|
|
3
|
+
google()
|
|
4
|
+
mavenCentral()
|
|
5
|
+
gradlePluginPortal()
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
dependencyResolutionManagement {
|
|
9
|
+
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
|
|
10
|
+
repositories {
|
|
11
|
+
google()
|
|
12
|
+
mavenCentral()
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
rootProject.name = "MystralNative"
|
|
17
|
+
include(":app")
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@threenative/runtime-native",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "ThreeNative-owned native host for desktop, Android, and iOS",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"files": [
|
|
11
|
+
"android/app/build.gradle.kts",
|
|
12
|
+
"android/app/proguard-rules.pro",
|
|
13
|
+
"android/app/src/main/AndroidManifest.xml",
|
|
14
|
+
"android/app/src/main/java",
|
|
15
|
+
"android/app/src/main/res",
|
|
16
|
+
"android/build.gradle.kts",
|
|
17
|
+
"android/gradle.properties",
|
|
18
|
+
"android/gradle/wrapper/gradle-wrapper.jar",
|
|
19
|
+
"android/gradle/wrapper/gradle-wrapper.properties",
|
|
20
|
+
"android/gradlew",
|
|
21
|
+
"android/README.md",
|
|
22
|
+
"android/settings.gradle.kts",
|
|
23
|
+
"scripts/bundle.mjs",
|
|
24
|
+
"scripts/install-prebuilt.mjs",
|
|
25
|
+
"scripts/package-android.mjs",
|
|
26
|
+
"scripts/package-desktop.mjs",
|
|
27
|
+
"scripts/package-ios.mjs",
|
|
28
|
+
"scripts/production-evidence.mjs",
|
|
29
|
+
"scripts/profile-production.mjs",
|
|
30
|
+
"scripts/verify-starter-desktop.mjs",
|
|
31
|
+
"LICENSE",
|
|
32
|
+
"NOTICE"
|
|
33
|
+
],
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"esbuild": "0.27.0",
|
|
36
|
+
"three": "0.185.1",
|
|
37
|
+
"vitest": "4.1.10"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"pngjs": "7.0.0"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"deps:download": "node scripts/download-deps.mjs",
|
|
44
|
+
"native:build": "node scripts/download-deps.mjs && node scripts/native-build.mjs",
|
|
45
|
+
"native:physics:cross": "node scripts/build-native-physics.mjs",
|
|
46
|
+
"native:physics:measure": "node scripts/measure-physics-divergence.mjs",
|
|
47
|
+
"native:physics:parity": "pnpm --dir ../.. exec vitest run packages/physics/__tests__/parity.spec.ts && cargo test --manifest-path native/physics/Cargo.toml --test parity -- --nocapture",
|
|
48
|
+
"native:physics:parity:device": "node scripts/verify-android-physics-parity.mjs",
|
|
49
|
+
"native:verify:desktop": "node scripts/verify-desktop-core.mjs && node scripts/verify-desktop-physics.mjs",
|
|
50
|
+
"native:verify:android:multitouch": "pnpm --dir ../playtest build && node scripts/verify-android-multitouch.mjs",
|
|
51
|
+
"profile:production": "node scripts/profile-production.mjs",
|
|
52
|
+
"parity": "node conformance/run-conformance.mjs",
|
|
53
|
+
"install": "node scripts/install-prebuilt.mjs",
|
|
54
|
+
"test": "vitest run --config vitest.config.ts && pnpm native:physics:parity"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Bundle Script
|
|
5
|
+
*
|
|
6
|
+
* Bundles JavaScript code and assets into a format that can be embedded
|
|
7
|
+
* in the native binary (similar to Deno compile / pkg).
|
|
8
|
+
*
|
|
9
|
+
* Options:
|
|
10
|
+
* 1. Generate C header with embedded data (like xxd)
|
|
11
|
+
* 2. Create a virtual filesystem archive
|
|
12
|
+
* 3. Use CMake's file(EMBED) feature
|
|
13
|
+
*
|
|
14
|
+
* Usage:
|
|
15
|
+
* node scripts/bundle.mjs --entry game.js --output dist/bundle
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { readFileSync, writeFileSync, writeSync, readdirSync, statSync, mkdirSync, existsSync } from 'fs';
|
|
19
|
+
import { createRequire } from 'module';
|
|
20
|
+
import { join, dirname, basename, resolve } from 'path';
|
|
21
|
+
import { fileURLToPath, pathToFileURL } from 'url';
|
|
22
|
+
|
|
23
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Convert a buffer to a C array string
|
|
27
|
+
*/
|
|
28
|
+
function bufferToCArray(buffer, varName) {
|
|
29
|
+
const bytes = Array.from(buffer);
|
|
30
|
+
const lines = [];
|
|
31
|
+
|
|
32
|
+
lines.push(`// Auto-generated - do not edit`);
|
|
33
|
+
lines.push(`// Size: ${buffer.length} bytes`);
|
|
34
|
+
lines.push(``);
|
|
35
|
+
lines.push(`static const unsigned char ${varName}[] = {`);
|
|
36
|
+
|
|
37
|
+
// Output 16 bytes per line
|
|
38
|
+
for (let i = 0; i < bytes.length; i += 16) {
|
|
39
|
+
const slice = bytes.slice(i, Math.min(i + 16, bytes.length));
|
|
40
|
+
const hex = slice.map(b => `0x${b.toString(16).padStart(2, '0')}`).join(', ');
|
|
41
|
+
lines.push(` ${hex}${i + 16 < bytes.length ? ',' : ''}`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
lines.push(`};`);
|
|
45
|
+
lines.push(``);
|
|
46
|
+
lines.push(`static const unsigned int ${varName}_len = ${buffer.length};`);
|
|
47
|
+
|
|
48
|
+
return lines.join('\n');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Bundle a single file
|
|
53
|
+
*/
|
|
54
|
+
function bundleFile(inputPath, varName) {
|
|
55
|
+
console.log(`Bundling: ${inputPath}`);
|
|
56
|
+
const buffer = readFileSync(inputPath);
|
|
57
|
+
return bufferToCArray(buffer, varName);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Bundle a directory recursively
|
|
62
|
+
*/
|
|
63
|
+
function bundleDirectory(dirPath, prefix = '') {
|
|
64
|
+
const files = [];
|
|
65
|
+
|
|
66
|
+
for (const entry of readdirSync(dirPath)) {
|
|
67
|
+
const fullPath = join(dirPath, entry);
|
|
68
|
+
const stat = statSync(fullPath);
|
|
69
|
+
|
|
70
|
+
if (stat.isDirectory()) {
|
|
71
|
+
files.push(...bundleDirectory(fullPath, prefix + entry + '_'));
|
|
72
|
+
} else {
|
|
73
|
+
const varName = prefix + entry.replace(/[^a-zA-Z0-9]/g, '_');
|
|
74
|
+
files.push({
|
|
75
|
+
path: fullPath,
|
|
76
|
+
varName,
|
|
77
|
+
relativePath: (prefix + entry).replace(/_/g, '/'),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return files;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Generate virtual filesystem header
|
|
87
|
+
*/
|
|
88
|
+
function generateVFSHeader(files, outputPath) {
|
|
89
|
+
const lines = [];
|
|
90
|
+
|
|
91
|
+
lines.push(`// Auto-generated virtual filesystem`);
|
|
92
|
+
lines.push(`// Do not edit`);
|
|
93
|
+
lines.push(``);
|
|
94
|
+
lines.push(`#pragma once`);
|
|
95
|
+
lines.push(``);
|
|
96
|
+
lines.push(`#include <cstddef>`);
|
|
97
|
+
lines.push(`#include <cstring>`);
|
|
98
|
+
lines.push(``);
|
|
99
|
+
lines.push(`namespace mystral {`);
|
|
100
|
+
lines.push(`namespace vfs {`);
|
|
101
|
+
lines.push(``);
|
|
102
|
+
|
|
103
|
+
// Embed each file
|
|
104
|
+
for (const file of files) {
|
|
105
|
+
const buffer = readFileSync(file.path);
|
|
106
|
+
lines.push(bufferToCArray(buffer, file.varName));
|
|
107
|
+
lines.push(``);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// File table
|
|
111
|
+
lines.push(`struct EmbeddedFile {`);
|
|
112
|
+
lines.push(` const char* path;`);
|
|
113
|
+
lines.push(` const unsigned char* data;`);
|
|
114
|
+
lines.push(` unsigned int size;`);
|
|
115
|
+
lines.push(`};`);
|
|
116
|
+
lines.push(``);
|
|
117
|
+
lines.push(`static const EmbeddedFile files[] = {`);
|
|
118
|
+
|
|
119
|
+
for (const file of files) {
|
|
120
|
+
lines.push(` { "${file.relativePath}", ${file.varName}, ${file.varName}_len },`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
lines.push(` { nullptr, nullptr, 0 } // Sentinel`);
|
|
124
|
+
lines.push(`};`);
|
|
125
|
+
lines.push(``);
|
|
126
|
+
|
|
127
|
+
// Lookup function
|
|
128
|
+
lines.push(`inline const EmbeddedFile* findFile(const char* path) {`);
|
|
129
|
+
lines.push(` for (const EmbeddedFile* f = files; f->path != nullptr; ++f) {`);
|
|
130
|
+
lines.push(` if (strcmp(f->path, path) == 0) {`);
|
|
131
|
+
lines.push(` return f;`);
|
|
132
|
+
lines.push(` }`);
|
|
133
|
+
lines.push(` }`);
|
|
134
|
+
lines.push(` return nullptr;`);
|
|
135
|
+
lines.push(`}`);
|
|
136
|
+
lines.push(``);
|
|
137
|
+
|
|
138
|
+
lines.push(`} // namespace vfs`);
|
|
139
|
+
lines.push(`} // namespace mystral`);
|
|
140
|
+
|
|
141
|
+
const outputDir = dirname(outputPath);
|
|
142
|
+
if (!existsSync(outputDir)) {
|
|
143
|
+
mkdirSync(outputDir, { recursive: true });
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
writeFileSync(outputPath, lines.join('\n'));
|
|
147
|
+
console.log(`Generated: ${outputPath}`);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Main
|
|
152
|
+
*/
|
|
153
|
+
async function bundleProject(project, entryPoint, outputPath, target) {
|
|
154
|
+
const absoluteProject = resolve(project);
|
|
155
|
+
const absoluteEntry = resolve(absoluteProject, entryPoint);
|
|
156
|
+
const absoluteOutput = resolve(outputPath);
|
|
157
|
+
const require = createRequire(join(absoluteProject, 'package.json'));
|
|
158
|
+
let viteEntry;
|
|
159
|
+
try {
|
|
160
|
+
viteEntry = require.resolve('vite');
|
|
161
|
+
} catch {
|
|
162
|
+
throw new Error(`Cannot bundle '${absoluteProject}': its pinned Vite dependency is missing.`);
|
|
163
|
+
}
|
|
164
|
+
const { build } = await import(pathToFileURL(viteEntry).href);
|
|
165
|
+
const nativePrelude = `
|
|
166
|
+
const nativeElements = new Map();
|
|
167
|
+
const nativeGetElementById = document.getElementById.bind(document);
|
|
168
|
+
const nativeQuerySelector = document.querySelector.bind(document);
|
|
169
|
+
const nativeCreateElement = document.createElement.bind(document);
|
|
170
|
+
document.getElementById = (id) => nativeElements.get(id) ?? nativeGetElementById(id);
|
|
171
|
+
document.querySelector = (selector) =>
|
|
172
|
+
selector.startsWith("#") ? nativeElements.get(selector.slice(1)) ?? null : nativeQuerySelector(selector);
|
|
173
|
+
document.createElement = (tag) =>
|
|
174
|
+
String(tag).toLowerCase() === "canvas" ? globalThis.canvas : nativeCreateElement(tag);
|
|
175
|
+
for (const id of ["app", "root"]) {
|
|
176
|
+
if (nativeGetElementById(id) == null) {
|
|
177
|
+
const element = document.createElement("div");
|
|
178
|
+
element.id = id;
|
|
179
|
+
element.nodeType = 1;
|
|
180
|
+
element.ownerDocument = document;
|
|
181
|
+
element.append = (child) => element.appendChild(child);
|
|
182
|
+
nativeElements.set(id, element);
|
|
183
|
+
document.body?.append?.(element);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (typeof globalThis.requestAnimationFrame === "function") {
|
|
187
|
+
const nativeRequestFrame = globalThis.requestAnimationFrame.bind(globalThis);
|
|
188
|
+
let nativeFrames = 0;
|
|
189
|
+
globalThis.requestAnimationFrame = (callback) => nativeRequestFrame((time) => {
|
|
190
|
+
const result = callback(time);
|
|
191
|
+
nativeFrames += 1;
|
|
192
|
+
if (nativeFrames === 1) {
|
|
193
|
+
console.info("TN_NATIVE_SMOKE_READY:webgpu");
|
|
194
|
+
console.info("TN_NATIVE_SMOKE_FIRST_FRAME");
|
|
195
|
+
}
|
|
196
|
+
if (nativeFrames === 300) console.info("TN_NATIVE_SMOKE_300_FRAMES:300");
|
|
197
|
+
return result;
|
|
198
|
+
});
|
|
199
|
+
}`;
|
|
200
|
+
const virtualEntry = 'virtual:threenative-native-entry';
|
|
201
|
+
const resolvedVirtualEntry = `\0${virtualEntry}`;
|
|
202
|
+
const nativeEntryPlugin = {
|
|
203
|
+
name: 'threenative-native-entry',
|
|
204
|
+
enforce: 'pre',
|
|
205
|
+
resolveId(id) {
|
|
206
|
+
return id === virtualEntry ? resolvedVirtualEntry : null;
|
|
207
|
+
},
|
|
208
|
+
load(id) {
|
|
209
|
+
if (id !== resolvedVirtualEntry) return null;
|
|
210
|
+
return `import * as gameModule from ${JSON.stringify(absoluteEntry)};
|
|
211
|
+
const game = gameModule.default;
|
|
212
|
+
void game.start().catch((error) => console.error(
|
|
213
|
+
\`TN_NATIVE_START_FAILED:\${error instanceof Error ? error.message : String(error)}\`,
|
|
214
|
+
));`;
|
|
215
|
+
},
|
|
216
|
+
moduleParsed(info) {
|
|
217
|
+
if (resolve(info.id) === absoluteEntry && !info.exports.includes('default')) {
|
|
218
|
+
this.error(`TN_NATIVE_ENTRY_NO_DEFAULT: ${entryPoint} must default-export the game.`);
|
|
219
|
+
}
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
mkdirSync(dirname(absoluteOutput), { recursive: true });
|
|
223
|
+
await build({
|
|
224
|
+
root: absoluteProject,
|
|
225
|
+
plugins: [nativeEntryPlugin],
|
|
226
|
+
resolve: target === 'desktop' ? undefined : { conditions: ['threenative-native'] },
|
|
227
|
+
configFile: existsSync(join(absoluteProject, 'vite.config.ts'))
|
|
228
|
+
? join(absoluteProject, 'vite.config.ts')
|
|
229
|
+
: undefined,
|
|
230
|
+
build: {
|
|
231
|
+
emptyOutDir: false,
|
|
232
|
+
lib: {
|
|
233
|
+
entry: absoluteEntry,
|
|
234
|
+
fileName: () => basename(absoluteOutput),
|
|
235
|
+
formats: ['es'],
|
|
236
|
+
},
|
|
237
|
+
minify: false,
|
|
238
|
+
outDir: dirname(absoluteOutput),
|
|
239
|
+
rollupOptions: {
|
|
240
|
+
input: virtualEntry,
|
|
241
|
+
output: {
|
|
242
|
+
banner: `/* TN_NATIVE_BUNDLE_SCOPE */\n(() => {\n${nativePrelude}`,
|
|
243
|
+
codeSplitting: false,
|
|
244
|
+
footer: '})();',
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
target: 'es2022',
|
|
248
|
+
},
|
|
249
|
+
});
|
|
250
|
+
const source = readFileSync(absoluteOutput, 'utf8');
|
|
251
|
+
if (/^\s*import\s+/m.test(source) || /\bimport\s*\(/.test(source)) {
|
|
252
|
+
throw new Error(`Native bundle '${absoluteOutput}' contains a runtime import.`);
|
|
253
|
+
}
|
|
254
|
+
console.log(`ThreeNative native bundle: ${absoluteOutput}`);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
async function main() {
|
|
258
|
+
const args = process.argv.slice(2);
|
|
259
|
+
|
|
260
|
+
// Parse arguments
|
|
261
|
+
let entryPoint = null;
|
|
262
|
+
let assetsDir = null;
|
|
263
|
+
let outputDir = 'dist';
|
|
264
|
+
let project = null;
|
|
265
|
+
let target = null;
|
|
266
|
+
|
|
267
|
+
for (let i = 0; i < args.length; i++) {
|
|
268
|
+
if (args[i] === '--entry' && args[i + 1]) {
|
|
269
|
+
entryPoint = args[++i];
|
|
270
|
+
} else if (args[i] === '--assets' && args[i + 1]) {
|
|
271
|
+
assetsDir = args[++i];
|
|
272
|
+
} else if (args[i] === '--output' && args[i + 1]) {
|
|
273
|
+
outputDir = args[++i];
|
|
274
|
+
} else if (args[i] === '--project' && args[i + 1]) {
|
|
275
|
+
project = args[++i];
|
|
276
|
+
} else if (args[i] === '--target' && args[i + 1]) {
|
|
277
|
+
target = args[++i];
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (project) {
|
|
282
|
+
if (!entryPoint) throw new Error('--project requires --entry.');
|
|
283
|
+
if (!['android', 'desktop', 'ios'].includes(target)) {
|
|
284
|
+
throw new Error('--project requires --target android|desktop|ios.');
|
|
285
|
+
}
|
|
286
|
+
await bundleProject(project, entryPoint, outputDir, target);
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (!entryPoint && !assetsDir) {
|
|
291
|
+
console.log('Mystral Bundle Tool');
|
|
292
|
+
console.log('');
|
|
293
|
+
console.log('Usage:');
|
|
294
|
+
console.log(' node scripts/bundle.mjs --entry game.js [--assets assets/] [--output dist/]');
|
|
295
|
+
console.log('');
|
|
296
|
+
console.log('Options:');
|
|
297
|
+
console.log(' --entry Entry point JavaScript file');
|
|
298
|
+
console.log(' --assets Directory containing assets to bundle');
|
|
299
|
+
console.log(' --output Output directory (default: dist/)');
|
|
300
|
+
console.log('');
|
|
301
|
+
console.log('Generates a C header file with embedded data for use in native builds.');
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const files = [];
|
|
306
|
+
|
|
307
|
+
// Bundle entry point
|
|
308
|
+
if (entryPoint) {
|
|
309
|
+
files.push({
|
|
310
|
+
path: entryPoint,
|
|
311
|
+
varName: 'entry_js',
|
|
312
|
+
relativePath: 'entry.js',
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Bundle assets directory
|
|
317
|
+
if (assetsDir) {
|
|
318
|
+
files.push(...bundleDirectory(assetsDir, 'asset_'));
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Generate header
|
|
322
|
+
const headerPath = join(outputDir, 'embedded_assets.h');
|
|
323
|
+
generateVFSHeader(files, headerPath);
|
|
324
|
+
|
|
325
|
+
console.log('');
|
|
326
|
+
console.log('Bundle complete!');
|
|
327
|
+
console.log(`Files bundled: ${files.length}`);
|
|
328
|
+
console.log(`Output: ${headerPath}`);
|
|
329
|
+
console.log('');
|
|
330
|
+
console.log('Include in your build:');
|
|
331
|
+
console.log(` #include "${headerPath}"`);
|
|
332
|
+
console.log('');
|
|
333
|
+
console.log('Access files:');
|
|
334
|
+
console.log(' const auto* file = mystral::vfs::findFile("entry.js");');
|
|
335
|
+
console.log(' if (file) {');
|
|
336
|
+
console.log(' // file->data, file->size');
|
|
337
|
+
console.log(' }');
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
main().catch((error) => {
|
|
341
|
+
writeSync(2, `${error instanceof Error ? error.message : String(error)}\n`);
|
|
342
|
+
process.exitCode = 1;
|
|
343
|
+
});
|