From b0776dd680574773fc494c420050bc8c7ecd12d7 Mon Sep 17 00:00:00 2001 From: Carlo Field Date: Sun, 12 Apr 2020 23:25:44 +0200 Subject: [PATCH] add item deserialization endpoint --- internal/item/item.go | 43 ++++++++++++++++++++++++++++-------- internal/server/character.go | 15 +++++++++++-- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/internal/item/item.go b/internal/item/item.go index 04645e0..6d16ec4 100644 --- a/internal/item/item.go +++ b/internal/item/item.go @@ -11,6 +11,8 @@ import ( "log" "strings" "sync" + + "github.com/cfi2017/bl3-save/pkg/pb" ) var ( @@ -27,6 +29,8 @@ type Item struct { Parts []string Generics []string Overflow string + Version uint64 + Wrapper *pb.OakInventoryItemSaveGameData } func DecryptSerial(data []byte) ([]byte, error) { @@ -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() @@ -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) } diff --git a/internal/server/character.go b/internal/server/character.go index a218811..a0d0b3f 100644 --- a/internal/server/character.go +++ b/internal/server/character.go @@ -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" @@ -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) {