From b758cd3f0f7c1466c37cb7b3f72b8d5bdfbda594 Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 24 Feb 2023 14:27:39 -0800 Subject: [PATCH] respond to invitations coming in as argv params including downloading wordlist if necessary --- xwords4/common/nli.c | 73 +++++++++++++++++++++++++++ xwords4/common/nli.h | 4 ++ xwords4/wasm/Makefile | 1 + xwords4/wasm/main.c | 103 +++++++-------------------------------- xwords4/wasm/wasmdutil.c | 10 +--- 5 files changed, 97 insertions(+), 94 deletions(-) diff --git a/xwords4/common/nli.c b/xwords4/common/nli.c index 32659a1f5..3e1c62fd8 100644 --- a/xwords4/common/nli.c +++ b/xwords4/common/nli.c @@ -290,6 +290,79 @@ nli_makeAddrRec( const NetLaunchInfo* nli, CommsAddrRec* addr ) } } +#ifdef XWFEATURE_NLI_FROM_ARGV +XP_Bool +nli_fromArgv( MPFORMAL NetLaunchInfo* nlip, int argc, const char** argv ) +{ + XP_LOGFF( "(argc=%d)", argc ); + CurGameInfo gi = {0}; + CommsAddrRec addr = {0}; + MQTTDevID mqttDevID = 0; + XP_U16 nPlayersH = 0; + XP_U16 forceChannel = 0; + const XP_UCHAR* gameName = NULL; + const XP_UCHAR* inviteID = NULL; + + XP_LOGFF("foo"); + for ( int ii = 0; ii < argc; ++ii ) { + const char* argp = argv[ii]; + char* param = strchr(argp, '='); + if ( !param ) { /* no '='? */ + XP_LOGFF( "arg bad?: %s", argp ); + continue; + } + char arg[8]; + int argLen = param - argp; + XP_MEMCPY( arg, argp, argLen ); + arg[argLen] = '\0'; + ++param; /* skip the '=' */ + + if ( 0 == strcmp( "iso", arg ) ) { + XP_STRNCPY( gi.isoCodeStr, param, VSIZE(gi.isoCodeStr) ); + } else if ( 0 == strcmp( "np", arg ) ) { + gi.nPlayers = atoi(param); + } else if ( 0 == strcmp( "nh", arg ) ) { + nPlayersH = atoi(param); + } else if ( 0 == strcmp( "gid", arg ) ) { + gi.gameID = atoi(param); + } else if ( 0 == strcmp( "fc", arg ) ) { + gi.forceChannel = atoi(param); + } else if ( 0 == strcmp( "nm", arg ) ) { + gameName = param; + } else if ( 0 == strcmp( "id", arg ) ) { + inviteID = param; + } else if ( 0 == strcmp( "wl", arg ) ) { + replaceStringIfDifferent( mpool, &gi.dictName, param ); + } else if ( 0 == strcmp( "r2id", arg ) ) { + if ( strToMQTTCDevID( param, &addr.u.mqtt.devID ) ) { + addr_addType( &addr, COMMS_CONN_MQTT ); + } else { + XP_LOGFF( "bad devid %s", param ); + } + } else { + XP_LOGFF( "dropping arg %s, param %s", arg, param ); + } + } + + bool success = 0 < nPlayersH && + addr_hasType( &addr, COMMS_CONN_MQTT ); + + if ( success ) { + nli_init( nlip, &gi, &addr, nPlayersH, forceChannel ); + if ( !!gameName ) { + nli_setGameName( nlip, gameName ); + } + if ( !!inviteID ) { + nli_setInviteID( nlip, inviteID ); + } + LOGNLI( nlip ); + } + gi_disposePlayerInfo( MPPARM(mpool) &gi ); + LOG_RETURNF( "%s", boolToStr(success) ); + return success; +} +#endif + # ifdef DEBUG void logNLI( const NetLaunchInfo* nli, const char* callerFunc, const int callerLine ) diff --git a/xwords4/common/nli.h b/xwords4/common/nli.h index cee19eeb6..17553193d 100644 --- a/xwords4/common/nli.h +++ b/xwords4/common/nli.h @@ -50,6 +50,10 @@ void nli_setInviteID( NetLaunchInfo* nli, const XP_UCHAR* inviteID ); void nli_setGameName( NetLaunchInfo* nli, const XP_UCHAR* gameName ); void nli_setMQTTDevID( NetLaunchInfo* nli, const MQTTDevID* mqttDevID ); +#ifdef XWFEATURE_NLI_FROM_ARGV +XP_Bool nli_fromArgv( MPFORMAL NetLaunchInfo* nlip, int argc, const char** argv ); +#endif + # ifdef DEBUG void logNLI( const NetLaunchInfo* nli, const char* callerFunc, const int callerLine ); # define LOGNLI(nli) \ diff --git a/xwords4/wasm/Makefile b/xwords4/wasm/Makefile index c2a3b87e7..1984a33a6 100644 --- a/xwords4/wasm/Makefile +++ b/xwords4/wasm/Makefile @@ -51,6 +51,7 @@ DEFINES += -DDEBUG_REF DEFINES += -DXWFEATURE_COMMS_INVITE DEFINES += -DMQTT_DEV_TOPICS DEFINES += -DMQTT_GAMEID_TOPICS +DEFINES += -DXWFEATURE_NLI_FROM_ARGV DEFINES += -Wno-switch DEFINES += -DGITREV=\"$(shell git describe --tags --dirty)\" diff --git a/xwords4/wasm/main.c b/xwords4/wasm/main.c index c422b2ed1..b85f14d70 100644 --- a/xwords4/wasm/main.c +++ b/xwords4/wasm/main.c @@ -648,24 +648,22 @@ langNameFor( Globals* globals, const char* lc, char buf[], size_t buflen ) static void showName( GameState* gs ) { - const char* title = NULL; - char buf[64]; + char title[128] = {0}; if ( !!gs ) { Globals* globals = gs->globals; - title = gs->gameName; + sprintf( title, "%s", gs->gameName ); if ( 1 < countLangs( globals ) ) { char langName[32]; if ( !langNameFor( globals, gs->gi.isoCodeStr, langName, sizeof(langName) ) ) { strcpy( langName, "??" ); } - sprintf( buf, "%s (%s)", title, langName ); - title = buf; -#ifdef DEBUG - } else { - sprintf( buf, "%s (gid=%d/%X)", title, gs->gi.gameID, gs->gi.gameID ); - title = buf; -#endif + char* start = title + strlen(title); + sprintf( start, " (%s)", langName ); } +#ifdef DEBUG + char* start = title + strlen(title); + sprintf( start, " (gid=%d/%X)", gs->gi.gameID, gs->gi.gameID ); +#endif } show_name( title ); } @@ -1001,7 +999,9 @@ haveDictFor(Globals* globals, const char* lc) int count = 0; const XP_UCHAR* keys[] = {KEY_DICTS, lc, KEY_DICTS, KEY_WILDCARD, NULL}; dutil_forEach( globals->dutil, NULL_XWE, keys, upCounter, &count ); - return 0 < count; + bool result = 0 < count; + // XP_LOGFF("(lc=%s)=>%s", lc, boolToStr(result) ); + return result; } static void @@ -1085,7 +1085,7 @@ storeAsDict( Globals* globals, GotDictData* gdd ) gdd->langName ); call_alert( msg ); } - LOG_RETURNF( "%d", success ); + LOG_RETURNF( "%s", boolToStr(success) ); return success; } @@ -1269,6 +1269,7 @@ typedef struct _DictDownState { static void onDictForInvite( void* closure, GotDictData* gdd ) { + XP_LOGFF( "(lc=%s)", gdd->lc ); DictDownState* dds = (DictDownState*)closure; if ( !!gdd->data && 0 < gdd->len @@ -1529,7 +1530,7 @@ onOneLangName( void* closure, const XP_UCHAR* keys[] ) } static void -loadAndDraw( Globals* globals, const NetLaunchInfo* invite, +loadAndDraw( Globals* globals, const NetLaunchInfo* nli, int gameID, NewGameParams* params ) { XP_LOGFF( "(gameID: %X)", gameID ); @@ -1542,8 +1543,8 @@ loadAndDraw( Globals* globals, const NetLaunchInfo* invite, gs = getSavedGame( globals, gameID ); XP_LOGFF( "got game id %X", gameID ); } - if ( !!invite ) { /* overwrite gs is ok: we'll likely want both games */ - gs = gameFromInvite( globals, invite ); + if ( !!nli ) { /* overwrite gs is ok: we'll likely want both games */ + gs = gameFromInvite( globals, nli ); } } @@ -2097,76 +2098,6 @@ looper( void* closure ) #endif } -static bool -inviteFromArgv( Globals* globals, NetLaunchInfo* nlip, - int argc, const char** argv ) -{ - XP_LOGFF( "(argc=%d)", argc ); - CurGameInfo gi = {0}; - CommsAddrRec addr = {0}; - MQTTDevID mqttDevID = 0; - XP_U16 nPlayersH = 0; - XP_U16 forceChannel = 0; - const XP_UCHAR* gameName = NULL; - const XP_UCHAR* inviteID = NULL; - - for ( int ii = 0; ii < argc; ++ii ) { - const char* argp = argv[ii]; - char* param = strchr(argp, '='); - if ( !param ) { /* no '='? */ - continue; - } - char arg[8]; - int argLen = param - argp; - XP_MEMCPY( arg, argp, argLen ); - arg[argLen] = '\0'; - ++param; /* skip the '=' */ - - if ( 0 == strcmp( "lang", arg ) ) { - XP_STRNCPY( gi.isoCodeStr, param, VSIZE(gi.isoCodeStr) ); - } else if ( 0 == strcmp( "np", arg ) ) { - gi.nPlayers = atoi(param); - } else if ( 0 == strcmp( "nh", arg ) ) { - nPlayersH = atoi(param); - } else if ( 0 == strcmp( "gid", arg ) ) { - gi.gameID = atoi(param); - } else if ( 0 == strcmp( "fc", arg ) ) { - gi.forceChannel = atoi(param); - } else if ( 0 == strcmp( "nm", arg ) ) { - gameName = param; - } else if ( 0 == strcmp( "id", arg ) ) { - inviteID = param; - } else if ( 0 == strcmp( "wl", arg ) ) { - replaceStringIfDifferent( globals->mpool, &gi.dictName, param ); - } else if ( 0 == strcmp( "r2id", arg ) ) { - if ( strToMQTTCDevID( param, &addr.u.mqtt.devID ) ) { - addr_addType( &addr, COMMS_CONN_MQTT ); - } else { - XP_LOGFF( "bad devid %s", param ); - } - } else { - XP_LOGFF( "dropping arg %s, param %s", arg, param ); - } - } - - bool success = 0 < nPlayersH && - addr_hasType( &addr, COMMS_CONN_MQTT ); - - if ( success ) { - nli_init( nlip, &gi, &addr, nPlayersH, forceChannel ); - if ( !!gameName ) { - nli_setGameName( nlip, gameName ); - } - if ( !!inviteID ) { - nli_setInviteID( nlip, inviteID ); - } - LOGNLI( nlip ); - } - gi_disposePlayerInfo( MPPARM(globals->mpool) &gi ); - LOG_RETURNF( "%s", boolToStr(success) ); - return success; -} - void MQTTConnectedChanged( void* closure, bool connected ) { @@ -2400,7 +2331,7 @@ mainPostSync( int argc, const char** argv ) NetLaunchInfo nli = {0}; NetLaunchInfo* nlip = NULL; - if ( inviteFromArgv( globals, &nli, argc, argv ) ) { + if ( nli_fromArgv( MPPARM(globals->mpool) &nli, argc, argv ) ) { nlip = &nli; } diff --git a/xwords4/wasm/wasmdutil.c b/xwords4/wasm/wasmdutil.c index b14f8ab25..06e295b45 100644 --- a/xwords4/wasm/wasmdutil.c +++ b/xwords4/wasm/wasmdutil.c @@ -224,8 +224,6 @@ static void wasm_dutil_storeStream( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* keys[], XWStreamCtxt* stream ) { - LOG_FUNC(); - char path[128]; ensurePath(keys, path, true); @@ -238,7 +236,7 @@ wasm_dutil_storeStream( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* keys[], const XP_U8* data = stream_getPtr( stream ); const XP_U16 len = stream_getSize( stream ); ssize_t nWritten = write( fd, data, len ); - XP_LOGFF( "wrote %d bytes to path %s", nWritten, path ); + // XP_LOGFF( "wrote %d bytes to path %s", nWritten, path ); XP_ASSERT( nWritten == len ); ++((WasmDUtilCtxt*)duc)->dirtyCount; @@ -248,10 +246,9 @@ static void wasm_dutil_loadStream( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* keys[], XWStreamCtxt* inOut ) { - LOG_FUNC(); char path[128]; ensurePath(keys, path, false); - XP_LOGFF( "(path: %s [retry: %s])", path, path ); + // XP_LOGFF( "(path: %s [retry: %s])", path, path ); struct stat statbuf; int err = stat(path, &statbuf); @@ -264,9 +261,6 @@ wasm_dutil_loadStream( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* keys[], } else { XP_LOGFF( "no file at %s", path ); } - XP_LOGFF( "from %s", path ); - XP_LOGFF( "read %zu bytes", statbuf.st_size ); - XP_LOGFF( "read %zu bytes from path %s", statbuf.st_size, path ); } static void