mirror of
https://github.com/Anvilcraft/modpacktools
synced 2024-06-10 22:49:26 +02:00
fix zip stuff
remove modpackjson config option with source dir
This commit is contained in:
parent
07bc8a062a
commit
9406ace044
|
@ -17,7 +17,7 @@ import java.util.concurrent.TimeUnit.MICROSECONDS
|
|||
//Lazy initialization will prevent objects from being initilized if not needed
|
||||
val CONFIG by lazy {Config("modpacktoolsconfig.toml")}
|
||||
val LOADER by lazy {CommandLoader("ley.anvil.modpacktools.commands")}
|
||||
val MPJH by lazy {ModpackJsonHandler(File(CONFIG.config.getPath<String>("Locations/modpackjsonFile")!!))}
|
||||
val MPJH by lazy {ModpackJsonHandler(File(CONFIG.config.getPath<String>("Locations/src")!!, "modpack.json"))}
|
||||
val GSON by lazy {GsonBuilder().setPrettyPrinting().create()}
|
||||
|
||||
//for checking if the client has been initialized when closing it
|
||||
|
|
|
@ -9,7 +9,7 @@ import ley.anvil.modpacktools.command.ICommand
|
|||
import ley.anvil.modpacktools.command.LoadCommand
|
||||
import ley.anvil.modpacktools.util.manifest.convertAStoManifest
|
||||
import ley.anvil.modpacktools.util.mergeTo
|
||||
import ley.anvil.modpacktools.util.zipDir
|
||||
import ley.anvil.modpacktools.util.toZip
|
||||
import org.apache.commons.io.FileUtils
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
|
@ -19,7 +19,7 @@ import java.util.zip.ZipOutputStream
|
|||
@LoadCommand
|
||||
object BuildTwitch : ICommand {
|
||||
override val name: String = "buildtwitch"
|
||||
override val helpMessage: String = "TODO"
|
||||
override val helpMessage: String = "builds a twitch export"
|
||||
private val tmp: File by lazy {File(CONFIG.config.getPath<String>("Locations/tempDir"), "twitch")}
|
||||
|
||||
override fun execute(args: Array<out String>): CommandReturn {
|
||||
|
@ -38,7 +38,7 @@ object BuildTwitch : ICommand {
|
|||
//TODO download & install files
|
||||
for(uf in ml.links) {
|
||||
if(uf.key.isFile) {
|
||||
val file = uf.key.getFile(CONFIG.config.getPath<String>("Locations/modpackjsonFile")!!)
|
||||
val file = uf.key.getFile(CONFIG.config.getPath<String>("Locations/src")!!)
|
||||
if(file.exists()) {
|
||||
when {
|
||||
uf.value == "internal.override" -> {
|
||||
|
@ -48,7 +48,8 @@ object BuildTwitch : ICommand {
|
|||
}
|
||||
|
||||
file.isDirectory -> {
|
||||
file.listFiles()!!.forEach {it.copyTo("overrides")}
|
||||
FileUtils.copyDirectory(file, File(tmp, "overrides"))
|
||||
//file.listFiles()!!.forEach {it.copyTo("overrides")}
|
||||
}
|
||||
|
||||
else -> {
|
||||
|
@ -92,13 +93,13 @@ object BuildTwitch : ICommand {
|
|||
}
|
||||
|
||||
val zip = ZipOutputStream(FileOutputStream(dir.path + "/$archiveName.zip"))
|
||||
zipDir(tmp, zip)
|
||||
tmp.toZip(zip)
|
||||
zip.flush()
|
||||
zip.close()
|
||||
return success("Successfully build twitch zip")
|
||||
}
|
||||
|
||||
private fun File.copyTo(dir: File) = FileUtils.copyFile(this, tmp.mergeTo(dir).apply {mkdirs()}.mergeTo(this))
|
||||
|
||||
private fun File.copyTo(dir: String) = this.copyTo(File(dir))
|
||||
private fun File.copyTo(dir: File) {
|
||||
FileUtils.copyFile(this, tmp.mergeTo(dir).apply {mkdirs()}.mergeTo(this))
|
||||
}
|
||||
}
|
|
@ -9,12 +9,18 @@ import okhttp3.MediaType
|
|||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import java.io.*
|
||||
import java.io.File
|
||||
import java.io.FileReader
|
||||
import java.io.IOException
|
||||
import java.net.URI
|
||||
import java.net.URL
|
||||
import java.nio.file.FileVisitResult
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.SimpleFileVisitor
|
||||
import java.nio.file.attribute.BasicFileAttributes
|
||||
import java.util.zip.ZipEntry
|
||||
import java.util.zip.ZipOutputStream
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KFunction
|
||||
import kotlin.reflect.full.functions
|
||||
|
@ -111,21 +117,27 @@ fun KClass<*>.getFun(name: String): KFunction<*>? = this.functions.find {it.name
|
|||
*/
|
||||
fun File.mergeTo(other: File): File = File(this.path, other.name)
|
||||
|
||||
fun zipDir(dir: File, zip: ZipOutputStream, parent: File? = null) {
|
||||
for (file in dir.listFiles()!!) {
|
||||
if (file.isDirectory) {
|
||||
zipDir(file, zip, parent?.mergeTo(file) ?: file)
|
||||
continue
|
||||
/**
|
||||
* zips a directory.
|
||||
*
|
||||
* @receiver the directory to zip
|
||||
* @param zStream the zip stream to write to
|
||||
*/
|
||||
fun Path.toZip(zStream: ZipOutputStream) {
|
||||
Files.walkFileTree(this, object : SimpleFileVisitor<Path>() {
|
||||
override fun visitFile(file: Path, attrs: BasicFileAttributes?): FileVisitResult {
|
||||
zStream.putNextEntry(ZipEntry(this@toZip.relativize(file).toString()))
|
||||
Files.copy(file, zStream)
|
||||
zStream.closeEntry()
|
||||
return FileVisitResult.CONTINUE
|
||||
}
|
||||
zip.putNextEntry(ZipEntry((parent?.mergeTo(file) ?: file).name))
|
||||
val inp = BufferedInputStream(FileInputStream(file))
|
||||
var bytesRead: Long = 0
|
||||
val bytesIn = ByteArray(4096)
|
||||
var read: Int
|
||||
while (inp.read(bytesIn).also { read = it } != -1) {
|
||||
zip.write(bytesIn, 0, read)
|
||||
bytesRead += read.toLong()
|
||||
}
|
||||
zip.closeEntry()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* zips a directory.
|
||||
*
|
||||
* @receiver the directory to zip
|
||||
* @param zStream the zip stream to write to
|
||||
*/
|
||||
fun File.toZip(zStream: ZipOutputStream) = this.toPath().toZip(zStream)
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
[Locations]
|
||||
#The location of the twitch manifest file (will be replaced with addonscript in later versions)
|
||||
manifestFile = "src/twitch/manifest.json"
|
||||
#The location of the modpackjson file
|
||||
modpackjsonFile = "src/modpack.json"
|
||||
#The location of the source
|
||||
src = "src"
|
||||
#The location used to store temporary files
|
||||
tempDir = ".mpt"
|
||||
[Local]
|
||||
|
|
Loading…
Reference in a new issue