Assets.h
1 /*
2 
3 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
4 All rights reserved.
5 
6 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
7 Dynacoe was released under the MIT License, as detailed below.
8 
9 
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is furnished
16 to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall
19 be included in all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29 
30 
31 */
32 
33 #ifndef H_DASSETS_INCLUDED
34 #define H_DASSETS_INCLUDED
35 
36 
37 
38 
39 #include <Dynacoe/AssetID.h>
40 #include <Dynacoe/Modules/Module.h>
41 #include <Dynacoe/Modules/Console.h>
42 
43 #include <vector>
44 #include <string>
45 #include <cctype>
46 #include <stack>
47 #include <unordered_map>
48 #include <map>
49 
50 
51 
52 
53 
54 
55 namespace Dynacoe {
56 class Image;
57 class Graphics;
58 class Particle;
59 class AudioBlock;
60 class Sequence;
61 class Particle;
62 class FontAsset;
63 class Decoder;
64 class Encoder;
65 
66 
67 
68 
69 class Asset {
70  public:
71  Asset(const std::string & n) : name{n}{}
72  std::string GetAssetName() const {return name;}
73  virtual ~Asset() = 0;
74  private:
75  std::string name;
76 
77 };
78 
79 
96 
97 class Assets : public Module {
98  public:
99 
102  enum class Type {
103  Placeholder,
104  Image,
105  Font,
106  Audio,
107  Sequence,
108  Particle,
109  RawData,
110  Model,
111  NoType
112  };
113 
114 
115 
116 
131  static AssetID Load(const std::string & fileType, const std::string & file, bool nameOnly=true);
132 
133 
141  static AssetID LoadFromBuffer(const std::string & filetype, const std::string & name, const std::vector<uint8_t> & data);
142 
143 
148  static std::vector<std::string> SupportedLoadExtensions(Assets::Type type);
149 
155  static AssetID Query(Assets::Type type, const std::string & name);
156 
162  static AssetID New(Assets::Type type, const std::string & name = "");
163 
167  template<typename T>
168  static T & Get(AssetID id);
169 
174  static bool Remove(AssetID id);
175 
185  static bool Write(AssetID id, const std::string & encoderExtension, const std::string & outputName);
186 
191  static std::vector<std::string> SupportedWriteExtensions(Assets::Type type);
192 
196  static std::string Name(AssetID type);
197 
198 
199 
200 
201 
202 
203  private:
204  friend class Graphics;
205 
206  static void notFoundError(Assets::Type, const std::string & name);
207  static std::string typeToString(Assets::Type);
208 
209  static std::map<std::string, Decoder *> decoders;
210  static std::vector<Asset *> errorInstances;
211  static std::vector<std::map<std::string, Encoder *>> encoders;
212  static AssetID storeGen(const std::string &, Asset *, Assets::Type);
213  static void LoadDecoders();
214  static void LoadDecoder(Decoder *);
215  static Decoder * GetDecoder(const std::string &);
216  static void LoadEncoders();
217  static void LoadEncoder(Encoder *);
218  static bool Encode(int, Asset *, const std::string & ext, const std::string &);
219 
220  // all storage buckets
221  static std::vector<Asset *> assetList[AssetID::NUMTYPES];
222  // zombie buckets of indices
223  static std::stack<int> deadList[AssetID::NUMTYPES];
224  static std::unordered_map<std::string, AssetID> assetMap[AssetID::NUMTYPES];
225  static void storeSystemImages();
226 
227 
228  //Attempts to store the image(s) specified by the 32-bit pixel buffer given.
229  //
230  // Hidden denotes whether
231  // or not the compiled Image is stored in the standard image list or
232  // the private System one.
233  static AssetID storePixels(std::vector<uint32_t*> &, std::string ID, int w, int h, bool hidden);
234 
235  static Asset * CreateAsset(Type type, const std::string & str);
236 
237  // Dynacoe's Asset Registration Process
238 
239  // The string representing the data is
240  // hashed and mapped to an index. The index
241  // refers to where it is placed in the vector.
242  //
243  // Registration:
244  // - path is hashed to check is already existed (O(path.length()))
245  // - if it does AND != -1,
246  // then done. done
247  // - else,
248  // get the next immediate free index.
249  // - if !deadList.empty() (O(1))
250  // - then deadList.pop() (O(1))
251  // - use this index as the free index
252  // - chunkList[free index] = AudioBlock (O(1))
253  // - else
254  // - chunkList.push_back(Asset) (average = O(1), worst = O(n))
255  // - use size() - 1 as the free index
256  // - hash(path, free index) (O(path.length()))
257  // - return index
258  // Retrieval (index)
259  // - if index < -1, retrun NULL
260  // - return chunkList[index] (O(1))
261  // Retrieval (string)
262  // - if (hash(path) == DNE || -1) return NULL; (O(path.length))
263  // - else return index
264  // Removal
265  // - hash(string, -1); (O(path.length())
266  // - deadList.push(old index); (O(1))
267  // - chunkList[old index] == NULL; (O(1))
268  // - * free data * (worst = O(Asset.size())???)
269 
270 
271 
272  // Private Members
273 
274 
275 
276 
277 
278 
279 
280 
281  static size_t assetCounter;
282 
283 
284 
285 
286 
287 
288 
289 
290  // Private Methods
291 
292 
293  static void initBase();
294  /* Image storage */
295 
296  static std::string fSearch(const std::string &);
297 
298 
299 
300 
301 
302 
303 
304 
305 
306  public:
307  std::string GetName() { return "Asset Manager"; }
308  void Init(); void InitAfter(); void RunBefore(); void RunAfter(); void DrawBefore(); void DrawAfter();
309  Backend * GetBackend();
310 };
311 
312 
313 
314 template<typename T>
316  if (!id.Valid()) {
317  Console::Error() << ("Error getting from cache: Non-existent ID\n");
318  return *((T*)errorInstances[id.type]);
319  }
320  Asset * i;
321  i = dynamic_cast<T*>(assetList[id.type][id.handle]);
322 
323  if (i) return *((T*)i);
324  if (!assetList[id.type][id.handle])
325  Console::Error() <<("Error getting from cache: Used deleted ID\n");
326  else
327  Console::Error() <<("Asset exists, but is a different type than the template given.\n");
328  return *((T*)errorInstances[id.type]);
329 }
330 
331 }
332 
333 
334 
335 #endif
Type
A classification of individual Assets.
Definition: Assets.h:102
static bool Remove(AssetID id)
Removes the specified Asset.
static std::string Name(AssetID type)
Returns the name associated with the Asset.
Definition: AssetID.h:37
static std::vector< std::string > SupportedWriteExtensions(Assets::Type type)
Returns a vector of strings containing the currently supported types that can be written to...
static AssetID LoadFromBuffer(const std::string &filetype, const std::string &name, const std::vector< uint8_t > &data)
Loads the specified asset from memory into, err, memory.
Assets are referred to by an AssetID. The AssetID uniquely refers to an Asset stored within memory...
Definition: AssetID.h:42
static bool Write(AssetID id, const std::string &encoderExtension, const std::string &outputName)
Attempts to dump the asset to a file.
static AssetID New(Assets::Type type, const std::string &name="")
Generates a new instance of the specified Asset type and returns its ID.
The central class for rendering.
Definition: Graphics.h:86
static AssetID Query(Assets::Type type, const std::string &name)
Returns the AssetID associated with the name and type.
Collection of meshes, animations, and/or materials.
static std::vector< std::string > SupportedLoadExtensions(Assets::Type type)
Returns a list of supported file types that Dynacoe can load.
static T & Get(AssetID id)
Returns the Asset reference associated with the given ID.
Definition: Assets.h:315
Handles loading media.
Definition: Assets.h:97
static AssetID Load(const std::string &fileType, const std::string &file, bool nameOnly=true)
Loads the specifed asset into memory.