void PakReader::removeFile(const res::path & file) { PakDirectory * dir = getDirectory(file.parent()); if(dir) { dir->removeFile(file.filename()); } }
static void dump(PakDirectory & dir, const fs::path & dirname = fs::path()) { if(!fs::create_directories(dirname)) { LogWarning << "Failed to create target directory"; } for(PakDirectory::files_iterator i = dir.files_begin(); i != dir.files_end(); ++i) { fs::path filenameISO = dirname / i->first; PakFile * file = i->second; #if ARX_PLATFORM == ARX_PLATFORM_WIN32 std::string filename = filenameISO.string(); #else std::string filename = util::convert<util::ISO_8859_1, util::UTF8>(filenameISO.string().c_str()); #endif printf("%s\n", filename.c_str()); fs::ofstream ofs(filename, fs::fstream::out | fs::fstream::binary | fs::fstream::trunc); if(!ofs.is_open()) { printf("error opening file for writing: %s\n", filename.c_str()); exit(1); } if(file->size() > 0) { char * data = (char*)file->readAlloc(); arx_assert(data != NULL); if(ofs.write(data, file->size()).fail()) { printf("error writing to file: %s\n", filename.c_str()); exit(1); } free(data); } } for(PakDirectory::dirs_iterator i = dir.dirs_begin(); i != dir.dirs_end(); ++i) { dump(i->second, dirname / i->first); } }
void dump(PakDirectory & dir, const fs::path & dirname = fs::path()) { fs::create_directories(dirname); for(PakDirectory::files_iterator i = dir.files_begin(); i != dir.files_end(); ++i) { fs::path filename = dirname / i->first; PakFile * file = i->second; printf("%s\n", filename.string().c_str()); fs::ofstream ofs(filename, fs::fstream::out | fs::fstream::binary | fs::fstream::trunc); if(!ofs.is_open()) { printf("error opening file for writing: %s\n", filename.string().c_str()); exit(1); } if(file->size() > 0) { char * data = (char*)file->readAlloc(); arx_assert(data != NULL); if(ofs.write(data, file->size()).fail()) { printf("error writing to file: %s\n", filename.string().c_str()); exit(1); } free(data); } } for(PakDirectory::dirs_iterator i = dir.dirs_begin(); i != dir.dirs_end(); ++i) { dump(i->second, dirname / i->first); } }
static EERIE_MULTI3DSCENE * PAK_MultiSceneToEerie_Impl(const res::path & dirr) { EERIE_MULTI3DSCENE * es = allocStructZero<EERIE_MULTI3DSCENE>(); LastLoadedScene = dirr; PakDirectory * dir = resources->getDirectory(dirr); if(dir) { bool loaded = false; for(PakDirectory::files_iterator i = dir->files_begin(); i != dir->files_end(); i++) { if(!res::path(i->first).has_ext("scn")) { continue; } char * adr = i->second->readAlloc(); if(adr) { es->scenes[es->nb_scenes] = ScnToEerie(adr, i->second->size(), dirr); es->nb_scenes++; free(adr); } else { LogError << "Could not read scene " << dirr << '/' << i->first; } loaded = true; } if(!loaded) { LogWarning << "Empty multiscene: " << dirr; } } else { LogWarning << "Multiscene not found: " << dirr; } es->cub.xmax = -9999999999.f; es->cub.xmin = 9999999999.f; es->cub.ymax = -9999999999.f; es->cub.ymin = 9999999999.f; es->cub.zmax = -9999999999.f; es->cub.zmin = 9999999999.f; for(long i = 0; i < es->nb_scenes; i++) { es->cub.xmax = std::max(es->cub.xmax, es->scenes[i]->cub.xmax); es->cub.xmin = std::min(es->cub.xmin, es->scenes[i]->cub.xmin); es->cub.ymax = std::max(es->cub.ymax, es->scenes[i]->cub.ymax); es->cub.ymin = std::min(es->cub.ymin, es->scenes[i]->cub.ymin); es->cub.zmax = std::max(es->cub.zmax, es->scenes[i]->cub.zmax); es->cub.zmin = std::min(es->cub.zmin, es->scenes[i]->cub.zmin); es->pos = es->scenes[i]->pos; if((es->scenes[i]->point0.x != -999999999999.f) && (es->scenes[i]->point0.y != -999999999999.f) && (es->scenes[i]->point0.z != -999999999999.f)) { es->point0 = es->scenes[i]->point0; } } if(es->nb_scenes == 0) { free(es); return NULL; } return es; }
static PakFile * autodetectLanguage() { PakDirectory * dir = resources->getDirectory("localisation"); if(!dir) { LogCritical << "Missing 'localisation' directory. Is 'loc.pak' present?"; return NULL; } std::ostringstream languages; PakFile * localisation = NULL; PakDirectory::files_iterator file = dir->files_begin(); for(; file != dir->files_end(); ++file) { const std::string & name = file->first; const std::string prefix = "utext_"; const std::string suffix = ".ini"; if(!boost::starts_with(name, prefix) || !boost::ends_with(name, suffix)) { // Not a localisation file. continue; } if(name.length() <= prefix.length() + suffix.length()) { // Missing language name. continue; } // Extract the language name. size_t length = name.length() - prefix.length() - suffix.length(); std::string language = name.substr(prefix.length(), length); if(!localisation) { localisation = file->second; config.language = language; } else { if(!languages.tellp()) { languages << config.language; } languages << ", " << language; // Prefer english if there are multiple localisations. if(language == "english") { localisation = file->second; config.language = language; } } } if(!localisation) { LogCritical << "Could not find any localisation file. (localisation/utext_*.ini)"; return NULL; } if(languages.tellp()) { LogWarning << "Multiple localisations avalable: " << languages.rdbuf(); } LogInfo << "Autodetected language: " << config.language; return localisation; }
bool PakReader::addArchive(const fs::path & pakfile) { fs::ifstream * ifs = new fs::ifstream(pakfile, fs::fstream::in | fs::fstream::binary); if(!ifs->is_open()) { delete ifs; return false; } // Read fat location and size. u32 fat_offset; u32 fat_size; if(fs::read(*ifs, fat_offset).fail()) { LogError << pakfile << ": error reading FAT offset"; delete ifs; return false; } if(ifs->seekg(fat_offset).fail()) { LogError << pakfile << ": error seeking to FAT offset " << fat_offset; delete ifs; return false; } if(fs::read(*ifs, fat_size).fail()) { LogError << pakfile << ": error reading FAT size at offset " << fat_offset; delete ifs; return false; } // Read the whole FAT. char * fat = new char[fat_size]; if(ifs->read(fat, fat_size).fail()) { LogError << pakfile << ": error reading FAT at " << fat_offset << " with size " << fat_size; delete[] fat; delete ifs; return false; } // Decrypt the FAT. ReleaseType key = guessReleaseType(*reinterpret_cast<const u32 *>(fat)); if(key != Unknown) { pakDecrypt(fat, fat_size, key); } else { LogWarning << pakfile << ": unknown PAK key ID 0x" << std::hex << std::setfill('0') << std::setw(8) << *(u32*)fat << ", assuming no key"; } release |= key; char * pos = fat; paks.push_back(ifs); while(fat_size) { char * dirname = safeGetString(pos, fat_size); if(!dirname) { LogError << pakfile << ": error reading directory name from FAT, wrong key?"; goto error; } PakDirectory * dir = addDirectory(res::path::load(dirname)); u32 nfiles; if(!safeGet(nfiles, pos, fat_size)) { LogError << pakfile << ": error reading file count from FAT, wrong key?"; goto error; } while(nfiles--) { char * filename = safeGetString(pos, fat_size); if(!filename) { LogError << pakfile << ": error reading file name from FAT, wrong key?"; goto error; } size_t len = strlen(filename); std::transform(filename, filename + len, filename, ::tolower); u32 offset; u32 flags; u32 uncompressedSize; u32 size; if(!safeGet(offset, pos, fat_size) || !safeGet(flags, pos, fat_size) || !safeGet(uncompressedSize, pos, fat_size) || !safeGet(size, pos, fat_size)) { LogError << pakfile << ": error reading file attributes from FAT, wrong key?"; goto error; } const u32 PAK_FILE_COMPRESSED = 1; PakFile * file; if((flags & PAK_FILE_COMPRESSED) && size != 0) { file = new CompressedFile(ifs, offset, uncompressedSize, size); } else { file = new UncompressedFile(ifs, offset, size); } dir->addFile(string(filename, len), file); } } delete[] fat; LogInfo << "Loaded PAK " << pakfile; return true; error: delete[] fat; return false; }