|
| 1 | +#include <memory> |
| 2 | +#include <string> |
| 3 | + |
| 4 | +#include "Entry.h" |
| 5 | +#include "PluginManager.h" |
| 6 | +#include "jni/java_Log.h" |
| 7 | +#include "jni/register.h" |
| 8 | +#include "ll/api/Expected.h" |
| 9 | +#include "ll/api/Logger.h" |
| 10 | +#include "ll/api/mod/Mod.h" |
| 11 | +#include "ll/api/mod/ModManager.h" |
| 12 | +#include "ll/api/mod/RegisterHelper.h" |
| 13 | +#include "ll/api/mod/ModManagerRegistry.h" |
| 14 | +#include "jni/java_Log.h" |
| 15 | + |
| 16 | +#include "jni.h" |
| 17 | +#include "ll/api/utils/StringUtils.h" |
| 18 | + |
| 19 | +namespace lje { |
| 20 | + |
| 21 | +LJE_Manager ljeManage; |
| 22 | +std::shared_ptr<LJE_Manager> pluginManager; |
| 23 | +std::unique_ptr<LJE> instance; |
| 24 | +JavaVM* jvm=nullptr; |
| 25 | +JavaVMInitArgs vm_args; |
| 26 | + |
| 27 | +void detachCurrentThread(){ |
| 28 | + jvm->DetachCurrentThread(); |
| 29 | +} |
| 30 | + |
| 31 | +JavaVM* getJVM() { return jvm; } |
| 32 | + |
| 33 | +JNIEnv* getEnv(){ |
| 34 | + JNIEnv* env; |
| 35 | + jvm->GetEnv((void**)&env, JNI_VERSION_1_8); |
| 36 | + jvm->AttachCurrentThread((void**)&env, nullptr); |
| 37 | + return env; |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +LJE& LJE::getInstance() { return *instance; } |
| 43 | + |
| 44 | +LJE_Manager& LJE::getPluginManager() { return *pluginManager; } |
| 45 | + |
| 46 | +bool LJE::load() { |
| 47 | + using namespace std; |
| 48 | + |
| 49 | + //create java vm |
| 50 | + JNIEnv* env=nullptr; |
| 51 | + std::string option = ("-Djava.class.path=" + getSelf().getModDir().string() + "/LeviLaminaJavaEngine.jar"); |
| 52 | + option=ll::string_utils::replaceAll(option,"/","\\"); |
| 53 | + getSelf().getLogger().info("JVM option: " + option); |
| 54 | + |
| 55 | + JavaVMOption options[1]; |
| 56 | + options[0].optionString = (char*) option.c_str(); |
| 57 | + vm_args.version = JNI_VERSION_1_8; |
| 58 | + vm_args.nOptions = 1; |
| 59 | + vm_args.options = options; |
| 60 | + vm_args.ignoreUnrecognized = JNI_FALSE; |
| 61 | + |
| 62 | + auto status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); |
| 63 | + if(status!=JNI_OK){ |
| 64 | + getSelf().getLogger().error("Failed to create JVM"); |
| 65 | + return false; |
| 66 | + } |
| 67 | + getSelf().getLogger().info("JVM created"); |
| 68 | + |
| 69 | + //尝试加载类 |
| 70 | + jclass jclass_LeviLamina = env->FindClass(PACK_JAVA_NAME("LeviLamina")); |
| 71 | + if(jclass_LeviLamina==nullptr){ |
| 72 | + getSelf().getLogger().error("Failed to load LeviLamina class"); |
| 73 | + env->ExceptionDescribe(); |
| 74 | + jvm->DestroyJavaVM(); |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + //register plugin manager |
| 79 | + pluginManager=std::make_shared<LJE_Manager>(); |
| 80 | + auto& pluginManagerRegistry=ll::mod::ModManagerRegistry::getInstance(); |
| 81 | + bool isSuccess=pluginManagerRegistry.addManager(pluginManager); |
| 82 | + if(!isSuccess){ |
| 83 | + getSelf().getLogger().error("Failed to register plugin manager"); |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + // 注册native方法 |
| 88 | + register_native_methods(env); |
| 89 | + |
| 90 | + // 启动 LeviLamina 类 |
| 91 | + jclass jclass_File=env->FindClass("Ljava/io/File;"); |
| 92 | + jfieldID jfield_LeviLamina_modRootDir=env->GetStaticFieldID(jclass_LeviLamina, "modRootDir", "Ljava/io/File;"); |
| 93 | + jfieldID jfield_LeviLamina_logger=env->GetStaticFieldID(jclass_LeviLamina, "logger", PACK_JAVA_NAME("Logger")); |
| 94 | + jstring jstring_native_path=env->NewStringUTF(ll::mod::getModsRoot().string().c_str()); |
| 95 | + jobject jobject_native_modRootDir=env->NewObject(jclass_File, env->GetMethodID(jclass_File, "<init>", "(Ljava/lang/String;)V"), jstring_native_path); |
| 96 | + env->DeleteLocalRef(jstring_native_path); |
| 97 | + env->SetStaticObjectField(jclass_LeviLamina, jfield_LeviLamina_modRootDir, jobject_native_modRootDir); |
| 98 | + env->DeleteLocalRef(jobject_native_modRootDir); |
| 99 | + jobject jobject_native_logger=lje::j_Logger::newLogger(env,&getSelf().getLogger()); |
| 100 | + env->SetStaticObjectField(jclass_LeviLamina, jfield_LeviLamina_logger, jobject_native_logger); |
| 101 | + env->DeleteLocalRef(jobject_native_logger); |
| 102 | + |
| 103 | + return true; |
| 104 | +} |
| 105 | + |
| 106 | +bool LJE::enable() { |
| 107 | + JNIEnv* env=getEnv(); |
| 108 | + if(env==nullptr){ |
| 109 | + return false; |
| 110 | + } |
| 111 | + jclass jclass_LeviLamina = env->FindClass(PACK_JAVA_NAME("LeviLamina")); |
| 112 | + jmethodID jmethod_enable=env->GetStaticMethodID(jclass_LeviLamina, "enable", "()V"); |
| 113 | + env->CallStaticVoidMethod(jclass_LeviLamina, jmethod_enable); |
| 114 | + |
| 115 | + detachCurrentThread(); |
| 116 | + |
| 117 | + return true; |
| 118 | +} |
| 119 | + |
| 120 | +bool LJE::disable() { |
| 121 | + JNIEnv* env=getEnv(); |
| 122 | + if(env==nullptr){ |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + jclass jclass_LeviLamina = env->FindClass(PACK_JAVA_NAME("LeviLamina")); |
| 127 | + jmethodID jmethod_disable=env->GetStaticMethodID(jclass_LeviLamina, "disable", "()V"); |
| 128 | + env->CallStaticVoidMethod(jclass_LeviLamina, jmethod_disable); |
| 129 | + |
| 130 | + detachCurrentThread(); |
| 131 | + |
| 132 | + return true; |
| 133 | +} |
| 134 | + |
| 135 | +} // namespace my_mod |
| 136 | + |
| 137 | +LL_REGISTER_MOD(lje::LJE, lje::instance); |
0 commit comments