From 17311dc75720a045bc9360fad94250da4b295b49 Mon Sep 17 00:00:00 2001 From: Eric House Date: Sun, 7 Mar 2021 09:22:03 -0800 Subject: [PATCH] get rid of dict processing where possible The old API required passing dict into game creation/loading. New doesn't, but in some places I was doing other stuff there (like checking existance), so can't remove there. Still code goes away. --- .../org/eehouse/android/xw4/GameUtils.java | 37 +++--------- .../eehouse/android/xw4/jni/DUtilCtxt.java | 11 ++-- .../eehouse/android/xw4/jni/JNIThread.java | 8 +-- .../org/eehouse/android/xw4/jni/XwJNI.java | 21 ++----- xwords4/android/jni/utilwrapper.c | 16 +++-- xwords4/android/jni/xwjni.c | 58 +++++++++---------- 6 files changed, 58 insertions(+), 93 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java index e55198eac..53220a994 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java @@ -131,8 +131,6 @@ public class GameUtils { { CurGameInfo gi = new CurGameInfo( context ); CommsAddrRec addr = null; - String[] dictNames = gi.dictNames(); - DictUtils.DictPairs pairs = DictUtils.openDicts( context, dictNames ); try ( GamePtr gamePtr = loadMakeGame( context, gi, lockSrc ) ) { if ( XwJNI.game_hasComms( gamePtr ) ) { @@ -141,10 +139,8 @@ public class GameUtils { } try ( GamePtr gamePtr = XwJNI - .initNew( gi, dictNames, pairs.m_bytes, pairs.m_paths, - gi.langName( context ), (UtilCtxt)null, - (DrawCtx)null, CommonPrefs.get( context ), - (TransportProcs)null ) ) { + .initNew( gi, (UtilCtxt)null, (DrawCtx)null, + CommonPrefs.get( context ), (TransportProcs)null ) ) { if ( juggle ) { gi.juggle(); @@ -411,16 +407,11 @@ public class GameUtils { TextUtils.join( ",", dictNames ) ); } else { String langName = gi.langName( context ); - gamePtr = XwJNI.initFromStream( rowid, stream, gi, dictNames, - pairs.m_bytes, pairs.m_paths, - langName, util, - null, + gamePtr = XwJNI.initFromStream( rowid, stream, gi, util, null, CommonPrefs.get(context), tp ); if ( null == gamePtr ) { - gamePtr = XwJNI.initNew( gi, dictNames, - pairs.m_bytes, pairs.m_paths, - langName, (UtilCtxt)null, null, + gamePtr = XwJNI.initNew( gi, (UtilCtxt)null, null, CommonPrefs.get(context), null ); } } @@ -1099,15 +1090,9 @@ public class GameUtils { // first time required so dictNames() will work gi.replaceDicts( context, newDict ); - String[] dictNames = gi.dictNames(); - DictUtils.DictPairs pairs = DictUtils.openDicts( context, - dictNames ); - try ( GamePtr gamePtr = - XwJNI.initFromStream( rowid, stream, gi, dictNames, - pairs.m_bytes, pairs.m_paths, - gi.langName( context ), null, - null, CommonPrefs.get( context ), null ) ) { + XwJNI.initFromStream( rowid, stream, gi, null, null, + CommonPrefs.get( context ), null ) ) { // second time required as game_makeFromStream can overwrite gi.replaceDicts( context, newDict ); @@ -1140,9 +1125,6 @@ public class GameUtils { // somesuch. But: do we have a way to save changes to a gi // that don't reset the game, e.g. player name for standalone // games? - String[] dictNames = gi.dictNames(); - DictUtils.DictPairs pairs = DictUtils.openDicts( context, dictNames ); - String langName = gi.langName( context ); boolean madeGame = false; CommonPrefs cp = CommonPrefs.get( context ); @@ -1154,8 +1136,6 @@ public class GameUtils { try ( GamePtr gamePtr = XwJNI .initFromStream( lock.getRowid(), stream, new CurGameInfo(context), - dictNames, pairs.m_bytes, - pairs.m_paths, langName, null, null, cp, null ) ) { if ( null != gamePtr ) { applyChanges( context, sink, gi, car, disab, @@ -1166,9 +1146,8 @@ public class GameUtils { } if ( forceNew || !madeGame ) { - try ( GamePtr gamePtr = XwJNI.initNew( gi, dictNames, pairs.m_bytes, - pairs.m_paths, langName, util, - (DrawCtx)null, cp, sink ) ) { + try ( GamePtr gamePtr = XwJNI.initNew( gi, util, (DrawCtx)null, + cp, sink ) ) { if ( null != gamePtr ) { applyChanges( context, sink, gi, car, disab, lock, gamePtr ); } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/DUtilCtxt.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/DUtilCtxt.java index f178b1759..1989dd449 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/DUtilCtxt.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/DUtilCtxt.java @@ -337,15 +337,16 @@ public class DUtilCtxt { return msg; } - public String getDictPath( int lang, String name ) + public void getDictPath( int lang, String name, String[] path, byte[][] bytes ) { Log.d( TAG, "getDictPath(%d, %s)", lang, name ); String[] names = { name }; DictUtils.DictPairs pairs = DictUtils.openDicts( m_context, names ); - String path = pairs.m_paths[0]; - Assert.assertNotNull( path ); - Log.d( TAG, "getDictPath(%s) => %s", name, path ); - return path; + Log.d( TAG, "openDicts() => %s", pairs ); + path[0] = pairs.m_paths[0]; + bytes[0] = pairs.m_bytes[0]; + Log.d( TAG, "getDictPath(%s): have path: %s; bytes: %s", name, path[0], bytes[0] ); + Assert.assertTrue( path[0] != null || bytes[0] != null ); } public void onDupTimerChanged( int gameID, int oldVal, int newVal ) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/JNIThread.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/JNIThread.java index f8db5e877..c6485535f 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/JNIThread.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/JNIThread.java @@ -230,16 +230,10 @@ public class JNIThread extends Thread implements AutoCloseable { m_jniGamePtr = null; if ( null != stream ) { m_jniGamePtr = XwJNI.initFromStream( m_rowid, stream, m_gi, - dictNames, pairs.m_bytes, - pairs.m_paths, - m_gi.langName( m_context ), utils, null, cp, m_xport ); } if ( null == m_jniGamePtr ) { - m_jniGamePtr = XwJNI.initNew( m_gi, dictNames, pairs.m_bytes, - pairs.m_paths, - m_gi.langName(m_context), - utils, null, cp, m_xport ); + m_jniGamePtr = XwJNI.initNew( m_gi, utils, null, cp, m_xport ); } Assert.assertNotNull( m_jniGamePtr ); notifyAll(); diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/XwJNI.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/XwJNI.java index 94af66510..d896c0f05 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/XwJNI.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/XwJNI.java @@ -285,15 +285,12 @@ public class XwJNI { public static synchronized GamePtr initFromStream( long rowid, byte[] stream, CurGameInfo gi, - String[] dictNames, byte[][] dictBytes, - String[] dictPaths, String langName, UtilCtxt util, DrawCtx draw, CommonPrefs cp, TransportProcs procs ) { GamePtr gamePtr = initGameJNI( rowid ); - if ( ! game_makeFromStream( gamePtr, stream, gi, dictNames, dictBytes, - dictPaths, langName, util, draw, + if ( ! game_makeFromStream( gamePtr, stream, gi, util, draw, cp, procs ) ) { gamePtr.release(); gamePtr = null; @@ -303,13 +300,11 @@ public class XwJNI { } public static synchronized GamePtr - initNew( CurGameInfo gi, String[] dictNames, byte[][] dictBytes, - String[] dictPaths, String langName, UtilCtxt util, - DrawCtx draw, CommonPrefs cp, TransportProcs procs ) + initNew( CurGameInfo gi, UtilCtxt util, DrawCtx draw, + CommonPrefs cp, TransportProcs procs ) { GamePtr gamePtr = initGameJNI( 0 ); - game_makeNewGame( gamePtr, gi, dictNames, dictBytes, dictPaths, - langName, util, draw, cp, procs ); + game_makeNewGame( gamePtr, gi, util, draw, cp, procs ); return gamePtr; } @@ -321,10 +316,6 @@ public class XwJNI { private static native void game_makeNewGame( GamePtr gamePtr, CurGameInfo gi, - String[] dictNames, - byte[][] dictBytes, - String[] dictPaths, - String langName, UtilCtxt util, DrawCtx draw, CommonPrefs cp, TransportProcs procs ); @@ -332,10 +323,6 @@ public class XwJNI { private static native boolean game_makeFromStream( GamePtr gamePtr, byte[] stream, CurGameInfo gi, - String[] dictNames, - byte[][] dictBytes, - String[] dictPaths, - String langName, UtilCtxt util, DrawCtx draw, CommonPrefs cp, diff --git a/xwords4/android/jni/utilwrapper.c b/xwords4/android/jni/utilwrapper.c index 37b64a44e..a681862bc 100644 --- a/xwords4/android/jni/utilwrapper.c +++ b/xwords4/android/jni/utilwrapper.c @@ -822,16 +822,22 @@ and_dutil_getDict( XW_DUtilCtxt* duc, XWEnv xwe, dmgr_get( dictMgr, xwe, dictName ); if ( !dict ) { jstring jname = (*env)->NewStringUTF( env, dictName ); - jstring jpath = NULL; - DUTIL_CBK_HEADER( "getDictPath", "(ILjava/lang/String;)Ljava/lang/String;" ); - jpath = (*env)->CallObjectMethod( env, dutil->jdutil, mid, lang, jname ); + + jobjectArray jstrs = makeStringArray( env, 1, NULL ); + jobjectArray jbytes = makeByteArrayArray( env, 1 ); + + DUTIL_CBK_HEADER( "getDictPath", "(ILjava/lang/String;[Ljava/lang/String;[[B)V" ); + (*env)->CallVoidMethod( env, dutil->jdutil, mid, lang, jname, jstrs, jbytes ); DUTIL_CBK_TAIL(); + jstring jpath = (*env)->GetObjectArrayElement( env, jstrs, 0 ); + jbyteArray jdata = (*env)->GetObjectArrayElement( env, jbytes, 0 ); + dict = makeDict( MPPARM(duc->mpool) xwe, TI_IF(&globalState->ti) dictMgr, jniutil, - jname, NULL, jpath, NULL, false ); - deleteLocalRefs( env, jname, jpath, DELETE_NO_REF ); + jname, jdata, jpath, NULL, false ); + deleteLocalRefs( env, jname, jstrs, jbytes, jdata, jpath, DELETE_NO_REF ); } LOG_RETURNF( "%p", dict ); return dict; diff --git a/xwords4/android/jni/xwjni.c b/xwords4/android/jni/xwjni.c index eec0c0159..7b4db95bc 100644 --- a/xwords4/android/jni/xwjni.c +++ b/xwords4/android/jni/xwjni.c @@ -1359,8 +1359,7 @@ Java_org_eehouse_android_xw4_jni_XwJNI_envDone JNIEXPORT void JNICALL Java_org_eehouse_android_xw4_jni_XwJNI_game_1makeNewGame ( JNIEnv* env, jclass C, GamePtrType gamePtr, jobject j_gi, - jobjectArray j_names, jobjectArray j_dicts, jobjectArray j_paths, - jstring j_lang, jobject j_util, jobject j_draw, jobject j_cp, + jobject j_util, jobject j_draw, jobject j_cp, jobject j_procs ) { XWJNI_START_GLOBALS(); @@ -1385,24 +1384,24 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1makeNewGame game_makeNewGame( MPPARM(mpool) env, &state->game, gi, globals->util, dctx, &cp, globals->xportProcs ); - DictionaryCtxt* dict; - PlayerDicts dicts; + /* DictionaryCtxt* dict; */ + /* PlayerDicts dicts; */ - makeDicts( MPPARM(state->globalJNI->mpool) env, - TI_IF(&state->globalJNI->ti) - state->globalJNI->dictMgr, - globals->jniutil, &dict, &dicts, j_names, j_dicts, - j_paths, j_lang ); -#ifdef STUBBED_DICT - if ( !dict ) { - XP_LOGFF( "falling back to stubbed dict" ); - dict = make_stubbed_dict( MPPARM_NOCOMMA(mpool) ); - } -#endif - model_setDictionary( state->game.model, env, dict ); - dict_unref( dict, env ); /* game owns it now */ - model_setPlayerDicts( state->game.model, env, &dicts ); - dict_unref_all( &dicts, env ); +/* makeDicts( MPPARM(state->globalJNI->mpool) env, */ +/* TI_IF(&state->globalJNI->ti) */ +/* state->globalJNI->dictMgr, */ +/* globals->jniutil, &dict, &dicts, j_names, j_dicts, */ +/* j_paths, j_lang ); */ +/* #ifdef STUBBED_DICT */ +/* if ( !dict ) { */ +/* XP_LOGFF( "falling back to stubbed dict" ); */ +/* dict = make_stubbed_dict( MPPARM_NOCOMMA(mpool) ); */ +/* } */ +/* #endif */ +/* model_setDictionary( state->game.model, env, dict ); */ +/* dict_unref( dict, env ); /\* game owns it now *\/ */ + /* model_setPlayerDicts( state->game.model, env, &dicts ); */ + /* dict_unref_all( &dicts, env ); */ XWJNI_END(); } /* makeNewGame */ @@ -1437,12 +1436,11 @@ JNIEXPORT void JNICALL Java_org_eehouse_android_xw4_jni_XwJNI_game_1dispose JNIEXPORT jboolean JNICALL Java_org_eehouse_android_xw4_jni_XwJNI_game_1makeFromStream ( JNIEnv* env, jclass C, GamePtrType gamePtr, jbyteArray jstream, jobject /*out*/jgi, - jobjectArray jdictNames, jobjectArray jdicts, jobjectArray jpaths, - jstring jlang, jobject jutil, jobject jdraw, jobject jcp, jobject jprocs ) + jobject jutil, jobject jdraw, jobject jcp, jobject jprocs ) { jboolean result; - DictionaryCtxt* dict; - PlayerDicts dicts; + /* DictionaryCtxt* dict; */ + /* PlayerDicts dicts; */ XWJNI_START_GLOBALS(); globals->gi = (CurGameInfo*)XP_CALLOC( mpool, sizeof(*globals->gi) ); @@ -1450,11 +1448,11 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1makeFromStream TI_IF(&state->globalJNI->ti) jutil, globals->gi, globals); globals->jniutil = state->globalJNI->jniutil; - makeDicts( MPPARM(state->globalJNI->mpool) env, - TI_IF(&state->globalJNI->ti) - state->globalJNI->dictMgr, - globals->jniutil, &dict, &dicts, jdictNames, jdicts, jpaths, - jlang ); + /* makeDicts( MPPARM(state->globalJNI->mpool) env, */ + /* TI_IF(&state->globalJNI->ti) */ + /* state->globalJNI->dictMgr, */ + /* globals->jniutil, &dict, &dicts, jdictNames, jdicts, jpaths, */ + /* jlang ); */ if ( !!jdraw ) { globals->dctx = makeDraw( MPPARM(mpool) env, TI_IF(&state->globalJNI->ti) @@ -1473,8 +1471,8 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1makeFromStream globals->gi, globals->util, globals->dctx, &cp, globals->xportProcs ); stream_destroy( stream, env ); - dict_unref( dict, env ); /* game owns it now */ - dict_unref_all( &dicts, env ); + /* dict_unref( dict, env ); /\* game owns it now *\/ */ + /* dict_unref_all( &dicts, env ); */ /* If game_makeFromStream() fails, the platform-side caller still needs to call game_dispose. That requirement's better than having cleanup code