blob: 43bbc7cdf8bd2e7607b2cba6098c420e4e1f9492 [file] [log] [blame] [raw]
package li.cil.oc.server.driver
import li.cil.oc
import li.cil.oc.api.driver.Slot
import li.cil.oc.common.item.{Disk, HardDiskDrive}
import li.cil.oc.{Config, Items}
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
object FileSystem extends Item {
override def api = getClass.getResourceAsStream(Config.driverPath + "filesystem.lua")
override def worksWith(item: ItemStack) = WorksWith(Items.hdd1, Items.hdd2, Items.hdd3, Items.disk)(item)
override def slot(item: ItemStack) = Items.multi.subItem(item) match {
case Some(hdd: HardDiskDrive) => Slot.HardDiskDrive
case Some(disk: Disk) => Slot.Disk
case _ => throw new IllegalArgumentException()
}
override def node(item: ItemStack) = Items.multi.subItem(item) match {
case Some(hdd: HardDiskDrive) => createNode(item, hdd.megaBytes * 1024 * 1024)
case Some(disk: Disk) => createNode(item, 512 * 1024)
case _ => null
}
private def createNode(item: ItemStack, capacity: Int) = {
// We have a bit of a chicken-egg problem here, because we want to use the
// node's address as the folder name... so we generate the address here,
// if necessary. No one will know, right? Right!?
val address = addressFromTag(nbt(item))
Option(oc.api.FileSystem.fromSaveDirectory(address, capacity, Config.filesBuffered)).
flatMap(fs => Option(oc.api.FileSystem.asNode(fs))) match {
case Some(node) =>
node.address = Some(address)
node
case None => null
}
}
private def addressFromTag(tag: NBTTagCompound) =
if (tag.hasKey("address")) tag.getString("address")
else java.util.UUID.randomUUID().toString
}