-
Notifications
You must be signed in to change notification settings - Fork 17
/
AmlResImg.go
119 lines (94 loc) · 2.32 KB
/
AmlResImg.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
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
"github.com/hzyitc/AmlImg/AmlResImg"
)
func res_unpack(filePath, extractPath string) error {
img, err := AmlResImg.NewReader(filePath, true)
if err != nil {
return errors.New("NewReader error: " + err.Error())
}
defer img.Close()
listfile, err := os.Create(extractPath + "/list.txt")
if err != nil {
return errors.New("Create error: " + err.Error())
}
defer listfile.Close()
for i := 0; i < int(img.Header.ItemCount); i++ {
item := img.Items[i]
filename := fmt.Sprintf("%d.%s", item.Index, item.Name)
if item.Type == 0x090000 {
filename += ".bmp"
}
println("Extracting ", extractPath+"/"+filename)
fmt.Fprintf(listfile, "%08X:%s:%s\n", item.Type, item.Name, filename)
file, err := os.Create(extractPath + "/" + filename)
if err != nil {
return errors.New("Create error:" + err.Error())
}
err = img.Seek(uint32(i), 0)
if err != nil {
file.Close()
return errors.New("Seek error:" + err.Error())
}
_, err = io.Copy(file, img)
if err != nil {
file.Close()
return errors.New("Copy error:" + err.Error())
}
file.Close()
}
return nil
}
func res_pack(filePath, dirPath string) error {
img, err := AmlResImg.NewWriter()
if err != nil {
return errors.New("NewWriter error: " + err.Error())
}
listfile, err := os.Open(dirPath + "/list.txt")
if err != nil {
return errors.New("Open error: " + err.Error())
}
defer listfile.Close()
scanner := bufio.NewScanner(listfile)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
txt := scanner.Text()
if txt == "" {
continue
} else if strings.HasPrefix(txt, "#") {
continue
}
c := strings.SplitN(txt, ":", 3)
Name := c[1]
filename := c[2]
Type, err := strconv.ParseInt(c[0], 16, 24)
if err != nil {
return errors.New("ParseInt error: " + err.Error())
}
img.Add(uint32(Type), Name, func(w io.Writer) error {
println("Packing ", filename)
file, err := os.Open(dirPath + "/" + filename)
if err != nil {
return errors.New("Open error: " + err.Error())
}
_, err = io.Copy(w, file)
return err
})
}
err = scanner.Err()
if err != nil {
return errors.New("scanner error: " + err.Error())
}
err = img.Write(filePath, 2)
if err != nil {
return errors.New("Write error: " + err.Error())
}
return nil
}