|
|
@ -60,53 +60,53 @@ func ReadSeedCompleted(uuid []byte, seed string) (bool, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func ReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
|
|
|
|
func ReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
|
|
|
|
// get and return save from S3
|
|
|
|
var system defs.SystemSaveData
|
|
|
|
system, err := GetSystemSaveFromS3(uuid)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
|
|
return system, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// otherwise look in database and try to move it
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
var data []byte
|
|
|
|
err = handle.QueryRow("SELECT data FROM systemSaveData WHERE uuid = ?", uuid).Scan(&data)
|
|
|
|
err := handle.QueryRow("SELECT data FROM systemSaveData WHERE uuid = ?", uuid).Scan(&data)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return system, err
|
|
|
|
return system, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dec, err := zstd.NewReader(nil)
|
|
|
|
zr, err := zstd.NewReader(bytes.NewReader(data))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return system, err
|
|
|
|
return system, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
defer dec.Close()
|
|
|
|
defer zr.Close()
|
|
|
|
|
|
|
|
|
|
|
|
decompressed, err := dec.DecodeAll(data, nil)
|
|
|
|
err = gob.NewDecoder(zr).Decode(&system)
|
|
|
|
if err == nil {
|
|
|
|
if err != nil {
|
|
|
|
// replace if it worked, otherwise use the original data
|
|
|
|
return system, err
|
|
|
|
data = decompressed
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = gob.NewDecoder(bytes.NewReader(data)).Decode(&system)
|
|
|
|
return system, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func StoreSystemSaveData(uuid []byte, data defs.SystemSaveData) error {
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
zw, err := zstd.NewWriter(buf)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return system, err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// put it in S3
|
|
|
|
defer zw.Close()
|
|
|
|
err = StoreSystemSaveData(uuid, system)
|
|
|
|
|
|
|
|
|
|
|
|
err = gob.NewEncoder(zw).Encode(data)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return system, err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// delete the one in db
|
|
|
|
_, err = handle.Exec("REPLACE INTO systemSaveData (uuid, data, timestamp) VALUES (?, ?, UTC_TIMESTAMP())", uuid, buf.Bytes())
|
|
|
|
err = DeleteSystemSaveData(uuid)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return system, err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return system, nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func StoreSystemSaveData(uuid []byte, data defs.SystemSaveData) error {
|
|
|
|
func StoreSystemSaveDataS3(uuid []byte, data defs.SystemSaveData) error {
|
|
|
|
cfg, _ := config.LoadDefaultConfig(context.TODO())
|
|
|
|
cfg, _ := config.LoadDefaultConfig(context.TODO())
|
|
|
|
|
|
|
|
|
|
|
|
client := s3.NewFromConfig(cfg, func(o *s3.Options) {
|
|
|
|
client := s3.NewFromConfig(cfg, func(o *s3.Options) {
|
|
|
@ -118,7 +118,9 @@ func StoreSystemSaveData(uuid []byte, data defs.SystemSaveData) error {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
json, err := json.Marshal(data)
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = json.NewEncoder(buf).Encode(data)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -126,7 +128,7 @@ func StoreSystemSaveData(uuid []byte, data defs.SystemSaveData) error {
|
|
|
|
_, err = client.PutObject(context.Background(), &s3.PutObjectInput{
|
|
|
|
_, err = client.PutObject(context.Background(), &s3.PutObjectInput{
|
|
|
|
Bucket: aws.String(os.Getenv("S3_SYSTEM_BUCKET_NAME")),
|
|
|
|
Bucket: aws.String(os.Getenv("S3_SYSTEM_BUCKET_NAME")),
|
|
|
|
Key: aws.String(username),
|
|
|
|
Key: aws.String(username),
|
|
|
|
Body: bytes.NewReader(json),
|
|
|
|
Body: buf,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
@ -153,20 +155,14 @@ func ReadSessionSaveData(uuid []byte, slot int) (defs.SessionSaveData, error) {
|
|
|
|
return session, err
|
|
|
|
return session, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dec, err := zstd.NewReader(nil)
|
|
|
|
zr, err := zstd.NewReader(bytes.NewReader(data))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return session, err
|
|
|
|
return session, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
defer dec.Close()
|
|
|
|
defer zr.Close()
|
|
|
|
|
|
|
|
|
|
|
|
decompressed, err := dec.DecodeAll(data, nil)
|
|
|
|
err = gob.NewDecoder(zr).Decode(&session)
|
|
|
|
if err == nil {
|
|
|
|
|
|
|
|
// replace if it worked, otherwise use the original data
|
|
|
|
|
|
|
|
data = decompressed
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = gob.NewDecoder(bytes.NewReader(data)).Decode(&session)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return session, err
|
|
|
|
return session, err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -185,20 +181,21 @@ func GetLatestSessionSaveDataSlot(uuid []byte) (int, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func StoreSessionSaveData(uuid []byte, data defs.SessionSaveData, slot int) error {
|
|
|
|
func StoreSessionSaveData(uuid []byte, data defs.SessionSaveData, slot int) error {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err := gob.NewEncoder(&buf).Encode(data)
|
|
|
|
|
|
|
|
|
|
|
|
zw, err := zstd.NewWriter(buf)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
enc, err := zstd.NewWriter(nil)
|
|
|
|
defer zw.Close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = gob.NewEncoder(zw).Encode(data)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
defer enc.Close()
|
|
|
|
_, err = handle.Exec("REPLACE INTO sessionSaveData (uuid, slot, data, timestamp) VALUES (?, ?, ?, UTC_TIMESTAMP())", uuid, slot, buf.Bytes())
|
|
|
|
|
|
|
|
|
|
|
|
_, err = handle.Exec("REPLACE INTO sessionSaveData (uuid, slot, data, timestamp) VALUES (?, ?, ?, UTC_TIMESTAMP())", uuid, slot, enc.EncodeAll(buf.Bytes(), nil))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -250,13 +247,12 @@ func GetSystemSaveFromS3(uuid []byte) (defs.SystemSaveData, error) {
|
|
|
|
return system, err
|
|
|
|
return system, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var session defs.SystemSaveData
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&system)
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&session)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return system, err
|
|
|
|
return system, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return session, nil
|
|
|
|
return system, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetLocalSystemAccounts() ([][]byte, error) {
|
|
|
|
func GetLocalSystemAccounts() ([][]byte, error) {
|
|
|
|