From c274f1d616d0609f29d90e5fc15dd44343b81f03 Mon Sep 17 00:00:00 2001 From: Carlo Field Date: Sat, 18 Apr 2020 16:13:57 +0200 Subject: [PATCH] add conversion endpoint --- cmd/convert.go | 83 ++------------------------------------ internal/item/convert.go | 77 +++++++++++++++++++++++++++++++++++ internal/server/convert.go | 64 +++++++++++++++++++++++++++++ internal/server/server.go | 2 + 4 files changed, 147 insertions(+), 79 deletions(-) create mode 100644 internal/item/convert.go create mode 100644 internal/server/convert.go diff --git a/cmd/convert.go b/cmd/convert.go index 5bf682f..f3a9802 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -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", @@ -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 @@ -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. diff --git a/internal/item/convert.go b/internal/item/convert.go new file mode 100644 index 0000000..827fb3b --- /dev/null +++ b/internal/item/convert.go @@ -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] +} diff --git a/internal/server/convert.go b/internal/server/convert.go new file mode 100644 index 0000000..2c879b1 --- /dev/null +++ b/internal/server/convert.go @@ -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 +} diff --git a/internal/server/server.go b/internal/server/server.go index d332aca..208d25b 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -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") }