forked from Galaco/bsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
153 lines (136 loc) · 3.48 KB
/
writer.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
package bsp
import (
"bytes"
"encoding/binary"
"github.com/galaco/bsp/lumps"
)
// Writer is a Bsp export writer.
type Writer struct {
data Bsp
}
// GetBsp Gets bsp file to write.
func (w *Writer) GetBsp() Bsp {
return w.data
}
// SetBsp Sets bsp file to write.
func (w *Writer) SetBsp(file Bsp) {
w.data = file
}
// Write bsp to []byte.
func (w *Writer) Write() ([]byte, error) {
// First we need to update the header to reflect any lump changes
// At the same time we can dump our lumps as bytes to write later
lumpBytes := make([][]byte, 64)
currentOffset := 1036 // Header always 1036bytes
for _, index := range getDefaultLumpOrdering() {
// We have to handle lump 35 (GameData differently)
// Because valve mis-designed the file format and relatively positioned data contains absolute file offsets.
if index == LumpGame {
gamelump := w.data.lumps[int(index)].Contents().(*lumps.Game)
w.data.lumps[int(index)].SetContents(
gamelump.UpdateInternalOffsets(int32(currentOffset) - w.data.header.Lumps[int(index)].Offset))
}
exportBytes, err := w.WriteLump(index)
if err != nil {
return nil, err
}
lumpBytes[int(index)] = exportBytes
lumpSize := len(lumpBytes[int(index)])
w.data.header.Lumps[int(index)].Length = int32(lumpSize)
w.data.header.Lumps[int(index)].Offset = int32(currentOffset)
currentOffset += lumpSize
// Finally 4byte align each lump.
lumpBytes[int(index)] = append(lumpBytes[int(index)], make([]byte, currentOffset%4)...)
currentOffset += currentOffset % 4
}
// Now we can export our bsp
var buf bytes.Buffer
//Write Header
err := binary.Write(&buf, binary.LittleEndian, w.data.header)
if err != nil {
return nil, err
}
//Write lumps
for _, lumpData := range lumpBytes {
if err = binary.Write(&buf, binary.LittleEndian, lumpData); err != nil {
return nil, err
}
}
return buf.Bytes(), nil
}
// WriteLump Exports a single lump to []byte.
func (w *Writer) WriteLump(index LumpId) ([]byte, error) {
lump := w.data.Lump(index)
return lump.Marshall()
}
// NewWriter Returns a new bsp writer instance.
func NewWriter() Writer {
w := Writer{}
return w
}
// getDefaultLumpOrdering gets Source Engines default export order.
// Source compile tools write lumps out of order
// While the ordering doesn't actually matter, it may
// be useful/more performant to maintain the same order, particularly post-export
func getDefaultLumpOrdering() [64]LumpId {
return [64]LumpId{
LumpPlanes,
LumpLeafs,
LumpLeafAmbientLighting,
LumpLeafAmbientIndex,
LumpLeafAmbientIndexHDR,
LumpLeafAmbientLightingHDR,
LumpVertexes,
LumpNodes,
LumpTexInfo,
LumpTexData,
LumpDispInfo,
LumpDispVerts,
LumpDispTris,
LumpDispLightmapSamplePositions,
LumpFaceMacroTextureInfo,
LumpPrimitives,
LumpPrimVerts,
LumpPrimIndices,
LumpFaces,
LumpFacesHDR,
LumpFaceIds,
LumpOriginalFaces,
LumpBrushes,
LumpBrushSides,
LumpLeafFaces,
LumpLeafBrushes,
LumpSurfEdges,
LumpEdges,
LumpModels,
LumpAreas,
LumpAreaPortals,
LumpLighting,
LumpLightingHDR,
LumpVisibility,
LumpEntities,
LumpWorldLights,
LumpWorldLightsHDR,
LumpLeafWaterData,
LumpOcclusion,
LumpMapFlags,
LumpPortals,
LumpClusters,
LumpPortalVerts,
LumpClusterPortals,
LumpClipPortalVerts,
LumpCubemaps,
LumpTexDataStringData,
LumpTexDataStringTable,
LumpOverlays,
LumpWaterOverlays,
LumpOverlayFades,
LumpPhysCollide,
LumpPhysDisp,
LumpVertNormals,
LumpVertNormalIndices,
LumpLeafMinDistToWater,
LumpGame,
LumpPakfile,
}
}