-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathanimation.go
49 lines (40 loc) · 972 Bytes
/
animation.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
package main
import (
"bytes"
_ "embed"
"fmt"
"os"
"strings"
)
type Animation struct {
Metadata map[string]string
Frames [][]byte
}
func LoadFromFile(file string) (*Animation, error) {
b, err := os.ReadFile(file)
if err != nil {
return nil, err
}
return LoadFromBytes(b)
}
func LoadFromBytes(b []byte) (*Animation, error) {
frames := bytes.Split(b, []byte("!--FRAME--!\n"))
if len(frames) <= 2 {
return nil, fmt.Errorf("no frames found")
}
// The first "frame" is actually the metadata.
metadata := make(map[string]string)
for _, line := range bytes.Split(frames[0], []byte{'\n'}) {
parts := bytes.SplitN(line, []byte{':'}, 2)
if len(parts) != 2 {
continue
}
metadata[strings.TrimSpace(string(parts[0]))] = strings.TrimSpace(string(parts[1]))
}
for i, frame := range frames[1:] {
if len(frame) == 0 {
return nil, fmt.Errorf("invalid animation: frame %d is empty", i)
}
}
return &Animation{metadata, frames[1:]}, nil
}