53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package common
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
// nil resets an override. Merge only the edited item keys, so a simultaneous
|
|
// game-side gacha grant cannot be erased by an SDK inventory edit.
|
|
func PatchMaterialOverrides(updates map[uint32]*uint32) (map[uint32]uint32, error) {
|
|
for id := range updates {
|
|
if id == 0 {
|
|
return nil, fmt.Errorf("invalid material ID")
|
|
}
|
|
}
|
|
persistentWriteMu.Lock()
|
|
defer persistentWriteMu.Unlock()
|
|
unlock, err := lockPersistentFile()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer unlock()
|
|
raw, err := readResourceFile("freesr-data.json")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var root map[string]json.RawMessage
|
|
if err = json.Unmarshal(raw, &root); err != nil || root == nil {
|
|
return nil, fmt.Errorf("invalid FreeSR import")
|
|
}
|
|
materials := map[uint32]uint32{}
|
|
if value, exists := root["materials"]; exists {
|
|
if err = json.Unmarshal(value, &materials); err != nil {
|
|
return nil, err
|
|
}
|
|
if materials == nil {
|
|
materials = map[uint32]uint32{}
|
|
}
|
|
}
|
|
for id, count := range updates {
|
|
if count == nil {
|
|
delete(materials, id)
|
|
} else {
|
|
materials[id] = *count
|
|
}
|
|
}
|
|
root["materials"], err = json.Marshal(materials)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return materials, writeJSONAtomic("freesr-data.json", root)
|
|
}
|