Skip to content
This repository was archived by the owner on Mar 22, 2026. It is now read-only.

Commit 10bc7f5

Browse files
committed
[TouchController] 修复读取预设时遇到 order.json 报错的问题
1 parent 9dbf627 commit 10bc7f5

1 file changed

Lines changed: 11 additions & 15 deletions

File tree

touchcontroller/common/config/preset/PresetManager.kt

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import kotlinx.collections.immutable.*
44
import kotlinx.coroutines.flow.MutableStateFlow
55
import kotlinx.coroutines.flow.asStateFlow
66
import kotlinx.coroutines.flow.updateAndGet
7-
import kotlinx.serialization.ExperimentalSerializationApi
8-
import kotlinx.serialization.json.decodeFromStream
9-
import kotlinx.serialization.json.encodeToStream
107
import org.slf4j.LoggerFactory
118
import top.fifthlight.touchcontroller.common.gal.config.ConfigDirectoryProvider
129
import top.fifthlight.touchcontroller.common.gal.config.ConfigDirectoryProviderFactory
@@ -25,20 +22,20 @@ object PresetManager {
2522
private val _presets = MutableStateFlow(PresetsContainer())
2623
val presets = _presets.asStateFlow()
2724

28-
@OptIn(ExperimentalSerializationApi::class)
2925
fun load() {
3026
try {
3127
logger.info("Reading TouchController preset file")
3228
val order = runCatching<PresetManager, List<Uuid>> {
33-
orderFile.inputStream().use(jsonFormat::decodeFromStream)
29+
jsonFormat.decodeFromString(orderFile.readText())
3430
}.getOrNull()?.toPersistentList() ?: persistentListOf()
3531
val presets = buildMap {
3632
for (entry in presetDir.listDirectoryEntries("*.json")) {
3733
val uuidStr = entry.fileName.toString().lowercase().removeSuffix(".json")
34+
val uuid = runCatching {
35+
Uuid.parse(uuidStr)
36+
}.getOrNull() ?: continue
3837
try {
39-
val uuid = Uuid.parse(uuidStr)
40-
val preset: LayoutPreset = entry.inputStream().use(jsonFormat::decodeFromStream)
41-
put(uuid, preset)
38+
put(uuid, jsonFormat.decodeFromString<LayoutPreset>(entry.readText()))
4239
} catch (ex: Exception) {
4340
logger.warn("Failed to load preset $uuidStr", ex)
4441
continue
@@ -56,24 +53,23 @@ object PresetManager {
5653

5754
private fun getPresetFile(uuid: Uuid) = presetDir.resolve("$uuid.json")
5855

59-
@OptIn(ExperimentalSerializationApi::class)
6056
private fun saveOrder(order: ImmutableList<Uuid>) {
6157
logger.info("Saving TouchController preset order file")
62-
orderFile.outputStream().use { jsonFormat.encodeToStream<List<Uuid>>(order, it) }
58+
orderFile.writeText(jsonFormat.encodeToString<List<Uuid>>(order))
6359
}
6460

65-
@OptIn(ExperimentalSerializationApi::class)
6661
fun savePreset(uuid: Uuid, preset: LayoutPreset, create: Boolean = true) {
6762
logger.info("Saving TouchController preset ${preset.name}($uuid)")
6863
presetDir.createDirectories()
6964
try {
70-
getPresetFile(uuid).outputStream(
71-
*if (create) {
65+
getPresetFile(uuid).writeText(
66+
text = jsonFormat.encodeToString(preset),
67+
options = if (create) {
7268
arrayOf(StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE, StandardOpenOption.CREATE)
7369
} else {
7470
arrayOf(StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)
75-
}
76-
).use { jsonFormat.encodeToStream(preset, it) }
71+
},
72+
)
7773
var addedPresets = false
7874
val newPresets = _presets.updateAndGet {
7975
if (it.containsKey(uuid)) {

0 commit comments

Comments
 (0)