Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: DisableUtfControlCharacterInNickname #1161

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
package wang.allenyou.hook

import android.annotation.SuppressLint
import android.graphics.Canvas
import android.widget.EditText
import android.graphics.Rect
import android.text.method.TransformationMethod
import android.view.View
import android.widget.TextView
import cc.ioctl.util.hookBeforeIfEnabled
import com.github.kyuubiran.ezxhelper.utils.hookAllConstructorAfter
import io.github.qauxv.base.annotation.FunctionHookEntry
import io.github.qauxv.base.annotation.UiItemAgentEntry
import io.github.qauxv.dsl.FunctionEntryRouter
Expand All @@ -47,24 +49,33 @@ object DisableUtfControlCharacterInNicknameHook : CommonSwitchFunctionHook() {

override val isAvailable: Boolean get() = requireMinQQVersion(QQVersion.QQ_9_0_20)

private class HookTransformationMethod(val originalMethod: TransformationMethod) : TransformationMethod {
override fun getTransformation(source: CharSequence?, view: View?): CharSequence {
if (source == null) {
return originalMethod.getTransformation(null, view)
}
return originalMethod.getTransformation(filterControlCharacter(source), view)
}

override fun onFocusChanged(view: View?, sourceText: CharSequence?, focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
originalMethod.onFocusChanged(view, sourceText, focused, direction, previouslyFocusedRect)
}
}

@SuppressLint("BidiSpoofing")
private const val BLACKLIST = "‭‮‪‫‎⁦⁧‏"

override fun initOnce(): Boolean {
val textViewClass = Initiator.loadClass("android.widget.TextView")
val onDrawMethod = textViewClass.getDeclaredMethod("onDraw", Canvas::class.java)
hookBeforeIfEnabled(onDrawMethod) {
if (it.thisObject !is TextView) {
return@hookBeforeIfEnabled
}
if (it.thisObject is EditText) {
return@hookBeforeIfEnabled
}
val str = (it.thisObject as TextView).text.toString()
if (BLACKLIST.none { ch -> str.contains(ch) }) {
return@hookBeforeIfEnabled
val setTransformationMethod = textViewClass.getDeclaredMethod("setTransformationMethod", TransformationMethod::class.java)
hookBeforeIfEnabled(setTransformationMethod) {
it.args[0] = HookTransformationMethod(it.args[0] as TransformationMethod)
}
textViewClass.hookAllConstructorAfter {
val original = (it.thisObject as TextView).transformationMethod
if (original != null) {
(it.thisObject as TextView).transformationMethod = HookTransformationMethod(original)
}
(it.thisObject as TextView).text = filterControlCharacter(str)
}
return true
}
Expand Down
Loading