From 6f41387e308708aa6dee10184a12650e44f0c145 Mon Sep 17 00:00:00 2001 From: gucio321 <73652197+gucio321@users.noreply.github.com> Date: Wed, 3 Mar 2021 00:20:44 +0100 Subject: [PATCH] Ds1 refactor - tests (#10) * ds1 refactor test: example data * added loader check * ds1 refactor: fixed bug, with loading substitutions; added descriptive error message in engine.go:118 and changed Logger.Error with Logger.Fatal * ds1 refactor: fixed loading bug * ds1 refactor: fixed bug when walls wasn't rendered (now we can see only walls :-) Co-authored-by: M. Sz --- d2common/d2fileformats/d2ds1/ds1.go | 8 +- d2common/d2fileformats/d2ds1/ds1_test.go | 221 +++++++++++++++++++++++ d2common/d2fileformats/d2ds1/tile.go | 8 +- d2core/d2asset/asset_manager.go | 2 +- d2core/d2map/d2mapengine/engine.go | 2 +- 5 files changed, 233 insertions(+), 8 deletions(-) diff --git a/d2common/d2fileformats/d2ds1/ds1.go b/d2common/d2fileformats/d2ds1/ds1.go index e13e5890..1669448a 100644 --- a/d2common/d2fileformats/d2ds1/ds1.go +++ b/d2common/d2fileformats/d2ds1/ds1.go @@ -42,7 +42,7 @@ type DS1 struct { const ( defaultNumFloors = 1 defaultNumShadows = maxShadowLayers - defaultNumSubstitutions = maxSubstitutionLayers + defaultNumSubstitutions = 0 ) // Unmarshal the given bytes to a DS1 struct @@ -154,8 +154,8 @@ func (ds1 *DS1) loadHeader(br *d2datautils.StreamReader) error { return fmt.Errorf("reading height: %v", err) } - //width++ - //height++ + width++ + height++ ds1.SetSize(int(width), int(height)) @@ -506,7 +506,7 @@ func (ds1 *DS1) loadLayerStreams(br *d2datautils.StreamReader) error { } } - tile := ds1.Orientations[wallIndex].Tile(x, y) + tile := ds1.Walls[wallIndex].Tile(x, y) tile.Type = d2enum.TileType(c) tile.Zero = byte((dw & wallZeroBitmask) >> wallZeroOffset) case layerStreamFloor1, layerStreamFloor2: diff --git a/d2common/d2fileformats/d2ds1/ds1_test.go b/d2common/d2fileformats/d2ds1/ds1_test.go index f69ef7f1..afc0ca0e 100644 --- a/d2common/d2fileformats/d2ds1/ds1_test.go +++ b/d2common/d2fileformats/d2ds1/ds1_test.go @@ -1 +1,222 @@ package d2ds1 + +import ( + "testing" + + "log" + "os" + + "github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum" + "github.com/OpenDiablo2/OpenDiablo2/d2common/d2path" +) + +func exampleData() *DS1 { + exampleFloor1 := Tile{ + // common fields + tileCommonFields: tileCommonFields{ + Prop1: 2, + Sequence: 89, + Unknown1: 123, + Style: 20, + Unknown2: 53, + HiddenBytes: 1, + RandomIndex: 2, + YAdjust: 21, + }, + tileFloorShadowFields: tileFloorShadowFields{ + Animated: false, + }, + } + + exampleFloor2 := Tile{ + // common fields + tileCommonFields: tileCommonFields{ + Prop1: 3, + Sequence: 89, + Unknown1: 213, + Style: 28, + Unknown2: 53, + HiddenBytes: 7, + RandomIndex: 3, + YAdjust: 28, + }, + tileFloorShadowFields: tileFloorShadowFields{ + Animated: true, + }, + } + + exampleWall1 := Tile{ + // common fields + tileCommonFields: tileCommonFields{ + Prop1: 3, + Sequence: 89, + Unknown1: 213, + Style: 28, + Unknown2: 53, + HiddenBytes: 7, + RandomIndex: 3, + YAdjust: 28, + }, + tileWallFields: tileWallFields{ + Type: d2enum.TileRightWall, + }, + } + + exampleWall2 := Tile{ + // common fields + tileCommonFields: tileCommonFields{ + Prop1: 3, + Sequence: 93, + Unknown1: 193, + Style: 17, + Unknown2: 13, + HiddenBytes: 1, + RandomIndex: 1, + YAdjust: 22, + }, + tileWallFields: tileWallFields{ + Type: d2enum.TileLeftWall, + }, + } + + exampleShadow := Tile{ + // common fields + tileCommonFields: tileCommonFields{ + Prop1: 3, + Sequence: 93, + Unknown1: 173, + Style: 17, + Unknown2: 12, + HiddenBytes: 1, + RandomIndex: 1, + YAdjust: 22, + }, + tileFloorShadowFields: tileFloorShadowFields{ + Animated: false, + }, + } + + result := &DS1{ + ds1Layers: &ds1Layers{ + width: 20, + height: 80, + Floors: layerGroup{ + // number of floors (one floor) + { + // tile grid = []tileRow + tiles: tileGrid{ + // tile rows = []Tile + // 2x2 tiles + { + exampleFloor1, + exampleFloor2, + }, + { + exampleFloor2, + exampleFloor1, + }, + }, + }, + }, + Walls: layerGroup{ + // number of walls (two floors) + { + // tile grid = []tileRow + tiles: tileGrid{ + // tile rows = []Tile + // 2x2 tiles + { + exampleWall1, + exampleWall2, + }, + { + exampleWall2, + exampleWall1, + }, + }, + }, + { + // tile grid = []tileRow + tiles: tileGrid{ + // tile rows = []Tile + // 2x2 tiles + { + exampleWall1, + exampleWall2, + }, + { + exampleWall2, + exampleWall1, + }, + }, + }, + }, + Shadows: layerGroup{ + // number of shadows (always 1) + { + // tile grid = []tileRow + tiles: tileGrid{ + // tile rows = []Tile + // 2x2 tiles + { + exampleShadow, + exampleShadow, + }, + { + exampleShadow, + exampleShadow, + }, + }, + }, + }, + }, + Files: []string{"a.dt1", "bfile.dt1"}, + Objects: []Object{ + {0, 0, 0, 0, 0, nil}, + {0, 1, 0, 0, 0, []d2path.Path{{}}}, + {0, 2, 0, 0, 0, nil}, + {0, 3, 0, 0, 0, nil}, + }, + substitutionGroups: nil, + version: 17, + Act: 1, + substitutionType: 0, + unknown1: make([]byte, 8), + unknown2: 20, + } + + return result +} + +func TestDS1_Load(t *testing.T) { + testFile, fileErr := os.Open("testdata/testdata.ds1") + if fileErr != nil { + t.Error("cannot open test data file") + return + } + + data := make([]byte, 0) + buf := make([]byte, 16) + + for { + numRead, err := testFile.Read(buf) + + data = append(data, buf[:numRead]...) + + if err != nil { + break + } + } + + _, loadErr := Unmarshal(data) + if loadErr != nil { + t.Error(loadErr) + } + + err := testFile.Close() + if err != nil { + t.Fail() + log.Print(err) + } + +} diff --git a/d2common/d2fileformats/d2ds1/tile.go b/d2common/d2fileformats/d2ds1/tile.go index 42726d31..a8077536 100644 --- a/d2common/d2fileformats/d2ds1/tile.go +++ b/d2common/d2fileformats/d2ds1/tile.go @@ -32,8 +32,6 @@ const ( ) type tileCommonFields struct { - Type d2enum.TileType - Zero byte Prop1 byte Sequence byte Unknown1 byte @@ -52,11 +50,17 @@ type tileSubstitutionFields struct { Substitution uint32 // unknown } +type tileWallFields struct { + Type d2enum.TileType + Zero byte +} + // Tile represents a tile record in a DS1 file. type Tile struct { tileCommonFields tileFloorShadowFields tileSubstitutionFields + tileWallFields } // Hidden returns if wall is hidden diff --git a/d2core/d2asset/asset_manager.go b/d2core/d2asset/asset_manager.go index 702d17ec..04771293 100644 --- a/d2core/d2asset/asset_manager.go +++ b/d2core/d2asset/asset_manager.go @@ -519,7 +519,7 @@ func (am *AssetManager) LoadDS1(ds1Path string) (*d2ds1.DS1, error) { ds1, err := d2ds1.Unmarshal(fileData) if err != nil { - return nil, err + return nil, fmt.Errorf("loading ds1 file %s: %v", "/data/global/tiles"+ds1Path, err) } if err := am.dt1s.Insert(ds1Path, ds1, defaultCacheEntryWeight); err != nil { diff --git a/d2core/d2map/d2mapengine/engine.go b/d2core/d2map/d2mapengine/engine.go index 981cbd4c..3f04855b 100644 --- a/d2core/d2map/d2mapengine/engine.go +++ b/d2core/d2map/d2mapengine/engine.go @@ -115,7 +115,7 @@ func (m *MapEngine) AddDS1(fileName string) { ds1, err := m.asset.LoadDS1(fileName) if err != nil { - m.Error(err.Error()) + m.Fatalf("Loading ds1: %v", err) } for idx := range ds1.Files {