Compare commits
2 Commits
2af5bd222e
...
55981730da
| Author | SHA1 | Date | |
|---|---|---|---|
| 55981730da | |||
| bb9a47cd6d |
@@ -1,3 +1,73 @@
|
|||||||
|
// Parsen der Lump aus wad.go zu go structs
|
||||||
package mapfmt
|
package mapfmt
|
||||||
|
|
||||||
//init
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Vertex struct {
|
||||||
|
X int16
|
||||||
|
Y int16
|
||||||
|
}
|
||||||
|
|
||||||
|
type Linedef struct {
|
||||||
|
V1, V2 uint16
|
||||||
|
Flags uint16
|
||||||
|
Special uint16
|
||||||
|
Tag uint16
|
||||||
|
RightSide int16
|
||||||
|
LeftSide int16
|
||||||
|
}
|
||||||
|
|
||||||
|
type MapData struct {
|
||||||
|
Vertices []Vertex
|
||||||
|
Linedefs []Linedef
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parsen der Vertices Lumps
|
||||||
|
func ParseVertices(data []byte) ([]Vertex, error) {
|
||||||
|
if len(data)%4 != 0 {
|
||||||
|
return nil, fmt.Errorf("VERTEXES size %d not multiple of 4", len(data))
|
||||||
|
}
|
||||||
|
n := len(data) / 4
|
||||||
|
verts := make([]Vertex, n)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
verts[i].X = int16(binary.LittleEndian.Uint16(data[i*4:]))
|
||||||
|
verts[i].Y = int16(binary.LittleEndian.Uint16(data[i*4+2:]))
|
||||||
|
}
|
||||||
|
return verts, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parsen der Linedef Lumps
|
||||||
|
func ParseLinedefs(data []byte) ([]Linedef, error) {
|
||||||
|
if len(data)%14 != 0 {
|
||||||
|
return nil, fmt.Errorf("LINEDEFS size %d not multiple of 14", len(data))
|
||||||
|
}
|
||||||
|
n := len(data) / 14
|
||||||
|
lines := make([]Linedef, n)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
base := i * 14
|
||||||
|
lines[i].V1 = binary.LittleEndian.Uint16(data[base:])
|
||||||
|
lines[i].V2 = binary.LittleEndian.Uint16(data[base+2:])
|
||||||
|
lines[i].Flags = binary.LittleEndian.Uint16(data[base+4:])
|
||||||
|
lines[i].Special = binary.LittleEndian.Uint16(data[base+6:])
|
||||||
|
lines[i].Tag = binary.LittleEndian.Uint16(data[base+8:])
|
||||||
|
lines[i].RightSide = int16(binary.LittleEndian.Uint16(data[base+10:]))
|
||||||
|
lines[i].LeftSide = int16(binary.LittleEndian.Uint16(data[base+12:]))
|
||||||
|
}
|
||||||
|
return lines, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generieren der MapData aus den von LoadMapLumps geliferten Daten und Parsen per funcs
|
||||||
|
func LoadMap(raw map[string][]byte) (*MapData, error) {
|
||||||
|
v, err := ParseVertices(raw["VERTEXES"])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
l, err := ParseLinedefs(raw["LINEDEFS"])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &MapData{Vertices: v, Linedefs: l}, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
// Extrahieren der "Rohen" Lump-Bytes einer WAD-Datei
|
||||||
|
// siehe def Lumps: https://doomwiki.org/wiki/Lump
|
||||||
package wad
|
package wad
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -31,6 +33,7 @@ type Wad struct {
|
|||||||
path string
|
path string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Öffnen der WAD und checks
|
||||||
func Open(path string) (*Wad, error) {
|
func Open(path string) (*Wad, error) {
|
||||||
f, err := os.Open(path)
|
f, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -90,6 +93,7 @@ func Open(path string) (*Wad, error) {
|
|||||||
return w, nil
|
return w, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WAD Closing
|
||||||
func (w *Wad) Close() error {
|
func (w *Wad) Close() error {
|
||||||
if w == nil || w.f == nil {
|
if w == nil || w.f == nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -101,6 +105,7 @@ func (w *Wad) Close() error {
|
|||||||
|
|
||||||
func (w *Wad) Dir() []DirEntry { return w.dir }
|
func (w *Wad) Dir() []DirEntry { return w.dir }
|
||||||
|
|
||||||
|
// Lessen der Lump roh Bytes
|
||||||
func (w *Wad) ReadLump(i int) (name string, data []byte, err error) {
|
func (w *Wad) ReadLump(i int) (name string, data []byte, err error) {
|
||||||
if i < 0 || i >= len(w.dir) {
|
if i < 0 || i >= len(w.dir) {
|
||||||
return "", nil, fmt.Errorf("lump index out of range: %d", i)
|
return "", nil, fmt.Errorf("lump index out of range: %d", i)
|
||||||
@@ -118,6 +123,7 @@ func (w *Wad) ReadLump(i int) (name string, data []byte, err error) {
|
|||||||
return name, buf, nil
|
return name, buf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Extrahieren der Lump Namen aus den rohen Bytes
|
||||||
func (w *Wad) ReadLumpByName(name string) ([]byte, int, error) {
|
func (w *Wad) ReadLumpByName(name string) ([]byte, int, error) {
|
||||||
want := strings.ToUpper(name)
|
want := strings.ToUpper(name)
|
||||||
for i, d := range w.dir {
|
for i, d := range w.dir {
|
||||||
@@ -129,6 +135,7 @@ func (w *Wad) ReadLumpByName(name string) ([]byte, int, error) {
|
|||||||
return nil, -1, fmt.Errorf("lump %q not found", want)
|
return nil, -1, fmt.Errorf("lump %q not found", want)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Finden der Map( start und ende) in der WAD
|
||||||
func (w *Wad) FindMap(marker string) (start, end int, err error) {
|
func (w *Wad) FindMap(marker string) (start, end int, err error) {
|
||||||
m := strings.ToUpper(marker)
|
m := strings.ToUpper(marker)
|
||||||
start = -1
|
start = -1
|
||||||
@@ -151,6 +158,7 @@ func (w *Wad) FindMap(marker string) (start, end int, err error) {
|
|||||||
return start, end, nil
|
return start, end, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Laden der Map Lumps
|
||||||
func (w *Wad) LoadMapLumps(marker string, names ...string) (map[string][]byte, error) {
|
func (w *Wad) LoadMapLumps(marker string, names ...string) (map[string][]byte, error) {
|
||||||
start, end, err := w.FindMap(marker)
|
start, end, err := w.FindMap(marker)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
11
main.go
11
main.go
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bspviz/internal/mapfmt"
|
||||||
"bspviz/internal/wad"
|
"bspviz/internal/wad"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -48,6 +49,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
fmt.Printf("Map %s: Directory [%d, %d)\n", strings.ToUpper(*mapMarker), start, end)
|
fmt.Printf("Map %s: Directory [%d, %d)\n", strings.ToUpper(*mapMarker), start, end)
|
||||||
|
|
||||||
|
//info über die daten in WAD
|
||||||
if *info {
|
if *info {
|
||||||
lumps, err := w.LoadMapLumps(*mapMarker, "VERTEXES", "LINEDEFS")
|
lumps, err := w.LoadMapLumps(*mapMarker, "VERTEXES", "LINEDEFS")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -55,12 +57,20 @@ func main() {
|
|||||||
}
|
}
|
||||||
vb := lumps["VERTEXES"]
|
vb := lumps["VERTEXES"]
|
||||||
lb := lumps["LINEDEFS"]
|
lb := lumps["LINEDEFS"]
|
||||||
|
m, err := mapfmt.LoadMap(lumps)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
verts := len(vb) / 4
|
verts := len(vb) / 4
|
||||||
lines := len(lb) / 14
|
lines := len(lb) / 14
|
||||||
fmt.Printf("VERTEXES: bytes=%d count=%d\n", len(vb), verts)
|
fmt.Printf("VERTEXES: bytes=%d count=%d\n", len(vb), verts)
|
||||||
fmt.Printf("LINEDEFS: bytes=%d count=%d\n", len(lb), lines)
|
fmt.Printf("LINEDEFS: bytes=%d count=%d\n", len(lb), lines)
|
||||||
|
|
||||||
|
fmt.Printf("Map has %d vertices and %d linedefs\n", len(m.Vertices), len(m.Linedefs))
|
||||||
|
fmt.Printf("First vertex: %+v\n", m.Vertices[0])
|
||||||
|
fmt.Printf("First linedef: %+v\n", m.Linedefs[0])
|
||||||
|
|
||||||
if len(vb)%4 != 0 {
|
if len(vb)%4 != 0 {
|
||||||
fmt.Println("WARN: VERTEXES size ist kein Vielfaches von 4 → Format prüfen")
|
fmt.Println("WARN: VERTEXES size ist kein Vielfaches von 4 → Format prüfen")
|
||||||
}
|
}
|
||||||
@@ -69,6 +79,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generiert einzelne Lump Dateien zum Debugen
|
||||||
if *extract != "" {
|
if *extract != "" {
|
||||||
want := strings.Split(*extract, ",")
|
want := strings.Split(*extract, ",")
|
||||||
for i := range want {
|
for i := range want {
|
||||||
|
|||||||
Reference in New Issue
Block a user