Skip to content

Commit fe0b8c3

Browse files
committed
add auto-detect
1 parent 93cb4ed commit fe0b8c3

File tree

1 file changed

+42
-28
lines changed

1 file changed

+42
-28
lines changed

main.go

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,50 @@ var builddate, commit string
2727

2828
const MagicNumber = 0xa042
2929

30+
type Option struct {
31+
WorkDir string
32+
Listname string
33+
OfficialType bool
34+
OfficialMeta bool
35+
}
36+
3037
func main() {
31-
fmt.Println("kcheck v1.4")
38+
fmt.Println("kcheck v1.4.1")
3239
fmt.Printf("build: %s(%s)\n", builddate, commit)
3340
fmt.Println("--------")
34-
//Workdir,_ := os.Getwd()
35-
//Listname := Workdir + "all.list"
36-
Listname := "all.list"
37-
CusType := false
38-
OfficialType := false
39-
OfficialMeta := false
41+
Workdir, _ := os.Getwd()
42+
opt := &Option{
43+
WorkDir: Workdir,
44+
Listname: "all.list",
45+
OfficialType: false,
46+
OfficialMeta: false,
47+
}
4048

41-
if _, err := os.Stat(Listname); !os.IsNotExist(err) {
49+
CusType := false
50+
if _, err := os.Stat(opt.Listname); !os.IsNotExist(err) {
4251
CusType = true
4352
} else {
44-
Listname = "filepath.xml"
45-
if _, err := os.Stat(Listname); !os.IsNotExist(err) {
46-
OfficialType = true
53+
if _, err := os.Stat("data" + string(os.PathSeparator) + "__metadata.metatxt"); !os.IsNotExist(err) {
54+
opt.Listname = "data" + string(os.PathSeparator) + "__metadata.metatxt"
55+
opt.WorkDir = Workdir + string(os.PathSeparator) + "data" + string(os.PathSeparator)
56+
opt.OfficialMeta = true
57+
} else if _, err := os.Stat("__metadata.metatxt"); !os.IsNotExist(err) {
58+
opt.Listname = "__metadata.metatxt"
59+
opt.OfficialMeta = true
4760
} else {
48-
Listname = "__metadata.metatxt"
49-
if _, err := os.Stat(Listname); !os.IsNotExist(err) {
50-
OfficialMeta = true
61+
if _, err := os.Stat("prop" + string(os.PathSeparator) + "filepath.xml"); !os.IsNotExist(err) {
62+
opt.Listname = "prop" + string(os.PathSeparator) + "filepath.xml"
63+
opt.OfficialType = true
64+
} else if _, err := os.Stat("filepath.xml"); !os.IsNotExist(err) {
65+
opt.Listname = "filepath.xml"
66+
opt.OfficialType = true
5167
} else {
52-
OfficialType = false
68+
opt.OfficialType = false
5369
}
5470
}
5571
}
5672

57-
file, err := os.Open(Listname)
73+
file, err := os.Open(opt.Listname)
5874
if err != nil {
5975
log.Fatal("open file err: ", err)
6076
}
@@ -63,7 +79,7 @@ func main() {
6379
var failures []string
6480
var fileCount, passCount, failCount int
6581

66-
if OfficialType {
82+
if opt.OfficialType {
6783
// detect kbin
6884
isKbin := false
6985
magicNumber := make([]byte, 2)
@@ -94,7 +110,7 @@ func main() {
94110
for _, FileNode := range FilepathStruct.File {
95111
fileCount++
96112
FormatPath := strings.TrimPrefix(filepath.FromSlash(FileNode.DstPath), string(os.PathSeparator))
97-
if err := CompareFileMD5(FormatPath, FileNode.DstMD5); err != nil {
113+
if err := opt.CompareFileMD5(FormatPath, FileNode.DstMD5); err != nil {
98114
errstring := "[" + err.Error() + "] "
99115
failures = append(failures, errstring+FormatPath)
100116
failCount++
@@ -105,10 +121,10 @@ func main() {
105121
}
106122
fmt.Println(FormatPath)
107123
}
108-
} else if OfficialMeta {
124+
} else if opt.OfficialMeta {
109125
//meta
110126
//load metadata
111-
meta, err := ioutil.ReadFile(Listname)
127+
meta, err := ioutil.ReadFile(opt.Listname)
112128
if err != nil {
113129
log.Fatal(err)
114130
}
@@ -124,7 +140,7 @@ func main() {
124140

125141
for _, files := range metaStruct.Files {
126142
FormatPath := strings.TrimPrefix(filepath.FromSlash(files.Path), string(os.PathSeparator))
127-
if err := CompareFileSHA1(FormatPath, files.SHA1); err != nil {
143+
if err := opt.CompareFileSHA1(FormatPath, files.SHA1); err != nil {
128144
errstring := "[" + err.Error() + "] "
129145
failures = append(failures, errstring+FormatPath)
130146
failCount++
@@ -152,7 +168,7 @@ func main() {
152168
break
153169
}
154170
fileCount++
155-
if err := CompareFileMD5(words[1], words[0]); err != nil {
171+
if err := opt.CompareFileMD5(words[1], words[0]); err != nil {
156172
failures = append(failures, words[1])
157173
failCount++
158174
fmt.Print("[" + err.Error() + "] ")
@@ -189,9 +205,8 @@ func main() {
189205
os.Exit(0)
190206
}
191207

192-
func CompareFileMD5(relativePath, filemd5 string) error {
193-
WorkDir, _ := os.Getwd()
194-
fpath := WorkDir + string(os.PathSeparator) + relativePath
208+
func (opt *Option) CompareFileMD5(relativePath, filemd5 string) error {
209+
fpath := opt.WorkDir + string(os.PathSeparator) + relativePath
195210
if _, err := os.Stat(fpath); err != nil {
196211
return errors.New("NOT FOUND")
197212
}
@@ -211,9 +226,8 @@ func CompareFileMD5(relativePath, filemd5 string) error {
211226
return nil
212227
}
213228

214-
func CompareFileSHA1(relativePath, filesha1 string) error {
215-
WorkDir, _ := os.Getwd()
216-
fpath := WorkDir + string(os.PathSeparator) + relativePath
229+
func (opt *Option) CompareFileSHA1(relativePath, filesha1 string) error {
230+
fpath := opt.WorkDir + string(os.PathSeparator) + relativePath
217231
if _, err := os.Stat(fpath); err != nil {
218232
return errors.New("NOT FOUND")
219233
}

0 commit comments

Comments
 (0)