-
Notifications
You must be signed in to change notification settings - Fork 6
/
fileparse.go
266 lines (228 loc) · 6.96 KB
/
fileparse.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"crypto/sha1"
"encoding/json"
"fmt"
"image/color"
"io"
"os"
"path/filepath"
"strings"
"fyne.io/fyne/v2/theme"
fatihColor "github.com/fatih/color"
)
func getSHA1Hash(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()
hash := sha1.New()
if _, err := io.Copy(hash, file); err != nil {
return "", err
}
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}
func loadIgnoreList(filepath string) ([]string, error) {
var ignoreList []string
data, err := os.ReadFile(filepath)
if err != nil {
return nil, err
}
if err := json.Unmarshal(data, &ignoreList); err != nil {
return nil, err
}
return ignoreList, nil
}
func contains(slice []string, val string) bool {
for _, item := range slice {
// fmt.Printf("Comparing %q to %q\n", item, val)
if item == val {
return true
}
}
return false
}
func checkForContent(directory string) error {
if _, err := os.Stat(directory); os.IsNotExist(err) {
printInfo(fatihColor.FgYellow, "%s directory not found\n", directory)
return fmt.Errorf("%s directory not found", directory)
}
logOutput := func(s string) {
if !guiEnabled {
printInfo(fatihColor.FgYellow, s+"\n")
} else {
addText(theme.PrimaryColorNamed(theme.ColorYellow), s)
}
}
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Check directories that are exactly 8 characters long, potential titleID
if info.IsDir() && len(info.Name()) == 8 {
titleID := strings.ToLower(info.Name())
titleData, ok := titles.Titles[titleID]
if ok {
// Process known titles as before
if guiEnabled {
addHeader(titleData.TitleName)
}
printHeader(titleData.TitleName)
}
// Check and potentially process $c subdirectory
subDirDLC := filepath.Join(path, "$c")
subInfoDLC, err := os.Stat(subDirDLC)
if err == nil && subInfoDLC.IsDir() {
if ok { // Process content if titleID is known
err = processDLCContent(subDirDLC, titleData, titleID, directory)
if err != nil {
return err
}
} else {
logOutput(fmt.Sprintf("DLC content found in unrecognized directory: %s\n", subDirDLC))
}
}
// Check and potentially process $u subdirectory
subDirUpdates := filepath.Join(path, "$u")
subInfoUpdates, err := os.Stat(subDirUpdates)
if err == nil && subInfoUpdates.IsDir() {
if ok { // Process updates if titleID is known
err = processUpdates(subDirUpdates, titleData, titleID, directory)
if err != nil {
return err
}
} else {
if guiEnabled {
}
logOutput(fmt.Sprintf("Updates found in unrecognized directory: %s\n", subDirUpdates))
}
}
if !ok {
return filepath.SkipDir // Skip further processing in unrecognized directories
}
}
return nil
})
return err
}
func processDLCContent(subDirDLC string, titleData TitleData, titleID string, directory string) error {
subContents, err := os.ReadDir(subDirDLC)
if err != nil {
return err
}
for _, subContent := range subContents {
subContentPath := filepath.Join(subDirDLC, subContent.Name())
if !subContent.IsDir() {
continue
}
subDirContents, err := os.ReadDir(subContentPath)
if err != nil {
return err
}
hasContentMetaXbx := false
for _, dlcFiles := range subDirContents {
if strings.Contains(strings.ToLower(dlcFiles.Name()), "contentmeta.xbx") && !dlcFiles.IsDir() {
hasContentMetaXbx = true
break
}
}
if !hasContentMetaXbx {
continue
}
contentID := strings.ToLower(subContent.Name())
if !contains(titleData.ContentIDs, contentID) {
if guiEnabled {
addText(theme.ErrorColor(), "Unknown content found at: %s", subContentPath)
}
printInfo(fatihColor.FgRed, "Unknown content found at: %s\n", subContentPath)
continue
}
archivedName := ""
for _, archived := range titleData.Archived {
for archivedID, name := range archived {
if archivedID == contentID {
archivedName = name
break
}
}
if archivedName != "" {
break
}
}
subContentPath = strings.TrimPrefix(subContentPath, directory+"/")
if archivedName != "" {
if guiEnabled {
addText(theme.PrimaryColorNamed(theme.ColorGreen), "Content is known and archived %s", archivedName)
}
printInfo(fatihColor.FgGreen, "Content is known and archived %s\n", archivedName)
} else {
if guiEnabled {
addText(theme.ErrorColor(), "%s has unarchived content found at: %s", titleData.TitleName, subContentPath)
}
printInfo(fatihColor.FgYellow, "%s has unarchived content found at: %s\n", titleData.TitleName, subContentPath)
}
}
return nil
}
func processUpdates(subDirUpdates string, titleData TitleData, titleID string, directory string) error {
files, err := os.ReadDir(subDirUpdates)
if err != nil {
return err
}
knownUpdateFound := false
for _, f := range files {
if filepath.Ext(f.Name()) != ".xbe" {
continue
}
filePath := filepath.Join(subDirUpdates, f.Name())
fileHash, err := getSHA1Hash(filePath)
if err != nil {
if guiEnabled {
addText(theme.ErrorColor(), "Error calculating hash for file: %s, error: %s", f.Name(), err.Error())
}
printInfo(fatihColor.FgRed, "Error calculating hash for file: %s, error: %s\n", f.Name(), err.Error())
continue
}
for _, knownUpdate := range titleData.TitleUpdatesKnown {
for knownHash, name := range knownUpdate {
if knownHash == fileHash {
if guiEnabled {
addHeader("File Info")
addText(theme.PrimaryColorNamed(theme.ColorGreen), "Known and Archived Title update found for %s (%s) (%s)", titleData.TitleName, titleID, name)
filePath = strings.TrimPrefix(filePath, directory+"/")
addText(theme.PrimaryColorNamed(theme.ColorGreen), "Path: %s", filePath)
addText(theme.PrimaryColorNamed(theme.ColorGreen), "SHA1: %s", fileHash)
addText(color.Transparent, separator)
}
printHeader("File Info")
printInfo(fatihColor.FgGreen, "Known and Archive Title update found for %s (%s) (%s)\n", titleData.TitleName, titleID, name)
filePath = strings.TrimPrefix(filePath, directory+"/")
printInfo(fatihColor.FgGreen, "Path: %s\n", filePath)
printInfo(fatihColor.FgGreen, "SHA1: %s\n", fileHash)
fmt.Println(separator)
knownUpdateFound = true
break
}
}
if knownUpdateFound {
break
}
}
if !knownUpdateFound {
if guiEnabled {
addHeader("File Info")
addText(theme.ErrorColor(), "Unknown Title Update found for %s (%s)", titleData.TitleName, titleID)
filePath = strings.TrimPrefix(filePath, directory+"/")
addText(theme.ErrorColor(), "Path: %s", filePath)
addText(theme.ErrorColor(), "SHA1: %s", fileHash)
}
printHeader("File Info")
printInfo(fatihColor.FgRed, "Unknown Title Update found for %s (%s)\n", titleData.TitleName, titleID)
filePath = strings.TrimPrefix(filePath, directory+"/")
printInfo(fatihColor.FgRed, "Path: %s\n", filePath)
printInfo(fatihColor.FgRed, "SHA1: %s\n", fileHash)
}
}
return nil
}