-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwad.go
158 lines (126 loc) · 2.78 KB
/
wad.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
package main
import (
"bytes"
"encoding/binary"
"os"
"path/filepath"
)
var le = binary.LittleEndian
type Lump struct {
Name string
Data []byte
HFlip bool
}
type WAD struct {
Name string
AllData []byte
Lumps []Lump
LumpByName map[string]uint32
}
func (wad *WAD) Load(wadPath string) (err error) {
var b []byte
b, err = os.ReadFile(wadPath)
if err != nil {
return
}
numEntries := le.Uint32(b[4:8])
directoryOffs := le.Uint32(b[8:12])
wad.Name = filepath.Base(wadPath)
wad.AllData = b
wad.Lumps = make([]Lump, numEntries)
wad.LumpByName = make(map[string]uint32, numEntries)
for i, o := uint32(0), directoryOffs; i < numEntries; i, o = i+1, o+16 {
offs := le.Uint32(b[o : o+4])
size := le.Uint32(b[o+4 : o+8])
name := b[o+8 : o+16]
nameStr := string(bytes.TrimRight(name[:], "\x00"))
wad.Lumps[i].Name = nameStr
wad.Lumps[i].Data = b[offs : offs+size]
wad.LumpByName[nameStr] = i
}
return
}
func (wad *WAD) FindLumpIndex(name string) uint32 {
return wad.LumpByName[name]
}
func (wad *WAD) EndIndex() uint32 {
return uint32(len(wad.Lumps) - 1)
}
func (wad *WAD) FindLumpBetween(start uint32, end uint32, matches func(string) bool) *Lump {
absEnd := wad.EndIndex()
if end > absEnd {
end = absEnd
}
for i := start; i <= end; i++ {
if matches(wad.Lumps[i].Name) {
return &wad.Lumps[i]
}
}
return nil
}
func (wad *WAD) IterateLumpsBetween(start uint32, end uint32, iter func(*Lump) bool) bool {
absEnd := wad.EndIndex()
if end > absEnd {
end = absEnd
}
for i := start; i <= end; i++ {
if iter(&wad.Lumps[i]) {
// break:
return true
}
}
return false
}
type WADCollection struct {
Ordered []*WAD
}
func (wc *WADCollection) Load(path string) (err error) {
wad := &WAD{}
err = wad.Load(path)
if err != nil {
return
}
// prepend to list:
wc.Ordered = append([]*WAD{wad}, wc.Ordered...)
return
}
func (wc *WADCollection) FindLumpBetween(start string, end string, matches func(string) bool) (lump *Lump) {
for _, wad := range wc.Ordered {
var startIndex uint32
if start == "" {
startIndex = 0
} else {
startIndex = wad.FindLumpIndex(start)
}
var endIndex uint32
if end == "" {
endIndex = wad.EndIndex()
} else {
endIndex = wad.FindLumpIndex(end)
}
lump = wad.FindLumpBetween(startIndex, endIndex, matches)
if lump != nil {
return
}
}
return nil
}
func (wc *WADCollection) IterateLumpsBetween(start string, end string, iter func(*Lump) bool) {
for _, wad := range wc.Ordered {
var startIndex uint32
if start == "" {
startIndex = 0
} else {
startIndex = wad.FindLumpIndex(start)
}
var endIndex uint32
if end == "" {
endIndex = wad.EndIndex()
} else {
endIndex = wad.FindLumpIndex(end)
}
if wad.IterateLumpsBetween(startIndex, endIndex, iter) {
break
}
}
}