From 40a1ce97e0249f32b06ae20bbbca24a0866f25e2 Mon Sep 17 00:00:00 2001 From: Up Date: Tue, 7 May 2024 22:41:53 +0200 Subject: [PATCH] fix import cycle --- api/savedata/common.go | 62 ------------------------------- db/db.go | 5 +-- db/legacy.go | 84 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 65 deletions(-) create mode 100644 db/legacy.go diff --git a/api/savedata/common.go b/api/savedata/common.go index 72e1292..39e113a 100644 --- a/api/savedata/common.go +++ b/api/savedata/common.go @@ -18,71 +18,9 @@ package savedata 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 -} - func validateSessionCompleted(session defs.SessionSaveData) bool { switch session.GameMode { case 0: diff --git a/db/db.go b/db/db.go index 55f074c..150e169 100644 --- a/db/db.go +++ b/db/db.go @@ -22,7 +22,6 @@ import ( "encoding/hex" "fmt" _ "github.com/go-sql-driver/mysql" - "github.com/pagefaultgames/rogueserver/api/savedata" "log" "os" ) @@ -71,7 +70,7 @@ func Init(username, password, protocol, address, database string) error { } // store new system data - systemData, err := savedata.LegacyReadSystemSaveData(uuid) + systemData, err := LegacyReadSystemSaveData(uuid) if err != nil { log.Printf("failed to read system save data for %v: %s", uuidString, err) continue @@ -90,7 +89,7 @@ func Init(username, password, protocol, address, database string) error { } for i := 0; i < 5; i++ { - sessionData, err := savedata.LegacyReadSessionSaveData(uuid, i) + sessionData, err := LegacyReadSessionSaveData(uuid, i) if err != nil { log.Printf("failed to read session save data %v for %v: %s", i, uuidString, err) continue diff --git a/db/legacy.go b/db/legacy.go new file mode 100644 index 0000000..f9b5848 --- /dev/null +++ b/db/legacy.go @@ -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 . +*/ + +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 +}