xoreos  0.0.5
resman.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef AURORA_RESMAN_H
26 #define AURORA_RESMAN_H
27 
28 #include <list>
29 #include <vector>
30 #include <map>
31 #include <set>
32 
33 #include "src/common/types.h"
34 #include "src/common/ustring.h"
35 #include "src/common/singleton.h"
36 #include "src/common/filelist.h"
37 #include "src/common/hash.h"
38 #include "src/common/changeid.h"
39 
40 #include "src/aurora/types.h"
41 
42 namespace Common {
43  class SeekableReadStream;
44 }
45 
46 namespace Aurora {
47 
48 class Archive;
49 class KEYFile;
50 class KEYDataFile;
51 
55 class ResourceManager : public Common::Singleton<ResourceManager> {
56 public:
57  struct ResourceID {
61  };
62 
65 
67  void clear();
68 
69  // .--- Global resource properties
71  void setRIMsAreERFs(bool rimsAreERFs);
72 
74  void setHasSmall(bool hasSmall);
75 
77  void setHashAlgo(Common::HashAlgo algo);
78 
80  void setCursorRemap(const std::vector<Common::UString> &remap);
81 
87  void addTypeAlias(FileType alias, FileType realType);
88  // '---
89 
90  // .--- Data base
104  void registerDataBase(const Common::UString &path);
105 
107  const Common::UString &getDataBase() const;
108  // '---
109 
110  // .--- Archives
116  bool hasArchive(const Common::UString &file);
117 
125  void indexArchive(const Common::UString &file, uint32 priority, Common::ChangeID *changeID = 0);
126  // '---
127 
136  void indexArchive(const Common::UString &file, uint32 priority, const std::vector<byte> &password,
137  Common::ChangeID *changeID = 0);
138  // '---
139 
140  // .--- Directories and files
146  bool hasResourceDir(const Common::UString &dir);
147 
157  void indexResourceFile(const Common::UString &file, uint32 priority, Common::ChangeID *changeID = 0);
158 
171  void indexResourceDir(const Common::UString &dir, const char *glob, int depth,
172  uint32 priority, Common::ChangeID *changeID = 0);
173  // '---
174 
175  // .--- Utility methods
177  void undo(Common::ChangeID &changeID);
178 
186  void blacklist(const Common::UString &name, FileType type);
187 
195  void declareResource(const Common::UString &name, FileType type);
196 
203  void declareResource(const Common::UString &name);
204  // '---
205 
206  // .--- Resources
212  bool hasResource(uint64 hash) const;
213 
220  bool hasResource(const Common::UString &name, FileType type) const;
221 
228  bool hasResource(const Common::UString &name, ResourceType type) const;
229 
235  bool hasResource(const Common::UString &name) const;
236 
243  bool hasResource(const Common::UString &name, const std::vector<FileType> &types) const;
244 
253  Common::UString findResourceFile(const Common::UString &name, FileType type) const;
254 
264 
273 
282  Common::UString findResourceFile(const Common::UString &name, const std::vector<FileType> &types) const;
283 
290  Common::SeekableReadStream *getResource(uint64 hash, FileType *type = 0) const;
291 
299 
306 
318  const std::vector<FileType> &types, FileType *foundType = 0) const;
319 
328  const Common::UString &name, FileType *foundType = 0) const;
329 
331  void getAvailableResources(FileType type, std::list<ResourceID> &list) const;
333  void getAvailableResources(const std::vector<FileType> &types, std::list<ResourceID> &list) const;
335  void getAvailableResources(ResourceType type, std::list<ResourceID> &list) const;
336  // '---
337 
339  void dumpResourcesList(const Common::UString &fileName) const;
340 
341 
342 private:
343  typedef std::vector<FileType> FileTypeList;
344  typedef std::set<FileType> FileTypeSet;
345 
346  struct Resource;
347  struct OpenedArchive;
348 
349  // .--- Archives
350  struct KnownArchive {
353 
356 
359 
360  KnownArchive();
362  };
363 
364  struct OpenedArchive {
367 
370 
374  std::list<OpenedArchive *> children;
375 
376  OpenedArchive();
377 
378  void set(KnownArchive &kA, Archive &a);
379  };
380 
382  typedef std::list<KnownArchive> KnownArchives;
384  typedef std::list<OpenedArchive> OpenedArchives;
385  // '---
386 
387  // .--- Resources
389  enum Source {
393  };
394 
396  struct Resource {
399 
401  bool isSmall;
402 
405 
407  std::pair<KnownArchives *, KnownArchives::iterator> selfArchive;
408 
411 
412  // For kSourceFile
414 
415  // For kSourceArchive
418 
419  Resource();
420 
421  bool operator<(const Resource &right) const;
422  };
423 
425  typedef std::list<Resource> ResourceList;
427  typedef std::map<uint64, ResourceList> ResourceMap;
428  // '---
429 
430  // .--- Changes
432  typedef std::pair<KnownArchives *, KnownArchives::iterator> KnownArchiveChange;
434  typedef OpenedArchives::iterator OpenedArchiveChange;
436  struct ResourceChange {
437  ResourceMap::iterator hashIt;
438  ResourceList::iterator resIt;
439  };
440 
441  typedef std::list<KnownArchiveChange> KnownArchiveChanges;
442  typedef std::list<OpenedArchiveChange> OpenedArchiveChanges;
443  typedef std::list<ResourceChange> ResourceChanges;
444 
446  struct ChangeSet {
450  };
451 
452  typedef std::list<ChangeSet> ChangeSetList;
453 
454  class Change : public Common::ChangeContent {
455  private:
456  Change(ChangeSetList::iterator change) : _change(change) { }
457 
458  ChangeSetList::iterator _change;
459 
460  friend class ResourceManager;
461 
462  public:
463  ~Change() { }
464 
465  Common::ChangeContent *clone() const { return new Change(_change); }
466  };
467  // '---
468 
469 
471  bool _hasSmall;
472 
475 
477  std::vector<Common::UString> _cursorRemap;
478 
483 
486 
488  std::map<FileType, FileType> _typeAliases;
489 
492 
495 
496 
497  void clearResources();
498 
499  // .--- Searching for archives
502  // '---
503 
504  // .--- Indexing archives
505  void indexKEY(Common::SeekableReadStream *stream, uint32 priority, Change *change);
507  std::vector<KnownArchive *> &archives, std::vector<KEYDataFile *> &keyData);
508 
509  void indexArchive(KnownArchive &knownArchive, Archive *archive,
510  uint32 priority, Change *change);
511 
513  // '---
514 
515  // .--- Adding resources
516 
517  bool checkResourceIsArchive(Resource &resource, Change *change);
518 
519  void addResource(Resource &resource, uint64 hash, Change *change);
520  void addResource(const Common::UString &path, Change *change, uint32 priority);
521 
522  void addResources(const Common::FileList &files, Change *change, uint32 priority);
523  // '---
524 
525  // .--- Finding and getting resources
526  const Resource *getRes(uint64 hash) const;
527  const Resource *getRes(const Common::UString &name, const std::vector<FileType> &types) const;
528  const Resource *getRes(const Common::UString &name, FileType type) const;
529 
530  Common::SeekableReadStream *getResource(const Resource &res, bool tryNoCopy = false) const;
531 
532  Common::SeekableReadStream *getArchiveResource(const Resource &res, bool tryNoCopy = false) const;
533 
534  uint32 getResourceSize(const Resource &res) const;
535  // '---
536 
537  // .--- Resource utility methods
538  bool normalizeType(Resource &resource);
539 
540  ArchiveType getArchiveType(FileType type) const;
541  ArchiveType getArchiveType(const Common::UString &name) const;
542  Common::UString getArchiveName(const Resource &resource) const;
543 
544  inline uint64 getHash(const Common::UString &name, FileType type) const;
545  inline uint64 getHash(const Common::UString &name) const;
546 
547  void checkHashCollision(const Resource &resource, ResourceMap::const_iterator resList);
548 
550  // '---
551 
552 };
553 
554 } // End of namespace Aurora
555 
557 #define ResMan ::Aurora::ResourceManager::instance()
558 
559 #endif // AURORA_RESMAN_H
ResourceType
Definition: types.h:407
uint32 getResourceSize(const Resource &res) const
Definition: resman.cpp:703
ChangeSetList::iterator _change
Definition: resman.h:458
FileTypeList _resourceTypeTypes[kResourceMAX]
All valid resource type file types.
Definition: resman.h:494
Class and macro for implementing singletons.
bool hasResourceDir(const Common::UString &dir)
Does a specific directory, relative to the base directory, exist?
Definition: resman.cpp:473
void addResources(const Common::FileList &files, Change *change, uint32 priority)
Definition: resman.cpp:1011
Source source
Where can the resource be found?
Definition: resman.h:410
std::list< OpenedArchive > OpenedArchives
List of all opened archive files.
Definition: resman.h:384
Definition: 2dafile.h:39
ResourceMap::iterator hashIt
Definition: resman.h:437
A class holding an UTF-8 string.
Definition: ustring.h:48
KnownArchiveChanges knownArchives
Definition: resman.h:447
Source
Where a resource can be found.
Definition: resman.h:389
void setHasSmall(bool hasSmall)
Do we have "small" files (compressed with Nintendo DS&#39;s LZSS algorithm)?
Definition: resman.cpp:222
FileType type
The resource&#39;s type.
Definition: resman.h:398
KnownArchive * known
The information we know about this archive.
Definition: resman.h:369
uint64_t uint64
Definition: types.h:206
Common::SeekableReadStream * openArchiveStream(const KnownArchive &archive) const
Definition: resman.cpp:296
uint32 archiveIndex
Index into the archive.
Definition: resman.h:417
const Resource * getRes(uint64 hash) const
Definition: resman.cpp:1016
void indexResourceDir(const Common::UString &dir, const char *glob, int depth, uint32 priority, Common::ChangeID *changeID=0)
Add a directory&#39;s contents to the resource manager.
Definition: resman.cpp:497
A set of changes produced by a manager operation.
Definition: resman.h:446
std::pair< KnownArchives *, KnownArchives::iterator > KnownArchiveChange
A change produced by adding an archive.
Definition: resman.h:432
Utility hash functions.
std::list< ResourceChange > ResourceChanges
Definition: resman.h:443
std::vector< Common::UString > _cursorRemap
Cursor ID -> cursor name.
Definition: resman.h:477
A change produced by indexing archive resources.
Definition: resman.h:436
std::set< FileType > FileTypeSet
Definition: resman.h:344
KnownArchives _knownArchives[kArchiveMAX]
List of all known archives.
Definition: resman.h:484
void clear()
Clear all resource information.
Definition: resman.cpp:177
void dumpResourcesList(const Common::UString &fileName) const
Dump a list of all resources into a file.
Definition: resman.cpp:1052
std::list< Resource > ResourceList
List of resources, sorted by priority.
Definition: resman.h:425
ChangeSetList _changes
Changes produced by indexing the currently known resources.
Definition: resman.h:491
Archive * archive
The actual archive.
Definition: resman.h:366
bool isSmall
Is this a "small" (compressed Nintendo DS) file?
Definition: resman.h:401
void setCursorRemap(const std::vector< Common::UString > &remap)
Set the array used to map cursor ID to cursor names.
Definition: resman.cpp:233
void indexKEY(Common::SeekableReadStream *stream, uint32 priority, Change *change)
Definition: resman.cpp:406
bool normalizeType(Resource &resource)
Definition: resman.cpp:866
Generic template base class for implementing the singleton design pattern.
Definition: singleton.h:61
OpenedArchive * parent
Is this archive is found in another archive, this is the "parent" archive.
Definition: resman.h:372
ArchiveType
Definition: types.h:416
Common::HashAlgo _hashAlgo
With which hash algorithm are/should the names be hashed?
Definition: resman.h:474
void addResource(Resource &resource, uint64 hash, Change *change)
Definition: resman.cpp:953
Helper class to represent an undoable change.
void registerDataBase(const Common::UString &path)
Register a path to be the data base.
Definition: resman.cpp:237
std::list< OpenedArchive * > children
If this archive contains other archives, these are the opened "children" archives.
Definition: resman.h:374
OpenedArchiveChanges openedArchives
Definition: resman.h:448
FileTypeSet _archiveTypeTypes[kArchiveMAX]
All valid archive types file types.
Definition: resman.h:493
void blacklist(const Common::UString &name, FileType type)
Blacklist a specific resource.
Definition: resman.cpp:612
Common::UString findResourceFile(const Common::UString &name, FileType type) const
Find and return the absolute filesystem file behind a resource.
Definition: resman.cpp:676
bool _hasSmall
Do we have "small" files?
Definition: resman.h:471
std::map< FileType, FileType > _typeAliases
The current type aliases, changing one type to another.
Definition: resman.h:488
bool hasResource(uint64 hash) const
Does a specific resource exist?
Definition: resman.cpp:672
ArchiveType getArchiveType(FileType type) const
Definition: resman.cpp:837
std::list< KnownArchiveChange > KnownArchiveChanges
Definition: resman.h:441
HashAlgo
The algorithm used for hashing.
Definition: hash.h:37
Low-level type definitions to handle fixed width types portably.
void indexArchive(const Common::UString &file, uint32 priority, Common::ChangeID *changeID=0)
Add all the resources of an archive to the resource manager.
Definition: resman.cpp:361
Common::ChangeContent * clone() const
Definition: resman.h:465
Common::UString getArchiveName(const Resource &resource) const
Definition: resman.cpp:849
Common::UString name
The resource&#39;s name.
Definition: resman.h:397
uint64 getHash(const Common::UString &name, FileType type) const
Definition: resman.cpp:895
ResourceMap _resources
All currently known resources.
Definition: resman.h:490
std::list< KnownArchive > KnownArchives
List of all known archive files.
Definition: resman.h:382
bool hasArchive(const Common::UString &file)
Does a specific archive exist?
Definition: resman.cpp:292
std::map< uint64, ResourceList > ResourceMap
Map over resources, indexed by their hashed name.
Definition: resman.h:427
Unicode string handling.
Basic type definitions to handle files used in BioWare&#39;s Aurora engine.
KnownArchive * findArchive(const Common::UString &file)
Definition: resman.cpp:267
Common::UString path
The file&#39;s path.
Definition: resman.h:413
void indexResourceFile(const Common::UString &file, uint32 priority, Common::ChangeID *changeID=0)
Add a single file to the resource manager.
Definition: resman.cpp:480
OpenedArchive * opened
The opened archive, if it was.
Definition: resman.h:358
Common::SeekableReadStream * getResource(uint64 hash, FileType *type=0) const
Return a resource.
Definition: resman.cpp:750
uint32 openKEYBIFs(Common::SeekableReadStream *keyStream, std::vector< KnownArchive *> &archives, std::vector< KEYDataFile *> &keyData)
Definition: resman.cpp:367
Change(ChangeSetList::iterator change)
Definition: resman.h:456
A class representing an undoable change.
Definition: changeid.h:35
std::list< ChangeSet > ChangeSetList
Definition: resman.h:452
std::list< OpenedArchiveChange > OpenedArchiveChanges
Definition: resman.h:442
uint32_t uint32
Definition: types.h:204
A list of files.
Definition: filelist.h:35
bool operator<(const Resource &right) const
Definition: resman.cpp:104
FileType
Various file types used by the Aurora engine and found in archives.
Definition: types.h:56
Common::UString name
The archive&#39;s name.
Definition: resman.h:351
uint32 priority
The resource&#39;s priority over others with the same name and type.
Definition: resman.h:404
Common::UString _baseArchive
The data base archive (if any), the archive the current game is in.
Definition: resman.h:482
A list of files.
Resource * resource
The resource this archive is.
Definition: resman.h:355
An abstract file archive.
Definition: archive.h:45
const Common::UString & getDataBase() const
Return the path of the currently registered base data directory or archive.
Definition: resman.cpp:260
A resource manager holding information about and handling all request for all resources usable by the...
Definition: resman.h:55
std::pair< KnownArchives *, KnownArchives::iterator > selfArchive
The archive this resource itself is.
Definition: resman.h:407
void undo(Common::ChangeID &changeID)
Undo the changes done in the specified change ID.
Definition: resman.cpp:529
void setRIMsAreERFs(bool rimsAreERFs)
Are .rim/.rimp files actually ERF files?
Definition: resman.cpp:205
void addTypeAlias(FileType alias, FileType realType)
Add an alias for one file type to another.
Definition: resman.cpp:608
Common::UString _baseDir
The data base directory (if any), the directory the current game is in.
Definition: resman.h:480
Common::SeekableReadStream * getArchiveResource(const Resource &res, bool tryNoCopy=false) const
Definition: resman.cpp:717
std::vector< FileType > FileTypeList
Definition: resman.h:343
OpenedArchives _openedArchives
List of currently used archives.
Definition: resman.h:485
ArchiveType type
The archive&#39;s type.
Definition: resman.h:352
Interface for a seekable & readable data stream.
Definition: readstream.h:265
void getAvailableResources(FileType type, std::list< ResourceID > &list) const
Return a list of all available resources of the specified type.
Definition: resman.cpp:800
void declareResource(const Common::UString &name, FileType type)
Declare the name of a specific resource.
Definition: resman.cpp:621
OpenedArchives::iterator OpenedArchiveChange
A change produced by indexing/opening an archive.
Definition: resman.h:434
void setHashAlgo(Common::HashAlgo algo)
With which hash algorithm are/should the names be hashed?
Definition: resman.cpp:226
bool checkResourceIsArchive(Resource &resource, Change *change)
Definition: resman.cpp:922
Change * newChangeSet(Common::ChangeID &changeID)
Definition: resman.cpp:1083
ResourceList::iterator resIt
Definition: resman.h:438
void checkHashCollision(const Resource &resource, ResourceMap::const_iterator resList)
Definition: resman.cpp:903
OpenedArchive * archive
Pointer to the opened archive.
Definition: resman.h:416