Skip to content

Commit 720ad26

Browse files
committed
fix: refactor HookItem loading logic and enhance WeDatabaseApi initialization
- Fix `HookQueryCashierPkg` and `CONTRIBUTING.md` examples to ensure `super.unload()` is called. - Refactor `HookItemLoader` and UI dialogs to remove redundant `startLoad()` calls, centralizing lifecycle management. - Enhance `WeDatabaseApi` with reactive initialization by hooking the storage getter instead of proactive warming. - Improve `WeDatabaseListener` logging with chunked output and detailed argument inspection. - Add `logChunkedD` to `WeLogger` for handling large debug logs. - Update `BaseHookItem` to include a stack trace when skipping redundant loads for better debugging.
1 parent 049fe08 commit 720ad26

10 files changed

Lines changed: 32 additions & 34 deletions

File tree

CONTRIBUTING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,6 +2227,7 @@ override fun entry(classLoader: ClassLoader) {
22272227
```kotlin
22282228
override fun unload(classLoader: ClassLoader) {
22292229
WePkgManager.removeInterceptor(this)
2230+
super.unload(classLoader)
22302231
}
22312232
```
22322233

@@ -2402,6 +2403,7 @@ class HookQueryCashierPkg : BaseClickableFunctionHookItem(), IWePkgInterceptor {
24022403
override fun unload(classLoader: ClassLoader) {
24032404
// 卸载时移除拦截器
24042405
WePkgManager.removeInterceptor(this)
2406+
super.unload(classLoader)
24052407
}
24062408

24072409
override fun onClick(context: Context?) {
@@ -2461,6 +2463,7 @@ class MyHook : BaseHookItem(), IWePkgInterceptor {
24612463
override fun unload(classLoader: ClassLoader) {
24622464
// ✅ 必须在卸载时移除拦截器
24632465
WePkgManager.removeInterceptor(this)
2466+
super.unload(classLoader)
24642467
}
24652468
}
24662469
```

app/src/main/java/moe/ouom/wekit/core/model/BaseClickableFunctionHookItem.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ public void setToggleCompletionCallback(Runnable callback) {
3535
* @param newState 新的状态 (true: 启用, false: 禁用)
3636
*/
3737
public void applyToggle(boolean newState) {
38-
// 1. 保存配置
38+
// 保存配置
3939
String configKey = Constants.PrekClickableXXX + this.getPath();
4040
WeConfig.getDefaultConfig().edit().putBoolean(configKey, newState).apply();
4141

42-
// 2. 更新状态
42+
// 更新状态
4343
this.setEnabled(newState);
4444

45-
// 3. 触发UI更新回调
45+
// 触发UI更新回调
4646
if (toggleCompletionCallback != null) {
4747
toggleCompletionCallback.run();
4848
}

app/src/main/java/moe/ouom/wekit/core/model/BaseHookItem.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ abstract class BaseHookItem {
7878

7979
if (isLoad) {
8080
if (verboseLog) {
81-
WeLogger.w("BaseHookItem.startLoad() skipped - already loaded for ${this::class.java.simpleName}")
81+
WeLogger.w("BaseHookItem.startLoad() skipped - already loaded for ${this::class.java.simpleName}. \nCall Stack: ${Exception().stackTraceToString()}")
8282
}
8383
return
8484
}

app/src/main/java/moe/ouom/wekit/core/model/BaseSwitchFunctionHookItem.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public void setToggleCompletionCallback(Runnable callback) {
3434
* @param newState 新的状态 (true: 启用, false: 禁用)
3535
*/
3636
public void applyToggle(boolean newState) {
37-
// 1. 保存配置
37+
// 保存配置
3838
String configKey = Constants.PrekXXX + this.getPath();
3939
WeConfig.getDefaultConfig().edit().putBoolean(configKey, newState).apply();
4040

41-
// 2. 更新状态
41+
// 更新状态
4242
this.setEnabled(newState);
4343

44-
// 3. 触发UI更新回调
44+
// 触发UI更新回调
4545
if (toggleCompletionCallback != null) {
4646
toggleCompletionCallback.run();
4747
}

app/src/main/java/moe/ouom/wekit/hooks/core/HookItemLoader.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,9 @@ class HookItemLoader {
202202
when (hookItem) {
203203
is BaseSwitchFunctionHookItem -> {
204204
WeLogger.i("HookItemLoader", "[Switch] Init ${hookItem.path}")
205-
hookItem.startLoad()
206205
}
207206
is BaseClickableFunctionHookItem -> {
208207
WeLogger.i("HookItemLoader", "[Clickable] Init ${hookItem.path}")
209-
hookItem.startLoad()
210208
}
211209
is ApiHookItem -> {
212210
WeLogger.i("HookItemLoader", "[API] Init ${hookItem.path}")

app/src/main/java/moe/ouom/wekit/hooks/item/chat/risk/HookQueryCashierPkg.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class HookQueryCashierPkg : BaseClickableFunctionHookItem(), IWePkgInterceptor {
147147

148148
override fun unload(classLoader: ClassLoader) {
149149
WePkgManager.removeInterceptor(this)
150+
super.unload(classLoader)
150151
}
151152

152153
override fun onClick(context: Context?) {

app/src/main/java/moe/ouom/wekit/hooks/sdk/api/WeDatabaseApi.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,15 @@ class WeDatabaseApi : ApiHookItem(), IDexFind {
8484
INSTANCE = this
8585
getStorageMethod = dexMethodGetStorage.method
8686

87-
// 尝试预热数据库引用
88-
initializeDatabase()
87+
if (getStorageMethod != null) {
88+
hookAfter(getStorageMethod!!) { param ->
89+
val storageObj = param.result ?: return@hookAfter
90+
91+
if (wcdbInstance == null) {
92+
initializeDatabase(storageObj)
93+
}
94+
}
95+
}
8996

9097
} catch (e: Exception) {
9198
WeLogger.e(TAG, "Entry 初始化异常", e)
@@ -96,15 +103,10 @@ class WeDatabaseApi : ApiHookItem(), IDexFind {
96103
* 核心初始化逻辑
97104
*/
98105
@Synchronized
99-
private fun initializeDatabase(): Boolean {
106+
private fun initializeDatabase(storageObj: Any): Boolean {
100107
if (wcdbInstance != null && rawQueryMethod != null) return true
101108

102109
try {
103-
// 获取 Storage 实例
104-
val storageObj = getStorageMethod?.invoke(null) ?: run {
105-
return false
106-
}
107-
108110
// 在 Storage 中寻找 Wrapper
109111
val wrapperObj = findDbWrapper(storageObj)
110112
if (wrapperObj == null) {
@@ -124,7 +126,6 @@ class WeDatabaseApi : ApiHookItem(), IDexFind {
124126
if (rawQuery != null) {
125127
wcdbInstance = dbInstance
126128
rawQueryMethod = rawQuery
127-
WeLogger.i(TAG, "数据库 API 就绪")
128129
return true
129130
}
130131

@@ -212,7 +213,6 @@ class WeDatabaseApi : ApiHookItem(), IDexFind {
212213
*/
213214
fun executeQuery(sql: String): List<Map<String, Any?>> {
214215
val result = mutableListOf<Map<String, Any?>>()
215-
if (!initializeDatabase()) return result
216216

217217
var cursor: Cursor? = null
218218
try {
@@ -328,7 +328,7 @@ class WeDatabaseApi : ApiHookItem(), IDexFind {
328328
val roomResult = executeQuery(roomSql)
329329

330330
if (roomResult.isEmpty()) {
331-
WeLogger.w(TAG, "未找到群聊信息: $chatroomId (可能未保存到通讯录)")
331+
WeLogger.w(TAG, "未找到群聊信息: $chatroomId")
332332
return emptyList()
333333
}
334334

app/src/main/java/moe/ouom/wekit/hooks/sdk/api/WeDatabaseListener.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,12 @@ class WeDatabaseListener : ApiHookItem() {
6969
if (listeners.isNotEmpty()) {
7070
if (verboseLog) {
7171
if (dbVerboseLog) {
72-
// 输出完整的 ContentValues
73-
WeLogger.d("WeDatabaseApi: 分发数据库插入事件 table=$table${listeners.size} 个监听器, values=$values")
72+
val argsInfo = param.args.mapIndexed { index, arg ->
73+
"arg[$index](${arg?.javaClass?.simpleName ?: "null"})=$arg"
74+
}.joinToString(", ")
75+
val result = param.result
76+
77+
WeLogger.logChunkedD("WeDatabaseApi","[Insert] table=$table, result=$result, args=[$argsInfo]")
7478
}
7579
}
7680
listeners.forEach { it.onInsert(table, values) }

app/src/main/java/moe/ouom/wekit/ui/creator/dialog/CategorySettingsDialog.kt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class CategorySettingsDialog(
7575
// 允许切换,保存配置并更新状态
7676
WeConfig.getDefaultConfig().edit().putBoolean(configKey, checked).apply()
7777
item.isEnabled = checked
78-
if (checked) item.startLoad()
7978
}
8079
listenerRef = listener
8180

@@ -135,17 +134,6 @@ class CategorySettingsDialog(
135134
// 允许切换,保存配置并更新状态
136135
WeConfig.getDefaultConfig().edit().putBoolean(configKey, checked).apply()
137136
item.isEnabled = checked
138-
if (checked) {
139-
WeLogger.i("[CategorySettings] Loading HookItem: ${item.path}")
140-
item.startLoad()
141-
} else {
142-
WeLogger.i("[CategorySettings] Unloading HookItem: ${item.path}")
143-
try {
144-
item.unload(context.classLoader)
145-
} catch (e: Throwable) {
146-
WeLogger.e("[CategorySettings] Unload HookItem Failed", e)
147-
}
148-
}
149137
}
150138
listenerRef = listener
151139

app/src/main/java/moe/ouom/wekit/util/log/WeLogger.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,4 +410,8 @@ public static void logChunkedE(@NonNull String tag, @NonNull String msg) {
410410
public static void logChunkedW(@NonNull String tag, @NonNull String msg) {
411411
logChunked(Log.WARN, tag, msg);
412412
}
413+
414+
public static void logChunkedD(@NonNull String tag, @NonNull String msg) {
415+
logChunked(Log.DEBUG, tag, msg);
416+
}
413417
}

0 commit comments

Comments
 (0)