Skip to content

Commit

Permalink
fix: original asset objects are now cast during first load to avoid l…
Browse files Browse the repository at this point in the history
…eaking originals where we shouldnt, #977
  • Loading branch information
AlmasB committed Feb 28, 2023
1 parent a65770d commit 6ba4715
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Expand Up @@ -533,7 +533,7 @@ class FXGLAssetLoaderService : AssetLoaderService() {
cachedAssets[cacheKey] = loaded as Any
}

loaded
data.cast(loaded as Any)
} catch (e: Exception) {
log.warning("Failed to load ${loadParams.url}", e)
data.getDummy()
Expand Down Expand Up @@ -696,6 +696,12 @@ private class TextAssetLoader : AssetLoader<List<*>>(
List::class.java,
TEXT_DIR
) {
override fun cast(obj: Any): List<String> {
val list = obj as List<String>

return list.toList()
}

override fun load(url: URL): List<String> = url.openStream().bufferedReader().readLines()

override fun getDummy(): List<String> = emptyList()
Expand Down
Expand Up @@ -185,7 +185,8 @@ class AssetLoaderServiceTest {

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

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

@Test
Expand Down

0 comments on commit 6ba4715

Please sign in to comment.