-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
280 lines (247 loc) · 8.99 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/pterm/pterm"
"os"
"path/filepath"
"strconv"
"strings"
)
var (
instanceDir *string
outputDir *string
configPath *string
// Optional
exportName *string
exportVersion *string
silent *bool
debug *bool
apiURL = "https://api.curse.tools/v1/cf/"
buildConfig BuildJson
appVersion = "${VERSION}"
tmpDir string
)
func init() {
instanceDir = flag.String("d", "./", "Path to Minecraft instance")
outputDir = flag.String("o", "./out", "Location to output export zip")
configPath = flag.String("c", "./.build.json", "Path to .build.json")
// Optional
exportName = flag.String("n", "CurseForge-Export", "Name of the export")
exportVersion = flag.String("v", "1.0.0", "Version of the export")
// Other
silent = flag.Bool("silent", false, "Silent output")
debug = flag.Bool("debug", false, "Debug output")
showHelp := flag.Bool("help", false, "Show help")
flag.Parse()
if *showHelp {
flag.Usage()
os.Exit(0)
}
if *debug {
pterm.EnableDebugMessages()
pterm.Debug.Prefix = pterm.Prefix{
Text: "DEBUG",
Style: pterm.NewStyle(pterm.BgLightMagenta, pterm.FgBlack),
}
pterm.Debug.MessageStyle = pterm.NewStyle(98)
pterm.Debug.Println("Debug mode enabled")
}
if *silent {
pterm.Debug.Println("Silent mode enabled")
pterm.DisableOutput()
}
logo, _ := pterm.DefaultBigText.WithLetters(
pterm.NewLettersFromStringWithStyle("CF", pterm.NewStyle(pterm.FgYellow)),
pterm.NewLettersFromStringWithStyle("Exporter", pterm.NewStyle(pterm.FgGray)),
).Srender()
pterm.DefaultCenter.Println(logo) // Print BigLetters with the default CenterPrinter
pterm.DefaultCenter.WithCenterEachLineSeparately().Println(fmt.Sprintf("Version: %s", appVersion))
pterm.DefaultSection.Println("Initializing...")
buildConfig = readBuildJson(*configPath)
if buildConfig.PackAuthor == "" {
pterm.Error.Println("No author specified in .build.json")
os.Exit(1)
}
if buildConfig.MinecraftVersion == "" {
pterm.Error.Println("No Minecraft version specified in .build.json")
os.Exit(1)
}
if buildConfig.ModLoader == "" {
pterm.Error.Println("No mod loader specified in .build.json")
os.Exit(1)
}
if buildConfig.MinecraftVersion == "" {
pterm.Error.Println("No Minecraft version specified in .build.json")
os.Exit(1)
}
pterm.Debug.Println("Config:", buildConfig)
pterm.Success.Println("Initialized")
}
func main() {
var err error
tmpDir, err = os.MkdirTemp("", "cfExporter-*")
if err != nil {
pterm.Error.Println("Could not create temp dir:", err)
os.Exit(1)
}
readInstanceDir()
}
func readInstanceDir() {
pterm.Info.Println("Reading Minecraft instance...")
files, err := os.ReadDir(*instanceDir)
if err != nil {
pterm.Error.Println("Failed to read Minecraft instance:", err)
os.Exit(1)
}
fileProgress, _ := pterm.DefaultProgressbar.WithTotal(len(files)).WithTitle("Reading instance directory").Start()
var foldersToScan []string
for _, file := range files {
fileProgress.UpdateTitle("Found: " + file.Name())
fileProgress.Increment()
if strings.ToLower(file.Name()) == "mods" || strings.ToLower(file.Name()) == "resourcepacks" {
foldersToScan = append(foldersToScan, file.Name())
}
}
scanFiles(foldersToScan)
}
func scanFiles(folders []string) {
var fMatchResp *FingerprintResponse
var exactMatches []FingerprintExactMatches
for _, folder := range folders {
var fingerprints []int64
pterm.Info.Println("Scanning " + folder + " directory...")
files, err := os.ReadDir(filepath.Join(*instanceDir, folder))
if err != nil {
pterm.Error.Println("Failed to read "+folder+" directory\n", err)
os.Exit(1)
}
fileProgress, _ := pterm.DefaultProgressbar.WithTotal(len(files)).WithTitle("Reading " + folder + " directory").Start()
for _, file := range files {
fileProgress.UpdateTitle("Found: " + file.Name())
fileHash, _ := GetFileHash(filepath.Join(*instanceDir, folder, file.Name()))
fingerprints = append(fingerprints, fileHash)
fileProgress.Increment()
}
fMatchResp, err = getProjectIds(fingerprints)
if fMatchResp != nil {
pterm.Debug.Println("Missing matches:", difference(fMatchResp.Data.InstalledFingerprints, fMatchResp.Data.ExactFingerprints))
genOverrides(difference(fMatchResp.Data.InstalledFingerprints, fMatchResp.Data.ExactFingerprints), folder)
} else {
pterm.Error.Println("Failed to get project IDs:", err)
os.Exit(1)
}
exactMatches = append(exactMatches, fMatchResp.Data.ExactMatches...)
}
extraIncludes()
genExport(exactMatches)
}
func genOverrides(missingFiles []int64, folder string) {
pterm.DefaultSection.Println("Generating overrides for " + folder + "...")
if _, err := os.Stat(filepath.Join(tmpDir, "overrides")); os.IsNotExist(err) {
err := os.Mkdir(filepath.Join(tmpDir, "overrides"), os.ModePerm)
if err != nil {
pterm.Error.Println("Failed to create overrides directory:", err)
}
}
if len(missingFiles) > 0 {
pterm.Debug.Println("Adding missing files to overrides:", missingFiles)
if _, err := os.Stat(filepath.Join(tmpDir, "overrides", folder)); os.IsNotExist(err) {
err := os.Mkdir(filepath.Join(tmpDir, "overrides", folder), os.ModePerm)
if err != nil {
pterm.Error.Println(fmt.Sprintf("Failed to create overrides/%s directory:", folder), err)
}
}
files, err := os.ReadDir(filepath.Join(*instanceDir, folder))
if err != nil {
pterm.Error.Println("Failed to read "+folder+" directory\n", err)
os.Exit(1)
}
fileProgress, _ := pterm.DefaultProgressbar.WithTotal(len(files)).WithTitle("Reading " + folder + " directory for overrides").Start()
for _, file := range files {
fileHash, _ := GetFileHash(filepath.Join(*instanceDir, folder, file.Name()))
if intInSlice(fileHash, missingFiles) {
fileProgress.UpdateTitle("Adding to overrides: " + file.Name())
pterm.Debug.Println(fmt.Sprintf("Failed to find file %s on CurseForge - generating override", file.Name()))
modSrc := filepath.Join(*instanceDir, folder, file.Name())
err = CopyFile(modSrc, filepath.Join(tmpDir, "overrides", folder, file.Name()))
if err != nil {
pterm.Error.Println("Failed to copy file:", err)
}
}
fileProgress.Increment()
}
}
}
func extraIncludes() {
includeProgress, _ := pterm.DefaultProgressbar.WithTotal(len(buildConfig.Includes)).WithTitle("Adding extra includes to overrides").Start()
for _, include := range buildConfig.Includes {
if include != "mods" && include != "resourcepacks" {
includeProgress.UpdateTitle("Adding: " + include + " to overrides")
fToInclude := filepath.Join(*instanceDir, include)
fi, err := os.Stat(fToInclude)
if err != nil {
includeProgress.UpdateTitle("Skipping adding: " + include + " to overrides")
pterm.Warning.Println("Failed to read "+include+" directory\n", err)
includeProgress.Increment()
continue
}
if fi.IsDir() {
err = CopyDir(fToInclude, filepath.Join(tmpDir, "overrides", include))
if err != nil {
pterm.Error.Println("Failed to copy directory:", err)
}
} else {
err = CopyFile(fToInclude, filepath.Join(tmpDir, "overrides", include))
if err != nil {
pterm.Error.Println("Failed to copy file:", err)
}
}
} else {
pterm.Debug.Println("Skipping", include)
}
includeProgress.Increment()
}
}
func genExport(projectFiles []FingerprintExactMatches) {
pterm.DefaultSection.Println("Generating export...")
var modLoader []ModLoaders
var tempFiles []Files
projectFilesProgress, _ := pterm.DefaultProgressbar.WithTotal(len(projectFiles)).WithTitle("Adding files to manifest").Start()
for _, file := range projectFiles {
projectFilesProgress.UpdateTitle("Adding " + strconv.Itoa(file.Id) + " to manifest")
tempFiles = append(tempFiles, Files{ProjectID: file.Id, FileID: file.File.Id, Required: true})
projectFilesProgress.Increment()
}
modLoader = append(modLoader, ModLoaders{buildConfig.ModLoader + "-" + buildConfig.ModLoaderVersion, true})
minecraft := Minecraft{buildConfig.MinecraftVersion, modLoader}
manifest := ExportManifest{minecraft, "minecraftModpack", 1, *exportName, *exportVersion, buildConfig.PackAuthor, tempFiles, "overrides"}
manifestJson, err := json.MarshalIndent(manifest, "", " ")
if err != nil {
pterm.Error.Println("Failed to marshal manifest:", err)
os.Exit(1)
}
err = os.WriteFile(filepath.Join(tmpDir, "manifest.json"), manifestJson, 0644)
if err != nil {
pterm.Error.Println("Failed to write manifest:", err)
os.Exit(1)
}
if _, err := os.Stat(*outputDir); os.IsNotExist(err) {
err := os.Mkdir(*outputDir, os.ModePerm)
if err != nil {
pterm.Error.Println("Failed to create output directory:", err)
}
}
err = RecursiveZip(tmpDir, filepath.Join(*outputDir, *exportName+"-"+*exportVersion+".zip"))
if err != nil {
pterm.Error.Println("Failed to zip export:", err)
os.Exit(1)
}
pterm.Success.Println("Export generated successfully\n", "Created zip: "+*exportName+"-"+*exportVersion+".zip")
pterm.Info.Println("Removing temporary directory:", tmpDir)
err = os.RemoveAll(tmpDir)
if err != nil {
pterm.Error.Println("Failed to remove temporary directory:", err)
}
}