[openjk] 56/130: JO: Fix reading/writing saved game chunk

Simon McVittie smcv at debian.org
Fri Oct 28 11:09:18 UTC 2016


This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to branch debian/master
in repository openjk.

commit 61205f24a891d6ba665c3363454113a89d5352cc
Author: bibendovsky <bibendovsky at hotmail.com>
Date:   Tue Jul 26 19:57:40 2016 +0300

    JO: Fix reading/writing saved game chunk
---
 shared/qcommon/ojk_saved_game.cpp | 116 ++++++++++++++++++++++++++------------
 shared/qcommon/ojk_saved_game.h   |   2 +
 2 files changed, 82 insertions(+), 36 deletions(-)

diff --git a/shared/qcommon/ojk_saved_game.cpp b/shared/qcommon/ojk_saved_game.cpp
index 5a18911..a021fe3 100644
--- a/shared/qcommon/ojk_saved_game.cpp
+++ b/shared/qcommon/ojk_saved_game.cpp
@@ -196,16 +196,6 @@ void SavedGame::read_chunk(
         const auto&& loaded_chunk_id_string = get_chunk_id_string(
             ulLoadedChid);
 
-#if 0
-        if (!is_preview_mode_)
-        {
-            ::Com_Error(
-                ERR_DROP,
-                "Loaded chunk ID (%s) does not match requested chunk ID (%s)",
-                loaded_chunk_id_string.c_str(),
-                chunk_id_string.c_str());
-        }
-#else
         const auto&& error_message =
             "Loaded chunk ID (" +
             loaded_chunk_id_string +
@@ -215,9 +205,19 @@ void SavedGame::read_chunk(
 
         throw_error(
             error_message);
-#endif
     }
 
+    uint32_t uiLoadedCksum = 0;
+
+#ifdef JK2_MODE
+    // Get checksum...
+    //
+    uiLoaded += ::FS_Read(
+        &uiLoadedCksum,
+        static_cast<int>(sizeof(uiLoadedCksum)),
+        file_handle_);
+#endif // JK2_MODE
+
     // Load in data and magic number...
     //
     uint32_t uiCompressedLength = 0;
@@ -255,14 +255,32 @@ void SavedGame::read_chunk(
             file_handle_);
     }
 
+#ifdef JK2_MODE
+    uint32_t uiLoadedMagic = 0;
+
+    uiLoaded += ::FS_Read(
+        &uiLoadedMagic,
+        static_cast<int>(sizeof(uiLoadedMagic)),
+        file_handle_);
+
+    if (uiLoadedMagic != get_jo_magic_value())
+    {
+        const auto&& error_message =
+            "Bad saved game magic for chunk " + chunk_id_string + ".";
+
+        throw_error(
+            error_message);
+    }
+#endif // JK2_MODE
+
+#ifndef JK2_MODE
     // Get checksum...
     //
-    uint32_t uiLoadedCksum = 0;
-
     uiLoaded += ::FS_Read(
         &uiLoadedCksum,
         static_cast<int>(sizeof(uiLoadedCksum)),
         file_handle_);
+#endif // JK2_MODE
 
     // Make sure the checksums match...
     //
@@ -272,21 +290,11 @@ void SavedGame::read_chunk(
 
     if (uiLoadedCksum != uiCksum)
     {
-#if 0
-        if (!is_preview_mode_)
-        {
-            ::Com_Error(
-                ERR_DROP,
-                "Failed checksum check for chunk",
-                chunk_id_string.c_str());
-        }
-#else
         const auto&& error_message =
             "Failed checksum check for chunk " + chunk_id_string + ".";
 
         throw_error(
             error_message);
-#endif
     }
 
     // Make sure we didn't encounter any read errors...
@@ -295,23 +303,17 @@ void SavedGame::read_chunk(
         sizeof(uiLoadedLength) +
         sizeof(uiLoadedCksum) +
         (bBlockIsCompressed ? sizeof(uiCompressedLength) : 0) +
-        (bBlockIsCompressed ? uiCompressedLength : io_buffer_.size()))
+        (bBlockIsCompressed ? uiCompressedLength : io_buffer_.size()) +
+#ifdef JK2_MODE
+        sizeof(uiLoadedMagic) +
+#endif
+        0)
     {
-#if 0
-        if (!is_preview_mode_)
-        {
-            ::Com_Error(
-                ERR_DROP,
-                "Error during loading chunk %s",
-                chunk_id_string.c_str());
-        }
-#else
         const auto&& error_message =
             "Error during loading chunk " + chunk_id_string + ".";
 
         throw_error(
             error_message);
-#endif
     }
 }
 
