Use custom Vector2 serialization - reduces total file size by 4% (#12077)

This commit is contained in:
Yair Morgenstern 2024-08-08 14:38:59 +03:00 committed by GitHub
parent a4a270c184
commit b9f907e74d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 47 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package com.unciv.json
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.files.FileHandle
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.utils.Json
import com.badlogic.gdx.utils.JsonWriter
import com.badlogic.gdx.utils.SerializationException
@ -22,6 +23,7 @@ fun json() = Json(JsonWriter.OutputType.json).apply {
setSerializer(Duration::class.java, DurationSerializer())
setSerializer(KeyCharAndCode::class.java, KeyCharAndCode.Serializer())
setSerializer(Vector2::class.java, Vector2Serializer())
}
/**

View File

@ -0,0 +1,35 @@
package com.unciv.json
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.utils.Json
import com.badlogic.gdx.utils.Json.Serializer
import com.badlogic.gdx.utils.JsonValue
class Vector2Serializer : Serializer<Vector2> {
override fun write(json: Json, vector2: Vector2?, knownType: Class<*>?) {
if (vector2 == null) json.writeValue(null)
else {
// NEW vector serialization - currently disabled
// json.writeValue("${vector2.x.toInt()}/${vector2.y.toInt()}")
// OLD vector serialization - deprecated 4.12.18
json.writeObjectStart()
json.writeFields(vector2)
json.writeObjectEnd()
}
}
override fun read(json: Json, jsonData: JsonValue, knownType: Class<*>?): Vector2? {
if (jsonData.isNull) return null // Not entirely sure it's necessary
if (jsonData.isString) {
val split = jsonData.asString().split("/")
return Vector2(split[0].toFloat(), split[1].toFloat())
}
// OLD vector serialization
val vector = Vector2()
json.readFields(vector, jsonData)
return vector
}
}

View File

@ -50,7 +50,8 @@ internal class WorkerAutomationTest {
// Assert
assertEquals("Worker should have replaced already existing improvement 'Farm' with 'Mine' to enable 'Iron' resource",
"Mine", currentTile.improvementInProgress)
"Mine", currentTile.improvementInProgress
)
assertTrue(currentTile.turnsToImprovement > 0)
}
@ -74,7 +75,8 @@ internal class WorkerAutomationTest {
workerAutomation.automateWorkerAction(mapUnit, hashSetOf())
assertEquals("Worker should begun removing the forest to clear a luxury resource but didn't",
"Remove Forest", currentTile.improvementInProgress)
"Remove Forest", currentTile.improvementInProgress
)
assertTrue(currentTile.turnsToImprovement > 0)
}
@ -98,7 +100,8 @@ internal class WorkerAutomationTest {
// Assert
assertEquals("Worker should be buliding a farm under 'Iron' resource because it can't see it and has nothing else to do",
"Farm", currentTile.improvementInProgress)
"Farm", currentTile.improvementInProgress
)
assertTrue(currentTile.turnsToImprovement > 0)
}
@ -124,7 +127,8 @@ internal class WorkerAutomationTest {
workerAutomation.automateWorkerAction(mapUnit, hashSetOf())
assertEquals("Worker should be buliding a mine on the Gold Ore luxury resource",
"Mine", currentTile.improvementInProgress)
"Mine", currentTile.improvementInProgress
)
assertTrue(currentTile.turnsToImprovement > 0)
}
@ -340,7 +344,8 @@ internal class WorkerAutomationTest {
// Assert
assertEquals("Worker should try to repair the mine",
"Repair", currentTile.improvementInProgress)
"Repair", currentTile.improvementInProgress
)
assertTrue(currentTile.turnsToImprovement > 0)
}