1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
/*
* PakLoader.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "PakLoader.h"
#include "../../../lib/filesystem/Filesystem.h"
#include "../../../lib/filesystem/CBinaryReader.h"
std::vector<std::vector<std::string>> stringtoTable(const std::string& input)
{
std::vector<std::vector<std::string>> result;
std::vector<std::string> lines;
boost::split(lines, input, boost::is_any_of("\n"));
for(auto& line : lines)
{
boost::trim(line);
if(line.empty())
continue;
std::vector<std::string> tokens;
boost::split(tokens, line, boost::is_any_of(" "), boost::token_compress_on);
result.push_back(tokens);
}
return result;
}
bool endsWithAny(const std::string & s, const std::unordered_set<std::string> & suffixes)
{
for(const auto & suf : suffixes)
if(boost::algorithm::ends_with(s, suf))
return true;
return false;
}
void PakLoader::loadPak(ResourcePath path, int scale, std::unordered_set<std::string> animToSkip, std::unordered_set<std::string> imagesToSkip, std::unordered_set<std::string> suffixesToSkip)
{
auto file = CResourceHandler::get()->load(path);
CBinaryReader reader(file.get());
std::vector<ArchiveEntry> archiveEntries;
[[maybe_unused]] uint32_t magic = reader.readUInt32();
uint32_t headerOffset = reader.readUInt32();
assert(magic == 4);
file->seek(headerOffset);
uint32_t entriesCount = reader.readUInt32();
for(uint32_t i = 0; i < entriesCount; ++i)
{
ArchiveEntry entry;
std::string buf(20, '\0');
reader.read(reinterpret_cast<ui8*>(buf.data()), buf.size());
size_t len = buf.find('\0');
std::string s = buf.substr(0, len);
entry.name = boost::algorithm::to_upper_copy(s);
entry.metadataOffset = reader.readUInt32();
entry.metadataSize = reader.readUInt32();
entry.countSheets = reader.readUInt32();
entry.compressedSize = reader.readUInt32();
entry.fullSize = reader.readUInt32();
entry.sheets.resize(entry.countSheets);
for(uint32_t j = 0; j < entry.countSheets; ++j)
entry.sheets[j].compressedSize = reader.readUInt32();
for(uint32_t j = 0; j < entry.countSheets; ++j)
entry.sheets[j].fullSize = reader.readUInt32();
entry.scale = scale;
if(animToSkip.find(entry.name) == animToSkip.end() && !endsWithAny(entry.name, suffixesToSkip))
archiveEntries.push_back(entry);
}
for(auto & entry : archiveEntries)
{
file->seek(entry.metadataOffset);
std::string buf(entry.metadataSize, '\0');
reader.read(reinterpret_cast<ui8*>(buf.data()), buf.size());
size_t len = buf.find('\0');
std::string data = buf.substr(0, len);
auto table = stringtoTable(data);
for(const auto & sheet : entry.sheets)
reader.skip(sheet.compressedSize);
ImageEntry image;
for(const auto & line : table)
{
assert(line.size() == 12 || line.size() == 18);
image.name = boost::algorithm::to_upper_copy(line[0]);
image.sheetIndex = std::stol(line[1]);
image.spriteOffsetX = std::stol(line[2]);
image.unknown1 = std::stol(line[3]);
image.spriteOffsetY = std::stol(line[4]);
image.unknown2 = std::stol(line[5]);
image.sheetOffsetX = std::stol(line[6]);
image.sheetOffsetY = std::stol(line[7]);
image.width = std::stol(line[8]);
image.height = std::stol(line[9]);
image.rotation = std::stol(line[10]);
image.hasShadow = std::stol(line[11]);
assert(image.rotation == 0 || image.rotation == 1);
if(image.hasShadow)
{
image.shadowSheetIndex = std::stol(line[12]);
image.shadowSheetOffsetX = std::stol(line[13]);
image.shadowSheetOffsetY = std::stol(line[14]);
image.shadowWidth = std::stol(line[15]);
image.shadowHeight = std::stol(line[16]);
image.shadowRotation = std::stol(line[17]);
assert(image.shadowRotation == 0 || image.shadowRotation == 1);
}
if(imagesToSkip.find(image.name) == imagesToSkip.end() && !endsWithAny(image.name, suffixesToSkip))
entry.images.push_back(image);
}
}
content[path] = archiveEntries;
// Build indices for fast lookup
for(auto& entry : content[path])
{
for(auto& image : entry.images)
imagesByName[scale].try_emplace(image.name, path, &image, &entry);
}
}
|