aboutsummaryrefslogtreecommitdiff
path: root/fromlua/static.go
blob: b989db8bae8c3bcdec58858a5959ce62508c3763 (plain)
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
package fromlua

import (
	"github.com/anon55555/mt"
	"github.com/yuin/gopher-lua"
)

//go:generate ./generate.lua

func readBool(l *lua.LState, val lua.LValue, ptr *bool) {
	if val.Type() != lua.LTBool {
		panic("invalid value for bool: must be a boolean")
	}
	*ptr = bool(val.(lua.LBool))
}

func readString(l *lua.LState, val lua.LValue, ptr *string) {
	if val.Type() != lua.LTString {
		panic("invalid value for string: must be a string")
	}
	*ptr = string(val.(lua.LString))
}

func readSliceByte(l *lua.LState, val lua.LValue, ptr *[]byte) {
	if val.Type() != lua.LTString {
		panic("invalid value for []byte: must be a string")
	}
	*ptr = []byte(val.(lua.LString))
}

func readSliceField(l *lua.LState, val lua.LValue, ptr *[]mt.Field) {
	if val.Type() != lua.LTTable {
		panic("invalid value for []Field: must be a table")
	}
	val.(*lua.LTable).ForEach(func(k, v lua.LValue) {
		if k.Type() != lua.LTString || v.Type() != lua.LTString {
			panic("invalid value for Field: key and value must be strings")
		}
		*ptr = append(*ptr, mt.Field{Name: string(k.(lua.LString)), Value: string(v.(lua.LString))})
	})
}

func readPointedThing(l *lua.LState, val lua.LValue, ptr *mt.PointedThing) {
	if val.Type() != lua.LTTable {
		panic("invalid value for PointedThing: must be a table")
	}
	id := l.GetField(val, "id")

	if id == lua.LNil {
		pt := &mt.PointedAO{}
		readAOID(l, id, &(*pt).ID)
		*ptr = pt
	} else {
		pt := &mt.PointedNode{}
		readVec3Int16(l, l.GetField(val, "under"), &(*pt).Under)
		readVec3Int16(l, l.GetField(val, "above"), &(*pt).Above)
		*ptr = pt
	}
}