Modern Apps git

← Modern-Apps.git

chess: don't copy nnue to storage

Commit
5bd453b95a4b89678b1e99660dd8bc7695520e68
Author
Vayun Mathur <[email protected]>
When
2026-07-19 21:12 UTC (4 months ago)
Parents
4945be3

Diff

games/chess/build.gradle.kts
diff --git a/games/chess/build.gradle.kts b/games/chess/build.gradle.ktsindex 2238f17964..ebd2540e51 100644--- a/games/chess/build.gradle.kts+++ b/games/chess/build.gradle.kts@@ -11,10 +11,17 @@ android {     defaultConfig {         applicationId = "com.vayunmathur.games.chess"     }+    androidResources {+        // Store the Stockfish NNUE uncompressed so it can be read in place from+        // the APK via AssetManager.openFd() (fd + offset), avoiding a copy into+        // internal storage on first launch.+        noCompress += "nnue"+    } }  dependencies {     // Prebuilt Stockfish engine (native .so + Kotlin API), built once on JitPack.-    implementation("com.github.vayun-mathur:Stockfish-Library:1.0.0")+    // 1.1.0 adds the "fd:<fd>:<offset>:<length>" EvalFile scheme used below.+    implementation("com.github.vayun-mathur:Stockfish-Library:1.1.0")     testImplementation("junit:junit:4.13.2") }
games/chess/src/main/java/com/vayunmathur/games/chess/util/StockfishEngine.kt
diff --git a/games/chess/src/main/java/com/vayunmathur/games/chess/util/StockfishEngine.kt b/games/chess/src/main/java/com/vayunmathur/games/chess/util/StockfishEngine.ktindex 6ca52716ad..5c49a2f417 100644--- a/games/chess/src/main/java/com/vayunmathur/games/chess/util/StockfishEngine.kt+++ b/games/chess/src/main/java/com/vayunmathur/games/chess/util/StockfishEngine.kt@@ -8,7 +8,6 @@ import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.launch import com.vayunmathur.games.chess.data.Board import com.vayunmathur.stockfish.Stockfish-import java.io.File  object StockfishEngine {     val inputChannel = Channel<String>(Channel.UNLIMITED)@@ -39,7 +38,7 @@ object StockfishEngine {             outputChannel.trySend(line)         } -        val nnuePath = copyAssetToFiles(context, "nn-71d6d32cb962.nnue")+        val evalFileSpec = nnueEvalFileSpec(context, "nn-71d6d32cb962.nnue")          CoroutineScope(Dispatchers.IO).launch {             for (cmd in inputChannel) {@@ -50,20 +49,25 @@ object StockfishEngine {          CoroutineScope(Dispatchers.IO).launch {             inputChannel.send("uci")-            inputChannel.send("setoption name EvalFile value $nnuePath")+            inputChannel.send("setoption name EvalFile value $evalFileSpec")             inputChannel.send("isready")         }     } -    private fun copyAssetToFiles(context: Context, fileName: String): String {-        val destFile = File(context.filesDir, fileName)-        if (!destFile.exists()) {-            context.assets.open(fileName).use { input ->-                destFile.outputStream().use { output ->-                    input.copyTo(output)-                }-            }-        }-        return destFile.absolutePath+    /**+     * Points Stockfish at the NNUE stored uncompressed inside the APK, read in+     * place via a file descriptor instead of being copied to internal storage.+     * Returns an "fd:<fd>:<offset>:<length>" spec understood by the engine's+     * EvalFile loader. The fd is detached (ownership handed to native, which+     * closes it after loading). Requires `noCompress += "nnue"` so the asset is+     * page-aligned and openable as a descriptor.+     */+    private fun nnueEvalFileSpec(context: Context, fileName: String): String {+        val afd = context.assets.openFd(fileName)+        val offset = afd.startOffset+        val length = afd.length+        val fd = afd.parcelFileDescriptor.detachFd()+        afd.close()+        return "fd:$fd:$offset:$length"     } }