From 5e9863adafed410ad3d72977e5d24fd7fef5bc2f Mon Sep 17 00:00:00 2001 From: Eric House Date: Sat, 27 Feb 2021 11:06:43 -0800 Subject: [PATCH] snapshot: downloading wordlist seems to work --- xwords4/wasm/Makefile | 2 +- xwords4/wasm/main.c | 45 +++++++++++++++----- xwords4/wasm/xwutils.js | 94 +++++++++++++++++++++++++++++------------ 3 files changed, 103 insertions(+), 38 deletions(-) diff --git a/xwords4/wasm/Makefile b/xwords4/wasm/Makefile index bc3a2734a..a4ad1eb02 100644 --- a/xwords4/wasm/Makefile +++ b/xwords4/wasm/Makefile @@ -75,7 +75,7 @@ main.html: ${INPUTS} Makefile shell_minimal.html --preload-file assets_dir --shell-file shell_minimal.html \ -s USE_SDL_TTF=2 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='["png"]' \ -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall']" \ - -s EXPORTED_FUNCTIONS='["_main", "_gotMQTTMsg", "_cbckVoid", "_cbckString", "_MQTTConnectedChanged", "_onNewGame"]' \ + -s EXPORTED_FUNCTIONS='["_main", "_cbckBinary", "_cbckVoid", "_cbckString", "_MQTTConnectedChanged", "_onNewGame", "_gotDictBinary"]' \ -s WASM=1 \ ${INPUTS} -o $@ diff --git a/xwords4/wasm/main.c b/xwords4/wasm/main.c index 9a13fc238..90a641e64 100644 --- a/xwords4/wasm/main.c +++ b/xwords4/wasm/main.c @@ -112,6 +112,12 @@ static void loadName( GameState* gs ); static void saveName( GameState* gs ); static bool isVisible( GameState* gs ); +typedef void (*BinProc)(void* closure, const uint8_t* data, int len ); + +EM_JS(void, call_get_dict, (void* closure), { + getDict(closure); + }); + EM_JS(void, show_name, (const char* name), { let jsname = UTF8ToString(name); document.getElementById('gamename').textContent = jsname; @@ -163,13 +169,14 @@ EM_JS(void, call_get_string, (const char* msg, const char* dflt, nbGetString( jsMgs, jsDflt, proc, closure ); } ); -EM_JS(void, call_haveDevID, (void* closure, const char* devid, - const char* gitrev, int now, - StringProc conflictProc, - StringProc focussedProc ), { +EM_JS(void, call_setup, (void* closure, const char* devid, + const char* gitrev, int now, + StringProc conflictProc, + StringProc focussedProc, + BinProc msgProc), { let jsgr = UTF8ToString(gitrev); - onHaveDevID(closure, UTF8ToString(devid), jsgr, now, - conflictProc, focussedProc); + jssetup(closure, UTF8ToString(devid), jsgr, now, + conflictProc, focussedProc, msgProc); }); EM_JS(bool, call_mqttSend, (const char* topic, const uint8_t* ptr, int len), { @@ -740,6 +747,13 @@ playLoadingDict( Globals* globals ) return dict; } +static void +onMqttMsg(void* closure, const uint8_t* data, int len ) +{ + CAST_GLOB(Globals*, globals, closure); + dvc_parseMQTTPacket( globals->dutil, NULL, data, len ); +} + static void initDeviceGlobals( Globals* globals ) { @@ -772,7 +786,9 @@ initDeviceGlobals( Globals* globals ) XP_SNPRINTF( buf, VSIZE(buf), MQTTDevID_FMT, devID ); XP_LOGFF( "got mqtt devID: %s", buf ); int now = dutil_getCurSeconds( globals->dutil, NULL ); - call_haveDevID( globals, buf, GITREV, now, onConflict, onFocussed ); + call_setup( globals, buf, GITREV, now, onConflict, onFocussed, onMqttMsg ); + + call_get_dict( globals ); } static void @@ -1585,10 +1601,9 @@ MQTTConnectedChanged( void* closure, bool connected ) } void -gotMQTTMsg( void* closure, int len, const uint8_t* msg ) +cbckBinary( BinProc proc, void* closure, int len, const uint8_t* msg ) { - Globals* globals = (Globals*)closure; - dvc_parseMQTTPacket( globals->dutil, NULL, msg, len ); + (*proc)(closure, msg, len ); } void @@ -1620,6 +1635,16 @@ cbckString( StringProc proc, void* closure, const char* str ) } } +void +gotDictBinary( void* closure, const char* xwd, const char* lang, + const char* lc, int len, uint8_t* data ) +{ + XP_LOGFF( "xwd: %s; lang: %s, lc: %s, len: %d", xwd, lang, lc, len ); + for ( int ii = 0; ii < 10; ++ii ) { + XP_LOGFF( "byte[%d]: 0x%X", ii, data[ii] ); + } +} + int main( int argc, const char** argv ) { diff --git a/xwords4/wasm/xwutils.js b/xwords4/wasm/xwutils.js index a3cf27ac7..45e484b77 100644 --- a/xwords4/wasm/xwutils.js +++ b/xwords4/wasm/xwutils.js @@ -45,29 +45,67 @@ function registerOnce(devid, gitrev, now) { } } -// function getDict(closure, proc, lang) { -// console.log('getDict()'); -// fetch('/xw4/info.py/listDicts?lc=fr', { -// method: 'post', -// headers: { -// 'Content-Type': 'application/json', -// }, -// }).then(response => { -// console.log(response); -// if (response.ok) { -// return response.json(); -// } else { -// console.log('bad respose; status: ' + respose.status); -// console.log('text: ' + response.statusText); -// console.log(response.type); -// } -// }).then(data => { -// console.log('data: ' + JSON.stringify(data)); -// }); -// console.log('getDict() done'); -// } +function getDict(closure) { + // set these later + let gots = {}; -function onHaveDevID(closure, devid, gitrev, now, noTabProc, focusProc) { + let langs = 'fr'; // navigator.language; + if (langs) { + langs = [langs.split('-')[0]]; + } + console.log('langs: ' + langs + '; langs[0]: ' + langs[0]); + if (langs[0] != 'en' ) { + langs.push('en'); + } + let args = '?lc=' + langs.join('|'); + console.log('args: ' + args); + fetch('/xw4/info.py/listDicts' + args, { + method: 'post', + headers: { + 'Content-Type': 'application/json', + }, + }).then(response => { + console.log(response); + if (response.ok) { + return response.json(); + } else { + console.log('bad respose; status: ' + respose.status); + console.log('text: ' + response.statusText); + console.log(response.type); + } + }).then(data => { + // let langs = data.langs; + // console.log('data: ' + JSON.stringify(data)); + for ( lang of data.langs ) { + console.log('lang: ' + JSON.stringify(lang)); + // wget --no-check-certificate 'http://pi4.lan/android/French/ODS7_2to15.xwd' + let dict = lang.dicts[0]; + gots.xwd = dict.xwd; + gots.langName = lang.lang; + gots.lc = lang.lc; + let path = '/' + ['android', gots.langName, gots.xwd].join('/'); + return fetch(path); + } + }).then(response => { + // console.log('got here!!!' + response); + return response.arrayBuffer(); + }).then(data=> { + let len = data.byteLength; + let dataPtr = Module._malloc(len); + // Copy data to Emscripten heap + var dataHeap = new Uint8Array(Module.HEAPU8.buffer, dataPtr, len); + dataHeap.set( new Uint8Array(data) ); + // console.log('made array?: ' + dataHeap); + Module.ccall('gotDictBinary', null, + ['number', 'string', 'string', 'string', 'number', 'array'], + [closure, gots.xwd, gots.langName, gots.lc, len, dataHeap]); + Module._free(dataPtr); + }); + console.log('getDict() done'); +} + +// Called from main() asap after things are initialized etc. +function jssetup(closure, devid, gitrev, now, noTabProc, focusProc, msgProc) { // Set a unique tag so we know if somebody comes along later let tabID = Math.random(); localStorage.setItem('tabID', tabID); @@ -88,10 +126,12 @@ function onHaveDevID(closure, devid, gitrev, now, noTabProc, focusProc) { registerOnce(devid, gitrev, now); state.closure = closure; + state.msgProc = msgProc; document.getElementById("mqtt_span").textContent=devid; function tellConnected(isConn) { - Module.ccall('MQTTConnectedChanged', null, ['number', 'boolean'], [state.closure, isConn]); + Module.ccall('MQTTConnectedChanged', null, ['number', 'boolean'], + [state.closure, isConn]); } state.client = new Paho.MQTT.Client("eehouse.org", 8883, '/wss', devid); @@ -106,10 +146,10 @@ function onHaveDevID(closure, devid, gitrev, now, noTabProc, focusProc) { } }; state.client.onMessageArrived = function onMessageArrived(message) { - var payload = message.payloadBytes; - var length = payload.length; - Module.ccall('gotMQTTMsg', null, ['number', 'number', 'array'], - [state.closure, length, payload]); + let payload = message.payloadBytes; + let length = payload.length; + Module.ccall('cbckBinary', null, ['number', 'number', 'number', 'array'], + [state.msgProc, state.closure, length, payload]); }; function onConnect() {