mirror of
https://github.com/packwiz/packwiz-installer.git
synced 2025-10-26 09:34:32 +01:00
Start porting to Kotlin
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package link.infra.packwiz.installer.request
|
||||
|
||||
import link.infra.packwiz.installer.metadata.SpaceSafeURI
|
||||
import okio.Source
|
||||
|
||||
/**
|
||||
* IRequestHandler handles requests for locations specified in modpack metadata.
|
||||
*/
|
||||
interface IRequestHandler {
|
||||
fun matchesHandler(loc: SpaceSafeURI): Boolean
|
||||
|
||||
fun getNewLoc(loc: SpaceSafeURI): SpaceSafeURI {
|
||||
return loc
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Source for a location. Must be threadsafe.
|
||||
* It is assumed that each location is read only once for the duration of an IRequestHandler.
|
||||
* @param loc The location to be read
|
||||
* @return The Source containing the data of the file
|
||||
* @throws Exception Exception if it failed to download a file!!!
|
||||
*/
|
||||
fun getFileSource(loc: SpaceSafeURI): Source?
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package link.infra.packwiz.installer.request.handlers
|
||||
|
||||
import link.infra.packwiz.installer.metadata.SpaceSafeURI
|
||||
import java.util.*
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock
|
||||
import java.util.regex.Pattern
|
||||
import kotlin.concurrent.read
|
||||
import kotlin.concurrent.write
|
||||
|
||||
class RequestHandlerGithub : RequestHandlerZip(true) {
|
||||
override fun getNewLoc(loc: SpaceSafeURI): SpaceSafeURI {
|
||||
return loc
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val repoMatcherPattern = Pattern.compile("/([\\w.-]+/[\\w.-]+).*")
|
||||
private val branchMatcherPattern = Pattern.compile("/[\\w.-]+/[\\w.-]+/blob/([\\w.-]+).*")
|
||||
}
|
||||
|
||||
// TODO: is caching really needed, if HTTPURLConnection follows redirects correctly?
|
||||
private val zipUriMap: MutableMap<String, SpaceSafeURI> = HashMap()
|
||||
private val zipUriLock = ReentrantReadWriteLock()
|
||||
private fun getRepoName(loc: SpaceSafeURI): String? {
|
||||
val matcher = repoMatcherPattern.matcher(loc.path)
|
||||
return if (matcher.matches()) {
|
||||
matcher.group(1)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun getZipUri(loc: SpaceSafeURI): SpaceSafeURI {
|
||||
val repoName = getRepoName(loc)
|
||||
val branchName = getBranch(loc)
|
||||
|
||||
zipUriLock.read {
|
||||
zipUriMap["$repoName/$branchName"]
|
||||
}?.let { return it }
|
||||
|
||||
var zipUri = SpaceSafeURI("https://api.github.com/repos/$repoName/zipball/$branchName")
|
||||
zipUriLock.write {
|
||||
// If another thread sets the value concurrently, use the existing value from the
|
||||
// thread that first acquired the lock.
|
||||
zipUri = zipUriMap.putIfAbsent("$repoName/$branchName", zipUri) ?: zipUri
|
||||
}
|
||||
return zipUri
|
||||
}
|
||||
|
||||
private fun getBranch(loc: SpaceSafeURI): String? {
|
||||
val matcher = branchMatcherPattern.matcher(loc.path)
|
||||
return if (matcher.matches()) {
|
||||
matcher.group(1)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun getLocationInZip(loc: SpaceSafeURI): SpaceSafeURI {
|
||||
val path = "/" + getRepoName(loc) + "/blob/" + getBranch(loc)
|
||||
return SpaceSafeURI(loc.scheme, loc.authority, path, null, null).relativize(loc)
|
||||
}
|
||||
|
||||
override fun matchesHandler(loc: SpaceSafeURI): Boolean {
|
||||
val scheme = loc.scheme
|
||||
if (!("http" == scheme || "https" == scheme)) {
|
||||
return false
|
||||
}
|
||||
return "github.com" == loc.host
|
||||
// TODO: sanity checks, support for more github urls
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package link.infra.packwiz.installer.request.handlers
|
||||
|
||||
import link.infra.packwiz.installer.metadata.SpaceSafeURI
|
||||
import link.infra.packwiz.installer.request.IRequestHandler
|
||||
import okio.Source
|
||||
import okio.source
|
||||
|
||||
open class RequestHandlerHTTP : IRequestHandler {
|
||||
override fun matchesHandler(loc: SpaceSafeURI): Boolean {
|
||||
val scheme = loc.scheme
|
||||
return "http" == scheme || "https" == scheme
|
||||
}
|
||||
|
||||
override fun getFileSource(loc: SpaceSafeURI): Source? {
|
||||
val conn = loc.toURL().openConnection()
|
||||
// TODO: when do we send specific headers??? should there be a way to signal this?
|
||||
// github *sometimes* requires it, sometimes not!
|
||||
//conn.addRequestProperty("Accept", "application/octet-stream");
|
||||
conn.apply {
|
||||
// 30 second read timeout
|
||||
readTimeout = 30 * 1000
|
||||
}
|
||||
return conn.getInputStream().source()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package link.infra.packwiz.installer.request.handlers
|
||||
|
||||
import link.infra.packwiz.installer.metadata.SpaceSafeURI
|
||||
import okio.Buffer
|
||||
import okio.Source
|
||||
import okio.buffer
|
||||
import okio.source
|
||||
import java.util.*
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock
|
||||
import java.util.function.Predicate
|
||||
import java.util.zip.ZipEntry
|
||||
import java.util.zip.ZipInputStream
|
||||
import kotlin.concurrent.read
|
||||
import kotlin.concurrent.withLock
|
||||
import kotlin.concurrent.write
|
||||
|
||||
abstract class RequestHandlerZip(private val modeHasFolder: Boolean) : RequestHandlerHTTP() {
|
||||
private fun removeFolder(name: String): String {
|
||||
return if (modeHasFolder) {
|
||||
// TODO: replace with proper path checks once switched to Path??
|
||||
name.substring(name.indexOf("/") + 1)
|
||||
} else {
|
||||
name
|
||||
}
|
||||
}
|
||||
|
||||
private inner class ZipReader internal constructor(zip: Source) {
|
||||
private val zis = ZipInputStream(zip.buffer().inputStream())
|
||||
private val readFiles: MutableMap<SpaceSafeURI, Buffer> = HashMap()
|
||||
// Write lock implies access to ZipInputStream - only 1 thread must read at a time!
|
||||
val filesLock = ReentrantLock()
|
||||
private var entry: ZipEntry? = null
|
||||
|
||||
private val zipSource = zis.source().buffer()
|
||||
|
||||
// File lock must be obtained before calling this function
|
||||
private fun readCurrFile(): Buffer {
|
||||
val fileBuffer = Buffer()
|
||||
zipSource.readFully(fileBuffer, entry!!.size)
|
||||
return fileBuffer
|
||||
}
|
||||
|
||||
// File lock must be obtained before calling this function
|
||||
private fun findFile(loc: SpaceSafeURI): Buffer? {
|
||||
while (true) {
|
||||
entry = zis.nextEntry
|
||||
entry?.also {
|
||||
val data = readCurrFile()
|
||||
val fileLoc = SpaceSafeURI(removeFolder(it.name))
|
||||
if (loc == fileLoc) {
|
||||
return data
|
||||
} else {
|
||||
readFiles[fileLoc] = data
|
||||
}
|
||||
} ?: return null
|
||||
}
|
||||
}
|
||||
|
||||
fun getFileSource(loc: SpaceSafeURI): Source? {
|
||||
filesLock.withLock {
|
||||
// Assume files are only read once, allow GC by removing
|
||||
readFiles.remove(loc)?.also { return it }
|
||||
return findFile(loc)
|
||||
}
|
||||
}
|
||||
|
||||
fun findInZip(matches: Predicate<SpaceSafeURI>): SpaceSafeURI? {
|
||||
filesLock.withLock {
|
||||
readFiles.keys.find { matches.test(it) }?.let { return it }
|
||||
|
||||
do {
|
||||
val entry = zis.nextEntry?.also {
|
||||
val data = readCurrFile()
|
||||
val fileLoc = SpaceSafeURI(removeFolder(it.name))
|
||||
readFiles[fileLoc] = data
|
||||
if (matches.test(fileLoc)) {
|
||||
return fileLoc
|
||||
}
|
||||
}
|
||||
} while (entry != null)
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val cache: MutableMap<SpaceSafeURI, ZipReader> = HashMap()
|
||||
private val cacheLock = ReentrantReadWriteLock()
|
||||
|
||||
protected abstract fun getZipUri(loc: SpaceSafeURI): SpaceSafeURI
|
||||
protected abstract fun getLocationInZip(loc: SpaceSafeURI): SpaceSafeURI
|
||||
abstract override fun matchesHandler(loc: SpaceSafeURI): Boolean
|
||||
|
||||
override fun getFileSource(loc: SpaceSafeURI): Source? {
|
||||
val zipUri = getZipUri(loc)
|
||||
var zr = cacheLock.read { cache[zipUri] }
|
||||
if (zr == null) {
|
||||
cacheLock.write {
|
||||
// Recheck, because unlocking read lock allows another thread to modify it
|
||||
zr = cache[zipUri]
|
||||
|
||||
if (zr == null) {
|
||||
val src = super.getFileSource(zipUri) ?: return null
|
||||
zr = ZipReader(src).also { cache[zipUri] = it }
|
||||
}
|
||||
}
|
||||
}
|
||||
return zr?.getFileSource(getLocationInZip(loc))
|
||||
}
|
||||
|
||||
protected fun findInZip(loc: SpaceSafeURI, matches: Predicate<SpaceSafeURI>): SpaceSafeURI? {
|
||||
val zipUri = getZipUri(loc)
|
||||
return (cacheLock.read { cache[zipUri] } ?: cacheLock.write {
|
||||
// Recheck, because unlocking read lock allows another thread to modify it
|
||||
cache[zipUri] ?: run {
|
||||
// Create the ZipReader if it doesn't exist, return null if getFileSource returns null
|
||||
super.getFileSource(zipUri)?.let { ZipReader(it) }
|
||||
?.also { cache[zipUri] = it }
|
||||
}
|
||||
})?.findInZip(matches)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user