Skip to content

Commit e2fa446

Browse files
committed
大获成功
1 parent a39d457 commit e2fa446

12 files changed

Lines changed: 1010 additions & 387 deletions

File tree

‎Android/library/src/main/cpp/CMakeLists.txt‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,17 @@ set_target_properties(clrjit PROPERTIES
5555
IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/libs/coreclr/libclrjit.so
5656
)
5757

58+
# ---- cimgui: build from source as shared library ----
59+
set(IMGUI_STATIC OFF CACHE BOOL "" FORCE)
60+
set(IMGUI_FREETYPE OFF CACHE BOOL "" FORCE)
61+
add_subdirectory(libs/cimgui ${CMAKE_CURRENT_BINARY_DIR}/cimgui)
62+
5863
# ---- Include directories (modloader) ----
5964
target_include_directories(modloader
6065
PRIVATE
6166
${CMAKE_CURRENT_SOURCE_DIR}
6267
${CMAKE_CURRENT_SOURCE_DIR}/core
68+
${CMAKE_CURRENT_SOURCE_DIR}/libs/cimgui
6369
${CMAKE_CURRENT_SOURCE_DIR}/libs/dobby/include
6470
${CMAKE_CURRENT_SOURCE_DIR}/libs/unityresolve
6571
)
@@ -78,6 +84,15 @@ find_library(log-lib log)
7884
target_link_libraries(modloader
7985
${log-lib}
8086
dobby
87+
cimgui
88+
)
89+
90+
# ---- Post-build: copy libcimgui.so alongside libmodloader.so ----
91+
add_custom_command(TARGET modloader POST_BUILD
92+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
93+
"$<TARGET_FILE:cimgui>"
94+
"$<TARGET_FILE_DIR:modloader>/libcimgui.so"
95+
COMMENT "Copying libcimgui.so to modloader output"
8196
)
8297

8398
# ---- Link libraries (monodroid) ----

‎Android/library/src/main/cpp/core/monodroid-coreclr.c‎

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -99,50 +99,65 @@ bundle_executable_path (const char* executable, const char* bundle_path, const c
9999
}
100100

101101
static bool
102-
external_assembly_probe(const char* relative_assembly_path, void** data, int64_t* size)
102+
try_map_assembly(const char* dir, const char* name, void** data, int64_t* size)
103103
{
104-
if (g_mapped_files_count >= MAX_MAPPED_COUNT)
105-
{
106-
LOG_ERROR("Too many mapped files, cannot map %s", relative_assembly_path);
107-
return false;
108-
}
109-
110-
// Look in the bundle path where the files were extracted
111104
char full_path[1024];
112-
size_t path_len = strlen(g_bundle_path) + strlen(relative_assembly_path) + 1; // +1 for '/'
113-
size_t res = snprintf(full_path, path_len + 1, "%s/%s", g_bundle_path, relative_assembly_path);
114-
if (res < 0 || res != path_len)
115-
return false;
105+
size_t path_len = strlen(dir) + strlen(name) + 1;
106+
size_t res = snprintf(full_path, path_len + 1, "%s/%s", dir, name);
107+
if (res < 0 || res != path_len) return false;
116108

117109
int fd = open(full_path, O_RDONLY);
118-
if (fd == -1)
119-
return false;
110+
if (fd == -1) return false;
120111

121112
struct stat buf;
122-
if (fstat(fd, &buf) == -1)
123-
{
124-
close(fd);
125-
return false;
126-
}
113+
if (fstat(fd, &buf) == -1) { close(fd); return false; }
127114

128115
int64_t size_local = buf.st_size;
129116
void* mapped = mmap(NULL, size_local, PROT_READ, MAP_PRIVATE, fd, 0);
130-
if (mapped == MAP_FAILED)
131-
{
132-
close(fd);
133-
return false;
134-
}
117+
if (mapped == MAP_FAILED) { close(fd); return false; }
135118

136-
LOG_INFO("Mapped %s -> %s", relative_assembly_path, full_path);
137119
g_mapped_files[g_mapped_files_count] = mapped;
138120
g_mapped_file_sizes[g_mapped_files_count] = size_local;
139121
g_mapped_files_count++;
140122
close(fd);
141123
*data = mapped;
142124
*size = size_local;
125+
LOG_INFO("Mapped %s -> %s", name, full_path);
143126
return true;
144127
}
145128

129+
static bool
130+
external_assembly_probe(const char* relative_assembly_path, void** data, int64_t* size)
131+
{
132+
if (g_mapped_files_count >= MAX_MAPPED_COUNT)
133+
{
134+
LOG_ERROR("Too many mapped files, cannot map %s", relative_assembly_path);
135+
return false;
136+
}
137+
138+
// 1) Try g_bundle_path (dotnet root)
139+
if (try_map_assembly(g_bundle_path, relative_assembly_path, data, size))
140+
return true;
141+
142+
// 2) Try each APP_PATHS directory
143+
const char* app_paths = getenv("APP_PATHS");
144+
if (app_paths) {
145+
char* paths = strdup(app_paths);
146+
char* tok = strtok(paths, ":");
147+
while (tok) {
148+
if (strcmp(tok, g_bundle_path) != 0 && // skip g_bundle_path (already tried)
149+
try_map_assembly(tok, relative_assembly_path, data, size)) {
150+
free(paths);
151+
return true;
152+
}
153+
tok = strtok(NULL, ":");
154+
}
155+
free(paths);
156+
}
157+
158+
return false;
159+
}
160+
146161
static void
147162
free_resources ()
148163
{

‎Android/library/src/main/cpp/core/unity_resolve.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ extern "C" {
1818

1919
// ---- Init / Lifecycle ----
2020

21+
void GetAssemblyCount(){
22+
UnityResolve::Init(dlopen("libil2cpp.so",1),UnityResolve::Mode::Il2Cpp);
23+
LOGE("assembly count:%zu",UnityResolve::assembly.size());
24+
}
2125
/**
2226
* 初始化 UnityResolve 并遍历所有程序集。
2327
* @param hmodule Mono 模块句柄 (通常为 dlopen("libmonosgen-2.0.so") 的返回值)
@@ -31,6 +35,26 @@ int modloader_resolve_init(void *hmodule, int mode) {
3135
return 0;
3236
}
3337

38+
/**
39+
* 初始化 UnityResolve(Il2Cpp 模式,自动获取句柄)。
40+
* 内部使用 dlopen+RTLD_NOLOAD 获取已加载的 libil2cpp.so 句柄,
41+
* 避免 C# 侧传递的句柄导致新加载副本。
42+
* @return 0 成功
43+
*/
44+
int modloader_resolve_init_il2cpp(void) {
45+
void *hmodule = dlopen("libil2cpp.so", RTLD_LAZY | RTLD_NOLOAD);
46+
LOGE("modloader_resolve_init_il2cpp: dlopen handle=%p", hmodule);
47+
if (!hmodule) {
48+
// 回退:RTLD_NOLOAD 可能在一些旧版 Android 上不支持
49+
hmodule = dlopen("libil2cpp.so", RTLD_LAZY);
50+
LOGE("modloader_resolve_init_il2cpp: fallback handle=%p", hmodule);
51+
}
52+
if (!hmodule) return -1;
53+
g_resolve_is_il2cpp = true;
54+
UnityResolve::Init(hmodule, UnityResolve::Mode::Il2Cpp);
55+
return 0;
56+
}
57+
3458
/**
3559
* 将当前线程附加到 Mono/Il2Cpp 域。
3660
*/

0 commit comments

Comments
 (0)