|
@@ -16,6 +16,7 @@
|
|
|
#include "../filesystem/CCompressedStream.h"
|
|
|
#include "../filesystem/CMemoryStream.h"
|
|
|
#include "../filesystem/CBinaryReader.h"
|
|
|
+#include "../filesystem/CZipLoader.h"
|
|
|
#include "../VCMI_Lib.h"
|
|
|
#include "../constants/StringConstants.h"
|
|
|
#include "../mapping/CMapHeader.h"
|
|
@@ -126,14 +127,19 @@ static std::string convertMapName(std::string input)
|
|
|
|
|
|
std::string CampaignHandler::readLocalizedString(CampaignHeader & target, CBinaryReader & reader, std::string filename, std::string modName, std::string encoding, std::string identifier)
|
|
|
{
|
|
|
- TextIdentifier stringID( "campaign", convertMapName(filename), identifier);
|
|
|
-
|
|
|
std::string input = TextOperations::toUnicode(reader.readBaseString(), encoding);
|
|
|
|
|
|
- if (input.empty())
|
|
|
+ return readLocalizedString(target, input, filename, modName, identifier);
|
|
|
+}
|
|
|
+
|
|
|
+std::string CampaignHandler::readLocalizedString(CampaignHeader & target, std::string text, std::string filename, std::string modName, std::string identifier)
|
|
|
+{
|
|
|
+ TextIdentifier stringID( "campaign", convertMapName(filename), identifier);
|
|
|
+
|
|
|
+ if (text.empty())
|
|
|
return "";
|
|
|
|
|
|
- target.getTexts().registerString(modName, stringID, input);
|
|
|
+ target.getTexts().registerString(modName, stringID, text);
|
|
|
return stringID.get();
|
|
|
}
|
|
|
|
|
@@ -149,8 +155,8 @@ void CampaignHandler::readHeaderFromJson(CampaignHeader & ret, JsonNode & reader
|
|
|
ret.version = CampaignVersion::VCMI;
|
|
|
ret.campaignRegions = CampaignRegions::fromJson(reader["regions"]);
|
|
|
ret.numberOfScenarios = reader["scenarios"].Vector().size();
|
|
|
- ret.name.appendTextID(reader["name"].String());
|
|
|
- ret.description.appendTextID(reader["description"].String());
|
|
|
+ ret.name.appendTextID(readLocalizedString(ret, reader["name"].String(), filename, modName, "name"));
|
|
|
+ ret.description.appendTextID(readLocalizedString(ret, reader["description"].String(), filename, modName, "description"));
|
|
|
ret.author.appendRawString(reader["author"].String());
|
|
|
ret.authorContact.appendRawString(reader["authorContact"].String());
|
|
|
ret.campaignVersion.appendRawString(reader["campaignVersion"].String());
|
|
@@ -588,32 +594,69 @@ CampaignTravel CampaignHandler::readScenarioTravelFromMemory(CBinaryReader & rea
|
|
|
|
|
|
std::vector< std::vector<ui8> > CampaignHandler::getFile(std::unique_ptr<CInputStream> file, const std::string & filename, bool headerOnly)
|
|
|
{
|
|
|
- CCompressedStream stream(std::move(file), true);
|
|
|
+ std::array<ui8, 2> magic;
|
|
|
+ file->read(magic.data(), magic.size());
|
|
|
+ file->seek(0);
|
|
|
|
|
|
std::vector< std::vector<ui8> > ret;
|
|
|
|
|
|
- try
|
|
|
+ static const std::array<ui8, 2> zipHeaderMagic{0x50, 0x4B};
|
|
|
+ if (magic == zipHeaderMagic) // ZIP archive - assume VCMP format
|
|
|
{
|
|
|
- do
|
|
|
+ CInputStream * buffer(file.get());
|
|
|
+ std::shared_ptr<CIOApi> ioApi(new CProxyROIOApi(buffer));
|
|
|
+ CZipLoader loader("", "_", ioApi);
|
|
|
+
|
|
|
+ // load header
|
|
|
+ JsonPath resource = JsonPath::builtin(VCMP_HEADER_FILE_NAME);
|
|
|
+ if(!loader.existsResource(resource))
|
|
|
+ throw std::runtime_error(resource.getName() + " not found in " + filename);
|
|
|
+ auto data = loader.load(resource)->readAll();
|
|
|
+ ret.push_back(std::vector<ui8>(data.first.get(), data.first.get() + data.second));
|
|
|
+
|
|
|
+ if(headerOnly)
|
|
|
+ return ret;
|
|
|
+
|
|
|
+ // load scenarios
|
|
|
+ JsonNode header(reinterpret_cast<const std::byte*>(data.first.get()), data.second, VCMP_HEADER_FILE_NAME);
|
|
|
+ for(auto scenario : header["scenarios"].Vector())
|
|
|
{
|
|
|
- std::vector<ui8> block(stream.getSize());
|
|
|
- stream.read(block.data(), block.size());
|
|
|
- ret.push_back(block);
|
|
|
- ret.back().shrink_to_fit();
|
|
|
+ ResourcePath resource = ResourcePath(scenario["map"].String(), EResType::MAP);
|
|
|
+ if(!loader.existsResource(resource))
|
|
|
+ throw std::runtime_error(resource.getName() + " not found in " + filename);
|
|
|
+ auto data = loader.load(resource)->readAll();
|
|
|
+ ret.push_back(std::vector<ui8>(data.first.get(), data.first.get() + data.second));
|
|
|
}
|
|
|
- while (!headerOnly && stream.getNextBlock());
|
|
|
+
|
|
|
+ return ret;
|
|
|
}
|
|
|
- catch (const DecompressionException & e)
|
|
|
+ else // H3C
|
|
|
{
|
|
|
- // Some campaigns in French version from gog.com have trailing garbage bytes
|
|
|
- // For example, slayer.h3c consist from 5 parts: header + 4 maps
|
|
|
- // However file also contains ~100 "extra" bytes after those 5 parts are decompressed that do not represent gzip stream
|
|
|
- // leading to exception "Incorrect header check"
|
|
|
- // Since H3 handles these files correctly, simply log this as warning and proceed
|
|
|
- logGlobal->warn("Failed to read file %s. Encountered error during decompression: %s", filename, e.what());
|
|
|
- }
|
|
|
+ CCompressedStream stream(std::move(file), true);
|
|
|
|
|
|
- return ret;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ do
|
|
|
+ {
|
|
|
+ std::vector<ui8> block(stream.getSize());
|
|
|
+ stream.read(block.data(), block.size());
|
|
|
+ ret.push_back(block);
|
|
|
+ ret.back().shrink_to_fit();
|
|
|
+ }
|
|
|
+ while (!headerOnly && stream.getNextBlock());
|
|
|
+ }
|
|
|
+ catch (const DecompressionException & e)
|
|
|
+ {
|
|
|
+ // Some campaigns in French version from gog.com have trailing garbage bytes
|
|
|
+ // For example, slayer.h3c consist from 5 parts: header + 4 maps
|
|
|
+ // However file also contains ~100 "extra" bytes after those 5 parts are decompressed that do not represent gzip stream
|
|
|
+ // leading to exception "Incorrect header check"
|
|
|
+ // Since H3 handles these files correctly, simply log this as warning and proceed
|
|
|
+ logGlobal->warn("Failed to read file %s. Encountered error during decompression: %s", filename, e.what());
|
|
|
+ }
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
VideoPath CampaignHandler::prologVideoName(ui8 index)
|