-
Notifications
You must be signed in to change notification settings - Fork 424
/
GraphicsCard.scala
421 lines (368 loc) · 18.2 KB
/
GraphicsCard.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package li.cil.oc.server.component
import java.util
import li.cil.oc.Constants
import li.cil.oc.Localization
import li.cil.oc.Settings
import li.cil.oc.api
import li.cil.oc.api.Network
import li.cil.oc.api.driver.DeviceInfo
import li.cil.oc.api.driver.DeviceInfo.DeviceAttribute
import li.cil.oc.api.driver.DeviceInfo.DeviceClass
import li.cil.oc.api.machine.Arguments
import li.cil.oc.api.machine.Callback
import li.cil.oc.api.machine.Context
import li.cil.oc.api.network._
import li.cil.oc.api.prefab
import li.cil.oc.util.PackedColor
import net.minecraft.nbt.NBTTagCompound
import scala.collection.convert.WrapAsJava._
import scala.util.matching.Regex
// IMPORTANT: usually methods with side effects should *not* be direct
// callbacks to avoid the massive headache synchronizing them ensues, in
// particular when it comes to world saving. I'm making an exception for
// screens, though since they'd be painfully sluggish otherwise. This also
// means we have to use a somewhat nasty trick in common.component.Buffer's
// save function: we wait for all computers in the same network to finish
// their current execution and then pause them, to ensure the state of the
// buffer is "clean", meaning the computer has the correct state when it is
// saved in turn. If we didn't, a computer might change a screen after it was
// saved, but before the computer was saved, leading to mismatching states in
// the save file - a Bad Thing (TM).
class GraphicsCard(val tier: Int) extends prefab.ManagedEnvironment with DeviceInfo {
override val node = Network.newNode(this, Visibility.Neighbors).
withComponent("gpu").
withConnector().
create()
private val maxResolution = Settings.screenResolutionsByTier(tier)
private val maxDepth = Settings.screenDepthsByTier(tier)
private var screenAddress: Option[String] = None
private var screenInstance: Option[api.internal.TextBuffer] = None
private def screen(f: (api.internal.TextBuffer) => Array[AnyRef]) = screenInstance match {
case Some(screen) => screen.synchronized(f(screen))
case _ => Array(Unit, "no screen")
}
final val setBackgroundCosts = Array(1.0 / 32, 1.0 / 64, 1.0 / 128)
final val setForegroundCosts = Array(1.0 / 32, 1.0 / 64, 1.0 / 128)
final val setPaletteColorCosts = Array(1.0 / 2, 1.0 / 8, 1.0 / 16)
final val setCosts = Array(1.0 / 64, 1.0 / 128, 1.0 / 256)
final val copyCosts = Array(1.0 / 16, 1.0 / 32, 1.0 / 64)
final val fillCosts = Array(1.0 / 32, 1.0 / 64, 1.0 / 128)
// ----------------------------------------------------------------------- //
private final lazy val deviceInfo = Map(
DeviceAttribute.Class -> DeviceClass.Display,
DeviceAttribute.Description -> "Graphics controller",
DeviceAttribute.Vendor -> Constants.DeviceInfo.DefaultVendor,
DeviceAttribute.Product -> ("MPG" + ((tier + 1) * 1000).toString + " GTZ"),
DeviceAttribute.Capacity -> capacityInfo,
DeviceAttribute.Width -> widthInfo,
DeviceAttribute.Clock -> clockInfo
)
def capacityInfo = (maxResolution._1 * maxResolution._2).toString
def widthInfo = Array("1", "4", "8").apply(maxDepth.ordinal())
def clockInfo = ((2000 / setBackgroundCosts(tier)).toInt / 100).toString + "/" + ((2000 / setForegroundCosts(tier)).toInt / 100).toString + "/" + ((2000 / setPaletteColorCosts(tier)).toInt / 100).toString + "/" + ((2000 / setCosts(tier)).toInt / 100).toString + "/" + ((2000 / copyCosts(tier)).toInt / 100).toString + "/" + ((2000 / fillCosts(tier)).toInt / 100).toString
override def getDeviceInfo: util.Map[String, String] = deviceInfo
// ----------------------------------------------------------------------- //
@Callback(doc = """function(address:string[, reset:boolean=true]):boolean -- Binds the GPU to the screen with the specified address and resets screen settings if `reset` is true.""")
def bind(context: Context, args: Arguments): Array[AnyRef] = {
val address = args.checkString(0)
val reset = args.optBoolean(1, true)
node.network.node(address) match {
case null => result(Unit, "invalid address")
case node: Node if node.host.isInstanceOf[api.internal.TextBuffer] =>
screenAddress = Option(address)
screenInstance = Some(node.host.asInstanceOf[api.internal.TextBuffer])
screen(s => {
if (reset) {
val (gmw, gmh) = maxResolution
val smw = s.getMaximumWidth
val smh = s.getMaximumHeight
s.setResolution(math.min(gmw, smw), math.min(gmh, smh))
s.setColorDepth(api.internal.TextBuffer.ColorDepth.values.apply(math.min(maxDepth.ordinal, s.getMaximumColorDepth.ordinal)))
s.setForegroundColor(0xFFFFFF)
s.setBackgroundColor(0x000000)
}
else context.pause(0.2) // To discourage outputting "in realtime" to multiple screens using one GPU.
result(true)
})
case _ => result(Unit, "not a screen")
}
}
@Callback(direct = true, doc = """function():string -- Get the address of the screen the GPU is currently bound to.""")
def getScreen(context: Context, args: Arguments): Array[AnyRef] = screen(s => result(s.node.address))
@Callback(direct = true, doc = """function():number, boolean -- Get the current background color and whether it's from the palette or not.""")
def getBackground(context: Context, args: Arguments): Array[AnyRef] =
screen(s => result(s.getBackgroundColor, s.isBackgroundFromPalette))
@Callback(direct = true, doc = """function(value:number[, palette:boolean]):number, number or nil -- Sets the background color to the specified value. Optionally takes an explicit palette index. Returns the old value and if it was from the palette its palette index.""")
def setBackground(context: Context, args: Arguments): Array[AnyRef] = {
context.consumeCallBudget(setBackgroundCosts(tier))
val color = args.checkInteger(0)
screen(s => {
val oldValue = s.getBackgroundColor
val (oldColor, oldIndex) =
if (s.isBackgroundFromPalette) {
(s.getPaletteColor(oldValue), oldValue)
}
else {
(oldValue, Unit)
}
s.setBackgroundColor(color, args.optBoolean(1, false))
result(oldColor, oldIndex)
})
}
@Callback(direct = true, doc = """function():number, boolean -- Get the current foreground color and whether it's from the palette or not.""")
def getForeground(context: Context, args: Arguments): Array[AnyRef] =
screen(s => result(s.getForegroundColor, s.isForegroundFromPalette))
@Callback(direct = true, doc = """function(value:number[, palette:boolean]):number, number or nil -- Sets the foreground color to the specified value. Optionally takes an explicit palette index. Returns the old value and if it was from the palette its palette index.""")
def setForeground(context: Context, args: Arguments): Array[AnyRef] = {
context.consumeCallBudget(setForegroundCosts(tier))
val color = args.checkInteger(0)
screen(s => {
val oldValue = s.getForegroundColor
val (oldColor, oldIndex) =
if (s.isForegroundFromPalette) {
(s.getPaletteColor(oldValue), oldValue)
}
else {
(oldValue, Unit)
}
s.setForegroundColor(color, args.optBoolean(1, false))
result(oldColor, oldIndex)
})
}
@Callback(direct = true, doc = """function(index:number):number -- Get the palette color at the specified palette index.""")
def getPaletteColor(context: Context, args: Arguments): Array[AnyRef] = {
val index = args.checkInteger(0)
screen(s => try result(s.getPaletteColor(index)) catch {
case _: ArrayIndexOutOfBoundsException => throw new IllegalArgumentException("invalid palette index")
})
}
@Callback(direct = true, doc = """function(index:number, color:number):number -- Set the palette color at the specified palette index. Returns the previous value.""")
def setPaletteColor(context: Context, args: Arguments): Array[AnyRef] = {
context.consumeCallBudget(setPaletteColorCosts(tier))
val index = args.checkInteger(0)
val color = args.checkInteger(1)
context.pause(0.1)
screen(s => try {
val oldColor = s.getPaletteColor(index)
s.setPaletteColor(index, color)
result(oldColor)
}
catch {
case _: ArrayIndexOutOfBoundsException => throw new IllegalArgumentException("invalid palette index")
})
}
@Callback(direct = true, doc = """function():number -- Returns the currently set color depth.""")
def getDepth(context: Context, args: Arguments): Array[AnyRef] =
screen(s => result(PackedColor.Depth.bits(s.getColorDepth)))
@Callback(doc = """function(depth:number):number -- Set the color depth. Returns the previous value.""")
def setDepth(context: Context, args: Arguments): Array[AnyRef] = {
val depth = args.checkInteger(0)
screen(s => {
val oldDepth = s.getColorDepth
depth match {
case 1 => s.setColorDepth(api.internal.TextBuffer.ColorDepth.OneBit)
case 4 if maxDepth.ordinal >= api.internal.TextBuffer.ColorDepth.FourBit.ordinal => s.setColorDepth(api.internal.TextBuffer.ColorDepth.FourBit)
case 8 if maxDepth.ordinal >= api.internal.TextBuffer.ColorDepth.EightBit.ordinal => s.setColorDepth(api.internal.TextBuffer.ColorDepth.EightBit)
case _ => throw new IllegalArgumentException("unsupported depth")
}
result(oldDepth)
})
}
@Callback(direct = true, doc = """function():number -- Get the maximum supported color depth.""")
def maxDepth(context: Context, args: Arguments): Array[AnyRef] =
screen(s => result(PackedColor.Depth.bits(api.internal.TextBuffer.ColorDepth.values.apply(math.min(maxDepth.ordinal, s.getMaximumColorDepth.ordinal)))))
@Callback(direct = true, doc = """function():number, number -- Get the current screen resolution.""")
def getResolution(context: Context, args: Arguments): Array[AnyRef] =
screen(s => result(s.getWidth, s.getHeight))
@Callback(doc = """function(width:number, height:number):boolean -- Set the screen resolution. Returns true if the resolution changed.""")
def setResolution(context: Context, args: Arguments): Array[AnyRef] = {
val w = args.checkInteger(0)
val h = args.checkInteger(1)
val (mw, mh) = maxResolution
// Even though the buffer itself checks this again, we need this here for
// the minimum of screen and GPU resolution.
if (w < 1 || h < 1 || w > mw || h > mw || h * w > mw * mh)
throw new IllegalArgumentException("unsupported resolution")
screen(s => result(s.setResolution(w, h)))
}
@Callback(direct = true, doc = """function():number, number -- Get the maximum screen resolution.""")
def maxResolution(context: Context, args: Arguments): Array[AnyRef] =
screen(s => {
val (gmw, gmh) = maxResolution
val smw = s.getMaximumWidth
val smh = s.getMaximumHeight
result(math.min(gmw, smw), math.min(gmh, smh))
})
@Callback(direct = true, doc = """function():number, number -- Get the current viewport resolution.""")
def getViewport(context: Context, args: Arguments): Array[AnyRef] =
screen(s => result(s.getViewportWidth, s.getViewportHeight))
@Callback(doc = """function(width:number, height:number):boolean -- Set the viewport resolution. Cannot exceed the screen resolution. Returns true if the resolution changed.""")
def setViewport(context: Context, args: Arguments): Array[AnyRef] = {
val w = args.checkInteger(0)
val h = args.checkInteger(1)
val (mw, mh) = maxResolution
// Even though the buffer itself checks this again, we need this here for
// the minimum of screen and GPU resolution.
if (w < 1 || h < 1 || w > mw || h > mw || h * w > mw * mh)
throw new IllegalArgumentException("unsupported viewport size")
screen(s => {
if (w > s.getWidth || h > s.getHeight)
throw new IllegalArgumentException("unsupported viewport size")
result(s.setViewport(w, h))
})
}
@Callback(direct = true, doc = """function(x:number, y:number):string, number, number, number or nil, number or nil -- Get the value displayed on the screen at the specified index, as well as the foreground and background color. If the foreground or background is from the palette, returns the palette indices as fourth and fifth results, else nil, respectively.""")
def get(context: Context, args: Arguments): Array[AnyRef] = {
val x = args.checkInteger(0) - 1
val y = args.checkInteger(1) - 1
screen(s => {
val fgValue = s.getForegroundColor(x, y)
val (fgColor, fgIndex) =
if (s.isForegroundFromPalette(x, y)) {
(s.getPaletteColor(fgValue), fgValue)
}
else {
(fgValue, Unit)
}
val bgValue = s.getBackgroundColor(x, y)
val (bgColor, bgIndex) =
if (s.isBackgroundFromPalette(x, y)) {
(s.getPaletteColor(bgValue), bgValue)
}
else {
(bgValue, Unit)
}
result(s.get(x, y), fgColor, bgColor, fgIndex, bgIndex)
})
}
@Callback(direct = true, doc = """function(x:number, y:number, value:string[, vertical:boolean]):boolean -- Plots a string value to the screen at the specified position. Optionally writes the string vertically.""")
def set(context: Context, args: Arguments): Array[AnyRef] = {
context.consumeCallBudget(setCosts(tier))
val x = args.checkInteger(0) - 1
val y = args.checkInteger(1) - 1
val value = args.checkString(2)
val vertical = args.optBoolean(3, false)
screen(s => {
if (consumePower(value.length, Settings.get.gpuSetCost)) {
s.set(x, y, value, vertical)
result(true)
}
else result(Unit, "not enough energy")
})
}
@Callback(direct = true, doc = """function(x:number, y:number, width:number, height:number, tx:number, ty:number):boolean -- Copies a portion of the screen from the specified location with the specified size by the specified translation.""")
def copy(context: Context, args: Arguments): Array[AnyRef] = {
context.consumeCallBudget(copyCosts(tier))
val x = args.checkInteger(0) - 1
val y = args.checkInteger(1) - 1
val w = math.max(0, args.checkInteger(2))
val h = math.max(0, args.checkInteger(3))
val tx = args.checkInteger(4)
val ty = args.checkInteger(5)
screen(s => {
if (consumePower(w * h, Settings.get.gpuCopyCost)) {
s.copy(x, y, w, h, tx, ty)
result(true)
}
else result(Unit, "not enough energy")
})
}
@Callback(direct = true, doc = """function(x:number, y:number, width:number, height:number, char:string):boolean -- Fills a portion of the screen at the specified position with the specified size with the specified character.""")
def fill(context: Context, args: Arguments): Array[AnyRef] = {
context.consumeCallBudget(fillCosts(tier))
val x = args.checkInteger(0) - 1
val y = args.checkInteger(1) - 1
val w = math.max(0, args.checkInteger(2))
val h = math.max(0, args.checkInteger(3))
val value = args.checkString(4)
if (value.length == 1) screen(s => {
val c = value.charAt(0)
val cost = if (c == ' ') Settings.get.gpuClearCost else Settings.get.gpuFillCost
if (consumePower(w * h, cost)) {
s.fill(x, y, w, h, value.charAt(0))
result(true)
}
else {
result(Unit, "not enough energy")
}
})
else throw new Exception("invalid fill value")
}
private def consumePower(n: Double, cost: Double) = node.tryChangeBuffer(-n * cost)
// ----------------------------------------------------------------------- //
override def onMessage(message: Message) {
super.onMessage(message)
if (message.name == "computer.stopped" && node.isNeighborOf(message.source)) {
screen(s => {
val (gmw, gmh) = maxResolution
val smw = s.getMaximumWidth
val smh = s.getMaximumHeight
s.setResolution(math.min(gmw, smw), math.min(gmh, smh))
s.setColorDepth(api.internal.TextBuffer.ColorDepth.values.apply(math.min(maxDepth.ordinal, s.getMaximumColorDepth.ordinal)))
s.setForegroundColor(0xFFFFFF)
val w = s.getWidth
val h = s.getHeight
message.source.host match {
case machine: li.cil.oc.server.machine.Machine if machine.lastError != null =>
if (s.getColorDepth.ordinal > api.internal.TextBuffer.ColorDepth.OneBit.ordinal) s.setBackgroundColor(0x0000FF)
else s.setBackgroundColor(0x000000)
s.fill(0, 0, w, h, ' ')
try {
val wrapRegEx = s"(.{1,${math.max(1, w - 2)}})\\s".r
val lines = wrapRegEx.replaceAllIn(Localization.localizeImmediately(machine.lastError).replace("\t", " ") + "\n", m => Regex.quoteReplacement(m.group(1) + "\n")).lines.toArray
val firstRow = ((h - lines.length) / 2) max 2
val message = "Unrecoverable Error"
s.set((w - message.length) / 2, firstRow - 2, message, false)
val maxLineLength = lines.map(_.length).max
val col = ((w - maxLineLength) / 2) max 0
for ((line, idx) <- lines.zipWithIndex) {
val row = firstRow + idx
s.set(col, row, line, false)
}
}
catch {
case t: Throwable => t.printStackTrace()
}
case _ =>
s.setBackgroundColor(0x000000)
s.fill(0, 0, w, h, ' ')
}
null // For screen()
})
}
}
override def onConnect(node: Node): Unit = {
super.onConnect(node)
if (screenInstance.isEmpty && screenAddress.fold(false)(_ == node.address)) {
node.host match {
case buffer: api.internal.TextBuffer =>
screenInstance = Some(buffer)
case _ => // Not the screen node we're looking for.
}
}
}
override def onDisconnect(node: Node) {
super.onDisconnect(node)
if (node == this.node || screenAddress.contains(node.address)) {
screenAddress = None
screenInstance = None
}
}
// ----------------------------------------------------------------------- //
override def load(nbt: NBTTagCompound) {
super.load(nbt)
if (nbt.hasKey("screen")) {
nbt.getString("screen") match {
case screen: String if !screen.isEmpty => screenAddress = Some(screen)
case _ => screenAddress = None
}
screenInstance = None
}
}
override def save(nbt: NBTTagCompound) {
super.save(nbt)
if (screenAddress.isDefined) {
nbt.setString("screen", screenAddress.get)
}
}
}