-
Notifications
You must be signed in to change notification settings - Fork 0
/
mods.go
81 lines (75 loc) · 1.83 KB
/
mods.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
)
var clusterRegexp = regexp.MustCompile(`\[\s*"workshop-(\d+)"\s*]\s*=\s*{`)
var serverRegexp = regexp.MustCompile(`[^(--)] *ServerModSetup *\(\s*"(\d+)"\s*\)`)
func getClusterMods(clusterDir, shard string, mods map[string]struct{}) error {
f, err := os.Open(filepath.Join(clusterDir, shard, "modoverrides.lua"))
if err != nil {
return err
}
defer f.Close()
buf, err := ioutil.ReadAll(f)
if err != nil {
return err
}
for _, match := range clusterRegexp.FindAllStringSubmatch(string(buf), -1) {
mods[match[1]] = struct{}{}
}
return nil
}
func getServerMods(setupPath string) ([]string, bool, error) {
f, err := os.Open(setupPath)
if err != nil {
return nil, false, err
}
defer f.Close()
buf, err := ioutil.ReadAll(f)
if err != nil {
return nil, false, err
}
matches := serverRegexp.FindAllStringSubmatch(string(buf), -1)
serverMods := make([]string, len(matches))
for i := range serverMods {
serverMods[i] = matches[i][1]
}
var noEOF bool
if n := len(buf); n >= 1 && buf[n-1] != '\n' {
noEOF = true
}
return serverMods, noEOF, nil
}
func appendServerMods(serverPath string, mods map[string]struct{}) error {
setupPath := filepath.Join(filepath.Dir(serverPath), "../mods/dedicated_server_mods_setup.lua")
serverMods, noEOF, err := getServerMods(setupPath)
if err != nil && !os.IsNotExist(err) {
return err
}
for _, mod := range serverMods {
delete(mods, mod)
}
if len(mods) == 0 {
return nil
}
f, err := os.OpenFile(setupPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
if noEOF {
if _, err := fmt.Fprintf(f, "\n"); err != nil {
return err
}
}
for mod := range mods {
if _, err := fmt.Fprintf(f, "ServerModSetup(\"%s\")\n", mod); err != nil {
return err
}
}
return nil
}