1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-07 03:12:36 +01:00

Merge pull request #526 from madebr/rwlib_changes

Rwlib changes
This commit is contained in:
Daniel Evans 2018-07-05 22:05:56 +01:00 committed by GitHub
commit 83ee9ef568
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
49 changed files with 240 additions and 218 deletions

View File

@ -2,7 +2,7 @@
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <rw/defines.hpp> #include <rw/debug.hpp>
namespace RW { namespace RW {

View File

@ -4,7 +4,7 @@
#include <cstring> #include <cstring>
#include <memory> #include <memory>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include "BinaryStream.hpp" #include "BinaryStream.hpp"

View File

@ -7,7 +7,7 @@
#include "ai/AIGraphNode.hpp" #include "ai/AIGraphNode.hpp"
#include "data/PathData.hpp" #include "data/PathData.hpp"
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include <rw/types.hpp> #include <rw/types.hpp>
AIGraph::~AIGraph() { AIGraph::~AIGraph() {

View File

@ -11,7 +11,7 @@
#include <LinearMath/btScalar.h> #include <LinearMath/btScalar.h>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include "data/WeaponData.hpp" #include "data/WeaponData.hpp"
#include "engine/Animator.hpp" #include "engine/Animator.hpp"

View File

@ -8,7 +8,7 @@ extern "C" {
#include <libavutil/avutil.h> #include <libavutil/avutil.h>
} }
//ab //ab
#include <rw/defines.hpp> #include <rw/debug.hpp>
// Rename some functions for older libavcodec/ffmpeg versions (e.g. Ubuntu Trusty) // Rename some functions for older libavcodec/ffmpeg versions (e.g. Ubuntu Trusty)
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1) #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,28,1)
@ -64,7 +64,7 @@ bool SoundManager::initializeAVCodec() {
return true; return true;
} }
void SoundManager::SoundSource::loadFromFile(const std::string& filename) { void SoundManager::SoundSource::loadFromFile(const rwfs::path& filePath) {
// Allocate audio frame // Allocate audio frame
AVFrame* frame = av_frame_alloc(); AVFrame* frame = av_frame_alloc();
if (!frame) { if (!frame) {
@ -74,9 +74,9 @@ void SoundManager::SoundSource::loadFromFile(const std::string& filename) {
// Allocate formatting context // Allocate formatting context
AVFormatContext* formatContext = nullptr; AVFormatContext* formatContext = nullptr;
if (avformat_open_input(&formatContext, filename.c_str(), nullptr, nullptr) != 0) { if (avformat_open_input(&formatContext, filePath.string().c_str(), nullptr, nullptr) != 0) {
av_frame_free(&frame); av_frame_free(&frame);
RW_ERROR("Error opening audio file (" << filename << ")"); RW_ERROR("Error opening audio file (" << filePath << ")");
return; return;
} }
@ -93,7 +93,7 @@ void SoundManager::SoundSource::loadFromFile(const std::string& filename) {
if (streamIndex < 0) { if (streamIndex < 0) {
av_frame_free(&frame); av_frame_free(&frame);
avformat_close_input(&formatContext); avformat_close_input(&formatContext);
RW_ERROR("Could not find any audio stream in the file " << filename); RW_ERROR("Could not find any audio stream in the file " << filePath);
return; return;
} }
@ -196,7 +196,7 @@ bool SoundManager::SoundBuffer::bufferData(SoundSource& soundSource) {
} }
bool SoundManager::loadSound(const std::string& name, bool SoundManager::loadSound(const std::string& name,
const std::string& fileName) { const rwfs::path& path) {
Sound* sound = nullptr; Sound* sound = nullptr;
auto sound_iter = sounds.find(name); auto sound_iter = sounds.find(name);
@ -208,7 +208,7 @@ bool SoundManager::loadSound(const std::string& name,
std::forward_as_tuple()); std::forward_as_tuple());
sound = &emplaced.first->second; sound = &emplaced.first->second;
sound->source.loadFromFile(fileName); sound->source.loadFromFile(path);
sound->isLoaded = sound->buffer.bufferData(sound->source); sound->isLoaded = sound->buffer.bufferData(sound->source);
} }
@ -280,8 +280,8 @@ bool SoundManager::playBackground(const std::string& fileName) {
} }
bool SoundManager::loadMusic(const std::string& name, bool SoundManager::loadMusic(const std::string& name,
const std::string& fileName) { const rwfs::path& path) {
return loadSound(name, fileName); return loadSound(name, path);
} }
void SoundManager::playMusic(const std::string& name) { void SoundManager::playMusic(const std::string& name) {

View File

@ -7,6 +7,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <rw/filesystem.hpp>
#include <al.h> #include <al.h>
#include <alc.h> #include <alc.h>
@ -15,7 +17,7 @@ public:
SoundManager(); SoundManager();
~SoundManager(); ~SoundManager();
bool loadSound(const std::string& name, const std::string& fileName); bool loadSound(const std::string& name, const rwfs::path& path);
bool isLoaded(const std::string& name); bool isLoaded(const std::string& name);
void playSound(const std::string& name); void playSound(const std::string& name);
void pauseSound(const std::string& name); void pauseSound(const std::string& name);
@ -26,9 +28,9 @@ public:
void pauseAllSounds(); void pauseAllSounds();
void resumeAllSounds(); void resumeAllSounds();
bool playBackground(const std::string& fileName); bool playBackground(const std::string& name);
bool loadMusic(const std::string& name, const std::string& fileName); bool loadMusic(const std::string& name, const rwfs::path& path);
void playMusic(const std::string& name); void playMusic(const std::string& name);
void stopMusic(const std::string& name); void stopMusic(const std::string& name);
@ -40,7 +42,7 @@ private:
friend class SoundBuffer; friend class SoundBuffer;
public: public:
void loadFromFile(const std::string& filename); void loadFromFile(const rwfs::path& filePath);
private: private:
std::vector<int16_t> data; std::vector<int16_t> data;

View File

@ -9,7 +9,7 @@
#include <vector> #include <vector>
#define time_unit std::chrono::microseconds #define time_unit std::chrono::microseconds
#include <rw/defines.hpp> #include <rw/debug.hpp>
namespace perf { namespace perf {

View File

@ -3,7 +3,7 @@
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include <rw/forward.hpp> #include <rw/forward.hpp>
// The default animations for every cycle // The default animations for every cycle

View File

@ -4,7 +4,7 @@
#include <cstddef> #include <cstddef>
#include <fstream> #include <fstream>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include "engine/GameWorld.hpp" #include "engine/GameWorld.hpp"
#include "objects/GameObject.hpp" #include "objects/GameObject.hpp"

View File

@ -11,7 +11,7 @@
#include <vector> #include <vector>
#include <data/Clump.hpp> #include <data/Clump.hpp>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include <rw/forward.hpp> #include <rw/forward.hpp>
#include <data/CollisionModel.hpp> #include <data/CollisionModel.hpp>

View File

@ -3,7 +3,7 @@
#include <map> #include <map>
#include <vector> #include <vector>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include <rw/forward.hpp> #include <rw/forward.hpp>
struct AnimationBone; struct AnimationBone;

View File

@ -13,7 +13,7 @@
#include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/predicate.hpp>
#include <data/Clump.hpp> #include <data/Clump.hpp>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include <rw/types.hpp> #include <rw/types.hpp>
#include "core/Logger.hpp" #include "core/Logger.hpp"
@ -39,7 +39,6 @@ GameData::GameData(Logger* log, const rwfs::path& path)
} }
void GameData::load() { void GameData::load() {
index.indexGameDirectory(datpath);
index.indexTree(datpath); index.indexTree(datpath);
loadIMG("models/gta3.img"); loadIMG("models/gta3.img");
@ -189,8 +188,7 @@ void GameData::loadCOL(const size_t zone, const std::string& name) {
} }
void GameData::loadIMG(const std::string& name) { void GameData::loadIMG(const std::string& name) {
auto syspath = index.findFilePath(name).string(); index.indexArchive(name);
index.indexArchive(syspath);
} }
void GameData::loadIPL(const std::string& path) { void GameData::loadIPL(const std::string& path) {
@ -288,7 +286,7 @@ void GameData::loadHandling(const std::string& path) {
} }
SCMFile* GameData::loadSCM(const std::string& path) { SCMFile* GameData::loadSCM(const std::string& path) {
auto scm_h = index.openFilePath(path); auto scm_h = index.openFileRaw(path);
SCMFile* scm = new SCMFile; SCMFile* scm = new SCMFile;
scm->loadFile(scm_h->data, scm_h->length); scm->loadFile(scm_h->data, scm_h->length);
scm_h.reset(); scm_h.reset();
@ -296,7 +294,7 @@ SCMFile* GameData::loadSCM(const std::string& path) {
} }
void GameData::loadGXT(const std::string& name) { void GameData::loadGXT(const std::string& name) {
auto file = index.openFilePath(name); auto file = index.openFileRaw(name);
LoaderGXT loader; LoaderGXT loader;
@ -417,7 +415,7 @@ ClumpPtr GameData::loadClump(const std::string& name, const std::string& texture
} }
void GameData::loadModelFile(const std::string& name) { void GameData::loadModelFile(const std::string& name) {
auto file = index.openFilePath(name); auto file = index.openFileRaw(name);
if (!file) { if (!file) {
logger->log("Data", Logger::Error, "Failed to load model file " + name); logger->log("Data", Logger::Error, "Failed to load model file " + name);
return; return;

View File

@ -14,7 +14,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <platform/FileIndex.hpp> #include <platform/FileIndex.hpp>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include <rw/forward.hpp> #include <rw/forward.hpp>
#include <data/AnimGroup.hpp> #include <data/AnimGroup.hpp>

View File

@ -691,11 +691,7 @@ void GameWorld::PhysicsTickCallback(btDynamicsWorld* physWorld,
} }
void GameWorld::loadCutscene(const std::string& name) { void GameWorld::loadCutscene(const std::string& name) {
std::string lowerName(name); auto datfile = data->index.openFile(name + ".dat");
std::transform(lowerName.begin(), lowerName.end(), lowerName.begin(),
::tolower);
auto datfile = data->index.openFile(lowerName + ".dat");
CutsceneData* cutscene = new CutsceneData; CutsceneData* cutscene = new CutsceneData;
@ -704,7 +700,7 @@ void GameWorld::loadCutscene(const std::string& name) {
loaderdat.load(cutscene->tracks, datfile); loaderdat.load(cutscene->tracks, datfile);
} }
data->loadIFP(lowerName + ".ifp"); data->loadIFP(name + ".ifp");
cutsceneAudioLoaded = data->loadAudioStream(name + ".mp3"); cutsceneAudioLoaded = data->loadAudioStream(name + ".mp3");

View File

@ -1,9 +1,9 @@
#ifndef _RWENGINE_Garage_HPP_ #ifndef _RWENGINE_GARAGE_HPP_
#define _RWENGINE_Garage_HPP_ #define _RWENGINE_GARAGE_HPP_
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp> #include <glm/gtc/quaternion.hpp>
#include <rw/defines.hpp> #include <rw/debug.hpp>
class PlayerController; class PlayerController;

View File

@ -1,6 +1,6 @@
#include "engine/Payphone.hpp" #include "engine/Payphone.hpp"
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include "ai/PlayerController.hpp" #include "ai/PlayerController.hpp"

View File

@ -12,7 +12,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp> #include <glm/gtc/quaternion.hpp>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include "data/ZoneData.hpp" #include "data/ZoneData.hpp"
#include "engine/GameData.hpp" #include "engine/GameData.hpp"

View File

@ -1,6 +1,6 @@
#include "engine/ScreenText.hpp" #include "engine/ScreenText.hpp"
#include <rw/defines.hpp> #include <rw/debug.hpp>
void ScreenText::tick(float dt) { void ScreenText::tick(float dt) {
int millis = dt * 1000; int millis = dt * 1000;

View File

@ -6,7 +6,7 @@
#include <iterator> #include <iterator>
#include <sstream> #include <sstream>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include <data/ModelData.hpp> #include <data/ModelData.hpp>
#include <data/WeaponData.hpp> #include <data/WeaponData.hpp>

View File

@ -7,7 +7,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include "data/CutsceneData.hpp" #include "data/CutsceneData.hpp"
#include "platform/FileHandle.hpp" #include "platform/FileHandle.hpp"

View File

@ -9,7 +9,7 @@
#include <BulletDynamics/Character/btKinematicCharacterController.h> #include <BulletDynamics/Character/btKinematicCharacterController.h>
#include <btBulletDynamicsCommon.h> #include <btBulletDynamicsCommon.h>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include "ai/CharacterController.hpp" #include "ai/CharacterController.hpp"
#include "ai/PlayerController.hpp" #include "ai/PlayerController.hpp"

View File

@ -7,7 +7,7 @@
#include <glm/gtc/quaternion.hpp> #include <glm/gtc/quaternion.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include <rw/forward.hpp> #include <rw/forward.hpp>
#include <data/ModelData.hpp> #include <data/ModelData.hpp>

View File

@ -4,7 +4,7 @@
#include <cstdint> #include <cstdint>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include <objects/GameObject.hpp> #include <objects/GameObject.hpp>

View File

@ -9,7 +9,7 @@
#include <gl/DrawBuffer.hpp> #include <gl/DrawBuffer.hpp>
#include <gl/GeometryBuffer.hpp> #include <gl/GeometryBuffer.hpp>
#include <gl/gl_core_3_3.h> #include <gl/gl_core_3_3.h>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include "render/GameRenderer.hpp" #include "render/GameRenderer.hpp"

View File

@ -7,7 +7,7 @@
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <gl/DrawBuffer.hpp> #include <gl/DrawBuffer.hpp>
#include <rw/defines.hpp> #include <rw/debug.hpp>
namespace { namespace {
constexpr GLuint kUBOIndexScene = 1; constexpr GLuint kUBOIndexScene = 1;

View File

@ -4,7 +4,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include <rw/types.hpp> #include <rw/types.hpp>
#include "engine/GameData.hpp" #include "engine/GameData.hpp"

View File

@ -1,13 +1,14 @@
#ifndef _RWENGINE_SCMFILE_HPP_ #ifndef _RWENGINE_SCMFILE_HPP_
#define _RWENGINE_SCMFILE_HPP_ #define _RWENGINE_SCMFILE_HPP_
#include "script/ScriptTypes.hpp"
#include <rw/bit_cast.hpp>
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <vector> #include <vector>
#include "script/ScriptTypes.hpp"
#include "rw/bit_cast.cpp"
/** /**
* @brief Handles in-memory SCM file data including section offsets. * @brief Handles in-memory SCM file data including section offsets.
*/ */

View File

@ -1,7 +1,7 @@
#ifndef _RWENGINE_SCRIPTFUNCTIONS_HPP_ #ifndef _RWENGINE_SCRIPTFUNCTIONS_HPP_
#define _RWENGINE_SCRIPTFUNCTIONS_HPP_ #define _RWENGINE_SCRIPTFUNCTIONS_HPP_
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include <ai/AIGraphNode.hpp> #include <ai/AIGraphNode.hpp>
#include <data/GameTexts.hpp> #include <data/GameTexts.hpp>

View File

@ -9,7 +9,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include "engine/Garage.hpp" #include "engine/Garage.hpp"

View File

@ -6,7 +6,7 @@
#include <SDL.h> #include <SDL.h>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include "GitSHA1.h" #include "GitSHA1.h"

View File

@ -1,7 +1,7 @@
#include "GameConfig.hpp" #include "GameConfig.hpp"
#include <algorithm> #include <algorithm>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include <rw/filesystem.hpp> #include <rw/filesystem.hpp>
#include <boost/property_tree/ini_parser.hpp> #include <boost/property_tree/ini_parser.hpp>

View File

@ -8,7 +8,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <render/GameRenderer.hpp> #include <render/GameRenderer.hpp>
#include <rw/defines.hpp> #include <rw/debug.hpp>
/** /**
* Default values for menus that should match the look and feel of the original * Default values for menus that should match the look and feel of the original

View File

@ -102,8 +102,8 @@ void IngameState::startTest() {
void IngameState::startGame() { void IngameState::startGame() {
game->startScript("data/main.scm"); game->startScript("data/main.scm");
game->getScriptVM()->startThread(0); game->getScriptVM()->startThread(0);
getWorld()->sound.playBackground(getWorld()->data->getDataPath().string() + auto audioCityPath = getWorld()->data->index.findFilePath("audio/City.wav");
"/audio/City.wav"); // FIXME: use path getWorld()->sound.playBackground(audioCityPath.string()); //FIXME: playBackground should just accept "audio/City.wav"
} }
void IngameState::enter() { void IngameState::enter() {

View File

@ -3,7 +3,7 @@
#include "game.hpp" #include "game.hpp"
#include <engine/SaveGame.hpp> #include <engine/SaveGame.hpp>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include "RWGame.hpp" #include "RWGame.hpp"
MenuState::MenuState(RWGame* game) : State(game) { MenuState::MenuState(RWGame* game) : State(game) {

View File

@ -14,11 +14,11 @@ SET(RWLIB_SOURCES
source/gl/TextureData.cpp source/gl/TextureData.cpp
source/rw/abort.cpp source/rw/abort.cpp
source/rw/bit_cast.cpp source/rw/bit_cast.hpp
source/rw/filesystem.hpp source/rw/filesystem.hpp
source/rw/forward.hpp source/rw/forward.hpp
source/rw/types.hpp source/rw/types.hpp
source/rw/defines.hpp source/rw/debug.hpp
source/platform/FileHandle.hpp source/platform/FileHandle.hpp
source/platform/FileIndex.hpp source/platform/FileIndex.hpp

View File

@ -14,7 +14,7 @@
#include "gl/gl_core_3_3.h" #include "gl/gl_core_3_3.h"
#include "loaders/RWBinaryStream.hpp" #include "loaders/RWBinaryStream.hpp"
#include "platform/FileHandle.hpp" #include "platform/FileHandle.hpp"
#include "rw/defines.hpp" #include "rw/debug.hpp"
enum DFFChunks { enum DFFChunks {
CHUNK_STRUCT = 0x0001, CHUNK_STRUCT = 0x0001,

View File

@ -3,7 +3,7 @@
#include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/predicate.hpp>
#include <cstdio> #include <cstdio>
#include "rw/defines.hpp" #include "rw/debug.hpp"
LoaderIMG::LoaderIMG() : m_version(GTAIIIVC), m_assetCount(0) { LoaderIMG::LoaderIMG() : m_version(GTAIIIVC), m_assetCount(0) {
} }

View File

@ -4,7 +4,7 @@
#include <cstdio> #include <cstdio>
#include <string> #include <string>
#include "rw/defines.hpp" #include "rw/debug.hpp"
LoaderSDT::LoaderSDT() : m_version(GTAIIIVC), m_assetCount(0) { LoaderSDT::LoaderSDT() : m_version(GTAIIIVC), m_assetCount(0) {
} }

View File

@ -10,7 +10,7 @@
#include "gl/gl_core_3_3.h" #include "gl/gl_core_3_3.h"
#include "loaders/RWBinaryStream.hpp" #include "loaders/RWBinaryStream.hpp"
#include "platform/FileHandle.hpp" #include "platform/FileHandle.hpp"
#include "rw/defines.hpp" #include "rw/debug.hpp"
GLuint gErrorTextureData[] = {0xFFFF00FF, 0xFF000000, 0xFF000000, 0xFFFF00FF}; GLuint gErrorTextureData[] = {0xFFFF00FF, 0xFF000000, 0xFF000000, 0xFFFF00FF};
GLuint gDebugTextureData[] = {0xFF0000FF, 0xFF00FF00}; GLuint gDebugTextureData[] = {0xFF0000FF, 0xFF00FF00};

View File

@ -6,8 +6,8 @@
#include <cstddef> #include <cstddef>
#include "rw/defines.hpp" #include "rw/debug.hpp"
#include "rw/bit_cast.cpp" #include "rw/bit_cast.hpp"
/** /**
* @brief Class for working with RenderWare binary streams. * @brief Class for working with RenderWare binary streams.

View File

@ -1,13 +1,11 @@
#ifndef _LIBRW_FILEHANDLE_HPP_ #ifndef _LIBRW_FILEHANDLE_HPP_
#define _LIBRW_FILEHANDLE_HPP_ #define _LIBRW_FILEHANDLE_HPP_
#include <memory>
/** /**
* @brief Contains a pointer to a file's contents. * @brief Contains a pointer to a file's contents.
*/ */
struct FileContentsInfo { struct FileContentsInfo {
char* data; char *data;
size_t length; size_t length;
FileContentsInfo(char* mem, size_t len) : data(mem), length(len) { FileContentsInfo(char* mem, size_t len) : data(mem), length(len) {

View File

@ -1,38 +1,69 @@
#include "platform/FileIndex.hpp" #include "platform/FileIndex.hpp"
#include <algorithm> #include <algorithm>
#include <cctype>
#include <fstream> #include <fstream>
#include <memory> #include <iterator>
#include <stdexcept> #include <sstream>
#include <boost/range/iterator_range.hpp>
#include "FileHandle.hpp"
#include "loaders/LoaderIMG.hpp" #include "loaders/LoaderIMG.hpp"
#include "platform/FileHandle.hpp"
void FileIndex::indexGameDirectory(const rwfs::path& base_path) { #include "rw/debug.hpp"
gamedatapath_ = base_path;
for (const rwfs::path& path : std::string FileIndex::normalizeFilePath(const std::string &filePath) {
rwfs::recursive_directory_iterator(base_path)) { std::ostringstream oss;
std::transform(filePath.cbegin(), filePath.cend(), std::ostreambuf_iterator<char>(oss), [](char c) {
if (c == '\\') {
return '/';
}
return static_cast<char>(std::tolower(c));
});
return oss.str();
}
void FileIndex::indexTree(const rwfs::path &path) {
// Remove the trailing "/" or "/." from base_path. Boost 1.66 and c++17 have different lexically_relative behavior.
rwfs::path basePath = (path / ".").lexically_normal();
basePath = basePath.parent_path();
for (const rwfs::path &path :
rwfs::recursive_directory_iterator(basePath)) {
if (!rwfs::is_regular_file(path)) { if (!rwfs::is_regular_file(path)) {
continue; continue;
} }
auto relPath = path.lexically_relative(basePath);
std::string relPathName = normalizeFilePath(relPath.string());
indexedData_[relPathName] = {IndexedDataType::FILE, path.string(), ""};
std::string name = path.string(); auto filename = normalizeFilePath(path.filename().string());
std::transform(name.begin(), name.end(), name.begin(), ::tolower); indexedData_[filename] = {IndexedDataType::FILE, path.string(), ""};
filesystemfiles_[name] = path;
} }
} }
FileHandle FileIndex::openFilePath(const std::string& file_path) { const FileIndex::IndexedData *FileIndex::getIndexedDataAt(const std::string &filePath) const {
auto datapath = findFilePath(file_path); auto normPath = normalizeFilePath(filePath);
std::ifstream dfile(datapath.string(), return &indexedData_.at(normPath);
std::ios_base::binary | std::ios_base::ate); }
rwfs::path FileIndex::findFilePath(const std::string &filePath) const {
return getIndexedDataAt(filePath)->path;
}
FileHandle FileIndex::openFileRaw(const std::string &filePath) const {
const auto *indexData = getIndexedDataAt(filePath);
std::ifstream dfile(indexData->path, std::ios::binary);
if (!dfile.is_open()) { if (!dfile.is_open()) {
throw std::runtime_error("Unable to open file: " + file_path); throw std::runtime_error("Unable to open file: " + filePath);
} }
#if RW_DEBUG
if (indexData->type != IndexedDataType::FILE) {
RW_MESSAGE("Reading raw data from archive \"" << filePath << "\"");
}
#endif
dfile.seekg(0, std::ios::end);
auto length = dfile.tellg(); auto length = dfile.tellg();
dfile.seekg(0); dfile.seekg(0);
auto data = new char[length]; auto data = new char[length];
@ -41,84 +72,57 @@ FileHandle FileIndex::openFilePath(const std::string& file_path) {
return std::make_shared<FileContentsInfo>(data, length); return std::make_shared<FileContentsInfo>(data, length);
} }
void FileIndex::indexTree(const rwfs::path& root) { void FileIndex::indexArchive(const std::string &archive) {
for (const rwfs::path& path : rwfs::recursive_directory_iterator(root)) { rwfs::path path = findFilePath(archive);
if (!rwfs::is_regular_file(path)) {
continue;
}
std::string directory = path.parent_path().string();
std::string realName = path.filename().string();
std::string lowerName = realName;
std::transform(lowerName.begin(), lowerName.end(), lowerName.begin(),
::tolower);
files[lowerName] = {lowerName, realName, directory, ""};
}
}
void FileIndex::indexArchive(const std::string& archive) {
// Split directory from archive name
auto archive_path = rwfs::path(archive);
auto directory = archive_path.parent_path();
auto archive_basename = archive_path.filename();
auto archive_full_path = directory / archive_basename;
LoaderIMG img; LoaderIMG img;
if (!img.load(archive_full_path.string())) { if (!img.load(path.string())) {
throw std::runtime_error("Failed to load IMG archive: " + throw std::runtime_error("Failed to load IMG archive: " + path.string());
archive_full_path.string());
} }
std::string lowerName;
for (size_t i = 0; i < img.getAssetCount(); ++i) { for (size_t i = 0; i < img.getAssetCount(); ++i) {
auto& asset = img.getAssetInfoByIndex(i); auto &asset = img.getAssetInfoByIndex(i);
if (asset.size == 0) continue; if (asset.size == 0) continue;
lowerName = asset.name; std::string assetName = normalizeFilePath(asset.name);
std::transform(lowerName.begin(), lowerName.end(), lowerName.begin(),
::tolower);
files[lowerName] = {lowerName, asset.name, directory.string(), indexedData_[assetName] = {IndexedDataType::ARCHIVE, path.string(), asset.name};
archive_basename.string()};
} }
} }
FileHandle FileIndex::openFile(const std::string& filename) { FileHandle FileIndex::openFile(const std::string &filePath) {
auto iterator = files.find(filename); auto cleanFilePath = normalizeFilePath(filePath);
if (iterator == files.end()) { auto indexedDataPos = indexedData_.find(cleanFilePath);
if (indexedDataPos == indexedData_.end()) {
return nullptr; return nullptr;
} }
IndexData& f = iterator->second; const auto &indexedData = indexedDataPos->second;
bool isArchive = !f.archive.empty();
auto fsName = f.directory + "/" + f.originalName; char *data = nullptr;
char* data = nullptr;
size_t length = 0; size_t length = 0;
if (isArchive) { if (indexedData.type == IndexedDataType::ARCHIVE) {
fsName = f.directory + "/" + f.archive;
LoaderIMG img; LoaderIMG img;
if (!img.load(fsName)) { if (!img.load(indexedData.path)) {
throw std::runtime_error("Failed to load IMG archive: " + fsName); throw std::runtime_error("Failed to load IMG archive: " + indexedData.path);
} }
LoaderIMGFile file; LoaderIMGFile file;
if (img.findAssetInfo(f.originalName, file)) { auto filename = rwfs::path(indexedData.assetData).filename().string();
if (img.findAssetInfo(filename, file)) {
length = file.size * 2048; length = file.size * 2048;
data = img.loadToMemory(f.originalName); data = img.loadToMemory(filename);
} }
} else { } else {
std::ifstream dfile(fsName.c_str(), std::ios_base::binary); std::ifstream dfile(indexedData.path, std::ios::binary);
if (!dfile.is_open()) { if (!dfile.is_open()) {
throw std::runtime_error("Unable to open file: " + fsName); throw std::runtime_error("Unable to open file: " + indexedData.path);
} }
dfile.seekg(0, std::ios_base::end); dfile.seekg(0, std::ios::end);
length = dfile.tellg(); length = dfile.tellg();
dfile.seekg(0); dfile.seekg(0);
data = new char[length]; data = new char[length];

View File

@ -1,91 +1,96 @@
#ifndef _LIBRW_FILEINDEX_HPP_ #ifndef _LIBRW_FILEINDEX_HPP_
#define _LIBRW_FILEINDEX_HPP_ #define _LIBRW_FILEINDEX_HPP_
#include <algorithm> #include "rw/filesystem.hpp"
#include <cctype> #include "rw/forward.hpp"
#include <map>
#include <string>
#include <unordered_map> #include <unordered_map>
#include <rw/filesystem.hpp>
#include <rw/forward.hpp>
class FileIndex { class FileIndex {
private:
/**
* Mapping type (lower case name) => (on disk name)
*/
using FileSystemMap = std::unordered_map<rwfs::path, rwfs::path>;
rwfs::path gamedatapath_;
FileSystemMap filesystemfiles_;
public: public:
/** /**
* @brief indexDirectory finds the true case for each file in the tree * @brief normalizeString Normalize a file path
* @param base_path * @param filePath the path to normalize
* @return normalized file path
*/
static std::string normalizeFilePath(const std::string &filePath);
/**
* @brief indexDirectory index all files at path
* @param path the path to index
* *
* This is used to build the mapping of lower-case file paths to the * This is used to build the mapping of lower-case file paths to the
* true case on the file system for platforms where this is an issue. * true case on the file system for platforms where this is an issue.
*
*/ */
void indexGameDirectory(const rwfs::path& base_path); void indexTree(const rwfs::path &path);
/** /**
* @brief findFilePath finds disk path for a game data file * @brief findFilePath finds disk path for a game data file
* @param path * @param filePath the path to find
* @return The file path as it exists on disk * @return The file path as it exists on disk
* @throws if this FileIndex has not indexed the path
*/ */
rwfs::path findFilePath(std::string path) { rwfs::path findFilePath(const std::string &filePath) const;
auto backslash = std::string::npos;
while ((backslash = path.find("\\")) != std::string::npos) {
path.replace(backslash, 1, "/");
}
auto realpath = gamedatapath_ / path;
std::string name = realpath.string();
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
return filesystemfiles_[name];
}
/** /**
* @brief openFilePath opens a file on the disk * @brief openFileRaw Opens a raw file on the disk
* @param file_path * @param filePath the path to open
* @return A handle for the file on the disk * @return FileHandle to the file
* @throws if this FileIndex has not indexed the path
*/ */
FileHandle openFilePath(const std::string& file_path); FileHandle openFileRaw(const std::string &filePath) const;
struct IndexData {
/// Lowercase identifying filename
std::string filename;
/// Original filename
std::string originalName;
/// Containing directory
std::string directory;
/// The archive filename (if applicable)
std::string archive;
};
/**
* Adds the files contained within the given directory tree to the
* file index.
*/
void indexTree(const rwfs::path& root);
/** /**
* Adds the files contained within the given Archive file to the * Adds the files contained within the given Archive file to the
* file index. * file index.
* @param filePath path to the archive
* @throws if this FileIndex has not indexed the archive itself
*/ */
void indexArchive(const std::string& archive); void indexArchive(const std::string &filePath);
/** /**
* Returns a FileHandle for the file if it can be found in the * Returns a FileHandle for the file if it can be found in the
* file index, otherwise an empty FileHandle is returned. * file index, otherwise an empty FileHandle is returned.
* @param filePath name of the file to open
* @return FileHandle to the file, nullptr if this FileINdexed has not indexed the path
*/ */
FileHandle openFile(const std::string& filename); FileHandle openFile(const std::string &filePath);
private: private:
std::map<std::string, IndexData> files; /**
* @brief Type of the indexed data.
*/
enum IndexedDataType {
/// Is a file on disk
FILE,
/// Is a member of an archive
ARCHIVE,
};
/**
* @brief All information of indexed data.
*/
struct IndexedData {
/// Type of indexed data.
IndexedDataType type;
/// Path of indexed data.
std::string path;
/// Extra data of assets (FIXME: use c++17 std::variant or std::option)
std::string assetData;
};
/**
* @brief indexedData_ A mapping from filepath (relative to game data path) to an IndexedData item.
*/
std::unordered_map<std::string, IndexedData> indexedData_;
/**
* @brief getIndexedDataAt Get IndexedData for filePath
* @param filePath the file path to get the IndexedData for
* @return IndexedData pointer if this FileIndex has indexed the filePath
* @throws If this FileIndex has not indexed filePath
*/
const IndexedData *getIndexedDataAt(const std::string &filePath) const;
}; };
#endif #endif

View File

@ -1,10 +1,9 @@
#ifndef _LIBRW_BIT_CAST_CPP_ #ifndef _LIBRW_BIT_CAST_HPP_
#define _LIBRW_BIT_CAST_CPP_ #define _LIBRW_BIT_CAST_HPP_
//Based on https://gist.github.com/socantre/3472964 //Based on https://gist.github.com/socantre/3472964
#include <cstring> // memcpy #include <cstring> // memcpy
#include <type_traits> // is_trivially_copyable #include <type_traits> // is_trivially_copyable
#include "rw/defines.hpp" // RW_ASSERT
template <class Dest, class Source> template <class Dest, class Source>
inline Dest bit_cast(Source const &source) { inline Dest bit_cast(Source const &source) {

View File

@ -1,5 +1,5 @@
#ifndef _LIBRW_DEFINES_HPP_ #ifndef _LIBRW_DEBUG_HPP_
#define _LIBRW_DEFINES_HPP_ #define _LIBRW_DEBUG_HPP_
#if RW_DEBUG #if RW_DEBUG
#include <cstdlib> #include <cstdlib>

View File

@ -5,7 +5,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <map> #include <map>
#include <memory> #include <memory>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include <string> #include <string>
#define RW_USING(feature) 1 == feature #define RW_USING(feature) 1 == feature

View File

@ -8,7 +8,7 @@
#include <fstream> #include <fstream>
#include <map> #include <map>
#include <rw/defines.hpp> #include <rw/debug.hpp>
#include <rw/filesystem.hpp> #include <rw/filesystem.hpp>
namespace pt = boost::property_tree; namespace pt = boost::property_tree;

View File

@ -2,23 +2,35 @@
#include <platform/FileIndex.hpp> #include <platform/FileIndex.hpp>
#include "test_Globals.hpp" #include "test_Globals.hpp"
#include <rw/filesystem.hpp>
namespace fs = rwfs;
BOOST_AUTO_TEST_SUITE(FileIndexTests) BOOST_AUTO_TEST_SUITE(FileIndexTests)
#if RW_TEST_WITH_DATA BOOST_AUTO_TEST_CASE(test_normalizeName) {
BOOST_AUTO_TEST_CASE(test_directory_paths) { std::string ref = "a/b/c";
FileIndex index; {
std::string dirty = "a\\b\\c";
BOOST_CHECK_EQUAL(ref, FileIndex::normalizeFilePath(dirty));
}
{
std::string dirty = "A/B/C";
BOOST_CHECK_EQUAL(ref, FileIndex::normalizeFilePath(dirty));
}
{
std::string dirty = "A\\B\\C";
BOOST_CHECK_EQUAL(ref, FileIndex::normalizeFilePath(dirty));
}
}
index.indexGameDirectory(Global::getGamePath()); #if RW_TEST_WITH_DATA
BOOST_AUTO_TEST_CASE(test_indexTree) {
FileIndex index;
index.indexTree(Global::getGamePath());
{ {
std::string upperpath{"DATA/CULLZONE.DAT"}; std::string upperpath{"DATA/CULLZONE.DAT"};
auto truepath = index.findFilePath(upperpath); auto truepath = index.findFilePath(upperpath);
BOOST_ASSERT(!truepath.empty()); BOOST_ASSERT(!truepath.empty());
BOOST_CHECK(upperpath != truepath); BOOST_CHECK(upperpath != truepath);
fs::path expected{Global::getGamePath()}; rwfs::path expected{Global::getGamePath()};
expected /= "data/CULLZONE.DAT"; expected /= "data/CULLZONE.DAT";
BOOST_CHECK_EQUAL(truepath.string(), expected.string()); BOOST_CHECK_EQUAL(truepath.string(), expected.string());
} }
@ -27,28 +39,35 @@ BOOST_AUTO_TEST_CASE(test_directory_paths) {
auto truepath = index.findFilePath(upperpath); auto truepath = index.findFilePath(upperpath);
BOOST_ASSERT(!truepath.empty()); BOOST_ASSERT(!truepath.empty());
BOOST_CHECK(upperpath != truepath); BOOST_CHECK(upperpath != truepath);
fs::path expected{Global::getGamePath()}; rwfs::path expected{Global::getGamePath()};
expected /= "data/maps/comnbtm/comNbtm.ipl"; expected /= "data/maps/comnbtm/comNbtm.ipl";
BOOST_CHECK_EQUAL(truepath.string(), expected.string()); BOOST_CHECK_EQUAL(truepath.string(), expected.string());
} }
} }
BOOST_AUTO_TEST_CASE(test_file) { BOOST_AUTO_TEST_CASE(test_openFile) {
FileIndex index; FileIndex index;
index.indexTree(Global::getGamePath() + "/data"); index.indexTree(Global::getGamePath() + "/data");
auto handle = index.openFile("cullzone.dat"); auto handle = index.openFile("cullzone.dat");
BOOST_CHECK(handle != nullptr); BOOST_CHECK(handle != nullptr);
} }
BOOST_AUTO_TEST_CASE(test_file_archive) { BOOST_AUTO_TEST_CASE(test_indexArchive) {
FileIndex index; FileIndex index;
index.indexTree(Global::getGamePath());
index.indexArchive(Global::getGamePath() + "/models/gta3.img"); {
auto handle = index.openFile("landstal.dff");
BOOST_CHECK(handle == nullptr);
}
index.indexArchive("models/gta3.img");
{
auto handle = index.openFile("landstal.dff"); auto handle = index.openFile("landstal.dff");
BOOST_CHECK(handle != nullptr); BOOST_CHECK(handle != nullptr);
}
} }
#endif #endif

View File

@ -11,7 +11,7 @@ BOOST_AUTO_TEST_SUITE(TextTests)
#if RW_TEST_WITH_DATA #if RW_TEST_WITH_DATA
BOOST_AUTO_TEST_CASE(load_test) { BOOST_AUTO_TEST_CASE(load_test) {
{ {
auto d = Global::get().e->data->index.openFilePath("text/english.gxt"); auto d = Global::get().e->data->index.openFileRaw("text/english.gxt");
GameTexts texts; GameTexts texts;