forked from 1lann/lol-replay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieve.go
199 lines (159 loc) · 4.77 KB
/
retrieve.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
package recording
import (
"bytes"
"encoding/gob"
"io"
"time"
)
// HasChunk returns whether or not the specified chunk ID already exists in
// the recording or not.
func (r *Recording) HasChunk(num int) bool {
r.mutex.Lock()
defer r.mutex.Unlock()
_, found := r.header.ChunkMap[num]
return found
}
// HasKeyFrame returns whether or not the specified keyframe already exists in
// the recording or not.
func (r *Recording) HasKeyFrame(num int) bool {
r.mutex.Lock()
defer r.mutex.Unlock()
_, found := r.header.KeyFrameMap[num]
return found
}
// HasGameMetadata returns whether or not the metadata of the game has already
// been written to the recording or not.
func (r *Recording) HasGameMetadata() bool {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.header.GameMetadata.Length > 0
}
// HasUserMetadata returns whether or not the user metadata has already been
// written to the recording or not.
func (r *Recording) HasUserMetadata() bool {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.header.UserMetadata.Length > 0
}
// RetrieveUserMetadata retrieves the arbitrary user data stored by
// StoreUserMetadata into metadata.
func (r *Recording) RetrieveUserMetadata(metadata interface{}) error {
r.mutex.Lock()
defer r.mutex.Unlock()
if r.header.UserMetadata.Length <= 0 {
return ErrMissingData
}
gob.RegisterName("UserMetadata", metadata)
if _, err := r.file.Seek(int64(r.header.UserMetadata.Position), 0); err != nil {
return err
}
dec := gob.NewDecoder(io.LimitReader(r.file, int64(r.header.UserMetadata.Length)))
return dec.Decode(metadata)
}
// RetrieveGameInfo retrieves the recorded game's basic information.
func (r *Recording) RetrieveGameInfo() GameInfo {
return r.header.Info
}
// IsComplete returns whether or not the recording has been declared as
// being complete or not.
func (r *Recording) IsComplete() bool {
return r.header.IsComplete
}
// LastWriteTime returns the last time data was written to the recording.
func (r *Recording) LastWriteTime() time.Time {
return r.header.LastWriteTime
}
// RetrieveGameMetadataTo retrieves the recorded game metadata into w.
// The number of bytes written to w and any errors that have occurred are
// returned.
func (r *Recording) RetrieveGameMetadataTo(w io.Writer) (int, error) {
r.mutex.Lock()
if r.header.GameMetadata.Length <= 0 {
r.mutex.Unlock()
return 0, ErrMissingData
}
if _, err := r.file.Seek(int64(r.header.GameMetadata.Position), 0); err != nil {
r.mutex.Unlock()
return 0, err
}
buf := bufferPool.Get().(*bytes.Buffer)
defer func() {
buf.Reset()
bufferPool.Put(buf)
}()
if _, err := io.CopyN(buf, r.file, int64(r.header.GameMetadata.Length)); err != nil {
r.mutex.Unlock()
return 0, err
}
r.mutex.Unlock()
written, err := buf.WriteTo(w)
return int(written), err
}
// RetrieveFirstChunkInfo retrieves the chunk info that should be returned
// first to the client.
func (r *Recording) RetrieveFirstChunkInfo() ChunkInfo {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.header.FirstChunkInfo
}
// RetrieveLastChunkInfo retrieves the chunk info that should be returned
// after FirstChunkInfo.
func (r *Recording) RetrieveLastChunkInfo() ChunkInfo {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.header.LastChunkInfo
}
// RetrieveChunkTo retrieves the chunk data for a chunk ID into w. The number
// of bytes written to w and any errors that have occurred are returned.
// If the chunk ID does not exist, ErrMissingData will be returned.
func (r *Recording) RetrieveChunkTo(num int, w io.Writer) (int, error) {
r.mutex.Lock()
seg, found := r.header.ChunkMap[num]
if !found {
r.mutex.Unlock()
return 0, ErrMissingData
}
if _, err := r.file.Seek(int64(seg.Position), 0); err != nil {
r.mutex.Unlock()
return 0, err
}
buf := bufferPool.Get().(*bytes.Buffer)
defer func() {
buf.Reset()
bufferPool.Put(buf)
}()
if _, err := io.CopyN(buf, r.file, int64(seg.Length)); err != nil {
r.mutex.Unlock()
return 0, err
}
r.mutex.Unlock()
written, err := buf.WriteTo(w)
return int(written), err
}
// RetrieveKeyFrameTo retrieves the keyframe data into w. The number
// of bytes written to w and any errors that have occurred are returned.
// If the chunk ID does not exist, ErrMissingData will be returned.
func (r *Recording) RetrieveKeyFrameTo(num int, w io.Writer) (int, error) {
r.mutex.Lock()
seg, found := r.header.KeyFrameMap[num]
if !found {
r.mutex.Unlock()
return 0, ErrMissingData
}
if _, err := r.file.Seek(int64(seg.Position), 0); err != nil {
r.mutex.Unlock()
return 0, err
}
buf := bufferPool.Get().(*bytes.Buffer)
defer func() {
buf.Reset()
bufferPool.Put(buf)
}()
if _, err := io.CopyN(buf, r.file, int64(seg.Length)); err != nil {
r.mutex.Unlock()
return 0, err
}
r.mutex.Unlock()
written, err := buf.WriteTo(w)
return int(written), err
}