Reuse zstandard encoder

oauth2
maru 2024-04-22 18:20:58 -04:00
parent 697c5335d7
commit 1de3ce72e4
No known key found for this signature in database
GPG Key ID: 37689350E9CD0F0D
1 changed files with 5 additions and 10 deletions

View File

@ -13,6 +13,8 @@ import (
"github.com/pagefaultgames/pokerogue-server/defs"
)
var zstdEncoder, _ = zstd.NewWriter(nil)
// /savedata/update - update save data
func Update(uuid []byte, slot int, save any) error {
err := db.UpdateAccountLastActivity(uuid)
@ -29,13 +31,6 @@ func Update(uuid []byte, slot int, save any) error {
var filename string
var buf bytes.Buffer
zstdEncoder, err := zstd.NewWriter(&buf)
if err != nil {
return fmt.Errorf("failed to create zstd encoder: %s", err)
}
defer zstdEncoder.Close()
switch save := save.(type) {
case defs.SystemSaveData: // System
if save.TrainerId == 0 && save.SecretId == 0 {
@ -53,7 +48,7 @@ func Update(uuid []byte, slot int, save any) error {
filename = "system"
err = gob.NewEncoder(zstdEncoder).Encode(save)
err = gob.NewEncoder(&buf).Encode(save)
if err != nil {
return fmt.Errorf("failed to serialize save: %s", err)
}
@ -69,7 +64,7 @@ func Update(uuid []byte, slot int, save any) error {
filename += strconv.Itoa(slot)
}
err = gob.NewEncoder(zstdEncoder).Encode(save)
err = gob.NewEncoder(&buf).Encode(save)
if err != nil {
return fmt.Errorf("failed to serialize save: %s", err)
}
@ -81,7 +76,7 @@ func Update(uuid []byte, slot int, save any) error {
return fmt.Errorf("tried to write empty save file")
}
err = os.WriteFile(fmt.Sprintf("userdata/%x/%s.pzs", uuid, filename), buf.Bytes(), 0644)
err = os.WriteFile(fmt.Sprintf("userdata/%x/%s.pzs", uuid, filename), zstdEncoder.EncodeAll(buf.Bytes(), nil), 0644)
if err != nil {
return fmt.Errorf("failed to write save to disk: %s", err)
}