1 /*********************************
2 ** Tsunagari Tile Engine **
4 ** Copyright 2011-2012 OmegaSDG **
5 *********************************/
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
33 #include <boost/shared_ptr.hpp>
34 #include <boost/unordered_map.hpp>
35 #include <libxml/parser.h>
41 #include "tiledimage.h"
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
;
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.
67 //! Get the Resourcer for the current World.
68 static Resourcer
* instance();
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
,
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();
111 DTDRef
parseDTD(const std::string
& path
);
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.
129 bool readFromDisk(const std::string
& name
, T
& buf
);
131 //! Build a pathname: arhive + "/" + entryName
132 std::string
path(const std::string
& entryName
) const;
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
;
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
;
149 void exportResourcer();