Commit 5a09d46f authored by Mygod's avatar Mygod

Fix UDP fallback handling in JSON parsing

Fixes #2437.
parent 5b6a6782
...@@ -162,7 +162,7 @@ data class Profile( ...@@ -162,7 +162,7 @@ data class Profile(
}.filterNotNull() }.filterNotNull()
private class JsonParser(private val feature: Profile? = null) : ArrayList<Profile>() { private class JsonParser(private val feature: Profile? = null) : ArrayList<Profile>() {
private val fallbackMap = mutableMapOf<Profile, Profile>() val fallbackMap = mutableMapOf<Profile, Profile>()
private val JsonElement?.optString get() = (this as? JsonPrimitive)?.asString private val JsonElement?.optString get() = (this as? JsonPrimitive)?.asString
private val JsonElement?.optBoolean private val JsonElement?.optBoolean
...@@ -223,7 +223,7 @@ data class Profile( ...@@ -223,7 +223,7 @@ data class Profile(
} }
} }
fun finalize(create: (Profile) -> Unit) { fun finalize(create: (Profile) -> Profile) {
val profiles = ProfileManager.getAllProfiles() ?: emptyList() val profiles = ProfileManager.getAllProfiles() ?: emptyList()
for ((profile, fallback) in fallbackMap) { for ((profile, fallback) in fallbackMap) {
val match = profiles.firstOrNull { val match = profiles.firstOrNull {
...@@ -231,19 +231,20 @@ data class Profile( ...@@ -231,19 +231,20 @@ data class Profile(
fallback.password == it.password && fallback.method == it.method && fallback.password == it.password && fallback.method == it.method &&
it.plugin.isNullOrEmpty() it.plugin.isNullOrEmpty()
} }
profile.udpFallback = if (match == null) { profile.udpFallback = (match ?: create(fallback)).id
create(fallback)
fallback.id
} else match.id
ProfileManager.updateProfile(profile) ProfileManager.updateProfile(profile)
} }
} }
} }
fun parseJson(json: JsonElement, feature: Profile? = null, create: (Profile) -> Unit) { fun parseJson(json: JsonElement, feature: Profile? = null, create: (Profile) -> Profile) {
JsonParser(feature).run { JsonParser(feature).run {
process(json) process(json)
for (profile in this) create(profile) for (i in indices) {
val fallback = fallbackMap.remove(this[i])
this[i] = create(this[i])
fallback?.also { fallbackMap[this[i]] = it }
}
finalize(create) finalize(create)
} }
} }
......
...@@ -194,7 +194,7 @@ class SubscriptionService : Service(), CoroutineScope { ...@@ -194,7 +194,7 @@ class SubscriptionService : Service(), CoroutineScope {
subscription = Profile.SubscriptionStatus.Active subscription = Profile.SubscriptionStatus.Active
}) })
} }
} }!!
} }
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment