Skip to content

Commit

Permalink
improve conversion and item deserializing (fewer panics)
Browse files Browse the repository at this point in the history
  • Loading branch information
cfi2017 committed Apr 18, 2020
1 parent cd7ac0a commit 354efae
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
6 changes: 4 additions & 2 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ var ConvertCmd = &cobra.Command{
// try deserializing item
i, err := item.Deserialize(bs)
if err != nil {
panic(err)
cmd.PrintErr("couldn't deserialize item")
return
}
// convert to dm item
bs, err = json.Marshal(item.GibbedToDm(i))
if err != nil {
panic(err)
cmd.PrintErr("couldn't convert item to dm format")
return
}
cmd.Print(base64.StdEncoding.EncodeToString(bs))
return
Expand Down
20 changes: 10 additions & 10 deletions internal/item/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,58 +218,58 @@ func Serialize(item Item, seed int32) ([]byte, error) {
err := w.WriteInt(index, bits)
if err != nil {
log.Printf("tried to fit index %v into %v bits for %s", index, bits, item.Generics[i])
panic(err)
return nil, err
}
}
err := w.WriteInt(len(item.Generics), 4)
if err != nil {
panic(err)
return nil, err
}
bits = getBits(k, item.Version)
for i := len(item.Parts) - 1; i >= 0; i-- {
err := w.WriteInt(getIndexFor(k, item.Parts[i])+1, bits)
if err != nil {
panic(err)
return nil, err
}
}
err = w.WriteInt(len(item.Parts), 6)
if err != nil {
panic(err)
return nil, err
}
}

err = w.WriteInt(item.Level, 7)
if err != nil {
panic(err)
return nil, err
}

manIndex := getIndexFor("ManufacturerData", item.Manufacturer) + 1
manBits := getBits("ManufacturerData", item.Version)
err = w.WriteInt(manIndex, manBits)
if err != nil {
panic(err)
return nil, err
}
invIndex := getIndexFor("InventoryData", item.InvData) + 1
invBits := getBits("InventoryData", item.Version)
err = w.WriteInt(invIndex, invBits)
if err != nil {
panic(err)
return nil, err
}
balanceIndex := getIndexFor("InventoryBalanceData", item.Balance) + 1
balanceBits := getBits("InventoryBalanceData", item.Version)
err = w.WriteInt(balanceIndex, balanceBits)
if err != nil {
panic(err)
return nil, err
}

err = w.WriteInt(int(item.Version), 7)
if err != nil {
panic(err)
return nil, err
}

err = w.WriteInt(128, 8)
if err != nil {
panic(err)
return nil, err
}

return EncryptSerial(w.GetBytes(), seed)
Expand Down

0 comments on commit 354efae

Please sign in to comment.