forked from Minetest-j45/mt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinv.go
229 lines (190 loc) · 3.92 KB
/
inv.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
package mt
import (
"fmt"
"io"
"reflect"
)
type Inv []NamedInvList
type NamedInvList struct {
Name string
InvList
}
func (inv Inv) List(name string) *NamedInvList {
for i, l := range inv {
if l.Name == name {
return &inv[i]
}
}
return nil
}
func (i Inv) Serialize(w io.Writer) error {
return i.SerializeKeep(w, nil)
}
func (i Inv) SerializeKeep(w io.Writer, old Inv) error {
ew := &errWriter{w: w}
for _, l := range i {
var oldList InvList
if l := old.List(l.Name); l != nil {
oldList = l.InvList
}
if reflect.DeepEqual(&i, oldList) {
fmt.Fprintln(ew, "KeepList", l.Name)
continue
}
fmt.Fprintln(ew, "List", l.Name, len(l.Stacks))
l.SerializeKeep(ew, oldList)
}
fmt.Fprintln(ew, "EndInventory")
return ew.err
}
func (i *Inv) Deserialize(r io.Reader) (err error) {
s := new(sentinel)
defer s.recover(&err)
old := *i
*i = nil
for {
if err := readCmdLn(r, map[string]interface{}{
"List": func(name string, size int) {
l := old.List(name)
if l == nil {
l = &NamedInvList{Name: name}
}
if err := l.Deserialize(r); err != nil {
s.ret(fmt.Errorf("List %s %d: %w", name, size, err))
}
if len(l.Stacks) != size {
s.ret(fmt.Errorf("List %s %d: contains %d stacks", name, size, len(l.Stacks)))
}
*i = append(*i, *l)
},
"KeepList": func(name string) {
l := old.List(name)
if l == nil {
s.ret(fmt.Errorf("KeepList %s: list does not exist", name))
}
*i = append(*i, *l)
},
"EndInventory": func() {
s.ret(nil)
},
}); err != nil {
if err == io.EOF {
return io.ErrUnexpectedEOF
}
return err
}
}
}
type InvList struct {
Width int
Stacks []Stack
}
func (l InvList) Serialize(w io.Writer) error {
return l.SerializeKeep(w, InvList{})
}
func (l InvList) SerializeKeep(w io.Writer, old InvList) error {
ew := &errWriter{w: w}
fmt.Fprintln(ew, "Width", l.Width)
for i, s := range l.Stacks {
if i < len(old.Stacks) && s == old.Stacks[i] {
fmt.Fprintln(ew, "Keep")
continue
}
if s.Count > 0 {
fmt.Fprintln(ew, "Item", s)
} else {
fmt.Fprintln(ew, "Empty")
}
}
fmt.Fprintln(ew, "EndInventoryList")
return ew.err
}
func (l *InvList) Deserialize(r io.Reader) (err error) {
s := new(sentinel)
defer s.recover(&err)
if _, err := fmt.Fscanf(r, "Width %d\n", &l.Width); err != nil {
return err
}
old := l.Stacks
l.Stacks = nil
for {
if err := readCmdLn(r, map[string]interface{}{
"Empty": func() {
l.Stacks = append(l.Stacks, Stack{})
},
"Item": func(stk Stack) {
l.Stacks = append(l.Stacks, stk)
},
"Keep": func() {
if i := len(l.Stacks); i < len(old) {
l.Stacks = append(l.Stacks, old[i])
} else {
l.Stacks = append(l.Stacks, Stack{})
}
},
"EndInventoryList": func() {
s.ret(nil)
},
}); err != nil {
if err == io.EOF {
return io.ErrUnexpectedEOF
}
return err
}
}
}
func readCmdLn(r io.Reader, cmds map[string]interface{}) error {
if _, ok := r.(io.RuneScanner); !ok {
r = &readRune{Reader: r, peekRune: -1}
}
var cmd string
if _, err := fmt.Fscan(r, &cmd); err != nil {
return err
}
f, ok := cmds[cmd]
if !ok {
return fmt.Errorf("unsupported line type: %+q", cmd)
}
t := reflect.TypeOf(f)
a := make([]interface{}, t.NumIn())
for i := range a {
a[i] = reflect.New(t.In(i)).Interface()
}
fmt.Fscanln(r, a...)
args := make([]reflect.Value, t.NumIn())
for i := range args {
args[i] = reflect.ValueOf(a[i]).Elem()
}
reflect.ValueOf(f).Call(args)
return nil
}
type sentinel struct {
err error
}
func (s *sentinel) ret(err error) {
s.err = err
panic(s)
}
func (s *sentinel) recover(p *error) {
if r := recover(); r != nil {
if r == s {
*p = s.err
} else {
panic(r)
}
}
}
type errWriter struct {
w io.Writer
err error
}
func (ew *errWriter) Write(p []byte) (int, error) {
if ew.err != nil {
return 0, ew.err
}
n, err := ew.w.Write(p)
if err != nil {
ew.err = err
}
return n, err
}