mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-14 07:36:06 +09:00
Fix on fonts, fixed special char printing (>=0x80) on Term, essentially changing encoding from UTF-8 to ISO-8859-1
Former-commit-id: 78d4423e896301aebdec2f27893bb2aefaf4aada Former-commit-id: e317df0eca8da1598159648bc44c30b86663d8a5
This commit is contained in:
@@ -6,6 +6,7 @@ import li.cil.repack.org.luaj.vm2.lib.TwoArgFunction
|
||||
import li.cil.repack.org.luaj.vm2.lib.ZeroArgFunction
|
||||
import net.torvald.terrarum.Terrarum
|
||||
import net.torvald.terrarum.virtualcomputer.computer.BaseTerrarumComputer
|
||||
import net.torvald.terrarum.virtualcomputer.luaapi.Term.Companion.checkIBM437
|
||||
import java.io.*
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
@@ -368,7 +369,7 @@ internal class Filesystem(globals: Globals, computer: BaseTerrarumComputer) {
|
||||
|
||||
private class FileClassWriteBytes(val fos: FileOutputStream): OneArgFunction() {
|
||||
override fun call(byteString: LuaValue): LuaValue {
|
||||
val byteString = byteString.checkjstring()
|
||||
val byteString = byteString.checkIBM437()
|
||||
val bytearr = ByteArray(byteString.length, { byteString[it].toByte() })
|
||||
fos.write(bytearr)
|
||||
|
||||
@@ -378,7 +379,7 @@ internal class Filesystem(globals: Globals, computer: BaseTerrarumComputer) {
|
||||
|
||||
private class FileClassPrintText(val fw: FileWriter): OneArgFunction() {
|
||||
override fun call(string: LuaValue): LuaValue {
|
||||
val text = string.checkjstring()
|
||||
val text = string.checkIBM437()
|
||||
fw.write(text)
|
||||
return LuaValue.NONE
|
||||
}
|
||||
@@ -386,7 +387,7 @@ internal class Filesystem(globals: Globals, computer: BaseTerrarumComputer) {
|
||||
|
||||
private class FileClassPrintlnText(val fw: FileWriter): OneArgFunction() {
|
||||
override fun call(string: LuaValue): LuaValue {
|
||||
val text = string.checkjstring() + "\n"
|
||||
val text = string.checkIBM437() + "\n"
|
||||
fw.write(text)
|
||||
return LuaValue.NONE
|
||||
}
|
||||
@@ -415,7 +416,7 @@ internal class Filesystem(globals: Globals, computer: BaseTerrarumComputer) {
|
||||
private class FileClassReadAllBytes(val path: Path): ZeroArgFunction() {
|
||||
override fun call(): LuaValue {
|
||||
val byteArr = Files.readAllBytes(path)
|
||||
val s: String = java.lang.String(byteArr, "ISO-8859-1").toString()
|
||||
val s: String = java.lang.String(byteArr, "IBM437").toString()
|
||||
return LuaValue.valueOf(s)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import li.cil.repack.org.luaj.vm2.*
|
||||
import li.cil.repack.org.luaj.vm2.lib.*
|
||||
import net.torvald.terrarum.virtualcomputer.terminal.Teletype
|
||||
import net.torvald.terrarum.virtualcomputer.terminal.Terminal
|
||||
import java.nio.charset.Charset
|
||||
|
||||
/**
|
||||
* APIs must have some extent of compatibility with ComputerCraft by dan200
|
||||
@@ -26,6 +27,7 @@ internal class Term(globals: Globals, term: Teletype) {
|
||||
if (term is Terminal) {
|
||||
globals["term"]["emitRaw"] = Term.EmitRaw(term)
|
||||
globals["term"]["emit"] = Term.Emit(term)
|
||||
globals["term"]["emitString"] = Term.EmitString(term)
|
||||
globals["term"]["resetColor"] = Term.ResetColour(term)
|
||||
globals["term"]["resetColour"] = Term.ResetColour(term)
|
||||
globals["term"]["clear"] = Term.Clear(term)
|
||||
@@ -44,18 +46,28 @@ internal class Term(globals: Globals, term: Teletype) {
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun LuaValue.checkIBM437(): String {
|
||||
if (this is LuaString)
|
||||
return m_bytes.copyOfRange(m_offset, m_length).toString(Charset.forName("ISO-8859-1"))
|
||||
// it only works if Charset is ISO-8859, despite of the name "IBM437"
|
||||
else
|
||||
throw LuaError("bad argument (string expected, got ${this.typename()})")
|
||||
}
|
||||
}
|
||||
|
||||
class WriteString(val tty: Teletype) : LuaFunction() {
|
||||
override fun call(p0: LuaValue): LuaValue {
|
||||
if (tty is Terminal)
|
||||
tty.writeString(p0.checkjstring())
|
||||
tty.writeString(p0.checkIBM437())
|
||||
else
|
||||
tty.writeChars(p0.checkjstring())
|
||||
tty.writeChars(p0.checkIBM437())
|
||||
return LuaValue.NONE
|
||||
}
|
||||
|
||||
override fun call(s: LuaValue, x: LuaValue, y: LuaValue): LuaValue {
|
||||
if (tty is Terminal)
|
||||
tty.writeString(s.checkjstring(), x.checkint(), y.checkint())
|
||||
tty.writeString(s.checkIBM437(), x.checkint(), y.checkint())
|
||||
else
|
||||
throw LuaError("couldn't move cursor; TTY is one-dimensional")
|
||||
return LuaValue.NONE
|
||||
@@ -65,15 +77,15 @@ internal class Term(globals: Globals, term: Teletype) {
|
||||
class PrintString(val tty: Teletype) : LuaFunction() {
|
||||
override fun call(p0: LuaValue): LuaValue {
|
||||
if (tty is Terminal)
|
||||
tty.printString(p0.checkjstring())
|
||||
tty.printString(p0.checkIBM437())
|
||||
else
|
||||
tty.printChars(p0.checkjstring())
|
||||
tty.printChars(p0.checkIBM437())
|
||||
return LuaValue.NONE
|
||||
}
|
||||
|
||||
override fun call(s: LuaValue, x: LuaValue, y: LuaValue): LuaValue {
|
||||
if (tty is Terminal)
|
||||
tty.printString(s.checkjstring(), x.checkint(), y.checkint())
|
||||
tty.printString(s.checkIBM437(), x.checkint(), y.checkint())
|
||||
else
|
||||
throw LuaError("couldn't move cursor; TTY is one-dimensional")
|
||||
return LuaValue.NONE
|
||||
@@ -87,16 +99,23 @@ internal class Term(globals: Globals, term: Teletype) {
|
||||
}
|
||||
}
|
||||
|
||||
class EmitRaw(val term: Terminal) : OneArgFunction() {
|
||||
override fun call(p0: LuaValue): LuaValue {
|
||||
term.emitChar(p0.checkint())
|
||||
class EmitRaw(val term: Terminal) : ThreeArgFunction() {
|
||||
override fun call(p0: LuaValue, x: LuaValue, y: LuaValue): LuaValue {
|
||||
term.emitChar(p0.checkint(), x.checkint(), y.checkint())
|
||||
return LuaValue.NONE
|
||||
}
|
||||
}
|
||||
|
||||
class Emit(val term: Terminal) : OneArgFunction() {
|
||||
override fun call(p0: LuaValue): LuaValue {
|
||||
term.emitChar(p0.checkint().toChar())
|
||||
class Emit(val term: Terminal) : ThreeArgFunction() {
|
||||
override fun call(p0: LuaValue, x: LuaValue, y: LuaValue): LuaValue {
|
||||
term.emitChar(p0.checkint().toChar(), x.checkint(), y.checkint())
|
||||
return LuaValue.NONE
|
||||
}
|
||||
}
|
||||
|
||||
class EmitString(val term: Terminal) : ThreeArgFunction() {
|
||||
override fun call(p0: LuaValue, x: LuaValue, y: LuaValue): LuaValue {
|
||||
term.emitString(p0.checkIBM437(), x.checkint(), y.checkint())
|
||||
return LuaValue.NONE
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user