forked from Beastwick18/genshin-real-time-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
embed.go
75 lines (64 loc) · 1.53 KB
/
embed.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
package embedded
import (
"embed"
"fmt"
"os"
"path/filepath"
"reflect"
"resin/pkg/logging"
)
//go:embed assets/*
var AssetFiles embed.FS
//go:embed login/WebView2Loader.dll login/WebViewLogin-v0.0.5.exe
var LoginFiles embed.FS
func ReadAssets[T any](a *T) {
val := reflect.ValueOf(a)
elem := val.Elem()
for i := 0; i < elem.NumField(); i++ {
file, ok := elem.Type().Field(i).Tag.Lookup("asset")
if !ok {
continue // no tag
}
bytes, err := AssetFiles.ReadFile(fmt.Sprintf("assets/%s", file))
// Panic on failure to read any asset
if err != nil {
logging.Panic("Failed to read assets:\n%v", err)
os.Exit(1)
return
}
elem.Field(i).SetBytes(bytes)
}
return
}
func ExtractEmbeddedFiles() {
read, err := LoginFiles.ReadDir("login")
if err != nil {
logging.Fail("Failed to read asset dir \"login\":\n%v", err)
return
}
err = os.MkdirAll("login", 0755)
for i, e := range read {
path := fmt.Sprintf("login/%s", e.Name())
winPath := filepath.Join(".", path)
if _, err := os.Stat(winPath); err == nil {
continue // File already exists
}
file, err := LoginFiles.ReadFile(path)
if err != nil {
logging.Fail("failed to read file %d:\n%v", i, err)
continue
}
newFile, err := os.Create(winPath)
defer newFile.Close()
if err != nil {
logging.Fail("failed to create file %d:\n%v", i, err)
continue
}
n, err := newFile.Write(file)
if err != nil {
logging.Fail("failed to write file %d:\n%v", i, err)
continue
}
logging.Info("%s: wrote %d bytes", newFile.Name(), i, n)
}
}