-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
94 lines (77 loc) · 1.7 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
// Author: Niels A.D.
// Project: gowarcraft3 (https://github.com/nielsAD/gowarcraft3)
// License: Mozilla Public License, v2.0
// w3gsdump is a tool that decodes and dumps w3m/w3x files.
package main
import (
"encoding/json"
"flag"
"fmt"
"image/png"
"io"
"log"
"os"
"strings"
"github.com/nielsAD/gowarcraft3/file/w3m"
)
var (
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(filename, " ", err)
}
info, err := m.Info()
if err != nil {
logErr.Fatal(filename, " ", err)
}
var defaultFiles = make(map[string]io.Reader)
if f, err := os.Open("common.j"); err == nil {
defaultFiles["scripts\\common.j"] = f
defer f.Close()
}
if f, err := os.Open("blizzard.j"); err == nil {
defaultFiles["scripts\\blizzard.j"] = f
defer f.Close()
}
hash, err := m.Checksum(defaultFiles)
if err != nil {
logErr.Fatal(filename, " ", 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.Minimap()
}
if err != nil {
logErr.Fatal(err)
}
out, err := os.Create(*preview)
if err != nil {
logErr.Fatal(err)
}
if err := png.Encode(out, img); err != nil {
logErr.Fatal(err)
}
}
}