Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
- ws.list not filtered correctly
- debug.log crashing with table
- new() not doing a deep copy of elements.
  • Loading branch information
dwursteisen committed Mar 20, 2024
1 parent 51e8a17 commit c05dda9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,30 @@ class DebugLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction(
}
}

private fun formatValue(arg: LuaValue, recursiveSecurity: MutableSet<Int> = mutableSetOf()): String = if (arg.istable()) {
val table = arg as LuaTable
if (recursiveSecurity.contains(table.hashCode())) {
"table[<${table.hashCode()}>]"
} else {
recursiveSecurity.add(table.hashCode())
val keys = table.keys()
val str = keys.joinToString(" ") {
it.optjstring("nil") + ":" + formatValue(table[it], recursiveSecurity)
}
"table[$str]"
}
} else if (arg.isfunction()) {
"function(" + (0 until arg.narg()).joinToString(", ") { "arg" } + ")"
} else {
arg.toString()
}

@TinyFunction("Log a message on the screen.", example = DEBUG_EXAMPLE)
internal inner class log : TwoArgFunction() {

@TinyCall("Log a message on the screen.")
override fun call(@TinyArg("str") arg1: LuaValue, @TinyArg("color") arg2: LuaValue): LuaValue {
val message = arg1.optjstring("")!!
val message = formatValue(arg1)
val color = arg2.optjstring("#32CD32")!!
resourceAccess.debug(DebugMessage(message, color))
return NIL
Expand All @@ -207,32 +225,12 @@ class DebugLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction(
@TinyFunction("Log a message into the console.", example = DEBUG_EXAMPLE)
internal inner class console : OneArgFunction() {

private val recursiveSecurity = mutableSetOf<Int>()

@TinyCall("Log a message into the console.")
override fun call(@TinyArg("str") arg: LuaValue): LuaValue {
val str = formatValue(arg)
recursiveSecurity.clear()
println("\uD83D\uDC1B $str")
return NIL
}

private fun formatValue(arg: LuaValue): String = if (arg.istable()) {
val table = arg as LuaTable
if (recursiveSecurity.contains(table.hashCode())) {
"table[<${table.hashCode()}>]"
} else {
recursiveSecurity.add(table.hashCode())
val keys = table.keys()
val str = keys.map { it.optjstring("nil") + ":" + formatValue(table.get(it)) }
.joinToString(" ")
"table[$str]"
}
} else if (arg.isfunction()) {
"function(" + (0 until arg.narg()).map { "arg" }.joinToString(", ") + ")"
} else {
arg.toString()
}
}

@TinyFunction("Draw a rectangle on the screen", example = DEBUG_ENABLED_EXAMPLE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,29 @@ class StdLib(
@TinyCall("Create new instance of class using default values.")
override fun call(@TinyArg("class") arg1: LuaValue, @TinyArg("default") arg2: LuaValue): LuaValue {
val default = if (arg2.istable()) {
val result = LuaTable()
val toCopy = arg2.checktable()!!
toCopy.keys().forEach { key ->
result.set(key, toCopy.get(key))
}
result
arg2.checktable()!!.deepCopy()
} else {
LuaTable()
}
default.setmetatable(arg1)
arg1.rawset("__index", arg1)
val reference = arg1.checktable()!!.deepCopy()
default.setmetatable(reference)
reference.rawset("__index", reference)
return default
}

private fun LuaTable.deepCopy(): LuaTable {
val result = LuaTable()
this.keys().forEach { key ->
var value = this[key]
value = if (value.istable()) {
value.checktable()!!.deepCopy()
} else {
value
}
result[key] = value
}
return result
}
}

@TinyFunction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class WorkspaceLib(
val ext = arg.optjstring(null).let { it?.lowercase() }
val result = LuaTable()
resources.forEach {
if ((ext == null || it.name.endsWith(ext))) {
if ((ext == null || it.extension.endsWith(ext))) {
result.insert(0, valueOf(it.name))
}
}
Expand Down

0 comments on commit c05dda9

Please sign in to comment.