Skip to content

Commit

Permalink
8072708 with less copypasta
Browse files Browse the repository at this point in the history
  • Loading branch information
rtri committed Feb 18, 2017
1 parent 8072708 commit 1545bb7
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 38 deletions.
15 changes: 6 additions & 9 deletions rts/Game/GameSetup.cpp
Expand Up @@ -210,23 +210,20 @@ void CGameSetup::ResetState()
demoName.clear();
saveName.clear();

// Synced unordered maps must be reconstructed since clearing
// may keep the container resized which will lead to
// difference in iteration and then desyncs
playerRemap = spring::unordered_map<int, int>();
teamRemap = spring::unordered_map<int, int>();
allyteamRemap = spring::unordered_map<int, int>();
spring::clear_unordered_map(playerRemap);
spring::clear_unordered_map(teamRemap);
spring::clear_unordered_map(allyteamRemap);

playerStartingData.clear();
teamStartingData.clear();
allyStartingData.clear();
skirmishAIStartingData.clear();
mutatorsList.clear();

restrictedUnits = spring::unordered_map<std::string, int>();
spring::clear_unordered_map(restrictedUnits);

mapOptions = spring::unordered_map<std::string, std::string>();
modOptions = spring::unordered_map<std::string, std::string>();
spring::clear_unordered_map(mapOptions);
spring::clear_unordered_map(modOptions);
}

void CGameSetup::PostLoad()
Expand Down
5 changes: 1 addition & 4 deletions rts/Lua/LuaFeatureDefs.cpp
Expand Up @@ -322,10 +322,7 @@ static int ColVolTable(lua_State* L, const void* data) {

static bool InitParamMap()
{
// Synced unordered maps must be reconstructed since clearing
// may keep the container resized which will lead to
// difference in iteration and then desyncs
paramMap = ParamMap();
spring::clear_unordered_map(paramMap);

paramMap["next"] = DataElement(READONLY_TYPE);
paramMap["pairs"] = DataElement(READONLY_TYPE);
Expand Down
5 changes: 1 addition & 4 deletions rts/Lua/LuaHandleSynced.h
Expand Up @@ -229,10 +229,7 @@ class CLuaHandleSynced
CUnsyncedLuaHandle unsyncedLuaHandle;

public:
// Synced unordered maps must be reconstructed since clearing
// may keep the container resized which will lead to
// difference in iteration and then desyncs
static void ClearGameParams() { gameParams = LuaRulesParams::Params(); }
static void ClearGameParams() { spring::clear_unordered_map(gameParams); }
static const LuaRulesParams::Params& GetGameParams() { return gameParams; }

private:
Expand Down
5 changes: 1 addition & 4 deletions rts/Lua/LuaUnitDefs.cpp
Expand Up @@ -572,10 +572,7 @@ static int ReturnNil(lua_State* L, const void* data) {

static bool InitParamMap()
{
// Synced unordered maps must be reconstructed since clearing
// may keep the container resized which will lead to
// difference in iteration and then desyncs
paramMap = ParamMap();
spring::clear_unordered_map(paramMap);

paramMap["next"] = DataElement(READONLY_TYPE);
paramMap["pairs"] = DataElement(READONLY_TYPE);
Expand Down
5 changes: 1 addition & 4 deletions rts/Lua/LuaWeaponDefs.cpp
Expand Up @@ -406,10 +406,7 @@ static int GuiSoundSetTable(lua_State* L, const void* data)

static bool InitParamMap()
{
// Synced unordered maps must be reconstructed since clearing
// may keep the container resized which will lead to
// difference in iteration and then desyncs
paramMap = ParamMap();
spring::clear_unordered_map(paramMap);

paramMap["next"] = DataElement(READONLY_TYPE);
paramMap["pairs"] = DataElement(READONLY_TYPE);
Expand Down
24 changes: 11 additions & 13 deletions rts/Sim/Units/CommandAI/BuilderCAI.cpp
Expand Up @@ -214,23 +214,21 @@ CBuilderCAI::CBuilderCAI(CUnit* owner):

CBuilderCAI::~CBuilderCAI()
{
// if uh == NULL then all pointers to units should be considered dangling pointers
if (unitHandler != NULL) {
RemoveUnitFromReclaimers(owner);
RemoveUnitFromFeatureReclaimers(owner);
RemoveUnitFromResurrecters(owner);
unitHandler->RemoveBuilderCAI(this);
}
// if uh == NULL then all pointers to units should be considered dangling
if (unitHandler == nullptr)
return;

RemoveUnitFromReclaimers(owner);
RemoveUnitFromFeatureReclaimers(owner);
RemoveUnitFromResurrecters(owner);
unitHandler->RemoveBuilderCAI(this);
}

void CBuilderCAI::InitStatic()
{
// Synced unordered sets must be reconstructed since clearing
// may keep the container resized which will lead to
// difference in iteration and then desyncs
reclaimers = spring::unordered_set<int>();
featureReclaimers = spring::unordered_set<int>();
resurrecters = spring::unordered_set<int>();
spring::clear_unordered_set(reclaimers);
spring::clear_unordered_set(featureReclaimers);
spring::clear_unordered_set(resurrecters);
}

void CBuilderCAI::PostLoad()
Expand Down
5 changes: 5 additions & 0 deletions rts/System/UnorderedMap.hpp
Expand Up @@ -39,6 +39,11 @@


namespace spring {
// Synced unordered maps must be reconstructed (on reload)
// since clear() may keep the container resized which will
// lead to differences in iteration order and then desyncs
template<typename C> void clear_unordered_map(C& cont) { cont = C(); }

template<typename K, typename V>
class unordered_bimap {
public:
Expand Down
8 changes: 8 additions & 0 deletions rts/System/UnorderedSet.hpp
Expand Up @@ -36,5 +36,13 @@
#endif


namespace spring {
// Synced unordered sets must be reconstructed (on reload)
// since clear() may keep the container resized which will
// lead to differences in iteration order and then desyncs
template<typename C> void clear_unordered_set(C& cont) { cont = C(); }
};


#endif

1 comment on commit 1545bb7

@ashdnazg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

Please sign in to comment.