refactor: ModConfig lateInit

This commit is contained in:
rhunk
2023-10-25 03:20:48 +02:00
parent a4b7432006
commit be4a0f5ccd
5 changed files with 24 additions and 10 deletions

View File

@ -57,7 +57,7 @@ class RemoteSideContext(
set(value) { _activity?.clear(); _activity = WeakReference(value) } set(value) { _activity?.clear(); _activity = WeakReference(value) }
val sharedPreferences: SharedPreferences get() = androidContext.getSharedPreferences("prefs", 0) val sharedPreferences: SharedPreferences get() = androidContext.getSharedPreferences("prefs", 0)
val config = ModConfig() val config = ModConfig(androidContext)
val translation = LocaleWrapper() val translation = LocaleWrapper()
val mappings = MappingsWrapper() val mappings = MappingsWrapper()
val downloadTaskManager = DownloadTaskManager() val downloadTaskManager = DownloadTaskManager()

View File

@ -1,5 +1,6 @@
package me.rhunk.snapenhance.common.config package me.rhunk.snapenhance.common.config
import android.content.Context
import com.google.gson.JsonObject import com.google.gson.JsonObject
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
@ -83,6 +84,12 @@ open class ConfigContainer(
} }
} }
open fun lateInit(context: Context) {
properties.values.filter { it.getNullable() is ConfigContainer }.forEach {
(it.get() as ConfigContainer).lateInit(context)
}
}
operator fun getValue(t: Any?, property: KProperty<*>) = this.globalState operator fun getValue(t: Any?, property: KProperty<*>) = this.globalState
operator fun setValue(t: Any?, property: KProperty<*>, t1: Boolean?) { this.globalState = t1 } operator fun setValue(t: Any?, property: KProperty<*>, t1: Boolean?) { this.globalState = t1 }
} }

View File

@ -12,7 +12,9 @@ import me.rhunk.snapenhance.common.config.impl.RootConfig
import me.rhunk.snapenhance.common.logger.AbstractLogger import me.rhunk.snapenhance.common.logger.AbstractLogger
import kotlin.properties.Delegates import kotlin.properties.Delegates
class ModConfig { class ModConfig(
private val context: Context
) {
var locale: String = LocaleWrapper.DEFAULT_LOCALE var locale: String = LocaleWrapper.DEFAULT_LOCALE
private val gson: Gson = GsonBuilder().setPrettyPrinting().create() private val gson: Gson = GsonBuilder().setPrettyPrinting().create()
@ -24,8 +26,10 @@ class ModConfig {
lateinit var root: RootConfig lateinit var root: RootConfig
private set private set
private fun createRootConfig() = RootConfig().apply { lateInit(context) }
private fun load() { private fun load() {
root = RootConfig() root = createRootConfig()
wasPresent = file.isFileExists() wasPresent = file.isFileExists()
if (!file.isFileExists()) { if (!file.isFileExists()) {
writeConfig() writeConfig()
@ -95,7 +99,7 @@ class ModConfig {
configStateListener?.also { configStateListener?.also {
runCatching { runCatching {
compareDiff(RootConfig().apply { compareDiff(createRootConfig().apply {
fromJson(gson.fromJson(file.read().toString(Charsets.UTF_8), JsonObject::class.java)) fromJson(gson.fromJson(file.read().toString(Charsets.UTF_8), JsonObject::class.java))
}, root) }, root)

View File

@ -36,10 +36,11 @@ import me.rhunk.snapenhance.nativelib.NativeLib
import kotlin.reflect.KClass import kotlin.reflect.KClass
import kotlin.system.exitProcess import kotlin.system.exitProcess
class ModContext { class ModContext(
val androidContext: Context
) {
val coroutineScope = CoroutineScope(Dispatchers.IO) val coroutineScope = CoroutineScope(Dispatchers.IO)
lateinit var androidContext: Context
lateinit var bridgeClient: BridgeClient lateinit var bridgeClient: BridgeClient
var mainActivity: Activity? = null var mainActivity: Activity? = null
@ -47,7 +48,7 @@ class ModContext {
val resources: Resources get() = androidContext.resources val resources: Resources get() = androidContext.resources
val gson: Gson = GsonBuilder().create() val gson: Gson = GsonBuilder().create()
private val _config = ModConfig() private val _config = ModConfig(androidContext)
val config by _config::root val config by _config::root
val log by lazy { CoreLogger(this.bridgeClient) } val log by lazy { CoreLogger(this.bridgeClient) }
val translation = LocaleWrapper() val translation = LocaleWrapper()

View File

@ -33,7 +33,7 @@ class SnapEnhance {
SnapClassCache(classLoader) SnapClassCache(classLoader)
} }
} }
private val appContext = ModContext() private lateinit var appContext: ModContext
private var isBridgeInitialized = false private var isBridgeInitialized = false
private var isActivityPaused = false private var isActivityPaused = false
@ -47,9 +47,11 @@ class SnapEnhance {
init { init {
Application::class.java.hook("attach", HookStage.BEFORE) { param -> Application::class.java.hook("attach", HookStage.BEFORE) { param ->
appContext.apply { appContext = ModContext(
androidContext = param.arg<Context>(0).also { classLoader = it.classLoader } androidContext = param.arg<Context>(0).also { classLoader = it.classLoader }
bridgeClient = BridgeClient(appContext) )
appContext.apply {
bridgeClient = BridgeClient(this)
bridgeClient.apply { bridgeClient.apply {
connect( connect(
onFailure = { onFailure = {