Add license text to the top of every source file.
[Tsunagari.git] / src / resourcer.h
blobe6d135e37e06a36ee7f8263f37041eede77e9642
1 /*********************************
2 ** Tsunagari Tile Engine **
3 ** resourcer.h **
4 ** Copyright 2011-2012 OmegaSDG **
5 *********************************/
7 // **********
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // **********
27 #ifndef RESOURCER_H
28 #define RESOURCER_H
30 #include <deque>
31 #include <string>
33 #include <boost/shared_ptr.hpp>
34 #include <boost/unordered_map.hpp>
35 #include <libxml/parser.h>
36 #include <Python.h>
38 #include "cache.h"
39 #include "image.h"
40 #include "sound.h"
41 #include "tiledimage.h"
42 #include "xml.h"
44 namespace Gosu {
45 class Buffer;
46 class Sample;
47 class Song;
50 // We hand out and manage resources in these forms:
51 typedef boost::shared_ptr<Sound> SampleRef;
52 typedef boost::shared_ptr<Gosu::Song> SongRef;
53 typedef boost::shared_ptr<XMLDoc> XMLRef;
54 typedef boost::shared_ptr<xmlDtd> DTDRef;
55 typedef boost::shared_ptr<std::string> StringRef;
57 /**
58 * Provides data and resource extraction for a World.
59 * Each World comes bundled with associated data in the form of a Zip file.
60 * A Resourcer object knows how to navigate the data, extract individual
61 * requested files, and process the files into data structures. The final data
62 * structures are kept in memory for future requests.
64 class Resourcer
66 public:
67 //! Get the Resourcer for the current World.
68 static Resourcer* instance();
70 Resourcer();
71 ~Resourcer();
72 bool init(char* argv0);
73 bool prependPath(const std::string& path);
74 bool appendPath(const std::string& path);
75 bool rmPath(const std::string& path);
77 //! Returns true if the World contains a resource by that name.
78 bool resourceExists(const std::string& name) const;
80 //! Request an image from the World.
81 ImageRef getImage(const std::string& name);
83 //! Request an image resource from the World and splits it into a
84 //! number of tiles that each have width and height w by h.
85 TiledImageRef getTiledImage(const std::string& name,
86 int w, int h);
88 //! Request a sound object from the World. The sound will be
89 //! completely loaded into memory at once.
90 SampleRef getSample(const std::string& name);
92 //! Request a music stream from the World. The stream will be
93 //! loaded from disk as it is being played.
94 SongRef getSong(const std::string& name);
96 //! Request an XML document from the World.
97 XMLRef getXMLDoc(const std::string& name,
98 const std::string& dtdPath);
100 //! Request a Python script from the World be run.
101 bool runPythonScript(const std::string& name);
103 //! Request a text file from the World.
104 std::string getText(const std::string& name);
106 //! Expunge old resources cached in memory. Decisions on which are
107 //! removed and which are kept are based on the global Conf struct.
108 void garbageCollect();
110 private:
111 DTDRef parseDTD(const std::string& path);
112 bool preloadDTDs();
114 //! Read an XML document from the game archive and parse it.
115 XMLDoc* readXMLDoc(const std::string& name,
116 const std::string& dtdFile);
118 //! Requests an XML DTD from Tsunagari. (Does not load from the World.)
119 xmlDtd* getDTD(const std::string& name);
121 //! Read a generic resource from the game archive.
122 Gosu::Buffer* readBuffer(const std::string& name);
124 //! Read a string resource from the game archive.
125 std::string readString(const std::string& name);
127 //! Reads a file from the game archive.
128 template <class T>
129 bool readFromDisk(const std::string& name, T& buf);
131 //! Build a pathname: arhive + "/" + entryName
132 std::string path(const std::string& entryName) const;
134 private:
135 // Caches that store processed, game-ready objects. Garbage collected.
136 Cache<ImageRef> images;
137 Cache<TiledImageRef> tiles;
138 Cache<SampleRef> sounds;
139 Cache<SongRef> songs;
140 Cache<XMLRef> xmls;
141 Cache<PyCodeObject*> codes;
142 Cache<StringRef> texts;
144 // DTDs don't expire. No garbage collection.
145 typedef boost::unordered_map<std::string, DTDRef> TextMap;
146 TextMap dtds;
149 void exportResourcer();
151 #endif