-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (119 loc) · 3.01 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bufio"
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
"time"
"gitgud.io/softashell/rpgmaker-patch-translator/block"
"gitgud.io/softashell/rpgmaker-patch-translator/engine"
"gitgud.io/softashell/rpgmaker-patch-translator/translate"
log "github.com/sirupsen/logrus"
)
var (
// Flags
lineLength int
lineTolerance int
cFileThreads int
cBlockThreads int
)
func main() {
start := time.Now()
args := parseFlags()
if len(args) < 1 {
log.Fatal("Program requires patch directory as argument")
}
dir := args[0]
err := checkPatchVersion(dir)
if err != nil {
log.Fatal(err)
}
fileList := getDirectoryContents(filepath.Join(dir, "Patch"))
if len(fileList) < 1 {
log.Fatal("Couldn't find anything to translate")
}
if lineLength == -1 {
if engine.Is(engine.Wolf) {
lineLength = 54
} else {
lineLength = 42
}
}
fmt.Println("Current settings:")
fmt.Println("- line length:", lineLength)
fmt.Println("- line length tolerance:", lineTolerance)
fileCount := len(fileList)
fmt.Printf("Found %d files to translate\n", fileCount)
translate.Init()
block.Init()
jobs, results := createFileWorkers(fileCount)
go func() {
for _, file := range fileList {
jobs <- file
}
close(jobs)
}()
for err := range results {
if err != nil {
log.Error(err)
}
}
fmt.Printf("Finished in %s\n", time.Since(start))
}
func checkPatchVersion(dir string) error {
file, err := os.Open(filepath.Join(dir, "RPGMKTRANSPATCH"))
if err != nil {
file, err = os.Open(filepath.Join(dir, "Patch", "dump", "GameDat.txt"))
if err != nil {
return fmt.Errorf("Unable to open RPGMKTRANSPATCH or Patch/dump/GameDat.txt")
}
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := scanner.Text()
if text == "> RPGMAKER TRANS PATCH V3" {
fmt.Println("Detected RPG Maker VX Ace Patch")
engine.Set(engine.RPGMVX)
return nil
} else if text == "> WOLF TRANS PATCH FILE VERSION 1.0" {
fmt.Println("Detected WOLF RPG Patch")
engine.Set(engine.Wolf)
return nil
}
return fmt.Errorf("Unsupported patch version")
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return err
}
func getDirectoryContents(dir string) []string {
var fileList []string
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if f.IsDir() || filepath.Ext(path) != ".txt" {
return nil
}
fileList = append(fileList, path)
return nil
})
if err != nil {
log.Fatal(err)
}
return fileList
}
func check(err error) {
if err != nil {
panic(err)
}
}
func parseFlags() []string {
flag.IntVar(&lineLength, "length", -1, "Max line legth")
flag.IntVar(&lineTolerance, "tolerance", 5, "Max amount of characters allowed to go over the line limit")
flag.IntVar(&cFileThreads, "filethreads", runtime.NumCPU()/2+1, "Amount of threads to use for processing files")
flag.IntVar(&cBlockThreads, "blockthreads", runtime.NumCPU()*2+1, "Amount of threads to use for processing blocks in each file")
flag.Parse()
return flag.Args()
}