-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheat-parser.go
100 lines (89 loc) · 2.56 KB
/
cheat-parser.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
package main
import (
"bufio"
"encoding/json"
"log"
"os"
"path/filepath"
"strings"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/storage/memory"
)
type Cheat struct {
Note string `json:"note"`
Data []string `json:"data"`
Options map[string]string `json:"options"`
HasOptions bool `json:"hasOptions"`
}
func main() {
fs := memfs.New()
if _, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{
URL: "https://github.com/project64/project64.git",
Depth: 1,
}); err != nil {
log.Panic(err)
}
basePath := "Config/Cheats"
items, err := fs.ReadDir(basePath)
if err != nil {
log.Panic(err)
}
cheatDB := map[string]map[string]Cheat{}
currentGame := ""
currentCheat := ""
for _, item := range items {
file, err := fs.Open(filepath.Join(basePath, item.Name()))
if err != nil {
log.Panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
game := cheatDB[currentGame]
cheat := game[currentCheat]
if strings.HasPrefix(scanner.Text(), "[") {
currentGame = strings.Trim(scanner.Text(), "[]")
cheatDB[currentGame] = map[string]Cheat{}
} else if strings.HasPrefix(scanner.Text(), "Name=") {
// do nothing
} else if strings.HasPrefix(scanner.Text(), "$") {
currentCheat = strings.TrimPrefix(scanner.Text(), "$")
game[currentCheat] = Cheat{}
cheatDB[currentGame] = game
} else if strings.HasPrefix(scanner.Text(), "Note=") {
cheat.Note = strings.TrimPrefix(scanner.Text(), "Note=")
game[currentCheat] = cheat
} else if scanner.Text() == "" {
// do nothing
} else if strings.Contains(scanner.Text(), "?") && !cheat.HasOptions {
cheat.HasOptions = true
cheat.Options = map[string]string{}
cheat.Data = append(cheat.Data, scanner.Text())
game[currentCheat] = cheat
} else if cheat.HasOptions && len(strings.Split(scanner.Text(), " ")[0]) < 8 {
cheat.Options[strings.Join(strings.Split(scanner.Text(), " ")[1:], " ")] = strings.Split(scanner.Text(), " ")[0]
game[currentCheat] = cheat
} else {
cheat.Data = append(cheat.Data, scanner.Text())
game[currentCheat] = cheat
}
}
if err := scanner.Err(); err != nil {
log.Panic(err)
}
}
b, err := json.Marshal(cheatDB)
if err != nil {
log.Panic(err)
}
f, err := os.OpenFile("cheats.json", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644) //nolint:gomnd,mnd
if err != nil {
log.Panic(err)
}
w := bufio.NewWriter(f)
if _, err := w.Write(b); err != nil {
log.Panic(err)
}
w.Flush()
}