Skip to content

Commit

Permalink
address issue nokka#10 and nokka#11.
Browse files Browse the repository at this point in the history
* add item version
* make prefix/suffixes a fixed length array
* add simple unit test
  • Loading branch information
dschu012 committed Sep 17, 2020
1 parent 60fb591 commit 6d182d5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
32 changes: 21 additions & 11 deletions d2s.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func parseMercItems(bfr io.ByteReader, char *Character) error {
// offset: 8, "f"
f := ibr.ReadBits64(8, false)

if string(j) != "j" || string(f) != "f" {
if fmt.Sprintf("%c", j) != "j" || fmt.Sprintf("%c", f) != "f" {
return errors.New("Failed to find merc header jf")
}

Expand Down Expand Up @@ -526,7 +526,7 @@ func parseItemList(bfr io.ByteReader, itemCount int) ([]item, error) {
break
}

name += string(c)
name += fmt.Sprintf("%c", c)
}

parsed.PersonalizedName = name
Expand Down Expand Up @@ -632,7 +632,7 @@ func parseItemList(bfr io.ByteReader, itemCount int) ([]item, error) {
// of items that need to be worn for each list of magical properties
// to be active
for i := 0; i < 5; i++ {
if (setListValue & (1 << uint(i)) == 0) {
if setListValue&(1<<uint(i)) == 0 {
continue
}
// bit position 0 means it requires >= 2 items worn, etc
Expand Down Expand Up @@ -728,7 +728,7 @@ func parseSimpleBits(ibr *bitReader, item *item) error {
m := ibr.ReadBits64(8, false)
readBits += 8

if string(j) != "J" || string(m) != "M" {
if fmt.Sprintf("%c", j) != "J" || fmt.Sprintf("%c", m) != "M" {
return errors.New("Failed to find item header JM")
}

Expand Down Expand Up @@ -797,8 +797,16 @@ func parseSimpleBits(ibr *bitReader, item *item) error {
readBits++

// offset 43, unknown
reverseBits(ibr.ReadBits64(15, true), 15)
readBits += 15
reverseBits(ibr.ReadBits64(5, true), 5)
readBits += 5

// offset 48
item.Version = reverseBits(ibr.ReadBits64(8, true), 8)
readBits += 8

// offset 56, unknown
reverseBits(ibr.ReadBits64(2, true), 2)
readBits += 2

// offset 58
item.LocationID = reverseBits(ibr.ReadBits64(3, true), 3)
Expand Down Expand Up @@ -828,7 +836,7 @@ func parseSimpleBits(ibr *bitReader, item *item) error {
// offset 76, item type, 4 chars, each 8 bit (not byte aligned)
var itemType string
for j := 0; j < 4; j++ {
itemType += string(reverseBits(ibr.ReadBits64(8, true), 8))
itemType += fmt.Sprintf("%c", reverseBits(ibr.ReadBits64(8, true), 8))
}

item.Type = strings.Trim(itemType, " ")
Expand Down Expand Up @@ -896,7 +904,7 @@ func parseSimpleBits(ibr *bitReader, item *item) error {
break
}
readBits += 7
name += string(c)
name += fmt.Sprintf("%c", c)
}

item.EarAttributes = earAttributes{
Expand Down Expand Up @@ -944,16 +952,18 @@ func parseRareOrCraftedBits(ibr *bitReader, item *item) (int, error) {

item.RareName2 = name2

// Following the name IDs, we got 6 possible magical name IDs, the pattern
// is 1 bit id, 11 bit value... But the value will only exist if the prefix
// is 1. So we'll read the id first and check it against 1.
// Array of 6 possible prefixes and suffixes. First read 1 bit. If 1, read 11 more
// for the prefix/suffix id defined in MagicPrefix.txt and MagicSuffix.txt.
// Even indices are prefixes, odd suffixes.
for i := 0; i < 6; i++ {
prefix := reverseBits(ibr.ReadBits64(1, true), 1)
readBits++

if prefix == 1 {
item.MagicalNameIDs = append(item.MagicalNameIDs, reverseBits(ibr.ReadBits64(11, true), 11))
readBits += 11
} else {
item.MagicalNameIDs = append(item.MagicalNameIDs, 0)
}
}

Expand Down
33 changes: 33 additions & 0 deletions d2s_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package d2s

import (
"log"
"os"
"testing"
)

/**
* Parse NokkaSorc.d2s from examples and verify the chars name
**/
func TestParse(t *testing.T) {
path := "examples/NokkaSorc.d2s"
file, err := os.Open(path)
if err != nil {
log.Fatal("Error while opening .d2s file", err)
}

defer file.Close()

char, err := Parse(file)
if err != nil {
log.Fatal(err)
}

if char.Header.Name.String() != "NokkaSorc" {
t.Errorf("Expected char name to be %v. Got %v", "NokkaSorc", char.Header.Name)
}

//Write out the data to console.
//charJSON, err := json.MarshalIndent(char, "", " ")
//fmt.Printf("%s", string(charJSON))
}
Binary file added examples/NokkaSorc.d2s
Binary file not shown.
1 change: 1 addition & 0 deletions item.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type item struct {
Personalized uint64 `json:"personalized"`
PersonalizedName string `json:"personalized_name,omitempty"`
GivenRuneword uint64 `json:"given_runeword"`
Version uint64 `json:"version"`
LocationID uint64 `json:"location_id"`
EquippedID uint64 `json:"equipped_id,omitempty"`
PositionX uint64 `json:"position_x"`
Expand Down

0 comments on commit 6d182d5

Please sign in to comment.