-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
93 lines (80 loc) · 2 KB
/
writer.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
package dlclist
import (
"encoding/xml"
"io/ioutil"
"path"
"sort"
"github.com/maestre3d/gtavd/xmlutil"
"github.com/maestre3d/gtavd/config"
"github.com/maestre3d/gtavd/logging"
)
const (
dlcListFileName = "dlclist.xml"
)
var (
// hashSet used to avoid duplicated on-the-go
dlcItems = map[string]struct{}{
"platform:/dlcPacks/mpBeach/": {},
"platform:/dlcPacks/mpBusiness/": {},
"platform:/dlcPacks/mpChristmas/": {},
"platform:/dlcPacks/mpValentines/": {},
"platform:/dlcPacks/mpBusiness2/": {},
"platform:/dlcPacks/mpHipster/": {},
"platform:/dlcPacks/mpIndependence/": {},
"platform:/dlcPacks/mpPilot/": {},
"platform:/dlcPacks/spUpgrade/": {},
"platform:/dlcPacks/mpLTS/": {},
}
)
type paths struct {
Items []string `xml:"Item"`
}
type sMandatoryPacksData struct {
XMLName xml.Name `xml:"SMandatoryPacksData"`
Paths paths `xml:"Paths"`
}
func WriteAddons() error {
if err := loadDlcItems(config.DefaultConfig.Addon.Dir); err != nil {
return err
}
if err := loadDlcItems(config.DefaultConfig.Addon.ModDir); err != nil {
return err
}
logging.Debug().Msgf("gtavd-dlclist-watcher: found %d dlclist directories", len(dlcItems))
return writeAddonsFile()
}
func loadDlcItems(dlcPath string) error {
readDir, err := ioutil.ReadDir(dlcPath)
if err != nil {
return err
}
for _, d := range readDir {
if !d.IsDir() {
continue
}
if _, ok := config.DefaultConfig.Addon.Blacklist[d.Name()]; !ok {
key := "dlcpacks:/" + d.Name() + "/"
dlcItems[key] = struct{}{}
}
}
return nil
}
func newDlcList() sMandatoryPacksData {
items := make([]string, 0, len(dlcItems))
for i := range dlcItems {
items = append(items, i)
}
sort.Slice(items, func(i, j int) bool {
return items[j] < items[i]
})
return sMandatoryPacksData{
Paths: paths{
Items: items,
},
}
}
func writeAddonsFile() error {
data := newDlcList()
fileName := path.Join(config.DefaultConfig.DaemonPath, dlcListFileName)
return xmlutil.WriteFile(fileName, data)
}