From 4df20821e7eb55f9451e03f1127114a3450c24c3 Mon Sep 17 00:00:00 2001 From: Jannik Vogel Date: Mon, 30 May 2016 23:05:58 +0200 Subject: [PATCH] Small cleanup in LoaderIMG --- rwlib/source/loaders/LoaderIMG.cpp | 32 ++++++++++-------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/rwlib/source/loaders/LoaderIMG.cpp b/rwlib/source/loaders/LoaderIMG.cpp index 1c8b81e4..510781e1 100644 --- a/rwlib/source/loaders/LoaderIMG.cpp +++ b/rwlib/source/loaders/LoaderIMG.cpp @@ -27,13 +27,12 @@ bool LoaderIMG::load(const std::string& filename) unsigned long fileSize = ftell(fp); fseek(fp, 0, SEEK_SET); - m_assets.resize(fileSize / 32); - m_assetCount = fileSize / 32; + m_assets.resize(m_assetCount); - if( fread(&m_assets[0], sizeof(LoaderIMGFile), fileSize / 32, fp) == 0 ) - { - std::cout << "No Records in IMG archive" << std::endl; + if ((m_assetCount = fread(&m_assets[0], sizeof(LoaderIMGFile), m_assetCount, fp)) != fileSize / 32) { + m_assets.resize(m_assetCount); + std::cout << "Error reading records in IMG archive" << std::endl; } fclose(fp); @@ -63,31 +62,22 @@ bool LoaderIMG::findAssetInfo(const std::string& assetname, LoaderIMGFile& out) char* LoaderIMG::loadToMemory(const std::string& assetname) { LoaderIMGFile assetInfo; - bool found = false; - for(size_t i = 0; i < m_assets.size(); ++i) - { - if(strcasecmp(m_assets[i].name, assetname.c_str()) == 0) - { - assetInfo = m_assets[i]; - found = true; - } - } + bool found = findAssetInfo(assetname, assetInfo); - if(!found) - { + if (!found) { std::cerr << "Asset '" << assetname << "' not found!" << std::endl; - return 0; + return nullptr; } - std::string dirName = m_archive; + std::string imgName = m_archive; - FILE* fp = fopen(dirName.c_str(), "rb"); + FILE* fp = fopen(imgName.c_str(), "rb"); if(fp) { char* raw_data = new char[assetInfo.size * 2048]; fseek(fp, assetInfo.offset * 2048, SEEK_SET); - if( fread(raw_data, 2048, assetInfo.size, fp) == 0 ) { + if( fread(raw_data, 2048, assetInfo.size, fp) != assetInfo.size ) { std::cerr << "Error reading asset " << assetInfo.name << std::endl; } @@ -111,7 +101,7 @@ bool LoaderIMG::saveAsset(const std::string& assetname, const std::string& filen LoaderIMGFile asset; if( findAssetInfo( assetname, asset ) ) { - fwrite(raw_data, asset.size * 2048, 1, dumpFile); + fwrite(raw_data, 2048, asset.size, dumpFile); printf("=> IMG: Saved %s to disk with filename %s\n", assetname.c_str(), filename.c_str()); } fclose(dumpFile);