1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-30 04:30:42 +02:00

Rewrote CSndHandler::CSndHandler and CVidHandler::CVidHandler to get rid of readNormalNr.

This commit is contained in:
Frank Zago 2009-10-11 16:05:40 +00:00
parent ea361fe2b4
commit cb18b7c82a
2 changed files with 36 additions and 51 deletions

View File

@ -2,6 +2,7 @@
#include <fstream> #include <fstream>
#include "CSndHandler.h" #include "CSndHandler.h"
#include <boost/iostreams/device/mapped_file.hpp> #include <boost/iostreams/device/mapped_file.hpp>
#include <SDL_endian.h>
/* /*
* CSndHandler.cpp, part of VCMI engine * CSndHandler.cpp, part of VCMI engine
@ -38,12 +39,6 @@ CMediaHandler::CMediaHandler(std::string fname)
} }
} }
// Reads a 4 byte integer. Format on file is little endian.
unsigned int CMediaHandler::readNormalNr (const unsigned char *p)
{
return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
}
void CMediaHandler::extract(int index, std::string dstfile) //saves selected file void CMediaHandler::extract(int index, std::string dstfile) //saves selected file
{ {
std::ofstream out(dstfile.c_str(),std::ios_base::binary); std::ofstream out(dstfile.c_str(),std::ios_base::binary);
@ -128,37 +123,24 @@ const char * CMediaHandler::extract (std::string srcName, int &size)
CSndHandler::CSndHandler(std::string fname) : CMediaHandler(fname) CSndHandler::CSndHandler(std::string fname) : CMediaHandler(fname)
{ {
const unsigned char *data = (const unsigned char *)mfile->data(); const unsigned char *data = (const unsigned char *)mfile->data();
unsigned int numFiles = SDL_SwapLE32(*(Uint32 *)&data[0]);
struct soundEntry *se = (struct soundEntry *)&data[4];
unsigned int numFiles = readNormalNr(&data[0]); for (unsigned int i=0; i<numFiles; i++, se++)
for (unsigned int i=0; i<numFiles; i++)
{ {
Entry entry; Entry entry;
const unsigned char *p; char *p;
// Read file name and extension // Reassemble the filename and its extension
p = &data[4+48*i]; entry.name = se->filename;
entry.name += '.';
while(*p) { p = se->filename;
entry.name += *p; while(*p) p++;
p++;
}
entry.name+='.';
p++; p++;
entry.name += p;
while(*p) entry.offset = SDL_SwapLE32(se->offset);
{ entry.size = SDL_SwapLE32(se->size);
entry.name += *p;
p++;
}
// Read offset and size
p = &data[4+48*i+40];
entry.offset = readNormalNr(p);
p += 4;
entry.size = readNormalNr(p);
entries.push_back(entry); entries.push_back(entry);
fimap[entry.name] = i; fimap[entry.name] = i;
@ -168,34 +150,24 @@ CSndHandler::CSndHandler(std::string fname) : CMediaHandler(fname)
CVidHandler::CVidHandler(std::string fname) : CMediaHandler(fname) CVidHandler::CVidHandler(std::string fname) : CMediaHandler(fname)
{ {
const unsigned char *data = (const unsigned char *)mfile->data(); const unsigned char *data = (const unsigned char *)mfile->data();
unsigned int numFiles = SDL_SwapLE32(*(Uint32 *)&data[0]);
struct videoEntry *ve = (struct videoEntry *)&data[4];
unsigned int numFiles = readNormalNr(&data[0]); for (unsigned int i=0; i<numFiles; i++, ve++)
for (unsigned int i=0; i<numFiles; i++)
{ {
Entry entry; Entry entry;
const unsigned char *p;
// Read file name and extension entry.name = ve->filename;
p = &data[4+44*i]; entry.offset = SDL_SwapLE32(ve->offset);
while(*p) {
entry.name += *p;
p++;
}
// Read offset and size
p = &data[4+44*i+40];
entry.offset = readNormalNr(p);
// There is no size, so check where the next file is // There is no size, so check where the next file is
if (i == numFiles - 1) { if (i == numFiles - 1) {
entry.size = mfile->size() - entry.offset; entry.size = mfile->size() - entry.offset;
} else { } else {
p = &data[4+44*(i+1)+40]; struct videoEntry *ve_next = ve+1;
int next_offset = readNormalNr(p);
entry.size = next_offset - entry.offset; entry.size = SDL_SwapLE32(ve_next->offset) - entry.offset;
} }
entries.push_back(entry); entries.push_back(entry);
fimap[entry.name] = i; fimap[entry.name] = i;

View File

@ -4,6 +4,7 @@
#include <vector> #include <vector>
#include <iosfwd> #include <iosfwd>
#include <map> #include <map>
#include <SDL_stdinc.h>
/* /*
@ -30,6 +31,19 @@ struct MemberFile
int length; int length;
}; };
struct soundEntry
{
char filename[40];
Uint32 offset; /* lettle endian */
Uint32 size; /* lettle endian */
};
struct videoEntry
{
char filename[40];
Uint32 offset; /* lettle endian */
};
class CMediaHandler class CMediaHandler
{ {
protected: protected:
@ -40,7 +54,6 @@ protected:
unsigned int offset; unsigned int offset;
}; };
inline unsigned int readNormalNr (const unsigned char *p);
boost::iostreams::mapped_file_source *mfile; boost::iostreams::mapped_file_source *mfile;
public: public: