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

Return-To-The-Roots / s25client / 14551527845

19 Apr 2025 05:54PM UTC coverage: 50.309% (+0.02%) from 50.288%
14551527845

Pull #1750

github

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

11 of 28 new or added lines in 15 files covered. (39.29%)

2 existing lines in 1 file now uncovered.

22365 of 44455 relevant lines covered (50.31%)

34666.77 hits per line

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

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

5
#include "world/MapSerializer.h"
6
#include "CatapultStone.h"
7
#include "Game.h"
8
#include "SerializedGameData.h"
9
#include "buildings/noBuildingSite.h"
10
#include "helpers/Range.h"
11
#include "lua/GameDataLoader.h"
12
#include "world/GameWorldBase.h"
13
#include "s25util/warningSuppression.h"
14
#include <mygettext/mygettext.h>
15

16
void MapSerializer::Serialize(const GameWorldBase& world, SerializedGameData& sgd)
6✔
17
{
18
    // Headinformationen
19
    helpers::pushPoint(sgd, world.GetSize());
6✔
20
    sgd.PushString(world.GetDescription().get(world.GetLandscapeType()).name);
6✔
21

22
    sgd.PushUnsignedInt(GameObject::GetObjIDCounter());
6✔
23

24
    // Alle Weltpunkte serialisieren
25
    const unsigned numPlayers = world.GetNumPlayers();
6✔
26
    for(const auto& node : world.nodes)
5,366✔
27
    {
28
        node.Serialize(sgd, numPlayers, world.GetDescription());
5,360✔
29
    }
30

31
    // Katapultsteine serialisieren
32
    sgd.PushObjectContainer(world.catapult_stones, true);
6✔
33
    // Meeresinformationen serialisieren
34
    sgd.PushUnsignedInt(world.seas.size());
6✔
35
    for(const auto& sea : world.seas)
6✔
36
    {
37
        sgd.PushUnsignedInt(sea.nodes_count);
×
38
    }
39
    // Hafenpositionen serialisieren
40
    sgd.PushUnsignedInt(world.harbor_pos.size());
6✔
41
    for(const auto& curHarborPos : world.harbor_pos)
12✔
42
    {
43
        helpers::pushPoint(sgd, curHarborPos.pos);
6✔
44
        helpers::pushContainer(sgd, curHarborPos.seaIds);
6✔
45
        for(const auto& curNeighbors : curHarborPos.neighbors)
42✔
46
        {
47
            sgd.PushUnsignedInt(curNeighbors.size());
36✔
48

49
            for(const auto& c : curNeighbors)
36✔
50
            {
51
                sgd.PushUnsignedInt(c.id);
×
52
                sgd.PushUnsignedInt(c.distance);
×
53
            }
54
        }
55
    }
56

57
    sgd.PushObjectContainer(world.harbor_building_sites_from_sea, true);
6✔
58

59
    if(!world.HasLua())
6✔
60
        sgd.PushLongString("");
4✔
61
    else
62
    {
63
        sgd.PushLongString(world.GetLua().getScript());
2✔
64
        Serializer luaSaveState;
4✔
65
        try
66
        {
67
            if(!world.GetLua().Serialize(luaSaveState))
2✔
68
                throw SerializedGameData::Error(_("Failed to save lua state!"));
×
NEW
69
        } catch(const std::exception& e)
×
70
        {
71
            throw SerializedGameData::Error(std::string(_("Failed to save lua state!")) + _("Error: ") + e.what());
×
72
        }
73
        sgd.PushUnsignedInt(0xC0DEBA5E); // Start Lua identifier
2✔
74
        sgd.PushUnsignedInt(luaSaveState.GetLength());
2✔
75
        sgd.PushRawData(luaSaveState.GetData(), luaSaveState.GetLength());
2✔
76
        sgd.PushUnsignedInt(0xC001C0DE); // End Lua identifier
2✔
77
    }
78
}
6✔
79

80
void MapSerializer::Deserialize(GameWorldBase& world, SerializedGameData& sgd, Game& game,
3✔
81
                                ILocalGameState& localgameState)
82
{
83
    // Initialisierungen
84
    GameDataLoader gdLoader(world.GetDescriptionWriteable());
6✔
85
    if(!gdLoader.Load())
3✔
86
        throw SerializedGameData::Error(_("Failed to load game data!"));
×
87

88
    // Headinformationen
89
    const auto size = helpers::popPoint<MapExtent>(sgd);
3✔
90
    DescIdx<LandscapeDesc> lt;
3✔
91
    if(sgd.GetGameDataVersion() < 3)
3✔
92
    {
93
        uint8_t gfxSet = sgd.PopUnsignedChar();
×
94
        lt = world.GetDescription().landscapes.find([gfxSet](const LandscapeDesc& l) { return l.s2Id == gfxSet; });
×
95
    } else
96
    {
97
        std::string sLandscape = sgd.PopString();
6✔
98
        lt = world.GetDescription().landscapes.getIndex(sLandscape);
3✔
99
        if(!lt)
3✔
100
            throw SerializedGameData::Error(std::string("Invalid landscape: ") + sLandscape);
×
101
    }
102
    world.Init(size, lt);
3✔
103
    GameObject::ResetCounters(sgd.PopUnsignedInt());
3✔
104

105
    std::vector<DescIdx<TerrainDesc>> landscapeTerrains;
6✔
106
    if(sgd.GetGameDataVersion() < 3)
3✔
107
    {
108
        // Assumes the order of the terrain in the description file is the same as in the prior RTTR versions
109
        landscapeTerrains =
110
          world.GetDescription().terrain.findAll([lt](const TerrainDesc& t) { return t.landscape == lt; });
×
111
    }
112
    // Alle Weltpunkte
113
    MapPoint curPos(0, 0);
3✔
114
    const unsigned numPlayers = world.GetNumPlayers();
3✔
115
    for(auto& node : world.nodes)
2,683✔
116
    {
117
        node.Deserialize(sgd, numPlayers, world.GetDescription(), landscapeTerrains);
2,680✔
118
        if(node.harborId)
2,680✔
119
        {
120
            HarborPos p(curPos);
2,422✔
121
            world.harbor_pos.push_back(p);
1,211✔
122
        }
123
        curPos.x++;
2,680✔
124
        if(curPos.x >= world.GetWidth())
2,680✔
125
        {
126
            curPos.x = 0;
74✔
127
            curPos.y++;
74✔
128
        }
129
    }
130

131
    // Katapultsteine deserialisieren
132
    sgd.PopObjectContainer(world.catapult_stones, GO_Type::Catapultstone);
3✔
133

134
    // Meeresinformationen deserialisieren
135
    world.seas.resize(sgd.PopUnsignedInt());
3✔
136
    for(auto& sea : world.seas)
3✔
137
    {
138
        sea.nodes_count = sgd.PopUnsignedInt();
×
139
    }
140

141
    // Hafenpositionen serialisieren
142
    const unsigned numHarborPositions = sgd.PopUnsignedInt();
3✔
143
    world.harbor_pos.clear();
3✔
144
    world.harbor_pos.reserve(numHarborPositions);
3✔
145
    for(const auto i : helpers::range<unsigned>(numHarborPositions))
18✔
146
    {
147
        RTTR_UNUSED(i);
148
        world.harbor_pos.emplace_back(sgd.PopMapPoint());
3✔
149
        auto& curHarborPos = world.harbor_pos.back();
3✔
150
        helpers::popContainer(sgd, curHarborPos.seaIds);
3✔
151
        for(auto& neighbor : curHarborPos.neighbors)
21✔
152
        {
153
            const unsigned numNeighbors = sgd.PopUnsignedInt();
18✔
154
            neighbor.reserve(numNeighbors);
18✔
155
            for(const auto j : helpers::range<unsigned>(numNeighbors))
72✔
156
            {
157
                RTTR_UNUSED(j);
158
                const auto id = sgd.PopUnsignedInt();
×
159
                const auto distance = sgd.PopUnsignedInt();
×
160
                neighbor.emplace_back(id, distance);
×
161
            }
162
        }
163
    }
164

165
    sgd.PopObjectContainer(world.harbor_building_sites_from_sea, GO_Type::Buildingsite);
3✔
166

167
    const std::string luaScript = sgd.PopLongString();
6✔
168
    if(!luaScript.empty())
3✔
169
    {
170
        if(sgd.PopUnsignedInt() != 0xC0DEBA5E)
1✔
171
            throw SerializedGameData::Error(_("Invalid id for lua data"));
×
172
        // If there is a script, there is also save data. Store reference to that
173
        const auto luaSaveSize = sgd.PopUnsignedInt();
1✔
174
        Serializer luaSaveState(sgd.PopAndDiscard(luaSaveSize), luaSaveSize);
2✔
175
        if(sgd.PopUnsignedInt() != 0xC001C0DE)
1✔
176
            throw SerializedGameData::Error(_("Invalid end-id for lua data"));
×
177

178
        // Now init and load lua
179
        auto lua = std::make_unique<LuaInterfaceGame>(game, localgameState);
1✔
180
        if(!lua->loadScriptString(luaScript))
1✔
181
            throw SerializedGameData::Error(_("Lua script failed to load."));
×
182
        if(!lua->CheckScriptVersion())
1✔
183
            throw SerializedGameData::Error(_("Wrong version for lua script."));
×
184
        try
185
        {
186
            if(!lua->Deserialize(luaSaveState))
1✔
187
                throw SerializedGameData::Error(_("Lua load callback returned failure!"));
×
188
        } catch(const std::exception& e)
×
189
        {
190
            throw SerializedGameData::Error(std::string(_("Failed to load lua state!")) + _("Error: ") + e.what());
×
191
        }
192
        game.SetLua(std::move(lua));
1✔
193
    }
194
    world.CreateTradeGraphs();
3✔
195
}
3✔
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