This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pbo.go
236 lines (183 loc) · 4.84 KB
/
pbo.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package pbo
import (
"bytes"
"crypto/sha1"
"encoding/binary"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"time"
"unsafe"
)
//HeaderEntry is a simple ArmA 3 header entry
type HeaderEntry struct {
FileName string
PackingMethod, OriginalSize, Reserved, TimeStamp, DataSize uint32
}
//ProductEntry is an entry for custom information purpose
type ProductEntry struct {
EntryName, ProductName, ProductVersion string
}
//PBO is a callable struct for creating a .pbo-file easily
type PBO struct {
Buffer *bytes.Buffer
From, To string
Files []string
Prefix string
Version string
}
//WriteProduct writes an header of struct ProductEntry into the buffer PBO.Buffer
func (pbo *PBO) WriteProduct(product ProductEntry) error {
tmpV := make([]byte, len(product.EntryName))
copy(tmpV[:], product.EntryName)
if err := binary.Write(pbo.Buffer, binary.BigEndian, tmpV); err != nil {
return err
}
if err := pbo.Buffer.WriteByte('\x00'); err != nil {
return err
}
tmpV = make([]byte, len(product.ProductName))
copy(tmpV[:], product.ProductName)
if err := binary.Write(pbo.Buffer, binary.BigEndian, tmpV); err != nil {
return err
}
if err := pbo.Buffer.WriteByte('\x00'); err != nil {
return err
}
tmpV = make([]byte, len(product.ProductVersion))
copy(tmpV[:], product.ProductVersion)
if err := binary.Write(pbo.Buffer, binary.BigEndian, tmpV); err != nil {
return err
}
err := pbo.Buffer.WriteByte('\x00')
return err
}
//WriteHeader writes a normal file header to the buffer PBO.Buffer
func (pbo *PBO) WriteHeader(header HeaderEntry) error {
tmpV := make([]byte, len(header.FileName))
copy(tmpV[:], header.FileName)
err := binary.Write(pbo.Buffer, binary.BigEndian, tmpV)
pbo.Buffer.WriteByte(byte('\x00'))
if err != nil {
return err
}
err = binary.Write(pbo.Buffer, binary.LittleEndian, (*[4]byte)(unsafe.Pointer(&header.PackingMethod)))
if err != nil {
return err
}
err = binary.Write(pbo.Buffer, binary.LittleEndian, (*[4]byte)(unsafe.Pointer(&header.OriginalSize)))
if err != nil {
return err
}
err = binary.Write(pbo.Buffer, binary.LittleEndian, (*[4]byte)(unsafe.Pointer(&header.Reserved)))
if err != nil {
return err
}
err = binary.Write(pbo.Buffer, binary.LittleEndian, (*[4]byte)(unsafe.Pointer(&header.TimeStamp)))
if err != nil {
return err
}
err = binary.Write(pbo.Buffer, binary.LittleEndian, (*[4]byte)(unsafe.Pointer(&header.DataSize)))
return err
}
//Generate generates the buffer which can be saved with PBO.Save() or PBO.SaveTo()
func (pbo *PBO) Generate() error {
pbo.Buffer.Reset()
pbo.WriteHeader(HeaderEntry{
FileName: "",
PackingMethod: 0x56657273,
OriginalSize: 0,
Reserved: 0,
TimeStamp: 0,
DataSize: 0,
})
pbo.WriteProduct(ProductEntry{
ProductName: pbo.Prefix,
ProductVersion: pbo.Version,
EntryName: "prefix",
})
files := pbo.GetFiles()
for _, f := range files {
fHandle, err := os.Open(f)
if err != nil {
return err
}
stat, err := fHandle.Stat()
if err != nil {
return err
}
size32, err := strconv.ParseUint(strconv.FormatInt(stat.Size(), 10), 10, 32)
if err != nil {
return err
}
time32, err := strconv.ParseUint(strconv.FormatInt(time.Now().Unix(), 10), 10, 32)
if err != nil {
return err
}
rP, err := filepath.Rel(pbo.From, f)
if err != nil {
return err
}
pbo.WriteHeader(HeaderEntry{
FileName: rP,
PackingMethod: 0x0,
OriginalSize: uint32(size32),
Reserved: 0,
TimeStamp: uint32(time32),
DataSize: uint32(size32),
})
}
pbo.WriteHeader(HeaderEntry{})
for _, f := range files {
f, err := os.Open(f)
defer f.Close()
if err != nil {
return err
}
_, err = io.Copy(pbo.Buffer, f)
if err != nil {
return err
}
}
checksum := pbo.GetChecksum()
pbo.Buffer.WriteByte(byte('\x00'))
pbo.Buffer.Write(checksum)
return nil
}
//Save saves the buffer PBO.Buffer to a predefined location PBO.To
func (pbo *PBO) Save() error {
return ioutil.WriteFile(pbo.To, pbo.Buffer.Bytes(), 0644)
}
//SaveTo saves the buffer PBO.Buffer to a given location to
func (pbo *PBO) SaveTo(to string) error {
return ioutil.WriteFile(to, pbo.Buffer.Bytes(), 0644)
}
//GetChecksum returns a SHA1 checksum for validation purposes
//Although, the checksum is needed at the end of each .pbo-file if you want to sign it
func (pbo *PBO) GetChecksum() []byte {
hash := sha1.New()
hash.Write(pbo.Buffer.Bytes())
return hash.Sum(nil)
}
//GetFiles gets every file in a .pbo-file directory
func (pbo *PBO) GetFiles() []string {
var files []string
filepath.Walk(
pbo.From,
func(file string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, file)
}
return nil
},
)
return files
}
//New returns a pointer to an PBO object
func New() *PBO {
return &PBO{
Buffer: &bytes.Buffer{},
}
}