forked from enmity-mod/patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
134 lines (103 loc) · 2.49 KB
/
util.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
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
133
134
package main
import (
"io"
"net/http"
"os"
"path/filepath"
"howett.net/plist"
)
func fileNameWithoutExtension(name string) string {
return name[:len(name)-len(filepath.Ext(name))]
}
func exit() {
logger.Debug("Cleaning up...")
if _, e := os.Stat(directory); e == nil {
if e := os.RemoveAll(directory); e != nil {
logger.Errorf("Failed to clean up extracted directory: %v", e)
}
}
if _, e := os.Stat(assets); e == nil {
defer func() {
if e := os.RemoveAll(assets); e != nil {
logger.Errorf("Failed to clean up temporary assets directory: %v", e)
}
}()
}
logger.Info("Cleaned up.")
os.Exit(0)
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func filter[T any](ss []T, test func(T) bool) (ret []T) {
for _, s := range ss {
if test(s) {
ret = append(ret, s)
}
}
return
}
func loadInfo() {
if info != nil {
return
}
path := filepath.Join(directory, "Payload", "Discord.app", "Info.plist")
file, err := os.Open(path)
if err != nil {
logger.Error("Couldn't find Info.plist. Is the provided zip an IPA file?")
exit()
}
decoder := plist.NewDecoder(file)
if err := decoder.Decode(&info); err != nil {
logger.Error("Couldn't find Info.plist. Is the provided zip an IPA file?")
exit()
}
}
func saveInfo() {
path := filepath.Join(directory, "Payload", "Discord.app", "Info.plist")
file, err := os.OpenFile(path, os.O_RDWR|os.O_TRUNC, 0600)
if err != nil {
logger.Errorf("Failed to open Info.plist for saving: %v", err)
exit()
}
logger.Debug("Saving Info.plist data...")
encoder := plist.NewEncoder(file)
err = encoder.Encode(info)
if err != nil {
logger.Errorf("Failed to save Info.plist. %v", err)
exit()
}
logger.Infof("Saved Info.plist data.")
}
func download(url string, path string) {
out, err := os.Create(path)
if err != nil {
logger.Errorf("Failed to pre-write file at %s.", path)
exit()
}
res, err := http.Get(url)
if err != nil {
logger.Errorf("Failed to download %s to %s %v", url, path, err)
exit()
}
defer res.Body.Close()
defer out.Close()
if res.StatusCode != http.StatusOK {
logger.Errorf("Received bad status while downloading %s: %s", url, res.Status)
exit()
}
_, err = io.Copy(out, res.Body)
if err == nil {
logger.Infof("Successfully downloaded \"%s\" to \"%s\".", url, path)
} else {
logger.Errorf("Failed to write \"%s\" to \"%s\": %v.", url, path, err)
exit()
}
}