1 // Copyright 2014-2017 the openage authors. See copying.md for legal info.
2 
3 #pragma once
4 
5 #include "config.h"
6 
7 #include <unordered_map>
8 #include <string>
9 #include <memory>
10 
11 #include "util/path.h"
12 
13 
14 namespace qtsdl {
15 class GuiItemLink;
16 } // namespace qtsdl
17 
18 namespace openage {
19 
20 class Engine;
21 class Texture;
22 
23 /**
24  * Container class for all available assets.
25  * Responsible for loading, providing and updating requested files.
26  */
27 class AssetManager final {
28 public:
29 	explicit AssetManager(qtsdl::GuiItemLink *gui_link);
30 
31 	/**
32 	 * Return the path where assets are found in.
33 	 */
34 	const util::Path &get_asset_dir();
35 
36 	/**
37 	 * Set the asset search path.
38 	 */
39 	void set_asset_dir(const util::Path& asset_dir);
40 
41 	/**
42 	 * Set the game engine of this asset manager.
43 	 * Called from QML.
44 	 */
45 	void set_engine(Engine *engine);
46 
47 	/**
48 	 * Return the engine responsible for this asset manager.
49 	 */
50 	Engine *get_engine() const;
51 
52 	/**
53 	 * Query the Texture for a given filename.
54 	 *
55 	 * @param name: the asset file name relative to the asset root.
56 	 * @param use_metafile: load subtexture information from meta file
57 	 * @param null_if_missing: instead of providing the "missing texture",
58 	 *                         return nullptr.
59 	 * @returns the queried texture handle.
60 	 */
61 	Texture *get_texture(const std::string &name, bool use_metafile=true,
62 	                     bool null_if_missing=false);
63 
64 	/**
65 	 * Ask the kernel whether there were updates to watched files.
66 	 */
67 	void check_updates();
68 
69 protected:
70 	/**
71 	 * Create an internal texture handle.
72 	 */
73 	std::shared_ptr<Texture> load_texture(const std::string &name,
74 	                                      bool use_metafile=true,
75 	                                      bool null_if_missing=false);
76 
77 	/**
78 	 * Retrieves the texture for missing textures.
79 	 */
80 	std::shared_ptr<Texture> get_missing_tex();
81 
82 private:
83 	void clear();
84 
85 	/**
86 	 * The engine this asset manager is attached to.
87 	 */
88 	Engine *engine;
89 
90 	/**
91 	 * The root directory for the available assets.
92 	 */
93 	util::Path asset_path;
94 
95 	/**
96 	 * The replacement texture for missing textures.
97 	 */
98 	std::shared_ptr<Texture> missing_tex;
99 
100 	/**
101 	 * Map from texture filename to texture instance ptr.
102 	 */
103 	std::unordered_map<std::string, std::shared_ptr<Texture>> textures;
104 
105 #if WITH_INOTIFY
106 	/**
107 	 * The file descriptor pointing to the inotify instance.
108 	 */
109 	int inotify_fd;
110 
111 	/**
112 	 * Map from inotify watch handle fd to texture instance ptr.
113 	 * The kernel returns the handle fd when events are triggered.
114 	 */
115 	std::unordered_map<int, std::shared_ptr<Texture>> watch_fds;
116 #endif
117 
118 public:
119 	qtsdl::GuiItemLink *gui_link;
120 };
121 
122 }
123