-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.go
89 lines (73 loc) · 1.71 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
// Author: Niels A.D.
// Project: gowarcraft3 (https://github.com/nielsAD/gowarcraft3)
// License: Mozilla Public License, v2.0
// w3mdump is a tool that decodes and dumps w3m/w3x files.
package main
import (
"encoding/json"
"flag"
"fmt"
"image/png"
"log"
"os"
"strings"
"github.com/nielsAD/gowarcraft3/file/fs"
"github.com/nielsAD/gowarcraft3/file/fs/dir"
"github.com/nielsAD/gowarcraft3/file/w3m"
)
var (
binpath = flag.String("b", dir.InstallDir(), "Path to game binaries")
preview = flag.String("preview", "", "Dump preview image to this file")
jsonout = flag.Bool("json", false, "Print machine readable format")
)
var logOut = log.New(os.Stdout, "", 0)
var logErr = log.New(os.Stderr, "", 0)
func main() {
flag.Parse()
var filename = strings.Join(flag.Args(), " ")
m, err := w3m.Open(filename)
if err != nil {
logErr.Fatal("Open error: ", err)
}
info, err := m.Info()
if err != nil {
logErr.Fatal("Info error: ", err)
}
stor := fs.Open(*binpath)
defer stor.Close()
hash, err := m.Checksum(stor)
if err != nil {
logErr.Fatal("Checksum error: ", err)
}
var print = struct {
Info w3m.Info
Checksum w3m.Hash
}{
*info,
*hash,
}
var str = fmt.Sprintf("%+v", print)
if *jsonout {
if json, err := json.MarshalIndent(print, "", " "); err == nil {
str = string(json)
}
}
logOut.Println(str)
if *preview != "" {
img, err := m.Preview()
if err == os.ErrNotExist {
img, err = m.MenuMinimap()
}
if err != nil {
logErr.Fatal("Preview error: ", err)
}
out, err := os.Create(*preview)
if err != nil {
logErr.Fatal("os.Create error: ", err)
}
defer out.Close()
if err := png.Encode(out, img); err != nil {
logErr.Fatal("png.Encode error: ", err)
}
}
}