@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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2025 Mystral Engine
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
ThreeNative Native Runtime
|
|
2
|
+
|
|
3
|
+
This package is a maintained fork of MystralNative, originally published by Mystral
|
|
4
|
+
Engine under the MIT License: https://github.com/mystralengine/mystralnative
|
|
5
|
+
|
|
6
|
+
Upstream provenance baseline: 841fe379ca1ab23c87c99fac3b901e37487ce8f2 (v0.1.5)
|
|
7
|
+
Imported maintained-fork state: c705b16eef02bc49e68fe6c9b4114698aa6b3d7f
|
|
8
|
+
|
|
9
|
+
The upstream license is preserved in LICENSE. Third-party dependencies are not vendored;
|
|
10
|
+
scripts/download-deps.mjs downloads them into the untracked third_party directory.
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# Mystral Native - Android Build
|
|
2
|
+
|
|
3
|
+
## Prerequisites
|
|
4
|
+
|
|
5
|
+
1. **Android Studio** (or command-line SDK tools)
|
|
6
|
+
2. **Android NDK** r25 or later
|
|
7
|
+
3. **CMake** 3.22.1+
|
|
8
|
+
4. **Mystral dependencies** downloaded
|
|
9
|
+
|
|
10
|
+
## Download Dependencies
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
# From the repository root
|
|
14
|
+
cd packages/runtime-native
|
|
15
|
+
|
|
16
|
+
# Download Android-specific dependencies
|
|
17
|
+
node scripts/download-deps.mjs --android
|
|
18
|
+
|
|
19
|
+
# Also need the shared header-only and JavaScript-engine dependencies
|
|
20
|
+
node scripts/download-deps.mjs --only stb
|
|
21
|
+
node scripts/download-deps.mjs --only quickjs
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
This downloads:
|
|
25
|
+
- `wgpu-android` - WebGPU implementation (aarch64, x86_64)
|
|
26
|
+
- `sdl3-android` - SDL3 Android development package
|
|
27
|
+
|
|
28
|
+
## Build with Android Studio
|
|
29
|
+
|
|
30
|
+
1. Open the `android/` folder in Android Studio
|
|
31
|
+
2. Let Gradle sync (will download Android SDK components)
|
|
32
|
+
3. Build > Make Project
|
|
33
|
+
|
|
34
|
+
## Build from Command Line
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
# From the repository root. The gate discovers adb, Android SDK, and JDK 17.
|
|
38
|
+
node packages/runtime-native/scripts/verify-android-first-proof.mjs
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The gate performs the complete first-proof sequence:
|
|
42
|
+
|
|
43
|
+
1. Builds the debug APK from the public-API `examples/native-smoke` bundle at catalog Three.js 0.185.1.
|
|
44
|
+
2. Installs and launches `com.threenative.game/com.threenative.runtime.MystralActivity` on the only online device.
|
|
45
|
+
3. Captures app logcat to `artifacts/android/first-proof-logcat.txt`.
|
|
46
|
+
4. Requires ordered ready, first-frame, and 300-frame markers plus a live 3-second stability window.
|
|
47
|
+
5. Rejects JS/WebGPU failures and always captures a screenshot with dimensions and SHA-256.
|
|
48
|
+
|
|
49
|
+
When more than one device is online, select one explicitly:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
node packages/runtime-native/scripts/verify-android-first-proof.mjs --device emulator-5554
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The gate checks `THREENATIVE_ADB`, `THREENATIVE_ANDROID_SDK`, and
|
|
56
|
+
`THREENATIVE_JAVA_HOME` before standard SDK/JDK locations. The default screenshot is
|
|
57
|
+
`artifacts/android/first-proof.png`; override its path with:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
node packages/runtime-native/scripts/verify-android-first-proof.mjs \
|
|
61
|
+
--screenshot artifacts/android/first-proof.png
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
For a manual Gradle-only build:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
cd android
|
|
68
|
+
|
|
69
|
+
# Debug build
|
|
70
|
+
bash ./gradlew assembleDebug
|
|
71
|
+
|
|
72
|
+
# Release build
|
|
73
|
+
bash ./gradlew assembleRelease
|
|
74
|
+
|
|
75
|
+
# Install on connected device
|
|
76
|
+
bash ./gradlew installDebug
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Architecture Support
|
|
80
|
+
|
|
81
|
+
| ABI | Description | Notes |
|
|
82
|
+
|-----|-------------|-------|
|
|
83
|
+
| `arm64-v8a` | ARM64 (most devices) | Primary target |
|
|
84
|
+
| `x86_64` | Intel/AMD 64-bit | Emulator support |
|
|
85
|
+
|
|
86
|
+
## Project Structure
|
|
87
|
+
|
|
88
|
+
```mermaid
|
|
89
|
+
flowchart TD
|
|
90
|
+
android["android/"]
|
|
91
|
+
app["app/"]
|
|
92
|
+
appGradle["build.gradle.kts<br/>App build config with NDK/CMake settings"]
|
|
93
|
+
proguard["proguard-rules.pro<br/>ProGuard config"]
|
|
94
|
+
srcMain["src/main/"]
|
|
95
|
+
manifest["AndroidManifest.xml"]
|
|
96
|
+
java["java/com/mystral/engine/"]
|
|
97
|
+
activity["MystralActivity.java<br/>SDL Activity subclass"]
|
|
98
|
+
res["res/"]
|
|
99
|
+
values["values/"]
|
|
100
|
+
strings["strings.xml"]
|
|
101
|
+
themes["themes.xml"]
|
|
102
|
+
rootGradle["build.gradle.kts<br/>Root build file"]
|
|
103
|
+
properties["gradle.properties<br/>Gradle/NDK settings"]
|
|
104
|
+
settings["settings.gradle.kts<br/>Project settings"]
|
|
105
|
+
|
|
106
|
+
android --> app
|
|
107
|
+
app --> appGradle
|
|
108
|
+
app --> proguard
|
|
109
|
+
app --> srcMain
|
|
110
|
+
srcMain --> manifest
|
|
111
|
+
srcMain --> java --> activity
|
|
112
|
+
srcMain --> res --> values
|
|
113
|
+
values --> strings
|
|
114
|
+
values --> themes
|
|
115
|
+
android --> rootGradle
|
|
116
|
+
android --> properties
|
|
117
|
+
android --> settings
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## How It Works
|
|
121
|
+
|
|
122
|
+
1. **SDLActivity** handles the Android lifecycle and creates a native window
|
|
123
|
+
2. **wgpu-native** creates a Vulkan surface from the ANativeWindow
|
|
124
|
+
3. **QuickJS** runs the JavaScript game code
|
|
125
|
+
4. **SDL3** provides input (touch, gamepad) and audio
|
|
126
|
+
|
|
127
|
+
## Loading Scripts
|
|
128
|
+
|
|
129
|
+
Scripts can be loaded from:
|
|
130
|
+
- **Assets**: `asset://scripts/main.js`
|
|
131
|
+
- **Internal storage**: `/data/data/com.threenative.game/files/game.js`
|
|
132
|
+
- **External URL**: `https://example.com/game.js` (requires INTERNET permission)
|
|
133
|
+
|
|
134
|
+
## Debugging
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
# View native logs
|
|
138
|
+
adb logcat -s Mystral SDL
|
|
139
|
+
|
|
140
|
+
# View all logs from app
|
|
141
|
+
adb logcat --pid=$(adb shell pidof com.threenative.game)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Run the gate's emulator-free unit coverage with:
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
pnpm --filter @threenative/runtime-native exec vitest run --config vitest.config.ts \
|
|
148
|
+
tests/android-first-proof-gate.test.mjs tests/runtime-next-contract.test.mjs
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Known Issues
|
|
152
|
+
|
|
153
|
+
1. **Vulkan 1.1 required** - Devices must support Vulkan 1.1 (Android 7.0+)
|
|
154
|
+
2. **No Canvas 2D yet** - Skia Android builds not integrated
|
|
155
|
+
3. **HTTP uses curl** - Should migrate to Java HttpURLConnection for better Android integration
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import java.util.zip.ZipFile
|
|
2
|
+
import org.gradle.api.provider.Provider
|
|
3
|
+
|
|
4
|
+
plugins {
|
|
5
|
+
id("com.android.application")
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
val runtimeRoot = layout.projectDirectory.dir("../..")
|
|
9
|
+
val prebuiltRoot = layout.projectDirectory.dir("../prebuilt")
|
|
10
|
+
val prebuiltFiles = listOf(
|
|
11
|
+
prebuiltRoot.file("SDL3-3.2.8.aar"),
|
|
12
|
+
prebuiltRoot.file("jniLibs/arm64-v8a/libSDL3.so"),
|
|
13
|
+
prebuiltRoot.file("jniLibs/arm64-v8a/libmystral-runtime.so"),
|
|
14
|
+
prebuiltRoot.file("jniLibs/x86_64/libSDL3.so"),
|
|
15
|
+
prebuiltRoot.file("jniLibs/x86_64/libmystral-runtime.so")
|
|
16
|
+
)
|
|
17
|
+
val prebuiltCount = prebuiltFiles.count { it.asFile.isFile }
|
|
18
|
+
if (prebuiltCount != 0 && prebuiltCount != prebuiltFiles.size) {
|
|
19
|
+
throw GradleException("Android prebuilt runtime is incomplete: $prebuiltCount/${prebuiltFiles.size} files")
|
|
20
|
+
}
|
|
21
|
+
val usePrebuiltRuntime = prebuiltCount == prebuiltFiles.size
|
|
22
|
+
if (!usePrebuiltRuntime && !runtimeRoot.file("CMakeLists.txt").asFile.isFile) {
|
|
23
|
+
throw GradleException("Android prebuilt runtime is missing and source compilation is unavailable")
|
|
24
|
+
}
|
|
25
|
+
val sdl3Aar = if (usePrebuiltRuntime) prebuiltRoot.file("SDL3-3.2.8.aar")
|
|
26
|
+
else runtimeRoot.file("third_party/sdl3-android/SDL3-3.2.8.aar")
|
|
27
|
+
val extractedSdl3JniLibs = layout.buildDirectory.dir("generated/sdl3-jniLibs")
|
|
28
|
+
val generatedThreeNativeAssets = layout.buildDirectory.dir("generated/threenative/assets")
|
|
29
|
+
val nativeVsync = providers.gradleProperty("threenativeVsync").orElse("true")
|
|
30
|
+
val nativeJsProfile = providers.gradleProperty("threenativeJsProfile").orElse("false")
|
|
31
|
+
val nativeJsProfileBusyLoop = providers.gradleProperty("threenativeJsProfileBusyLoop").orElse("false")
|
|
32
|
+
// QuickJS stays the default. It is the only JIT-less engine ThreeNative ships on any platform, and
|
|
33
|
+
// the phone is where that costs the most (PRD-118), so the alternative has to be reachable without
|
|
34
|
+
// editing this file. `-PthreenativeJsEngine=v8` needs third_party/v8-android present.
|
|
35
|
+
val nativeJsEngine = providers.gradleProperty("threenativeJsEngine").orElse("quickjs")
|
|
36
|
+
|
|
37
|
+
fun Provider<String>.asCmakeBoolean(propertyName: String): String = map { value ->
|
|
38
|
+
when (value.lowercase()) {
|
|
39
|
+
"true", "on", "1" -> "ON"
|
|
40
|
+
"false", "off", "0" -> "OFF"
|
|
41
|
+
else -> throw GradleException("-$propertyName must be true or false")
|
|
42
|
+
}
|
|
43
|
+
}.get()
|
|
44
|
+
|
|
45
|
+
// V8 on Android keeps its startup snapshot outside the library, so the APK has to carry it and the
|
|
46
|
+
// runtime reads it back at launch. QuickJS builds have no snapshot and skip this entirely.
|
|
47
|
+
tasks.register("copyV8Snapshot") {
|
|
48
|
+
val engine = nativeJsEngine.get().lowercase()
|
|
49
|
+
val source = runtimeRoot.file("third_party/v8-android/snapshot_blob/arm64-v8a/snapshot_blob.bin")
|
|
50
|
+
val target = generatedThreeNativeAssets.map { it.file("v8/snapshot_blob.bin") }
|
|
51
|
+
// Declaring only the output made Gradle skip this whenever the file happened to survive from an
|
|
52
|
+
// earlier build, and an APK then shipped `libv8android.so` with no snapshot beside it — it
|
|
53
|
+
// installs, launches, and dies with "V8 startup snapshot asset is missing". The engine choice is
|
|
54
|
+
// the real input, so switching engines has to invalidate the copy.
|
|
55
|
+
inputs.property("threenativeJsEngine", engine)
|
|
56
|
+
if (engine == "v8") inputs.file(source)
|
|
57
|
+
outputs.file(target)
|
|
58
|
+
doLast {
|
|
59
|
+
val out = target.get().asFile
|
|
60
|
+
out.parentFile.mkdirs()
|
|
61
|
+
if (engine == "v8") {
|
|
62
|
+
val input = source.asFile
|
|
63
|
+
if (!input.isFile) {
|
|
64
|
+
throw GradleException("V8 startup snapshot is missing: ${'$'}{input.absolutePath}")
|
|
65
|
+
}
|
|
66
|
+
input.copyTo(out, overwrite = true)
|
|
67
|
+
} else if (out.exists()) {
|
|
68
|
+
out.delete()
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
tasks.register("extractSdl3JniLibs") {
|
|
74
|
+
inputs.file(sdl3Aar)
|
|
75
|
+
outputs.dir(extractedSdl3JniLibs)
|
|
76
|
+
doLast {
|
|
77
|
+
val outDir = extractedSdl3JniLibs.get().asFile
|
|
78
|
+
outDir.deleteRecursively()
|
|
79
|
+
ZipFile(sdl3Aar.asFile).use { zip ->
|
|
80
|
+
zip.entries().asSequence()
|
|
81
|
+
.filter { !it.isDirectory && it.name.matches(Regex("prefab/modules/SDL3-shared/libs/android\\.[^/]+/libSDL3\\.so")) }
|
|
82
|
+
.forEach { entry ->
|
|
83
|
+
val abi = entry.name.substringAfter("libs/android.").substringBefore('/')
|
|
84
|
+
val out = outDir.resolve("$abi/libSDL3.so")
|
|
85
|
+
out.parentFile.mkdirs()
|
|
86
|
+
zip.getInputStream(entry).use { input -> out.outputStream().use { output -> input.copyTo(output) } }
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
tasks.register<Exec>("buildAndroidFirstProofBundle") {
|
|
93
|
+
val playtestBridge = providers.environmentVariable("THREENATIVE_PLAYTEST_BRIDGE").orElse("enabled").get()
|
|
94
|
+
val physicsProof = providers.environmentVariable("THREENATIVE_PHYSICS_PROOF").orElse("disabled").get()
|
|
95
|
+
val physicsControl = providers.environmentVariable("THREENATIVE_PHYSICS_CONTROL").orElse("normal").get()
|
|
96
|
+
val jsProfileEnvironment = mapOf(
|
|
97
|
+
"THREENATIVE_JS_PROFILE_EXTRA_DRAW_CONTROL" to providers.environmentVariable("THREENATIVE_JS_PROFILE_EXTRA_DRAW_CONTROL").orElse("false").get(),
|
|
98
|
+
"THREENATIVE_JS_PROFILE_FRAME_WINDOW" to providers.environmentVariable("THREENATIVE_JS_PROFILE_FRAME_WINDOW").orElse("300").get(),
|
|
99
|
+
"THREENATIVE_JS_PROFILE_MATERIALS" to providers.environmentVariable("THREENATIVE_JS_PROFILE_MATERIALS").orElse("shared").get(),
|
|
100
|
+
"THREENATIVE_JS_PROFILE_MESHES" to providers.environmentVariable("THREENATIVE_JS_PROFILE_MESHES").orElse("0").get(),
|
|
101
|
+
"THREENATIVE_JS_PROFILE_PURE_JS_ITERATIONS" to providers.environmentVariable("THREENATIVE_JS_PROFILE_PURE_JS_ITERATIONS").orElse("0").get(),
|
|
102
|
+
"THREENATIVE_JS_PROFILE_PURE_JS_OBJECTS" to providers.environmentVariable("THREENATIVE_JS_PROFILE_PURE_JS_OBJECTS").orElse("2358").get(),
|
|
103
|
+
"THREENATIVE_JS_PROFILE_VISIBILITY" to providers.environmentVariable("THREENATIVE_JS_PROFILE_VISIBILITY").orElse("1").get(),
|
|
104
|
+
"THREENATIVE_JS_PROFILE_WARMUP_FRAMES" to providers.environmentVariable("THREENATIVE_JS_PROFILE_WARMUP_FRAMES").orElse("60").get(),
|
|
105
|
+
)
|
|
106
|
+
workingDir = layout.projectDirectory.dir("../..").asFile
|
|
107
|
+
commandLine(
|
|
108
|
+
"node",
|
|
109
|
+
if (physicsProof == "enabled") "scripts/build-android-physics-proof.mjs"
|
|
110
|
+
else "scripts/build-android-first-proof.mjs"
|
|
111
|
+
)
|
|
112
|
+
environment("THREENATIVE_PLAYTEST_BRIDGE", playtestBridge)
|
|
113
|
+
environment("THREENATIVE_PHYSICS_CONTROL", physicsControl)
|
|
114
|
+
environment("THREENATIVE_PHYSICS_PROOF", physicsProof)
|
|
115
|
+
environment(jsProfileEnvironment)
|
|
116
|
+
inputs.property("playtestBridge", playtestBridge)
|
|
117
|
+
inputs.property("physicsControl", physicsControl)
|
|
118
|
+
inputs.property("physicsProof", physicsProof)
|
|
119
|
+
inputs.properties(jsProfileEnvironment)
|
|
120
|
+
inputs.file(layout.projectDirectory.file("../../scripts/build-android-first-proof.mjs"))
|
|
121
|
+
inputs.file(layout.projectDirectory.file("../../scripts/build-android-physics-proof.mjs"))
|
|
122
|
+
inputs.file(layout.projectDirectory.file("../../../../pnpm-workspace.yaml"))
|
|
123
|
+
inputs.file(layout.projectDirectory.file("../../../../pnpm-lock.yaml"))
|
|
124
|
+
inputs.file(layout.projectDirectory.file("../../../../examples/native-smoke/package.json"))
|
|
125
|
+
inputs.file(layout.projectDirectory.file("../../../../examples/native-smoke/vite.config.ts"))
|
|
126
|
+
inputs.file(layout.projectDirectory.file("../../../../examples/native-smoke/scripts/verify-bundle.mjs"))
|
|
127
|
+
inputs.dir(layout.projectDirectory.dir("../../../../examples/native-smoke/src"))
|
|
128
|
+
inputs.dir(layout.projectDirectory.dir("../../../../examples/native-smoke/node_modules/three"))
|
|
129
|
+
inputs.dir(layout.projectDirectory.dir("../../../core/src"))
|
|
130
|
+
outputs.file(generatedThreeNativeAssets.map { it.file("scripts/main.js") })
|
|
131
|
+
outputs.file(generatedThreeNativeAssets.map { it.file("scripts/main.js.meta.json") })
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
val conformanceBundle = providers.gradleProperty("threenativeConformanceBundle")
|
|
135
|
+
val conformanceBundleSha256 = providers.gradleProperty("threenativeConformanceBundleSha256")
|
|
136
|
+
if (conformanceBundle.isPresent != conformanceBundleSha256.isPresent) {
|
|
137
|
+
throw GradleException(
|
|
138
|
+
"Android conformance override requires both -PthreenativeConformanceBundle and " +
|
|
139
|
+
"-PthreenativeConformanceBundleSha256"
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
tasks.register<Exec>("buildAndroidConformanceBundle") {
|
|
144
|
+
onlyIf { conformanceBundle.isPresent }
|
|
145
|
+
workingDir = layout.projectDirectory.dir("../..").asFile
|
|
146
|
+
commandLine(
|
|
147
|
+
"node",
|
|
148
|
+
"scripts/build-android-conformance.mjs",
|
|
149
|
+
"--bundle",
|
|
150
|
+
conformanceBundle.getOrElse(""),
|
|
151
|
+
"--sha256",
|
|
152
|
+
conformanceBundleSha256.getOrElse(""),
|
|
153
|
+
"--out",
|
|
154
|
+
generatedThreeNativeAssets.get().file("scripts/main.js").asFile.absolutePath
|
|
155
|
+
)
|
|
156
|
+
inputs.file(layout.projectDirectory.file("../../scripts/build-android-conformance.mjs"))
|
|
157
|
+
inputs.file(conformanceBundle)
|
|
158
|
+
inputs.property("conformanceBundleSha256", conformanceBundleSha256)
|
|
159
|
+
outputs.file(generatedThreeNativeAssets.map { it.file("scripts/main.js") })
|
|
160
|
+
outputs.file(generatedThreeNativeAssets.map { it.file("scripts/main.js.meta.json") })
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
val buildNativePhysics by tasks.registering(Exec::class) {
|
|
164
|
+
workingDir = layout.projectDirectory.dir("../..").asFile
|
|
165
|
+
environment("ANDROID_HOME", android.sdkDirectory.absolutePath)
|
|
166
|
+
commandLine("node", "scripts/build-native-physics.mjs")
|
|
167
|
+
inputs.dir(layout.projectDirectory.dir("../../native/physics/src"))
|
|
168
|
+
inputs.file(layout.projectDirectory.file("../../native/physics/Cargo.toml"))
|
|
169
|
+
inputs.file(layout.projectDirectory.file("../../native/physics/Cargo.lock"))
|
|
170
|
+
outputs.dir(layout.projectDirectory.dir("../../.runtime/physics-target"))
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
tasks.named("preBuild") {
|
|
174
|
+
dependsOn("copyV8Snapshot")
|
|
175
|
+
if (conformanceBundle.isPresent) dependsOn("buildAndroidConformanceBundle")
|
|
176
|
+
else dependsOn("buildAndroidFirstProofBundle")
|
|
177
|
+
if (!usePrebuiltRuntime) dependsOn("extractSdl3JniLibs", buildNativePhysics)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
dependencies {
|
|
181
|
+
if (usePrebuiltRuntime) implementation(files(sdl3Aar))
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
android {
|
|
185
|
+
namespace = "com.threenative.game"
|
|
186
|
+
compileSdk = 35
|
|
187
|
+
ndkVersion = "27.1.12297006"
|
|
188
|
+
|
|
189
|
+
defaultConfig {
|
|
190
|
+
applicationId = "com.threenative.game"
|
|
191
|
+
minSdk = 24 // Android 7.0 - minimum for Vulkan
|
|
192
|
+
targetSdk = 35
|
|
193
|
+
versionCode = 1
|
|
194
|
+
versionName = "0.1.0"
|
|
195
|
+
|
|
196
|
+
ndk {
|
|
197
|
+
// Target modern 64-bit architectures
|
|
198
|
+
// arm64-v8a: Most Android devices (ARM64)
|
|
199
|
+
// x86_64: Android emulator on Intel/AMD
|
|
200
|
+
abiFilters.addAll(listOf("arm64-v8a", "x86_64"))
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (!usePrebuiltRuntime) externalNativeBuild {
|
|
204
|
+
cmake {
|
|
205
|
+
// CMake arguments for the Mystral native build
|
|
206
|
+
val engine = nativeJsEngine.get().lowercase()
|
|
207
|
+
if (engine != "quickjs" && engine != "v8") {
|
|
208
|
+
throw GradleException("-PthreenativeJsEngine must be quickjs or v8, got '$engine'")
|
|
209
|
+
}
|
|
210
|
+
val nativeArguments = mutableListOf(
|
|
211
|
+
"-DANDROID=ON",
|
|
212
|
+
// V8's Android build links the shared libc++, so the whole runtime has to use
|
|
213
|
+
// it too and the APK has to carry it. With the static STL the app dies at
|
|
214
|
+
// launch: `dlopen failed: library "libc++_shared.so" not found`.
|
|
215
|
+
"-DANDROID_STL=${if (engine == "v8") "c++_shared" else "c++_static"}",
|
|
216
|
+
"-DMYSTRAL_USE_QUICKJS=${if (engine == "quickjs") "ON" else "OFF"}",
|
|
217
|
+
"-DMYSTRAL_USE_WGPU=ON",
|
|
218
|
+
"-DMYSTRAL_USE_V8=${if (engine == "v8") "ON" else "OFF"}",
|
|
219
|
+
"-DMYSTRAL_USE_DAWN=OFF",
|
|
220
|
+
"-DTN_ENABLE_CANVAS2D=OFF",
|
|
221
|
+
"-DTN_ENABLE_VIDEO=OFF",
|
|
222
|
+
"-DTN_ENABLE_WEBTRANSPORT=OFF",
|
|
223
|
+
"-DTN_ENABLE_DEBUG_SERVER=OFF",
|
|
224
|
+
"-DTN_ENABLE_NATIVE_PHYSICS=ON",
|
|
225
|
+
"-DTN_ANDROID_VSYNC=${nativeVsync.asCmakeBoolean("PthreenativeVsync")}",
|
|
226
|
+
"-DTN_ANDROID_JS_PROFILE=${nativeJsProfile.asCmakeBoolean("PthreenativeJsProfile")}",
|
|
227
|
+
"-DTN_ANDROID_JS_PROFILE_BUSY_LOOP=${nativeJsProfileBusyLoop.asCmakeBoolean("PthreenativeJsProfileBusyLoop")}",
|
|
228
|
+
)
|
|
229
|
+
System.getenv("THREENATIVE_WGPU_ROOT")?.takeIf { it.isNotBlank() }?.let {
|
|
230
|
+
nativeArguments.add("-DTHREENATIVE_WGPU_ROOT=$it")
|
|
231
|
+
}
|
|
232
|
+
arguments.addAll(nativeArguments)
|
|
233
|
+
// C/C++ flags
|
|
234
|
+
cppFlags.add("-std=c++17")
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
buildTypes {
|
|
240
|
+
debug {
|
|
241
|
+
// AGP pins the debug variant's native build to CMAKE_BUILD_TYPE=Debug and ignores
|
|
242
|
+
// an -DCMAKE_BUILD_TYPE argument, so QuickJS and the runtime compile at -O0. The
|
|
243
|
+
// host is a dependency here, not the code under test, and an unoptimized
|
|
244
|
+
// interpreter costs a real game its frame rate on device. cFlags/cppFlags land
|
|
245
|
+
// after the build-type flags, so -O2 wins.
|
|
246
|
+
if (!usePrebuiltRuntime) externalNativeBuild {
|
|
247
|
+
cmake {
|
|
248
|
+
cFlags.add("-O2")
|
|
249
|
+
cppFlags.add("-O2")
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
release {
|
|
254
|
+
isMinifyEnabled = false
|
|
255
|
+
proguardFiles(
|
|
256
|
+
getDefaultProguardFile("proguard-android-optimize.txt"),
|
|
257
|
+
"proguard-rules.pro"
|
|
258
|
+
)
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (!usePrebuiltRuntime) externalNativeBuild {
|
|
263
|
+
cmake {
|
|
264
|
+
// Point to the main CMakeLists.txt (parent of android directory)
|
|
265
|
+
path = file("../../CMakeLists.txt")
|
|
266
|
+
version = "3.22.1"
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
sourceSets {
|
|
271
|
+
getByName("main") {
|
|
272
|
+
assets.setSrcDirs(listOf(generatedThreeNativeAssets.get().asFile))
|
|
273
|
+
// Compile the vendored SDL Java glue so ThreeNative can request a
|
|
274
|
+
// larger SDLThread stack while retaining the official SDL native libs.
|
|
275
|
+
if (!usePrebuiltRuntime) {
|
|
276
|
+
java.srcDir("../../third_party/sdl3/SDL3-3.2.8/android-project/app/src/main/java")
|
|
277
|
+
}
|
|
278
|
+
// SDLActivity loads libSDL3.so before libmystral-runtime.so. The SDL3 AAR
|
|
279
|
+
// stores native libs under prefab/, so extract and package both official
|
|
280
|
+
// arm64-v8a and emulator x86_64 ABIs as jniLibs.
|
|
281
|
+
if (usePrebuiltRuntime) jniLibs.srcDir(prebuiltRoot.dir("jniLibs"))
|
|
282
|
+
else jniLibs.srcDir(extractedSdl3JniLibs)
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
packaging {
|
|
287
|
+
jniLibs.keepDebugSymbols.add("**/*.so")
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
compileOptions {
|
|
291
|
+
sourceCompatibility = JavaVersion.VERSION_1_8
|
|
292
|
+
targetCompatibility = JavaVersion.VERSION_1_8
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
buildFeatures {
|
|
296
|
+
prefab = !usePrebuiltRuntime
|
|
297
|
+
}
|
|
298
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# ProGuard rules for Mystral Native Android
|
|
2
|
+
|
|
3
|
+
# Keep SDL classes
|
|
4
|
+
-keep class org.libsdl.app.** { *; }
|
|
5
|
+
|
|
6
|
+
# Keep JNI classes
|
|
7
|
+
-keep class com.threenative.runtime.** { *; }
|
|
8
|
+
|
|
9
|
+
# Keep native methods
|
|
10
|
+
-keepclasseswithmembernames class * {
|
|
11
|
+
native <methods>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
|
3
|
+
|
|
4
|
+
<!-- Vulkan/GPU features -->
|
|
5
|
+
<uses-feature
|
|
6
|
+
android:name="android.hardware.vulkan.level"
|
|
7
|
+
android:version="1"
|
|
8
|
+
android:required="true" />
|
|
9
|
+
<uses-feature
|
|
10
|
+
android:name="android.hardware.vulkan.version"
|
|
11
|
+
android:version="0x00401000"
|
|
12
|
+
android:required="true" />
|
|
13
|
+
|
|
14
|
+
<!-- Optional: Game controller support -->
|
|
15
|
+
<uses-feature
|
|
16
|
+
android:name="android.hardware.gamepad"
|
|
17
|
+
android:required="false" />
|
|
18
|
+
|
|
19
|
+
<!-- Network permission for fetch() API -->
|
|
20
|
+
<uses-permission android:name="android.permission.INTERNET" />
|
|
21
|
+
|
|
22
|
+
<application
|
|
23
|
+
android:allowBackup="true"
|
|
24
|
+
android:label="@string/app_name"
|
|
25
|
+
android:supportsRtl="true"
|
|
26
|
+
android:theme="@style/Theme.ThreeNative"
|
|
27
|
+
android:hasCode="true">
|
|
28
|
+
|
|
29
|
+
<!-- Main SDL Activity -->
|
|
30
|
+
<activity
|
|
31
|
+
android:name="com.threenative.runtime.MystralActivity"
|
|
32
|
+
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|navigation|uiMode|density"
|
|
33
|
+
android:exported="true"
|
|
34
|
+
android:hardwareAccelerated="true">
|
|
35
|
+
<intent-filter>
|
|
36
|
+
<action android:name="android.intent.action.MAIN" />
|
|
37
|
+
<category android:name="android.intent.category.LAUNCHER" />
|
|
38
|
+
</intent-filter>
|
|
39
|
+
<meta-data
|
|
40
|
+
android:name="SDL_ENV.MYSTRAL_RUNTIME_VERSION"
|
|
41
|
+
android:value="0.1.0" />
|
|
42
|
+
</activity>
|
|
43
|
+
</application>
|
|
44
|
+
|
|
45
|
+
</manifest>
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
package com.threenative.runtime;
|
|
2
|
+
|
|
3
|
+
import android.content.pm.ApplicationInfo;
|
|
4
|
+
import android.content.pm.PackageManager;
|
|
5
|
+
import android.os.Bundle;
|
|
6
|
+
import android.view.WindowManager;
|
|
7
|
+
import org.libsdl.app.SDLActivity;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* MystralActivity - Main entry point for Mystral Native on Android.
|
|
11
|
+
*
|
|
12
|
+
* Extends SDLActivity which handles:
|
|
13
|
+
* - Native library loading
|
|
14
|
+
* - Surface creation (ANativeWindow)
|
|
15
|
+
* - Input events (touch, keyboard, gamepad)
|
|
16
|
+
* - Lifecycle management (pause/resume)
|
|
17
|
+
*
|
|
18
|
+
* The SDL3 Android backend provides the native window to wgpu-native
|
|
19
|
+
* for Vulkan surface creation.
|
|
20
|
+
*/
|
|
21
|
+
public class MystralActivity extends SDLActivity {
|
|
22
|
+
|
|
23
|
+
private Bundle applicationMetadata() {
|
|
24
|
+
try {
|
|
25
|
+
ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(
|
|
26
|
+
getPackageName(), PackageManager.GET_META_DATA);
|
|
27
|
+
return applicationInfo.metaData;
|
|
28
|
+
} catch (PackageManager.NameNotFoundException exception) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
@Override
|
|
34
|
+
protected void onCreate(Bundle savedInstanceState) {
|
|
35
|
+
super.onCreate(savedInstanceState);
|
|
36
|
+
Bundle metadata = applicationMetadata();
|
|
37
|
+
if (metadata != null && metadata.getBoolean("TN_KEEP_SCREEN_ON", false)) {
|
|
38
|
+
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
|
39
|
+
} else {
|
|
40
|
+
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Override to specify which native libraries to load.
|
|
46
|
+
* SDL handles loading SDL3, then we load mystral-runtime.
|
|
47
|
+
*/
|
|
48
|
+
@Override
|
|
49
|
+
protected String[] getLibraries() {
|
|
50
|
+
return new String[] {
|
|
51
|
+
"SDL3",
|
|
52
|
+
"mystral-runtime"
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Override to specify the main function.
|
|
58
|
+
* This is called by SDL after the libraries are loaded.
|
|
59
|
+
*/
|
|
60
|
+
@Override
|
|
61
|
+
protected String getMainFunction() {
|
|
62
|
+
return "SDL_main";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Get command line arguments for the main function.
|
|
67
|
+
* Can be used to specify the script path.
|
|
68
|
+
*/
|
|
69
|
+
@Override
|
|
70
|
+
protected String[] getArguments() {
|
|
71
|
+
String endpoint = getIntent().getStringExtra("TN_PLAYTEST_ENDPOINT");
|
|
72
|
+
String mailboxRoot = getIntent().getStringExtra("TN_PLAYTEST_MAILBOX_ROOT");
|
|
73
|
+
Bundle metadata = applicationMetadata();
|
|
74
|
+
String title = metadata == null ? "ThreeNative" : metadata.getString("TN_WINDOW_TITLE", "ThreeNative");
|
|
75
|
+
boolean fullscreen = metadata == null || metadata.getBoolean("TN_FULLSCREEN", true);
|
|
76
|
+
if (mailboxRoot == null) {
|
|
77
|
+
java.io.File externalFiles = getExternalFilesDir(null);
|
|
78
|
+
mailboxRoot = externalFiles == null ? getFilesDir().getAbsolutePath() : externalFiles.getAbsolutePath();
|
|
79
|
+
}
|
|
80
|
+
return new String[] {
|
|
81
|
+
"asset://scripts/main.js",
|
|
82
|
+
endpoint == null ? "" : endpoint,
|
|
83
|
+
mailboxRoot,
|
|
84
|
+
title,
|
|
85
|
+
Boolean.toString(fullscreen)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
+
<resources>
|
|
3
|
+
<!-- Fullscreen theme for games -->
|
|
4
|
+
<style name="Theme.ThreeNative" parent="android:Theme.NoTitleBar.Fullscreen">
|
|
5
|
+
<item name="android:windowFullscreen">true</item>
|
|
6
|
+
<item name="android:windowContentOverlay">@null</item>
|
|
7
|
+
</style>
|
|
8
|
+
</resources>
|
|
Binary file
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
distributionBase=GRADLE_USER_HOME
|
|
2
|
+
distributionPath=wrapper/dists
|
|
3
|
+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
|
|
4
|
+
distributionSha256Sum=9d926787066a081739e8200858338b4a69e837c3a821a33aca9db09dd4a41026
|
|
5
|
+
zipStoreBase=GRADLE_USER_HOME
|
|
6
|
+
zipStorePath=wrapper/dists
|