aboutsummaryrefslogtreecommitdiff
path: root/convert/read_static.go
blob: 13d80f48eed22e493db50914fee0d212f3130cb3 (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 convert

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

//go:generate ./read_mkauto.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
	}
}