static jobject java_createAICallback(JNIEnv* env, const struct SSkirmishAICallback* aiCallback, int skirmishAIId) { jobject o_clb = NULL; // initialize the AI Callback class, if not yet done if (g_cls_aiCallback == NULL) { // get the AI Callback class g_cls_aiCallback = jniUtil_findClass(env, CLS_AI_CALLBACK); if (g_cls_aiCallback == NULL) { return NULL; } g_cls_aiCallback = jniUtil_makeGlobalRef(env, g_cls_aiCallback, CLS_AI_CALLBACK); if (g_cls_aiCallback == NULL) { return NULL; } // get (int skirmishAIId) constructor g_m_aiCallback_ctor_I = jniUtil_getMethodID(env, g_cls_aiCallback, "<init>", "(I)V"); if (g_m_aiCallback_ctor_I == NULL) { return NULL; } } o_clb = (*env)->NewObject(env, g_cls_aiCallback, g_m_aiCallback_ctor_I, skirmishAIId); if (jniUtil_checkException(env, "Failed creating Java AI Callback instance")) { o_clb = NULL; }/* else { o_clb = jniUtil_makeGlobalRef(env, o_clb, "AI callback instance"); }*/ return o_clb; }
static bool java_loadSkirmishAI(JNIEnv* env, const char* shortName, const char* version, const char* className, jobject* o_ai, jobject* o_aiClassLoader) { /* // convert className from "com.myai.AI" to "com/myai/AI" const size_t classNameP_sizeMax = strlen(className) + 1; char classNameP[classNameP_sizeMax]; STRCPYS(classNameP, classNameP_sizeMax, className); util_strReplaceChar(classNameP, '.', '/');*/ // get the AIs private class-loader jobject o_global_aiClassLoader = java_createAIClassLoader(env, shortName, version); if (o_global_aiClassLoader == NULL) { return false; } *o_aiClassLoader = o_global_aiClassLoader; // get the AI interface (from AIInterface.jar) if (g_cls_ai_int == NULL) { g_cls_ai_int = jniUtil_findClass(env, INT_AI); if (g_cls_ai_int == NULL) { return false; } g_cls_ai_int = jniUtil_makeGlobalRef(env, g_cls_ai_int, "AI interface class"); if (g_cls_ai_int == NULL) { return false; } } // get the AI implementation class (from SkirmishAI.jar) jclass cls_ai = jniUtil_findClassThroughLoader(env, o_global_aiClassLoader, className); if (cls_ai == NULL) { return false; } const bool implementsAIInt = (bool) (*env)->IsAssignableFrom(env, cls_ai, g_cls_ai_int); bool hasException = (*env)->ExceptionCheck(env); if (!implementsAIInt || hasException) { simpleLog_logL(SIMPLELOG_LEVEL_ERROR, "AI class not assignable from interface "INT_AI": %s", className); simpleLog_logL(SIMPLELOG_LEVEL_ERROR, "possible reasons (this list could be incomplete):"); simpleLog_logL(SIMPLELOG_LEVEL_ERROR, "* "INT_AI" interface not implemented"); simpleLog_logL(SIMPLELOG_LEVEL_ERROR, "* The AI is not compiled for the Java AI Interface version in use"); if (hasException) { (*env)->ExceptionDescribe(env); } return false; } // get factory no-arg ctor jmethodID m_ai_ctor = jniUtil_getMethodID(env, cls_ai, "<init>", "()V"); if (m_ai_ctor == NULL) { return false; } // get AI instance jobject o_local_ai = (*env)->NewObject(env, cls_ai, m_ai_ctor); hasException = (*env)->ExceptionCheck(env); if (!o_local_ai || hasException) { simpleLog_logL(SIMPLELOG_LEVEL_ERROR, "Failed fetching AI instance for class: %s", className); if (hasException) { (*env)->ExceptionDescribe(env); } return false; } // make the AI a global reference, // so it will not be garbage collected, // even after this method returned *o_ai = jniUtil_makeGlobalRef(env, o_local_ai, "AI instance"); return true; }
int eventsJniBridge_initStatic(JNIEnv* env, size_t _skirmishAIId_size) { skirmishAIId_size = _skirmishAIId_size; skirmishAIId_callback = (jobject*) calloc(skirmishAIId_size, sizeof(jobject)); size_t t; for (t=0; t < skirmishAIId_size; ++t) { skirmishAIId_callback[t] = NULL; } jobject c_aiInt = (*env)->FindClass(env, "com/springrts/ai/AI"); if (jniUtil_checkException(env, "Failed fetching AI interface class com.springrts.ai.AI")) { return -2; } m_ai_init = jniUtil_getMethodID(env, c_aiInt, "init", "(ILcom/springrts/ai/AICallback;)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: init")) { return -3; } m_ai_release = jniUtil_getMethodID(env, c_aiInt, "release", "(I)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: release")) { return -3; } m_ai_update = jniUtil_getMethodID(env, c_aiInt, "update", "(I)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: update")) { return -3; } m_ai_message = jniUtil_getMethodID(env, c_aiInt, "message", "(ILjava/lang/String;)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: message")) { return -3; } m_ai_luaMessage = jniUtil_getMethodID(env, c_aiInt, "luaMessage", "(Ljava/lang/String;)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: luaMessage")) { return -3; } m_ai_unitCreated = jniUtil_getMethodID(env, c_aiInt, "unitCreated", "(II)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: unitCreated")) { return -3; } m_ai_unitFinished = jniUtil_getMethodID(env, c_aiInt, "unitFinished", "(I)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: unitFinished")) { return -3; } m_ai_unitIdle = jniUtil_getMethodID(env, c_aiInt, "unitIdle", "(I)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: unitIdle")) { return -3; } m_ai_unitMoveFailed = jniUtil_getMethodID(env, c_aiInt, "unitMoveFailed", "(I)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: unitMoveFailed")) { return -3; } m_ai_unitDamaged = jniUtil_getMethodID(env, c_aiInt, "unitDamaged", "(IIF[FIZ)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: unitDamaged")) { return -3; } m_ai_unitDestroyed = jniUtil_getMethodID(env, c_aiInt, "unitDestroyed", "(II)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: unitDestroyed")) { return -3; } m_ai_unitGiven = jniUtil_getMethodID(env, c_aiInt, "unitGiven", "(III)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: unitGiven")) { return -3; } m_ai_unitCaptured = jniUtil_getMethodID(env, c_aiInt, "unitCaptured", "(III)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: unitCaptured")) { return -3; } m_ai_enemyEnterLOS = jniUtil_getMethodID(env, c_aiInt, "enemyEnterLOS", "(I)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: enemyEnterLOS")) { return -3; } m_ai_enemyLeaveLOS = jniUtil_getMethodID(env, c_aiInt, "enemyLeaveLOS", "(I)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: enemyLeaveLOS")) { return -3; } m_ai_enemyEnterRadar = jniUtil_getMethodID(env, c_aiInt, "enemyEnterRadar", "(I)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: enemyEnterRadar")) { return -3; } m_ai_enemyLeaveRadar = jniUtil_getMethodID(env, c_aiInt, "enemyLeaveRadar", "(I)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: enemyLeaveRadar")) { return -3; } m_ai_enemyDamaged = jniUtil_getMethodID(env, c_aiInt, "enemyDamaged", "(IIF[FIZ)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: enemyDamaged")) { return -3; } m_ai_enemyDestroyed = jniUtil_getMethodID(env, c_aiInt, "enemyDestroyed", "(II)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: enemyDestroyed")) { return -3; } m_ai_weaponFired = jniUtil_getMethodID(env, c_aiInt, "weaponFired", "(II)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: weaponFired")) { return -3; } m_ai_playerCommand = jniUtil_getMethodID(env, c_aiInt, "playerCommand", "([IIII)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: playerCommand")) { return -3; } m_ai_commandFinished = jniUtil_getMethodID(env, c_aiInt, "commandFinished", "(III)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: commandFinished")) { return -3; } m_ai_seismicPing = jniUtil_getMethodID(env, c_aiInt, "seismicPing", "([FF)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: seismicPing")) { return -3; } m_ai_load = jniUtil_getMethodID(env, c_aiInt, "load", "(Ljava/lang/String;)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: load")) { return -3; } m_ai_save = jniUtil_getMethodID(env, c_aiInt, "save", "(Ljava/lang/String;)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: save")) { return -3; } m_ai_enemyCreated = jniUtil_getMethodID(env, c_aiInt, "enemyCreated", "(I)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: enemyCreated")) { return -3; } m_ai_enemyFinished = jniUtil_getMethodID(env, c_aiInt, "enemyFinished", "(I)I"); if (jniUtil_checkException(env, "Failed fetching Java AI method ID for: enemyFinished")) { return -3; } return 0; // -> no error }