Prevent writing empty save files

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

@ -29,6 +29,13 @@ func Update(uuid []byte, slot int, save any) error {
var filename string var filename string
var buf bytes.Buffer 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) { 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 {
@ -46,13 +53,6 @@ func Update(uuid []byte, slot int, save any) error {
filename = "system" 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) err = gob.NewEncoder(zstdEncoder).Encode(save)
if err != nil { if err != nil {
return fmt.Errorf("failed to serialize save: %s", err) 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) 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) err = gob.NewEncoder(zstdEncoder).Encode(save)
if err != nil { if err != nil {
return fmt.Errorf("failed to serialize save: %s", err) 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") 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) err = os.WriteFile(fmt.Sprintf("userdata/%x/%s.pzs", uuid, filename), buf.Bytes(), 0644)
if err != nil { if err != nil {
return fmt.Errorf("failed to write save to disk: %s", err) return fmt.Errorf("failed to write save to disk: %s", err)

Loading…
Cancel
Save