mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-09 05:24:44 +01:00
seed random at start of jni, not of first game opened
Noticed the same emulator would always generate the same MQTT id, even after a factory reset. That's because I was seeding rand() in that jni *game* init code, not the (called-earlier) init of the whole jni world. MQTT id generation happens on app launch before any game can be opened so was using an unseeded rand().
This commit is contained in:
parent
786bb39941
commit
85969fb913
2 changed files with 16 additions and 12 deletions
|
@ -121,7 +121,11 @@ public class XwJNI {
|
||||||
private long m_ptrGlobals;
|
private long m_ptrGlobals;
|
||||||
private XwJNI()
|
private XwJNI()
|
||||||
{
|
{
|
||||||
m_ptrGlobals = initGlobals( new DUtilCtxt(), JNIUtilsImpl.get() );
|
long seed = Utils.nextRandomInt();
|
||||||
|
seed <<= 32;
|
||||||
|
seed |= Utils.nextRandomInt();
|
||||||
|
seed ^= System.currentTimeMillis();
|
||||||
|
m_ptrGlobals = globalsInit( new DUtilCtxt(), JNIUtilsImpl.get(), seed );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void cleanGlobalsEmu()
|
public static void cleanGlobalsEmu()
|
||||||
|
@ -217,8 +221,7 @@ public class XwJNI {
|
||||||
// Game methods
|
// Game methods
|
||||||
private static GamePtr initGameJNI( long rowid )
|
private static GamePtr initGameJNI( long rowid )
|
||||||
{
|
{
|
||||||
int seed = Utils.nextRandomInt();
|
long ptr = gameJNIInit( getJNI().m_ptrGlobals );
|
||||||
long ptr = initGameJNI( getJNI().m_ptrGlobals, seed );
|
|
||||||
GamePtr result = 0 == ptr ? null : new GamePtr( ptr, rowid );
|
GamePtr result = 0 == ptr ? null : new GamePtr( ptr, rowid );
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -659,7 +662,7 @@ public class XwJNI {
|
||||||
private static native int[] di_getIndices( long closure );
|
private static native int[] di_getIndices( long closure );
|
||||||
|
|
||||||
// Private methods -- called only here
|
// Private methods -- called only here
|
||||||
private static native long initGlobals( DUtilCtxt dutil, JNIUtils jniu );
|
private static native long globalsInit( DUtilCtxt dutil, JNIUtils jniu, long seed );
|
||||||
private static native String dvc_getMQTTDevID( long jniState, String[] topic );
|
private static native String dvc_getMQTTDevID( long jniState, String[] topic );
|
||||||
private static native byte[] dvc_makeMQTTInvite( long jniState, NetLaunchInfo nli,
|
private static native byte[] dvc_makeMQTTInvite( long jniState, NetLaunchInfo nli,
|
||||||
String[] addrToTopic );
|
String[] addrToTopic );
|
||||||
|
@ -675,7 +678,7 @@ public class XwJNI {
|
||||||
byte[] stream );
|
byte[] stream );
|
||||||
private static native byte[] nli_to_stream( long jniState, NetLaunchInfo nli );
|
private static native byte[] nli_to_stream( long jniState, NetLaunchInfo nli );
|
||||||
private static native NetLaunchInfo nli_from_stream( long jniState, byte[] stream );
|
private static native NetLaunchInfo nli_from_stream( long jniState, byte[] stream );
|
||||||
private static native long initGameJNI( long jniState, int seed );
|
private static native long gameJNIInit( long jniState );
|
||||||
private static native void envDone( long globals );
|
private static native void envDone( long globals );
|
||||||
private static native long dict_make( long jniState, byte[] dict, String name, String path );
|
private static native long dict_make( long jniState, byte[] dict, String name, String path );
|
||||||
private static native void dict_ref( long dictPtr );
|
private static native void dict_ref( long dictPtr );
|
||||||
|
|
|
@ -367,13 +367,17 @@ getState( JNIEnv* env, GamePtrType gamePtr, const char* func )
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
JNIEXPORT jlong JNICALL
|
JNIEXPORT jlong JNICALL
|
||||||
Java_org_eehouse_android_xw4_jni_XwJNI_initGlobals
|
Java_org_eehouse_android_xw4_jni_XwJNI_globalsInit
|
||||||
( JNIEnv* env, jclass C, jobject jdutil, jobject jniu )
|
( JNIEnv* env, jclass C, jobject jdutil, jobject jniu, jlong jseed )
|
||||||
{
|
{
|
||||||
#ifdef MEM_DEBUG
|
#ifdef MEM_DEBUG
|
||||||
MemPoolCtx* mpool = mpool_make( NULL );
|
MemPoolCtx* mpool = mpool_make( NULL );
|
||||||
XP_LOGF( "%s(): ptr size: %zu", __func__, sizeof(mpool) );
|
XP_LOGF( "%s(): ptr size: %zu", __func__, sizeof(mpool) );
|
||||||
#endif
|
#endif
|
||||||
|
int seed = (int)jseed;
|
||||||
|
XP_LOGFF( "calling srandom(seed %d)", seed );
|
||||||
|
srandom( seed );
|
||||||
|
|
||||||
JNIGlobalState* globalState = (JNIGlobalState*)XP_CALLOC( mpool,
|
JNIGlobalState* globalState = (JNIGlobalState*)XP_CALLOC( mpool,
|
||||||
sizeof(*globalState) );
|
sizeof(*globalState) );
|
||||||
map_init( MPPARM(mpool) &globalState->ti, env );
|
map_init( MPPARM(mpool) &globalState->ti, env );
|
||||||
|
@ -1227,8 +1231,8 @@ struct _JNIState {
|
||||||
} \
|
} \
|
||||||
|
|
||||||
JNIEXPORT jlong JNICALL
|
JNIEXPORT jlong JNICALL
|
||||||
Java_org_eehouse_android_xw4_jni_XwJNI_initGameJNI
|
Java_org_eehouse_android_xw4_jni_XwJNI_gameJNIInit
|
||||||
( JNIEnv* env, jclass C, jlong jniGlobalPtr, jint seed )
|
( JNIEnv* env, jclass C, jlong jniGlobalPtr )
|
||||||
{
|
{
|
||||||
#ifdef MEM_DEBUG
|
#ifdef MEM_DEBUG
|
||||||
MemPoolCtx* mpool = ((JNIGlobalState*)jniGlobalPtr)->mpool;
|
MemPoolCtx* mpool = ((JNIGlobalState*)jniGlobalPtr)->mpool;
|
||||||
|
@ -1245,9 +1249,6 @@ Java_org_eehouse_android_xw4_jni_XwJNI_initGameJNI
|
||||||
MPASSIGN( state->mpool, mpool );
|
MPASSIGN( state->mpool, mpool );
|
||||||
globals->vtMgr = make_vtablemgr(MPPARM_NOCOMMA(mpool));
|
globals->vtMgr = make_vtablemgr(MPPARM_NOCOMMA(mpool));
|
||||||
|
|
||||||
/* XP_LOGF( "%s: initing srand with %d", __func__, seed ); */
|
|
||||||
srandom( seed );
|
|
||||||
|
|
||||||
/* LOG_RETURNF( "%p", state ); */
|
/* LOG_RETURNF( "%p", state ); */
|
||||||
return (jlong) state;
|
return (jlong) state;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue