Use WriteFile instead of stream writing to files

pull/4/head
maru 8 months ago
parent 8f51cd826c
commit b00ed4c0b9
No known key found for this signature in database
GPG Key ID: 37689350E9CD0F0D

@ -3,6 +3,7 @@ package account
import ( import (
"crypto/rand" "crypto/rand"
"fmt" "fmt"
"os"
"github.com/pagefaultgames/pokerogue-server/db" "github.com/pagefaultgames/pokerogue-server/db"
) )
@ -34,5 +35,10 @@ func Register(username, password string) error {
return fmt.Errorf("failed to add account record: %s", err) return fmt.Errorf("failed to add account record: %s", err)
} }
err = os.MkdirAll(fmt.Sprintf("userdata/%x", uuid), 0755)
if err != nil && !os.IsExist(err) {
return fmt.Errorf(fmt.Sprintf("failed to create userdata folder: %s", err))
}
return nil return nil
} }

@ -1,6 +1,7 @@
package savedata package savedata
import ( import (
"bytes"
"encoding/gob" "encoding/gob"
"fmt" "fmt"
"log" "log"
@ -19,6 +20,15 @@ func Update(uuid []byte, slot int, save any) error {
log.Print("failed to update account last activity") log.Print("failed to update account last activity")
} }
// ideally should have been done at account creation
err = os.MkdirAll(fmt.Sprintf("userdata/%x", uuid), 0755)
if err != nil && !os.IsExist(err) {
return fmt.Errorf(fmt.Sprintf("failed to create userdata folder: %s", err))
}
var filename string
var buf bytes.Buffer
switch save := save.(type) { switch save := save.(type) {
case defs.SystemSaveData: // System case defs.SystemSaveData: // System
if save.TrainerId == 0 && save.SecretId == 0 { if save.TrainerId == 0 && save.SecretId == 0 {
@ -34,19 +44,9 @@ func Update(uuid []byte, slot int, save any) error {
return fmt.Errorf("failed to update account stats: %s", err) return fmt.Errorf("failed to update account stats: %s", err)
} }
err = os.MkdirAll(fmt.Sprintf("userdata/%x", uuid), 0755) filename = "system"
if err != nil && !os.IsExist(err) {
return fmt.Errorf("failed to create userdata folder: %s", err)
}
file, err := os.OpenFile(fmt.Sprintf("userdata/%x/system.pzs", uuid), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) zstdEncoder, err := zstd.NewWriter(&buf)
if err != nil {
return fmt.Errorf("failed to open save file for writing: %s", err)
}
defer file.Close()
zstdEncoder, err := zstd.NewWriter(file)
if err != nil { if err != nil {
return fmt.Errorf("failed to create zstd encoder: %s", err) return fmt.Errorf("failed to create zstd encoder: %s", err)
} }
@ -64,24 +64,12 @@ func Update(uuid []byte, slot int, save any) error {
return fmt.Errorf("slot id %d out of range", slot) return fmt.Errorf("slot id %d out of range", slot)
} }
fileName := "session" filename = "session"
if slot != 0 { if slot != 0 {
fileName += strconv.Itoa(slot) filename += strconv.Itoa(slot)
} }
err = os.MkdirAll(fmt.Sprintf("userdata/%x", uuid), 0755) zstdEncoder, err := zstd.NewWriter(&buf)
if err != nil && !os.IsExist(err) {
return fmt.Errorf(fmt.Sprintf("failed to create userdata folder: %s", err))
}
file, err := os.OpenFile(fmt.Sprintf("userdata/%x/%s.pzs", uuid, fileName), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("failed to open save file for writing: %s", err)
}
defer file.Close()
zstdEncoder, err := zstd.NewWriter(file)
if err != nil { if err != nil {
return fmt.Errorf("failed to create zstd encoder: %s", err) return fmt.Errorf("failed to create zstd encoder: %s", err)
} }
@ -96,5 +84,10 @@ func Update(uuid []byte, slot int, save any) error {
return fmt.Errorf("invalid data type") return fmt.Errorf("invalid data type")
} }
err = os.WriteFile(fmt.Sprintf("userdata/%x/%s.pzs", uuid, filename), buf.Bytes(), 0644)
if err != nil {
return fmt.Errorf("failed to write save to disk: %s", err)
}
return nil return nil
} }

Loading…
Cancel
Save