manual web bundle

This commit is contained in:
jusax23 2024-03-07 23:13:29 +01:00
parent 80e224c7d9
commit 70a892c07b
Signed by: jusax23
GPG key ID: 499E2AA870C1CD41
3 changed files with 21 additions and 9 deletions

View file

@ -31,6 +31,18 @@ A complete Example can be found at [stockfish_chess_engine](https://github.com/l
## Web support ## Web support
Web support is currently experimental and currently requires manuly adding assets. It uses a version of stockfish compiled with [emscripten](https://emscripten.org/). Web support is currently experimental and currently requires manuly adding assets. It uses a version of stockfish compiled with [emscripten](https://emscripten.org/).
Usage:
- Install `emscripten` and the the Environment-Variable `EMSDK`
- Build the CMakeLists.txt in `web/`
- Copy the following files to `web/stockfish/` in your project:
- flutter_stockfish_plugin.js
- flutter_stockfish_plugin.wasm
- flutter_stockfish_plugin.worker.js
- js_bindings.js
- stockfish_data.bin
If a different path should be used: Change the path const's in `js_bindungs.js` and `stockfish_web_bindings.dart`
In order to make multithreading available, the site must run in a secure environment. In order to make multithreading available, the site must run in a secure environment.
The following headers must be set for this: The following headers must be set for this:
@ -38,9 +50,8 @@ The following headers must be set for this:
- `Cross-Origin-Opener-Policy: same-origin` - `Cross-Origin-Opener-Policy: same-origin`
Problems: Problems:
- The current version includes the `.js`, `.wasm` and neuralnetwork data as assets. - The current version does not include the `.js`, `.wasm` and neuralnetwork data as assets.
These files are bundled with every build on every platform, even if they are not needed. This files will not be bundles automaticly on the web.
This approach wasts about 41MB, if not striped out by hand.
## Goal of this fork of stockfish_chess_engine ## Goal of this fork of stockfish_chess_engine

View file

@ -7,6 +7,8 @@ import 'dart:html' as html;
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter_stockfish_plugin/stockfish_bindings.dart'; import 'package:flutter_stockfish_plugin/stockfish_bindings.dart';
const jsPath = "stockfish/";
class StockfishChessEngineBindings class StockfishChessEngineBindings
extends StockfishChessEngineAbstractBindings { extends StockfishChessEngineAbstractBindings {
Future<void>? loadJs; Future<void>? loadJs;
@ -49,15 +51,13 @@ bool _jsloaded = false;
Future<void> loadJsFileIfNeeded() async { Future<void> loadJsFileIfNeeded() async {
if (kIsWeb && !_jsloaded) { if (kIsWeb && !_jsloaded) {
final stockfishScript = html.document.createElement("script"); final stockfishScript = html.document.createElement("script");
stockfishScript.setAttribute("src", stockfishScript.setAttribute("src", "${jsPath}flutter_stockfish_plugin.js");
"assets/packages/flutter_stockfish_plugin/web/flutter_stockfish_plugin.js");
html.document.head?.append(stockfishScript); html.document.head?.append(stockfishScript);
await stockfishScript.onLoad.first; await stockfishScript.onLoad.first;
final jsBindingsScript = html.document.createElement("script"); final jsBindingsScript = html.document.createElement("script");
jsBindingsScript.setAttribute( jsBindingsScript.setAttribute("src", "${jsPath}js_bindings.js");
"src", "assets/packages/flutter_stockfish_plugin/web/js_bindings.js");
html.document.head?.append(jsBindingsScript); html.document.head?.append(jsBindingsScript);
await jsBindingsScript.onLoad.first; await jsBindingsScript.onLoad.first;

View file

@ -1,4 +1,5 @@
const nnue_name = "nn-5af11540bbfe.nnue"; const nnue_name = "nn-5af11540bbfe.nnue";
const path = "stockfish/";
let s_read, s_write, s_main, s_init, s_state; let s_read, s_write, s_main, s_init, s_state;
@ -6,7 +7,7 @@ let ready = false;
let ready_cb = null; let ready_cb = null;
Module.onRuntimeInitialized = async function () { Module.onRuntimeInitialized = async function () {
let data = await fetch("assets/packages/flutter_stockfish_plugin/web/stockfish_data.bin"); let data = await fetch(path + "stockfish_data.bin");
let b = new Uint8Array(await data.arrayBuffer()); let b = new Uint8Array(await data.arrayBuffer());
FS.createDataFile("/", nnue_name, b, true, false, true); FS.createDataFile("/", nnue_name, b, true, false, true);
s_read = Module.cwrap("stockfish_stdout_read", "char*", ["bool"], { async: false }); s_read = Module.cwrap("stockfish_stdout_read", "char*", ["bool"], { async: false });
@ -30,7 +31,7 @@ let _listener_state_cb = (_) => { };
let _last_state = -2; let _last_state = -2;
function _stockfish_listener() { function _stockfish_listener() {
let state = s_state(); let state = s_state();
if(state >= 0 && _last_state != state){ if (state >= 0 && _last_state != state) {
_listener_state_cb(state); _listener_state_cb(state);
} }
_last_state = state; _last_state = state;