Ticket #8931: archive_ptr_v1.patch

File archive_ptr_v1.patch, 16.9 KB (added by lordhoto, 16 years ago)

Patch against r34831.

  • gui/ThemeEngine.cpp

     
    525525        if (!node.exists() || !node.isReadable())
    526526                return false;
    527527
    528         Common::ArchivePtr archive;
     528        Common::Archive *archive;
    529529
    530530        if (node.getName().hasSuffix(".zip") && !node.isDirectory()) {
    531531#ifdef USE_ZLIB
    532532                Common::ZipArchive *zipArchive = new Common::ZipArchive(node);
    533                 archive = Common::ArchivePtr(zipArchive);
     533                archive = zipArchive;
    534534
    535535                if (!zipArchive || !zipArchive->isOpen()) {
     536                        delete zipArchive;
    536537                        warning("Failed to open Zip archive '%s'.", themeName.c_str());
    537538                        return false;
    538539                }
    539540               
    540541#endif
    541542        } else if (node.isDirectory()) {
    542                 archive = Common::ArchivePtr(new Common::FSDirectory(node));
     543                archive = new Common::FSDirectory(node);
    543544        }
    544545
    545546        Common::File themercFile;
    546547        themercFile.open("THEMERC", *archive);
    547548        if (!themercFile.isOpen()) {
     549                delete archive;
    548550                warning("Theme '%s' contains no 'THEMERC' file.", themeName.c_str());
    549551                return false;
    550552        }
    551553
    552554        Common::String stxHeader = themercFile.readLine();
    553555        if (!themeConfigParseHeader(stxHeader, _themeName) || _themeName.empty()) {
     556                delete archive;
    554557                warning("Corrupted 'THEMERC' file in theme '%s'", _themeFileName.c_str());
    555558                return false;
    556559        }
    557560
    558561        Common::ArchiveMemberList members;
    559562        if (0 == archive->listMatchingMembers(members, "*.stx")) {
     563                delete archive;
    560564                warning("Found no STX files for theme '%s'.", themeName.c_str());
    561565                return false;
    562566        }
     
    566570                assert((*i)->getName().hasSuffix(".stx"));
    567571
    568572                if (_parser->loadStream((*i)->open()) == false) {
     573                        delete archive;
    569574                        warning("Failed to load STX file '%s'", (*i)->getName().c_str());
    570575                        _parser->close();
    571576                        return false;
    572577                }
    573578       
    574579                if (_parser->parse() == false) {
     580                        delete archive;
    575581                        warning("Failed to parse STX file '%s'", (*i)->getName().c_str());
    576582                        _parser->close();
    577583                        return false;
     
    580586                _parser->close();
    581587        }
    582588
     589        delete archive;
    583590        assert(!_themeName.empty());
    584591        return true;
    585592}
  • engines/kyra/resource.h

     
    5555
    5656        bool loadPakFile(Common::String filename);
    5757        bool loadPakFile(Common::String name, Common::SharedPtr<Common::ArchiveMember> file);
     58
    5859        void unloadPakFile(Common::String filename, bool remFromCache = false);
     60
    5961        bool isInPakList(Common::String filename);
     62
    6063        bool isInCacheList(Common::String name);
    6164
    6265        bool loadFileList(const Common::String &filedata);
     
    7578
    7679        bool loadFileToBuf(const char *file, void *buf, uint32 maxSize);
    7780protected:
    78         typedef Common::HashMap<Common::String, Common::ArchivePtr, Common::CaseSensitiveString_Hash, Common::CaseSensitiveString_EqualTo> ArchiveMap;
     81        typedef Common::HashMap<Common::String, Common::Archive*, Common::CaseSensitiveString_Hash, Common::CaseSensitiveString_EqualTo> ArchiveMap;
    7982        ArchiveMap _archiveCache;
    8083
    8184        Common::SearchSet _files;
    82         Common::SharedPtr<Common::SearchSet> _archiveFiles;
    83         Common::SharedPtr<Common::SearchSet> _protectedFiles;
     85        Common::SearchSet _archiveFiles;
     86        Common::SearchSet _protectedFiles;
    8487
    85         Common::ArchivePtr loadArchive(const Common::String &name, Common::SharedPtr<Common::ArchiveMember> member);
    86         Common::ArchivePtr loadInstallerArchive(const Common::String &file, const Common::String &ext, const uint8 offset);
     88        Common::Archive *loadArchive(const Common::String &name, Common::SharedPtr<Common::ArchiveMember> member);
     89        Common::Archive *loadInstallerArchive(const Common::String &file, const Common::String &ext, const uint8 offset);
    8790
    8891        void initializeLoaders();
    8992
  • engines/kyra/resource.cpp

     
    3535
    3636namespace Kyra {
    3737
    38 Resource::Resource(KyraEngine_v1 *vm) : _archiveCache(), _files(), _archiveFiles(new Common::SearchSet()), _protectedFiles(new Common::SearchSet()), _loaders(), _vm(vm) {
     38Resource::Resource(KyraEngine_v1 *vm) : _archiveCache(), _files(), _archiveFiles(), _protectedFiles(), _loaders(), _vm(vm) {
    3939        initializeLoaders();
    4040
    41         Common::SharedPtr<Common::Archive> path(new Common::FSDirectory(ConfMan.get("path"), 2));
    42         Common::SharedPtr<Common::Archive> extrapath(new Common::FSDirectory(ConfMan.get("extrapath")));
    43 
    44         _files.add("path", path, 4);
    45         _files.add("extrapath", extrapath, 4);
    46         _vm->_system->addSysArchivesToSearchSet(_files, 3);
     41        _files.add("global_search", &Common::SearchManager::instance(), 3, false);
    4742        // compressed installer archives are added at level '2',
    4843        // but that's done in Resource::reset not here
    49         _files.add("protected", _protectedFiles, 1);
    50         _files.add("archives", _archiveFiles, 0);
     44        _files.add("protected", &_protectedFiles, 1, false);
     45        _files.add("archives", &_archiveFiles, 0, false);
    5146}
    5247
    5348Resource::~Resource() {
    5449        _loaders.clear();
     50
     51        for (ArchiveMap::iterator i = _archiveCache.begin(); i != _archiveCache.end(); ++i)
     52                delete i->_value;
     53        _archiveCache.clear();
    5554}
    5655
    5756bool Resource::reset() {
     
    6867                        return true;
    6968        } else if (_vm->game() == GI_KYRA2) {
    7069                if (_vm->gameFlags().useInstallerPackage)
    71                         _files.add("installer", loadInstallerArchive("WESTWOOD", "%03d", 6), 2);
     70                        _files.add("installer", loadInstallerArchive("WESTWOOD", "%03d", 6), 2, false);
    7271
    7372                // mouse pointer, fonts, etc. required for initializing
    7473                if (_vm->gameFlags().isDemo && !_vm->gameFlags().isTalkie) {
     
    9190                return true;
    9291        } else if (_vm->game() == GI_LOL) {
    9392                if (_vm->gameFlags().useInstallerPackage)
    94                         _files.add("installer", loadInstallerArchive("WESTWOOD", "%d", 0), 2);
     93                        _files.add("installer", loadInstallerArchive("WESTWOOD", "%d", 0), 2, false);
    9594
    9695                return true;
    9796        }
     
    114113                        if (fileList.empty())
    115114                                error("Couldn't load PAK file '%s'", list[i]);
    116115
    117                         Common::ArchivePtr archive = loadArchive(list[i], *fileList.begin());
     116                        Common::Archive *archive = loadArchive(list[i], *fileList.begin());
    118117                        if (archive)
    119                                 _protectedFiles->add(list[i], archive, 0);
     118                                _protectedFiles.add(list[i], archive, 0, false);
    120119                }
    121120        } else {
    122121                for (Common::FSList::const_iterator file = fslist.begin(); file != fslist.end(); ++file) {
     
    155154bool Resource::loadPakFile(Common::String name, Common::SharedPtr<Common::ArchiveMember> file) {
    156155        name.toUppercase();
    157156
    158         if (_archiveFiles->hasArchive(name) || _protectedFiles->hasArchive(name))
     157        if (_archiveFiles.hasArchive(name) || _protectedFiles.hasArchive(name))
    159158                return true;
    160159
    161         Common::ArchivePtr archive = loadArchive(name, file);
     160        Common::Archive *archive = loadArchive(name, file);
    162161        if (!archive)
    163162                return false;
    164163
    165         _archiveFiles->add(name, archive, 0);
     164        _archiveFiles.add(name, archive, 0, false);
    166165
    167166        return true;
    168167}
     
    220219
    221220void Resource::unloadPakFile(Common::String filename, bool remFromCache) {
    222221        filename.toUppercase();
    223         _archiveFiles->remove(filename);
    224         if (remFromCache)
    225                 _archiveCache.erase(filename);
     222        _archiveFiles.remove(filename);
     223        if (remFromCache) {
     224                ArchiveMap::iterator iter = _archiveCache.find(filename);
     225                if (iter != _archiveCache.end()) {
     226                        delete iter->_value;
     227                        _archiveCache.erase(filename);
     228                }
     229        }
    226230        // We do not remove files from '_protectedFiles' here, since
    227231        // those are protected against unloading.
    228232}
    229233
    230234bool Resource::isInPakList(Common::String filename) {
    231235        filename.toUppercase();
    232         return (_archiveFiles->hasArchive(filename) || _protectedFiles->hasArchive(filename));
     236        return (_archiveFiles.hasArchive(filename) || _protectedFiles.hasArchive(filename));
    233237}
    234238
    235239bool Resource::isInCacheList(Common::String name) {
     
    238242}
    239243
    240244void Resource::unloadAllPakFiles() {
    241         _archiveFiles->clear();
    242         _protectedFiles->clear();
     245        _archiveFiles.clear();
     246        _protectedFiles.clear();
    243247}
    244248
    245249void Resource::listFiles(const Common::String &pattern, Common::ArchiveMemberList &list) {
     
    294298        return _files.openFile(file);
    295299}
    296300
    297 Common::ArchivePtr Resource::loadArchive(const Common::String &name, Common::SharedPtr<Common::ArchiveMember> member) {
     301Common::Archive *Resource::loadArchive(const Common::String &name, Common::SharedPtr<Common::ArchiveMember> member) {
    298302        ArchiveMap::iterator cachedArchive = _archiveCache.find(name);
    299303        if (cachedArchive != _archiveCache.end())
    300304                return cachedArchive->_value;
     
    302306        Common::SeekableReadStream *stream = member->open();
    303307
    304308        if (!stream)
    305                 return Common::ArchivePtr();
     309                return 0;
    306310
    307         Common::ArchivePtr archive;
     311        Common::Archive *archive = 0;
    308312        for (LoaderList::const_iterator i = _loaders.begin(); i != _loaders.end(); ++i) {
    309313                if ((*i)->checkFilename(name)) {
    310314                        if ((*i)->isLoadable(name, *stream)) {
    311315                                stream->seek(0, SEEK_SET);
    312                                 archive = Common::ArchivePtr((*i)->load(member, *stream));
     316                                archive = (*i)->load(member, *stream);
    313317                                break;
    314318                        } else {
    315319                                stream->seek(0, SEEK_SET);
     
    320324        delete stream;
    321325
    322326        if (!archive)
    323                 return Common::ArchivePtr();
     327                return 0;
    324328
    325329        _archiveCache[name] = archive;
    326330        return archive;
    327331}
    328332
    329 Common::ArchivePtr Resource::loadInstallerArchive(const Common::String &file, const Common::String &ext, const uint8 offset) {
     333Common::Archive *Resource::loadInstallerArchive(const Common::String &file, const Common::String &ext, const uint8 offset) {
    330334        ArchiveMap::iterator cachedArchive = _archiveCache.find(file);
    331335        if (cachedArchive != _archiveCache.end())
    332336                return cachedArchive->_value;
    333337
    334         Common::ArchivePtr archive(InstallerLoader::load(this, file, ext, offset));
     338        Common::Archive *archive = InstallerLoader::load(this, file, ext, offset);
    335339        if (!archive)
    336                 return Common::ArchivePtr();
     340                return 0;
    337341
    338342        _archiveCache[file] = archive;
    339343        return archive;
  • engines/parallaction/disk_br.cpp

     
    129129
    130130        debugC(5, kDebugDisk, "DosDisk_br::selectArchive: adding part directory to search set");
    131131        _sset.remove("part");
    132         Common::SharedPtr<Common::FSDirectory> partDir(_baseDir->getSubDirectory(name, 3));
    133         _sset.add("part", partDir, 10);
     132        _sset.add("part", _baseDir->getSubDirectory(name, 3), 10);
    134133
    135134        return oldPath;
    136135}
     
    149148        // TODO: clarify whether the engine or OSystem should add the base game directory to the search manager.
    150149        // Right now, I am keeping an internal search set to do the job.
    151150        _baseDir = Common::SharedPtr<Common::FSDirectory>(new Common::FSDirectory(ConfMan.get("path")));
    152         _sset.add("base", _baseDir, 5);
     151        // FIXME: We use this gross hack here since we switched SearchSet to accept plain pointers
     152        _sset.add("base", _baseDir.get(), 5, false);
    153153}
    154154
    155155
     
    392392        // TODO: clarify whether the engine or OSystem should add the base game directory to the search manager.
    393393        // Right now, I am keeping an internal search set to do the job.
    394394        _baseDir = Common::SharedPtr<Common::FSDirectory>(new Common::FSDirectory(ConfMan.get("path"), 2));
    395         _sset.add("base", _baseDir, 5);
     395        // FIXME: We use this gross hack here since we switched SearchSet to accept plain pointers
     396        _sset.add("base", _baseDir.get(), 5, false);
    396397}
    397398
    398399
     
    413414
    414415void AmigaDisk_br::init() {
    415416        _baseDir = Common::SharedPtr<Common::FSDirectory>(new Common::FSDirectory(ConfMan.get("path")));
    416         _sset.add("base", _baseDir, 5);
     417        // FIXME: We use this gross hack here since we switched SearchSet to accept plain pointers
     418        _sset.add("base", _baseDir.get(), 5, false);
    417419
    418420        const Common::String subDirNames[3] = { "fonts", "backs", "common" };
    419421        const Common::String subDirPrefixes[3] = { "fonts", "backs", Common::String::emptyString };
    420         for (int i = 0; i < 3; i++) {
    421                 Common::SharedPtr<Common::Archive> subDir(_baseDir->getSubDirectory(subDirPrefixes[i], subDirNames[i], 2));
    422                 _sset.add(subDirNames[i], subDir, 6);
    423         }
     422        for (int i = 0; i < 3; i++)
     423                _sset.add(subDirNames[i], _baseDir->getSubDirectory(subDirPrefixes[i], subDirNames[i], 2), 6);
    424424}
    425425
    426426AmigaDisk_br::~AmigaDisk_br() {
     
    623623
    624624        debugC(5, kDebugDisk, "AmigaDisk_br::selectArchive: adding part directory to search set");
    625625        _sset.remove("part");
    626         Common::SharedPtr<Common::FSDirectory> partDir(_baseDir->getSubDirectory(name, 3));
    627         _sset.add("part", partDir, 10);
     626        _sset.add("part", _baseDir->getSubDirectory(name, 3), 10);
    628627
    629628        return oldPath;
    630629}
  • common/file.cpp

     
    4949        if (level <= 0 || !dir.exists() || !dir.isDirectory())
    5050                return;
    5151
    52         Common::ArchivePtr dataArchive(new Common::FSDirectory(dir, level));
    53         SearchMan.add(dir.getPath(), dataArchive);
     52        SearchMan.add(dir.getPath(), new Common::FSDirectory(dir, level));
    5453}
    5554
    5655void File::resetDefaultDirectories() {
  • common/archive.cpp

     
    281281        _list.insert(it, node);
    282282}
    283283
    284 void SearchSet::add(const String& name, ArchivePtr archive, int priority) {
     284void SearchSet::add(const String& name, Archive *archive, int priority, bool autoFree) {
    285285        if (find(name) == _list.end()) {
    286                 Node node(priority, name, archive);
     286                Node node(priority, name, archive, autoFree);
    287287                insert(node);
    288288        } else {
    289289                warning("SearchSet::add: archive '%s' already present", name.c_str());
     
    294294void SearchSet::remove(const String& name) {
    295295        ArchiveList::iterator it = find(name);
    296296        if (it != _list.end()) {
     297                if (it->_autoFree)
     298                        delete it->_arc;
    297299                _list.erase(it);
    298300        }
    299301}
     
    303305}
    304306
    305307void SearchSet::clear() {
     308        for (ArchiveList::iterator i = _list.begin(); i != _list.end(); ++i) {
     309                if (i->_autoFree)
     310                        delete i->_arc;
     311        }
     312
    306313        _list.clear();
    307314}
    308315
     
    387394}
    388395
    389396void SearchManager::addDirectoryRecursive(const String &name, const String &directory, int depth, int priority) {
    390         add(name, ArchivePtr(new FSDirectory(directory, depth)), priority);
     397        add(name, new FSDirectory(directory, depth), priority, true);
    391398}
    392399
    393400void SearchManager::clear() {
     
    400407
    401408        // Add the current dir as a very last resort.
    402409        // See also bug #2137680.
    403         add(".", ArchivePtr(new FSDirectory(".")), -2);
     410        add(".", new FSDirectory("."), -2, true);
    404411}
    405412
    406413} // namespace Common
  • common/archive.h

     
    114114};
    115115
    116116
    117 typedef SharedPtr<Archive> ArchivePtr;
    118 
    119 
    120117/**
    121118 * FSDirectory models a directory tree from the filesystem and allows users
    122119 * to access it through the Archive interface. Searching is case-insensitive,
     
    228225 */
    229226class SearchSet : public Archive {
    230227        struct Node {
    231                 int                     _priority;
    232                 String          _name;
    233                 ArchivePtr      _arc;
    234                 Node(int priority, const String &name, ArchivePtr arc)
    235                         : _priority(priority), _name(name), _arc(arc) {
     228                int             _priority;
     229                String  _name;
     230                Archive *_arc;
     231                bool    _autoFree;
     232                Node(int priority, const String &name, Archive *arc, bool autoFree)
     233                        : _priority(priority), _name(name), _arc(arc), _autoFree(autoFree) {
    236234                }
    237235        };
    238236        typedef List<Node> ArchiveList;
     
    244242        void insert(const Node& node);
    245243
    246244public:
     245        virtual ~SearchSet() { clear(); }
     246
    247247        /**
    248248         * Add a new archive to the searchable set.
    249249         */
    250         void add(const String& name, ArchivePtr archive, int priority = 0);
     250        void add(const String& name, Archive *arch, int priority = 0, bool autoFree = true);
    251251
    252252        /**
    253253         * Remove an archive from the searchable set.
  • graphics/imageman.cpp

     
    6666                arch = dir;
    6767        }
    6868
    69         _archives.add(name, Common::ArchivePtr(arch));
     69        _archives.add(name, arch);
    7070        return true;
    7171}
    7272
  • backends/platform/sdl/sdl.cpp

     
    282282        // FIXME: We use depth = 4 for now, to match the old code. May want to change that
    283283        Common::FSNode dataNode(DATA_PATH);
    284284        if (dataNode.exists() && dataNode.isDirectory()) {
    285                 Common::ArchivePtr dataArchive(new Common::FSDirectory(dataNode, 4));
    286                 s.add(DATA_PATH, dataArchive, priority);
     285                s.add(DATA_PATH, new Common::FSDirectory(dataNode, 4), priority);
    287286        }
    288287#endif
    289288
     
    296295                if (CFURLGetFileSystemRepresentation(fileUrl, true, buf, sizeof(buf))) {
    297296                        // Success: Add it to the search path
    298297                        Common::String bundlePath((const char *)buf);
    299                         Common::ArchivePtr bundleArchive(new Common::FSDirectory(bundlePath));
    300                         s.add("__OSX_BUNDLE__", bundleArchive, priority);
     298                        s.add("__OSX_BUNDLE__", new Common::FSDirectory(bundlePath), priority);
    301299                }
    302300                CFRelease(fileUrl);
    303301        }