fix: set aside an unreadable game environment file (#2808)

fix: set aside an unreadable game environment file instead of failing every session
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-11-18 00:47:12 -05:00
1 parent 5e704296ed
commit 384eb78215
2 files changed
+27 -1

No files matched your search

@@ -2,6 +2,7 @@ package com.droiddeck.launcher.session
import android.content.Context
import android.util.AtomicFile
import android.util.Log
import com.droiddeck.launcher.core.GameEnvironment
import com.droiddeck.launcher.core.TextureFiltering
import com.droiddeck.launcher.runtime.LinuxRuntime
@@ -9,6 +10,7 @@ import org.json.JSONObject
import java.io.File
object GameEnvironmentStore {
private const val TAG = "GameEnvironmentStore"
private const val SETTINGS = "game-environment.json"
private const val GUEST_FILE = "root/.config/droiddeck/game-environment.json"
const val DXVK_CONFIG = "dxvkConfig"
@@ -19,7 +21,16 @@ object GameEnvironmentStore {
fun read(context: Context): GameEnvironment.Config {
val file = AtomicFile(File(context.filesDir, SETTINGS))
val bytes = try { file.readFully() } catch (_: java.io.FileNotFoundException) { return GameEnvironment.Config() }
return decode(JSONObject(bytes.toString(Charsets.UTF_8)))
return try {
decode(JSONObject(bytes.toString(Charsets.UTF_8)))
} catch (error: Exception) {
if (error !is org.json.JSONException && error !is IllegalArgumentException) throw error
val aside = File(context.filesDir, "$SETTINGS.unreadable")
Log.w(TAG, "game environment settings are unreadable, set aside as ${aside.name}", error)
aside.delete()
file.baseFile.renameTo(aside)
GameEnvironment.Config()
}
}
fun decode(json: JSONObject): GameEnvironment.Config {
@@ -323,4 +323,19 @@ class GameEnvironmentStoreTest {
assertEquals(mapOf("DD_SYNC" to "1", "DXVK_HUD" to "fps"), config.shared)
assertEquals(mapOf("DD_FSYNC_FIRST" to "1", "DD_SYNC_FALLBACK" to "1"), config.games["440"])
}
@Test fun unreadableSettingsAreSetAsideAndSessionsStillStart() {
val settings = File(context.filesDir, "game-environment.json")
val aside = File(context.filesDir, "game-environment.json.unreadable")
aside.delete()
for (broken in listOf("", """{"version":1,"shared":{"bad name":"1"}}""")) {
settings.writeText(broken)
assertEquals(GameEnvironment.Config(), GameEnvironmentStore.read(context))
assertFalse(settings.exists())
assertEquals(broken, aside.readText())
}
GameEnvironmentStore.publish(context)
assertEquals(1, JSONObject(guest.readText()).getInt("version"))
aside.delete()
}
}