-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
289 lines (271 loc) · 7.39 KB
/
template.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package template
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/qbradq/sharduo/lib/uo"
"github.com/qbradq/sharduo/lib/util"
)
var objctors = make(map[string]func() any)
// RegisterConstructor registers a constructor with the template package.
func RegisterConstructor(name string, fn func() any) {
if _, duplicate := objctors[name]; duplicate {
log.Fatalf("fatal: duplicate template constructor registered for name %s", name)
}
objctors[name] = fn
}
// GetConstructor returns the named constructor or nil.
func GetConstructor(name string) func() any {
return objctors[name]
}
// Object is the interface all Template objects must implement.
type Object interface {
// Serial returns the unique ID of the object.
Serial() uo.Serial
// Deserialize takes data from the template object and initializes the
// object's data structures with it.
Deserialize(*Template, bool)
// RecalculateStats is called after Deserialize() and should be used to
// recalculate any dynamic values of the data structures initialized by
// Deserialize().
RecalculateStats()
// InsertObject adds an object as a child of this object through an empty
// interface.
InsertObject(any)
// SetAmount sets the stack amount for the object, if any.
SetAmount(int)
}
// Template contains all of the property lines of the template.
type Template struct {
TypeName string // Name of the object constructor used to create the object.
TemplateName string // Unique name of the template.
BaseTemplate string // Name of the base template. The empty string means a root template.
IsResolved bool // True if the template's inheritance chain has already been satisfied.
properties map[string]*expression // List of all property expressions
}
// NewTemplate creates a new template.T object from the provided TagFileObject. The
// inheritance chain has not been resolved for this object and the text template
// has not been created or compiled.
func NewTemplate(tfo *util.TagFileObject, tm *TemplateManager) (*Template, []error) {
t := &Template{
TypeName: tfo.TypeName(),
properties: make(map[string]*expression),
}
templateName := tfo.GetString("TemplateName", "")
if templateName == "" {
panic(fmt.Sprintf("template of type %s missing TemplateName field", tfo.TypeName()))
}
errs := tfo.Map(func(name, value string) error {
if name == "TemplateName" {
t.TemplateName = value
} else if name == "BaseTemplate" {
t.BaseTemplate = value
} else {
t.properties[name] = &expression{
text: value,
}
}
return nil
})
return t, errs
}
// compileExpressions prepares every expression for execution and must be done
// once before any values methods are called
func (t *Template) compileExpressions() {
for _, e := range t.properties {
if err := e.compile(); err != nil {
log.Println(err)
}
}
}
// GetString returns the named property as a string or the default if not
// found. This function panics if no context is on the stack. See PushContext
// and PopContext.
func (t *Template) GetString(name, def string) string {
e, ok := t.properties[name]
if !ok {
return def
}
v, err := e.execute()
if err != nil {
log.Println(err)
return def
}
return v
}
// GetNumber returns the named property as a number or the default if not
// found. This function may add errors to the internal error slice.
func (t *Template) GetNumber(name string, def int) int {
v := t.GetString(name, "")
if v == "" {
return def
}
n, err := strconv.ParseInt(v, 0, 32)
if err != nil {
log.Println(err)
return def
}
return int(n)
}
// GetULong returns the named property as a uint64 or the default if not found.
// This function may add errors to the internal error slice. Only use this for
// actual 64-bit values, like uo.Time.
func (t *Template) GetULong(name string, def uint64) uint64 {
v := t.GetString(name, "")
if v == "" {
return def
}
n, err := strconv.ParseUint(v, 0, 64)
if err != nil {
log.Println(err)
return def
}
return n
}
// GetFloat returns the named property as a float32 or the default if not
// found. This function may add errors to the internal error slice.
func (t *Template) GetFloat(name string, def float32) float32 {
v := t.GetString(name, "")
if v == "" {
return def
}
f, err := strconv.ParseFloat(v, 32)
if err != nil {
log.Println(err)
return def
}
return float32(f)
}
// GetHex returns the named property as an unsigned number or the default if not
// found. This function may add errors to the internal error slice.
func (t *Template) GetHex(name string, def uint32) uint32 {
v := t.GetString(name, "")
if v == "" {
return def
}
n, err := strconv.ParseInt(v, 0, 64)
if err != nil {
log.Println(err)
return def
}
return uint32(n)
}
// GetBool returns the named property as a boolean value or the default if not
// found. This function may add errors to the internal error slice.
func (t *Template) GetBool(name string, def bool) bool {
v := t.GetString(name, "~~NOT~FOUND~~")
if v == "~~NOT~FOUND~~" {
return def
}
// This is the naked boolean case
if v == "" {
return true
}
var b bool
var err error
if b, err = strconv.ParseBool(v); err != nil {
log.Println(err)
return def
}
return b
}
// GetObjectReferences returns a slice of uo.Serial values. nil is the default
// value. This function may add errors to the internal error slice.
func (t *Template) GetObjectReferences(name string) []uo.Serial {
v := t.GetString(name, "")
if v == "" {
return nil
}
parts := strings.Split(v, ",")
ret := make([]uo.Serial, 0, len(parts))
for _, str := range parts {
n, err := strconv.ParseInt(str, 0, 32)
if err != nil {
log.Println(err)
} else {
ret = append(ret, uo.Serial(n))
}
}
return ret
}
// GetLocation returns a uo.Location value. The default value is returned if the
// named tag is not found.
func (t *Template) GetLocation(name string, def uo.Location) uo.Location {
str := t.GetString(name, "")
if str == "" {
return def
}
parts := strings.Split(str, ",")
if len(parts) != 3 {
log.Printf("error: GetLocation(%s) did not find three values", name)
return def
}
hasErrors := false
var l uo.Location
v, err := strconv.ParseInt(parts[0], 0, 16)
if err != nil {
log.Println(err)
hasErrors = true
}
l.X = int16(v)
v, err = strconv.ParseInt(parts[1], 0, 16)
if err != nil {
log.Println(err)
hasErrors = true
}
l.Y = int16(v)
v, err = strconv.ParseInt(parts[2], 0, 8)
if err != nil {
log.Println(err)
hasErrors = true
}
l.Z = int8(v)
if hasErrors {
return def
}
return l
}
// GetBounds returns a uo.Bounds value. The default value is returned if the
// named tag is not found.
func (t *Template) GetBounds(name string, def uo.Bounds) uo.Bounds {
str := t.GetString(name, "")
if str == "" {
return def
}
parts := strings.Split(str, ",")
if len(parts) != 4 {
log.Printf("error: GetBounds(%s) did not find four values", name)
return def
}
hasErrors := false
var b uo.Bounds
v, err := strconv.ParseInt(parts[0], 0, 16)
if err != nil {
log.Println(err)
hasErrors = true
}
b.X = int16(v)
v, err = strconv.ParseInt(parts[1], 0, 16)
if err != nil {
log.Println(err)
hasErrors = true
}
b.Y = int16(v)
v, err = strconv.ParseInt(parts[2], 0, 16)
if err != nil {
log.Println(err)
hasErrors = true
}
b.W = int16(v)
v, err = strconv.ParseInt(parts[3], 0, 16)
if err != nil {
log.Println(err)
hasErrors = true
}
b.H = int16(v)
if hasErrors {
return def
}
return b
}