Skip to content

Commit 2b1145d

Browse files
Fix #327
1 parent cf8ff8e commit 2b1145d

File tree

10 files changed

+38
-45
lines changed

10 files changed

+38
-45
lines changed

nova/src/main/kotlin/xyz/xenondevs/nova/data/resources/builder/font/Font.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class Font(
130130
fun fromDisk(assetsDir: Path, fontFile: Path): Font {
131131
val id = readIdFromPath(assetsDir, fontFile)
132132
val providers = fontFile.parseJson().asJsonObject.getArray("providers")
133-
.mapTo(ArrayList()) { FontProvider.fromDisk(assetsDir, it.asJsonObject) }
133+
.mapTo(ArrayList()) { FontProvider.fromDisk(it.asJsonObject) }
134134

135135
return Font(id, providers)
136136
}

nova/src/main/kotlin/xyz/xenondevs/nova/data/resources/builder/font/provider/FontProvider.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ abstract class FontProvider internal constructor() {
4141

4242
companion object {
4343

44-
fun fromDisk(assetsDir: Path, provider: JsonObject): FontProvider =
44+
fun fromDisk(provider: JsonObject): FontProvider =
4545
when (val type = provider.getString("type")) {
4646
"reference" -> ReferenceProvider.of(provider)
4747
"space" -> SpaceProvider.of(provider)
48-
"bitmap" -> MutableBitmapProvider.fromDisk(assetsDir, provider)
49-
"unihex" -> UnihexProvider.fromDisk(assetsDir, provider)
48+
"bitmap" -> MutableBitmapProvider.fromDisk(provider)
49+
"unihex" -> UnihexProvider.fromDisk(provider)
5050
else -> throw UnsupportedOperationException("Unsupported font provider type: $type")
5151
}
5252

nova/src/main/kotlin/xyz/xenondevs/nova/data/resources/builder/font/provider/bitmap/BitmapProvider.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ abstract class BitmapProvider<T> internal constructor() : FontProvider() {
184184
* An immutable [BitmapProvider] that lazily loads a single [BufferedImage] glyph.
185185
*/
186186
private class SingleLazyLoaded(
187-
private val assetsDir: Path,
188187
override val file: ResourcePath,
189188
codePoint: Int,
190189
override val height: Int,
@@ -194,7 +193,7 @@ abstract class BitmapProvider<T> internal constructor() : FontProvider() {
194193
override val glyphImageType = BitmapGlyphImageType.BUFFERED_IMAGE
195194

196195
override val codePointGrid = CodePointGrid.single(codePoint)
197-
override val glyphGrid by lazy { GlyphGrid.single(file.getPath(assetsDir, "textures").readImage()) }
196+
override val glyphGrid by lazy { GlyphGrid.single(file.findInAssets("textures").readImage()) }
198197

199198
// not necessary because the image is loaded from disk and cannot be changed
200199
override fun write(assetsDir: Path) = Unit
@@ -266,15 +265,14 @@ abstract class BitmapProvider<T> internal constructor() : FontProvider() {
266265
/**
267266
* Creates a immutable [BitmapProvider] with a single glyph, assuming the texture will exist.
268267
*
269-
* @param assetsDir The assets directory to use for reading the bitmap image, if required later.
270268
* @param file The path where the texture will be read from and saved to.
271269
* @param codePoint The code point of the glyph.
272270
* @param height The height that glyphs will be rendered as. If this does not match the glyphs actual height,
273271
* the glyphs will be scaled, keeping the aspect ratio. This is the `height` property in json.
274272
* @param ascent The defined character ascent. This is the `ascent` property in json.
275273
*/
276-
fun single(assetsDir: Path, file: ResourcePath, codePoint: Int, height: Int, ascent: Int): BitmapProvider<BufferedImage> =
277-
SingleLazyLoaded(assetsDir, file, codePoint, height, ascent)
274+
fun single(file: ResourcePath, codePoint: Int, height: Int, ascent: Int): BitmapProvider<BufferedImage> =
275+
SingleLazyLoaded(file, codePoint, height, ascent)
278276

279277
}
280278

nova/src/main/kotlin/xyz/xenondevs/nova/data/resources/builder/font/provider/bitmap/MutableBitmapProvider.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ abstract class MutableBitmapProvider<T> : BitmapProvider<T>() {
7676
* A [MutableBitmapProvider] that is read lazily.
7777
*/
7878
private class LazyLoaded(
79-
private val assetsDir: Path,
8079
private val codePointRows: List<String>,
8180
override var file: ResourcePath,
8281
override var height: Int,
@@ -89,7 +88,7 @@ abstract class MutableBitmapProvider<T> : BitmapProvider<T>() {
8988
override val glyphGrid: ReferenceGlyphGrid by lazy(::loadGlyphGrid)
9089

9190
private fun loadGlyphGrid(): ReferenceGlyphGrid {
92-
val img = file.getPath(assetsDir, "textures").readImage()
91+
val img = file.findInAssets("textures").readImage()
9392
val codePoints = codePointGrid
9493

9594
require(img.width % codePoints.width == 0 && img.height % codePoints.height == 0) {
@@ -190,14 +189,13 @@ abstract class MutableBitmapProvider<T> : BitmapProvider<T>() {
190189
/**
191190
* Reads a [BitmapProvider] from disk.
192191
*
193-
* @param assetsDir The assets directory to use for reading the bitmap image.
194192
* @param provider The json object containing the provider data. Might be modified by the resulting [BitmapProvider].
195193
*/
196-
fun fromDisk(assetsDir: Path, provider: JsonObject): MutableBitmapProvider<BufferedImage> {
194+
fun fromDisk(provider: JsonObject): MutableBitmapProvider<BufferedImage> {
197195
val file = provider.getDeserialized<ResourcePath>("file")
198196
val height = provider.getIntOrNull("height") ?: 8
199197
val ascent = provider.getInt("ascent")
200-
return LazyLoaded(assetsDir, provider.getArray("chars").getAllStrings(), file, height, ascent)
198+
return LazyLoaded(provider.getArray("chars").getAllStrings(), file, height, ascent)
201199
}
202200

203201
}

nova/src/main/kotlin/xyz/xenondevs/nova/data/resources/builder/font/provider/unihex/UnihexProvider.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ abstract class UnihexProvider internal constructor(
223223
}
224224

225225
private class LazyLoaded(
226-
private val assetsDir: Path,
227226
hexFile: ResourcePath,
228227
sizeOverrides: List<SizeOverride>
229228
) : UnihexProvider(hexFile) {
@@ -235,7 +234,7 @@ abstract class UnihexProvider internal constructor(
235234
val glyphs = UnihexGlyphs()
236235

237236
// hexFile: zip file that contains .hex files
238-
hexFile.getPath(assetsDir).useZip { zip ->
237+
hexFile.findInAssets().useZip { zip ->
239238
zip.walk()
240239
.filter { it.extension == "hex" }
241240
.forEach { glyphs.merge(UnihexGlyphs.readUnihexFile(it)) }
@@ -259,13 +258,12 @@ abstract class UnihexProvider internal constructor(
259258
/**
260259
* Reads a [UnihexProvider] from disk.
261260
*
262-
* @param assetsDir The assets directory to use for reading the bitmap image.
263261
* @param provider The json object containing the provider data.
264262
*/
265-
fun fromDisk(assetsDir: Path, provider: JsonObject): UnihexProvider {
263+
fun fromDisk(provider: JsonObject): UnihexProvider {
266264
val hexFile = provider.getDeserialized<ResourcePath>("hex_file")
267265
val sizeOverrides = provider.getDeserialized<List<SizeOverride>>("size_overrides")
268-
return LazyLoaded(assetsDir, hexFile, sizeOverrides)
266+
return LazyLoaded(hexFile, sizeOverrides)
269267
}
270268

271269
}

nova/src/main/kotlin/xyz/xenondevs/nova/data/resources/builder/task/font/CustomFontContent.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import xyz.xenondevs.nova.data.resources.builder.font.provider.bitmap.BitmapProv
99
import xyz.xenondevs.nova.data.resources.builder.task.PackTaskHolder
1010
import xyz.xenondevs.nova.util.component.adventure.font
1111
import xyz.xenondevs.nova.util.data.readImageDimensions
12-
import java.nio.file.Path
1312

1413
private const val START_CODE_POINT: Int = 0xE000
1514
private const val END_CODE_POINT: Int = 0xF8FF
@@ -28,11 +27,11 @@ abstract class CustomFontContent internal constructor(
2827
private var currentCodePoint = END_CODE_POINT
2928
private var currentFontNum = -1
3029

31-
fun addEntry(charId: String, assetsDir: Path, image: ResourcePath, height: Int?, ascent: Int) {
32-
addEntry(ResourcePath.of(charId), assetsDir, image, height, ascent)
30+
fun addEntry(charId: String, image: ResourcePath, height: Int?, ascent: Int) {
31+
addEntry(ResourcePath.of(charId), image, height, ascent)
3332
}
3433

35-
fun addEntry(charId: ResourcePath, assetsDir: Path, image: ResourcePath, height: Int?, ascent: Int) {
34+
fun addEntry(charId: ResourcePath, image: ResourcePath, height: Int?, ascent: Int) {
3635
if (++currentCodePoint > 0xF8FF) {
3736
currentCodePoint = START_CODE_POINT
3837
val id = ResourcePath.of(fontNameTemplate.format(++currentFontNum))
@@ -43,7 +42,7 @@ abstract class CustomFontContent internal constructor(
4342
}
4443

4544
currentFont += BitmapProvider.single(
46-
assetsDir, image,
45+
image,
4746
currentCodePoint,
4847
height ?: image.findInAssets("textures").readImageDimensions().height,
4948
ascent

nova/src/main/kotlin/xyz/xenondevs/nova/data/resources/builder/task/font/FontContent.kt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import xyz.xenondevs.nova.data.resources.builder.font.Font
88
import xyz.xenondevs.nova.data.resources.builder.task.PackTask
99
import xyz.xenondevs.nova.data.resources.builder.task.PackTaskHolder
1010
import java.nio.file.Path
11+
import kotlin.io.path.exists
1112
import kotlin.io.path.extension
12-
import kotlin.io.path.notExists
13+
import kotlin.io.path.listDirectoryEntries
1314
import kotlin.io.path.walk
1415

1516
class FontContent internal constructor(private val builder: ResourcePackBuilder) : PackTaskHolder {
@@ -45,7 +46,7 @@ class FontContent internal constructor(private val builder: ResourcePackBuilder)
4546
return _customFonts.getOrPut(id) { Font(id) }
4647
}
4748

48-
fun add(font: Font){
49+
fun add(font: Font) {
4950
_customFonts[font.id] = font
5051
}
5152

@@ -71,20 +72,21 @@ class FontContent internal constructor(private val builder: ResourcePackBuilder)
7172

7273
@PackTask(runAfter = ["ExtractTask#extractAll"])
7374
private fun discoverAllFonts() {
74-
discoverFonts(MCASSETS_ASSETS_DIR, MCASSETS_ASSETS_DIR.resolve("minecraft/font/"), _vanillaFonts)
75-
discoverFonts(ASSETS_DIR, ASSETS_DIR.resolve("minecraft/font/"), _customFonts)
76-
builder.assetPacks.forEach { discoverFonts(ASSETS_DIR, ASSETS_DIR.resolve("${it.namespace}/font/"), _customFonts) }
75+
discoverFonts(MCASSETS_ASSETS_DIR, _vanillaFonts)
76+
discoverFonts(ASSETS_DIR, _customFonts)
7777
}
7878

79-
private fun discoverFonts(assetsDir: Path, fontDir: Path, map: MutableMap<ResourcePath, Font>) {
80-
if (fontDir.notExists())
81-
return
82-
83-
fontDir.walk()
84-
.filter { it.extension.equals("json", true) }
85-
.forEach { path ->
86-
val font = Font.fromDisk(assetsDir, path)
87-
map[font.id] = font
79+
private fun discoverFonts(assetsDir: Path, map: MutableMap<ResourcePath, Font>) {
80+
assetsDir.listDirectoryEntries()
81+
.map { it.resolve("font") }
82+
.filter { it.exists() }
83+
.forEach { fontDir ->
84+
fontDir.walk()
85+
.filter { it.extension.equals("json", true) }
86+
.forEach { path ->
87+
val font = Font.fromDisk(assetsDir, path)
88+
map[font.id] = font
89+
}
8890
}
8991
}
9092

nova/src/main/kotlin/xyz/xenondevs/nova/data/resources/builder/task/font/GuiContent.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class GuiContent internal constructor(
1717
@PackTask(runBefore = ["FontContent#write"])
1818
private fun write() {
1919
builder.assetPacks.forEach { pack ->
20-
pack.guisIndex?.forEach { (id, path) -> addEntry(id, ResourcePackBuilder.ASSETS_DIR, path, null, ASCENT) }
20+
pack.guisIndex?.forEach { (id, path) -> addEntry(id, path, null, ASCENT) }
2121
}
2222

2323
ResourceLookups.GUI_DATA_LOOKUP.set(fontCharLookup)

nova/src/main/kotlin/xyz/xenondevs/nova/data/resources/builder/task/font/TextureIconContent.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ class TextureIconContent internal constructor(
3939
return
4040

4141
val id = ResourcePath(path.namespace, path.path)
42-
val assetsDir = if (path.namespace == "minecraft") ResourcePackBuilder.MCASSETS_ASSETS_DIR else ResourcePackBuilder.ASSETS_DIR
43-
addEntry(id, assetsDir, path.copy(path = path.path + ".png"), HEIGHT, ASCENT)
42+
addEntry(id, path.copy(path = path.path + ".png"), HEIGHT, ASCENT)
4443
added += path
4544
}
4645

nova/src/main/kotlin/xyz/xenondevs/nova/data/resources/builder/task/font/WailaContent.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class WailaContent internal constructor(
234234
val file = ResourcePackBuilder.PACK_DIR.resolve("assets/nova/textures/waila_generated/${id.namespace}/${id.name}.png")
235235
file.parent.createDirectories()
236236
renderer.renderModelToFile(path.toString(), file)
237-
addEntry(id.toString(), ASSETS_DIR, ResourcePath("nova", "waila_generated/${id.namespace}/${id.name}.png"), SIZE, ASCENT)
237+
addEntry(id.toString(), ResourcePath("nova", "waila_generated/${id.namespace}/${id.name}.png"), SIZE, ASCENT)
238238
count++
239239
} catch (e: Exception) {
240240
LOGGER.log(Level.WARNING, "Failed to render $id ($path) ", e)
@@ -261,11 +261,11 @@ class WailaContent internal constructor(
261261
MATERIAL_TEXTURES.forEach { (material, texture) ->
262262
val name = material.name.lowercase()
263263
val path = ResourcePath.of("$texture.png")
264-
addEntry(ResourcePath("minecraft", name), ASSETS_DIR, copyMCTexture(path), SIZE, ASCENT)
264+
addEntry(ResourcePath("minecraft", name), copyMCTexture(path), SIZE, ASCENT)
265265
}
266266

267267
TEXTURES.forEach {
268-
addEntry(ResourcePath("minecraft", it), ASSETS_DIR, copyMCTexture(ResourcePath("minecraft", "block/$it.png")), SIZE, ASCENT)
268+
addEntry(ResourcePath("minecraft", it), copyMCTexture(ResourcePath("minecraft", "block/$it.png")), SIZE, ASCENT)
269269
}
270270
}
271271

@@ -284,7 +284,6 @@ class WailaContent internal constructor(
284284
val idNamespace = pack.namespace.takeUnless { it == "nova" } ?: "minecraft" // all textures form "nova" asset pack are for minecraft blocks
285285
addEntry(
286286
ResourcePath(idNamespace, file.nameWithoutExtension),
287-
ASSETS_DIR,
288287
ResourcePath(pack.namespace, "waila/${file.name}"),
289288
SIZE, ASCENT
290289
)

0 commit comments

Comments
 (0)