/
exo.go
71 lines (63 loc) · 1.77 KB
/
exo.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
package pjsekaioverlay
import (
_ "embed"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"unicode/utf16"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
)
func encodeString(str string) string {
bytes := utf16.Encode([]rune(str))
encoded := make([]string, 1024)
if len(str) > 1024 {
panic("too long string")
}
for i := range encoded {
var hex string
if i >= len(bytes) {
hex = fmt.Sprintf("%04x", 0)
} else {
hex = fmt.Sprintf("%02x%02x", bytes[i]&0xff, bytes[i]>>8)
}
encoded[i] = hex
}
return strings.Join(encoded, "")
}
//go:embed main.exo
var rawBaseExo []byte
func WriteExoFiles(assets string, destDir string, title string, description string) error {
baseExo := string(rawBaseExo)
replacedExo := baseExo
mapping := []string{
"{assets}", strings.ReplaceAll(assets, "\\", "/"),
"{dist}", strings.ReplaceAll(destDir, "\\", "/"),
"{text:difficulty}", encodeString("MASTER"),
"{text:title}", encodeString(title),
"{text:description}", encodeString(description),
}
for i := range mapping {
if i%2 == 0 {
continue
}
if !strings.Contains(replacedExo, mapping[i-1]) {
panic(fmt.Sprintf("exoファイルの生成に失敗しました(%sが見つかりません)", mapping[i-1]))
}
replacedExo = strings.ReplaceAll(replacedExo, mapping[i-1], mapping[i])
}
replacedExo = strings.ReplaceAll(replacedExo, "\n", "\r\n")
encodedExo, err := io.ReadAll(transform.NewReader(
strings.NewReader(replacedExo), japanese.ShiftJIS.NewEncoder()))
if err != nil {
return fmt.Errorf("エンコードに失敗しました(%w)", err)
}
if err := os.WriteFile(filepath.Join(destDir, "main.exo"),
encodedExo,
0644); err != nil {
return fmt.Errorf("ファイルの書き込みに失敗しました(%w)", err)
}
return nil
}