You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.2 KiB
Kotlin

package net.brokenmoon.redcontrol.api
import com.simon816.j65el02.device.ByteDiskDriver
import com.simon816.j65el02.device.DiskDriver
import com.simon816.j65el02.device.FileDiskDriver
import net.brokenmoon.redcontrol.RedControl
import net.fabricmc.loader.api.FabricLoader
import net.minecraft.data.client.BlockStateVariantMap.TriFunction
import java.net.URI
import kotlin.io.path.absolute
import kotlin.io.path.createDirectories
import kotlin.io.path.exists
/**
* A factory that takes a URI,String,String and produces a DiskDriver for the URI (or null if it cannot make one)
*/
object DriveFactory {
private val gamedir = FabricLoader.getInstance().gameDir.resolve("redbin").absolute()
/**
* a map of uri protocol -> tri funcctions to create drivers for ones not explicitly handled by the mod
*/
val extendedDrivers = HashMap<String,TriFunction<URI,String,String,DiskDriver?>>()
/**
* creates a driver from the given URI
*/
fun createDriver(uri: URI, name: String, serial: String): DiskDriver? {
if (!gamedir.exists()) {gamedir.createDirectories()}
return when (uri.scheme) {
"img" -> {
val bytes = RedControl.images[uri.host+uri.path]?: return null
ByteDiskDriver(bytes,name,serial)
}
"save" -> {
val path = gamedir.resolve(uri.host+uri.path).absolute()
if (path.startsWith(gamedir)) {
return FileDiskDriver(path,name,serial,false)
} else { return null }
}
"rosave" -> {
val path = gamedir.resolve(uri.host+uri.path).absolute()
if (path.startsWith(gamedir)) {
return FileDiskDriver(path,name,serial,false)
} else { return null }
}
"http", "https" -> {return null}
else -> {
if (extendedDrivers.containsKey(uri.scheme)) {
return extendedDrivers[uri.scheme]!!.apply(uri,name,serial)
} else {return null}
}
}
}
}
@FunctionalInterface
interface PentaFunction<A1,A2,A3,A4,R> {
fun execute(a1: A1,a2: A2,a3: A3,a4: A4): R
}