ScummVM API documentation
resource.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  *
21  * This file is dual-licensed.
22  * In addition to the GPLv3 license mentioned above, MojoTouch has
23  * exclusively licensed this code on March 23th, 2024, to be used in
24  * closed-source products.
25  * Therefore, any contributions (commits) to it will also be dual-licensed.
26  *
27  */
28 
29 #ifndef TOON_RESOURCE_H
30 #define TOON_RESOURCE_H
31 
32 #include "common/array.h"
33 #include "common/str.h"
34 #include "common/file.h"
35 #include "common/stream.h"
36 
37 #define MAX_CACHE_SIZE (4 * 1024 * 1024)
38 
39 namespace Toon {
40 
41 class PakFile {
42 public:
43  PakFile();
44  ~PakFile();
45 
46  void open(Common::SeekableReadStream *rs, const Common::Path &packName);
47  uint8 *getFileData(const Common::Path &fileName, uint32 *fileSize);
48  Common::Path getPackName() { return _packName; }
49  Common::SeekableReadStream *createReadStream(const Common::Path &fileName);
50  void close();
51 
52 protected:
53  struct File {
54  char _name[13];
55  int32 _offset;
56  int32 _size;
57  };
58  Common::Path _packName;
59 
60  uint32 _numFiles;
61  Common::Array<File> _files;
62 };
63 
64 class ToonEngine;
65 
66 class CacheEntry {
67 public:
68  CacheEntry() : _age(0), _size(0), _data(0) {}
69  ~CacheEntry() {
70  free(_data);
71  }
72 
73  Common::Path _packName;
74  Common::Path _fileName;
75  uint32 _age;
76  uint32 _size;
77  uint8 *_data;
78 };
79 
80 class Resources {
81 public:
82  Resources(ToonEngine *vm);
83  ~Resources();
84  bool openPackage(const Common::Path &file);
85  void closePackage(const Common::Path &fileName);
86  Common::SeekableReadStream *openFile(const Common::Path &file);
87  uint8 *getFileData(const Common::Path &fileName, uint32 *fileSize); // this memory must be copied to your own structures!
88  void purgeFileData();
89 
90 protected:
91  ToonEngine *_vm;
92  Common::Array<uint8 *> _allocatedFileData;
93  Common::Array<PakFile *> _pakFiles;
94  uint32 _cacheSize;
95  Common::Array<CacheEntry *> _resourceCache;
96 
97  void removePackageFromCache(const Common::Path &packName);
98  bool getFromCache(const Common::Path &fileName, uint32 *fileSize, uint8 **fileData);
99  void addToCache(const Common::Path &packName, const Common::Path &fileName, uint32 fileSize, uint8 *fileData);
100 };
101 
102 } // End of namespace Toon
103 #endif
Definition: resource.h:41
Definition: array.h:52
Definition: path.h:52
Definition: stream.h:745
Definition: toon.h:105
Definition: resource.h:80
Definition: anim.h:39
Definition: resource.h:66
Definition: resource.h:53