/
container.go
250 lines (233 loc) · 5.02 KB
/
container.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
package io
import (
"bytes"
"context"
"errors"
"image"
"io"
"log"
"sync"
"sync/atomic"
"time"
"github.com/gabstv/primen/dom"
)
type Container interface {
Len() int64
FS() Filesystem
Load(name string) chan struct{}
Unload(name string) (bool, error)
LoadAll(names []string) (progress chan float64, done chan struct{})
UnloadAll()
Get(name string) ([]byte, error)
GetImage(name string) (image.Image, error)
GetAtlas(name string) (*Atlas, error)
GetAudioStream(name string) (*AudioStream, error)
GetAudioBytes(name string) ([]byte, error)
GetXMLDOM(name string) ([]dom.Node, error)
}
type container struct {
ctx context.Context
fs Filesystem
m sync.RWMutex
loadedfiles map[string][]byte
loadingfiles map[string]chan struct{}
loadedlen int64 // atomic
loadinglen int64 // atomic
loadingn int32 // atomic
audiostreams []*AudioStream
}
func (c *container) Len() int64 {
return atomic.LoadInt64(&c.loadedlen)
}
func (c *container) FS() Filesystem {
return c.fs
}
func (c *container) Load(name string) chan struct{} {
c.m.RLock()
xch, ok := c.loadingfiles[name]
c.m.RUnlock()
if ok {
// already loading/loaded
return xch
}
atomic.AddInt32(&c.loadingn, 1)
ldch := make(chan struct{}, 1)
go func() {
defer close(ldch)
c.m.Lock()
c.loadingfiles[name] = ldch
c.m.Unlock()
defer atomic.AddInt32(&c.loadingn, -1)
f, err := c.fs.Open(name)
if err != nil {
log.Println("container io error: " + err.Error())
return
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
log.Println("container stat error: " + err.Error())
return
}
atomic.AddInt64(&c.loadinglen, fi.Size())
defer atomic.AddInt64(&c.loadinglen, -fi.Size())
buf := new(bytes.Buffer)
buf.Grow(int(fi.Size()))
if _, err := buf.ReadFrom(f); err != nil {
log.Println("container io error: " + err.Error())
return
}
c.m.Lock()
c.loadedfiles[name] = buf.Bytes()
c.m.Unlock()
atomic.AddInt64(&c.loadedlen, int64(buf.Len()))
}()
return ldch
}
func (c *container) Unload(name string) (bool, error) {
c.m.RLock()
loadingch, ok := c.loadingfiles[name]
c.m.RUnlock()
if !ok {
return false, nil
}
select {
case <-time.After(time.Microsecond * 500):
return false, errors.New("resource is loading")
case <-loadingch:
}
c.m.Lock()
delete(c.loadingfiles, name)
x := len(c.loadedfiles[name])
delete(c.loadedfiles, name)
c.m.Unlock()
atomic.AddInt64(&c.loadedlen, int64(x))
return true, nil
}
func (c *container) LoadAll(names []string) (progress chan float64, done chan struct{}) {
progress = make(chan float64, 64)
done = make(chan struct{})
if len(names) < 1 {
close(done)
return
}
go func() {
defer close(done)
defer func() {
select {
case <-c.ctx.Done():
case <-time.After(time.Second):
}
close(progress)
}()
var step float64 = 1 / float64(len(names))
var current float64
for _, name := range names {
done1 := c.Load(name)
select {
case <-c.ctx.Done():
return
case <-done1:
}
current += step
progress <- current
}
}()
return
}
func (c *container) UnloadAll() {
c.m.RLock()
names := make([]string, 0, len(c.loadingfiles))
for name, _ := range c.loadingfiles {
names = append(names, name)
}
c.m.RUnlock()
for _, name := range names {
_, _ = c.Unload(name)
}
}
func (c *container) Get(name string) ([]byte, error) {
c.m.RLock()
fd, ok := c.loadedfiles[name]
c.m.RUnlock()
if ok {
return fd, nil
}
c.m.RLock()
fch, ok := c.loadingfiles[name]
c.m.RUnlock()
if ok {
<-fch
c.m.RLock()
fd = c.loadedfiles[name]
c.m.RUnlock()
if fd == nil {
return nil, errors.New("x not found")
}
return fd, nil
}
// check if exists
if _, err := c.fs.Stat(name); err != nil {
return nil, err
}
ch := c.Load(name)
<-ch
c.m.RLock()
fd = c.loadedfiles[name]
c.m.RUnlock()
if fd == nil {
return nil, errors.New("x not found")
}
return fd, nil
}
func (c *container) GetImage(name string) (image.Image, error) {
b, err := c.Get(name)
if err != nil {
return nil, err
}
img, _, err := image.Decode(bytes.NewReader(b))
return img, err
}
func (c *container) GetAtlas(name string) (*Atlas, error) {
b, err := c.Get(name)
if err != nil {
return nil, err
}
return ParseAtlas(b)
}
func (c *container) GetAudioStream(name string) (*AudioStream, error) {
b, err := c.Get(name)
if err != nil {
return nil, err
}
return ParseAudioStream(name, b)
}
func (c *container) GetAudioBytes(name string) ([]byte, error) {
b, err := c.Get(name)
if err != nil {
return nil, err
}
stream, err := ParseAudioStream(name, b)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
io.Copy(buf, stream.Data)
return buf.Bytes(), nil
}
func (c *container) GetXMLDOM(name string) ([]dom.Node, error) {
b, err := c.Get(name)
if err != nil {
return nil, err
}
return dom.ParseXMLString(string(b))
}
func NewContainer(ctx context.Context, fs Filesystem) Container {
c := &container{
ctx: ctx,
fs: fs,
loadedfiles: make(map[string][]byte),
loadingfiles: make(map[string]chan struct{}),
}
return c
}