Skip to content

Commit

Permalink
fix a bug with save files not closing properly
Browse files Browse the repository at this point in the history
  • Loading branch information
cfi2017 committed Apr 19, 2020
1 parent 9b0df43 commit 80cf46f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions internal/server/character.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
67 changes: 67 additions & 0 deletions internal/server/profile.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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

}
2 changes: 2 additions & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

var (
version = "v0.0.0-dev"
version = "v100.0.0"
commit = ""
date = ""
builtBy = "local"
Expand Down

0 comments on commit 80cf46f

Please sign in to comment.