-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresources.go
81 lines (72 loc) · 1.55 KB
/
resources.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package spin
import (
"reflect"
"strconv"
)
type Resources struct {
Fonts map[string]struct{}
Switches map[string]*Switch
}
type resourceSystem struct {
eng *Engine
resources *Resources
}
func registerResourceSystem(eng *Engine) {
s := &resourceSystem{
eng: eng,
resources: GetResourceVars(eng),
}
eng.RegisterActionHandler(s)
}
func (s *resourceSystem) HandleAction(action Action) {
switch act := action.(type) {
case RegisterFont:
s.registerFont(act)
case SetVar:
s.setVar(act)
}
}
func (s *resourceSystem) registerFont(act RegisterFont) {
s.resources.Fonts[act.ID] = struct{}{}
}
func (s *resourceSystem) setVar(act SetVar) {
vars, ok := s.eng.GetVars(act.Vars)
if !ok {
Warn("no such variables: %v", act.Vars)
return
}
t := reflect.TypeOf(vars).Elem()
v := reflect.ValueOf(vars).Elem()
f, ok := t.FieldByName(act.ID)
if !ok {
Warn("no such %v variable: %v", act.Vars, act.ID)
return
}
switch f.Type.Kind() {
case reflect.String:
v.FieldByName(act.ID).SetString(act.Val)
case reflect.Int:
i, err := strconv.Atoi(act.Val)
if err != nil {
Warn("not an integer: %v", act.Val)
return
}
v.FieldByName(act.ID).SetInt(int64(i))
default:
Warn("cannot handle type %v: %v", f.Type.Kind(), act.Val)
}
}
func GetResourceVars(store Store) *Resources {
v, ok := store.GetVars("resources")
var vars *Resources
if ok {
vars = v.(*Resources)
} else {
vars = &Resources{
Fonts: make(map[string]struct{}),
Switches: make(map[string]*Switch),
}
store.RegisterVars("resources", vars)
}
return vars
}