-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
230 lines (164 loc) · 5.12 KB
/
main.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
package main
import (
"errors"
"flag"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
"time"
)
// API URL
// For more documentation on the API visit: https://twitchappapi.docs.apiary.io/
const API = "https://addons-ecs.forgesvc.net/api/v2/"
// Globals
var (
jarFingerprints map[int]string // Contains 32-bit MurmurHash2 of each .jar and the fileName
oldMap map[string][]string // Old mods map
newMap map[string][]string // New mods map
externalMods []string // Mods that cannot be found on CurseForge
currentTime time.Time // Current time
)
// Command line Arguments
var (
instancePath *string // Path to instance folder
gameVersion *string // Specified game version
releaseType *string // File release type
downloadPath *string // Path for new .jar files
exportNewManifest *bool // Export manifest.json
exportOldManifest *bool // Export oldmanifest.json
exportManifestPath *string // Path to export.json
silentMode *bool // Silent mode
cliFlag *bool // Enable CLI
guiFlag *bool // Enable GUI
)
func main() {
parseArgs()
getTime()
if *cliFlag {
readInstancePath()
useArgs()
checkUpdates(oldMap, newMap)
}
if *guiFlag {
launchGUI()
}
}
// Parse arguments specified in the CLI
func parseArgs() {
cliFlag = flag.Bool("cli", false, "CLI. Must be followed up by arguments.")
guiFlag = flag.Bool("gui", false, "GUI. Standalone, don't bother using additional arguments.")
instancePath = flag.String("d", "./", "Absolute path to Minecraft instance folder.")
gameVersion = flag.String("version", "1.12.2", "Game version of located mods.")
releaseType = flag.String("release", "stable", "Release type of file to check for.")
downloadPath = flag.String("download", "./", "Path of where to download .jar file")
exportNewManifest = flag.Bool("export-new", false, "Creation of new manifest.json")
exportOldManifest = flag.Bool("export-old", false, "Creation of old manifest.json")
exportManifestPath = flag.String("manifest", "./", "Absolute path of export.json")
silentMode = flag.Bool("s", false, "Silent mode.")
flag.Parse()
}
// getTime gets the current time and stores it into currentTime as a
// RFC3339Nano format.
// Example: currentTime = 2019-08-06 16:22:02.5950613 -0400 EDT
func getTime() {
log.Println("Retrieving current local time...")
var err error
currentTimeStr := time.Now().Format(time.RFC3339Nano)
currentTime, err = time.Parse(time.RFC3339Nano, currentTimeStr)
if err != nil {
log.Println("Cannot parse current time/date.")
log.Println(err)
}
log.Printf("Current local time: %v", currentTime)
}
// Determine if instance folder is valid & reads it
func readInstancePath() {
err := readInstanceFolder(*instancePath)
if err != nil {
log.Println(err)
}
}
// Use arguments specified in the CLI
func useArgs() {
if *exportNewManifest {
readExport(*exportManifestPath, "new")
}
if *exportOldManifest {
readExport(*exportManifestPath, "old")
}
if *silentMode {
log.SetOutput(ioutil.Discard)
}
}
// Actually read the instance folder
func readInstanceFolder(path string) error {
log.Println("Reading Minecraft directory...")
files, err := ioutil.ReadDir(path)
if err != nil {
log.Println("Cannot read directory.")
log.Println(err)
}
for _, f := range files {
if strings.ToLower(f.Name()) == "mods" {
log.Println("Reading Minecraft directory completed.")
listMods(path)
return nil
}
}
return errors.New("cannot find mods folder")
}
func listMods(modsFolder string) {
log.Print("Reading mods folder...")
jarFingerprints = make(map[int]string)
err := filepath.Walk(path.Join(modsFolder, "mods"), func(path string, info os.FileInfo, err error) error {
// Skip subdirectories, this ignores unnecessary folders such as: [1.12.2, memory_repo]
if info.IsDir() && info.Name() != "mods" {
return filepath.SkipDir
}
if filepath.Ext(path) == ".jar" {
fileHash, _ := GetFileHash(path)
jarFingerprints[fileHash] = info.Name()
}
return nil
})
if err != nil {
log.Println("Error searching through mods folder.")
log.Println(err)
}
log.Println("Reading mods folder completed.")
createMaps(jarFingerprints)
}
func checkUpdates(oldMap map[string][]string, newMap map[string][]string) {
// Create the directory if downloads requested
if *downloadPath != "./" {
err := os.Mkdir(*downloadPath, os.ModePerm)
if err != nil {
log.Println(err)
}
}
log.Println("--- Updates ---")
var updates int
for key, value := range newMap {
if value[2] != oldMap[key][2] { // (new fileID) != (old fileID)
// Check for empty values
if value[0] != "" {
log.Printf("Name: %v | URL: %v | ID: %v", value[0], value[1], value[2])
updates++
// Actually download the files
if *downloadPath != "./" {
// Pass in the fileName and downloadUrl
err := DownloadFile(value[0], value[1])
if err != nil {
log.Println(err)
}
}
}
}
}
log.Println("--- End of updates ---")
log.Printf("* Available updates: %v *", updates)
log.Printf("Mods that can't be found on CurseForge: %v", externalMods)
}