diff --git a/README.md b/README.md index d17f1d8..24ced75 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,18 @@ A complete Example can be found at [stockfish_chess_engine](https://github.com/l ## 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/). +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. 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` Problems: -- The current version includes 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 approach wasts about 41MB, if not striped out by hand. +- The current version does not include the `.js`, `.wasm` and neuralnetwork data as assets. +This files will not be bundles automaticly on the web. ## Goal of this fork of stockfish_chess_engine diff --git a/lib/stockfish_web_bindings.dart b/lib/stockfish_web_bindings.dart index 6aa041c..12a9107 100644 --- a/lib/stockfish_web_bindings.dart +++ b/lib/stockfish_web_bindings.dart @@ -7,6 +7,8 @@ import 'dart:html' as html; import 'package:flutter/foundation.dart'; import 'package:flutter_stockfish_plugin/stockfish_bindings.dart'; +const jsPath = "stockfish/"; + class StockfishChessEngineBindings extends StockfishChessEngineAbstractBindings { Future? loadJs; @@ -49,15 +51,13 @@ bool _jsloaded = false; Future loadJsFileIfNeeded() async { if (kIsWeb && !_jsloaded) { final stockfishScript = html.document.createElement("script"); - stockfishScript.setAttribute("src", - "assets/packages/flutter_stockfish_plugin/web/flutter_stockfish_plugin.js"); + stockfishScript.setAttribute("src", "${jsPath}flutter_stockfish_plugin.js"); html.document.head?.append(stockfishScript); await stockfishScript.onLoad.first; final jsBindingsScript = html.document.createElement("script"); - jsBindingsScript.setAttribute( - "src", "assets/packages/flutter_stockfish_plugin/web/js_bindings.js"); + jsBindingsScript.setAttribute("src", "${jsPath}js_bindings.js"); html.document.head?.append(jsBindingsScript); await jsBindingsScript.onLoad.first; diff --git a/web/js_bindings.js b/web/js_bindings.js index 002d39a..3e5f026 100644 --- a/web/js_bindings.js +++ b/web/js_bindings.js @@ -1,4 +1,5 @@ const nnue_name = "nn-5af11540bbfe.nnue"; +const path = "stockfish/"; let s_read, s_write, s_main, s_init, s_state; @@ -6,7 +7,7 @@ let ready = false; let ready_cb = null; 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()); FS.createDataFile("/", nnue_name, b, true, false, true); s_read = Module.cwrap("stockfish_stdout_read", "char*", ["bool"], { async: false }); @@ -30,7 +31,7 @@ let _listener_state_cb = (_) => { }; let _last_state = -2; function _stockfish_listener() { let state = s_state(); - if(state >= 0 && _last_state != state){ + if (state >= 0 && _last_state != state) { _listener_state_cb(state); } _last_state = state;