mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-18 03:44:54 -04:00
fix profile saving
This commit is contained in:
parent
749e3958e9
commit
94571f207f
@ -13,7 +13,9 @@
|
||||
|
||||
package de.bixilon.minosoft.config.profile
|
||||
|
||||
import com.fasterxml.jackson.databind.InjectableValues
|
||||
import com.fasterxml.jackson.databind.JavaType
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import de.bixilon.kutil.collections.map.bi.AbstractMutableBiMap
|
||||
import de.bixilon.kutil.concurrent.pool.DefaultThreadPool
|
||||
import de.bixilon.kutil.exception.ExceptionUtil.tryCatch
|
||||
@ -40,6 +42,7 @@ import java.util.concurrent.locks.ReentrantLock
|
||||
|
||||
|
||||
interface ProfileManager<T : Profile> {
|
||||
val mapper: ObjectMapper
|
||||
val namespace: ResourceLocation
|
||||
val latestVersion: Int
|
||||
val saveLock: ReentrantLock
|
||||
@ -68,13 +71,19 @@ interface ProfileManager<T : Profile> {
|
||||
fun migrate(from: Int, data: MutableMap<String, Any?>) = Unit
|
||||
|
||||
|
||||
fun load(name: String, data: MutableMap<String, Any?>?): T {
|
||||
val profile = if (data == null) {
|
||||
createProfile(name)
|
||||
} else {
|
||||
Jackson.MAPPER.convertValue(data, jacksonProfileType) as T
|
||||
}
|
||||
fun updateValue(profile: T, data: MutableMap<String, Any?>?) {
|
||||
profile.reloading = true
|
||||
val injectable = InjectableValues.Std()
|
||||
injectable.addValue(profileClass, profile)
|
||||
mapper.injectableValues = injectable
|
||||
mapper.updateValue(profile, data)
|
||||
profile.saved = true
|
||||
profile.reloading = false
|
||||
}
|
||||
|
||||
fun load(name: String, data: MutableMap<String, Any?>?): T {
|
||||
val profile = createProfile()
|
||||
updateValue(profile, data)
|
||||
profiles[name] = profile
|
||||
return profile
|
||||
}
|
||||
@ -248,9 +257,7 @@ interface ProfileManager<T : Profile> {
|
||||
}
|
||||
try {
|
||||
val data = readAndMigrate(path.path).second
|
||||
val dataString = Jackson.MAPPER.writeValueAsString(data)
|
||||
profile.reloading = true
|
||||
Jackson.MAPPER.readerForUpdating(profile).readValue<T>(dataString)
|
||||
updateValue(profile, data)
|
||||
} catch (exception: Exception) {
|
||||
exception.printStackTrace()
|
||||
exception.crash()
|
||||
|
@ -30,4 +30,11 @@ interface AbstractDelegate<T> : TextFormattable {
|
||||
override fun toText(): Any? {
|
||||
return get()
|
||||
}
|
||||
|
||||
fun queueSave() {
|
||||
if (profile.reloading) {
|
||||
return
|
||||
}
|
||||
profile.saved = false
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ package de.bixilon.minosoft.config.profile.delegate
|
||||
import de.bixilon.kutil.observer.DataObserver
|
||||
import de.bixilon.minosoft.config.profile.profiles.Profile
|
||||
import de.bixilon.minosoft.util.KUtil.minosoft
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
open class SimpleDelegate<T>(
|
||||
override val profile: Profile,
|
||||
@ -32,6 +33,11 @@ open class SimpleDelegate<T>(
|
||||
this.value = value
|
||||
}
|
||||
|
||||
override fun setValue(thisRef: Any, property: KProperty<*>, value: T) {
|
||||
super.setValue(thisRef, property, value)
|
||||
queueSave()
|
||||
}
|
||||
|
||||
override fun validate(value: T) {
|
||||
verify?.invoke(value)
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import de.bixilon.kutil.observer.list.ListObserver
|
||||
import de.bixilon.minosoft.config.profile.delegate.AbstractDelegate
|
||||
import de.bixilon.minosoft.config.profile.profiles.Profile
|
||||
import de.bixilon.minosoft.util.KUtil.minosoft
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class ListDelegate<V>(
|
||||
override val profile: Profile,
|
||||
@ -26,11 +27,20 @@ class ListDelegate<V>(
|
||||
override val name = minosoft(name)
|
||||
override val description = minosoft("$name.description")
|
||||
|
||||
init {
|
||||
value.addObserver { queueSave() }
|
||||
}
|
||||
|
||||
override fun get() = value
|
||||
override fun set(value: MutableList<V>) {
|
||||
validate(value)
|
||||
this.value.unsafe = value
|
||||
}
|
||||
|
||||
override fun setValue(thisRef: Any, property: KProperty<*>, value: MutableList<V>) {
|
||||
super.setValue(thisRef, property, value)
|
||||
queueSave()
|
||||
}
|
||||
|
||||
override fun validate(value: MutableList<V>) = Unit
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import de.bixilon.kutil.observer.map.MapObserver
|
||||
import de.bixilon.minosoft.config.profile.delegate.AbstractDelegate
|
||||
import de.bixilon.minosoft.config.profile.profiles.Profile
|
||||
import de.bixilon.minosoft.util.KUtil.minosoft
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
open class MapDelegate<K, V>(
|
||||
override val profile: Profile,
|
||||
@ -26,11 +27,20 @@ open class MapDelegate<K, V>(
|
||||
override val name = minosoft(name)
|
||||
override val description = minosoft("$name.description")
|
||||
|
||||
init {
|
||||
value.addObserver { queueSave() }
|
||||
}
|
||||
|
||||
override fun get() = value
|
||||
override fun set(value: MutableMap<K, V>) {
|
||||
validate(value)
|
||||
this.value.unsafe = value
|
||||
}
|
||||
|
||||
override fun setValue(thisRef: Any, property: KProperty<*>, value: MutableMap<K, V>) {
|
||||
super.setValue(thisRef, property, value)
|
||||
queueSave()
|
||||
}
|
||||
|
||||
override fun validate(value: MutableMap<K, V>) = Unit
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import de.bixilon.kutil.observer.set.SetObserver
|
||||
import de.bixilon.minosoft.config.profile.delegate.AbstractDelegate
|
||||
import de.bixilon.minosoft.config.profile.profiles.Profile
|
||||
import de.bixilon.minosoft.util.KUtil.minosoft
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class SetDelegate<V>(
|
||||
override val profile: Profile,
|
||||
@ -26,11 +27,20 @@ class SetDelegate<V>(
|
||||
override val name = minosoft(name)
|
||||
override val description = minosoft("$name.description")
|
||||
|
||||
init {
|
||||
value.addObserver { queueSave() }
|
||||
}
|
||||
|
||||
override fun get() = value
|
||||
override fun set(value: MutableSet<V>) {
|
||||
validate(value)
|
||||
this.value.unsafe = value
|
||||
}
|
||||
|
||||
override fun setValue(thisRef: Any, property: KProperty<*>, value: MutableSet<V>) {
|
||||
super.setValue(thisRef, property, value)
|
||||
queueSave()
|
||||
}
|
||||
|
||||
override fun validate(value: MutableSet<V>) = Unit
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ import org.kordamp.ikonli.fontawesome5.FontAwesomeSolid
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
|
||||
object AccountProfileManager : ProfileManager<AccountProfile> {
|
||||
override val mapper = Jackson.MAPPER.copy()
|
||||
override val namespace = "minosoft:account".toResourceLocation()
|
||||
override val latestVersion get() = 1
|
||||
override val saveLock = ReentrantLock()
|
||||
|
@ -28,6 +28,7 @@ import org.kordamp.ikonli.fontawesome5.FontAwesomeSolid
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
|
||||
object AudioProfileManager : ProfileManager<AudioProfile> {
|
||||
override val mapper = Jackson.MAPPER.copy()
|
||||
override val namespace = "minosoft:audio".toResourceLocation()
|
||||
override val latestVersion get() = 1
|
||||
override val saveLock = ReentrantLock()
|
||||
|
@ -27,6 +27,7 @@ import org.kordamp.ikonli.fontawesome5.FontAwesomeSolid
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
|
||||
object BlockProfileManager : ProfileManager<BlockProfile> {
|
||||
override val mapper = Jackson.MAPPER.copy()
|
||||
override val namespace = "minosoft:block".toResourceLocation()
|
||||
override val latestVersion get() = 1
|
||||
override val saveLock = ReentrantLock()
|
||||
|
@ -27,6 +27,7 @@ import org.kordamp.ikonli.fontawesome5.FontAwesomeSolid
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
|
||||
object ConnectionProfileManager : ProfileManager<ConnectionProfile> {
|
||||
override val mapper = Jackson.MAPPER.copy()
|
||||
override val namespace = "minosoft:connection".toResourceLocation()
|
||||
override val latestVersion get() = 1
|
||||
override val saveLock = ReentrantLock()
|
||||
|
@ -27,6 +27,7 @@ import org.kordamp.ikonli.fontawesome5.FontAwesomeSolid
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
|
||||
object ControlsProfileManager : ProfileManager<ControlsProfile> {
|
||||
override val mapper = Jackson.MAPPER.copy()
|
||||
override val namespace = "minosoft:controls".toResourceLocation()
|
||||
override val latestVersion get() = 1
|
||||
override val saveLock = ReentrantLock()
|
||||
|
@ -27,6 +27,7 @@ import org.kordamp.ikonli.fontawesome5.FontAwesomeSolid
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
|
||||
object EntityProfileManager : ProfileManager<EntityProfile> {
|
||||
override val mapper = Jackson.MAPPER.copy()
|
||||
override val namespace = "minosoft:entity".toResourceLocation()
|
||||
override val latestVersion get() = 1
|
||||
override val saveLock = ReentrantLock()
|
||||
|
@ -27,6 +27,7 @@ import org.kordamp.ikonli.fontawesome5.FontAwesomeSolid
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
|
||||
object ErosProfileManager : ProfileManager<ErosProfile> {
|
||||
override val mapper = Jackson.MAPPER.copy()
|
||||
override val namespace = "minosoft:eros".toResourceLocation()
|
||||
override val latestVersion get() = 1
|
||||
override val saveLock = ReentrantLock()
|
||||
|
@ -13,7 +13,6 @@
|
||||
|
||||
package de.bixilon.minosoft.config.profile.profiles.eros.server
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude
|
||||
import de.bixilon.minosoft.config.profile.delegate.types.list.ListDelegate
|
||||
import de.bixilon.minosoft.config.profile.profiles.eros.ErosProfile
|
||||
import de.bixilon.minosoft.config.profile.profiles.eros.server.entries.ErosServer
|
||||
@ -24,6 +23,5 @@ class ServerC(profile: ErosProfile) {
|
||||
val modify = ModifyC(profile)
|
||||
val list = ListC(profile)
|
||||
|
||||
@get:JsonInclude(JsonInclude.Include.NON_EMPTY)
|
||||
var entries: MutableList<ErosServer> by ListDelegate(profile, mutableListOf(), "")
|
||||
}
|
||||
|
@ -13,6 +13,7 @@
|
||||
|
||||
package de.bixilon.minosoft.config.profile.profiles.eros.server.entries
|
||||
|
||||
import com.fasterxml.jackson.annotation.JacksonInject
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore
|
||||
import com.fasterxml.jackson.annotation.JsonInclude
|
||||
import com.fasterxml.jackson.annotation.JsonProperty
|
||||
@ -31,13 +32,14 @@ import de.bixilon.minosoft.data.registries.versions.Versions
|
||||
import de.bixilon.minosoft.data.text.ChatComponent
|
||||
|
||||
class ErosServer(
|
||||
profile: ErosProfile,
|
||||
@JacksonInject profile: ErosProfile,
|
||||
address: String,
|
||||
name: ChatComponent = ChatComponent.of(address),
|
||||
forcedVersion: Any? = null, // must be version
|
||||
profiles: MutableMap<ResourceLocation, String> = mutableMapOf(),
|
||||
queryVersion: Boolean = true,
|
||||
) : AbstractServer {
|
||||
|
||||
init {
|
||||
check(forcedVersion == null || forcedVersion is Version)
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ import org.kordamp.ikonli.fontawesome5.FontAwesomeSolid
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
|
||||
object GUIProfileManager : ProfileManager<GUIProfile> {
|
||||
override val mapper = Jackson.MAPPER.copy()
|
||||
override val namespace = "minosoft:gui".toResourceLocation()
|
||||
override val latestVersion get() = 1
|
||||
override val saveLock = ReentrantLock()
|
||||
|
@ -27,6 +27,7 @@ import org.kordamp.ikonli.fontawesome5.FontAwesomeSolid
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
|
||||
object OtherProfileManager : ProfileManager<OtherProfile> {
|
||||
override val mapper = Jackson.MAPPER.copy()
|
||||
override val namespace = "minosoft:other".toResourceLocation()
|
||||
override val latestVersion get() = 2
|
||||
override val saveLock = ReentrantLock()
|
||||
|
@ -27,6 +27,7 @@ import org.kordamp.ikonli.fontawesome5.FontAwesomeSolid
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
|
||||
object ParticleProfileManager : ProfileManager<ParticleProfile> {
|
||||
override val mapper = Jackson.MAPPER.copy()
|
||||
override val namespace = "minosoft:particle".toResourceLocation()
|
||||
override val latestVersion get() = 1
|
||||
override val saveLock = ReentrantLock()
|
||||
|
@ -27,6 +27,7 @@ import org.kordamp.ikonli.fontawesome5.FontAwesomeSolid
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
|
||||
object RenderingProfileManager : ProfileManager<RenderingProfile> {
|
||||
override val mapper = Jackson.MAPPER.copy()
|
||||
override val namespace = "minosoft:rendering".toResourceLocation()
|
||||
override val latestVersion get() = 1
|
||||
override val saveLock = ReentrantLock()
|
||||
|
@ -27,6 +27,7 @@ import org.kordamp.ikonli.fontawesome5.FontAwesomeSolid
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
|
||||
object ResourcesProfileManager : ProfileManager<ResourcesProfile> {
|
||||
override val mapper = Jackson.MAPPER.copy()
|
||||
override val namespace = "minosoft:resources".toResourceLocation()
|
||||
override val latestVersion get() = 1
|
||||
override val saveLock = ReentrantLock()
|
||||
|
Loading…
x
Reference in New Issue
Block a user