1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-10-08 23:22:25 +02:00

code review

This commit is contained in:
Laserlicht
2025-09-21 22:07:57 +02:00
parent 0cc9c62e8a
commit 6c6350ad8b
13 changed files with 71 additions and 17 deletions

View File

@@ -288,12 +288,12 @@ std::shared_ptr<CIntObject> AdventureMapWidget::buildResourceDateBar(const JsonN
for (auto & i : LIBRARY->resourceTypeHandler->getAllObjects())
{
const auto & node = input[GameResID(i).toResource()->getJsonKey()];
const auto & node = input[i.toResource()->getJsonKey()];
if(node.isNull())
continue;
result->setResourcePosition(GameResID(i), Point(node["x"].Integer(), node["y"].Integer()));
result->setResourcePosition(i, Point(node["x"].Integer(), node["y"].Integer()));
}
result->setDatePosition(Point(input["date"]["x"].Integer(), input["date"]["y"].Integer()));

View File

@@ -77,7 +77,7 @@ void CStatisticScreen::onSelectButton()
else
{
auto content = static_cast<Content>(selectedIndex);
auto possibleRes = std::vector<EGameResID>{EGameResID::GOLD, EGameResID::WOOD, EGameResID::MERCURY, EGameResID::ORE, EGameResID::SULFUR, EGameResID::CRYSTAL, EGameResID::GEMS};
auto possibleRes = LIBRARY->resourceTypeHandler->getAllObjects();
std::vector<std::string> resourceText;
for(const auto & res : possibleRes)
resourceText.emplace_back(res.toResource()->getNameTranslated());

View File

@@ -55,9 +55,7 @@ JsonNode readBuilding(CLegacyConfigParser & parser)
for(const std::string & resID : GameConstants::RESOURCE_NAMES)
cost[resID].Float() = parser.readNumber();
parser.readNumber(); //note: will try to parse mithril -> needs mapping to resource from mithril mod
parser.endLine();
return ret;

View File

@@ -316,7 +316,7 @@ JsonRandom::JsonRandom(IGameInfoCallback * cb, IGameRandomizer & gameRandomizer)
GameResID::CRYSTAL,
GameResID::GEMS,
GameResID::GOLD
};
}; //todo: configurable resource support
std::set<GameResID> potentialPicks = filterKeys(value, defaultResources, variables);
GameResID resourceID = *RandomGeneratorUtil::nextItem(potentialPicks, rng);

View File

@@ -259,12 +259,12 @@ std::set<FactionID> ZoneOptions::getMonsterTypes() const
return vstd::difference(monsterTypes, bannedMonsters);
}
void ZoneOptions::setMinesInfo(const std::map<TResource, ui16> & value)
void ZoneOptions::setMinesInfo(const std::map<GameResID, ui16> & value)
{
mines = value;
}
std::map<TResource, ui16> ZoneOptions::getMinesInfo() const
std::map<GameResID, ui16> ZoneOptions::getMinesInfo() const
{
return mines;
}
@@ -533,10 +533,7 @@ void ZoneOptions::serializeJson(JsonSerializeFormat & handler)
if((minesLikeZone == NO_ZONE) && (!handler.saving || !mines.empty()))
{
auto minesData = handler.enterStruct("mines");
for(auto & idx : LIBRARY->resourceTypeHandler->getAllObjects())
handler.serializeInt(idx.toResource()->getJsonKey(), mines[idx], 0);
handler.serializeIdMap<GameResID, ui16>("mines", mines);
}
handler.serializeStruct("customObjects", objectConfig);

View File

