package ru.dbotthepony.kstarbound.lua.bindings import org.classdump.luna.ByteString import org.classdump.luna.LuaRuntimeException import org.classdump.luna.TableFactory import org.classdump.luna.impl.NonsuspendableFunctionException import org.classdump.luna.runtime.AbstractFunction1 import org.classdump.luna.runtime.ExecutionContext import ru.dbotthepony.kstarbound.Starbound import ru.dbotthepony.kstarbound.lua.from class AssetJsonFunction(private val tables: TableFactory) : AbstractFunction1() { override fun resume(context: ExecutionContext?, suspendedState: Any?) { throw NonsuspendableFunctionException(this::class.java) } override fun invoke(context: ExecutionContext, arg1: ByteString?) { arg1 ?: throw LuaRuntimeException("bad argument #1 to root.assetJson (string expected, got nil)") val load = Starbound.loadJsonAsset(arg1.decode()) if (load == null) { context.returnBuffer.setTo() } else { context.returnBuffer.setTo(tables.from(load)) } } }