From 82e0b6fc4bb719e9b3e647c3fcb40cefec9fcd28 Mon Sep 17 00:00:00 2001 From: Strangerke Date: Wed, 14 Sep 2011 00:10:59 +0200 Subject: CGE: Rewrite fileIO --- engines/cge/bitmap.cpp | 2 +- engines/cge/cge.cpp | 6 +- engines/cge/cge_main.cpp | 13 ++-- engines/cge/fileio.cpp | 155 ++++++++++++++++++++++++----------------------- engines/cge/fileio.h | 43 ++++++------- engines/cge/general.cpp | 9 --- engines/cge/general.h | 3 - engines/cge/sound.cpp | 2 +- engines/cge/text.cpp | 2 +- engines/cge/vga13h.cpp | 2 +- 10 files changed, 109 insertions(+), 128 deletions(-) (limited to 'engines/cge') diff --git a/engines/cge/bitmap.cpp b/engines/cge/bitmap.cpp index c54ba09245..f1600854b2 100644 --- a/engines/cge/bitmap.cpp +++ b/engines/cge/bitmap.cpp @@ -49,7 +49,7 @@ Bitmap::Bitmap(const char *fname) : _m(NULL), _v(NULL), _map(0) { char pat[kMaxPath]; forceExt(pat, fname, ".VBM"); - if (_cat->exist(pat)) { + if (_resman->exist(pat)) { EncryptedStream file(pat); if (file.err()) error("Unable to find VBM [%s]", fname); diff --git a/engines/cge/cge.cpp b/engines/cge/cge.cpp index 9ff8a76a30..ef2796b169 100644 --- a/engines/cge/cge.cpp +++ b/engines/cge/cge.cpp @@ -79,8 +79,7 @@ void CGEEngine::init() { _miniShp = NULL; _miniShpList = NULL; _sprite = NULL; - _dat = new IoHand(kDatName); - _cat = new BtFile(kCatName); + _resman = new ResourceManager(); // Create debugger console _console = new CGEConsole(this); @@ -172,8 +171,7 @@ void CGEEngine::deinit() { delete _snail; delete _snail_; delete _hero; - delete _dat; - delete _cat; + delete _resman; if (_miniShpList) { for (int i = 0; _miniShpList[i]; ++i) diff --git a/engines/cge/cge_main.cpp b/engines/cge/cge_main.cpp index 24aa79801c..a78165c66a 100644 --- a/engines/cge/cge_main.cpp +++ b/engines/cge/cge_main.cpp @@ -72,8 +72,7 @@ Snail *_snail_; Fx *_fx; Sound *_sound; -IoHand *_dat; -BtFile *_cat; +ResourceManager *_resman; // 0.75 - 17II95 - full sound support // 0.76 - 18II95 - small MiniEMS in DEMO, @@ -1031,7 +1030,7 @@ void CGEEngine::loadSprite(const char *fname, int ref, int cav, int col = 0, int Common::String line; mergeExt(tmpStr, fname, kSprExt); - if (_cat->exist(tmpStr)) { // sprite description file exist + if (_resman->exist(tmpStr)) { // sprite description file exist EncryptedStream sprf(tmpStr); if (sprf.err()) error("Bad SPR [%s]", tmpStr); @@ -1311,7 +1310,7 @@ void CGEEngine::runGame() { if (!_music) _midiPlayer.killMidi(); - if (_cat->exist("MINI.SPR")) { + if (_resman->exist("MINI.SPR")) { _miniShp = new BitmapPtr[2]; _miniShp[0] = _miniShp[1] = NULL; @@ -1329,7 +1328,7 @@ void CGEEngine::runGame() { if (_hero) { expandSprite(_hero); _hero->gotoxy(_heroXY[_now - 1].x, _heroXY[_now - 1].y); - if (_cat->exist("00SHADOW.SPR")) { + if (_resman->exist("00SHADOW.SPR")) { loadSprite("00SHADOW", -1, 0, _hero->_x + 14, _hero->_y + 51); delete _shadow; if ((_shadow = _sprite) != NULL) { @@ -1398,7 +1397,7 @@ void CGEEngine::movie(const char *ext) { char fn[12]; sprintf(fn, "CGE.%s", (*ext == '.') ? ext +1 : ext); - if (_cat->exist(fn)) { + if (_resman->exist(fn)) { loadScript(fn); expandSprite(_vga->_spareQ->locate(999)); feedSnail(_vga->_showQ->locate(999), kTake); @@ -1505,7 +1504,7 @@ void CGEEngine::cge_main() { if (!_mouse->_exist) error("%s", _text->getText(kTextNoMouse)); - if (!_cat->exist(kSavegame0Name)) + if (!_resman->exist(kSavegame0Name)) _mode = 2; _debugLine->_flags._hide = true; diff --git a/engines/cge/fileio.cpp b/engines/cge/fileio.cpp index ddefc573f5..830c1a7c21 100644 --- a/engines/cge/fileio.cpp +++ b/engines/cge/fileio.cpp @@ -35,51 +35,10 @@ namespace CGE { -/*----------------------------------------------------------------------- - * IOHand - *-----------------------------------------------------------------------*/ -IoHand::IoHand() : _error(0) { - _file = new Common::File(); -} - -IoHand::IoHand(const char *name) : _error(0) { - _file = new Common::File(); - _file->open(name); -} - -IoHand::~IoHand() { - _file->close(); - delete _file; -} - -uint16 IoHand::read(void *buf, uint16 len) { - if (!_file->isOpen()) - return 0; - - uint16 bytesRead = _file->read(buf, len); - if (!bytesRead) - error("Read %s - %d bytes", _file->getName(), len); - XCrypt(buf, len); - return bytesRead; -} - -long IoHand::mark() { - return _file->pos(); -} - -long IoHand::seek(long pos) { - _file->seek(pos, SEEK_SET); - return _file->pos(); -} - -long IoHand::size() { - return _file->size(); -} - /*----------------------------------------------------------------------- * BtPage *-----------------------------------------------------------------------*/ -void BtPage::read(Common::ReadStream &s) { +void BtPage::readBTree(Common::ReadStream &s) { _header._count = s.readUint16LE(); _header._down = s.readUint16LE(); @@ -87,7 +46,7 @@ void BtPage::read(Common::ReadStream &s) { // Leaf list for (int i = 0; i < kBtLeafCount; ++i) { s.read(_leaf[i]._key, kBtKeySize); - _leaf[i]._mark = s.readUint32LE(); + _leaf[i]._pos = s.readUint32LE(); _leaf[i]._size = s.readUint16LE(); } } else { @@ -100,56 +59,94 @@ void BtPage::read(Common::ReadStream &s) { } /*----------------------------------------------------------------------- - * BtFile + * ResourceManager *-----------------------------------------------------------------------*/ -BtFile::BtFile(const char *name) : IoHand(name) { - debugC(1, kCGEDebugFile, "BtFile::BtFile(%s)", name); +ResourceManager::ResourceManager() { + debugC(1, kCGEDebugFile, "ResourceManager::ResourceManager()"); + + _datFile = new Common::File(); + _datFile->open(kDatName); + + _catFile = new Common::File(); + _catFile->open(kCatName); + + if ((!_datFile) || (!_catFile)) + error("Unable to open data files"); for (int i = 0; i < kBtLevel; i++) { _buff[i]._page = new BtPage; - _buff[i]._pgNo = kBtValNone; - _buff[i]._indx = -1; + _buff[i]._pageNo = kBtValNone; + _buff[i]._index = -1; assert(_buff[i]._page != NULL); } } -BtFile::~BtFile() { - debugC(1, kCGEDebugFile, "BtFile::~BtFile()"); +ResourceManager::~ResourceManager() { + debugC(1, kCGEDebugFile, "ResourceManager::~ResourceManager()"); + _datFile->close(); + delete _datFile; + + _catFile->close(); + delete _catFile; + for (int i = 0; i < kBtLevel; i++) delete _buff[i]._page; } -BtPage *BtFile::getPage(int lev, uint16 pgn) { - debugC(1, kCGEDebugFile, "BtFile::getPage(%d, %d)", lev, pgn); +uint16 ResourceManager::XCrypt(void *buf, uint16 length) { + byte *b = static_cast(buf); - if (_buff[lev]._pgNo != pgn) { - int32 pos = pgn * kBtSize; - _buff[lev]._pgNo = pgn; - assert(size() > pos); + for (uint16 i = 0; i < length; i++) + *b++ ^= kCryptSeed; + + return kCryptSeed; +} + +bool ResourceManager::seek(int32 offs, int whence) { + return _datFile->seek(offs, whence); +} + +uint16 ResourceManager::read(void *buf, uint16 length) { + if (!_datFile->isOpen()) + return 0; + + uint16 bytesRead = _datFile->read(buf, length); + if (!bytesRead) + error("Read %s - %d bytes", _datFile->getName(), length); + XCrypt(buf, length); + return bytesRead; +} + +BtPage *ResourceManager::getPage(int level, uint16 pageId) { + debugC(1, kCGEDebugFile, "IoHand::getPage(%d, %d)", level, pageId); + + if (_buff[level]._pageNo != pageId) { + int32 pos = pageId * kBtSize; + _buff[level]._pageNo = pageId; + assert(_catFile->size() > pos); // In the original, there was a check verifying if the // purpose was to write a new file. This should only be // to create a new file, thus it was removed. - seek((uint32) pgn * kBtSize); + _catFile->seek(pageId * kBtSize, SEEK_SET); // Read in the page byte buffer[kBtSize]; - int bytesRead = read(buffer, kBtSize); + int bytesRead = catRead(buffer, kBtSize); // Unpack it into the page structure Common::MemoryReadStream stream(buffer, bytesRead, DisposeAfterUse::NO); - _buff[lev]._page->read(stream); - - _buff[lev]._indx = -1; + _buff[level]._page->readBTree(stream); + _buff[level]._index = -1; } - return _buff[lev]._page; + return _buff[level]._page; } -BtKeypack *BtFile::find(const char *key) { - debugC(1, kCGEDebugFile, "BtFile::find(%s)", key); +BtKeypack *ResourceManager::find(const char *key) { + debugC(1, kCGEDebugFile, "IoHand::find(%s)", key); int lev = 0; uint16 nxt = kBtValRoot; - while (!_error) { + while (!_catFile->eos()) { BtPage *pg = getPage(lev, nxt); // search if (pg->_header._down != kBtValNone) { @@ -160,7 +157,7 @@ BtKeypack *BtFile::find(const char *key) { break; } nxt = (i) ? pg->_inner[i - 1]._down : pg->_header._down; - _buff[lev]._indx = i - 1; + _buff[lev]._index = i - 1; lev++; } else { int i; @@ -168,19 +165,30 @@ BtKeypack *BtFile::find(const char *key) { if (scumm_stricmp((const char *)key, (const char *)pg->_leaf[i]._key) <= 0) break; } - _buff[lev]._indx = i; + _buff[lev]._index = i; return &pg->_leaf[i]; } } return NULL; } -bool BtFile::exist(const char *name) { - debugC(1, kCGEDebugFile, "BtFile::exist(%s)", name); +bool ResourceManager::exist(const char *name) { + debugC(1, kCGEDebugFile, "ResourceManager::exist(%s)", name); return scumm_stricmp(find(name)->_key, name) == 0; } +uint16 ResourceManager::catRead(void *buf, uint16 length) { + if (!_catFile->isOpen()) + return 0; + + uint16 bytesRead = _catFile->read(buf, length); + if (!bytesRead) + error("Read %s - %d bytes", _catFile->getName(), length); + XCrypt(buf, length); + return bytesRead; +} + /*----------------------------------------------------------------------- * EncryptedStream *-----------------------------------------------------------------------*/ @@ -188,16 +196,13 @@ EncryptedStream::EncryptedStream(const char *name) { debugC(3, kCGEDebugFile, "EncryptedStream::EncryptedStream(%s)", name); _error = false; - if (_dat->_error || _cat->_error) - error("Bad volume data"); - BtKeypack *kp = _cat->find(name); + BtKeypack *kp = _resman->find(name); if (scumm_stricmp(kp->_key, name) != 0) _error = true; - _dat->_file->seek(kp->_mark); + _resman->seek(kp->_pos); byte *dataBuffer = (byte *)malloc(kp->_size); - _dat->_file->read(dataBuffer, kp->_size); - XCrypt(dataBuffer, kp->_size); + _resman->read(dataBuffer, kp->_size); _readStream = new Common::MemoryReadStream(dataBuffer, kp->_size, DisposeAfterUse::YES); } diff --git a/engines/cge/fileio.h b/engines/cge/fileio.h index 443cddde14..4d2539cc37 100644 --- a/engines/cge/fileio.h +++ b/engines/cge/fileio.h @@ -40,14 +40,12 @@ namespace CGE { #define kBtLeafCount ((kBtSize - 4 /*sizeof(Hea) */) / (kBtKeySize + 4 + 2 /*sizeof(BtKeypack) */)) #define kBtValNone 0xFFFF #define kBtValRoot 0 -#define kLineMaxSize 512 -#define kBufferSize 2048 #define kCatName "VOL.CAT" #define kDatName "VOL.DAT" struct BtKeypack { char _key[kBtKeySize]; - uint32 _mark; + uint32 _pos; uint16 _size; }; @@ -61,20 +59,6 @@ struct Header { uint16 _down; }; -class IoHand { -public: - Common::File *_file; - uint16 _error; - - IoHand(const char *name); - IoHand(); - virtual ~IoHand(); - uint16 read(void *buf, uint16 len); - long mark(); - long size(); - long seek(long pos); -}; - struct BtPage { Header _header; union { @@ -86,20 +70,28 @@ struct BtPage { BtKeypack _leaf[kBtLeafCount]; }; - void read(Common::ReadStream &s); + void readBTree(Common::ReadStream &s); }; -class BtFile : public IoHand { +class ResourceManager { struct { BtPage *_page; - uint16 _pgNo; - int _indx; + uint16 _pageNo; + int _index; } _buff[kBtLevel]; - BtPage *getPage(int lev, uint16 pgn); + BtPage *getPage(int level, uint16 pageId); + uint16 catRead(void *buf, uint16 length); + Common::File *_catFile; + Common::File *_datFile; + uint16 XCrypt(void *buf, uint16 length); public: - BtFile(const char *name); - virtual ~BtFile(); + + ResourceManager(); + ~ResourceManager(); + uint16 read(void *buf, uint16 length); + bool seek(int32 offs, int whence = 0); + BtKeypack *find(const char *key); bool exist(const char *name); }; @@ -120,8 +112,7 @@ public: Common::String readLine(); }; -extern IoHand *_dat; -extern BtFile *_cat; +extern ResourceManager *_resman; } // End of namespace CGE diff --git a/engines/cge/general.cpp b/engines/cge/general.cpp index dd375f5248..fff3ff0212 100644 --- a/engines/cge/general.cpp +++ b/engines/cge/general.cpp @@ -30,15 +30,6 @@ namespace CGE { -uint16 XCrypt(void *buf, uint16 siz) { - byte *b = static_cast(buf); - - for (uint16 i = 0; i < siz; i++) - *b++ ^= kCryptSeed; - - return kCryptSeed; -} - char *mergeExt(char *buf, const char *name, const char *ext) { strcpy(buf, name); char *dot = strrchr(buf, '.'); diff --git a/engines/cge/general.h b/engines/cge/general.h index a02cfd0c37..02c9f7fd9a 100644 --- a/engines/cge/general.h +++ b/engines/cge/general.h @@ -45,9 +45,6 @@ struct Dac { uint8 _b; }; -typedef uint16 Crypt(void *buf, uint16 siz); - -uint16 XCrypt(void *buf, uint16 siz); int takeEnum(const char **tab, const char *text); uint16 chkSum(void *m, uint16 n); char *mergeExt(char *buf, const char *name, const char *ext); diff --git a/engines/cge/sound.cpp b/engines/cge/sound.cpp index 3052f792fd..26a06cae41 100644 --- a/engines/cge/sound.cpp +++ b/engines/cge/sound.cpp @@ -215,7 +215,7 @@ void MusicPlayer::killMidi() { void MusicPlayer::loadMidi(int ref) { // Work out the filename and check the given MIDI file exists Common::String filename = Common::String::format("%.2d.MID", ref); - if (!_cat->exist(filename.c_str())) + if (!_resman->exist(filename.c_str())) return; // Stop any currently playing MIDI file diff --git a/engines/cge/text.cpp b/engines/cge/text.cpp index 9e5ceac9c4..021fa1a574 100644 --- a/engines/cge/text.cpp +++ b/engines/cge/text.cpp @@ -40,7 +40,7 @@ Talk *_talk = NULL; Text::Text(CGEEngine *vm, const char *fname) : _vm(vm) { mergeExt(_fileName, fname, kSayExt); - if (!_cat->exist(_fileName)) + if (!_resman->exist(_fileName)) error("No talk (%s)\n", _fileName); int16 txtCount = count() + 1; if (!txtCount) diff --git a/engines/cge/vga13h.cpp b/engines/cge/vga13h.cpp index 77f51db6f6..d01a2b25fd 100644 --- a/engines/cge/vga13h.cpp +++ b/engines/cge/vga13h.cpp @@ -226,7 +226,7 @@ Sprite *Sprite::expand() { Snail::Com *nearList = NULL; Snail::Com *takeList = NULL; mergeExt(fname, _file, kSprExt); - if (_cat->exist(fname)) { // sprite description file exist + if (_resman->exist(fname)) { // sprite description file exist EncryptedStream sprf(fname); if (sprf.err()) error("Bad SPR [%s]", fname); -- cgit v1.2.3