-
Notifications
You must be signed in to change notification settings - Fork 0
/
farm.go
226 lines (180 loc) · 4.8 KB
/
farm.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
package main
import (
"archive/zip"
"crypto/sha1"
"encoding/xml"
"flag"
"fmt"
"hash/crc32"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
)
var seed = map[string]string{} // map[rom crc32]"file path"
type Datfile struct {
Header Header `xml:"header"`
Sets []Set `xml:"game"`
}
type Header struct {
Name string `xml:"name"`
Description string `xml:"description"`
Category string `xml:"category"`
Version string `xml:"version"`
Author string `xml:"author"`
Comment string `xml:"comment"`
}
type Set struct {
Name string `xml:"name,attr"`
CloneOf string `xml:"cloneof,attr"`
RomOf string `xml:"romof,attr"`
Description string `xml:"description"`
Year string `xml:"year"`
Manufacturer string `xml:"manufacturer"`
Roms []Rom `xml:"rom"`
}
type Rom struct {
Name string `xml:"name,attr"`
Merge string `xml:"merge,attr"`
Size string `xml:"size,attr"`
Crc string `xml:"crc,attr"`
Sha1 string `xml:"sha1,attr"`
}
func getFileSha1(file string) string {
dat, err := ioutil.ReadFile(file)
if err != nil {
return ""
}
h := sha1.New()
h.Write(dat)
return fmt.Sprintf("%x", h.Sum(nil))
}
func getFileCRC32(file string) string {
dat, err := ioutil.ReadFile(file)
if err != nil {
return ""
}
h := crc32.New(crc32.IEEETable)
h.Write(dat)
return fmt.Sprintf("%x", h.Sum(nil))
}
func l(line ...interface{}) {
log.Println(line...)
}
func walkSourceDir(root string) {
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println("error: ", err)
}
if !info.IsDir() {
currentCRC32 := getFileCRC32(path)
seed[currentCRC32] = path
// fmt.Print("\r", path, " ", currentCRC32)
}
return nil
})
if err != nil {
fmt.Println("error: ", err)
}
// fmt.Print("\r")
}
func zipSet(destDir string, romSet *Set, mode string) {
zipFile := destDir + string(os.PathSeparator) + romSet.Name + ".zip"
l("creating zip:", zipFile)
zipf, _ := os.Create(zipFile)
defer zipf.Close()
zipw := zip.NewWriter(zipf)
defer zipw.Close()
for _, r := range romSet.Roms {
currentRomFile := seed[r.Crc]
switch mode {
case "nonmerge":
f, _ := os.Open(currentRomFile)
defer f.Close()
w, _ := zipw.Create(r.Name)
io.Copy(w, f)
case "split":
if r.Merge == "" {
f, _ := os.Open(currentRomFile)
defer f.Close()
w, _ := zipw.Create(r.Name)
io.Copy(w, f)
}
}
}
}
func main() {
l("f.a.r.m. is starting")
datPtr := flag.String("datfile", "", "DAT file location")
sourcePtr := flag.String("source", "", "Location of the source files")
destPtr := flag.String("dest", "", "Location of the destination files")
modePtr := flag.String("mode", "nonmerge", "Desired output mode (nonmerge, split or merge")
flag.Parse()
canStart := true
if *datPtr == "" {
canStart = false
l("[ERROR] no DAT file location provided, use \"-datfile\" flag to specify what reference DAT file you want to use")
}
if *sourcePtr == "" {
canStart = false
l("[ERROR] no source directory specified, use \"-source\" flag to specify where your source files (roms) are located")
}
if *destPtr == "" {
canStart = false
l("[ERROR] no destination directory specified, use \"-dest\" flag to specify in what directory you want f.a.r.m. to write your reorganized files")
}
if *sourcePtr == *destPtr && *sourcePtr != "" {
canStart = false
l("[ERROR] source and destination directories are the same ... this can't be good :-/")
}
if *modePtr == "merge" {
canStart = false
l("[ERROR] merge collection mode is not supported, yet. Sorry about that.")
}
if !canStart {
os.Exit(1)
}
l("start reading file " + *datPtr)
dat, err := ioutil.ReadFile(*datPtr)
if err != nil {
panic(err)
}
l("done reading file ")
var datFile Datfile
l("start marshalling XML")
xml.Unmarshal(dat, &datFile)
l("done marshalling XML")
l("[name] ", datFile.Header.Name)
l("[description]", datFile.Header.Description)
l("[category] ", datFile.Header.Category)
l("[version] ", datFile.Header.Version)
l("[author] ", datFile.Header.Author)
l("[comment] ", datFile.Header.Comment)
l("start parsing sets and roms")
rom := map[string]Rom{} // map[rom crc32]{rom details}
for _, s := range datFile.Sets {
for _, r := range s.Roms {
if r.Merge == "" {
rom[r.Crc] = r
}
}
}
l("done parsing sets and roms")
l("found", len(datFile.Sets), "sets and", len(rom), "roms")
l("parsing source directory", *sourcePtr)
walkSourceDir(*sourcePtr)
l("identified", len(seed), "unique roms in directory", *sourcePtr)
// Let's see what we have...
for _, s := range datFile.Sets {
complete := len(s.Roms) > 0
for _, r := range s.Roms {
_, gotIt := seed[r.Crc]
complete = complete && gotIt
}
if complete {
l("Set", s.Name, "is complete")
zipSet(*destPtr, &s, *modePtr)
}
}
}