client io architecture
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"galaxy/util"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
const (
|
||||
// Name of the file under the storage's root where [model.State] is stored.
|
||||
stateFileName = "state.dat"
|
||||
|
||||
// Suffix of a Game's file inder the storage's root where [model.GameData] is stored.
|
||||
gameDataFileSuffix = ".dat"
|
||||
)
|
||||
|
||||
type storage struct {
|
||||
root string
|
||||
}
|
||||
|
||||
// NewStorage returns implementation of the "galaxy/client.Storage" interface
|
||||
// or nil with a non-nil error when filesystem storage initialisation failed.
|
||||
func NewStorage(rootPath string) (*storage, error) {
|
||||
ok, err := util.Writable(rootPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !ok {
|
||||
return nil, errors.New("user does not have write permissions to the storage root")
|
||||
}
|
||||
s := &storage{
|
||||
root: rootPath,
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// StateFilePath returns client's state file path relative to the root,
|
||||
// file name and extension are pre-defined constant.
|
||||
func StateFilePath(root string) string {
|
||||
return filepath.Join(root, stateFileName)
|
||||
}
|
||||
|
||||
// GameDataPath returns game's data file path relative to the root,
|
||||
// data file name is GameID string representation and extension is a pre-defined constant.
|
||||
func GameDataFilePath(root string, id fmt.Stringer) string {
|
||||
return filepath.Join(root, id.String()) + gameDataFileSuffix
|
||||
}
|
||||
Reference in New Issue
Block a user