Skip to content

Commit

Permalink
Only require "campaigns" list for alternateCampaigns
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Mar 16, 2024
1 parent 492f36f commit 4355c5a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
1 change: 1 addition & 0 deletions doc/CampaignMods.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ It may also optionally contain a `mod-banner.png` image, which is used as the ba
- updatesURL: (string) A URL to which a user can browse to obtain a newer version of your mod, if available
- We recommend hosting your mod on GitHub, and using GitHub releases. ex: `"https://github.com/<USER>/<MODREPO>/releases/latest"`
- **campaigns**: (array) An array of campaign json file names (which will be loaded from `campaigns/<filename>` in your mod)
- Note: Required for alternateCampaigns. Will be ignored for campaignBalanceMods.
- **universe**: (string) The "universe" in which your campaign's story takes place
- Options are:
- `wz2100` (usually used for campaign balance mods that just provide stats modifications to the "official" Warzone 2100 story)
Expand Down
59 changes: 31 additions & 28 deletions src/modinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,39 +287,42 @@ WzCampaignModInfo loadCampaignModInfo(const nlohmann::json& j, const std::string
}
loadBaseModInfo(j, v);

// campaigns
std::vector<std::string> campaignJSONList;
auto& campaigns = j.at("campaigns");
if (!campaigns.is_array())
if (v.type == WzModType::AlternateCampaign)
{
throw nlohmann::json::type_error::create(302, "campaigns type must be an array, but is " + std::string(campaigns.type_name()), &j);
}
campaignJSONList = campaigns.get<std::vector<std::string>>();
for (const auto& jsonName : campaignJSONList)
{
std::string fullJSONPath = baseDir + "/campaigns/" + jsonName;
auto jsonObj = wzLoadJsonObjectFromFile(fullJSONPath.c_str());
if (!jsonObj.has_value())
// campaigns
std::vector<std::string> campaignJSONList;
auto& campaigns = j.at("campaigns");
if (!campaigns.is_array())
{
debug(LOG_ERROR, "Failed to find: campaigns/%s", jsonName.c_str());
continue;
throw nlohmann::json::type_error::create(302, "campaigns type must be an array, but is " + std::string(campaigns.type_name()), &j);
}

CAMPAIGN_FILE c;
try {
c = jsonObj.value().get<CAMPAIGN_FILE>();
campaignJSONList = campaigns.get<std::vector<std::string>>();
for (const auto& jsonName : campaignJSONList)
{
std::string fullJSONPath = baseDir + "/campaigns/" + jsonName;
auto jsonObj = wzLoadJsonObjectFromFile(fullJSONPath.c_str());
if (!jsonObj.has_value())
{
debug(LOG_ERROR, "Failed to find: campaigns/%s", jsonName.c_str());
continue;
}

CAMPAIGN_FILE c;
try {
c = jsonObj.value().get<CAMPAIGN_FILE>();
}
catch (const std::exception &e) {
// Failed to parse the JSON
debug(LOG_ERROR, "JSON document from %s is invalid: %s", jsonName.c_str(), e.what());
continue;
}

v.campaignFiles.push_back(c);
}
catch (const std::exception &e) {
// Failed to parse the JSON
debug(LOG_ERROR, "JSON document from %s is invalid: %s", jsonName.c_str(), e.what());
continue;
if (v.campaignFiles.empty())
{
throw nlohmann::json::parse_error::create(302, 0, "\"campaigns\" must list one or more valid campaign JSON files", &j);
}

v.campaignFiles.push_back(c);
}
if (v.campaignFiles.empty())
{
throw nlohmann::json::parse_error::create(302, 0, "\"campaigns\" must list one or more valid campaign JSON files", &j);
}

// difficultyLevels
Expand Down

0 comments on commit 4355c5a

Please sign in to comment.