HMCL modpack import

This commit is contained in:
huangyuhui
2017-08-22 20:15:26 +08:00
parent 0cc7d621bc
commit d83291eac0
9 changed files with 186 additions and 21 deletions

View File

@@ -30,7 +30,6 @@ import java.io.File
import java.io.IOException
import java.net.URL
import java.util.logging.Level
import java.util.zip.ZipFile
class CurseForgeModpackManifest @JvmOverloads constructor(
@SerializedName("manifestType")
@@ -54,6 +53,8 @@ class CurseForgeModpackManifest @JvmOverloads constructor(
check(manifestType == MINECRAFT_MODPACK, { "Only support Minecraft modpack" })
}
fun toModpack() = Modpack(name = name, author = author, version = version, description = "No description")
companion object {
val MINECRAFT_MODPACK = "minecraftModpack"
}
@@ -101,14 +102,15 @@ class CurseForgeModpackManifestFile (
/**
* @param f the CurseForge modpack file.
* @throws IOException if the file is not a valid zip file.
* @throws JsonParseException if the manifest.json is missing or malformed.
* @return the manifest.
*/
@Throws(IOException::class, JsonParseException::class)
fun readCurseForgeModpackManifest(f: File): CurseForgeModpackManifest {
ZipFile(f).use { zipFile ->
val entry = zipFile.getEntry("manifest.json") ?: throw IOException("`manifest.json` not found. Not a valid CurseForge modpack.")
val json = zipFile.getInputStream(entry).readFullyAsString()
return GSON.fromJson<CurseForgeModpackManifest>(json) ?: throw JsonParseException("`manifest.json` not found. Not a valid CurseForge modpack.")
}
val json = readTextFromZipFile(f, "manifest.json")
return GSON.fromJson<CurseForgeModpackManifest>(json)
?: throw JsonParseException("`manifest.json` not found. Not a valid CurseForge modpack.")
}
/**
@@ -123,8 +125,7 @@ fun readCurseForgeModpackManifest(f: File): CurseForgeModpackManifest {
class CurseForgeModpackInstallTask(private val dependencyManager: DefaultDependencyManager, private val zipFile: File, private val manifest: CurseForgeModpackManifest, private val name: String): Task() {
val repository = dependencyManager.repository
init {
if (repository.hasVersion(name))
throw IllegalStateException("Version $name already exists.")
check(!repository.hasVersion(name), { "Version $name already exists." })
}
val root = repository.getVersionRoot(name)

View File

@@ -17,8 +17,10 @@
*/
package org.jackhuang.hmcl.mod
import java.io.File
class Modpack(val file: File) {
}
data class Modpack @JvmOverloads constructor(
val name: String = "",
val author: String = "",
val version: String = "",
val description: String = "",
val manifest: Any? = null
)

View File

@@ -20,6 +20,7 @@ package org.jackhuang.hmcl.util
import java.io.File
import java.io.IOException
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream
@@ -194,4 +195,35 @@ fun unzipSubDirectory(zip: File, dest: File, subDirectory: String, ignoreExisten
}
}
}
}
/**
* Read the text content of a file in zip.
*
* @param f the zip file
* @param location the location of the text in zip file, something like A/B/C/D.txt
* @throws IOException if the file is not a valid zip file.
* @return the content of given file.
*/
@Throws(IOException::class)
fun readTextFromZipFile(f: File, location: String): String {
ZipFile(f).use { zipFile ->
val entry = zipFile.getEntry(location) ?: throw IOException("`$location` not found.")
return zipFile.getInputStream(entry).readFullyAsString()
}
}
/**
* Read the text content of a file in zip.
*
* @param f the zip file
* @param location the location of the text in zip file, something like A/B/C/D.txt
* @return the content of given file.
*/
fun readTextFromZipFileQuietly(f: File, location: String): String? {
try {
return readTextFromZipFile(f, location)
} catch (e: IOException) {
return null
}
}