Skip to content

Commit

Permalink
add conversion endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cfi2017 committed Apr 18, 2020
1 parent 34fd347 commit c274f1d
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 79 deletions.
83 changes: 4 additions & 79 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,11 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"strings"

"github.com/cfi2017/bl3-save/internal/item"
"github.com/spf13/cobra"
)

type DigitalMarineItem struct {
CopyType string `json:"copyType"`
Level int `json:"level"`
Blueprint string `json:"blueprint"`
Balance string `json:"balance"`
Manufacturer string `json:"manufacturer"`
ComponentNames []string `json:"componentNames"`
Components []int `json:"components"`
}

// deserializeCmd represents the deserialize command
var convertCmd = &cobra.Command{
Use: "convert",
Expand All @@ -30,7 +19,7 @@ var convertCmd = &cobra.Command{
if err != nil {
panic(err)
}
var dmi DigitalMarineItem
var dmi item.DigitalMarineItem
err = json.Unmarshal(bs, &dmi)
if err != nil {
// try deserializing item
Expand All @@ -39,86 +28,22 @@ var convertCmd = &cobra.Command{
panic(err)
}
// convert to dm item
bs, err = json.Marshal(gibbedToDm(i))
bs, err = json.Marshal(item.GibbedToDm(i))
if err != nil {
panic(err)
}
fmt.Print(base64.StdEncoding.EncodeToString(bs))
return
}
i := dmToGibbed(dmi)
i := item.DmToGibbed(dmi)
bs, err = item.Serialize(i, 0) // encrypt with 0 seed
if err != nil {
panic(err)
}
fmt.Print(base64.StdEncoding.EncodeToString(bs))
fmt.Printf("bl3(%s)", base64.StdEncoding.EncodeToString(bs))
},
}

func dmToGibbed(dmi DigitalMarineItem) item.Item {
i := item.Item{}
db := item.GetDB()
btik := item.GetBtik()
i.Balance = dmKeyToInvKey(dmi.Balance, db.GetData("InventoryBalanceData").Assets)
i.Manufacturer = dmKeyToInvKey(dmi.Manufacturer, db.GetData("ManufacturerData").Assets)
i.Level = dmi.Level
k := btik[strings.ToLower(i.Balance)]
for _, i2 := range dmi.Components {
i.Parts = append(i.Parts, dmKeyToInvKey(dmi.ComponentNames[i2], db.GetData(k).Assets))
}
i.Version = 55
i.InvData = dmKeyToInvKey(strings.Split(dmi.Blueprint, " ")[1], db.GetData("InventoryData").Assets)
return i
}

func getBlueprint(key, invdata string) string {
key = strings.Replace(key, "Part", "", 1)
parts := strings.Split(key, "_")
parts[1], parts[2] = parts[2], parts[1]
key = strings.Join(parts, "_")
return key + " " + invdata
}

func gibbedToDm(i item.Item) DigitalMarineItem {
m := DigitalMarineItem{}
m.Manufacturer = getPartSuffix(i.Manufacturer)
m.Level = i.Level
m.CopyType = "item"
m.Balance = getPartSuffix(i.Balance)
btik := item.GetBtik()
key := btik[strings.ToLower(i.Balance)]
m.Blueprint = getBlueprint(key, getPartSuffix(i.InvData))
for _, part := range i.Parts {
p := getPartSuffix(part)
found := false
for i2, name := range m.ComponentNames {
if name == p {
m.Components = append(m.Components, i2)
found = true
}
}
if !found {
m.ComponentNames = append(m.ComponentNames, p)
m.Components = append(m.Components, len(m.ComponentNames)-1)
}
}
return m
}

func dmKeyToInvKey(key string, assets []string) string {
for _, a := range assets {
if strings.HasSuffix(a, key) {
return a
}
}
return ""
}

func getPartSuffix(part string) string {
p := strings.Split(part, "/")
return p[len(p)-1]
}

