ResCache.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #pragma once
  2. //========================================================================
  3. // ResCache.h : Defines a simple resource cache.
  4. //
  5. // Part of the GameCode4 Application
  6. //
  7. // GameCode4 is the sample application that encapsulates much of the source code
  8. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  9. // "Rez" Graham, published by Charles River Media.
  10. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  11. //
  12. // If this source code has found it's way to you, and you think it has helped you
  13. // in any way, do the authors a favor and buy a new copy of the book - there are
  14. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  15. // by clicking here:
  16. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  17. //
  18. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  19. //
  20. // The source code is managed and maintained through Google Code:
  21. // http://code.google.com/p/gamecode4/
  22. //
  23. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  24. //
  25. // This program is free software; you can redistribute it and/or
  26. // modify it under the terms of the GNU Lesser GPL v3
  27. // as published by the Free Software Foundation.
  28. //
  29. // This program is distributed in the hope that it will be useful,
  30. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  32. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  33. //
  34. // You should have received a copy of the GNU Lesser GPL v3
  35. // along with this program; if not, write to the Free Software
  36. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  37. //
  38. //========================================================================
  39. class ResHandle;
  40. class ResCache;
  41. #include "ZipFile.h" // needed for ZipContentsMap
  42. #include "../GameCode4/interfaces.h"
  43. #include "../Debugging/Logger.h"
  44. #include <memory>
  45. #include <filesystem>
  46. #include <vector>
  47. //
  48. // class IResourceExtraData - Chapter 8, page 224 (see notes below)
  49. //
  50. // This isn't mentioned specifically on that page, but it is a class that can attach extra data to
  51. // a particular resource. Best example is storing the length and format of a sound file.
  52. // There's a great discussion of this in Chapter 13, "Game Audio"
  53. //
  54. class IResourceExtraData
  55. {
  56. public:
  57. virtual std::string VToString()=0;
  58. };
  59. //
  60. // class Resource - Chapter 8, page 220
  61. //
  62. class Resource
  63. {
  64. public:
  65. std::string m_name;
  66. Resource(const std::string &name);
  67. };
  68. //
  69. // class ResourceZipFile - not discussed in the book
  70. //
  71. // This class implements the IResourceFile interface with a ZipFile.
  72. class ResourceZipFile : public IResourceFile
  73. {
  74. ZipFile *m_pZipFile;
  75. std::wstring m_resFileName;
  76. public:
  77. ResourceZipFile(const std::wstring resFileName) { m_pZipFile = NULL; m_resFileName=resFileName; }
  78. virtual ~ResourceZipFile();
  79. virtual bool VOpen();
  80. virtual int VGetRawResourceSize(const Resource &r);
  81. virtual int VGetRawResource(const Resource &r, char *buffer);
  82. virtual int VGetNumResources() const;
  83. virtual std::string VGetResourceName(int num) const;
  84. virtual bool VIsUsingDevelopmentDirectories(void) const { return false; }
  85. };
  86. //
  87. // class DevelopmentResourceZipFile - not discussed in the book
  88. //
  89. // This class fakes a ZIP file from a normal directory, and is used in the
  90. // editor.
  91. //
  92. class DevelopmentResourceZipFile : public ResourceZipFile
  93. {
  94. public:
  95. enum Mode
  96. {
  97. Development, // this mode checks the original asset directory for changes - helps during development
  98. Editor // this mode only checks the original asset directory - the ZIP file is left unopened.
  99. };
  100. Mode m_mode;
  101. std::filesystem::path m_AssetsDir;
  102. std::vector<std::filesystem::directory_entry> m_AssetFileInfo;
  103. ZipContentsMap m_DirectoryContentsMap;
  104. DevelopmentResourceZipFile(const std::wstring resFileName, const Mode mode);
  105. virtual bool VOpen();
  106. virtual int VGetRawResourceSize(const Resource &r);
  107. virtual int VGetRawResource(const Resource &r, char *buffer);
  108. virtual int VGetNumResources() const;
  109. virtual std::string VGetResourceName(int num) const;
  110. virtual bool VIsUsingDevelopmentDirectories(void) const { return true; }
  111. int Find(const std::string &path);
  112. protected:
  113. bool ReadAssetsDirectory(std::filesystem::path dir);
  114. };
  115. //
  116. // class ResHandle - Chapter 8, page 222
  117. //
  118. class ResHandle
  119. {
  120. friend class ResCache;
  121. protected:
  122. Resource m_resource;
  123. char *m_buffer;
  124. unsigned int m_size;
  125. std::shared_ptr<IResourceExtraData> m_extra;
  126. ResCache *m_pResCache;
  127. public:
  128. ResHandle(Resource & resource, char *buffer, unsigned int size, ResCache *pResCache);
  129. virtual ~ResHandle();
  130. const std::string GetName() { return m_resource.m_name; }
  131. unsigned int Size() const { return m_size; }
  132. char *Buffer() const { return m_buffer; }
  133. char *WritableBuffer() { return m_buffer; }
  134. std::shared_ptr<IResourceExtraData> GetExtra() { return m_extra; }
  135. void SetExtra(std::shared_ptr<IResourceExtraData> extra) { m_extra = extra; }
  136. };
  137. //
  138. // class DefaultResourceLoader - Chapter 8, page 225
  139. //
  140. class DefaultResourceLoader : public IResourceLoader
  141. {
  142. public:
  143. virtual bool VUseRawFile() { return true; }
  144. virtual bool VDiscardRawBufferAfterLoad() { return true; }
  145. virtual bool VAddNullZero() { return true; }
  146. virtual unsigned int VGetLoadedResourceSize(char *rawBuffer, unsigned int rawSize) { return rawSize; }
  147. virtual bool VLoadResource(char *rawBuffer, unsigned int rawSize, std::shared_ptr<ResHandle> handle) { return true; }
  148. virtual std::string VGetPattern() { return "*"; }
  149. };
  150. //
  151. // class ResCache - Chapter 8, page 225
  152. //
  153. typedef std::list< std::shared_ptr<ResHandle> > ResHandleList; // lru list
  154. typedef std::map< std::string, std::shared_ptr<ResHandle> > ResHandleMap; // maps indentifiers to resource data
  155. typedef std::list< std::shared_ptr<IResourceLoader> > ResourceLoaders;
  156. class ResCache
  157. {
  158. friend class ResHandle;
  159. ResHandleList m_lru; // lru list
  160. ResHandleMap m_resources;
  161. ResourceLoaders m_resourceLoaders;
  162. IResourceFile *m_file;
  163. unsigned int m_cacheSize; // total memory size
  164. unsigned int m_allocated; // total memory allocated
  165. protected:
  166. bool MakeRoom(unsigned int size);
  167. char *Allocate(unsigned int size);
  168. void Free(std::shared_ptr<ResHandle> gonner);
  169. std::shared_ptr<ResHandle> Load(Resource *r);
  170. std::shared_ptr<ResHandle> Find(Resource * r);
  171. void Update(std::shared_ptr<ResHandle> handle);
  172. void FreeOneResource();
  173. void MemoryHasBeenFreed(unsigned int size);
  174. public:
  175. ResCache(const unsigned int sizeInMb, IResourceFile *file);
  176. virtual ~ResCache();
  177. bool Init();
  178. void RegisterLoader(std::shared_ptr<IResourceLoader> loader);
  179. std::shared_ptr<ResHandle> GetHandle(Resource * r);
  180. int Preload(const std::string pattern, void (*progressCallback)(int, bool &));
  181. std::vector<std::string> Match(const std::string pattern);
  182. bool IsUsingDevelopmentDirectories(void) const { GCC_ASSERT(m_file); return m_file->VIsUsingDevelopmentDirectories(); }
  183. };