@@ -209,8 +209,8 @@ public:
void setMonsterTypes(const std::set<FactionID> & value);
void setMinesInfo(const std::map<TResource, ui16> & value);
std::map<TResource, ui16> getMinesInfo() const;
void setMinesInfo(const std::map<GameResID, ui16> & value);
std::map<GameResID, ui16> getMinesInfo() const;
void setTreasureInfo(const std::vector<CTreasureInfo> & value);
void addTreasureInfo(const CTreasureInfo & value);
@@ -277,7 +277,7 @@ protected:
std::set<FactionID> monsterTypes;
std::set<FactionID> bannedMonsters;
std::map<TResource, ui16> mines; //obligatory mines to spawn in this zone
std::map<GameResID, ui16> mines; //obligatory mines to spawn in this zone
std::vector<CTreasureInfo> treasureInfo;
@@ -373,7 +373,7 @@ private:
std::set<HeroTypeID> bannedHeroes;
std::set<TerrainId> inheritTerrainType(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
std::map<TResource, ui16> inheritMineTypes(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
std::map<GameResID, ui16> inheritMineTypes(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
std::vector<CTreasureInfo> inheritTreasureInfo(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);
void inheritTownProperties(std::shared_ptr<rmg::ZoneOptions> zone, uint32_t iteration = 0);

View File

@@ -119,6 +119,16 @@ void JsonDeserializer::serializeInternal(const std::string & fieldName, std::vec
}
}
void JsonDeserializer::serializeInternal(const std::string & fieldName, std::map<std::string, uint16_t> & value)
{
const JsonMap & data = currentObject->operator[](fieldName).Struct();
value.clear();
for(const auto & [id, elem] : data)
value[id] = elem.Integer();
}
void JsonDeserializer::serializeInternal(std::string & value)
{
value = currentObject->String();

View File

@@ -32,6 +32,7 @@ protected:
void serializeInternal(const std::string & fieldName, si64 & value, const std::optional<si64> & defaultValue) override;
void serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const std::vector<std::string> & enumMap) override;
void serializeInternal(const std::string & fieldName, std::vector<std::string> & value) override;
void serializeInternal(const std::string & fieldName, std::map<std::string, uint16_t> & value) override;
void serializeInternal(std::string & value) override;
void serializeInternal(int64_t & value) override;

View File

@@ -331,6 +331,33 @@ public:
}
}
/// si32-convertible identifier map <-> Json object of {key: string}
template <typename Key, typename T, typename E = T>
void serializeIdMap(const std::string & fieldName, std::map<Key, T> & value)
{
if (saving)
{
std::map<std::string, T> fieldValue;
for (const auto & [key, val] : value)
fieldValue[Key::encode(key.getNum())] = val;
serializeInternal(fieldName, fieldValue);
}
else
{
const JsonNode & node = getCurrent()[fieldName];
for (const auto & [keyStr, jsonVal] : node.Struct())
{
Key key = Key::decode(keyStr);
LIBRARY->identifiers()->requestIdentifier(node.getModScope(), Key::entityType(), keyStr, [&value, key](int32_t index) {
value[key] = T(index);
});
}
}
}
///si32-convertible identifier vector <-> Json array of string
template <typename T, typename E = T>
void serializeIdArray(const std::string & fieldName, std::vector<T> & value)
@@ -443,6 +470,9 @@ protected:
///String vector <-> Json string vector
virtual void serializeInternal(const std::string & fieldName, std::vector<std::string> & value) = 0;
///String map <-> Json map of int
virtual void serializeInternal(const std::string & fieldName, std::map<std::string, uint16_t> & value) = 0;
virtual void pop() = 0;
virtual void pushStruct(const std::string & fieldName) = 0;
virtual void pushArray(const std::string & fieldName) = 0;

View File

@@ -75,6 +75,17 @@ void JsonSerializer::serializeInternal(const std::string & fieldName, std::vecto
data.emplace_back(rawId);
}
void JsonSerializer::serializeInternal(const std::string & fieldName, std::map<std::string, uint16_t> & value)
{
if(value.empty())
return;
JsonMap & data = currentObject->operator[](fieldName).Struct();
for(const auto & [rawId, val] : value)
data[rawId].Integer() = val;
}
void JsonSerializer::serializeInternal(std::string & value)
{
currentObject->String() = value;

View File

@@ -32,6 +32,7 @@ protected:
void serializeInternal(const std::string & fieldName, si64 & value, const std::optional<si64> & defaultValue) override;
void serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const std::vector<std::string> & enumMap) override;
void serializeInternal(const std::string & fieldName, std::vector<std::string> & value) override;
void serializeInternal(const std::string & fieldName, std::map<std::string, uint16_t> & value) override;
void serializeInternal(std::string & value) override;
void serializeInternal(int64_t & value) override;

View File

@@ -65,6 +65,11 @@ void JsonUpdater::serializeInternal(const std::string & fieldName, std::vector<s
// TODO
}
void JsonUpdater::serializeInternal(const std::string & fieldName, std::map<std::string, uint16_t> & value)
{
// TODO
}
void JsonUpdater::serializeInternal(const std::string & fieldName, double & value, const std::optional<double> & defaultValue)
{
const JsonNode & data = currentObject->operator[](fieldName);

View File

@@ -36,6 +36,7 @@ protected:
void serializeInternal(const std::string & fieldName, si64 & value, const std::optional<si64> & defaultValue) override;
void serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const std::vector<std::string> & enumMap) override;
void serializeInternal(const std::string & fieldName, std::vector<std::string> & value) override;
void serializeInternal(const std::string & fieldName, std::map<std::string, uint16_t> & value) override;
void serializeInternal(std::string & value) override;
void serializeInternal(int64_t & value) override;