Prevent writing empty save files

oauth2
maru 2024-04-22 18:06:32 -04:00
parent b00ed4c0b9
commit 697c5335d7
No known key found for this signature in database
GPG Key ID: 37689350E9CD0F0D
1 changed files with 11 additions and 14 deletions

View File

@ -29,6 +29,13 @@ 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 {
@ -46,13 +53,6 @@ func Update(uuid []byte, slot int, save any) error {
filename = "system"
zstdEncoder, err := zstd.NewWriter(&buf)
if err != nil {
return fmt.Errorf("failed to create zstd encoder: %s", err)
}
defer zstdEncoder.Close()
err = gob.NewEncoder(zstdEncoder).Encode(save)
if err != nil {
return fmt.Errorf("failed to serialize save: %s", err)
@ -69,13 +69,6 @@ func Update(uuid []byte, slot int, save any) error {
filename += strconv.Itoa(slot)
}
zstdEncoder, err := zstd.NewWriter(&buf)
if err != nil {
return fmt.Errorf("failed to create zstd encoder: %s", err)
}
defer zstdEncoder.Close()
err = gob.NewEncoder(zstdEncoder).Encode(save)
if err != nil {
return fmt.Errorf("failed to serialize save: %s", err)
@ -84,6 +77,10 @@ func Update(uuid []byte, slot int, save any) error {
return fmt.Errorf("invalid data type")
}
if buf.Len() == 0 {
return fmt.Errorf("tried to write empty save file")
}
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)