forked from Galaco/bsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lump.go
74 lines (62 loc) · 1.71 KB
/
lump.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
package bsp
import (
"fmt"
"github.com/galaco/bsp/internal/versions"
"github.com/galaco/bsp/lumps"
)
// Lump is a container for a lump. Also includes metadata about the lump.
// N.B. Some information mirrors the header's lump descriptor, but header information should not be trusted after
// import completion.
type Lump struct {
raw []byte
data lumps.ILump
length int32
id LumpId
loaded bool
}
// SetId sets lump identifier
// Id is the lump type id (not the id for the order the lumps are stored)
func (l *Lump) SetId(index LumpId) {
l.id = index
}
// Contents Get the contents of a lump.
// NOTE: Will need to be cast to the relevant lumps
func (l *Lump) Contents() lumps.ILump {
if !l.loaded {
if l.data.Unmarshall(l.raw) != nil {
return nil
}
l.loaded = true
}
return l.data
}
// SetContents Set content type of a lump.
func (l *Lump) SetContents(data lumps.ILump) {
l.data = data
l.loaded = false
}
// RawContents Get the raw []byte contents of a lump.
// N.B. This is the raw imported value. To get the raw value of a modified lump, use Contents().Marshall()
func (l *Lump) RawContents() []byte {
return l.raw
}
// SetRawContents Set raw []byte contents of a lump.
func (l *Lump) SetRawContents(raw []byte) {
l.raw = raw
}
// Length Get length of a lump in bytes.
func (l *Lump) Length() int32 {
return l.length
}
// getReferenceLumpByIndex Return an instance of a Lump for a given offset.
func getReferenceLumpByIndex(index int, version int32) (lumps.ILump, error) {
if index < 0 || index > 63 {
return nil, fmt.Errorf("invalid lump id: %d provided", index)
}
l, err := versions.GetLumpForVersion(int(version), index)
if err != nil {
return nil, err
}
l.SetVersion(version)
return l, nil
}