Skip to content

Commit 6ba4715

Browse files
committed
fix: original asset objects are now cast during first load to avoid leaking originals where we shouldnt, #977
1 parent a65770d commit 6ba4715

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

fxgl/src/main/kotlin/com/almasb/fxgl/app/services/FXGLAssetLoaderService.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ class FXGLAssetLoaderService : AssetLoaderService() {
533533
cachedAssets[cacheKey] = loaded as Any
534534
}
535535

536-
loaded
536+
data.cast(loaded as Any)
537537
} catch (e: Exception) {
538538
log.warning("Failed to load ${loadParams.url}", e)
539539
data.getDummy()
@@ -696,6 +696,12 @@ private class TextAssetLoader : AssetLoader<List<*>>(
696696
List::class.java,
697697
TEXT_DIR
698698
) {
699+
override fun cast(obj: Any): List<String> {
700+
val list = obj as List<String>
701+
702+
return list.toList()
703+
}
704+
699705
override fun load(url: URL): List<String> = url.openStream().bufferedReader().readLines()
700706

701707
override fun getDummy(): List<String> = emptyList()

fxgl/src/test/kotlin/com/almasb/fxgl/app/AssetLoaderServiceTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ class AssetLoaderServiceTest {
185185

186186
assertThat(lines.size, `is`(0))
187187

188-
assertTrue(assetLoader.loadText("test1.txt") === assetLoader.loadText("test1.txt"))
188+
// ensure a new copy of the list is loaded so that original cannot be modified
189+
assertTrue(assetLoader.loadText("test1.txt") !== assetLoader.loadText("test1.txt"))
189190
}
190191

191192
@Test

0 commit comments

Comments
 (0)