-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
142 lines (113 loc) · 2.94 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
type banDataType struct {
UserName string `json:"username"`
Reason string `json:"reason,omitempty"`
}
func main() {
var composite []banDataType
exe, _ := os.Executable()
//Help
if len(os.Args) <= 1 {
fmt.Println("Usage: " + filepath.Base(exe) + " <file1> <file2> ...")
fmt.Print("Output file: composite.json\n")
os.Exit(1)
}
//Get list of files
filesToProcess := os.Args[1:]
//Loop through files
for _, file := range filesToProcess {
file, err := os.Open(file)
if err != nil {
log.Println(file, err)
return
}
defer file.Close()
var bData []banDataType
data, err := ioutil.ReadAll(file)
if err != nil {
log.Println(err)
os.Exit(1)
}
/* This area deals with 'array of strings' format */
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 _, name := range names {
if name != "" {
bData = append(bData, banDataType{UserName: strings.ToLower(name)})
}
}
}
/* This area deals with standard format bans */
var bans []banDataType
_ = json.Unmarshal(data, &bans)
for _, item := range bans {
if item.UserName != "" {
bData = append(bData, banDataType{UserName: strings.ToLower(item.UserName), Reason: item.Reason})
}
}
log.Println("Read " + fmt.Sprintf("%v", len(bData)) + " bans from banlist.")
/* Combine all the data, dealing with duplicates */
dupes := 0
diffDupes := 0
for apos, aBan := range bData {
found := false
dupReason := ""
for bpos, bBan := range bData {
if strings.EqualFold(aBan.UserName, bBan.UserName) && apos != bpos {
found = true
dupReason = bBan.Reason
dupes++
break
}
}
if !found {
composite = append(composite, aBan)
} else {
if !strings.EqualFold(aBan.Reason, dupReason) && !strings.HasPrefix(dupReason, "[dup]") {
if aBan.Reason != "" && dupReason != "" {
bData[apos].Reason = "[dup] " + aBan.Reason + ", " + dupReason
} else {
bData[apos].Reason = "[dup] " + aBan.Reason + dupReason
}
diffDupes++
composite = append(composite, bData[apos])
}
}
}
log.Printf("Removed %v duplicates from banlist, %v dupes had multiple reasons (reasons combined)\n", dupes, diffDupes)
}
/* Write out composite banlist */
file, err := os.Create("composite.json")
if err != nil {
log.Println(err)
os.Exit(1)
}
outbuf := new(bytes.Buffer)
enc := json.NewEncoder(outbuf)
enc.SetIndent("", "\t")
err = enc.Encode(composite)
if err != nil {
log.Println("Error encoding ban list file: " + err.Error())
os.Exit(1)
}
wrote, err := file.Write(outbuf.Bytes())
if err != nil {
log.Println(err)
os.Exit(1)
}
log.Printf("Wrote banlist (%v) of %v bytes.\n", len(composite), wrote)
}