2015-02-26 04:57:28 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <fstream>
|
2016-08-04 14:07:11 +02:00
|
|
|
#include <boost/range/iterator_range.hpp>
|
2016-08-04 02:09:47 +02:00
|
|
|
#include <platform/FileIndex.hpp>
|
|
|
|
#include <loaders/LoaderIMG.hpp>
|
2015-02-26 04:57:28 +01:00
|
|
|
|
2016-08-04 02:09:47 +02:00
|
|
|
using namespace boost::filesystem;
|
2015-03-06 02:40:29 +01:00
|
|
|
|
2016-08-28 17:23:34 +02:00
|
|
|
void FileIndex::indexGameDirectory(const fs::path& base_path)
|
2016-08-04 02:50:41 +02:00
|
|
|
{
|
2016-08-28 17:23:34 +02:00
|
|
|
gamedatapath_ = base_path;
|
2016-08-04 02:50:41 +02:00
|
|
|
|
2016-08-28 17:23:34 +02:00
|
|
|
for(const path& entry : boost::make_iterator_range(recursive_directory_iterator(base_path), {})) {
|
|
|
|
if(is_regular_file(entry)) {
|
2016-09-08 21:17:30 +02:00
|
|
|
std::string name = entry.string();
|
2016-08-28 17:23:34 +02:00
|
|
|
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
|
2016-08-04 02:50:41 +02:00
|
|
|
|
2016-08-28 17:23:34 +02:00
|
|
|
filesystemfiles_[name] = entry;
|
2016-08-04 02:50:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-31 21:36:29 +02:00
|
|
|
FileHandle FileIndex::openFilePath(const std::string &file_path)
|
|
|
|
{
|
|
|
|
auto datapath = findFilePath(file_path);
|
2016-09-08 21:17:30 +02:00
|
|
|
std::ifstream dfile((const char*)datapath.c_str(), std::ios_base::binary | std::ios_base::ate);
|
2016-08-31 21:36:29 +02:00
|
|
|
if ( ! dfile.is_open()) {
|
|
|
|
throw std::runtime_error("Unable to open file: " + file_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto length = dfile.tellg();
|
|
|
|
dfile.seekg(0);
|
|
|
|
auto data = new char[length];
|
|
|
|
dfile.read(data, length);
|
|
|
|
|
|
|
|
return std::make_shared<FileContentsInfo> ( data, length );
|
|
|
|
}
|
|
|
|
|
2015-03-06 02:40:29 +01:00
|
|
|
void FileIndex::indexTree(const std::string& root)
|
|
|
|
{
|
2016-08-04 14:07:11 +02:00
|
|
|
for(const path& entry : boost::make_iterator_range(recursive_directory_iterator(root), {})) {
|
|
|
|
std::string directory = entry.parent_path().string();
|
|
|
|
std::string realName = entry.filename().string();
|
2016-08-04 02:09:47 +02:00
|
|
|
std::string lowerName = realName;
|
|
|
|
std::transform(lowerName.begin(), lowerName.end(), lowerName.begin(), ::tolower);
|
2016-05-24 00:54:46 +02:00
|
|
|
|
2016-08-04 14:07:11 +02:00
|
|
|
if(is_regular_file(entry)) {
|
2016-08-04 02:09:47 +02:00
|
|
|
files[lowerName] = {lowerName, realName, directory, ""};
|
2015-03-06 02:40:29 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-26 04:57:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileIndex::indexArchive(const std::string& archive)
|
|
|
|
{
|
|
|
|
// Split directory from archive name
|
2016-08-04 02:09:47 +02:00
|
|
|
path archive_path = path(archive);
|
|
|
|
path directory = archive_path.parent_path();
|
2016-08-04 14:07:11 +02:00
|
|
|
path archive_basename = archive_path.filename();
|
2016-08-04 02:09:47 +02:00
|
|
|
path archive_full_path = directory/archive_basename;
|
2015-02-26 04:57:28 +01:00
|
|
|
|
2016-08-04 02:09:47 +02:00
|
|
|
LoaderIMG img;
|
|
|
|
if(!img.load(archive_full_path.string())) {
|
|
|
|
throw std::runtime_error("Failed to load IMG archive: " + archive_full_path.string());
|
2015-02-26 04:57:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string lowerName;
|
2016-08-04 02:09:47 +02:00
|
|
|
for( size_t i = 0; i < img.getAssetCount(); ++i ) {
|
2015-02-26 04:57:28 +01:00
|
|
|
auto& asset = img.getAssetInfoByIndex(i);
|
|
|
|
|
|
|
|
if( asset.size == 0 ) continue;
|
|
|
|
|
|
|
|
lowerName = asset.name;
|
|
|
|
std::transform(lowerName.begin(), lowerName.end(), lowerName.begin(), ::tolower);
|
|
|
|
|
2016-08-04 02:09:47 +02:00
|
|
|
files[lowerName] = {lowerName, asset.name, directory.string(), archive_basename.string()};
|
2015-02-26 04:57:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FileHandle FileIndex::openFile(const std::string& filename)
|
|
|
|
{
|
|
|
|
auto iterator = files.find( filename );
|
2016-08-04 02:09:47 +02:00
|
|
|
if( iterator == files.end() ) {
|
2015-02-26 04:57:28 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexData& f = iterator->second;
|
|
|
|
bool isArchive = !f.archive.empty();
|
|
|
|
|
|
|
|
auto fsName = f.directory + "/" + f.originalName;
|
|
|
|
|
|
|
|
char* data = nullptr;
|
|
|
|
size_t length = 0;
|
|
|
|
|
2016-08-04 02:09:47 +02:00
|
|
|
if( isArchive ) {
|
2015-02-26 04:57:28 +01:00
|
|
|
fsName = f.directory + "/" + f.archive;
|
|
|
|
|
|
|
|
LoaderIMG img;
|
|
|
|
|
2016-08-04 02:09:47 +02:00
|
|
|
if( ! img.load(fsName) ) {
|
2015-02-26 04:57:28 +01:00
|
|
|
throw std::runtime_error("Failed to load IMG archive: " + fsName);
|
|
|
|
}
|
|
|
|
|
|
|
|
LoaderIMGFile file;
|
2016-08-04 02:09:47 +02:00
|
|
|
if( img.findAssetInfo(f.originalName, file) ) {
|
2015-02-26 04:57:28 +01:00
|
|
|
length = file.size * 2048;
|
|
|
|
data = img.loadToMemory(f.originalName);
|
|
|
|
}
|
|
|
|
}
|
2016-08-04 02:09:47 +02:00
|
|
|
else {
|
2016-06-08 20:37:00 +02:00
|
|
|
std::ifstream dfile(fsName.c_str(), std::ios_base::binary);
|
2015-02-26 04:57:28 +01:00
|
|
|
if ( ! dfile.is_open()) {
|
|
|
|
throw std::runtime_error("Unable to open file: " + fsName);
|
|
|
|
}
|
|
|
|
|
|
|
|
dfile.seekg(0, std::ios_base::end);
|
2015-03-06 02:40:29 +01:00
|
|
|
length = dfile.tellg();
|
2015-02-26 04:57:28 +01:00
|
|
|
dfile.seekg(0);
|
2015-03-06 02:40:29 +01:00
|
|
|
data = new char[length];
|
2015-02-26 04:57:28 +01:00
|
|
|
dfile.read(data, length);
|
|
|
|
}
|
|
|
|
|
2016-08-04 02:09:47 +02:00
|
|
|
if( data == nullptr ) {
|
2015-03-06 02:40:29 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-02-26 04:57:28 +01:00
|
|
|
return FileHandle( new FileContentsInfo{ data, length } );
|
|
|
|
}
|