Use WriteFile instead of stream writing to files
parent
8f51cd826c
commit
b00ed4c0b9
|
@ -3,6 +3,7 @@ package account
|
|||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"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)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package savedata
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"log"
|
||||
|
@ -19,6 +20,15 @@ func Update(uuid []byte, slot int, save any) error {
|
|||
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) {
|
||||
case defs.SystemSaveData: // System
|
||||
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)
|
||||
}
|
||||
|
||||
err = os.MkdirAll(fmt.Sprintf("userdata/%x", uuid), 0755)
|
||||
if err != nil && !os.IsExist(err) {
|
||||
return fmt.Errorf("failed to create userdata folder: %s", err)
|
||||
}
|
||||
filename = "system"
|
||||
|
||||
file, err := os.OpenFile(fmt.Sprintf("userdata/%x/system.pzs", uuid), 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)
|
||||
zstdEncoder, err := zstd.NewWriter(&buf)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
|
||||
fileName := "session"
|
||||
filename = "session"
|
||||
if slot != 0 {
|
||||
fileName += strconv.Itoa(slot)
|
||||
filename += strconv.Itoa(slot)
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
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)
|
||||
zstdEncoder, err := zstd.NewWriter(&buf)
|
||||
if err != nil {
|
||||
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")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue