1
0

Add plugin check #3

This commit is contained in:
chinosk 2024-06-13 21:16:08 +08:00
parent 4247e4ba29
commit d92831443f
No known key found for this signature in database
GPG Key ID: 00610B08C1BF7BE9
4 changed files with 119 additions and 15 deletions

@ -1 +1 @@
Subproject commit e301986ec94c69a772f3c40a981f05a5eb2d9cb1
Subproject commit e97c861d73668fdaa00c4aded70b18ead28fee02

View File

@ -204,7 +204,10 @@ namespace GakumasLocal::Local {
static auto genericFile = GetBasePath() / "local-files" / "generic.json";
static auto genericDir = GetBasePath() / "local-files" / "genericTrans";
if (!exists(localizationFile)) return;
if (!std::filesystem::is_regular_file(localizationFile)) {
Log::ErrorFmt("localizationFile: %s not found.", localizationFile.c_str());
return;
}
LoadJsonDataToMap(localizationFile, i18nData, true);
Log::InfoFmt("%ld localization items loaded.", i18nData.size());

View File

@ -2,10 +2,12 @@ package io.github.chinosk.gakumas.localify
import android.annotation.SuppressLint
import android.app.Activity
import android.app.AlertDialog
import android.app.AndroidAppHelper
import android.content.Context
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.util.Log
@ -23,6 +25,7 @@ import com.google.gson.Gson
import de.robv.android.xposed.XposedBridge
import io.github.chinosk.gakumas.localify.models.GakumasConfig
import java.io.File
import java.util.Locale
val TAG = "GakumasLocalify"
@ -35,6 +38,8 @@ class GakumasHookMain : IXposedHookLoadPackage, IXposedHookZygoteInit {
private var gkmsDataInited = false
private var getConfigError: Exception? = null
override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) {
if (lpparam.packageName != targetPackageName) {
return
@ -62,16 +67,26 @@ class GakumasHookMain : IXposedHookLoadPackage, IXposedHookZygoteInit {
super.beforeHookedMethod(param)
Log.d(TAG, "onStart")
val currActivity = param.thisObject as Activity
if (getConfigError != null) {
showGetConfigFailed(currActivity)
}
else {
initGkmsConfig(currActivity)
}
}
})
XposedBridge.hookAllMethods(appActivityClass, "onResume", object : XC_MethodHook() {
override fun beforeHookedMethod(param: MethodHookParam) {
Log.d(TAG, "onResume")
val currActivity = param.thisObject as Activity
if (getConfigError != null) {
showGetConfigFailed(currActivity)
}
else {
initGkmsConfig(currActivity)
}
}
})
val cls = lpparam.classLoader.loadClass("com.unity3d.player.UnityPlayer")
@ -92,10 +107,10 @@ class GakumasHookMain : IXposedHookLoadPackage, IXposedHookZygoteInit {
val app = AndroidAppHelper.currentApplication()
if (nativeLibLoadSuccess) {
showToast("lib$nativeLibName.so 已加载")
showToast("lib$nativeLibName.so loaded.")
}
else {
showToast("加载 native 库 lib$nativeLibName.so 失败")
showToast("Load native library lib$nativeLibName.so failed.")
return
}
@ -137,15 +152,99 @@ class GakumasHookMain : IXposedHookLoadPackage, IXposedHookZygoteInit {
}
}
private fun showGetConfigFailedImpl(activity: Context, title: String, msg: String, infoButton: String, dlButton: String, okButton: String) {
if (getConfigError == null) return
val builder = AlertDialog.Builder(activity)
val infoBuilder = AlertDialog.Builder(activity)
val errConfigStr = getConfigError.toString()
builder.setTitle("$title: $errConfigStr")
getConfigError = null
builder.setCancelable(false)
builder.setMessage(msg)
builder.setPositiveButton(okButton) { dialog, _ ->
dialog.dismiss()
}
builder.setNegativeButton(dlButton) { dialog, _ ->
dialog.dismiss()
val webpage = Uri.parse("https://github.com/chinosk6/gakuen-imas-localify")
val intent = Intent(Intent.ACTION_VIEW, webpage)
activity.startActivity(intent)
}
builder.setNeutralButton(infoButton) { _, _ ->
infoBuilder.setTitle("Error Info")
infoBuilder.setMessage(errConfigStr)
val infoDialog = infoBuilder.create()
infoDialog.show()
}
val dialog = builder.create()
infoBuilder.setOnCancelListener {
dialog.show()
}
dialog.show()
}
fun showGetConfigFailed(activity: Context) {
val langData = when (getCurrentLanguage(activity)) {
"zh" -> {
mapOf(
"title" to "无法读取设置",
"message" to "配置读取失败,将使用默认配置。\n" +
"可能是您使用了 LSPatch 等工具的集成模式,也有可能是您拒绝了拉起插件的权限。\n" +
"若您使用了 LSPatch 等工具的集成模式,且没有单独安装插件本体,请下载插件本体。\n" +
"若您安装了插件本体,却弹出这个错误,请允许本应用拉起其他应用。",
"infoButton" to "详情",
"dlButton" to "下载",
"okButton" to "确定"
)
}
else -> {
mapOf(
"title" to "Get Config Failed",
"message" to "Configuration loading failed, the default configuration will be used.\n" +
"This might be due to the use the integration mode of LSPatch, or possibly because you denied the permission to launch the plugin.\n" +
"If you used the integration mode of LSPatch and did not install the plugin itself separately, please download the plugin.\n" +
"If you have installed the plugin but still see this error, please allow this application to launch other applications.",
"infoButton" to "Info",
"dlButton" to "Download",
"okButton" to "OK"
)
}
}
showGetConfigFailedImpl(activity, langData["title"]!!, langData["message"]!!, langData["infoButton"]!!,
langData["dlButton"]!!, langData["okButton"]!!)
}
private fun getCurrentLanguage(context: Context): String {
val locale: Locale = context.resources.configuration.locales.get(0)
return locale.language
}
fun requestConfig(activity: Context) {
try {
val intent = Intent().apply {
setClassName("io.github.chinosk.gakumas.localify", "io.github.chinosk.gakumas.localify.MainActivity")
putExtra("gkmsData", "芜湖")
putExtra("gkmsData", "requestConfig")
flags = FLAG_ACTIVITY_NEW_TASK
}
// activity.startActivityForResult(intent, 114514)
activity.startActivity(intent)
}
catch (e: Exception) {
getConfigError = e
val fakeActivity = Activity().apply {
intent = Intent().apply {
putExtra("gkmsData", "{}")
}
}
initGkmsConfig(fakeActivity)
}
}
override fun initZygote(startupParam: IXposedHookZygoteInit.StartupParam) {
modulePath = startupParam.modulePath

View File

@ -59,9 +59,11 @@ class MainActivity : AppCompatActivity(), ConfigListener {
val requestData = intent.getStringExtra("gkmsData")
if (requestData != null) {
if (requestData == "requestConfig") {
onClickStartGame()
finish()
}
}
showVersion()
}