@@ -355,6 +357,13 @@ void SavedGame::write_chunk(
         static_cast<int>(sizeof(chunk_id)),
         file_handle_);
 
+#ifdef JK2_MODE
+    uiSaved += ::FS_Write(
+        &uiCksum,
+        static_cast<int>(sizeof(uiCksum)),
+        file_handle_);
+#endif // JK2_MODE
+
     auto iCompressedLength = -1;
 
     if (::sv_compress_saved_games->integer != 0)
@@ -369,6 +378,10 @@ void SavedGame::write_chunk(
         }
     }
 
+#ifdef JK2_MODE
+    const auto uiMagic = get_jo_magic_value();
+#endif // JK2_MODE
+
     if (iCompressedLength > 0)
     {
         const auto iLength = -static_cast<int>(io_buffer_.size());
@@ -388,17 +401,30 @@ void SavedGame::write_chunk(
             iCompressedLength,
             file_handle_);
 
+#ifdef JK2_MODE
+        uiSaved += ::FS_Write(
+            &uiMagic,
+            static_cast<int>(sizeof(uiMagic)),
+            file_handle_);
+#endif // JK2_MODE
+
+#ifndef JK2_MODE
         uiSaved += ::FS_Write(
             &uiCksum,
             static_cast<int>(sizeof(uiCksum)),
             file_handle_);
+#endif // JK2_MODE
 
         if (uiSaved !=
             sizeof(chunk_id) +
             sizeof(iLength) +
             sizeof(uiCksum) +
             sizeof(iCompressedLength) +
-            iCompressedLength)
+            iCompressedLength +
+#ifdef JK2_MODE
+            sizeof(uiMagic) +
+#endif // JK2_MODE
+            0)
         {
             is_write_failed_ = true;
 
@@ -423,16 +449,29 @@ void SavedGame::write_chunk(
             iLength,
             file_handle_);
 
+#ifdef JK2_MODE
+        uiSaved += ::FS_Write(
+            &uiMagic,
+            static_cast<int>(sizeof(uiMagic)),
+            file_handle_);
+#endif // JK2_MODE
+
+#ifdef JK2_MODE
         uiSaved += ::FS_Write(
             &uiCksum,
             static_cast<int>(sizeof(uiCksum)),
             file_handle_);
+#endif // JK2_MODE
 
         if (uiSaved !=
             sizeof(chunk_id) +
             sizeof(iLength) +
             sizeof(uiCksum) +
-            iLength)
+            iLength +
+#ifdef JK2_MODE
+            sizeof(uiMagic) +
+#endif // JK2_MODE
+            0)
         {
             is_write_failed_ = true;
 
@@ -789,5 +828,10 @@ void SavedGame::reset_buffer_offset()
     io_buffer_offset_ = 0;
 }
 
+constexpr uint32_t SavedGame::get_jo_magic_value()
+{
+    return 0x1234ABCD;
+}
+
 
 } // ojk
diff --git a/shared/qcommon/ojk_saved_game.h b/shared/qcommon/ojk_saved_game.h
index 1b59232..481b71c 100644
--- a/shared/qcommon/ojk_saved_game.h
+++ b/shared/qcommon/ojk_saved_game.h
@@ -186,6 +186,8 @@ private:
     // Returns a string representation of a chunk id.
     static std::string get_chunk_id_string(
         uint32_t chunk_id);
+
+    static constexpr uint32_t get_jo_magic_value();
 }; // SavedGame
 
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/openjk.git



More information about the Pkg-games-commits mailing list