fix some assets bugs

This commit is contained in:
Bixilon 2021-12-16 00:01:13 +01:00
parent 215187615c
commit 048edd04e3
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
9 changed files with 23 additions and 25 deletions

View File

@ -435,9 +435,9 @@
<version>2.13.0</version> <version>2.13.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.kamranzafar</groupId>
<artifactId>commons-compress</artifactId> <artifactId>jtar</artifactId>
<version>1.21</version> <version>2.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.reflections</groupId> <groupId>org.reflections</groupId>

View File

@ -25,8 +25,9 @@ import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import de.bixilon.minosoft.util.CountUpAndDownLatch import de.bixilon.minosoft.util.CountUpAndDownLatch
import de.bixilon.minosoft.util.KUtil.toResourceLocation import de.bixilon.minosoft.util.KUtil.toResourceLocation
import de.bixilon.minosoft.util.Util import de.bixilon.minosoft.util.Util
import org.apache.commons.compress.archivers.tar.TarArchiveEntry import org.kamranzafar.jtar.TarEntry
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream import org.kamranzafar.jtar.TarHeader
import org.kamranzafar.jtar.TarOutputStream
import java.io.* import java.io.*
/** /**
@ -49,7 +50,7 @@ class JarAssetsManager(
check(!loaded) { "Already loaded!" } check(!loaded) { "Already loaded!" }
val jarAssetFile = File(FileAssetsUtil.getPath(jarAssetsHash)) val jarAssetFile = File(FileAssetsUtil.getPath(jarAssetsHash))
if (FileAssetsUtil.verifyAsset(jarAssetsHash, jarAssetFile, profile.verify, FileAssetsUtil.HashTypes.SHA1)) { if (FileAssetsUtil.verifyAsset(jarAssetsHash, jarAssetFile, profile.verify)) {
val jarAssets = FileUtil.readFile(jarAssetFile).readArchive() val jarAssets = FileUtil.readFile(jarAssetFile).readArchive()
for ((path, data) in jarAssets) { for ((path, data) in jarAssets) {
this.jarAssets[path.removePrefix("assets/" + ProtocolDefinition.DEFAULT_NAMESPACE + "/")] = data this.jarAssets[path.removePrefix("assets/" + ProtocolDefinition.DEFAULT_NAMESPACE + "/")] = data
@ -60,14 +61,14 @@ class JarAssetsManager(
val downloaded = FileAssetsUtil.downloadAndGetAsset(Util.formatString(profile.source.launcherPackages, mapOf( val downloaded = FileAssetsUtil.downloadAndGetAsset(Util.formatString(profile.source.launcherPackages, mapOf(
"fullHash" to clientJarHash, "fullHash" to clientJarHash,
"filename" to "client.jar", "filename" to "client.jar",
)), false) )), false, FileAssetsUtil.HashTypes.SHA1)
check(downloaded.first == clientJarHash) { "Minecraft client.jar verification failed!" } check(downloaded.first == clientJarHash) { "Minecraft client.jar verification failed!" }
clientJar = ByteArrayInputStream(downloaded.second).readZipArchive() clientJar = ByteArrayInputStream(downloaded.second).readZipArchive()
} }
val buildingJarAsset: MutableMap<String, ByteArray> = mutableMapOf() val buildingJarAsset: MutableMap<String, ByteArray> = mutableMapOf()
val byteOutputStream = ByteArrayOutputStream(5_000_0000) // ToDo: Memory optimize this val byteOutputStream = ByteArrayOutputStream(10_000_0000) // ToDo: Memory optimize this
val tarOutputStream = TarArchiveOutputStream(byteOutputStream) val tarOutputStream = TarOutputStream(byteOutputStream)
for ((filename, data) in clientJar) { for ((filename, data) in clientJar) {
if (!filename.startsWith("assets/")) { if (!filename.startsWith("assets/")) {
continue continue
@ -89,17 +90,16 @@ class JarAssetsManager(
if (!required) { if (!required) {
continue continue
} }
val entry = TarArchiveEntry(filename) buildingJarAsset[cutFilename] = data
entry.size = data.size.toLong() tarOutputStream.putNextEntry(TarEntry(TarHeader.createHeader(filename, data.size.toLong(), 0L, false, 777)))
tarOutputStream.putArchiveEntry(entry)
tarOutputStream.write(data) tarOutputStream.write(data)
tarOutputStream.closeArchiveEntry() tarOutputStream.flush()
} }
tarOutputStream.close() tarOutputStream.close()
val savedHash = FileAssetsUtil.saveAsset(byteOutputStream.toByteArray()) val savedHash = FileAssetsUtil.saveAsset(byteOutputStream.toByteArray())
File(FileAssetsUtil.getPath(clientJarHash)).delete() File(FileAssetsUtil.getPath(clientJarHash)).delete()
if (savedHash != jarAssetsHash) { if (savedHash != jarAssetsHash) {
throw InvalidAssetException("".toResourceLocation(), savedHash, jarAssetsHash) throw InvalidAssetException("jar_assets".toResourceLocation(), savedHash, jarAssetsHash)
} }
this.jarAssets = buildingJarAsset this.jarAssets = buildingJarAsset

View File

@ -19,7 +19,7 @@ object AssetsPropertiesGenerator {
profile.verify = false profile.verify = false
val (versionId, clientJarHash) = args val (versionId, clientJarHash) = args
val assetsManager = JarAssetsManager("dummy", clientJarHash, profile, Version(versionId, -1, -1, HashBiMap.create(), HashBiMap.create())) val assetsManager = JarAssetsManager("abcdef", clientJarHash, profile, Version(versionId, -1, -1, HashBiMap.create(), HashBiMap.create()))
try { try {
assetsManager.load(CountUpAndDownLatch(1)) assetsManager.load(CountUpAndDownLatch(1))
} catch (exception: InvalidAssetException) { } catch (exception: InvalidAssetException) {

View File

@ -31,7 +31,7 @@ object FileAssetsUtil {
fun getPath(hash: String): String { fun getPath(hash: String): String {
if (!hash.isHexString) { if (!hash.isHexString) {
throw IllegalArgumentException("String is not a hex string. Invalid data or manipulated?") throw IllegalArgumentException("String is not a hex string. Invalid data or manipulated?: $hash")
} }
return BASE_PATH + hash.substring(0, 2) + "/" + hash return BASE_PATH + hash.substring(0, 2) + "/" + hash
} }
@ -52,7 +52,7 @@ object FileAssetsUtil {
tempFile.parentFile.apply { tempFile.parentFile.apply {
mkdirs() mkdirs()
if (!isDirectory) { if (!isDirectory) {
throw IllegalStateException("Could not create folder: ${tempFile.parentFile}") throw IllegalStateException("Could not create folder: $this")
} }
} }
val returnStream = if (get) { val returnStream = if (get) {
@ -63,7 +63,7 @@ object FileAssetsUtil {
val digest = hashType.createDigest() val digest = hashType.createDigest()
var output: OutputStream = FileOutputStream(tempFile) var output: OutputStream = FileOutputStream(tempFile)
if (compress) { if (compress) {
output = ZstdOutputStream(output) output = ZstdOutputStream(output, 5)
} }
val buffer = ByteArray(ProtocolDefinition.DEFAULT_BUFFER_SIZE) val buffer = ByteArray(ProtocolDefinition.DEFAULT_BUFFER_SIZE)

View File

@ -20,7 +20,7 @@ import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import de.bixilon.minosoft.util.KUtil.unsafeCast import de.bixilon.minosoft.util.KUtil.unsafeCast
import de.bixilon.minosoft.util.json.Jackson import de.bixilon.minosoft.util.json.Jackson
import de.matthiasmann.twl.utils.PNGDecoder import de.matthiasmann.twl.utils.PNGDecoder
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream import org.kamranzafar.jtar.TarInputStream
import org.lwjgl.BufferUtils import org.lwjgl.BufferUtils
import java.io.File import java.io.File
import java.io.FileInputStream import java.io.FileInputStream
@ -95,7 +95,7 @@ object FileUtil {
fun InputStream.readArchive(): Map<String, ByteArray> { fun InputStream.readArchive(): Map<String, ByteArray> {
val content: MutableMap<String, ByteArray> = mutableMapOf() val content: MutableMap<String, ByteArray> = mutableMapOf()
val stream = TarArchiveInputStream(this) val stream = TarInputStream(this)
while (true) { while (true) {
val entry = stream.nextEntry ?: break val entry = stream.nextEntry ?: break
content[entry.name] = stream.readAllBytes() content[entry.name] = stream.readAllBytes()

View File

@ -35,7 +35,7 @@ object FaviconManager {
file.delete() // ToDo: Check if other servers are using it file.delete() // ToDo: Check if other servers are using it
return return
} }
val outputStream = ZstdOutputStream(FileOutputStream(file)) val outputStream = ZstdOutputStream(FileOutputStream(file), 5)
outputStream.write(favicon) outputStream.write(favicon)
outputStream.close() outputStream.close()

View File

@ -167,8 +167,6 @@ public final class Util {
} }
private static String hash(MessageDigest digest, InputStream inputStream) throws IOException { private static String hash(MessageDigest digest, InputStream inputStream) throws IOException {
digest.reset();
byte[] buffer = new byte[ProtocolDefinition.DEFAULT_BUFFER_SIZE]; byte[] buffer = new byte[ProtocolDefinition.DEFAULT_BUFFER_SIZE];
int length; int length;
while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) { while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) {

File diff suppressed because one or more lines are too long

View File

@ -28,7 +28,7 @@ SKIP_COMPILE = True
def generateJarAssets(versionId, assetsProperties): def generateJarAssets(versionId, assetsProperties):
process = subprocess.Popen(r'mvn -q exec:java -Dexec.mainClass="de.bixilon.minosoft.assets.properties.version.generator.AssetsPropertiesGenerator" -Dexec.args="\"%s\" \"%s\""' % (versionId, assetsProperties["client_jar_hash"]), shell=True, cwd='../', stdout=subprocess.PIPE, stderr=subprocess.PIPE) process = subprocess.Popen(r'mvn -e -q exec:java -Dexec.mainClass="de.bixilon.minosoft.assets.properties.version.generator.AssetsPropertiesGenerator" -Dexec.args="\"%s\" \"%s\""' % (versionId, assetsProperties["client_jar_hash"]), shell=True, cwd='../', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
exitCode = process.wait() exitCode = process.wait()
if exitCode != 0: if exitCode != 0:
print(process.stdout.read().decode('utf-8')) print(process.stdout.read().decode('utf-8'))