diff --git a/UI/BackgroundAudio.cpp b/UI/BackgroundAudio.cpp index 261f4af5d2..348dd9e860 100644 --- a/UI/BackgroundAudio.cpp +++ b/UI/BackgroundAudio.cpp @@ -153,7 +153,7 @@ public: } private: - ChunkFile file_; + RIFFReader file_; uint8_t *raw_data_; int raw_data_size_; int raw_offset_; diff --git a/ext/native/file/chunk_file.cpp b/ext/native/file/chunk_file.cpp index 0c3f2fb5c2..1099e2ae25 100644 --- a/ext/native/file/chunk_file.cpp +++ b/ext/native/file/chunk_file.cpp @@ -9,15 +9,14 @@ inline uint32_t flipID(uint32_t id) { return ((id >> 24) & 0xFF) | ((id >> 8) & 0xFF00) | ((id << 8) & 0xFF0000) | ((id << 24) & 0xFF000000); } -ChunkFile::ChunkFile(const char *filename, bool readMode) { +RIFFReader::RIFFReader(const char *filename) { data_ = 0; filename_ = filename; depth_ = 0; - readMode_ = readMode; pos_ = 0; didFail_ = false; - fastMode = readMode_ ? true : false; + fastMode = true; if (fastMode) { size_t size; @@ -40,18 +39,17 @@ ChunkFile::ChunkFile(const char *filename, bool readMode) { } } -ChunkFile::ChunkFile(const uint8_t *data, int dataSize) { +RIFFReader::RIFFReader(const uint8_t *data, int dataSize) { data_ = new uint8_t[dataSize]; memcpy(data_, data, dataSize); fastMode = true; depth_ = 0; - readMode_ = true; pos_ = 0; didFail_ = false; eof_ = dataSize; } -ChunkFile::~ChunkFile() { +RIFFReader::~RIFFReader() { if (fastMode) { delete[] data_; } else { @@ -59,7 +57,7 @@ ChunkFile::~ChunkFile() { } } -int ChunkFile::readInt() { +int RIFFReader::readInt() { if (data_ && pos_ < eof_) { pos_ += 4; if (fastMode) @@ -74,75 +72,57 @@ int ChunkFile::readInt() { return 0; } -void ChunkFile::writeInt(int i) { - fwrite(&i, 1, 4, file); - pos_ += 4; -} - // let's get into the business -bool ChunkFile::descend(uint32_t id) { +bool RIFFReader::descend(uint32_t id) { if (depth_ > 30) return false; id = flipID(id); - if (readMode_) { - bool found = false; + bool found = false; - // save information to restore after the next Ascend - stack[depth_].parentStartLocation = pos_; - stack[depth_].parentEOF = eof_; + // save information to restore after the next Ascend + stack[depth_].parentStartLocation = pos_; + stack[depth_].parentEOF = eof_; - ChunkInfo temp = stack[depth_]; + ChunkInfo temp = stack[depth_]; - int firstID = 0; - // let's search through children.. - while (pos_ < eof_) { - stack[depth_].ID = readInt(); - if (firstID == 0) firstID = stack[depth_].ID | 1; - stack[depth_].length = readInt(); - stack[depth_].startLocation = pos_; - - if (stack[depth_].ID == id) { - found = true; - break; - } else { - seekTo(pos_ + stack[depth_].length); // try next block - } - } - - // if we found nothing, return false so the caller can skip this - if (!found) { -#ifdef CHUNKDEBUG - ILOG("Couldn't find %c%c%c%c", id, id >> 8, id >> 16, id >> 24); -#endif - stack[depth_] = temp; - seekTo(stack[depth_].parentStartLocation); - return false; - } - - // descend into it - // pos was set inside the loop above - eof_ = stack[depth_].startLocation + stack[depth_].length; - depth_++; -#ifdef CHUNKDEBUG - ILOG("Descended into %c%c%c%c", id, id >> 8, id >> 16, id >> 24); -#endif - return true; - } else { -#ifndef DEMO_VERSION // if this is missing.. heheh - // write a chunk id, and prepare for filling in length later - writeInt(id); - writeInt(0); // will be filled in by Ascend + int firstID = 0; + // let's search through children.. + while (pos_ < eof_) { + stack[depth_].ID = readInt(); + if (firstID == 0) firstID = stack[depth_].ID | 1; + stack[depth_].length = readInt(); stack[depth_].startLocation = pos_; - depth_++; - return true; -#else - return true; -#endif + + if (stack[depth_].ID == id) { + found = true; + break; + } else { + seekTo(pos_ + stack[depth_].length); // try next block + } } + + // if we found nothing, return false so the caller can skip this + if (!found) { +#ifdef CHUNKDEBUG + ILOG("Couldn't find %c%c%c%c", id, id >> 8, id >> 16, id >> 24); +#endif + stack[depth_] = temp; + seekTo(stack[depth_].parentStartLocation); + return false; + } + + // descend into it + // pos was set inside the loop above + eof_ = stack[depth_].startLocation + stack[depth_].length; + depth_++; +#ifdef CHUNKDEBUG + ILOG("Descended into %c%c%c%c", id, id >> 8, id >> 16, id >> 24); +#endif + return true; } -void ChunkFile::seekTo(int _pos) { +void RIFFReader::seekTo(int _pos) { if (!fastMode) { fseek(file, 0, SEEK_SET); } @@ -150,28 +130,19 @@ void ChunkFile::seekTo(int _pos) { } // let's ascend out -void ChunkFile::ascend() { - if (readMode_) { - // ascend, and restore information - depth_--; - seekTo(stack[depth_].parentStartLocation); - eof_ = stack[depth_].parentEOF; +void RIFFReader::ascend() { + // ascend, and restore information + depth_--; + seekTo(stack[depth_].parentStartLocation); + eof_ = stack[depth_].parentEOF; #ifdef CHUNKDEBUG - int id = stack[depth_].ID; - ILOG("Ascended out of %c%c%c%c", id, id >> 8, id >> 16, id >> 24); + int id = stack[depth_].ID; + ILOG("Ascended out of %c%c%c%c", id, id >> 8, id >> 16, id >> 24); #endif - } else { - depth_--; - // now fill in the written length automatically - int posNow = pos_; - seekTo(stack[depth_].startLocation - 4); - writeInt(posNow - stack[depth_].startLocation); - seekTo(posNow); - } } // read a block -void ChunkFile::readData(void *what, int count) { +void RIFFReader::readData(void *what, int count) { if (fastMode) { memcpy(what, data_ + pos_, count); } else { @@ -193,39 +164,6 @@ void ChunkFile::readData(void *what, int count) { } } -// write a block -void ChunkFile::writeData(const void *what, int count) { - fwrite(what, 1, count, file); - pos_ += count; - char temp[5] = { 0,0,0,0,0 }; - count &= 3; - if (count) { - count = 4 - count; - fwrite(temp, 1, count, file); - pos_ += count; - } -} - -// Takes utf-8 -void ChunkFile::writeWString(const std::string &str) { - unsigned short *text; - int len = (int)str.length(); - text = new unsigned short[len + 1]; - for (int i = 0; i < len; i++) - text[i] = str[i]; - text[len] = 0; - writeInt(len); - writeData((char *)text, len * sizeof(unsigned short)); - delete[] text; -} - -static void toUnicode(const std::string &str, uint16_t *t) { - for (size_t i = 0; i < str.size(); i++) { - *t++ = str[i]; - } - *t++ = '\0'; -} - static std::string fromUnicode(const uint16_t *src, int len) { std::string str; str.resize(len); @@ -235,7 +173,7 @@ static std::string fromUnicode(const uint16_t *src, int len) { return str; } -std::string ChunkFile::readWString() { +std::string RIFFReader::readWString() { int len = readInt(); uint16_t *text = new uint16_t[len + 1]; readData((char *)text, len * sizeof(uint16_t)); @@ -245,26 +183,7 @@ std::string ChunkFile::readWString() { return temp; } -void ChunkFile::writeString(const std::string &str) { - uint16_t *text; - int len = (int)str.size(); - text = new uint16_t[len + 1]; - toUnicode(str, text); - writeInt(len); - writeData((char *)text, len * sizeof(uint16_t)); - delete[] text; -} - -std::string ChunkFile::readString() { - int len = readInt(); - uint16_t *text = new uint16_t[len + 1]; - readData((char *)text, len * sizeof(uint16_t)); - text[len] = 0; - std::string temp = fromUnicode(text, len); - delete[] text; - return temp; -} -int ChunkFile::getCurrentChunkSize() { +int RIFFReader::getCurrentChunkSize() { if (depth_) return stack[depth_ - 1].length; else diff --git a/ext/native/file/chunk_file.h b/ext/native/file/chunk_file.h index ae46f2b6b9..8a3cb8f0e9 100644 --- a/ext/native/file/chunk_file.h +++ b/ext/native/file/chunk_file.h @@ -14,12 +14,12 @@ #include "base/basictypes.h" -class ChunkFile { +class RIFFReader { public: - ChunkFile(const char *filename, bool readMode); - ChunkFile(const uint8_t *data, int dataSize); + RIFFReader(const char *filename); + RIFFReader(const uint8_t *data, int dataSize); - ~ChunkFile(); + ~RIFFReader(); bool descend(uint32_t id); void ascend(); @@ -30,14 +30,6 @@ public: // String readWString(); std::string readWString(); - void writeString(const std::string &str); - std::string readString(); - - void writeInt(int i); - //void writeWString(String str); - void writeWString(const std::string &str); - void writeData(const void *data, int count); - int getCurrentChunkSize(); bool failed() const { return didFail_; } std::string filename() const { return filename_; } @@ -57,7 +49,6 @@ private: int pos_ = 0; int eof_ = 0; bool fastMode; - bool readMode_; bool didFail_ = false; std::string filename_;