-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathskin.go
60 lines (54 loc) · 1.25 KB
/
skin.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
package main
import (
"archive/zip"
"bytes"
"fmt"
"io"
"os"
"strings"
)
type noopCloser struct {
io.Reader
}
func (*noopCloser) Close() error { return nil }
type Skin struct {
files map[string][]byte
}
func (s *Skin) Open(name string) (io.ReadCloser, error) {
f, ok := s.files[name]
if !ok {
return nil, os.ErrNotExist
}
return &noopCloser{bytes.NewReader(f)}, nil
}
// we can afford to load this into memory, they are tiny
func skinFromPath(skinPath string) (*Skin, error) {
f, err := os.Open(skinPath)
if err != nil {
return nil, fmt.Errorf("failed to open skin file: %w", err)
}
defer f.Close()
fInfo, err := f.Stat()
if err != nil {
return nil, fmt.Errorf("failed to stat skin file: %w", err)
}
zf, err := zip.NewReader(f, fInfo.Size())
if err != nil {
return nil, fmt.Errorf("failed to open skin file: %w", err)
}
s := &Skin{
files: make(map[string][]byte),
}
for _, file := range zf.File {
r, err := file.Open()
if err != nil {
return nil, fmt.Errorf("failed to open skin file: %w", err)
}
s.files[strings.ToLower(file.Name)], err = io.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("failed to read skin file: %w", err)
}
r.Close() // this is likely not that important as f closes upon exit.
}
return s, nil
}