mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-17 11:24:56 -04:00
make ResourceAssetsManager work
This commit is contained in:
parent
0802c48e69
commit
b5d67855c0
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
package de.bixilon.minosoft
|
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.assets.properties.version.AssetsVersionProperties
|
||||||
import de.bixilon.minosoft.config.profile.GlobalProfileManager
|
import de.bixilon.minosoft.config.profile.GlobalProfileManager
|
||||||
import de.bixilon.minosoft.config.profile.delegate.watcher.SimpleProfileDelegateWatcher.Companion.profileWatch
|
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 {
|
object Minosoft {
|
||||||
val MAIN_THREAD: Thread = Thread.currentThread()
|
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 LANGUAGE_MANAGER = MultiLanguageManager()
|
||||||
val START_UP_LATCH = CountUpAndDownLatch(1)
|
val START_UP_LATCH = CountUpAndDownLatch(1)
|
||||||
|
|
||||||
|
@ -13,23 +13,30 @@
|
|||||||
|
|
||||||
package de.bixilon.minosoft.assets.file
|
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 de.bixilon.minosoft.util.CountUpAndDownLatch
|
||||||
import org.reflections.Reflections
|
import java.io.InputStream
|
||||||
import org.reflections.scanners.Scanners
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides resources from a class
|
* Provides assets that are saved in a directory (on your hard drive)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class ResourceAssetsManager(
|
class DirectoryAssetsManager(
|
||||||
private val clazz: Class<*>,
|
private val basePath: String,
|
||||||
) : FileAssetsManager() {
|
) : 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) {
|
override fun load(latch: CountUpAndDownLatch) = Unit
|
||||||
TODO("Not yet implemented")
|
|
||||||
|
override fun get(path: ResourceLocation): InputStream {
|
||||||
|
return FileUtil.readFile(path.filePath, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun nullGet(path: ResourceLocation): InputStream? {
|
||||||
|
return FileUtil.saveReadFile(path.filePath, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -5,5 +5,5 @@ data class AssetsVersionProperty(
|
|||||||
val indexHash: String,
|
val indexHash: String,
|
||||||
val clientJarHash: String,
|
val clientJarHash: String,
|
||||||
val jarAssetsHash: String,
|
val jarAssetsHash: String,
|
||||||
val pixlyzerHash: String,
|
val pixlyzerHash: String?,
|
||||||
)
|
)
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
package de.bixilon.minosoft.assets.util
|
package de.bixilon.minosoft.assets.util
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.module.kotlin.readValue
|
||||||
import com.github.luben.zstd.ZstdInputStream
|
import com.github.luben.zstd.ZstdInputStream
|
||||||
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
|
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
|
||||||
import de.bixilon.minosoft.util.json.Jackson
|
import de.bixilon.minosoft.util.json.Jackson
|
||||||
@ -61,7 +62,7 @@ object FileUtil {
|
|||||||
if (length < 0) {
|
if (length < 0) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
builder.append(String(buffer, 0, length))
|
builder.append(String(buffer, 0, length, Charsets.UTF_8))
|
||||||
}
|
}
|
||||||
if (close) {
|
if (close) {
|
||||||
this.close()
|
this.close()
|
||||||
@ -82,7 +83,7 @@ object FileUtil {
|
|||||||
|
|
||||||
inline fun <reified T> InputStream.readJson(close: Boolean = true): T {
|
inline fun <reified T> InputStream.readJson(close: Boolean = true): T {
|
||||||
try {
|
try {
|
||||||
return Jackson.MAPPER.readValue(this, T::class.java)
|
return Jackson.MAPPER.readValue(this)
|
||||||
} finally {
|
} finally {
|
||||||
if (close) {
|
if (close) {
|
||||||
this.close()
|
this.close()
|
||||||
|
@ -99,7 +99,7 @@ data class Version(
|
|||||||
registries.parentRegistries = Versions.PRE_FLATTENING_MAPPING
|
registries.parentRegistries = Versions.PRE_FLATTENING_MAPPING
|
||||||
}
|
}
|
||||||
val pixlyzerData = try {
|
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) {
|
} 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...
|
// 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 }
|
Log.log(LogMessageType.VERSION_LOADING, level = LogLevels.VERBOSE) { exception }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user