Files
GaCy-GO/internal/common/persistent_patch.go
T
2026-10-28 18:54:43 +07:00

35 lines
808 B
Go

package common
import (
"encoding/json"
"fmt"
)
// Patch selected top-level fields against the latest file, under the same lock
// used by game saves. Raw JSON retains 64-bit scores and unknown save sections.
func PatchPersistentFields(patch map[string]json.RawMessage) error {
if patch == nil {
return fmt.Errorf("persistent patch must be a JSON object")
}
for _, raw := range patch {
if !json.Valid(raw) {
return fmt.Errorf("invalid persistent field JSON")
}
}
persistentWriteMu.Lock()
defer persistentWriteMu.Unlock()
unlock, err := lockPersistentFile()
if err != nil {
return err
}
defer unlock()
root, err := readCurrencyWarsPersistentRoot()
if err != nil {
return err
}
for key, value := range patch {
root[key] = value
}
return writeJSONAtomic("persistent", root)
}