-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-read.go
182 lines (145 loc) · 4.15 KB
/
json-read.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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"strings"
"time"
)
/* Lots of dupe code, but I'd rather have the duplicate code than a single overly complex function */
func readBanCache() {
for spos, server := range serverList.ServerList {
if strings.EqualFold(server.CommunityName, serverConfig.CommunityName) {
continue
}
serverList.ServerList[spos].LocalData.BanList = readBanCacheFile(server.CommunityName)
}
}
func readBanCacheFile(serverName string) []banDataType {
bandata := []banDataType{}
serverName = FileNameFilter(serverName)
path := serverConfig.PathData.BanCacheDir + "/" + serverName + ".json"
file, err := os.ReadFile(path)
if file != nil && err == nil {
err = json.Unmarshal(file, &bandata)
if err != nil {
log.Println("Error reading ban cache file: " + err.Error())
}
}
return bandata
}
// Read list of servers from file
func readServerListFile() {
file, err := os.ReadFile(serverConfig.PathData.ServerListFile)
//Read server list file if it exists
if file != nil && !os.IsNotExist(err) {
var temp serverListData
err = json.Unmarshal(file, &temp)
if err != nil {
log.Println("Error reading server list file: " + err.Error())
os.Exit(1)
}
if temp.Version == "0.0.1" {
serverList = temp
} else {
log.Print("Server list file version is not supported, exiting.")
time.Sleep(time.Minute)
os.Exit(1)
}
} else {
//Generate empty list
serverList = serverListData{Version: "0.0.1", ServerList: []serverData{}}
log.Println("No server list file found, creating new one.")
writeServerListFile()
}
}
// Read server config from file
func readConfigFile() {
//Read server config file
file, err := os.ReadFile(configPath)
if file != nil && err == nil {
var temp serverConfigData
err = json.Unmarshal(file, &temp)
if err != nil {
log.Println("Error reading config file: " + err.Error())
os.Exit(1)
}
//Let user know further config is required
if strings.EqualFold(serverConfig.CommunityName, "Default") {
log.Println("Please change ServerName in the config file, or use --runWizard")
os.Exit(1)
}
if temp.Version == "0.0.2" {
serverConfig = temp
} else {
log.Print("Config file version is not supported, exiting.")
time.Sleep(time.Minute)
os.Exit(1)
}
} else {
//Make example config file, with reasonable defaults
log.Println("No config file found, generating defaults and saving to " + configPath)
os.Mkdir(defaultDataDir, 0755)
makeDefaultConfigFile()
fmt.Println("Would you like to use the setup wizard? (Y/n)")
var input string
fmt.Scanln(&input)
if input == "y" || input == "Y" || input == "" {
setupWizard()
return
} else {
log.Println("Please edit the config file, or use --runWizard")
}
log.Println("Exiting...")
os.Exit(1)
}
}
// Read the Factorio ban list file locally
func readServerBanList() {
if serverConfig.PathData.FactorioBanFile == "" {
return
}
file, err := os.Open(serverConfig.PathData.FactorioBanFile)
if err != nil {
log.Println(err)
return
}
var bData []banDataType
data, err := io.ReadAll(file)
if err != nil {
log.Println(err)
os.Exit(1)
}
var names []string
err = json.Unmarshal(data, &names)
if err != nil {
//Not really an error, just empty array
//Only needed because Factorio will write some bans as an array for some unknown reason.
} else {
for npos, name := range names {
if name != "" {
bData = append(bData, banDataType{UserName: strings.ToLower(name), Added: time.Now().Format(timeFormat)})
names[npos] = strings.ToLower(name)
}
}
}
var bans []banDataType
err = json.Unmarshal(data, &bans)
if err != nil {
fmt.Print("") //Annoying warning remover
}
for ipos, item := range bans {
if item.UserName != "" && !strings.HasPrefix(strings.ToLower(item.Reason), strings.ToLower("[FCL]")) {
bData = append(bData, banDataType{UserName: strings.ToLower(item.UserName), Reason: item.Reason, Added: time.Now().Format(timeFormat)})
bans[ipos].UserName = strings.ToLower(item.UserName)
}
}
ourBanData = bData
if serverConfig.ServerPrefs.VerboseLogging {
log.Printf("Read %v bans from banlist.\n", len(bData))
}
compositeBans()
updateWebCache()
}