make ResourceAssetsManager work

This commit is contained in:
Bixilon 2021-12-11 17:32:06 +01:00
parent 0802c48e69
commit b5d67855c0
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
6 changed files with 48 additions and 15 deletions

View File

@ -13,7 +13,7 @@
package de.bixilon.minosoft
import de.bixilon.minosoft.assets.file.ResourceAssetsManager
import de.bixilon.minosoft.assets.file.ResourcesAssetsUtil
import de.bixilon.minosoft.assets.properties.version.AssetsVersionProperties
import de.bixilon.minosoft.config.profile.GlobalProfileManager
import de.bixilon.minosoft.config.profile.delegate.watcher.SimpleProfileDelegateWatcher.Companion.profileWatch
@ -48,7 +48,7 @@ import de.bixilon.minosoft.util.task.worker.tasks.Task
object Minosoft {
val MAIN_THREAD: Thread = Thread.currentThread()
val MINOSOFT_ASSETS_MANAGER = ResourceAssetsManager(Minosoft::class.java)
val MINOSOFT_ASSETS_MANAGER = ResourcesAssetsUtil.create(Minosoft::class.java)
val LANGUAGE_MANAGER = MultiLanguageManager()
val START_UP_LATCH = CountUpAndDownLatch(1)

View File

@ -13,23 +13,30 @@
package de.bixilon.minosoft.assets.file
import de.bixilon.minosoft.assets.util.FileUtil
import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.util.CountUpAndDownLatch
import org.reflections.Reflections
import org.reflections.scanners.Scanners
import java.io.InputStream
/**
* Provides resources from a class
* Provides assets that are saved in a directory (on your hard drive)
*/
class ResourceAssetsManager(
private val clazz: Class<*>,
class DirectoryAssetsManager(
private val basePath: String,
) : FileAssetsManager() {
var reflections = Reflections(clazz::class.java, Scanners.Resources)
var resourceList: Set<String> = reflections.getResources("assets")
private val ResourceLocation.filePath: String
get() = "$basePath/$namespace/$path"
override fun load(latch: CountUpAndDownLatch) {
TODO("Not yet implemented")
override fun load(latch: CountUpAndDownLatch) = Unit
override fun get(path: ResourceLocation): InputStream {
return FileUtil.readFile(path.filePath, false)
}
override fun nullGet(path: ResourceLocation): InputStream? {
return FileUtil.saveReadFile(path.filePath, false)
}
}

View File

@ -0,0 +1,25 @@
package de.bixilon.minosoft.assets.file
import de.bixilon.minosoft.assets.AssetsManager
import java.io.FileNotFoundException
import java.net.URLDecoder
import java.nio.charset.StandardCharsets
object ResourcesAssetsUtil {
fun create(clazz: Class<*>): AssetsManager {
val rootResources = clazz.classLoader.getResource("assets") ?: throw FileNotFoundException("Can not find assets folder in $clazz")
return when (rootResources.protocol) {
"file" -> DirectoryAssetsManager(rootResources.path)// Read them directly from the folder
"jar" -> {
val path: String = rootResources.path
val jarPath = path.substring(5, path.indexOf("!"))
val zip = URLDecoder.decode(jarPath, StandardCharsets.UTF_8)
ZipAssetsManager(zip)
}
else -> TODO("Can not read resources: $rootResources")
}
}
}

View File

@ -5,5 +5,5 @@ data class AssetsVersionProperty(
val indexHash: String,
val clientJarHash: String,
val jarAssetsHash: String,
val pixlyzerHash: String,
val pixlyzerHash: String?,
)

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.assets.util
import com.fasterxml.jackson.module.kotlin.readValue
import com.github.luben.zstd.ZstdInputStream
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import de.bixilon.minosoft.util.json.Jackson
@ -61,7 +62,7 @@ object FileUtil {
if (length < 0) {
break
}
builder.append(String(buffer, 0, length))
builder.append(String(buffer, 0, length, Charsets.UTF_8))
}
if (close) {
this.close()
@ -82,7 +83,7 @@ object FileUtil {
inline fun <reified T> InputStream.readJson(close: Boolean = true): T {
try {
return Jackson.MAPPER.readValue(this, T::class.java)
return Jackson.MAPPER.readValue(this)
} finally {
if (close) {
this.close()

View File

@ -99,7 +99,7 @@ data class Version(
registries.parentRegistries = Versions.PRE_FLATTENING_MAPPING
}
val pixlyzerData = try {
MBFBinaryReader(FileUtil.readFile(FileAssetsUtil.getPath(AssetsVersionProperties[this]!!.pixlyzerHash), false)).readMBF().data.asCompound()
MBFBinaryReader(FileUtil.readFile(FileAssetsUtil.getPath(AssetsVersionProperties[this]!!.pixlyzerHash ?: throw IllegalStateException("$this has no pixlyzer data!")), false)).readMBF().data.asCompound()
} catch (exception: Throwable) {
// should not happen, but if this version is not flattened, we can fallback to the flatten mappings. Some things might not work...
Log.log(LogMessageType.VERSION_LOADING, level = LogLevels.VERBOSE) { exception }