Skip to content

Commit 22dbfe6

Browse files
committedAug 10, 2021
升级 Riru v26
1 parent 2d6a292 commit 22dbfe6

19 files changed

+345
-463
lines changed
 

‎.gitattributes

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
*.prop text eol=lf
2-
*.sh text eol=lf
1+
* text=auto eol=lf
2+
3+
*.bat text eol=crlf
4+
*.jar binary

‎LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Rikka
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎build.gradle

+9-7
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,28 @@ idea.module {
88

99
buildscript {
1010
repositories {
11+
mavenCentral()
1112
google()
12-
jcenter()
1313
}
1414
dependencies {
15-
classpath 'com.android.tools.build:gradle:4.1.1'
15+
classpath 'com.android.tools.build:gradle:4.2.2'
1616
}
1717
}
1818

1919
allprojects {
2020
repositories {
21+
mavenCentral()
2122
google()
22-
jcenter()
2323
}
2424
}
2525

26-
task clean(type: Delete) {
27-
delete rootProject.buildDir
28-
}
29-
3026
ext {
3127
minSdkVersion = 23
3228
targetSdkVersion = 30
29+
30+
outDir = file("$rootDir/out")
31+
}
32+
33+
task clean(type: Delete) {
34+
delete rootProject.buildDir, outDir
3335
}

‎gradle.properties

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ android.useAndroidX=true
1818
# Automatically convert third-party libraries to use AndroidX
1919
android.enableJetifier=true
2020
# https://github.com/google/prefab/issues/122
21-
android.prefabVersion=1.1.2
21+
# Remove this until AGP update prefab version
22+
android.prefabVersion=1.1.3
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Fri Oct 09 23:12:33 CST 2020
1+
#Mon Jul 12 21:05:17 CST 2021
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-all.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

‎module.gradle

+26-27
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
ext {
2-
// FIXME replace with yours
3-
moduleId = "il2cppdumper"
4-
moduleName = "Il2CppDumper"
5-
moduleAuthor = "Perfare"
6-
moduleDescription = "Il2CppDumper Riru version."
7-
moduleVersion = "v1.1"
8-
moduleVersionCode = 1
2+
/*
3+
This name will be used in the name of the so file ("lib${moduleLibraryName}.so").
4+
*/
5+
moduleLibraryName = "il2cppdumper"
6+
7+
/* Minimal supported Riru API version, used in the version check of riru.sh */
8+
moduleMinRiruApiVersion = 24
9+
10+
/* The version name of minimal supported Riru, used in the version check of riru.sh */
11+
moduleMinRiruVersionName = "v24.0.0"
912

10-
moduleMinRiruApiVersion = 9
11-
moduleMinRiruVersionName = "v22.0"
12-
moduleRiruApiVersion = 10
13+
/* Maximum supported Riru API version, used in the version check of riru.sh */
14+
moduleRiruApiVersion = 26
1315

14-
moduleProp = [
15-
name : moduleName,
16-
version : moduleVersion,
17-
versionCode: moduleVersionCode.toString(),
18-
author : moduleAuthor,
19-
description: moduleDescription,
20-
minApi : moduleMinRiruApiVersion
21-
]
16+
/*
17+
Magisk module ID
18+
Since Magisk use it to distinguish different modules, you should never change it.
2219
23-
magiskModuleProp = [
24-
id : "riru-${moduleId.replace('_', '-')}",
25-
name : "Riru - ${moduleProp['name']}",
26-
version : moduleProp['version'],
27-
versionCode: moduleProp['versionCode'],
28-
author : moduleProp['author'],
29-
description: moduleProp['description']
30-
]
31-
}
20+
Note, the older version of the template uses '-' instead of '_', if your are upgrading from
21+
the older version, please pay attention.
22+
*/
23+
magiskModuleId = "riru_il2cppdumper"
24+
25+
moduleName = "Il2CppDumper"
26+
moduleAuthor = "Perfare"
27+
moduleDescription = "Il2CppDumper Riru version."
28+
moduleVersion = "v26.0.0"
29+
moduleVersionCode = 26
30+
}

‎module/build.gradle

+85-91
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import org.apache.tools.ant.filters.FixCrLfFilter
2+
import org.apache.tools.ant.filters.ReplaceTokens
3+
4+
import java.security.MessageDigest
5+
16
apply plugin: 'com.android.library'
27
apply from: file(rootProject.file('module.gradle'))
38

@@ -8,11 +13,11 @@ android {
813
targetSdkVersion rootProject.ext.targetSdkVersion
914
externalNativeBuild {
1015
cmake {
11-
arguments "-DMODULE_NAME:STRING=riru_$moduleId",
16+
arguments "-DMODULE_NAME:STRING=$moduleLibraryName",
1217
"-DRIRU_MODULE_API_VERSION=$moduleRiruApiVersion",
1318
"-DRIRU_MODULE_VERSION=$moduleVersionCode",
14-
"-DRIRU_MODULE_VERSION_NAME:STRING=\"$moduleVersion\""
15-
19+
"-DRIRU_MODULE_VERSION_NAME:STRING=$moduleVersion",
20+
"-DRIRU_MODULE_MIN_API_VERSION=$moduleMinRiruApiVersion"
1621
}
1722
}
1823
}
@@ -29,8 +34,6 @@ android {
2934

3035
repositories {
3136
mavenLocal()
32-
jcenter()
33-
maven { url 'https://dl.bintray.com/rikkaw/Libraries' }
3437
}
3538

3639
dependencies {
@@ -39,104 +42,95 @@ dependencies {
3942
// you can copy this file from https://github.com/RikkaApps/Riru/blob/master/riru/src/main/cpp/include_riru/riru.h
4043

4144
// The default version of prefab in AGP has problem to process header only package,
42-
// you may have to add android.prefabVersion=1.1.2 in your gradle.properties.
45+
// you may have to add "android.prefabVersion" in your gradle.properties.
4346
// See https://github.com/google/prefab/issues/122
4447

45-
implementation 'rikka.ndk:riru:10'
48+
implementation 'dev.rikka.ndk:riru:26.0.0'
4649
}
4750

48-
def outDir = file("$rootDir/out")
49-
def magiskDir = file("$outDir/magisk_module")
50-
def zipName = "${magiskModuleProp['id'].replace('_', '-')}-${magiskModuleProp['version']}.zip"
51-
def riruDir = "$magiskDir/riru"
5251

52+
afterEvaluate {
53+
android.libraryVariants.forEach { variant ->
54+
def variantCapped = variant.name.capitalize()
55+
def variantLowered = variant.name.toLowerCase()
5356

54-
import org.apache.tools.ant.filters.FixCrLfFilter
57+
def zipName = "${magiskModuleId.replace('_', '-')}-${moduleVersion}-${variantLowered}.zip"
58+
def magiskDir = file("$outDir/magisk_module_$variantLowered")
5559

56-
import java.nio.file.Files
57-
import java.security.MessageDigest
60+
task("prepareMagiskFiles${variantCapped}", type: Sync) {
61+
dependsOn("assemble$variantCapped")
5862

59-
static def calcSha256(file) {
60-
def md = MessageDigest.getInstance("SHA-256")
61-
file.eachByte 4096, { bytes, size ->
62-
md.update(bytes, 0, size);
63-
}
64-
return md.digest().encodeHex()
65-
}
63+
def templatePath = "$rootDir/template/magisk_module"
6664

67-
static def renameOrFail(File from, File to) {
68-
if (!from.renameTo(to)) {
69-
throw new IOException("Unable reanme file $from to $to")
70-
}
71-
}
65+
into magiskDir
66+
from(templatePath) {
67+
exclude 'riru.sh', 'module.prop'
68+
}
69+
from(templatePath) {
70+
include 'riru.sh'
71+
filter(ReplaceTokens.class, tokens: [
72+
"RIRU_MODULE_LIB_NAME" : moduleLibraryName,
73+
"RIRU_MODULE_API_VERSION" : moduleRiruApiVersion.toString(),
74+
"RIRU_MODULE_MIN_API_VERSION" : moduleMinRiruApiVersion.toString(),
75+
"RIRU_MODULE_MIN_RIRU_VERSION_NAME": moduleMinRiruVersionName,
76+
])
77+
filter(FixCrLfFilter.class,
78+
eol: FixCrLfFilter.CrLf.newInstance("lf"))
79+
}
80+
from(templatePath) {
81+
include 'module.prop'
82+
expand([
83+
id : magiskModuleId,
84+
name : moduleName,
85+
version : moduleVersion,
86+
versionCode: moduleVersionCode.toString(),
87+
author : moduleAuthor,
88+
description: moduleDescription,
89+
])
90+
filter(FixCrLfFilter.class,
91+
eol: FixCrLfFilter.CrLf.newInstance("lf"))
92+
}
93+
from("$buildDir/intermediates/stripped_native_libs/$variantLowered/out/lib") {
94+
into 'lib'
95+
}
96+
doLast {
97+
fileTree("$magiskDir").visit { f ->
98+
if (f.directory) return
99+
if (f.file.name == '.gitattributes') return
100+
101+
def md = MessageDigest.getInstance("SHA-256")
102+
f.file.eachByte 4096, { bytes, size ->
103+
md.update(bytes, 0, size)
104+
}
105+
file(f.file.path + ".sha256sum").text = md.digest().encodeHex()
106+
}
107+
}
108+
}
72109

73-
android.libraryVariants.all { variant ->
74-
def task = variant.assembleProvider.get()
75-
task.doLast {
76-
// clear
77-
delete { delete magiskDir }
78-
79-
// copy from template
80-
copy {
81-
from "$rootDir/template/magisk_module"
82-
into magiskDir.path
83-
exclude 'riru.sh'
110+
task("zip${variantCapped}", type: Zip) {
111+
dependsOn("prepareMagiskFiles${variantCapped}")
112+
from magiskDir
113+
archiveName zipName
114+
destinationDir outDir
84115
}
85-
// copy riru.sh
86-
copy {
87-
from "$rootDir/template/magisk_module"
88-
into magiskDir.path
89-
include 'riru.sh'
90-
filter { line ->
91-
line.replaceAll('%%%RIRU_MODULE_ID%%%', moduleId)
92-
.replaceAll('%%%RIRU_MODULE_API_VERSION%%%', moduleRiruApiVersion.toString())
93-
.replaceAll('%%%RIRU_MODULE_MIN_API_VERSION%%%', moduleMinRiruApiVersion.toString())
94-
.replaceAll('%%%RIRU_MODULE_MIN_RIRU_VERSION_NAME%%%', moduleMinRiruVersionName)
95-
}
96-
filter(FixCrLfFilter.class,
97-
eol: FixCrLfFilter.CrLf.newInstance("lf"))
116+
117+
task("push${variantCapped}", type: Exec) {
118+
dependsOn("zip${variantCapped}")
119+
workingDir outDir
120+
commandLine android.adbExecutable, "push", zipName, "/data/local/tmp/"
121+
}
122+
123+
task("flash${variantCapped}", type: Exec) {
124+
dependsOn("push${variantCapped}")
125+
commandLine android.adbExecutable, "shell", "su", "-c",
126+
"magisk --install-module /data/local/tmp/${zipName}"
98127
}
99-
// copy .git files manually since gradle exclude it by default
100-
Files.copy(file("$rootDir/template/magisk_module/.gitattributes").toPath(), file("${magiskDir.path}/.gitattributes").toPath())
101-
102-
// generate module.prop
103-
def modulePropText = ""
104-
magiskModuleProp.each { k, v -> modulePropText += "$k=$v\n" }
105-
modulePropText = modulePropText.trim()
106-
file("$magiskDir/module.prop").text = modulePropText
107-
108-
// generate module.prop for Riru
109-
def riruModulePropText = ""
110-
moduleProp.each { k, v -> riruModulePropText += "$k=$v\n" }
111-
riruModulePropText = riruModulePropText.trim()
112-
file(riruDir).mkdirs()
113-
114-
// module.prop.new will be renamed to module.prop in post-fs-data.sh
115-
file("$riruDir/module.prop.new").text = riruModulePropText
116-
117-
// copy native files
118-
def nativeOutDir = file("build/intermediates/cmake/$variant.name/obj")
119-
120-
file("$magiskDir/system").mkdirs()
121-
file("$magiskDir/system_x86").mkdirs()
122-
renameOrFail(file("$nativeOutDir/arm64-v8a"), file("$magiskDir/system/lib64"))
123-
renameOrFail(file("$nativeOutDir/armeabi-v7a"), file("$magiskDir/system/lib"))
124-
renameOrFail(file("$nativeOutDir/x86_64"), file("$magiskDir/system_x86/lib64"))
125-
renameOrFail(file("$nativeOutDir/x86"), file("$magiskDir/system_x86/lib"))
126-
127-
// generate sha1sum
128-
fileTree("$magiskDir").matching {
129-
exclude "README.md", "META-INF"
130-
}.visit { f ->
131-
if (f.directory) return
132-
file(f.file.path + ".sha256sum").text = calcSha256(f.file)
128+
129+
task("flashAndReboot${variantCapped}", type: Exec) {
130+
dependsOn("flash${variantCapped}")
131+
commandLine android.adbExecutable, "shell", "reboot"
133132
}
133+
134+
variant.assembleProvider.get().finalizedBy("zip${variantCapped}")
134135
}
135-
task.finalizedBy zipMagiskMoudle
136136
}
137-
138-
task zipMagiskMoudle(type: Zip) {
139-
from magiskDir
140-
archiveName zipName
141-
destinationDir outDir
142-
}

‎module/src/main/cpp/CMakeLists.txt

+16-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.4.1)
22

33
if (NOT DEFINED MODULE_NAME)
44
message(FATAL_ERROR "MODULE_NAME is not set")
5+
else ()
6+
project(${MODULE_NAME})
57
endif ()
68

79
set(DobbyHome Dobby)
@@ -15,32 +17,38 @@ SET_OPTION(DOBBY_GENERATE_SHARED OFF)
1517
add_subdirectory(${DobbyHome} dobby)
1618

1719
add_definitions(-DRIRU_MODULE)
18-
add_definitions(-DRIRU_MODULE_API_VERSION=${RIRU_MODULE_API_VERSION})
19-
add_definitions(-DRIRU_MODULE_VERSION=${RIRU_MODULE_VERSION})
20-
add_definitions(-DRIRU_MODULE_VERSION_NAME=${RIRU_MODULE_VERSION_NAME})
20+
21+
configure_file(template/config.cpp config.cpp)
2122

2223
message("Build type: ${CMAKE_BUILD_TYPE}")
2324

2425
set(CMAKE_CXX_STANDARD 11)
2526

2627
set(LINKER_FLAGS "-ffixed-x18 -Wl,--hash-style=both")
2728
set(C_FLAGS "-Werror=format -fdata-sections -ffunction-sections")
29+
set(CXX_FLAGS "${CXX_FLAGS} -fno-exceptions -fno-rtti")
2830

29-
if (CMAKE_BUILD_TYPE STREQUAL "Release")
31+
if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
3032
set(C_FLAGS "${C_FLAGS} -O2 -fvisibility=hidden -fvisibility-inlines-hidden")
31-
set(LINKER_FLAGS "${LINKER_FLAGS} -Wl,-exclude-libs,ALL -Wl,--gc-sections")
33+
set(LINKER_FLAGS "${LINKER_FLAGS} -Wl,-exclude-libs,ALL -Wl,--gc-sections -Wl,--strip-all")
3234
else ()
3335
set(C_FLAGS "${C_FLAGS} -O0")
3436
endif ()
3537

3638
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_FLAGS}")
37-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${C_FLAGS}")
39+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${C_FLAGS} ${CXX_FLAGS}")
3840

3941
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAGS}")
4042
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINKER_FLAGS}")
4143

4244
find_package(riru REQUIRED CONFIG)
4345

44-
add_library(${MODULE_NAME} SHARED main.cpp hook.cpp il2cpp_dump.cpp)
46+
include_directories(include)
47+
48+
add_library(${MODULE_NAME} SHARED main.cpp ${CMAKE_CURRENT_BINARY_DIR}/config.cpp hook.cpp il2cpp_dump.cpp)
4549
target_link_libraries(${MODULE_NAME} log riru::riru dobby)
46-
set_target_properties(${MODULE_NAME} PROPERTIES LINK_FLAGS_RELEASE -s)
50+
51+
if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
52+
add_custom_command(TARGET ${MODULE_NAME} POST_BUILD
53+
COMMAND ${CMAKE_STRIP} --strip-all --remove-section=.comment "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib${MODULE_NAME}.so")
54+
endif ()

‎module/src/main/cpp/include/config.h

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
namespace riru {
4+
extern const int moduleVersionCode;
5+
extern const char* const moduleVersionName;
6+
extern const int moduleApiVersion;
7+
extern const int moduleMinApiVersion;
8+
}

‎module/src/main/cpp/main.cpp

+67-99
Original file line numberDiff line numberDiff line change
@@ -3,154 +3,122 @@
33
#include <riru.h>
44
#include <malloc.h>
55
#include <cstring>
6+
#include <config.h>
67
#include <pthread.h>
78
#include "hook.h"
89

910
static void forkAndSpecializePre(
10-
JNIEnv *env, jclass clazz, jint *_uid, jint *gid, jintArray *gids, jint *runtimeFlags,
11+
JNIEnv *env, jclass clazz, jint *uid, jint *gid, jintArray *gids, jint *runtimeFlags,
1112
jobjectArray *rlimits, jint *mountExternal, jstring *seInfo, jstring *niceName,
1213
jintArray *fdsToClose, jintArray *fdsToIgnore, jboolean *is_child_zygote,
13-
jstring *instructionSet, jstring *appDataDir, jboolean *isTopApp,
14-
jobjectArray *pkgDataInfoList,
15-
jobjectArray *whitelistedDataInfoList, jboolean *bindMountAppDataDirs,
16-
jboolean *bindMountAppStorageDirs) {
17-
enable_hack = isGame(env, *appDataDir);
14+
jstring *instructionSet, jstring *appDataDir, jboolean *isTopApp, jobjectArray *pkgDataInfoList,
15+
jobjectArray *whitelistedDataInfoList, jboolean *bindMountAppDataDirs, jboolean *bindMountAppStorageDirs) {
16+
// Called "before" com_android_internal_os_Zygote_nativeForkAndSpecialize in frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
17+
// Parameters are pointers, you can change the value of them if you want
18+
// Some parameters are not exist is older Android versions, in this case, they are null or 0
19+
enable_hack = isGame(env, *appDataDir);
1820
}
1921

2022
static void forkAndSpecializePost(JNIEnv *env, jclass clazz, jint res) {
23+
// Called "after" com_android_internal_os_Zygote_nativeForkAndSpecialize in frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
24+
// "res" is the return value of com_android_internal_os_Zygote_nativeForkAndSpecialize
25+
2126
if (res == 0) {
22-
// in app process
27+
// In app process
2328
if (enable_hack) {
2429
int ret;
2530
pthread_t ntid;
2631
if ((ret = pthread_create(&ntid, nullptr, hack_thread, nullptr))) {
2732
LOGE("can't create thread: %s\n", strerror(ret));
2833
}
2934
}
35+
// When unload allowed is true, the module will be unloaded (dlclose) by Riru
36+
// If this modules has hooks installed, DONOT set it to true, or there will be SIGSEGV
37+
// This value will be automatically reset to false before the "pre" function is called
38+
riru_set_unload_allowed(false);
3039
} else {
31-
// in zygote process, res is child pid
32-
// don't print log here, see https://github.com/RikkaApps/Riru/blob/77adfd6a4a6a81bfd20569c910bc4854f2f84f5e/riru-core/jni/main/jni_native_method.cpp#L55-L66
40+
// In zygote process
3341
}
3442
}
3543

3644
static void specializeAppProcessPre(
37-
JNIEnv *env, jclass clazz, jint *_uid, jint *gid, jintArray *gids, jint *runtimeFlags,
45+
JNIEnv *env, jclass clazz, jint *uid, jint *gid, jintArray *gids, jint *runtimeFlags,
3846
jobjectArray *rlimits, jint *mountExternal, jstring *seInfo, jstring *niceName,
3947
jboolean *startChildZygote, jstring *instructionSet, jstring *appDataDir,
4048
jboolean *isTopApp, jobjectArray *pkgDataInfoList, jobjectArray *whitelistedDataInfoList,
4149
jboolean *bindMountAppDataDirs, jboolean *bindMountAppStorageDirs) {
42-
// added from Android 10, but disabled at least in Google Pixel devices
50+
// Called "before" com_android_internal_os_Zygote_nativeSpecializeAppProcess in frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
51+
// Parameters are pointers, you can change the value of them if you want
52+
// Some parameters are not exist is older Android versions, in this case, they are null or 0
4353
}
4454

4555
static void specializeAppProcessPost(
4656
JNIEnv *env, jclass clazz) {
47-
// added from Android 10, but disabled at least in Google Pixel devices
57+
// Called "after" com_android_internal_os_Zygote_nativeSpecializeAppProcess in frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
58+
59+
// When unload allowed is true, the module will be unloaded (dlclose) by Riru
60+
// If this modules has hooks installed, DONOT set it to true, or there will be SIGSEGV
61+
// This value will be automatically reset to false before the "pre" function is called
62+
riru_set_unload_allowed(true);
4863
}
4964

5065
static void forkSystemServerPre(
5166
JNIEnv *env, jclass clazz, uid_t *uid, gid_t *gid, jintArray *gids, jint *runtimeFlags,
5267
jobjectArray *rlimits, jlong *permittedCapabilities, jlong *effectiveCapabilities) {
53-
68+
// Called "before" com_android_internal_os_Zygote_forkSystemServer in frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
69+
// Parameters are pointers, you can change the value of them if you want
70+
// Some parameters are not exist is older Android versions, in this case, they are null or 0
5471
}
5572

5673
static void forkSystemServerPost(JNIEnv *env, jclass clazz, jint res) {
74+
// Called "after" com_android_internal_os_Zygote_forkSystemServer in frameworks/base/core/jni/com_android_internal_os_Zygote.cpp
75+
5776
if (res == 0) {
58-
// in system server process
77+
// In system server process
5978
} else {
60-
// in zygote process, res is child pid
61-
// don't print log here, see https://github.com/RikkaApps/Riru/blob/77adfd6a4a6a81bfd20569c910bc4854f2f84f5e/riru-core/jni/main/jni_native_method.cpp#L55-L66
79+
// In zygote process
6280
}
6381
}
6482

65-
static int shouldSkipUid(int uid) {
66-
// by default, Riru only call module functions in "normal app processes" (10000 <= uid % 100000 <= 19999)
67-
// false = don't skip
68-
return false;
69-
}
70-
7183
static void onModuleLoaded() {
72-
// called when the shared library of Riru core is loaded
84+
// Called when this library is loaded and "hidden" by Riru (see Riru's hide.cpp)
85+
86+
// If you want to use threads, start them here rather than the constructors
87+
// __attribute__((constructor)) or constructors of static variables,
88+
// or the "hide" will cause SIGSEGV
7389
}
7490

7591
extern "C" {
7692

7793
int riru_api_version;
78-
RiruApiV9 *riru_api_v9;
79-
80-
/*
81-
* Init will be called three times.
82-
*
83-
* The first time:
84-
* Returns the highest version number supported by both Riru and the module.
85-
*
86-
* arg: (int *) Riru's API version
87-
* returns: (int *) the highest possible API version
88-
*
89-
* The second time:
90-
* Returns the RiruModuleX struct created by the module.
91-
* (X is the return of the first call)
92-
*
93-
* arg: (RiruApiVX *) RiruApi strcut, this pointer can be saved for further use
94-
* returns: (RiruModuleX *) RiruModule strcut
95-
*
96-
* The second time:
97-
* Let the module to cleanup (such as RiruModuleX struct created before).
98-
*
99-
* arg: null
100-
* returns: (ignored)
101-
*
102-
*/
103-
void *init(void *arg) {
104-
static int step = 0;
105-
step += 1;
106-
107-
static void *_module;
108-
109-
switch (step) {
110-
case 1: {
111-
auto core_max_api_version = *(int *) arg;
112-
riru_api_version =
113-
core_max_api_version <= RIRU_MODULE_API_VERSION ? core_max_api_version
114-
: RIRU_MODULE_API_VERSION;
115-
return &riru_api_version;
116-
}
117-
case 2: {
118-
switch (riru_api_version) {
119-
// RiruApiV10 and RiruModuleInfoV10 are equal to V9
120-
case 10:
121-
case 9: {
122-
riru_api_v9 = (RiruApiV9 *) arg;
123-
124-
auto module = (RiruModuleInfoV9 *) malloc(sizeof(RiruModuleInfoV9));
125-
memset(module, 0, sizeof(RiruModuleInfoV9));
126-
_module = module;
127-
128-
module->supportHide = true;
129-
130-
module->version = RIRU_MODULE_VERSION;
131-
module->versionName = RIRU_MODULE_VERSION_NAME;
132-
module->onModuleLoaded = onModuleLoaded;
133-
module->shouldSkipUid = shouldSkipUid;
134-
module->forkAndSpecializePre = forkAndSpecializePre;
135-
module->forkAndSpecializePost = forkAndSpecializePost;
136-
module->specializeAppProcessPre = specializeAppProcessPre;
137-
module->specializeAppProcessPost = specializeAppProcessPost;
138-
module->forkSystemServerPre = forkSystemServerPre;
139-
module->forkSystemServerPost = forkSystemServerPost;
140-
return module;
141-
}
142-
default: {
143-
return nullptr;
144-
}
145-
}
146-
}
147-
case 3: {
148-
free(_module);
149-
return nullptr;
150-
}
151-
default: {
152-
return nullptr;
94+
const char *riru_magisk_module_path = nullptr;
95+
int *riru_allow_unload = nullptr;
96+
97+
static auto module = RiruVersionedModuleInfo{
98+
.moduleApiVersion = riru::moduleApiVersion,
99+
.moduleInfo= RiruModuleInfo{
100+
.supportHide = true,
101+
.version = riru::moduleVersionCode,
102+
.versionName = riru::moduleVersionName,
103+
.onModuleLoaded = onModuleLoaded,
104+
.forkAndSpecializePre = forkAndSpecializePre,
105+
.forkAndSpecializePost = forkAndSpecializePost,
106+
.forkSystemServerPre = forkSystemServerPre,
107+
.forkSystemServerPost = forkSystemServerPost,
108+
.specializeAppProcessPre = specializeAppProcessPre,
109+
.specializeAppProcessPost = specializeAppProcessPost
153110
}
111+
};
112+
113+
RiruVersionedModuleInfo *init(Riru *riru) {
114+
auto core_max_api_version = riru->riruApiVersion;
115+
riru_api_version = core_max_api_version <= riru::moduleApiVersion ? core_max_api_version : riru::moduleApiVersion;
116+
module.moduleApiVersion = riru_api_version;
117+
118+
riru_magisk_module_path = strdup(riru->magiskModulePath);
119+
if (riru_api_version >= 25) {
120+
riru_allow_unload = riru->allowUnload;
154121
}
122+
return &module;
123+
}
155124
}
156-
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "config.h"
2+
3+
namespace riru {
4+
const int moduleVersionCode = ${RIRU_MODULE_VERSION};
5+
const char* const moduleVersionName = "${RIRU_MODULE_VERSION_NAME}";
6+
const int moduleApiVersion = ${RIRU_MODULE_API_VERSION};
7+
const int moduleMinApiVersion = ${RIRU_MODULE_MIN_API_VERSION};
8+
}

‎settings.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
include ':module'
2+
3+
import org.apache.tools.ant.DirectoryScanner
4+
5+
DirectoryScanner.removeDefaultExclude('**/.gitattributes')

‎template/magisk_module/META-INF/com/google/android/update-binary

+7-147
Original file line numberDiff line numberDiff line change
@@ -6,168 +6,28 @@
66

77
umask 022
88

9-
# Global vars
10-
TMPDIR=/dev/tmp
11-
PERSISTDIR=/sbin/.magisk/mirror/persist
12-
13-
rm -rf $TMPDIR 2>/dev/null
14-
mkdir -p $TMPDIR
15-
169
# echo before loading util_functions
1710
ui_print() { echo "$1"; }
1811

1912
require_new_magisk() {
2013
ui_print "*******************************"
21-
ui_print " Please install Magisk v19.0+! "
14+
ui_print " Please install Magisk v20.4+! "
2215
ui_print "*******************************"
2316
exit 1
2417
}
2518

26-
is_legacy_script() {
27-
unzip -l "$ZIPFILE" install.sh | grep -q install.sh
28-
return $?
29-
}
30-
31-
print_modname() {
32-
local len
33-
len=`echo -n $MODNAME | wc -c`
34-
len=$((len + 2))
35-
local pounds=`printf "%${len}s" | tr ' ' '*'`
36-
ui_print "$pounds"
37-
ui_print " $MODNAME "
38-
ui_print "$pounds"
39-
ui_print "*******************"
40-
ui_print " Powered by Magisk "
41-
ui_print "*******************"
42-
}
43-
44-
##############
45-
# Environment
46-
##############
19+
#########################
20+
# Load util_functions.sh
21+
#########################
4722

4823
OUTFD=$2
4924
ZIPFILE=$3
5025

5126
mount /data 2>/dev/null
5227

53-
# Load utility functions
5428
[ -f /data/adb/magisk/util_functions.sh ] || require_new_magisk
5529
. /data/adb/magisk/util_functions.sh
56-
[ $MAGISK_VER_CODE -gt 18100 ] || require_new_magisk
57-
58-
# Preperation for flashable zips
59-
setup_flashable
60-
61-
# Mount partitions
62-
mount_partitions
63-
64-
# Detect version and architecture
65-
api_level_arch_detect
66-
67-
# Setup busybox and binaries
68-
$BOOTMODE && boot_actions || recovery_actions
69-
70-
##############
71-
# Preparation
72-
##############
73-
74-
# Extract prop file
75-
unzip -o "$ZIPFILE" module.prop -d $TMPDIR >&2
76-
[ ! -f $TMPDIR/module.prop ] && abort "! Unable to extract zip file!"
77-
78-
$BOOTMODE && MODDIRNAME=modules_update || MODDIRNAME=modules
79-
MODULEROOT=$NVBASE/$MODDIRNAME
80-
MODID=`grep_prop id $TMPDIR/module.prop`
81-
MODPATH=$MODULEROOT/$MODID
82-
MODNAME=`grep_prop name $TMPDIR/module.prop`
83-
84-
# Create mod paths
85-
rm -rf $MODPATH 2>/dev/null
86-
mkdir -p $MODPATH
87-
88-
##########
89-
# Install
90-
##########
91-
92-
if is_legacy_script; then
93-
unzip -oj "$ZIPFILE" module.prop install.sh uninstall.sh 'common/*' -d $TMPDIR >&2
94-
95-
# Load install script
96-
. $TMPDIR/install.sh
97-
98-
# Callbacks
99-
print_modname
100-
on_install
101-
102-
# Custom uninstaller
103-
[ -f $TMPDIR/uninstall.sh ] && cp -af $TMPDIR/uninstall.sh $MODPATH/uninstall.sh
104-
105-
# Skip mount
106-
$SKIPMOUNT && touch $MODPATH/skip_mount
107-
108-
# prop file
109-
$PROPFILE && cp -af $TMPDIR/system.prop $MODPATH/system.prop
110-
111-
# Module info
112-
cp -af $TMPDIR/module.prop $MODPATH/module.prop
113-
114-
# post-fs-data scripts
115-
$POSTFSDATA && cp -af $TMPDIR/post-fs-data.sh $MODPATH/post-fs-data.sh
116-
117-
# service scripts
118-
$LATESTARTSERVICE && cp -af $TMPDIR/service.sh $MODPATH/service.sh
119-
120-
ui_print "- Setting permissions"
121-
set_permissions
122-
else
123-
print_modname
124-
125-
unzip -o "$ZIPFILE" customize.sh -d $MODPATH >&2
126-
127-
if ! grep -q '^SKIPUNZIP=1$' $MODPATH/customize.sh 2>/dev/null; then
128-
ui_print "- Extracting module files"
129-
unzip -o "$ZIPFILE" -x 'META-INF/*' -d $MODPATH >&2
130-
131-
# Default permissions
132-
set_perm_recursive $MODPATH 0 0 0755 0644
133-
fi
134-
135-
# Load customization script
136-
[ -f $MODPATH/customize.sh ] && . $MODPATH/customize.sh
137-
fi
138-
139-
# Handle replace folders
140-
for TARGET in $REPLACE; do
141-
ui_print "- Replace target: $TARGET"
142-
mktouch $MODPATH$TARGET/.replace
143-
done
144-
145-
if $BOOTMODE; then
146-
# Update info for Magisk Manager
147-
mktouch $NVBASE/modules/$MODID/update
148-
cp -af $MODPATH/module.prop $NVBASE/modules/$MODID/module.prop
149-
fi
150-
151-
# Copy over custom sepolicy rules
152-
if [ -f $MODPATH/sepolicy.rule -a -e $PERSISTDIR ]; then
153-
ui_print "- Installing custom sepolicy patch"
154-
PERSISTMOD=$PERSISTDIR/magisk/$MODID
155-
mkdir -p $PERSISTMOD
156-
cp -af $MODPATH/sepolicy.rule $PERSISTMOD/sepolicy.rule
157-
fi
158-
159-
# Remove stuffs that don't belong to modules
160-
rm -rf \
161-
$MODPATH/system/placeholder $MODPATH/customize.sh \
162-
$MODPATH/README.md $MODPATH/.git* 2>/dev/null
163-
164-
##############
165-
# Finalizing
166-
##############
167-
168-
cd /
169-
$BOOTMODE || recovery_cleanup
170-
rm -rf $TMPDIR
30+
[ $MAGISK_VER_CODE -lt 20400 ] && require_new_magisk
17131

172-
ui_print "- Done"
173-
exit 0
32+
install_module
33+
exit 0

‎template/magisk_module/customize.sh

+40-33
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
SKIPUNZIP=1
22

3-
# check_architecture
4-
if [ "$ARCH" != "arm" ] && [ "$ARCH" != "arm64" ] && [ "$ARCH" != "x86" ] && [ "$ARCH" != "x64" ]; then
5-
abort "! Unsupported platform: $ARCH"
6-
else
7-
ui_print "- Device platform: $ARCH"
8-
fi
9-
10-
# extract verify.sh
3+
# Extract verify.sh
114
ui_print "- Extracting verify.sh"
125
unzip -o "$ZIPFILE" 'verify.sh' -d "$TMPDIR" >&2
136
if [ ! -f "$TMPDIR/verify.sh" ]; then
@@ -18,46 +11,60 @@ if [ ! -f "$TMPDIR/verify.sh" ]; then
1811
fi
1912
. $TMPDIR/verify.sh
2013

21-
# extract riru.sh
22-
extract "$ZIPFILE" 'riru.sh' "$MODPATH"
23-
. $MODPATH/riru.sh
14+
# Extract riru.sh
15+
16+
# Variables provided by riru.sh:
17+
#
18+
# RIRU_API: API version of installed Riru, 0 if not installed
19+
# RIRU_MIN_COMPATIBLE_API: minimal supported API version by installed Riru, 0 if not installed or version < v23.2
20+
# RIRU_VERSION_CODE: version code of installed Riru, 0 if not installed or version < v23.2
21+
# RIRU_VERSION_NAME: version name of installed Riru, "" if not installed or version < v23.2
22+
23+
extract "$ZIPFILE" 'riru.sh' "$TMPDIR"
24+
. $TMPDIR/riru.sh
2425

26+
# Functions from util_functions.sh (it will be loaded by riru.sh)
2527
check_riru_version
28+
enforce_install_from_magisk_app
2629

27-
# extract libs
30+
# Check architecture
31+
if [ "$ARCH" != "arm" ] && [ "$ARCH" != "arm64" ] && [ "$ARCH" != "x86" ] && [ "$ARCH" != "x64" ]; then
32+
abort "! Unsupported platform: $ARCH"
33+
else
34+
ui_print "- Device platform: $ARCH"
35+
fi
36+
37+
# Extract libs
2838
ui_print "- Extracting module files"
2939

3040
extract "$ZIPFILE" 'module.prop' "$MODPATH"
31-
extract "$ZIPFILE" 'post-fs-data.sh' "$MODPATH"
3241
extract "$ZIPFILE" 'uninstall.sh' "$MODPATH"
33-
#extract "$ZIPFILE" 'sepolicy.rule' "$MODPATH"
3442

35-
if [ "$ARCH" = "x86" ] || [ "$ARCH" = "x64" ]; then
36-
ui_print "- Extracting x86 libraries"
37-
extract "$ZIPFILE" "system_x86/lib/libriru_$RIRU_MODULE_ID.so" "$MODPATH"
38-
mv "$MODPATH/system_x86" "$MODPATH/system"
43+
# Riru v24+ load files from the "riru" folder in the Magisk module folder
44+
# This "riru" folder is also used to determine if a Magisk module is a Riru module
3945

40-
if [ "$IS64BIT" = true ]; then
41-
ui_print "- Extracting x64 libraries"
42-
extract "$ZIPFILE" "system_x86/lib64/libriru_$RIRU_MODULE_ID.so" "$MODPATH"
43-
mv "$MODPATH/system_x86/lib64" "$MODPATH/system/lib64"
44-
fi
45-
else
46+
mkdir "$MODPATH/riru"
47+
mkdir "$MODPATH/riru/lib"
48+
mkdir "$MODPATH/riru/lib64"
49+
50+
if [ "$ARCH" = "arm" ] || [ "$ARCH" = "arm64" ]; then
4651
ui_print "- Extracting arm libraries"
47-
extract "$ZIPFILE" "system/lib/libriru_$RIRU_MODULE_ID.so" "$MODPATH"
52+
extract "$ZIPFILE" "lib/armeabi-v7a/lib$RIRU_MODULE_LIB_NAME.so" "$MODPATH/riru/lib" true
4853

4954
if [ "$IS64BIT" = true ]; then
5055
ui_print "- Extracting arm64 libraries"
51-
extract "$ZIPFILE" "system/lib64/libriru_$RIRU_MODULE_ID.so" "$MODPATH"
56+
extract "$ZIPFILE" "lib/arm64-v8a/lib$RIRU_MODULE_LIB_NAME.so" "$MODPATH/riru/lib64" true
5257
fi
5358
fi
5459

55-
set_perm_recursive "$MODPATH" 0 0 0755 0644
60+
if [ "$ARCH" = "x86" ] || [ "$ARCH" = "x64" ]; then
61+
ui_print "- Extracting x86 libraries"
62+
extract "$ZIPFILE" "lib/x86/lib$RIRU_MODULE_LIB_NAME.so" "$MODPATH/riru/lib" true
5663

57-
# extract Riru files
58-
ui_print "- Extracting extra files"
59-
[ -d "$RIRU_MODULE_PATH" ] || mkdir -p "$RIRU_MODULE_PATH" || abort "! Can't create $RIRU_MODULE_PATH"
64+
if [ "$IS64BIT" = true ]; then
65+
ui_print "- Extracting x64 libraries"
66+
extract "$ZIPFILE" "lib/x86_64/lib$RIRU_MODULE_LIB_NAME.so" "$MODPATH/riru/lib64" true
67+
fi
68+
fi
6069

61-
rm -f "$RIRU_MODULE_PATH/module.prop.new"
62-
extract "$ZIPFILE" 'riru/module.prop.new' "$RIRU_MODULE_PATH" true
63-
set_perm "$RIRU_MODULE_PATH/module.prop.new" 0 0 0600 $RIRU_SECONTEXT
70+
set_perm_recursive "$MODPATH" 0 0 0755 0644

‎template/magisk_module/module.prop

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
id=${id}
2+
name=${name}
3+
version=${version}
4+
versionCode=${versionCode}
5+
author=${author}
6+
description=${description}

‎template/magisk_module/post-fs-data.sh

-10
This file was deleted.

‎template/magisk_module/riru.sh

+39-31
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
11
#!/sbin/sh
2-
RIRU_PATH="/data/adb/riru"
3-
RIRU_MODULE_ID="%%%RIRU_MODULE_ID%%%"
4-
RIRU_MODULE_PATH="$RIRU_PATH/modules/$RIRU_MODULE_ID"
5-
RIRU_SECONTEXT="u:object_r:magisk_file:s0"
2+
RIRU_MODULE_LIB_NAME="@RIRU_MODULE_LIB_NAME@"
63

7-
# used by /data/adb/riru/util_functions.sh
8-
RIRU_MODULE_API_VERSION=%%%RIRU_MODULE_API_VERSION%%%
9-
RIRU_MODULE_MIN_API_VERSION=%%%RIRU_MODULE_MIN_API_VERSION%%%
10-
RIRU_MODULE_MIN_RIRU_VERSION_NAME="%%%RIRU_MODULE_MIN_RIRU_VERSION_NAME%%%"
4+
# Variables for customize.sh
5+
RIRU_API=0
6+
RIRU_MIN_COMPATIBLE_API=0
7+
RIRU_VERSION_CODE=0
8+
RIRU_VERSION_NAME=""
119

12-
# this function will be used when /data/adb/riru/util_functions.sh not exits
13-
check_riru_version() {
14-
if [ ! -f "$RIRU_PATH/api_version" ] && [ ! -f "$RIRU_PATH/api_version.new" ]; then
15-
ui_print "*********************************************************"
16-
ui_print "! Riru $RIRU_MIN_VERSION_NAME or above is required"
17-
ui_print "! Please install Riru from Magisk Manager or https://github.com/RikkaApps/Riru/releases"
18-
abort "*********************************************************"
19-
fi
20-
local_api_version=$(cat "$RIRU_PATH/api_version.new") || local_api_version=$(cat "$RIRU_PATH/api_version") || local_api_version=0
21-
[ "$local_api_version" -eq "$local_api_version" ] || local_api_version=0
22-
ui_print "- Riru API version: $local_api_version"
23-
if [ "$local_api_version" -lt $RIRU_MODULE_MIN_API_VERSION ]; then
24-
ui_print "*********************************************************"
25-
ui_print "! Riru $RIRU_MIN_VERSION_NAME or above is required"
26-
ui_print "! Please upgrade Riru from Magisk Manager or https://github.com/RikkaApps/Riru/releases"
27-
abort "*********************************************************"
28-
fi
29-
}
10+
# Used by util_functions.sh
11+
RIRU_MODULE_API_VERSION=@RIRU_MODULE_API_VERSION@
12+
RIRU_MODULE_MIN_API_VERSION=@RIRU_MODULE_MIN_API_VERSION@
13+
RIRU_MODULE_MIN_RIRU_VERSION_NAME="@RIRU_MODULE_MIN_RIRU_VERSION_NAME@"
3014

31-
if [ -f /data/adb/riru/util_functions.sh ]; then
32-
ui_print "- Load /data/adb/riru/util_functions.sh"
33-
. /data/adb/riru/util_functions.sh
15+
if [ "$MAGISK_VER_CODE" -ge 21000 ]; then
16+
MAGISK_CURRENT_RIRU_MODULE_PATH=$(magisk --path)/.magisk/modules/riru-core
3417
else
35-
ui_print "- Can't find /data/adb/riru/util_functions.sh"
36-
fi
18+
MAGISK_CURRENT_RIRU_MODULE_PATH=/sbin/.magisk/modules/riru-core
19+
fi
20+
21+
if [ ! -d $MAGISK_CURRENT_RIRU_MODULE_PATH ]; then
22+
ui_print "*********************************************************"
23+
ui_print "! Riru is not installed"
24+
ui_print "! Please install Riru from Magisk Manager or https://github.com/RikkaApps/Riru/releases"
25+
abort "*********************************************************"
26+
fi
27+
28+
if [ -f "$MAGISK_CURRENT_RIRU_MODULE_PATH/disable" ] || [ -f "$MAGISK_CURRENT_RIRU_MODULE_PATH/remove" ]; then
29+
ui_print "*********************************************************"
30+
ui_print "! Riru is not enabled or will be removed"
31+
ui_print "! Please enable Riru in Magisk first"
32+
abort "*********************************************************"
33+
fi
34+
35+
if [ -f $MAGISK_CURRENT_RIRU_MODULE_PATH/util_functions.sh ]; then
36+
ui_print "- Load $MAGISK_CURRENT_RIRU_MODULE_PATH/util_functions.sh"
37+
# shellcheck disable=SC1090
38+
. $MAGISK_CURRENT_RIRU_MODULE_PATH/util_functions.sh
39+
else
40+
ui_print "*********************************************************"
41+
ui_print "! Riru $RIRU_MODULE_MIN_RIRU_VERSION_NAME or above is required"
42+
ui_print "! Please upgrade Riru from Magisk Manager or https://github.com/RikkaApps/Riru/releases"
43+
abort "*********************************************************"
44+
fi

‎template/magisk_module/uninstall.sh

-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
11
#!/sbin/sh
22
MODDIR=${0%/*}
3-
[ ! -f "$MODDIR/riru.sh" ] && exit 1
4-
. $MODDIR/riru.sh
5-
6-
rm -rf "$RIRU_MODULE_PATH"

‎template/magisk_module/verify.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ extract() {
3636

3737
(echo "$(cat "$hash_path") $file_path" | sha256sum -c -s -) || abort_verify "Failed to verify $file"
3838
ui_print "- Verified $file" >&1
39-
}
39+
}

0 commit comments

Comments
 (0)
Please sign in to comment.