From 4355c5af2b2c98f5f915b8a63f71d6f72033c85a Mon Sep 17 00:00:00 2001 From: past-due <30942300+past-due@users.noreply.github.com> Date: Fri, 15 Mar 2024 22:52:07 -0400 Subject: [PATCH] Only require "campaigns" list for alternateCampaigns --- doc/CampaignMods.md | 1 + src/modinfo.cpp | 59 ++++++++++++++++++++++++--------------------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/doc/CampaignMods.md b/doc/CampaignMods.md index d16329e392a..dbaf83b08dd 100644 --- a/doc/CampaignMods.md +++ b/doc/CampaignMods.md @@ -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///releases/latest"` - **campaigns**: (array) An array of campaign json file names (which will be loaded from `campaigns/` 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) diff --git a/src/modinfo.cpp b/src/modinfo.cpp index 4793020ce83..badf7edc747 100644 --- a/src/modinfo.cpp +++ b/src/modinfo.cpp @@ -287,39 +287,42 @@ WzCampaignModInfo loadCampaignModInfo(const nlohmann::json& j, const std::string } loadBaseModInfo(j, v); - // campaigns - std::vector 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>(); - for (const auto& jsonName : campaignJSONList) - { - std::string fullJSONPath = baseDir + "/campaigns/" + jsonName; - auto jsonObj = wzLoadJsonObjectFromFile(fullJSONPath.c_str()); - if (!jsonObj.has_value()) + // campaigns + std::vector 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(); + campaignJSONList = campaigns.get>(); + 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(); + } + 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