-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
606 lines (509 loc) · 17.8 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
package main
/*
Goal of Project:
- Be able to download files from modworkshop (https://modworkshop.net/)
*/
// Different Games in navbar Home/Games/{Game}
// Detect what game users
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/cavaliergopher/grab/v3"
"github.com/gocolly/colly"
"github.com/gosuri/uilive"
"github.com/mholt/archiver/v3"
)
var version string = "1.5.0"
var baseURL string = "modworkshop.net"
var apiURL string = "https://modworkshop.net/api/files/"
var modsDirectory string = "."
var assetsDirectory string = "."
var writer = uilive.New()
// CLI Argument Variables
var file string
var search string
var help bool
var install string
var displayVersion bool
var update bool
var installSBLT bool
type Mod struct {
Did int `json:"did"`
Name string `json:"name"`
SuspendedStatus int `json:"suspended_status"`
FileStatus int `json:"file_status"`
Hidden int `json:"hidden"`
Thumbnail string `json:"thumbnail"`
SubmitterUID int `json:"submitter_uid"`
Submitter string `json:"submitter"`
CollaboratorsNr int `json:"collaborators_nr"`
ShortDescription string `json:"short_description"`
Game string `json:"game"`
GameShort string `json:"game_short"`
Gid int `json:"gid"`
Category string `json:"category"`
Cid int `json:"cid"`
Views interface{} `json:"views"`
Downloads interface{} `json:"downloads"`
Likes int `json:"likes"`
Date string `json:"date"`
PubDate string `json:"pub_date"`
Timeago string `json:"timeago"`
TimeagoPub string `json:"timeago_pub"`
DateTimestamp int `json:"date_timestamp"`
PubDateTimestamp int `json:"pub_date_timestamp"`
IsNsfw int `json:"is_nsfw"`
UsesDefaultThumbnail interface{} `json:"uses_default_thumbnail"`
}
var modResponseObject Response
type Response struct {
Success int `json:"success"`
Cache string `json:"cache"`
Content []Mod `json:"content"`
Total int `json:"total"`
PerPage int `json:"perpage"`
}
var games = map[string]string{
"Payday 2": `C:\Program Files (x86)\Steam\SteamApps\common\PAYDAY 2\mods`,
"Noita": `C:\Program Files (x86)\Steam\SteamApps\common\Noita\mods`,
"Enter the Gungeon": `C:\Program Files (x86)\Steam\steamapps\common\Enter the Gungeon\Mods`,
"Payday: The Heist": `C:\Program Files (x86)\Steam\SteamApps\common\PAYDAY The Heist\mods`,
"Final Fantasy XV": ".",
"Stolen Realm": ".",
"RAID: World War II": ".",
"Aurora": ".",
"Zuma": ".",
"Luxor": ".",
"VRChat": `C:\Program Files (x86)\Steam\SteamApps\common\VRChat\mods`,
"Left 4 Dead 2": `C:\Program Files (x86)\Steam\SteamApps\common\Left 4 Ded 2\left4dead2\addons`,
"Hitman 3": ".",
"Monster Sanctuary": `C:\Program Files (x86)\Steam\SteamApps\common\Monster Sanctuary\BapInEx\plugins`,
"Fallout 4": `C:\Program Files (x86)\Steam\SteamApps\common\Fallout 4\Data`,
"Teardown": `C:\Program Files (x86)\Steam\SteamApps\common\Teardown\data`,
"Black Mesa": ".",
"Yakuza Kiwami 2": ".",
"Hotline Miami 2: Wrong Number": `C:\Program Files (x86)\Steam\SteamApps\common\Hotline Miami 2\mods`,
"Friday Night Funkin'": ".",
"Hotdogs, Horseshoes & Hand Grenades": ".",
"Yakuza Kiwami 1": ".",
"100% Orange Juice": `C:\Program Files (x86)\Steam\SteamApps\common\100% Orange Juice\mods`,
"Hyperdimension Neptunia Re;Birth2": ".",
"Non-games / Plugins": ".",
"Yakuza 0": ".",
"One Step From Eden": ".",
"OVERKILL's The Walking Dead": ".",
"The Elder Scrolls V: Skyrim - Legendary Edition": `C:\Program Files (x86)\Steam\SteamApps\common\Skyrim\Data`,
"SCP: Containment Breach": ".",
"Fallout: New Vegas": `C:\Program Files (x86)\Steam\SteamApps\common\Fallout New Vegas\Data`,
"OneShot": ".",
"SteamVR": `C:\Program Files (x86)\Steam\SteamApps\common\SteamVR\bin\win64`,
"Criminal Girls: Invite Only": ".",
"Gal*Gun: Double Peace": `C:\Program Files (x86)\Steam\SteamApps\common\GalGun Double Peace`,
"Warhammer: End Times - Vermintide": `C:\Program Files (x86)\Steam\SteamApps\common\Warhammer End Times Vermintide\binaries\mods`,
"Tales of Berseria": ".",
"Team Fortress 2": `C:\Program Files (x86)\Steam\SteamApps\common\Team Fortress 2\tf\custom`,
"Hyperdimension Neptunia Re;Birth3": ".",
"Hyperdimension Neptunia Re;Birth1": ".",
"Metal Gear Solid V: The Phantom Pain": ".",
"Skyrim Special Edition": ".",
"Forspoken": ".",
}
func main() {
c := colly.NewCollector(
colly.AllowedDomains(baseURL),
)
parseCliArgs(c)
}
func getModInformation(c *colly.Collector, modLink string) (title string, downloadID string) {
c.OnHTML("div.flex-grow-1.p-3.d-flex.flex-column.data", func(r *colly.HTMLElement) {
title = r.ChildText("[id=title]")
downloadButtonText := r.ChildAttr("[id=download-button]", "href")
if !strings.Contains(downloadButtonText, "download") {
log.Fatal("The Mod You have Requested is Invalid / Does not Have a Download Link. Check the GitHub Page of the Mod!")
}
downloadID = strings.Split(downloadButtonText, "/download/")[1]
})
err := c.Visit(modLink)
if err != nil {
log.Fatalf("There was an error while running the mods you specified. Please Look in the %s file for any formatting errors. %s\n", file, err)
}
return title, downloadID
}
func downloadFromFile(c *colly.Collector) error {
modsArray, assetsArray, err := parseText(file)
if err != nil {
log.Fatal(err)
}
modsDirectory = games["Payday 2"]
assetsDirectory = games["Payday 2"] + `\assets`
fmt.Println(assetsDirectory)
endStr := "Done! The "
if len(modsArray) > 0 {
endStr += "Mods "
fmt.Println("Downloading Mods!")
for i := 0; i < len(modsArray); i++ {
title, downloadID := getModInformation(c, modsArray[i])
resp, err := downloadFile(title, downloadID, modsDirectory)
if err != nil {
return nil
}
unzipFile(resp.Filename, modsDirectory)
os.Remove(resp.Filename)
}
}
if len(modsArray) > 0 && len(assetsArray) > 0 {
endStr += "and "
}
if len(assetsArray) > 0 {
endStr += "Assets "
fmt.Println("Downloading Assets!")
for i := 0; i < len(assetsArray); i++ {
title, downloadID := getModInformation(c, assetsArray[i])
resp, err := downloadFile(title, downloadID, assetsDirectory)
if err != nil {
return nil
}
unzipFile(resp.Filename, assetsDirectory)
os.Remove(resp.Filename)
}
}
fmt.Println(endStr, "Have Been Downloaded and Installed!")
return nil
}
func parseText(filePath string) (modsArray []string, assetsArray []string, error error) {
if doesExist(filePath) {
file, err := os.Open(filePath)
if err != nil {
log.Fatalf("There was an error while parsing the %s. Please check for any possible errors or contact the Developer on GitHub! %s", file.Name(), err)
return nil, nil, fmt.Errorf("Error While Parsing %s", filePath)
}
defer file.Close()
scanner := bufio.NewScanner(file)
matchedMods := false
matchedAssets := false
for scanner.Scan() {
text := scanner.Text()
if text == "" {
continue
}
if text == "Mods" {
matchedMods = true
continue
}
if text == "Assets" {
matchedMods = false
matchedAssets = true
continue
}
if matchedMods {
modsArray = append(modsArray, text)
}
if matchedAssets {
assetsArray = append(assetsArray, text)
}
}
return modsArray, assetsArray, nil
}
return nil, nil, fmt.Errorf("The File %s Does Not Exist", filePath)
}
func doesExist(filePath string) bool {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return false
}
return true
}
func downloadFile(title string, downloadID string, destination string) (resp *grab.Response, error error) {
downloadLink := apiURL + downloadID + "/download?"
resp, err := grab.Get(destination, downloadLink)
if err != nil {
return nil, fmt.Errorf("Error While Downloading The %s. %s", resp.Filename, err)
}
progress := writer.Newline()
writer.Start()
defer writer.Stop()
t := time.NewTicker(100 * time.Millisecond)
defer t.Stop()
fmt.Fprintf(writer, "Downloading: %s\n", title)
Downloading:
for {
select {
case <-t.C:
fmt.Println("Downloading")
fmt.Fprintf(progress, "Downloaded %v / %v (%.2f%%)\n", resp.BytesComplete(), resp.Size(), 100*resp.Progress())
case <-resp.Done:
fmt.Fprintf(progress, "The Download has Complete! Took %v\n", resp.Duration())
break Downloading
}
}
return resp, nil
}
func unzipFile(file string, destination string) {
switch file[len(file)-3:] {
case "zip":
err := archiver.DefaultZip.Unarchive(file, destination)
if err != nil {
log.Fatalf("There was an error while unzipping the Zip File %s", err)
}
case "tar":
err := archiver.DefaultTar.Unarchive(file, destination)
if err != nil {
log.Fatalf("There was an error while unzipping the Tar File %s", err)
}
case "rar":
err := archiver.DefaultRar.Unarchive(file, destination)
if err != nil {
log.Fatalf("There was an error while unzipping the Rar File %s", err)
}
}
}
func parseCliArgs(c *colly.Collector) {
flag.StringVar(&file, "file", "", "The text file containing the mods.")
flag.StringVar(&file, "f", "", "The text file containing the mods.")
flag.StringVar(&search, "search", "", "The Mod To Search.")
flag.StringVar(&search, "S", "", "The Mod To Search")
flag.StringVar(&install, "install", "", "The Mod To Install")
flag.StringVar(&install, "I", "", "The Mod To Install")
flag.BoolVar(&help, "help", false, "View all Commands.")
flag.BoolVar(&help, "h", false, "View all Commands.")
flag.BoolVar(&displayVersion, "version", false, "Display Version")
flag.BoolVar(&displayVersion, "v", false, "Display Version")
flag.BoolVar(&update, "update", false, "Update Modworkshop-DL")
flag.BoolVar(&update, "u", false, "Update Modworkshop-DL")
flag.BoolVar(&installSBLT, "installSBLT", false, "Install SuperBLT")
flag.BoolVar(&installSBLT, "is", false, "Install SuperBLT")
flag.Parse()
// Dear Future Will, Hey! I'm from the past; you are most likely going to tab them all inline because that is just you but here to ruin the fun and say don't bother. Kindest Regards, Past Will 04/03/2023
helpMsg :=
`
Modworkshop-dl allows for installing mods with ease.
usage: modworkshop-dl [<command>] [<argument>]
The following commands are available:
search, S The mod to search [-S <Name>]
file, f The text file containing the mods [-f <File>]
install, I The Link / ModID To Be Installed [-I <Link / ModID>]
help, h Display this Help Message [-h]
version, v Display the Current Version [-v]
update, u Update Modworkshop-DL [-u]
installSBLT, is Install SuperBLT [-is]
`
if len(os.Args) > 1 {
if os.Args[1] == "" {
fmt.Println("none")
}
if os.Args[1] == "-S" || os.Args[1] == "--S" || os.Args[1] == "--search" || os.Args[1] == "-search" {
search = strings.Join(os.Args[2:], " ")
if strings.Contains(search, "http") {
log.Fatalf("It Looks Like You Are Trying to Install a Mod via Link! Run the command:\n modworkshop-dl -I %s", search)
}
searchForMod(search, c)
}
if file != "" {
downloadFromFile(c)
}
if install != "" {
installMod(install, c)
}
if help {
fmt.Println(helpMsg)
}
if displayVersion {
showVersion()
}
if update {
updateProgram()
}
if installSBLT {
installSuperBLT()
}
} else {
fmt.Println(helpMsg)
}
}
func searchForMod(query string, c *colly.Collector) {
res, err := http.Get(fmt.Sprintf("https://modworkshop.net/mws/api/modsapi.php?count_total=1&query=%s&func=mods&page=1", query))
if err != nil {
log.Fatalf("There was an error while fetching the API! Try Again in 5 Minutes or Contact the Developer on GitHub! %s", err)
}
responseData, err := ioutil.ReadAll(res.Body)
json.Unmarshal(responseData, &modResponseObject)
if len(modResponseObject.Content) > 0 {
for i := 0; i < len(modResponseObject.Content); i++ {
if i == 10 {
break
}
fmt.Printf(
`
[%v] %s
🧍 %s
📍 %s / %s
❤ %v 😋 %v 👁️ %v 🕑 %s
`, i+1,
modResponseObject.Content[i].Name,
modResponseObject.Content[i].Submitter,
modResponseObject.Content[i].Game,
modResponseObject.Content[i].Category,
modResponseObject.Content[i].Likes,
modResponseObject.Content[i].Downloads,
modResponseObject.Content[i].Views,
modResponseObject.Content[i].Timeago,
)
}
fmt.Println("\n\nWhich mod would you like to install?")
var modChoice int
fmt.Scanln(&modChoice)
downloadModFromIndex(modChoice, c)
} else {
log.Fatalf("No Mods Found for Query: (%s) ", query)
}
}
func downloadModFromID(ID int, c *colly.Collector, destination string) error {
iID := strconv.Itoa(ID)
link := "https://modworkshop.net/mod/" + iID
err := downloadModFromLink(link, c, destination)
if err != nil {
return err
}
return nil
}
func downloadModFromLink(link string, c *colly.Collector, destination string) error {
title, downloadID := getModInformation(c, link)
resp, err := downloadFile(title, downloadID, destination)
if err != nil {
return err
}
unzipFile(resp.Filename, destination)
os.Remove(resp.Filename)
return nil
}
func downloadModFromIndex(index int, c *colly.Collector) {
index = index - 1
if index > modResponseObject.Total {
index = index + 1
log.Fatalf("The index %d is out the bounds! Select a Mod Provided", index)
}
downloadLink := "https://modworkshop.net/mod/" + strconv.Itoa(modResponseObject.Content[index].Did)
title, downloadID := getModInformation(c, downloadLink)
gameName := modResponseObject.Content[index].Game
gameDir := games[gameName]
ensureDir(gameDir)
resp, err := downloadFile(title, downloadID, gameDir)
if err != nil {
return
}
unzipFile(resp.Filename, gameDir)
os.Remove(resp.Filename)
}
func installMod(mod string, c *colly.Collector) {
if strings.Contains(mod, "http") {
downloadModFromLink(mod, c, ".")
return
}
if len(mod) > 3 && len(mod) < 8 {
iMod, err := strconv.Atoi(mod)
if err != nil {
log.Fatalf("There was a conversion error! Please Contact the Developer on GitHub %s", err)
}
downloadModFromID(iMod, c, modsDirectory)
return
}
}
func ensureDir(dirPath string) error {
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
os.MkdirAll(dirPath, os.ModeSticky|os.ModePerm)
} else {
if err != nil {
log.Fatalf("There was an error ensuring that %s exists! %s\n", dirPath, err)
return fmt.Errorf("The Directory (%s) Could Not Be Created!", dirPath)
}
return nil
}
if strings.Contains(dirPath, "PAYDAY 2") {
ensureDir(`C:\Program Files (x86)\Steam\steamapps\common\PAYDAY 2\mods`)
ensureDir(`C:\Program Files (x86)\Steam\steamapps\common\PAYDAY 2\mods\assets`)
}
ensureDir(dirPath)
return nil
}
func showVersion() {
fmt.Println("v" + version)
}
func updateProgram() {
baseUrl := "https://willkirkmanm.github.io/modworkshop-dl/index.json"
type apiResponse struct {
Version string `json:"version"`
}
var apiRes apiResponse
fmt.Println("Checking Latest Version...")
res, err := http.Get(baseUrl)
if err != nil {
log.Fatal(err)
}
responseData, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
json.Unmarshal(responseData, &apiRes)
if version == apiRes.Version {
fmt.Printf("You are already on the latest version (%s)", version)
} else {
fmt.Println("New Version Available! Update? (Y/n)")
fmt.Println("Make sure you are running on an elevated shell")
update := ""
fmt.Scanln(&update)
if update == "Y" || update == "y" {
path := `C:\Program Files (x86)\Modworkshop-DL`
downloadURL := fmt.Sprintf("https://github.com/WillKirkmanM/modworkshop-dl/releases/download/v%s/modworkshop-dl.exe", apiRes)
grab.Get(path + `\modworkshop-dl-tmp.exe`, downloadURL)
os.Remove(path + `\modworkshop-dl.exe`)
os.Rename(path + `\modworkshop-dl-tmp.exe`, path + `\modworkshop-dl.exe`)
fmt.Printf("Successfully Updated to Version (%s)", apiRes.Version)
} else {
fmt.Println("Got it! Exiting...")
os.Exit(0)
}
}
}
func installSuperBLT() {
url := "https://sblt-update.znix.xyz/pd2update/download/get.php?src=homepage&id=payday2bltwsockdll"
installPath := `C:\Program Files (x86)\Steam\SteamApps\common\PAYDAY 2\`
dllName := "WSOCK32.dll"
dllPath := fmt.Sprintf("%s%s", installPath, dllName)
if _, err := os.Stat(dllPath); os.IsNotExist(err) {
writer := uilive.New()
writer.Start()
resp, err := grab.Get(installPath, url)
if err != nil {
log.Fatal(err)
}
t := time.NewTicker(500 * time.Millisecond)
defer t.Stop()
writer.Stop()
Loop:
for {
select {
case <-t.C:
fmt.Fprintf(writer, "Downloading: %v / %v bytes (%.2f%%)\n",
resp.BytesComplete(),
resp.Size(),
100*resp.Progress())
case <-resp.Done:
break Loop
}
}
unzipFile(resp.Filename, installPath)
os.Remove(resp.Filename)
fmt.Println("SuperBLT has Successfully Been installed")
} else {
fmt.Println("You Already have SuperBLT Installed!")
os.Exit(0)
}
}