diff --git a/db/db.go b/db/db.go index 998af39..c1f1a7c 100644 --- a/db/db.go +++ b/db/db.go @@ -19,10 +19,8 @@ package db import ( "database/sql" - "encoding/hex" "fmt" "log" - "os" "time" _ "github.com/go-sql-driver/mysql" @@ -55,7 +53,7 @@ func Init(username, password, protocol, address, database string) error { err = setupDb(tx) if err != nil { - _ = tx.Rollback() + tx.Rollback() log.Fatal(err) } @@ -64,82 +62,6 @@ func Init(username, password, protocol, address, database string) error { log.Fatal(err) } - // TODO temp code - _, err = os.Stat("userdata") - if err != nil { - if !os.IsNotExist(err) { // not found, do not migrate - log.Fatalf("failed to stat userdata directory: %s", err) - } - - return nil - } - - entries, err := os.ReadDir("userdata") - if err != nil { - log.Fatal(err) - } - - for _, entry := range entries { - if !entry.IsDir() { - continue - } - - uuidString := entry.Name() - uuid, err := hex.DecodeString(uuidString) - if err != nil { - log.Printf("failed to decode uuid: %s", err) - continue - } - - var count int - err = handle.QueryRow("SELECT COUNT(*) FROM systemSaveData WHERE uuid = ?", uuid).Scan(&count) - if err != nil || count != 0 { - continue - } - - // store new system data - systemData, err := LegacyReadSystemSaveData(uuid) - if err != nil { - log.Printf("failed to read system save data for %v: %s", uuidString, err) - continue - } - - err = StoreSystemSaveData(uuid, systemData) - if err != nil { - log.Fatalf("failed to store system save data for %v: %s\n", uuidString, err) - } - - // delete old system data - err = os.Remove("userdata/" + uuidString + "/system.pzs") - if err != nil { - log.Fatalf("failed to remove legacy system save data for %v: %s", uuidString, err) - } - - for i := 0; i < 5; 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 - } - - // store new session data - err = StoreSessionSaveData(uuid, sessionData, i) - if err != nil { - log.Fatalf("failed to store session save data for %v: %s\n", uuidString, err) - } - - // delete old session data - filename := "session" - if i != 0 { - filename += fmt.Sprintf("%d", i) - } - err = os.Remove(fmt.Sprintf("userdata/%s/%s.pzs", uuidString, filename)) - if err != nil { - log.Fatalf("failed to remove legacy session save data %v for %v: %s", i, uuidString, err) - } - } - } - return nil } diff --git a/db/legacy.go b/db/legacy.go deleted file mode 100644 index f9b5848..0000000 --- a/db/legacy.go +++ /dev/null @@ -1,84 +0,0 @@ -/* - 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 -}