|
@@ -47,15 +47,22 @@ CCampaignHeader CCampaignHandler::getHeader( const std::string & name)
|
|
|
std::string modName = VLC->modh->findResourceOrigin(resourceID);
|
|
|
std::string language = VLC->modh->getModLanguage(modName);
|
|
|
std::string encoding = Languages::getLanguageOptions(language).encoding;
|
|
|
- auto fileStream = CResourceHandler::get(modName)->load(resourceID);
|
|
|
-
|
|
|
- std::vector<ui8> cmpgn = getFile(std::move(fileStream), true)[0];
|
|
|
-
|
|
|
- CMemoryStream stream(cmpgn.data(), cmpgn.size());
|
|
|
- CBinaryReader reader(&stream);
|
|
|
- CCampaignHeader ret = readHeaderFromMemory(reader, resourceID.getName(), modName, encoding);
|
|
|
-
|
|
|
- return ret;
|
|
|
+
|
|
|
+ JsonNode jsonCampaign(resourceID);
|
|
|
+ if(jsonCampaign.isNull())
|
|
|
+ {
|
|
|
+ auto fileStream = CResourceHandler::get(modName)->load(resourceID);
|
|
|
+ std::vector<ui8> cmpgn = getFile(std::move(fileStream), true)[0];
|
|
|
+ CMemoryStream stream(cmpgn.data(), cmpgn.size());
|
|
|
+ CBinaryReader reader(&stream);
|
|
|
+ CCampaignHeader ret = readHeaderFromMemory(reader, resourceID.getName(), modName, encoding);
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ CCampaignHeader ret = readHeaderFromJson(jsonCampaign, resourceID.getName(), modName, encoding);
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
std::unique_ptr<CCampaign> CCampaignHandler::getCampaign( const std::string & name )
|
|
@@ -64,48 +71,69 @@ std::unique_ptr<CCampaign> CCampaignHandler::getCampaign( const std::string & na
|
|
|
std::string modName = VLC->modh->findResourceOrigin(resourceID);
|
|
|
std::string language = VLC->modh->getModLanguage(modName);
|
|
|
std::string encoding = Languages::getLanguageOptions(language).encoding;
|
|
|
- auto fileStream = CResourceHandler::get(modName)->load(resourceID);
|
|
|
-
|
|
|
+
|
|
|
auto ret = std::make_unique<CCampaign>();
|
|
|
+
|
|
|
+ JsonNode jsonCampaign(resourceID);
|
|
|
+ if(jsonCampaign.isNull())
|
|
|
+ {
|
|
|
+ auto fileStream = CResourceHandler::get(modName)->load(resourceID);
|
|
|
|
|
|
- std::vector<std::vector<ui8>> file = getFile(std::move(fileStream), false);
|
|
|
-
|
|
|
- CMemoryStream stream(file[0].data(), file[0].size());
|
|
|
- CBinaryReader reader(&stream);
|
|
|
- ret->header = readHeaderFromMemory(reader, resourceID.getName(), modName, encoding);
|
|
|
+ std::vector<std::vector<ui8>> file = getFile(std::move(fileStream), false);
|
|
|
|
|
|
- int howManyScenarios = static_cast<int>(VLC->generaltexth->getCampaignLength(ret->header.mapVersion));
|
|
|
- for(int g=0; g<howManyScenarios; ++g)
|
|
|
- {
|
|
|
- CCampaignScenario sc = readScenarioFromMemory(reader, resourceID.getName(), modName, encoding, ret->header.version, ret->header.mapVersion);
|
|
|
- ret->scenarios.push_back(sc);
|
|
|
- }
|
|
|
+ CMemoryStream stream(file[0].data(), file[0].size());
|
|
|
+ CBinaryReader reader(&stream);
|
|
|
+ ret->header = readHeaderFromMemory(reader, resourceID.getName(), modName, encoding);
|
|
|
|
|
|
- int scenarioID = 0;
|
|
|
+ int howManyScenarios = static_cast<int>(VLC->generaltexth->getCampaignLength(ret->header.mapVersion));
|
|
|
+ for(int g=0; g<howManyScenarios; ++g)
|
|
|
+ {
|
|
|
+ CCampaignScenario sc = readScenarioFromMemory(reader, resourceID.getName(), modName, encoding, ret->header.version, ret->header.mapVersion);
|
|
|
+ ret->scenarios.push_back(sc);
|
|
|
+ }
|
|
|
+
|
|
|
+ int scenarioID = 0;
|
|
|
+
|
|
|
+ //first entry is campaign header. start loop from 1
|
|
|
+ for (int g=1; g<file.size() && scenarioID<howManyScenarios; ++g)
|
|
|
+ {
|
|
|
+ while(!ret->scenarios[scenarioID].isNotVoid()) //skip void scenarios
|
|
|
+ {
|
|
|
+ scenarioID++;
|
|
|
+ }
|
|
|
|
|
|
- //first entry is campaign header. start loop from 1
|
|
|
- for (int g=1; g<file.size() && scenarioID<howManyScenarios; ++g)
|
|
|
+ std::string scenarioName = resourceID.getName();
|
|
|
+ boost::to_lower(scenarioName);
|
|
|
+ scenarioName += ':' + std::to_string(g - 1);
|
|
|
+
|
|
|
+ //set map piece appropriately, convert vector to string
|
|
|
+ ret->mapPieces[scenarioID].assign(reinterpret_cast< const char* >(file[g].data()), file[g].size());
|
|
|
+ CMapService mapService;
|
|
|
+ auto hdr = mapService.loadMapHeader(
|
|
|
+ reinterpret_cast<const ui8 *>(ret->mapPieces[scenarioID].c_str()),
|
|
|
+ static_cast<int>(ret->mapPieces[scenarioID].size()),
|
|
|
+ scenarioName,
|
|
|
+ modName,
|
|
|
+ encoding);
|
|
|
+ ret->scenarios[scenarioID].scenarioName = hdr->name;
|
|
|
+ scenarioID++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
{
|
|
|
- while(!ret->scenarios[scenarioID].isNotVoid()) //skip void scenarios
|
|
|
+ ret->header = readHeaderFromJson(jsonCampaign, resourceID.getName(), modName, encoding);
|
|
|
+
|
|
|
+ for(auto & scenario : jsonCampaign["scenarios"].Vector())
|
|
|
{
|
|
|
- scenarioID++;
|
|
|
+ CCampaignScenario sc = readScenarioFromJson(scenario, resourceID.getName(), modName, encoding, ret->header.version, ret->header.mapVersion);
|
|
|
+ if(sc.isNotVoid())
|
|
|
+ {
|
|
|
+ CMapService mapService;
|
|
|
+ auto hdr = mapService.loadMapHeader(ResourceID(sc.mapName, EResType::MAP));
|
|
|
+ sc.scenarioName = hdr->name;
|
|
|
+ }
|
|
|
+ ret->scenarios.push_back(sc);
|
|
|
}
|
|
|
-
|
|
|
- std::string scenarioName = resourceID.getName();
|
|
|
- boost::to_lower(scenarioName);
|
|
|
- scenarioName += ':' + std::to_string(g - 1);
|
|
|
-
|
|
|
- //set map piece appropriately, convert vector to string
|
|
|
- ret->mapPieces[scenarioID].assign(reinterpret_cast< const char* >(file[g].data()), file[g].size());
|
|
|
- CMapService mapService;
|
|
|
- auto hdr = mapService.loadMapHeader(
|
|
|
- reinterpret_cast<const ui8 *>(ret->mapPieces[scenarioID].c_str()),
|
|
|
- static_cast<int>(ret->mapPieces[scenarioID].size()),
|
|
|
- scenarioName,
|
|
|
- modName,
|
|
|
- encoding);
|
|
|
- ret->scenarios[scenarioID].scenarioName = hdr->name;
|
|
|
- scenarioID++;
|
|
|
}
|
|
|
|
|
|
// handle campaign specific discrepancies
|
|
@@ -151,6 +179,198 @@ std::string CCampaignHandler::readLocalizedString(CBinaryReader & reader, std::s
|
|
|
return VLC->generaltexth->translate(stringID.get());
|
|
|
}
|
|
|
|
|
|
+CCampaignHeader CCampaignHandler::readHeaderFromJson(JsonNode & reader, std::string filename, std::string modName, std::string encoding )
|
|
|
+{
|
|
|
+ CCampaignHeader ret;
|
|
|
+
|
|
|
+ ret.version = reader["version"].Integer();
|
|
|
+ if(ret.version < CampaignVersion::VCMI_MIN || ret.version > CampaignVersion::VCMI_MAX)
|
|
|
+ {
|
|
|
+ logGlobal->info("Unsupported campaign %s version %d", filename, ret.version);
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ ret.version = CampaignVersion::VCMI;
|
|
|
+ ret.mapVersion = reader["campaignId"].Integer();
|
|
|
+ ret.name = reader["name"].String();
|
|
|
+ ret.description = reader["description"].String();
|
|
|
+ ret.difficultyChoosenByPlayer = reader["allowDifficultySelection"].Bool();
|
|
|
+ //skip ret.music because it's unused in vcmi
|
|
|
+ ret.filename = filename;
|
|
|
+ ret.modName = modName;
|
|
|
+ ret.encoding = encoding;
|
|
|
+ ret.valid = true;
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+CCampaignScenario CCampaignHandler::readScenarioFromJson(JsonNode & reader, std::string filename, std::string modName, std::string encoding, int version, int mapVersion)
|
|
|
+{
|
|
|
+ auto prologEpilogReader = [](JsonNode & identifier) -> CCampaignScenario::SScenarioPrologEpilog
|
|
|
+ {
|
|
|
+ CCampaignScenario::SScenarioPrologEpilog ret;
|
|
|
+ ret.hasPrologEpilog = !identifier.isNull();
|
|
|
+ if(ret.hasPrologEpilog)
|
|
|
+ {
|
|
|
+ ret.prologVideo = identifier["video"].Integer();
|
|
|
+ ret.prologMusic = identifier["music"].Integer();
|
|
|
+ ret.prologText = identifier["text"].String();
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+ };
|
|
|
+
|
|
|
+ CCampaignScenario ret;
|
|
|
+ ret.conquered = false;
|
|
|
+ ret.mapName = reader["map"].String();
|
|
|
+ for(auto & g : reader["precoditions"].Vector())
|
|
|
+ ret.preconditionRegions.insert(g.Integer());
|
|
|
+
|
|
|
+ ret.regionColor = reader["color"].Integer();
|
|
|
+ ret.difficulty = reader["difficulty"].Integer();
|
|
|
+ ret.regionText = reader["regionText"].String();
|
|
|
+ ret.prolog = prologEpilogReader(reader["prolog"]);
|
|
|
+ ret.epilog = prologEpilogReader(reader["epilog"]);
|
|
|
+
|
|
|
+ ret.travelOptions = readScenarioTravelFromJson(reader, version);
|
|
|
+
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+CScenarioTravel CCampaignHandler::readScenarioTravelFromJson(JsonNode & reader, int version )
|
|
|
+{
|
|
|
+ CScenarioTravel ret;
|
|
|
+
|
|
|
+ std::map<std::string, ui8> heroKeepsMap = {
|
|
|
+ {"experience", 1},
|
|
|
+ {"primary", 2},
|
|
|
+ {"skills", 4},
|
|
|
+ {"spells", 8},
|
|
|
+ {"artifacts", 16}
|
|
|
+ };
|
|
|
+ std::map<std::string, ui8> startOptionsMap = {
|
|
|
+ {"none", 0},
|
|
|
+ {"bonus", 1},
|
|
|
+ {"crossover", 2},
|
|
|
+ {"hero", 3}
|
|
|
+ };
|
|
|
+
|
|
|
+ std::map<std::string, CScenarioTravel::STravelBonus::EBonusType> bonusTypeMap = {
|
|
|
+ {"spell", CScenarioTravel::STravelBonus::EBonusType::SPELL},
|
|
|
+ {"creature", CScenarioTravel::STravelBonus::EBonusType::MONSTER},
|
|
|
+ {"building", CScenarioTravel::STravelBonus::EBonusType::BUILDING},
|
|
|
+ {"artifact", CScenarioTravel::STravelBonus::EBonusType::ARTIFACT},
|
|
|
+ {"scroll", CScenarioTravel::STravelBonus::EBonusType::SPELL_SCROLL},
|
|
|
+ {"primary", CScenarioTravel::STravelBonus::EBonusType::PRIMARY_SKILL},
|
|
|
+ {"skill", CScenarioTravel::STravelBonus::EBonusType::SECONDARY_SKILL},
|
|
|
+ {"resource", CScenarioTravel::STravelBonus::EBonusType::RESOURCE},
|
|
|
+ //{"prevHero", CScenarioTravel::STravelBonus::EBonusType::HEROES_FROM_PREVIOUS_SCENARIO},
|
|
|
+ //{"hero", CScenarioTravel::STravelBonus::EBonusType::HERO},
|
|
|
+ };
|
|
|
+
|
|
|
+ std::map<std::string, ui16> heroSpecialMap = {
|
|
|
+ {"strongest", 0xFFFD},
|
|
|
+ {"generated", 0xFFFE},
|
|
|
+ {"random", 0xFFFF}
|
|
|
+ };
|
|
|
+
|
|
|
+ std::map<std::string, ui8> resourceTypeMap = {
|
|
|
+ //FD - wood+ore
|
|
|
+ //FE - mercury+sulfur+crystal+gem
|
|
|
+ {"wood", 0},
|
|
|
+ {"ore", 1},
|
|
|
+ {"mercury", 2},
|
|
|
+ {"sulfur", 3},
|
|
|
+ {"crystal", 4},
|
|
|
+ {"gem", 5},
|
|
|
+ {"gold", 6},
|
|
|
+ {"common", 0xFD},
|
|
|
+ {"rare", 0xFE}
|
|
|
+ };
|
|
|
+
|
|
|
+ for(auto & k : reader["heroKeeps"].Vector())
|
|
|
+ ret.whatHeroKeeps |= heroKeepsMap[k.String()];
|
|
|
+
|
|
|
+ //reader.getStream()->read(ret.monstersKeptByHero.data(), ret.monstersKeptByHero.size());
|
|
|
+ //reader.getStream()->read(ret.artifsKeptByHero.data(), ret.artifsKeptByHero.size());
|
|
|
+
|
|
|
+ ret.startOptions = startOptionsMap[reader["startOptions"].String()];
|
|
|
+
|
|
|
+ switch(ret.startOptions)
|
|
|
+ {
|
|
|
+ case 0:
|
|
|
+ //no bonuses. Seems to be OK
|
|
|
+ break;
|
|
|
+ case 1: //reading of bonuses player can choose
|
|
|
+ {
|
|
|
+ ret.playerColor = reader["playerColor"].Integer();
|
|
|
+ for(auto & bjson : reader["bonuses"].Vector())
|
|
|
+ {
|
|
|
+ CScenarioTravel::STravelBonus bonus;
|
|
|
+ bonus.type = bonusTypeMap[bjson["what"].String()];
|
|
|
+ if(bonus.type == CScenarioTravel::STravelBonus::EBonusType::RESOURCE)
|
|
|
+ {
|
|
|
+ bonus.info1 = resourceTypeMap[bjson["type"].String()];
|
|
|
+ bonus.info2 = bjson["amount"].Integer();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if(int heroId = heroSpecialMap[bjson["hero"].String()])
|
|
|
+ bonus.info1 = heroId;
|
|
|
+ else
|
|
|
+ bonus.info1 = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), "hero", bjson["hero"].String()).get();
|
|
|
+
|
|
|
+ if(bonus.type == CScenarioTravel::STravelBonus::EBonusType::SPELL
|
|
|
+ || bonus.type == CScenarioTravel::STravelBonus::EBonusType::MONSTER
|
|
|
+ || bonus.type == CScenarioTravel::STravelBonus::EBonusType::ARTIFACT)
|
|
|
+ bonus.info2 = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), bjson["what"].String(), bjson["type"].String()).get();
|
|
|
+ else
|
|
|
+ bonus.info2 = bjson["type"].Integer();
|
|
|
+
|
|
|
+ bonus.info3 = bjson["amount"].Integer();
|
|
|
+ }
|
|
|
+ ret.bonusesToChoose.push_back(bonus);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case 2: //reading of players (colors / scenarios ?) player can choose
|
|
|
+ {
|
|
|
+ for(auto & bjson : reader["bonuses"].Vector())
|
|
|
+ {
|
|
|
+ CScenarioTravel::STravelBonus bonus;
|
|
|
+ bonus.type = CScenarioTravel::STravelBonus::HEROES_FROM_PREVIOUS_SCENARIO;
|
|
|
+ bonus.info1 = bjson["playerColor"].Integer(); //player color
|
|
|
+ bonus.info2 = bjson["scenario"].Integer(); //from what scenario
|
|
|
+ ret.bonusesToChoose.push_back(bonus);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case 3: //heroes player can choose between
|
|
|
+ {
|
|
|
+ for(auto & bjson : reader["bonuses"].Vector())
|
|
|
+ {
|
|
|
+ CScenarioTravel::STravelBonus bonus;
|
|
|
+ bonus.type = CScenarioTravel::STravelBonus::HERO;
|
|
|
+ bonus.info1 = bjson["playerColor"].Integer(); //player color
|
|
|
+
|
|
|
+ if(int heroId = heroSpecialMap[bjson["hero"].String()])
|
|
|
+ bonus.info2 = heroId;
|
|
|
+ else
|
|
|
+ bonus.info2 = VLC->modh->identifiers.getIdentifier(CModHandler::scopeMap(), "hero", bjson["hero"].String()).get();
|
|
|
+
|
|
|
+ ret.bonusesToChoose.push_back(bonus);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ {
|
|
|
+ logGlobal->warn("Corrupted h3c file");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
CCampaignHeader CCampaignHandler::readHeaderFromMemory( CBinaryReader & reader, std::string filename, std::string modName, std::string encoding )
|
|
|
{
|
|
|
CCampaignHeader ret;
|
|
@@ -167,6 +387,7 @@ CCampaignHeader CCampaignHandler::readHeaderFromMemory( CBinaryReader & reader,
|
|
|
ret.filename = filename;
|
|
|
ret.modName = modName;
|
|
|
ret.encoding = encoding;
|
|
|
+ ret.valid = true;
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
@@ -490,12 +711,16 @@ CMap * CCampaignState::getMap(int scenarioId) const
|
|
|
// FIXME: there is certainly better way to handle maps inside campaigns
|
|
|
if(scenarioId == -1)
|
|
|
scenarioId = currentMap.get();
|
|
|
+
|
|
|
+ CMapService mapService;
|
|
|
+ if(camp->header.version == CampaignVersion::Version::VCMI)
|
|
|
+ return mapService.loadMap(ResourceID(camp->scenarios.at(scenarioId).mapName, EResType::MAP)).release();
|
|
|
+
|
|
|
std::string scenarioName = camp->header.filename.substr(0, camp->header.filename.find('.'));
|
|
|
boost::to_lower(scenarioName);
|
|
|
scenarioName += ':' + std::to_string(scenarioId);
|
|
|
std::string & mapContent = camp->mapPieces.find(scenarioId)->second;
|
|
|
const auto * buffer = reinterpret_cast<const ui8 *>(mapContent.data());
|
|
|
- CMapService mapService;
|
|
|
return mapService.loadMap(buffer, static_cast<int>(mapContent.size()), scenarioName, camp->header.modName, camp->header.encoding).release();
|
|
|
}
|
|
|
|
|
@@ -503,13 +728,16 @@ std::unique_ptr<CMapHeader> CCampaignState::getHeader(int scenarioId) const
|
|
|
{
|
|
|
if(scenarioId == -1)
|
|
|
scenarioId = currentMap.get();
|
|
|
+
|
|
|
+ CMapService mapService;
|
|
|
+ if(camp->header.version == CampaignVersion::Version::VCMI)
|
|
|
+ return mapService.loadMapHeader(ResourceID(camp->scenarios.at(scenarioId).mapName, EResType::MAP));
|
|
|
|
|
|
std::string scenarioName = camp->header.filename.substr(0, camp->header.filename.find('.'));
|
|
|
boost::to_lower(scenarioName);
|
|
|
scenarioName += ':' + std::to_string(scenarioId);
|
|
|
std::string & mapContent = camp->mapPieces.find(scenarioId)->second;
|
|
|
const auto * buffer = reinterpret_cast<const ui8 *>(mapContent.data());
|
|
|
- CMapService mapService;
|
|
|
return mapService.loadMapHeader(buffer, static_cast<int>(mapContent.size()), scenarioName, camp->header.modName, camp->header.encoding);
|
|
|
}
|
|
|
|