Skip to content

Commit 6e54b10

Browse files
committed
fix(plugin): load XStream properly
1 parent acf8afe commit 6e54b10

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

plugin/src/client/sc/plugin2021/AbstractClient.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ abstract class AbstractClient(
3030
companion object {
3131
private val logger = LoggerFactory.getLogger(AbstractClient::class.java);
3232
private val gameType = GamePlugin.PLUGIN_UUID
33+
34+
init {
35+
GamePlugin.registerXStream()
36+
}
3337
}
3438

3539
var isGameOver = false

plugin/src/server/sc/plugin2021/GamePlugin.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package sc.plugin2021
22

3+
import com.thoughtworks.xstream.XStream
34
import sc.api.plugins.IGameInstance
45
import sc.api.plugins.IGamePlugin
6+
import sc.helpers.xStream
57
import sc.plugin2021.xstream.BoardConverter
68
import sc.plugins.PluginDescriptor
79
import sc.protocol.helpers.LobbyProtocol
810
import sc.shared.ScoreAggregation
911
import sc.shared.ScoreDefinition
1012
import sc.shared.ScoreFragment
11-
import sc.helpers.xStream as xstream
13+
import java.util.concurrent.atomic.AtomicBoolean
1214

1315
@PluginDescriptor(name = "Blokus", uuid = "swc_2021_blokus")
1416
class GamePlugin: IGamePlugin {
1517

1618
companion object {
1719
val PLUGIN_UUID = "swc_2021_blokus"
20+
val loaded = AtomicBoolean(false)
1821

1922
val SCORE_DEFINITION = ScoreDefinition(arrayOf(
2023
ScoreFragment("Gewinner"),
@@ -28,15 +31,16 @@ class GamePlugin: IGamePlugin {
2831
Color::class.java, Team::class.java)
2932

3033
fun registerXStream() {
31-
LobbyProtocol.registerAdditionalMessages(xstream, classesToRegister)
32-
33-
xstream.registerConverter(BoardConverter())
34+
if (loaded.compareAndSet(false, true)) {
35+
LobbyProtocol.registerAdditionalMessages(xStream, classesToRegister)
36+
xStream.registerConverter(BoardConverter())
37+
}
3438
}
35-
39+
3640
@JvmStatic
37-
val xStream by lazy {
41+
fun loadXStream(): XStream {
3842
registerXStream()
39-
xstream
43+
return xStream
4044
}
4145
}
4246

plugin/src/test/sc/plugin2021/GameStateTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import io.kotest.matchers.should
88
import io.kotest.matchers.shouldBe
99
import org.junit.jupiter.api.assertDoesNotThrow
1010
import org.junit.jupiter.api.assertThrows
11-
import sc.plugin2021.GamePlugin.Companion.xStream
1211
import sc.plugin2021.util.Constants
12+
import sc.plugin2021.GamePlugin.Companion.loadXStream
1313
import sc.plugin2021.util.GameRuleLogic
1414
import sc.shared.InvalidMoveException
1515

@@ -70,7 +70,7 @@ class GameStateTest: StringSpec({
7070
}
7171
"XML conversion works" {
7272
val state = GameState()
73-
val transformed = xStream.fromXML(xStream.toXML(GameState(startPiece = state.startPiece))) as GameState
73+
val transformed = loadXStream().fromXML(loadXStream().toXML(GameState(startPiece = state.startPiece))) as GameState
7474
transformed.toString() shouldBe state.toString()
7575
transformed shouldBe state
7676

plugin/src/test/sc/plugin2021/PieceTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import io.kotest.matchers.maps.shouldContain
77
import io.kotest.matchers.maps.shouldContainExactly
88
import io.kotest.matchers.shouldBe
99
import org.opentest4j.AssertionFailedError
10-
import sc.plugin2021.GamePlugin.Companion.xStream
1110
import sc.plugin2021.helper.shouldSerializeTo
1211
import sc.plugin2021.util.align
1312
import sc.plugin2021.util.flip

plugin/src/test/sc/plugin2021/helper/XStreamMatcher.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import io.kotest.matchers.shouldBe
44
import sc.plugin2021.GamePlugin
55

66
infix fun <T : Any> T.shouldSerializeTo(serialized: String) {
7-
GamePlugin.xStream.toXML(this) shouldBe serialized
8-
val deserialized = GamePlugin.xStream.fromXML(serialized)
7+
GamePlugin.loadXStream().toXML(this) shouldBe serialized
8+
val deserialized = GamePlugin.loadXStream().fromXML(serialized)
99
deserialized.toString() shouldBe this.toString()
1010
deserialized shouldBe this
1111
}

0 commit comments

Comments
 (0)