forked from focus-creative-games/il2cpp_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitialize.cpp
More file actions
99 lines (83 loc) · 2.48 KB
/
Initialize.cpp
File metadata and controls
99 lines (83 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "il2cpp-config.h"
#if IL2CPP_TARGET_ANDROID
#include <string>
#include "os/Initialize.h"
#include "os/LibraryLoader.h"
#include "utils/Logging.h"
#include <jni.h>
#include <android/log.h>
static void AndroidLogCallback(const char* message)
{
__android_log_print(ANDROID_LOG_INFO, "IL2CPP", "%s", message);
}
JavaVM *sJavaVM = nullptr;
static const Il2CppNativeChar* AndroidLoadLibrary(const Il2CppNativeChar* libName)
{
if (sJavaVM == nullptr)
{
__android_log_print(ANDROID_LOG_INFO, "IL2CPP", "Java VM not initialized");
return libName;
}
if (libName != nullptr)
{
JNIEnv* env = nullptr;
bool detached = sJavaVM->GetEnv((void**)&env, JNI_VERSION_1_2) == JNI_EDETACHED;
if (detached)
{
sJavaVM->AttachCurrentThread(&env, NULL);
}
env->ExceptionClear();
jclass systemClass = env->FindClass("java/lang/System");
if (systemClass != nullptr)
{
jmethodID loadLibrary = env->GetStaticMethodID(systemClass, "loadLibrary", "(Ljava/lang/String;)V");
if (loadLibrary != nullptr)
{
jstring jstr = env->NewStringUTF(libName);
env->CallStaticVoidMethod(systemClass, loadLibrary, jstr);
if (env->ExceptionCheck())
{
env->ExceptionClear();
// try without lib prefix
if (std::string(libName).find("lib") == 0)
{
jstr = env->NewStringUTF(libName + 3);
env->CallStaticVoidMethod(systemClass, loadLibrary, jstr);
}
}
}
}
if (env->ExceptionCheck())
{
env->ExceptionClear();
}
if (detached)
{
sJavaVM->DetachCurrentThread();
}
}
return libName;
}
extern "C"
JNIEXPORT jint JNI_OnLoad(JavaVM *jvm, void *reserved)
{
__android_log_print(ANDROID_LOG_INFO, "IL2CPP", "JNI_OnLoad");
sJavaVM = jvm;
il2cpp::os::LibraryLoader::SetFindPluginCallback(AndroidLoadLibrary);
return JNI_VERSION_1_6;
}
extern "C"
JNIEXPORT void JNI_OnUnload(JavaVM *jvm, void *reserved)
{
__android_log_print(ANDROID_LOG_INFO, "IL2CPP", "JNI_OnUnload");
sJavaVM = nullptr;
}
void il2cpp::os::Initialize()
{
if (!utils::Logging::IsLogCallbackSet())
utils::Logging::SetLogCallback(AndroidLogCallback);
}
void il2cpp::os::Uninitialize()
{
}
#endif