1 #ifndef MSP_DATAFILE_PACKCOLLECTION_H_
2 #define MSP_DATAFILE_PACKCOLLECTION_H_
4 #include "collection.h"
5 #include "objectloader.h"
11 A collection class that loads data from pack files. As opposed to plain
12 collection files, pack files are composed from a number of logical files. They
13 also contain a directory with a list of objects contained in the pack and which
14 logical files they are in. This allows the pack to be loaded in a piecewise
15 manner instead of all at once.
17 It's possible for a pack file to contain plain collection files as well. When
18 an object from such a file is requested, the entire sub-collection it is stored
21 class PackCollection: public Collection
27 typedef std::map<std::string, const Object *> ObjectMap;
32 class Loader: public ObjectLoader<Pack>
37 void file(const std::string &);
43 std::list<File> files;
46 Pack(const std::string &);
48 const std::string &get_filename() const { return filename; }
49 unsigned get_base_offset() const { return base_offset; }
51 void collect_objects(ObjectMap &) const;
57 class Loader: public ObjectLoader<File>
62 virtual void finish();
63 void object(const std::string &, const std::string &);
72 std::list<Object> objects;
76 File(const Pack &, const std::string &);
78 RefPtr<IO::Base> open() const;
79 const std::string &get_filename() const { return filename; }
80 std::string get_full_name() const;
81 bool is_collection() const { return collection; }
84 bool is_loaded() const { return loaded; }
86 void collect_objects(ObjectMap &) const;
97 Object(File &, const std::string &, const std::string &);
99 File &get_file() const { return file; }
100 const std::string &get_name() const { return name; }
101 const std::string &get_keyword() const { return keyword; }
104 std::list<Pack> packs;
108 /** Adds a pack file to the collection. The directory is read immediately,
109 and packed objects are loaded as they are needed. */
110 void add_pack_file(const std::string &);
114 CollectionItemType<T> &add_type()
116 return Collection::add_type<T>().creator(&PackCollection::create<T>);
121 T *create(const std::string &name)
123 ObjectMap::iterator i = objects.find(name);
127 File &file = i->second->get_file();
132 RefPtr<IO::Base> in = file.open();
133 Parser parser(*in, file.get_full_name());
134 if(file.is_collection())
142 RefPtr<T> item = new T;
143 ItemLoader<T> ldr(*item, *this);
145 return item.release();
150 } // namespace DataFile