first commit

This commit is contained in:
2025-05-10 17:42:25 +02:00
commit 7827b9c0cc
8 changed files with 337 additions and 0 deletions

86
pkg/game/game.go Normal file
View File

@@ -0,0 +1,86 @@
package game
import (
"cloudsave/pkg/tools/id"
"encoding/json"
"fmt"
"os"
"path/filepath"
)
type (
Metadata struct {
ID string `json:"id"`
Name string `json:"name"`
Path string `json:"path"`
}
)
var (
roaming string
datastorepath string
)
func init() {
var err error
roaming, err = os.UserConfigDir()
if err != nil {
panic("failed to get user config path: " + err.Error())
}
datastorepath = filepath.Join(roaming, "cloudsave", "data")
err = os.MkdirAll(datastorepath, 0740)
if err != nil {
panic("cannot make the datastore:" + err.Error())
}
}
func Add(name, path string) (Metadata, error) {
m := Metadata{
ID: id.New(),
Name: name,
Path: path,
}
f, err := os.OpenFile(filepath.Join(datastorepath, m.ID, "metadata.json"), os.O_CREATE|os.O_WRONLY, 0740)
if err != nil {
return Metadata{}, fmt.Errorf("cannot open the metadata file in the datastore: %w", err)
}
defer f.Close()
e := json.NewEncoder(f)
err = e.Encode(m)
if err != nil {
return Metadata{}, fmt.Errorf("cannot write into the metadata file in the datastore: %w", err)
}
return m, nil
}
func All() ([]Metadata, error) {
ds, err := os.ReadDir(datastorepath)
if err != nil {
return nil, fmt.Errorf("cannot open the datastore: %w", err)
}
var datastore []Metadata
for _, d := range ds {
content, err := os.ReadFile(filepath.Join(datastorepath, d.Name(), "metadata.json"))
if err != nil {
continue
}
var m Metadata
err = json.Unmarshal(content, &m)
if err != nil {
return nil, fmt.Errorf("corrupted datastore: failed to parse %s/metadata.json: %w", d.Name(), err)
}
datastore = append(datastore, m)
}
return datastore, nil
}
func DatastorePath() string {
return datastorepath
}