refactor: lazy bridge references
This commit is contained in:
@@ -5,6 +5,8 @@ import android.os.ParcelFileDescriptor
|
||||
import android.os.ParcelFileDescriptor.AutoCloseInputStream
|
||||
import android.os.ParcelFileDescriptor.AutoCloseOutputStream
|
||||
import me.rhunk.snapenhance.bridge.storage.FileHandle
|
||||
import me.rhunk.snapenhance.common.util.LazyBridgeValue
|
||||
import me.rhunk.snapenhance.common.util.lazyBridge
|
||||
import java.io.File
|
||||
|
||||
|
||||
@@ -43,10 +45,10 @@ enum class InternalFileHandleType(
|
||||
}
|
||||
}
|
||||
|
||||
fun FileHandle.toWrapper() = FileHandleWrapper(lazy { this })
|
||||
fun FileHandle.toWrapper() = FileHandleWrapper(lazyBridge { this })
|
||||
|
||||
open class FileHandleWrapper(
|
||||
private val fileHandle: Lazy<FileHandle>
|
||||
private val fileHandle: LazyBridgeValue<FileHandle>
|
||||
) {
|
||||
fun exists() = fileHandle.value.exists()
|
||||
fun create() = fileHandle.value.create()
|
||||
@@ -54,8 +56,8 @@ open class FileHandleWrapper(
|
||||
|
||||
fun writeBytes(data: ByteArray) = fileHandle.value.open(
|
||||
ParcelFileDescriptor.MODE_WRITE_ONLY or
|
||||
ParcelFileDescriptor.MODE_CREATE or
|
||||
ParcelFileDescriptor.MODE_TRUNCATE
|
||||
ParcelFileDescriptor.MODE_CREATE or
|
||||
ParcelFileDescriptor.MODE_TRUNCATE
|
||||
).use { pfd ->
|
||||
AutoCloseOutputStream(pfd).use {
|
||||
it.write(data)
|
||||
@@ -64,7 +66,7 @@ open class FileHandleWrapper(
|
||||
|
||||
open fun readBytes(): ByteArray = fileHandle.value.open(
|
||||
ParcelFileDescriptor.MODE_READ_ONLY or
|
||||
ParcelFileDescriptor.MODE_CREATE
|
||||
ParcelFileDescriptor.MODE_CREATE
|
||||
).use { pfd ->
|
||||
AutoCloseInputStream(pfd).use {
|
||||
it.readBytes()
|
||||
@@ -73,7 +75,7 @@ open class FileHandleWrapper(
|
||||
|
||||
fun inputStream(block: (AutoCloseInputStream) -> Unit) = fileHandle.value.open(
|
||||
ParcelFileDescriptor.MODE_READ_ONLY or
|
||||
ParcelFileDescriptor.MODE_CREATE
|
||||
ParcelFileDescriptor.MODE_CREATE
|
||||
).use { pfd ->
|
||||
AutoCloseInputStream(pfd).use {
|
||||
block(it)
|
||||
@@ -82,8 +84,8 @@ open class FileHandleWrapper(
|
||||
|
||||
fun outputStream(block: (AutoCloseOutputStream) -> Unit) = fileHandle.value.open(
|
||||
ParcelFileDescriptor.MODE_WRITE_ONLY or
|
||||
ParcelFileDescriptor.MODE_CREATE or
|
||||
ParcelFileDescriptor.MODE_TRUNCATE
|
||||
ParcelFileDescriptor.MODE_CREATE or
|
||||
ParcelFileDescriptor.MODE_TRUNCATE
|
||||
).use { pfd ->
|
||||
AutoCloseOutputStream(pfd).use {
|
||||
block(it)
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package me.rhunk.snapenhance.common.bridge
|
||||
|
||||
import me.rhunk.snapenhance.bridge.storage.FileHandleManager
|
||||
import me.rhunk.snapenhance.common.util.LazyBridgeValue
|
||||
import me.rhunk.snapenhance.common.util.lazyBridge
|
||||
|
||||
open class InternalFileWrapper(
|
||||
fileHandleManager: FileHandleManager,
|
||||
fileHandleManager: LazyBridgeValue<FileHandleManager>,
|
||||
private val fileType: InternalFileHandleType,
|
||||
val defaultValue: String? = null
|
||||
): FileHandleWrapper(lazy { fileHandleManager.getFileHandle(FileHandleScope.INTERNAL.key, fileType.key)!! }) {
|
||||
): FileHandleWrapper(lazyBridge { fileHandleManager.value.getFileHandle(FileHandleScope.INTERNAL.key, fileType.key)!! }) {
|
||||
override fun readBytes(): ByteArray {
|
||||
if (!exists()) {
|
||||
defaultValue?.toByteArray(Charsets.UTF_8)?.let {
|
||||
|
||||
@@ -8,11 +8,12 @@ import com.google.gson.JsonParser
|
||||
import me.rhunk.snapenhance.bridge.storage.FileHandleManager
|
||||
import me.rhunk.snapenhance.common.bridge.FileHandleScope
|
||||
import me.rhunk.snapenhance.common.logger.AbstractLogger
|
||||
import me.rhunk.snapenhance.common.util.LazyBridgeValue
|
||||
import java.util.Locale
|
||||
|
||||
|
||||
class LocaleWrapper(
|
||||
private val fileHandleManager: FileHandleManager
|
||||
private val fileHandleManager: LazyBridgeValue<FileHandleManager>
|
||||
) {
|
||||
companion object {
|
||||
const val DEFAULT_LOCALE = "en_US"
|
||||
@@ -62,15 +63,14 @@ class LocaleWrapper(
|
||||
}
|
||||
|
||||
fun load() {
|
||||
load(
|
||||
DEFAULT_LOCALE,
|
||||
fileHandleManager.getFileHandle(FileHandleScope.LOCALE.key, "$DEFAULT_LOCALE.json")?.open(ParcelFileDescriptor.MODE_READ_ONLY) ?: run {
|
||||
throw IllegalStateException("Failed to load default locale")
|
||||
}
|
||||
)
|
||||
fileHandleManager.value.getFileHandle(FileHandleScope.LOCALE.key, "$DEFAULT_LOCALE.json")?.open(ParcelFileDescriptor.MODE_READ_ONLY)?.use {
|
||||
load(DEFAULT_LOCALE, it)
|
||||
} ?: run {
|
||||
throw IllegalStateException("Failed to load default locale")
|
||||
}
|
||||
|
||||
if (userLocale != DEFAULT_LOCALE) {
|
||||
fileHandleManager.getFileHandle(FileHandleScope.LOCALE.key, "$userLocale.json")?.open(ParcelFileDescriptor.MODE_READ_ONLY)?.let {
|
||||
fileHandleManager.value.getFileHandle(FileHandleScope.LOCALE.key, "$userLocale.json")?.open(ParcelFileDescriptor.MODE_READ_ONLY)?.use {
|
||||
load(userLocale, it)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,12 +9,13 @@ import me.rhunk.snapenhance.common.Constants
|
||||
import me.rhunk.snapenhance.common.bridge.InternalFileHandleType
|
||||
import me.rhunk.snapenhance.common.bridge.InternalFileWrapper
|
||||
import me.rhunk.snapenhance.common.logger.AbstractLogger
|
||||
import me.rhunk.snapenhance.common.util.LazyBridgeValue
|
||||
import me.rhunk.snapenhance.mapper.AbstractClassMapper
|
||||
import me.rhunk.snapenhance.mapper.ClassMapper
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
class MappingsWrapper(
|
||||
fileHandleManager: FileHandleManager
|
||||
fileHandleManager: LazyBridgeValue<FileHandleManager>
|
||||
): InternalFileWrapper(fileHandleManager, InternalFileHandleType.MAPPINGS, defaultValue = "{}") {
|
||||
private lateinit var context: Context
|
||||
private var mappingUniqueHash: Long = 0
|
||||
|
||||
@@ -11,11 +11,12 @@ import me.rhunk.snapenhance.common.bridge.InternalFileWrapper
|
||||
import me.rhunk.snapenhance.common.bridge.wrapper.LocaleWrapper
|
||||
import me.rhunk.snapenhance.common.config.impl.RootConfig
|
||||
import me.rhunk.snapenhance.common.logger.AbstractLogger
|
||||
import me.rhunk.snapenhance.common.util.LazyBridgeValue
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
class ModConfig(
|
||||
private val context: Context,
|
||||
fileHandleManager: FileHandleManager
|
||||
fileHandleManager: LazyBridgeValue<FileHandleManager>
|
||||
) {
|
||||
private val fileWrapper = InternalFileWrapper(fileHandleManager, InternalFileHandleType.CONFIG, "{}")
|
||||
var locale: String = LocaleWrapper.DEFAULT_LOCALE
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package me.rhunk.snapenhance.common.util
|
||||
|
||||
import android.os.IInterface
|
||||
|
||||
|
||||
open class LazyBridgeValue<T: IInterface>(
|
||||
private val block: () -> T,
|
||||
private val isConstant: Boolean = false
|
||||
): Lazy<T> {
|
||||
private val lock = Any()
|
||||
private var _value: T? = null
|
||||
|
||||
override val value: T get() = run {
|
||||
synchronized(lock) {
|
||||
if (_value == null || (!isConstant && !_value!!.asBinder().pingBinder())) {
|
||||
_value = block()
|
||||
}
|
||||
}
|
||||
return _value!!
|
||||
}
|
||||
|
||||
override fun isInitialized(): Boolean {
|
||||
return _value != null && (isConstant || _value!!.asBinder().pingBinder())
|
||||
}
|
||||
|
||||
operator fun getValue(thisRef: Any?, property: Any?): T {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun <T : IInterface, R> mappedLazyBridge(lazyBridgeValue: LazyBridgeValue<T>, map: (T) -> R): Lazy<R> {
|
||||
return object : Lazy<R> {
|
||||
private var _value: T? = null
|
||||
private var _mappedValue: R? = null
|
||||
|
||||
override val value: R get() = run {
|
||||
if (_value != lazyBridgeValue.value) {
|
||||
_value = lazyBridgeValue.value
|
||||
_mappedValue = map(_value!!)
|
||||
}
|
||||
return _mappedValue!!
|
||||
}
|
||||
override fun isInitialized(): Boolean = lazyBridgeValue.isInitialized()
|
||||
}
|
||||
}
|
||||
|
||||
fun <T: IInterface> lazyBridge(block: () -> T) = LazyBridgeValue(block)
|
||||
fun <T: IInterface> constantLazyBridge(value: () -> T) = LazyBridgeValue(value, isConstant = true)
|
||||
Reference in New Issue
Block a user