mirror of
https://github.com/pagefaultgames/rogueserver.git
synced 2025-04-03 19:47:14 +08:00
fix import cycle
This commit is contained in:
parent
d39d2d4e7f
commit
40a1ce97e0
@ -18,71 +18,9 @@
|
|||||||
package savedata
|
package savedata
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/gob"
|
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/klauspost/compress/zstd"
|
|
||||||
"github.com/pagefaultgames/rogueserver/defs"
|
"github.com/pagefaultgames/rogueserver/defs"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LegacyReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
|
|
||||||
var system defs.SystemSaveData
|
|
||||||
|
|
||||||
file, err := os.Open("userdata/" + hex.EncodeToString(uuid) + "/system.pzs")
|
|
||||||
if err != nil {
|
|
||||||
return system, fmt.Errorf("failed to open save file for reading: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
zstdDecoder, err := zstd.NewReader(file)
|
|
||||||
if err != nil {
|
|
||||||
return system, fmt.Errorf("failed to create zstd decoder: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer zstdDecoder.Close()
|
|
||||||
|
|
||||||
err = gob.NewDecoder(zstdDecoder).Decode(&system)
|
|
||||||
if err != nil {
|
|
||||||
return system, fmt.Errorf("failed to deserialize save: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return system, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func LegacyReadSessionSaveData(uuid []byte, slotID int) (defs.SessionSaveData, error) {
|
|
||||||
var session defs.SessionSaveData
|
|
||||||
|
|
||||||
fileName := "session"
|
|
||||||
if slotID != 0 {
|
|
||||||
fileName += strconv.Itoa(slotID)
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := os.Open(fmt.Sprintf("userdata/%s/%s.pzs", hex.EncodeToString(uuid), fileName))
|
|
||||||
if err != nil {
|
|
||||||
return session, fmt.Errorf("failed to open save file for reading: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
zstdDecoder, err := zstd.NewReader(file)
|
|
||||||
if err != nil {
|
|
||||||
return session, fmt.Errorf("failed to create zstd decoder: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer zstdDecoder.Close()
|
|
||||||
|
|
||||||
err = gob.NewDecoder(zstdDecoder).Decode(&session)
|
|
||||||
if err != nil {
|
|
||||||
return session, fmt.Errorf("failed to deserialize save: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return session, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func validateSessionCompleted(session defs.SessionSaveData) bool {
|
func validateSessionCompleted(session defs.SessionSaveData) bool {
|
||||||
switch session.GameMode {
|
switch session.GameMode {
|
||||||
case 0:
|
case 0:
|
||||||
|
5
db/db.go
5
db/db.go
@ -22,7 +22,6 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/pagefaultgames/rogueserver/api/savedata"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
@ -71,7 +70,7 @@ func Init(username, password, protocol, address, database string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// store new system data
|
// store new system data
|
||||||
systemData, err := savedata.LegacyReadSystemSaveData(uuid)
|
systemData, err := LegacyReadSystemSaveData(uuid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to read system save data for %v: %s", uuidString, err)
|
log.Printf("failed to read system save data for %v: %s", uuidString, err)
|
||||||
continue
|
continue
|
||||||
@ -90,7 +89,7 @@ func Init(username, password, protocol, address, database string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
sessionData, err := savedata.LegacyReadSessionSaveData(uuid, i)
|
sessionData, err := LegacyReadSessionSaveData(uuid, i)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to read session save data %v for %v: %s", i, uuidString, err)
|
log.Printf("failed to read session save data %v for %v: %s", i, uuidString, err)
|
||||||
continue
|
continue
|
||||||
|
84
db/legacy.go
Normal file
84
db/legacy.go
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
Copyright (C) 2024 Pagefault Games
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Affero General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Affero General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/gob"
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/klauspost/compress/zstd"
|
||||||
|
"github.com/pagefaultgames/rogueserver/defs"
|
||||||
|
)
|
||||||
|
|
||||||
|
func LegacyReadSystemSaveData(uuid []byte) (defs.SystemSaveData, error) {
|
||||||
|
var system defs.SystemSaveData
|
||||||
|
|
||||||
|
file, err := os.Open("userdata/" + hex.EncodeToString(uuid) + "/system.pzs")
|
||||||
|
if err != nil {
|
||||||
|
return system, fmt.Errorf("failed to open save file for reading: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
zstdDecoder, err := zstd.NewReader(file)
|
||||||
|
if err != nil {
|
||||||
|
return system, fmt.Errorf("failed to create zstd decoder: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer zstdDecoder.Close()
|
||||||
|
|
||||||
|
err = gob.NewDecoder(zstdDecoder).Decode(&system)
|
||||||
|
if err != nil {
|
||||||
|
return system, fmt.Errorf("failed to deserialize save: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return system, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func LegacyReadSessionSaveData(uuid []byte, slotID int) (defs.SessionSaveData, error) {
|
||||||
|
var session defs.SessionSaveData
|
||||||
|
|
||||||
|
fileName := "session"
|
||||||
|
if slotID != 0 {
|
||||||
|
fileName += strconv.Itoa(slotID)
|
||||||
|
}
|
||||||
|
|
||||||
|
file, err := os.Open(fmt.Sprintf("userdata/%s/%s.pzs", hex.EncodeToString(uuid), fileName))
|
||||||
|
if err != nil {
|
||||||
|
return session, fmt.Errorf("failed to open save file for reading: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
zstdDecoder, err := zstd.NewReader(file)
|
||||||
|
if err != nil {
|
||||||
|
return session, fmt.Errorf("failed to create zstd decoder: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer zstdDecoder.Close()
|
||||||
|
|
||||||
|
err = gob.NewDecoder(zstdDecoder).Decode(&session)
|
||||||
|
if err != nil {
|
||||||
|
return session, fmt.Errorf("failed to deserialize save: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return session, nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user