• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Return-To-The-Roots / s25client / 14284082480

05 Apr 2025 05:14PM UTC coverage: 50.296% (+0.008%) from 50.288%
14284082480

Pull #1750

github

web-flow
Merge 739414d33 into c8cf430f2
Pull Request #1750: Fix size of Settings window

8 of 26 new or added lines in 15 files covered. (30.77%)

2 existing lines in 1 file now uncovered.

22363 of 44463 relevant lines covered (50.3%)

36104.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.57
/libs/s25main/Savegame.cpp
1
// Copyright (C) 2005 - 2024 Settlers Freaks (sf-team at siedler25.org)
2
//
3
// SPDX-License-Identifier: GPL-2.0-or-later
4

5
#include "Savegame.h"
6
#include "gameTypes/CompressedData.h"
7
#include "s25util/BinaryFile.h"
8
#include <boost/filesystem/operations.hpp>
9
#include <boost/nowide/fstream.hpp>
10

11
std::string Savegame::GetSignature() const
11✔
12
{
13
    return "RTTRSV";
11✔
14
}
15

16
uint16_t Savegame::GetVersion() const
11✔
17
{
18
    // Note: If you increase the version, reset currentGameDataVersion in SerializedGameData.cpp (see note there)
19
    // Note2: Also remove the workaround for the team in BasePlayerInfo & CompressedFlag here
20
    return 4; // SaveGameVersion -- Updater signature, do NOT remove
11✔
21
}
22

23
//////////////////////////////////////////////////////////////////////////
24

25
Savegame::Savegame() : start_gf(0) {}
10✔
26

27
Savegame::~Savegame() = default;
12✔
28

29
bool Savegame::Save(const boost::filesystem::path& filepath, const std::string& mapName)
2✔
30
{
31
    BinaryFile file;
2✔
32

33
    return file.Open(filepath, OpenFileMode::Write) && Save(file, mapName);
4✔
34
}
35

36
bool Savegame::Save(BinaryFile& file, const std::string& mapName)
3✔
37
{
38
    WriteAllHeaderData(file, mapName);
3✔
39
    WritePlayerData(file);
3✔
40
    WriteGGS(file);
3✔
41
    WriteGameData(file);
3✔
42

43
    return true;
3✔
44
}
45

46
bool Savegame::Load(const boost::filesystem::path& filePath, const SaveGameDataToLoad what)
4✔
47
{
48
    BinaryFile file;
4✔
49

50
    return file.Open(filePath, OpenFileMode::Read) && Load(file, what);
8✔
51
}
52

53
bool Savegame::Load(BinaryFile& file, const SaveGameDataToLoad what)
5✔
54
{
55
    try
56
    {
57
        ClearPlayers();
5✔
58
        sgd.Clear();
5✔
59
        if(!ReadAllHeaderData(file))
5✔
60
            return false;
×
61

62
        if(what == SaveGameDataToLoad::Header)
5✔
63
            return true;
1✔
64

65
        ReadPlayerData(file);
4✔
66
        ReadGGS(file);
4✔
67

68
        if(what == SaveGameDataToLoad::HeaderAndSettings)
4✔
69
            return true;
1✔
70

71
        ReadGameData(file);
3✔
NEW
72
    } catch(const std::runtime_error& e)
×
73
    {
74
        lastErrorMsg = e.what();
×
75
        return false;
×
76
    }
77

78
    return true;
3✔
79
}
80

81
void Savegame::WriteExtHeader(BinaryFile& file, const std::string& mapName)
3✔
82
{
83
    SavedFile::WriteExtHeader(file, mapName);
3✔
84
    file.WriteUnsignedInt(start_gf);
3✔
85
}
3✔
86

87
bool Savegame::ReadExtHeader(BinaryFile& file)
5✔
88
{
89
    if(!SavedFile::ReadExtHeader(file))
5✔
90
        return false;
×
91
    start_gf = file.ReadUnsignedInt();
5✔
92
    return true;
5✔
93
}
94

95
void Savegame::WriteGameData(BinaryFile& file)
3✔
96
{
97
    file.WriteUnsignedInt(1); // Compressed flag for compatibility
3✔
98
    std::vector<char> data(sgd.GetData(), sgd.GetData() + sgd.GetLength());
6✔
99
    const unsigned uncompressedLength = data.size();
3✔
100
    data = CompressedData::compress(data);
3✔
101
    file.WriteUnsignedInt(uncompressedLength);
3✔
102
    file.WriteUnsignedInt(data.size());
3✔
103
    file.WriteRawData(data.data(), data.size());
3✔
104
}
3✔
105

106
bool Savegame::ReadGameData(BinaryFile& file)
3✔
107
{
108
    std::vector<char> data;
3✔
109
    const auto compressedFlagOrSize = file.ReadUnsignedInt();
3✔
110
    if(compressedFlagOrSize == 1u)
3✔
111
    {
112
        const auto uncompressedLength = file.ReadUnsignedInt();
3✔
113
        const auto compressedLength = file.ReadUnsignedInt();
3✔
114
        data.resize(compressedLength);
3✔
115
        file.ReadRawData(data.data(), data.size());
3✔
116
        data = CompressedData::decompress(data, uncompressedLength);
3✔
117
#ifndef NDEBUG
118
        // In debug builds write uncompressed game data to temporary file
119
        const auto gameDataPath = boost::filesystem::temp_directory_path() / "rttrGameData.raw";
6✔
120
        boost::nowide::ofstream f(gameDataPath, std::ios::binary);
6✔
121
        f.write(data.data(), data.size());
3✔
122
#endif
123
    } else
124
    { // Old savegames have a size here which is always bigger than 1
125
        RTTR_Assert(compressedFlagOrSize > 1u);
×
126
        data.resize(compressedFlagOrSize);
×
127
        file.ReadRawData(data.data(), data.size());
×
128
    }
129

130
    sgd.Clear();
3✔
131
    sgd.PushRawData(data.data(), data.size());
3✔
132
    return true;
6✔
133
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc