-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathicons.go
More file actions
132 lines (122 loc) · 2.96 KB
/
Copy pathicons.go
File metadata and controls
132 lines (122 loc) · 2.96 KB
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
package main
import (
"archive/zip"
"bufio"
"bytes"
"embed"
"encoding/base64"
"errors"
"io"
fs2 "io/fs"
"path"
"regexp"
"strings"
)
const hkSplitMakerDir = "hk-split-maker/src/asset/hollowknight"
//go:embed hk-split-maker/src/asset/hollowknight/icons
var iconFs embed.FS
var iconDict = make(map[string]string)
func init() {
f, err := iconFs.Open(hkSplitMakerDir + "/icons/icons.ts")
if err != nil {
panic(err)
}
defer func() { _ = f.Close() }()
re, err := regexp.Compile(`import\s+(\w+)\s+from\s+"(.*?)"\s*;`)
if err != nil {
panic(err)
}
rd := bufio.NewReader(f)
line, isPrefix, err := rd.ReadLine()
for ; err == nil; line, isPrefix, err = rd.ReadLine() {
if isPrefix {
panic(err)
}
lineStr := strings.TrimSpace(string(line))
if len(lineStr) < 2 || lineStr[:2] == "//" {
continue
}
result := re.FindStringSubmatch(lineStr)
if result != nil {
iconDict[result[1]] = result[2]
}
}
if errors.Is(err, io.EOF) {
err = nil
}
if err != nil {
panic(err)
}
}
func zipIcons() ([]byte, error) {
b := &bytes.Buffer{}
zipWriter := zip.NewWriter(b)
err := fs2.WalkDir(iconFs, hkSplitMakerDir+"/icons", func(filePath string, d fs2.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || d.Name() == "icons.ts" {
return nil
}
fw, err := zipWriter.Create(strings.Replace(filePath, "hk-split-maker/src/asset/hollowknight/", "", 1))
if err != nil {
return err
}
buf, err := iconFs.ReadFile(filePath)
if err != nil {
return err
}
_, err = fw.Write(buf)
return err
})
if err != nil {
return nil, err
}
if err = zipWriter.Close(); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func getIconHtmlFormat(splitId string) string {
iconPath, ok := iconDict[splitId]
if !ok {
return ""
}
iconPath = path.Join(hkSplitMakerDir, "icons", iconPath)
buf, err := iconFs.ReadFile(iconPath)
if err != nil {
return ""
}
s := base64.StdEncoding.EncodeToString(buf)
return htmlFormatHeader + s
}
func convertIconToHtmlFormat(icon string) string {
if len(icon) == 0 {
return ""
}
buf, err := base64.StdEncoding.DecodeString(icon)
if err != nil {
return ""
}
headerIndex := bytes.Index(buf, []byte("\x89PNG\x0D\x0A\x1A\x0A"))
if headerIndex < 0 {
return ""
}
buf = buf[headerIndex:]
return htmlFormatHeader + base64.StdEncoding.EncodeToString(buf)
}
func convertIconToLiveSplitFormat(icon string) string {
if !strings.HasPrefix(icon, htmlFormatHeader) {
return ""
}
icon = icon[len(htmlFormatHeader):]
buf, err := base64.StdEncoding.DecodeString(icon)
if err != nil || len(buf) == 0 {
return ""
}
return livesplitFormatHeader + base64.StdEncoding.EncodeToString(append([]byte{0, 2}, buf...))
}
const (
htmlFormatHeader = "data:image/png;base64,"
livesplitFormatHeader = "AAEAAAD/////AQAAAAAAAAAMAgAAAFFTeXN0ZW0uRHJhd2luZywgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWIwM2Y1ZjdmMTFkNTBhM2EFAQAAABVTeXN0ZW0uRHJhd2luZy5CaXRtYXABAAAABERhdGEHAgIAAAAJAwAAAA8DAAAAOw8A"
)