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