diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/lua/bindings/UtilityBindings.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/lua/bindings/UtilityBindings.kt index e73856b0..887c8364 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/lua/bindings/UtilityBindings.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/lua/bindings/UtilityBindings.kt @@ -1,11 +1,14 @@ package ru.dbotthepony.kstarbound.lua.bindings +import com.google.gson.JsonElement import org.apache.logging.log4j.LogManager import org.classdump.luna.ByteString import org.classdump.luna.Table +import org.classdump.luna.runtime.ExecutionContext import ru.dbotthepony.kstarbound.math.vector.Vector2d import ru.dbotthepony.kstarbound.Starbound import ru.dbotthepony.kstarbound.defs.PerlinNoiseParameters +import ru.dbotthepony.kstarbound.json.JsonPath import ru.dbotthepony.kstarbound.lua.LuaEnvironment import ru.dbotthepony.kstarbound.lua.from import ru.dbotthepony.kstarbound.lua.get @@ -130,3 +133,18 @@ fun provideUtilityBindings(lua: LuaEnvironment) { table["staticRandomI32Range"] = staticRandomI32Range table["staticRandomI64Range"] = staticRandomI32Range } + +fun provideConfigBindings(lua: LuaEnvironment, lookup: ExecutionContext.(path: JsonPath, ifMissing: Any?) -> Any?) { + val config = lua.newTable() + lua.globals["config"] = config + config["getParameter"] = createConfigBinding(lookup) +} + +fun createConfigBinding(lookup: ExecutionContext.(path: JsonPath, ifMissing: Any?) -> Any?) = luaFunction { name: ByteString, default: Any? -> + val get = lookup(this, JsonPath.query(name.decode()), default) + + if (get is JsonElement) + returnBuffer.setTo(from(get)) + else + returnBuffer.setTo(get) +}