mirror of
https://github.com/OpenDiablo2/OpenDiablo2
synced 2025-02-02 22:57:04 -05:00
Ds1 refactor: some test improvement (#5)
* ds1 refactor: floor_shadow.go: methods Encode, Decode an Hidden are methods of floorShadow * ds1 refactor: test checks, if our methods sets all fields correctly * ds1 refactor: minor bugfixes * i don't remember what's this, but i commit it ;-) * ds1 refactor: reverted some pushed by mistake things Co-authored-by: M. Sz <mszeptuch@protonmail.com>
This commit is contained in:
parent
4bc4fa0221
commit
ca47018e8a
@ -135,7 +135,9 @@ func (ds1 *DS1) SetTiles(tiles [][]Tile) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ds1.tiles = tiles
|
ds1.tiles = tiles
|
||||||
|
ds1.layerStreamTypes = ds1.setupStreamLayerTypes()
|
||||||
ds1.dirty = true
|
ds1.dirty = true
|
||||||
|
ds1.update()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tile returns the tile at the given x,y tile coordinate (nil if x,y is out of bounds)
|
// Tile returns the tile at the given x,y tile coordinate (nil if x,y is out of bounds)
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package d2ds1
|
package d2ds1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
||||||
@ -48,11 +46,16 @@ func exampleDS1() *DS1 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// checks, if DS1 structure could be marshaled and unmarshaled
|
// checks, if DS1 structure could be marshaled and unmarshaled
|
||||||
func testIfRestorable(ds1 *DS1) error {
|
func testIfRestorable(ds1 *DS1, test func(ds1 *DS1)) error {
|
||||||
|
test(ds1)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
data := ds1.Marshal()
|
data := ds1.Marshal()
|
||||||
_, err = LoadDS1(data)
|
newDS1, err := LoadDS1(data)
|
||||||
|
_ = newDS1
|
||||||
|
|
||||||
|
test(newDS1)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -94,11 +97,13 @@ func TestDS1_AddFile(t *testing.T) {
|
|||||||
|
|
||||||
numAfter := len(ds1.files)
|
numAfter := len(ds1.files)
|
||||||
|
|
||||||
|
test := func(ds1 *DS1) {
|
||||||
if (numBefore + 1) != numAfter {
|
if (numBefore + 1) != numAfter {
|
||||||
t.Error("unexpected number of files in ds1")
|
t.Error("unexpected number of files in ds1")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := testIfRestorable(ds1); err != nil {
|
if err := testIfRestorable(ds1, test); err != nil {
|
||||||
t.Errorf("unable to restore: %v", err)
|
t.Errorf("unable to restore: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -106,6 +111,7 @@ func TestDS1_AddFile(t *testing.T) {
|
|||||||
func TestDS1_RemoveFile(t *testing.T) {
|
func TestDS1_RemoveFile(t *testing.T) {
|
||||||
ds1 := exampleDS1()
|
ds1 := exampleDS1()
|
||||||
|
|
||||||
|
test := func(ds1 *DS1) {
|
||||||
numBefore := len(ds1.files)
|
numBefore := len(ds1.files)
|
||||||
|
|
||||||
err := ds1.RemoveFile("nonexistant file")
|
err := ds1.RemoveFile("nonexistant file")
|
||||||
@ -133,8 +139,9 @@ func TestDS1_RemoveFile(t *testing.T) {
|
|||||||
if len(ds1.files) != numBefore {
|
if len(ds1.files) != numBefore {
|
||||||
t.Error("file not removed when it should have been")
|
t.Error("file not removed when it should have been")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := testIfRestorable(ds1); err != nil {
|
if err := testIfRestorable(ds1, test); err != nil {
|
||||||
t.Errorf("unable to restore: %v", err)
|
t.Errorf("unable to restore: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -160,11 +167,13 @@ func TestDS1_AddObject(t *testing.T) {
|
|||||||
|
|
||||||
numAfter := len(ds1.objects)
|
numAfter := len(ds1.objects)
|
||||||
|
|
||||||
|
test := func(ds1 *DS1) {
|
||||||
if (numBefore + 1) != numAfter {
|
if (numBefore + 1) != numAfter {
|
||||||
t.Error("unexpected number of objects in ds1")
|
t.Error("unexpected number of objects in ds1")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := testIfRestorable(ds1); err != nil {
|
if err := testIfRestorable(ds1, test); err != nil {
|
||||||
t.Errorf("unable to restore: %v", err)
|
t.Errorf("unable to restore: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -184,11 +193,13 @@ func TestDS1_RemoveObject(t *testing.T) {
|
|||||||
|
|
||||||
ds1.RemoveObject(obj)
|
ds1.RemoveObject(obj)
|
||||||
|
|
||||||
|
test := func(ds1 *DS1) {
|
||||||
if len(ds1.objects) == numBefore {
|
if len(ds1.objects) == numBefore {
|
||||||
t.Error("did not remove object when expected")
|
t.Error("did not remove object when expected")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := testIfRestorable(ds1); err != nil {
|
if err := testIfRestorable(ds1, test); err != nil {
|
||||||
t.Errorf("unable to restore: %v", err)
|
t.Errorf("unable to restore: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -218,18 +229,30 @@ func TestDS1_SetTiles(t *testing.T) {
|
|||||||
Floors: []floorShadow{
|
Floors: []floorShadow{
|
||||||
{0, 0, 2, 3, 4, 55, 33, true, 999},
|
{0, 0, 2, 3, 4, 55, 33, true, 999},
|
||||||
},
|
},
|
||||||
Shadows: []floorShadow{
|
|
||||||
{2, 4, 5, 33, 6, 7, 0, false, 1024},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
exampleTile2 := Tile{
|
|
||||||
Walls: []Wall{
|
Walls: []Wall{
|
||||||
{2, 3, 4, 5, 3, 2, 3, 0, 33, 99},
|
{2, 3, 4, 5, 3, 2, 3, 0, 33, 99},
|
||||||
},
|
},
|
||||||
Shadows: []floorShadow{
|
Shadows: []floorShadow{
|
||||||
{2, 4, 5, 33, 6, 7, 0, false, 1024},
|
{2, 4, 5, 33, 6, 7, 0, false, 1024},
|
||||||
},
|
},
|
||||||
|
Substitutions: []Substitution{
|
||||||
|
{1024},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
exampleTile2 := Tile{
|
||||||
|
Floors: []floorShadow{
|
||||||
|
{0, 0, 2, 3, 4, 55, 33, true, 999},
|
||||||
|
},
|
||||||
|
Walls: []Wall{
|
||||||
|
{2, 3, 4, 5, 3, 2, 3, 0, 33, 99},
|
||||||
|
},
|
||||||
|
Shadows: []floorShadow{
|
||||||
|
{2, 4, 5, 33, 6, 7, 0, false, 1024},
|
||||||
|
},
|
||||||
|
Substitutions: []Substitution{
|
||||||
|
{1234},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
tiles := [][]Tile{{exampleTile1, exampleTile2}}
|
tiles := [][]Tile{{exampleTile1, exampleTile2}}
|
||||||
@ -244,13 +267,17 @@ func TestDS1_SetTiles(t *testing.T) {
|
|||||||
t.Fatal("unexpected tile was set")
|
t.Fatal("unexpected tile was set")
|
||||||
}
|
}
|
||||||
|
|
||||||
if ds1.tiles[0][1].Walls[0] != exampleTile2.Walls[0] {
|
if ds1.tiles[0][0].Walls[0] != exampleTile2.Walls[0] {
|
||||||
t.Fatal("unexpected tile was set")
|
t.Fatal("unexpected tile was set")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(ds1.tiles[0][1].Walls) != len(exampleTile2.Walls) {
|
if len(ds1.tiles[0][0].Walls) != len(exampleTile2.Walls) {
|
||||||
t.Fatal("unexpected tile was set")
|
t.Fatal("unexpected tile was set")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := testIfRestorable(ds1, func(_ *DS1) {}); err != nil {
|
||||||
|
t.Errorf("unable to restore: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDS1_Tile(t *testing.T) {
|
func TestDS1_Tile(t *testing.T) {
|
||||||
@ -268,28 +295,36 @@ func TestDS1_SetTile(t *testing.T) {
|
|||||||
|
|
||||||
exampleTile := Tile{
|
exampleTile := Tile{
|
||||||
Floors: []floorShadow{
|
Floors: []floorShadow{
|
||||||
{5, 8, 9, 4, 3, 4, 2, true, 1024},
|
{5, 8, 9, 4, 3, 0, 0, true, 1024},
|
||||||
{8, 22, 7, 9, 6, 3, 0, false, 1024},
|
|
||||||
},
|
},
|
||||||
Walls: []Wall{
|
Walls: []Wall{
|
||||||
{2, 3, 4, 5, 3, 2, 3, 0, 33, 99},
|
{2, 0, 4, 5, 3, 2, 3, 0, 33, 99},
|
||||||
},
|
},
|
||||||
Shadows: []floorShadow{
|
Shadows: []floorShadow{
|
||||||
{2, 44, 99, 2, 4, 3, 2, true, 933},
|
{2, 44, 99, 2, 4, 3, 2, true, 933},
|
||||||
},
|
},
|
||||||
|
Substitutions: []Substitution{
|
||||||
|
{10244},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
ds1.SetTile(0, 0, &exampleTile)
|
ds1.SetTile(0, 0, &exampleTile)
|
||||||
|
|
||||||
if ds1.tiles[0][0].Floors[0] != exampleTile.Floors[0] {
|
test := func(ds1 *DS1) {
|
||||||
t.Fatal("unexpected tile was set")
|
if ds1.tiles[0][0].Floors[0].Prop1 != exampleTile.Floors[0].Prop1 {
|
||||||
|
t.Fatal("c1.unexpected tile was set")
|
||||||
|
}
|
||||||
|
|
||||||
|
if ds1.tiles[0][0].Walls[0].Zero != exampleTile.Walls[0].Zero {
|
||||||
|
t.Fatal("c1.unexpected tile was set")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(ds1.tiles[0][0].Walls) != len(exampleTile.Walls) {
|
if len(ds1.tiles[0][0].Walls) != len(exampleTile.Walls) {
|
||||||
t.Fatal("unexpected tile was set")
|
t.Fatal("c2.unexpected tile was set")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := testIfRestorable(ds1); err != nil {
|
if err := testIfRestorable(ds1, test); err != nil {
|
||||||
t.Errorf("unable to restore: %v", err)
|
t.Errorf("unable to restore: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -311,11 +346,13 @@ func TestDS1_SetVersion(t *testing.T) {
|
|||||||
|
|
||||||
ds1.SetVersion(newVersion)
|
ds1.SetVersion(newVersion)
|
||||||
|
|
||||||
|
test := func(ds1 *DS1) {
|
||||||
if newVersion != int(ds1.version) {
|
if newVersion != int(ds1.version) {
|
||||||
t.Fatal("ds1.SetVersion set version incorrectly")
|
t.Fatal("ds1.SetVersion set version incorrectly")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := testIfRestorable(ds1); err != nil {
|
if err := testIfRestorable(ds1, test); err != nil {
|
||||||
t.Errorf("unable to restore: %v", err)
|
t.Errorf("unable to restore: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -335,11 +372,13 @@ func TestDS1_SetWidth(t *testing.T) {
|
|||||||
|
|
||||||
ds1.SetWidth(int(newWidth))
|
ds1.SetWidth(int(newWidth))
|
||||||
|
|
||||||
|
test := func(ds1 *DS1) {
|
||||||
if newWidth != ds1.width {
|
if newWidth != ds1.width {
|
||||||
t.Fatal("unexpected width after set")
|
t.Fatal("unexpected width after set")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := testIfRestorable(ds1); err != nil {
|
if err := testIfRestorable(ds1, test); err != nil {
|
||||||
t.Errorf("unable to restore: %v", err)
|
t.Errorf("unable to restore: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -359,12 +398,13 @@ func TestDS1_SetHeight(t *testing.T) {
|
|||||||
|
|
||||||
ds1.SetHeight(int(newHeight))
|
ds1.SetHeight(int(newHeight))
|
||||||
|
|
||||||
|
test := func(ds1 *DS1) {
|
||||||
if newHeight != ds1.height {
|
if newHeight != ds1.height {
|
||||||
fmt.Println(newHeight, ds1.height)
|
|
||||||
t.Fatal("unexpected heigth after set")
|
t.Fatal("unexpected heigth after set")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := testIfRestorable(ds1); err != nil {
|
if err := testIfRestorable(ds1, test); err != nil {
|
||||||
t.Errorf("unable to restore: %v", err)
|
t.Errorf("unable to restore: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -386,15 +426,17 @@ func TestDS1_SetAct(t *testing.T) {
|
|||||||
t.Error("act cannot be less than 0")
|
t.Error("act cannot be less than 0")
|
||||||
}
|
}
|
||||||
|
|
||||||
nice := 69420
|
nice := 5
|
||||||
|
|
||||||
ds1.SetAct(nice)
|
ds1.SetAct(nice)
|
||||||
|
|
||||||
|
test := func(ds1 *DS1) {
|
||||||
if int(ds1.act) != nice {
|
if int(ds1.act) != nice {
|
||||||
t.Error("unexpected value for act")
|
t.Error("unexpected value for act")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := testIfRestorable(ds1); err != nil {
|
if err := testIfRestorable(ds1, test); err != nil {
|
||||||
t.Errorf("unable to restore: %v", err)
|
t.Errorf("unable to restore: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ func (f *Floor) Hidden() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Decode decodes floor-shadow record
|
// Decode decodes floor-shadow record
|
||||||
func (f *Floor) Decode(dw uint32) {
|
func (f *floorShadow) Decode(dw uint32) {
|
||||||
f.Prop1 = byte((dw & prop1Bitmask) >> prop1Offset)
|
f.Prop1 = byte((dw & prop1Bitmask) >> prop1Offset)
|
||||||
f.Sequence = byte((dw & sequenceBitmask) >> sequenceOffset)
|
f.Sequence = byte((dw & sequenceBitmask) >> sequenceOffset)
|
||||||
f.Unknown1 = byte((dw & unknown1Bitmask) >> unknown1Offset)
|
f.Unknown1 = byte((dw & unknown1Bitmask) >> unknown1Offset)
|
||||||
@ -64,7 +64,7 @@ func (f *Floor) Decode(dw uint32) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Encode adds Floor's bits to stream writter given
|
// Encode adds Floor's bits to stream writter given
|
||||||
func (f *Floor) Encode(sw *d2datautils.StreamWriter) {
|
func (f *floorShadow) Encode(sw *d2datautils.StreamWriter) {
|
||||||
sw.PushBits32(uint32(f.Prop1), prop1Length)
|
sw.PushBits32(uint32(f.Prop1), prop1Length)
|
||||||
sw.PushBits32(uint32(f.Sequence), sequenceLength)
|
sw.PushBits32(uint32(f.Sequence), sequenceLength)
|
||||||
sw.PushBits32(uint32(f.Unknown1), unknown1Length)
|
sw.PushBits32(uint32(f.Unknown1), unknown1Length)
|
||||||
|
Loading…
Reference in New Issue
Block a user