func init() {
rootCmd.AddCommand(convertCmd)
// Here you will define your flags and configuration settings.
Expand Down
77 changes: 77 additions & 0 deletions internal/item/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package item

import "strings"

type DigitalMarineItem struct {
CopyType string `json:"copyType"`
Level int `json:"level"`
Blueprint string `json:"blueprint"`
Balance string `json:"balance"`
Manufacturer string `json:"manufacturer"`
ComponentNames []string `json:"componentNames"`
Components []int `json:"components"`
}

func DmToGibbed(dmi DigitalMarineItem) Item {
i := Item{}
db := GetDB()
btik := GetBtik()
i.Balance = DmKeyToInvKey(dmi.Balance, db.GetData("InventoryBalanceData").Assets)
i.Manufacturer = DmKeyToInvKey(dmi.Manufacturer, db.GetData("ManufacturerData").Assets)
i.Level = dmi.Level
k := btik[strings.ToLower(i.Balance)]
for _, i2 := range dmi.Components {
i.Parts = append(i.Parts, DmKeyToInvKey(dmi.ComponentNames[i2], db.GetData(k).Assets))
}
i.Version = 55
i.InvData = DmKeyToInvKey(strings.Split(dmi.Blueprint, " ")[1], db.GetData("InventoryData").Assets)
return i
}

func GetBlueprint(key, invdata string) string {
key = strings.Replace(key, "Part", "", 1)
parts := strings.Split(key, "_")
parts[1], parts[2] = parts[2], parts[1]
key = strings.Join(parts, "_")
return key + " " + invdata
}

func GibbedToDm(i Item) DigitalMarineItem {
m := DigitalMarineItem{}
m.Manufacturer = GetPartSuffix(i.Manufacturer)
m.Level = i.Level
m.CopyType = "item"
m.Balance = GetPartSuffix(i.Balance)
btik := GetBtik()
key := btik[strings.ToLower(i.Balance)]
m.Blueprint = GetBlueprint(key, GetPartSuffix(i.InvData))
for _, part := range i.Parts {
p := GetPartSuffix(part)
found := false
for i2, name := range m.ComponentNames {
if name == p {
m.Components = append(m.Components, i2)
found = true
}
}
if !found {
m.ComponentNames = append(m.ComponentNames, p)
m.Components = append(m.Components, len(m.ComponentNames)-1)
}
}
return m
}

func DmKeyToInvKey(key string, assets []string) string {
for _, a := range assets {
if strings.HasSuffix(a, key) {
return a
}
}
return ""
}

func GetPartSuffix(part string) string {
p := strings.Split(part, "/")
return p[len(p)-1]
}
64 changes: 64 additions & 0 deletions internal/server/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package server

import (
"encoding/base64"
"encoding/json"
"strings"

"github.com/cfi2017/bl3-save/internal/item"
"github.com/cfi2017/bl3-save/pkg/pb"
"github.com/gin-gonic/gin"
)

func convertItem(c *gin.Context) {

var request struct {
Base64 string `json:"base64"`
}
request.Base64 = strings.TrimPrefix(request.Base64, "bl3(")
request.Base64 = strings.TrimSuffix(request.Base64, ")")
err := c.BindJSON(&request)
if err != nil {
c.AbortWithStatusJSON(500, err)
return
}
bs, err := base64.StdEncoding.DecodeString(request.Base64)
if err != nil {
c.AbortWithStatusJSON(500, err)
return
}
var dmi item.DigitalMarineItem
err = json.Unmarshal(bs, &dmi)
if err != nil {
// try deserializing item
i, err := item.Deserialize(bs)
if err != nil {
c.AbortWithStatusJSON(500, err)
return
}
i.Wrapper = &pb.OakInventoryItemSaveGameData{
ItemSerialNumber: bs,
PickupOrderIndex: 200,
Flags: 3,
WeaponSkinPath: "",
DevelopmentSaveData: nil,
}
c.JSON(200, &i)
return
}
i := item.DmToGibbed(dmi)
bs, err = item.Serialize(i, 0) // encrypt with 0 seed
if err != nil {
c.AbortWithStatusJSON(500, err)
return
}
i.Wrapper = &pb.OakInventoryItemSaveGameData{
ItemSerialNumber: bs,
PickupOrderIndex: 200,
Flags: 3,
WeaponSkinPath: "",
DevelopmentSaveData: nil,
}
c.JSON(200, &i)
return
}
2 changes: 2 additions & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,7 @@ func Start(opts Options) error {
r.GET("/characters/:id/items", getItemsRequest)
r.POST("/characters/:id/items", updateItemsRequest)

r.GET("/convert", convertItem)

return r.Run(":5050")
}

0 comments on commit c274f1d

Please sign in to comment.