From 80cf46fc98f9345eb49e19b3560bb4040021e017 Mon Sep 17 00:00:00 2001 From: Carlo Field Date: Sun, 19 Apr 2020 14:06:55 +0200 Subject: [PATCH] fix a bug with save files not closing properly --- internal/server/character.go | 1 + internal/server/profile.go | 67 ++++++++++++++++++++++++++++++++++++ internal/server/server.go | 2 ++ main.go | 2 +- 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/internal/server/character.go b/internal/server/character.go index 7396815..b28e0f5 100644 --- a/internal/server/character.go +++ b/internal/server/character.go @@ -191,6 +191,7 @@ func updateItemsRequest(c *gin.Context) { c.AbortWithStatus(500) return } + defer f.Close() character.Serialize(f, s, char) c.Status(204) return diff --git a/internal/server/profile.go b/internal/server/profile.go index fc63542..1d4d9a9 100644 --- a/internal/server/profile.go +++ b/internal/server/profile.go @@ -1,8 +1,11 @@ package server import ( + "encoding/base64" + "log" "os" + "github.com/cfi2017/bl3-save/internal/item" "github.com/cfi2017/bl3-save/internal/shared" "github.com/cfi2017/bl3-save/pkg/pb" "github.com/cfi2017/bl3-save/pkg/profile" @@ -45,3 +48,67 @@ func updateProfile(c *gin.Context) { return } + +func getBankRequest(c *gin.Context) { + f, err := os.Open(pwd + "/profile.sav") + if err != nil { + c.AbortWithStatus(500) + return + } + defer f.Close() + _, p := profile.Deserialize(f) + items := make([]item.Item, 0) + for _, data := range p.BankInventoryList { + d := make([]byte, len(data)) + copy(d, data) + i, err := item.Deserialize(d) + if err != nil { + log.Println(err) + log.Println(base64.StdEncoding.EncodeToString(data)) + // c.AbortWithStatus(500) + // return + } + i.Wrapper = &pb.OakInventoryItemSaveGameData{ + ItemSerialNumber: data, + } + items = append(items, i) + } + c.JSON(200, &items) + return +} + +func updateBankRequest(c *gin.Context) { + f, err := os.Open(pwd + "/profile.sav") + if err != nil { + c.AbortWithStatus(500) + return + } + err = f.Close() + s, p := profile.Deserialize(f) + if err != nil { + c.AbortWithStatus(500) + return + } + var items []item.Item + err = c.BindJSON(&items) + if err != nil { + c.AbortWithStatus(500) + return + } + backup(pwd, "profile") + pba, err := itemsToPBArray(items) + p.BankInventoryList = make([][]byte, len(pba)) + for i := range pba { + p.BankInventoryList[i] = pba[i].ItemSerialNumber + } + f, err = os.Create(pwd + "/profile.sav") + if err != nil { + c.AbortWithStatus(500) + return + } + defer f.Close() + profile.Serialize(f, s, p) + c.Status(204) + return + +} diff --git a/internal/server/server.go b/internal/server/server.go index 3981fed..296e312 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -75,6 +75,8 @@ func Start(opts Options) error { r.GET("/profile", getProfile) r.POST("/profile", updateProfile) + r.GET("/profile/bank", getBankRequest) + r.POST("/profile/bank", updateBankRequest) r.GET("/characters", listCharacters) r.GET("/characters/:id", getCharacterRequest) diff --git a/main.go b/main.go index 281e5eb..02517f4 100644 --- a/main.go +++ b/main.go @@ -22,7 +22,7 @@ import ( ) var ( - version = "v0.0.0-dev" + version = "v100.0.0" commit = "" date = "" builtBy = "local"