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

Added IMG loader files

This commit is contained in:
Daniel Evans 2013-06-30 21:17:31 +01:00
parent 259d3ecbca
commit b006c308df
3 changed files with 189 additions and 1 deletions

View File

@ -1,4 +1,4 @@
add_executable(datadump main.cpp)
add_executable(datadump main.cpp ../framework/LoaderIMG.cpp)
target_link_libraries( datadump sfml-graphics sfml-window sfml-system GL GLEW )

123
framework/LoaderIMG.cpp Normal file
View File

@ -0,0 +1,123 @@
#include "LoaderIMG.h"
#include <cstring>
LoaderIMG::LoaderIMG()
: m_version(GTAIIIVC)
, m_assetCount(0)
{
}
bool LoaderIMG::load(const std::string& filename)
{
std::string dirName = filename;
dirName.append(".dir");
FILE* fp = fopen(dirName.c_str(), "rb");
if(fp)
{
fseek(fp, 0, SEEK_END);
unsigned long fileSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
m_assets.resize(fileSize / 32);
m_assetCount = fileSize / 32;
fread(&m_assets[0], sizeof(LoaderIMGFile), fileSize / 32, fp);
fclose(fp);
m_archive = filename;
return true;
}
else
return false;
}
/// Get the information of a asset in the examining archive
LoaderIMGFile LoaderIMG::getAssetInfo(const std::string& assetname)
{
LoaderIMGFile file;
for(size_t i = 0; i < m_assets.size(); ++i)
{
if(strcmp(m_assets[i].name, assetname.c_str()) == 0)
{
file = m_assets[i];
}
}
return file;
}
char* LoaderIMG::loadToMemory(const std::string& assetname)
{
LoaderIMGFile assetInfo;
bool found = false;
for(size_t i = 0; i < m_assets.size(); ++i)
{
if(strcmp(m_assets[i].name, assetname.c_str()) == 0)
{
assetInfo = m_assets[i];
found = true;
}
}
if(!found)
{
printf("No such asset found.\n");
return 0;
}
std::string dirName = m_archive;
dirName.append(".img");
FILE* fp = fopen(dirName.c_str(), "rb");
if(fp)
{
char* raw_data = new char[assetInfo.size * 2048];
fseek(fp, assetInfo.offset * 2048, SEEK_SET);
fread(raw_data, 2048, assetInfo.size, fp);
fclose(fp);
return raw_data;
}
else
return 0;
}
/// Writes the contents of assetname to filename
bool LoaderIMG::saveAsset(const std::string& assetname, const std::string& filename)
{
char* raw_data = loadToMemory(assetname);
if(!raw_data)
return false;
FILE* dumpFile = fopen(filename.c_str(), "wb");
if(dumpFile)
{
fwrite(raw_data, getAssetInfo(assetname).size * 2048, 1, dumpFile);
fclose(dumpFile);
printf("=> IMG: Saved %s to disk with filename %s\n", assetname.c_str(), filename.c_str());
delete[] raw_data;
return true;
}
else
{
delete[] raw_data;
return false;
}
}
/// Get the information of an asset by its index
LoaderIMGFile LoaderIMG::getAssetInfoByIndex(size_t index)
{
return m_assets[index];
}
uint32_t LoaderIMG::getAssetCount()
{
return m_assetCount;
}

65
framework/LoaderIMG.h Normal file
View File

@ -0,0 +1,65 @@
#ifndef LoaderIMG_h__
#define LoaderIMG_h__
#include <iostream>
#include <vector>
#include <cstdint>
/// \brief Points to one file within the archive
class LoaderIMGFile
{
public:
uint32_t offset;
uint32_t size;
char name[24];
};
/**
\class LoaderIMG
\brief Parses the structure of GTA .IMG archives and loads the files in it
*/
class LoaderIMG
{
public:
/// Multiple versions of .IMG files
enum Versions
{
GTAIIIVC, ///< GTA III and GTA VC archives -- only this one is implemented
GTASA,
GTAIV
};
/// Construct
LoaderIMG();
/// Load the structure of the archive
/// Omit the extension in filename so both .dir and .img are loaded when appropriate
bool load(const std::string& filename);
/// Load a file from the archive to memory and pass a pointer to it
/// Warning: Please delete[] the memory in the end.
/// Warning: Returns NULL (0) if by any reason it can't load the file
char* loadToMemory(const std::string& assetname);
/// Writes the contents of assetname to filename
bool saveAsset(const std::string& assetname, const std::string& filename);
/// Get the information of an asset in the examining archive
LoaderIMGFile getAssetInfo(const std::string& assetname);
/// Get the information of an asset by its index
LoaderIMGFile getAssetInfoByIndex(size_t index);
/// Returns the number of asset files in the archive
uint32_t getAssetCount();
private:
Versions m_version; ///< Version of this IMG archive
uint32_t m_assetCount; ///< Number of assets in the current archive
std::string m_archive; ///< Path to the archive being used (no extension)
std::vector<LoaderIMGFile> m_assets; ///< Asset info of the archive
};
#endif // LoaderIMG_h__