/
DriverFileSystem.scala
86 lines (73 loc) · 3.17 KB
/
DriverFileSystem.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package li.cil.oc.integration.opencomputers
import li.cil.oc
import li.cil.oc.Constants
import li.cil.oc.Settings
import li.cil.oc.api
import li.cil.oc.api.driver.EnvironmentHost
import li.cil.oc.common.Slot
import li.cil.oc.common.item.Delegator
import li.cil.oc.common.item.FloppyDisk
import li.cil.oc.common.item.HardDiskDrive
import li.cil.oc.server.fs.FileSystem.ItemLabel
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
object DriverFileSystem extends Item {
override def worksWith(stack: ItemStack) = isOneOf(stack,
api.Items.get(Constants.ItemName.HDDTier1),
api.Items.get(Constants.ItemName.HDDTier2),
api.Items.get(Constants.ItemName.HDDTier3),
api.Items.get(Constants.ItemName.Floppy))
override def createEnvironment(stack: ItemStack, host: EnvironmentHost) =
Delegator.subItem(stack) match {
case Some(hdd: HardDiskDrive) => createEnvironment(stack, hdd.kiloBytes * 1024, host, hdd.tier + 2)
case Some(disk: FloppyDisk) => createEnvironment(stack, Settings.get.floppySize * 1024, host, 1)
case _ => null
}
override def slot(stack: ItemStack) =
Delegator.subItem(stack) match {
case Some(hdd: HardDiskDrive) => Slot.HDD
case Some(disk: FloppyDisk) => Slot.Floppy
case _ => throw new IllegalArgumentException()
}
override def tier(stack: ItemStack) =
Delegator.subItem(stack) match {
case Some(hdd: HardDiskDrive) => hdd.tier
case _ => 0
}
private def createEnvironment(stack: ItemStack, capacity: Int, host: EnvironmentHost, speed: 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(dataTag(stack))
val isFloppy = api.Items.get(stack) == api.Items.get(Constants.ItemName.Floppy)
val fs = oc.api.FileSystem.fromSaveDirectory(address, capacity, Settings.get.bufferChanges)
val environment = oc.api.FileSystem.asManagedEnvironment(fs, new ReadWriteItemLabel(stack), host, Settings.resourceDomain + ":" + (if (isFloppy) "floppy_access" else "hdd_access"), speed)
if (environment != null && environment.node != null) {
environment.node.asInstanceOf[oc.server.network.Node].address = address
}
environment
}
private def addressFromTag(tag: NBTTagCompound) =
if (tag.hasKey("node") && tag.getCompoundTag("node").hasKey("address")) {
tag.getCompoundTag("node").getString("address")
}
else java.util.UUID.randomUUID().toString
private class ReadWriteItemLabel(stack: ItemStack) extends ItemLabel(stack) {
var label: Option[String] = None
override def getLabel = label.orNull
override def setLabel(value: String) {
label = Option(value).map(_.take(16))
}
override def load(nbt: NBTTagCompound) {
if (nbt.hasKey(Settings.namespace + "fs.label")) {
label = Option(nbt.getString(Settings.namespace + "fs.label"))
}
}
override def save(nbt: NBTTagCompound) {
label match {
case Some(value) => nbt.setString(Settings.namespace + "fs.label", value)
case _ =>
}
}
}
}