mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-26 21:35:14 -04:00
Texture pack/load for mods also distributes by Images.*X* (#5296)
This commit is contained in:
parent
318a8d99ff
commit
8161da26de
@ -1,6 +1,7 @@
|
|||||||
package com.unciv.ui.utils
|
package com.unciv.ui.utils
|
||||||
|
|
||||||
import com.badlogic.gdx.Gdx
|
import com.badlogic.gdx.Gdx
|
||||||
|
import com.badlogic.gdx.files.FileHandle
|
||||||
import com.badlogic.gdx.graphics.Color
|
import com.badlogic.gdx.graphics.Color
|
||||||
import com.badlogic.gdx.graphics.Texture
|
import com.badlogic.gdx.graphics.Texture
|
||||||
import com.badlogic.gdx.graphics.Texture.TextureFilter
|
import com.badlogic.gdx.graphics.Texture.TextureFilter
|
||||||
@ -62,38 +63,40 @@ object ImageGetter {
|
|||||||
textureRegionDrawables[region.name] = drawable
|
textureRegionDrawables[region.name] = drawable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load base (except game.atlas which is already loaded)
|
||||||
|
loadModAtlases("", Gdx.files.internal("."))
|
||||||
|
|
||||||
|
// These are from the mods
|
||||||
|
for (mod in UncivGame.Current.settings.visualMods + ruleset.mods) {
|
||||||
|
loadModAtlases(mod, Gdx.files.local("mods/$mod"))
|
||||||
|
}
|
||||||
|
|
||||||
|
TileSetCache.assembleTileSetConfigs(ruleset.mods)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Loads all atlas/texture files from a folder, as controlled by an Atlases.json */
|
||||||
|
fun loadModAtlases(mod: String, folder: FileHandle) {
|
||||||
// See #4993 - you can't .list() on a jar file, so the ImagePacker leaves us the list of actual atlases.
|
// See #4993 - you can't .list() on a jar file, so the ImagePacker leaves us the list of actual atlases.
|
||||||
val fileNames = GameSaver.json().fromJson(Array<String>::class.java, Gdx.files.internal("Atlases.json"))
|
val controlFile = folder.child("Atlases.json")
|
||||||
|
val fileNames = (if (controlFile.exists()) GameSaver.json().fromJson(Array<String>::class.java, controlFile)
|
||||||
|
else emptyArray()).toMutableList()
|
||||||
|
if (mod.isNotEmpty()) fileNames += "game"
|
||||||
for (fileName in fileNames) {
|
for (fileName in fileNames) {
|
||||||
val file = Gdx.files.internal("$fileName.atlas")
|
val file = folder.child("$fileName.atlas")
|
||||||
val extraAtlas = file.nameWithoutExtension()
|
if (!file.exists()) continue
|
||||||
val tempAtlas = atlases[extraAtlas] // fetch if cached
|
val extraAtlas = if (mod.isEmpty()) fileName else if (fileName == "game") mod else "$mod/$fileName"
|
||||||
?: TextureAtlas(file.name()).apply { // load if not
|
var tempAtlas = atlases[extraAtlas] // fetch if cached
|
||||||
atlases[extraAtlas] = this // cache the freshly loaded
|
if (tempAtlas == null) {
|
||||||
|
println("Loading $extraAtlas = ${file.path()}")
|
||||||
|
tempAtlas = TextureAtlas(file) // load if not
|
||||||
|
atlases[extraAtlas] = tempAtlas // cache the freshly loaded
|
||||||
}
|
}
|
||||||
for (region in tempAtlas.regions) {
|
for (region in tempAtlas.regions) {
|
||||||
val drawable = TextureRegionDrawable(region)
|
val drawable = TextureRegionDrawable(region)
|
||||||
textureRegionDrawables[region.name] = drawable
|
textureRegionDrawables[region.name] = drawable
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// These are from the mods
|
|
||||||
for (mod in UncivGame.Current.settings.visualMods + ruleset.mods) {
|
|
||||||
val modAtlasFile = Gdx.files.local("mods/$mod/game.atlas")
|
|
||||||
if (!modAtlasFile.exists()) continue
|
|
||||||
|
|
||||||
if (!atlases.containsKey(mod)) atlases[mod] = TextureAtlas(modAtlasFile)
|
|
||||||
val modAtlas = atlases[mod]!!
|
|
||||||
|
|
||||||
for (region in modAtlas.regions) {
|
|
||||||
val drawable = TextureRegionDrawable(region)
|
|
||||||
textureRegionDrawables[region.name] = drawable
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
TileSetCache.assembleTileSetConfigs(ruleset.mods)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Colors a multilayer image and returns it as a list of layers (Image).
|
* Colors a multilayer image and returns it as a list of layers (Image).
|
||||||
|
@ -58,23 +58,15 @@ internal object ImagePacker {
|
|||||||
val defaultSettings = getDefaultSettings()
|
val defaultSettings = getDefaultSettings()
|
||||||
|
|
||||||
// Scan for Image folders and build one atlas each
|
// Scan for Image folders and build one atlas each
|
||||||
if (File("../Images").exists()) { // So we don't run this from within a fat JAR
|
packImagesPerMod("..", ".", defaultSettings)
|
||||||
val atlasList = mutableListOf<String>()
|
|
||||||
for ((file, packFileName) in imageFolders()) {
|
|
||||||
atlasList += packFileName
|
|
||||||
packImagesIfOutdated(defaultSettings, file, ".", packFileName)
|
|
||||||
}
|
|
||||||
atlasList.remove("game")
|
|
||||||
File("Atlases.json").writeText(atlasList.sorted().joinToString(",","[","]"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// pack for mods as well
|
// pack for mods as well
|
||||||
val modDirectory = File("mods")
|
val modDirectory = File("mods")
|
||||||
if (modDirectory.exists()) {
|
if (modDirectory.exists()) {
|
||||||
for (mod in modDirectory.listFiles()!!) {
|
for (mod in modDirectory.listFiles()!!) {
|
||||||
if (!mod.isHidden && File(mod.path + "/Images").exists()) {
|
if (!mod.isHidden) {
|
||||||
try {
|
try {
|
||||||
packImagesIfOutdated(defaultSettings, mod.path + "/Images", mod.path, "game")
|
packImagesPerMod(mod.path, mod.path, defaultSettings)
|
||||||
} catch (ex: Throwable) {
|
} catch (ex: Throwable) {
|
||||||
println("Exception in ImagePacker: ${ex.message}")
|
println("Exception in ImagePacker: ${ex.message}")
|
||||||
}
|
}
|
||||||
@ -86,6 +78,20 @@ internal object ImagePacker {
|
|||||||
println("Packing textures - " + texturePackingTime + "ms")
|
println("Packing textures - " + texturePackingTime + "ms")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Scan multiple image folders and generate an atlas for each - if outdated
|
||||||
|
private fun packImagesPerMod(input: String, output: String, defaultSettings: TexturePacker.Settings) {
|
||||||
|
if (!File("$input${File.separator}Images").exists()) return // So we don't run this from within a fat JAR
|
||||||
|
val atlasList = mutableListOf<String>()
|
||||||
|
for ((file, packFileName) in imageFolders(input)) {
|
||||||
|
atlasList += packFileName
|
||||||
|
packImagesIfOutdated(defaultSettings, file, output, packFileName)
|
||||||
|
}
|
||||||
|
atlasList.remove("game")
|
||||||
|
val listFile = File("$output${File.separator}Atlases.json")
|
||||||
|
if (atlasList.isEmpty()) listFile.delete()
|
||||||
|
else listFile.writeText(atlasList.sorted().joinToString(",","[","]"))
|
||||||
|
}
|
||||||
|
|
||||||
// Process one Image folder, checking for atlas older than contained images first
|
// Process one Image folder, checking for atlas older than contained images first
|
||||||
private fun packImagesIfOutdated(defaultSettings: TexturePacker.Settings, input: String, output: String, packFileName: String) {
|
private fun packImagesIfOutdated(defaultSettings: TexturePacker.Settings, input: String, output: String, packFileName: String) {
|
||||||
fun File.listTree(): Sequence<File> = when {
|
fun File.listTree(): Sequence<File> = when {
|
||||||
@ -112,13 +118,13 @@ internal object ImagePacker {
|
|||||||
|
|
||||||
// Iterator providing all Image folders to process with the destination atlas name
|
// Iterator providing all Image folders to process with the destination atlas name
|
||||||
private data class ImageFolderResult(val folder: String, val atlasName: String)
|
private data class ImageFolderResult(val folder: String, val atlasName: String)
|
||||||
private fun imageFolders() = sequence {
|
private fun imageFolders(path: String) = sequence {
|
||||||
val parent = File("..")
|
val parent = File(path)
|
||||||
for (folder in parent.listFiles()!!) {
|
for (folder in parent.listFiles()!!) {
|
||||||
if (!folder.isDirectory) continue
|
if (!folder.isDirectory) continue
|
||||||
if (folder.nameWithoutExtension != "Images") continue
|
if (folder.nameWithoutExtension != "Images") continue
|
||||||
val atlasName = if (folder.name == "Images") "game" else folder.extension
|
val atlasName = if (folder.name == "Images") "game" else folder.extension
|
||||||
yield(ImageFolderResult("..${File.separator}${folder.name}", atlasName))
|
yield(ImageFolderResult(folder.path, atlasName))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user