mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-30 08:34:16 +01:00
fix to deal with failed downloads
This commit is contained in:
parent
52b1300d90
commit
829242c9bb
5 changed files with 64 additions and 33 deletions
|
@ -401,7 +401,7 @@ lcToLocale( XP_LangCode lc )
|
|||
{
|
||||
const XP_UCHAR* result = NULL;
|
||||
switch ( lc ) {
|
||||
/* <item>@string/lang_unknown</item> <!-- Unknown --> */
|
||||
case 0: result = "??"; break;
|
||||
case 1: result = "en"; break;
|
||||
case 2: result = "fr"; break;
|
||||
case 3: result = "de"; break;
|
||||
|
|
|
@ -799,7 +799,7 @@ onMqttMsg(void* closure, const uint8_t* data, int len )
|
|||
dvc_parseMQTTPacket( globals->dutil, NULL, data, len );
|
||||
}
|
||||
|
||||
static void
|
||||
static bool
|
||||
storeAsDict(Globals* globals, const char* lc, const char* name,
|
||||
uint8_t* data, int len )
|
||||
{
|
||||
|
@ -811,8 +811,23 @@ storeAsDict(Globals* globals, const char* lc, const char* name,
|
|||
}
|
||||
XP_LOGFF("shortName: %s", shortName);
|
||||
|
||||
/* First make a dict of it. If it doesn't work out, don't store the
|
||||
data! */
|
||||
uint8_t* poolCopy = XP_MALLOC( globals->mpool, len );
|
||||
XP_MEMCPY( poolCopy, data, len );
|
||||
DictionaryCtxt* dict =
|
||||
wasm_dictionary_make( globals, NULL, shortName, poolCopy, len );
|
||||
bool success = !!dict;
|
||||
if ( success ) {
|
||||
dict_unref( dict, NULL );
|
||||
|
||||
const XP_UCHAR* keys[] = {KEY_DICTS, lc, shortName, NULL};
|
||||
dutil_storePtr( globals->dutil, NULL, keys, data, len );
|
||||
} else {
|
||||
XP_FREE( globals->mpool, poolCopy );
|
||||
}
|
||||
LOG_RETURNF( "%d", success );
|
||||
return success;
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -820,9 +835,13 @@ onGotDict( void* closure, const char* lc, const char* name,
|
|||
uint8_t* data, int len)
|
||||
{
|
||||
CAST_GLOB(Globals*, globals, closure);
|
||||
storeAsDict( globals, lc, name, data, len );
|
||||
if ( storeAsDict( globals, lc, name, data, len ) ) {
|
||||
if ( 0 == countGames(globals) ) {
|
||||
loadAndDraw( globals, NULL, NULL, NULL );
|
||||
}
|
||||
updateDeviceButtons( globals );
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
initDeviceGlobals( Globals* globals )
|
||||
|
@ -936,8 +955,9 @@ onDictForInvite( void* closure, const char* lc, const char* name,
|
|||
uint8_t* data, int len )
|
||||
{
|
||||
DictDownState* dds = (DictDownState*)closure;
|
||||
if ( !!data && 0 < len ) {
|
||||
storeAsDict( dds->globals, lc, name, data, len );
|
||||
if ( !!data
|
||||
&& 0 < len
|
||||
&& storeAsDict( dds->globals, lc, name, data, len ) ) {
|
||||
loadAndDraw( dds->globals, &dds->invite, NULL, NULL );
|
||||
} else {
|
||||
char msg[128];
|
||||
|
|
|
@ -175,8 +175,8 @@ wasm_dictionary_make_empty( Globals* globals )
|
|||
}
|
||||
|
||||
DictionaryCtxt*
|
||||
wasm_dictionary_make( Globals* globals, XWEnv xwe,
|
||||
const char* name, uint8_t* base, size_t len )
|
||||
wasm_dictionary_make( Globals* globals, XWEnv xwe, const char* name,
|
||||
uint8_t* base, size_t len )
|
||||
{
|
||||
WasmDictionaryCtxt* result = (WasmDictionaryCtxt*)
|
||||
XP_CALLOC(globals->mpool, sizeof(*result));
|
||||
|
@ -192,20 +192,21 @@ wasm_dictionary_make( Globals* globals, XWEnv xwe,
|
|||
result->super.func_dict_getShortName = getShortName;
|
||||
setBlankTile( &result->super );
|
||||
} else {
|
||||
XP_ASSERT( 0 ); /* gonna crash anyway */
|
||||
XP_FREE( globals->mpool, result );
|
||||
result = NULL;
|
||||
}
|
||||
|
||||
if ( !!result ) {
|
||||
(void)dict_ref( &result->super, xwe );
|
||||
|
||||
XP_U16 nf = dict_numTileFaces( &result->super );
|
||||
for ( Tile tile = 0; tile < nf; ++tile ) {
|
||||
const XP_UCHAR* face = dict_getTileString( &result->super, tile );
|
||||
XP_LOGFF( "faces[%d]: %s", tile, face );
|
||||
}
|
||||
}
|
||||
|
||||
LOG_RETURNF( "%p", &result->super );
|
||||
|
||||
/* XP_U16 nf = dict_numTileFaces( &result->super ); */
|
||||
/* for ( Tile tile = 0; tile < nf; ++tile ) { */
|
||||
/* const XP_UCHAR* face = dict_getTileString( &result->super, tile ); */
|
||||
/* XP_LOGFF( "faces[%d]: %s", tile, face ); */
|
||||
/* } */
|
||||
|
||||
return &result->super;
|
||||
}
|
||||
|
||||
|
|
|
@ -292,7 +292,7 @@ wasm_dutil_loadPtr( XW_DUtilCtxt* duc, XWEnv xwe,
|
|||
XP_LOGFF( "no file at %s", path );
|
||||
*lenp = 0; /* does not exist */
|
||||
}
|
||||
LOG_RETURN_VOID();
|
||||
// XP_LOGFF( "(path: %s)=> (len: %d)", path, *lenp );
|
||||
}
|
||||
|
||||
/* Iterate over every child of the provided path. This isn't a recursive
|
||||
|
|
|
@ -45,9 +45,22 @@ function registerOnce(devid, gitrev, now) {
|
|||
}
|
||||
}
|
||||
|
||||
function handleFetchErrors(response) {
|
||||
if ( response.ok ) {
|
||||
return response;
|
||||
} else {
|
||||
throw Error(response.statusText);
|
||||
}
|
||||
}
|
||||
|
||||
function getDict(langs, proc, closure) {
|
||||
// set these later
|
||||
let gots = {};
|
||||
function callWhenDone(xwd, lc, data, len) {
|
||||
Module.ccall('gotDictBinary', null,
|
||||
['number', 'number', 'string', 'string', 'array', 'number'],
|
||||
[proc, closure, xwd, lc, data, len ]);
|
||||
}
|
||||
|
||||
let gots = {}; // for later
|
||||
|
||||
console.log('langs: ' + langs + '; langs[0]: ' + langs[0]);
|
||||
let args = '?lc=' + langs.join('|');
|
||||
|
@ -57,11 +70,9 @@ function getDict(langs, proc, closure) {
|
|||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}).then(response => {
|
||||
console.log(response);
|
||||
if (response.ok) {
|
||||
}).then(handleFetchErrors)
|
||||
.then(response => {
|
||||
return response.json();
|
||||
}
|
||||
}).then(data => {
|
||||
// console.log('data: ' + JSON.stringify(data));
|
||||
for ( lang of data.langs ) {
|
||||
|
@ -75,11 +86,12 @@ function getDict(langs, proc, closure) {
|
|||
gots.xwd = dict.xwd;
|
||||
gots.langName = lang.lang;
|
||||
gots.lc = lang.lc;
|
||||
let path = '/' + ['android', gots.langName, gots.xwd].join('/');
|
||||
let path = '/' + ['and_wordlists', gots.langName, gots.xwd].join('/');
|
||||
return fetch(path);
|
||||
}
|
||||
}
|
||||
}).then(response => {
|
||||
}).then(handleFetchErrors)
|
||||
.then(response => {
|
||||
// console.log('got here!!!' + response);
|
||||
return response.arrayBuffer();
|
||||
}).then(data=> {
|
||||
|
@ -88,13 +100,11 @@ function getDict(langs, proc, closure) {
|
|||
// 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', 'number', 'string', 'string', 'array', 'number'],
|
||||
[proc, closure, gots.xwd, gots.lc, dataHeap, len]);
|
||||
callWhenDone(gots.xwd, gots.lc, dataHeap, len);
|
||||
Module._free(dataPtr);
|
||||
}).catch(ex => {
|
||||
callWhenDone(null, null, [], 0);
|
||||
});
|
||||
console.log('getDict() done');
|
||||
}
|
||||
|
||||
// Called from main() asap after things are initialized etc.
|
||||
|
|
Loading…
Add table
Reference in a new issue