Skip to content

Commit

Permalink
add item deserialization endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cfi2017 committed Apr 12, 2020
1 parent 2633aa6 commit b0776dd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
43 changes: 34 additions & 9 deletions internal/item/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"log"
"strings"
"sync"

"github.com/cfi2017/bl3-save/pkg/pb"
)

var (
Expand All @@ -27,6 +29,8 @@ type Item struct {
Parts []string
Generics []string
Overflow string
Version uint64
Wrapper *pb.OakInventoryItemSaveGameData
}

func DecryptSerial(data []byte) ([]byte, error) {
Expand Down Expand Up @@ -95,23 +99,29 @@ func Deserialize(data []byte) (item Item, err error) {
return
}

version := readNBits(r, 7)
item.Version = readNBits(r, 7)

item.Balance = getPart("InventoryBalanceData", version, r)
item.InvData = getPart("InventoryData", version, r)
item.Manufacturer = getPart("ManufacturerData", version, r)
item.Balance = getPart("InventoryBalanceData", readNBits(r,
getBits("InventoryBalanceData", item.Version))-1)
item.InvData = getPart("InventoryData", readNBits(r,
getBits("InventoryData", item.Version))-1)
item.Manufacturer = getPart("ManufacturerData", readNBits(r,
getBits("ManufacturerData", item.Version))-1)
item.Level = int(readNBits(r, 7))

if k, e := btik[strings.ToLower(item.Balance)]; e {
bits := db.GetData(k).GetBits(item.Version)
partCount := int(readNBits(r, 6))
item.Parts = make([]string, partCount)
for i := 0; i < partCount; i++ {
item.Parts[i] = getPart(k, version, r)
item.Parts[i] = getPart(k, readNBits(r, bits)-1)
}
genericCount := int(readNBits(r, 4))
item.Generics = make([]string, genericCount)
for i := 0; i < genericCount; i++ {
item.Generics[i] = getPart(k, version, r)
// looks like the bits are the same
// for all the parts and generics
item.Generics[i] = getPart(k, readNBits(r, bits)-1)
}
item.Overflow = r.Overflow()

Expand All @@ -122,10 +132,25 @@ func Deserialize(data []byte) (item Item, err error) {
return
}

func getPart(key string, version uint64, r *Reader) string {
func getBits(k string, v uint64) int {
return db.GetData(k).GetBits(v)
}

/*func Serialize(item Item) []byte {
data := make([]byte, 0)
buffer := item.Overflow
if k, e := btik[strings.ToLower(item.Balance)]; e {
for i := len(item.Generics); i <= 0; i-- {
bits := getBits(k, item.Version)
item.Generics[i]
}
}
}*/

func getPart(key string, index uint64) string {
data := db.GetData(key)
bits := data.GetBits(version)
index := readNBits(r, bits) - 1
return data.GetPart(index)
}

Expand Down
15 changes: 13 additions & 2 deletions internal/server/character.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package server

import (
"fmt"
"io/ioutil"
"math"
"os"
"regexp"
"strconv"

"github.com/cfi2017/bl3-save/internal/item"
"github.com/cfi2017/bl3-save/internal/shared"
"github.com/cfi2017/bl3-save/pkg/character"
"github.com/cfi2017/bl3-save/pkg/pb"
Expand Down Expand Up @@ -126,7 +126,18 @@ func getItems(c *gin.Context) {
c.AbortWithStatus(500)
}
_, char := character.Deserialize(f)
fmt.Println(char) // temp
items := make([]item.Item, len(char.InventoryItems))
for _, data := range char.InventoryItems {
i, err := item.Deserialize(data.ItemSerialNumber)
if err != nil {
c.AbortWithStatus(500)
return
}
i.Wrapper = data
items = append(items, i)
}
c.JSON(200, items)
return
}

func saveItems(c *gin.Context) {
Expand Down

0 comments on commit b0776dd

Please sign in to comment.