My Project
AssetManifest.h
1 #pragma once
2 #include <map>
3 #include "UrlLoaders.h"
4 
5 namespace ParaEngine
6 {
7  class CUrlProcessorUserData;
8 
11  {
12  public:
14  typedef boost::signals2::signal<void(int, AssetFileEntry*)> SyncFile_Callback_t;
15 
25  };
28  AssetFileType_default = 0,
29  AssetFileType_dds_file,
30  AssetFileType_x_file,
31  AssetFileType_ui_texture,
32  AssetFileType_script,
33  AssetFileType_audio,
34  AssetFileType_big_file,
35  AssetFileType_web_file,
36  };
37 
40  string m_url;
41 
42  bool m_bIsZipFile;
49 
53  SyncFile_Callback_t* m_sync_callback;
54 
57  public:
59  ~AssetFileEntry();
60 
62  bool DoesFileExist();
63 
66 
68  inline void SetStatus(AssetFileStatus status){m_nStatus = status;}
69 
72 
76  AssetFileTypeEnum SetFileType(const std::string& file_extension);
77 
80 
84  bool IsDownloading();
85 
92  bool SyncFile();
93 
101  HRESULT SyncFile_Async(URL_LOADER_CALLBACK pFuncCallback=NULL, CUrlProcessorUserData* pUserData=NULL, bool bDeleteUserData=false);
102 
124  HRESULT SyncFile_Async(const SyncFile_Callback_t::slot_type& slot);
125 
127  bool CheckSyncFile();
128 
130  void SignalComplete(int nResult=0);
131 
135 
139  bool AddDownloadCount();
140 
142  const string& GetUrl() {return m_url;};
143 
145  string GetAbsoluteUrl();
146 
148  bool IsUrlFileCompressed();
149 
151  bool CheckMD5AndSize(const char* buffer, int nSize);
152 
154  bool SaveToDisk(const char* buffer, int nSize, bool bCheckMD5=true);
155 
157  int GetFileSize() {return m_nFileSize;}
158 
160  inline const std::string& GetLocalFileName() { return m_localFileName; }
162  std::string GetFullFilePath();
163  };
164 
165 
173  {
174  public:
175  CFileReplaceMap();
176 
177  /* load a replace file mapping. it is just file to file pairs. If the file is requested on the left during GetFile(filename), file on the right is returned instead.
178  * This function is very useful to temperarily change the 3D and 2D theme of the entire game world, in which only a text file needs to be updated.
179  * @param filename: the file map file.
180  * @param bReplaceExistingOnes: whether we will overwrite any previous calls to this function.
181  */
182  CFileReplaceMap(const string& filename, bool bReplaceExistingOnes = true);
183 
184  ~CFileReplaceMap();
185 
186  /* load a replace file mapping. it is just file to file pairs. If the file is requested on the left during GetFile(filename), file on the right is returned instead.
187  * This function is very useful to temperarily change the 3D and 2D theme of the entire game world, in which only a text file needs to be updated.
188  * @param filename: the file map file.
189  * @param bReplaceExistingOnes: whether we will overwrite any previous calls to this function.
190  */
191  void LoadReplaceFile(const string& filename, bool bReplaceExistingOnes = true);
192 
194  static CFileReplaceMap& GetSingleton();
195 
197  bool IsEmpty();
198 
203  bool ReplaceFile(string& inout);
204  private:
205  typedef std::map<string, string> Asset_Replace_Map_Type;
206 
207  /* a replace file mapping. it is just file name to file name pairs. If the file is requested on the left during GetFile(filename), file on the right is returned instead.
208  * This function is very useful to temperarily change the 3D and 2D theme of the entire game world, in which only a text file needs to be updated.
209  */
210  Asset_Replace_Map_Type m_replace_map;
211  };
212 
244  {
245  public:
246  CAssetManifest(void);
247  virtual ~CAssetManifest(void);
248  public:
250  static CAssetManifest& GetSingleton();
251 
252  /* load all "Assets_manifest*.txt" manifest file under the root directory */
253  void LoadManifest();
254 
255  void PrepareCacheFolders();
256 
257  /* load a given manifest file. */
258  void LoadManifestFile(const string& filename);
259 
261  bool DoesFileExist(const char* filename);
262 
264  bool DoesFileExist(const string& filename);
265 
273  int CheckSyncFile(const char* filename);
274 
282  AssetFileEntry* GetFile(const string& filename, bool bUseReplaceMap = true, bool bIgnoreLocalFile = false);
283 
287  void AddEntry(const char* filename);
288 
290  void CleanUp();
291 
293  void PrintStat();
294 
296  inline bool IsEnabled() {return m_bEnableManifest;}
297 
301  void EnableManifest(bool bEnable=true);
302 
304  bool IsUseLocalFileFirst() const;
305  void SetUseLocalFileFirst(bool val);
306 
307  private:
308  bool ParseAssetEntryStr(const string &sFilename, string &fileKey, string &md5, string& filesize);
309  private:
310  typedef std::map<string, AssetFileEntry*> Asset_Manifest_Map_Type;
311 
315  Asset_Manifest_Map_Type m_files;
316 
318  bool m_bEnableManifest;
320  bool m_bUseLocalFileFirst;
321  };
322 }
string GetAbsoluteUrl()
get the absolute url by prepending the default asset domain.
Definition: AssetManifest.cpp:124
void SetStatus(AssetFileStatus status)
set asset file status
Definition: AssetManifest.h:68
const std::string & GetLocalFileName()
get local file name.
Definition: AssetManifest.h:160
light-weighted file record header in memory.
Definition: AssetManifest.h:10
mapping a file to another file a replace file mapping.
Definition: AssetManifest.h:172
bool AddDownloadCount()
add the download count and return !HasReachedMaxRetryCount()
Definition: AssetManifest.cpp:142
boost::signals2::signal< void(int, AssetFileEntry *)> SyncFile_Callback_t
sync file call back function or class.
Definition: AssetManifest.h:14
one should derive from this class with virtual destructor.
Definition: UrlLoaders.h:62
different physics engine has different winding order.
Definition: EventBinding.h:32
AssetFileTypeEnum GetFileType()
the asset file type is usually set according to the file extension.
Definition: AssetManifest.h:71
bool HasReachedMaxRetryCount()
whether we have downloaded the file so many times, but still can not get it right.
Definition: AssetManifest.cpp:137
bool CheckSyncFile()
this is similar to SyncFile, except that it will only sync file if the file is not up to date...
Definition: AssetManifest.cpp:148
bool SaveToDisk(const char *buffer, int nSize, bool bCheckMD5=true)
save a give buffer to local file.
Definition: AssetManifest.cpp:477
bool IsEnabled()
if false, manifest will be temporarily disabled and all GetFile() and DoesFileExist() functions will ...
Definition: AssetManifest.h:296
bool DoesFileExist()
whether an up to date file exist locally.
Definition: AssetManifest.cpp:61
HRESULT SyncFile_Async(URL_LOADER_CALLBACK pFuncCallback=NULL, CUrlProcessorUserData *pUserData=NULL, bool bDeleteUserData=false)
similar to SyncFile(), except that this function will return immediately and does not redownload or c...
Definition: AssetManifest.cpp:305
std::string GetFullFilePath()
get the full disk file path.
Definition: AssetManifest.cpp:467
AssetFileTypeEnum SetFileType(const std::string &file_extension)
set the asset file type according to file extension.
Definition: AssetManifest.cpp:543
bool CheckMD5AndSize(const char *buffer, int nSize)
check whether the MD5 of the input buffer matches.
Definition: AssetManifest.cpp:437
bool IsDownloading()
whether this asset is currently being downloaded.
Definition: AssetManifest.cpp:365
int m_nDownloadCount
how many times we have tried to download this file since start.
Definition: AssetManifest.h:44
bool IsUrlFileCompressed()
return true if url file is compressed.
Definition: AssetManifest.cpp:236
Unable to download the file, after trying a number of times.
Definition: AssetManifest.h:24
Asset manifest manager.
Definition: AssetManifest.h:243
bool SyncFile()
download the file even it is up to date.
Definition: AssetManifest.cpp:160
AssetFileTypeEnum
the asset file type is usually set according to the file extension.
Definition: AssetManifest.h:27
Asset is being downloaded but is not finished yet.
Definition: AssetManifest.h:22
AssetFileStatus
Definition: AssetManifest.h:16
In local computer and ready for use.
Definition: AssetManifest.h:20
int m_nFileSize
the compressed file size
Definition: AssetManifest.h:46
const string & GetUrl()
get relative url
Definition: AssetManifest.h:142
ResourceRequestID GetRequestQueueID()
get the proper request queue.
Definition: AssetManifest.cpp:283
ResourceRequestID
predefined resource request id.
Definition: IDataLoader.h:114
string m_localFileName
this is concatenation of hex md5 with file size
Definition: AssetManifest.h:39
undetermined.
Definition: AssetManifest.h:18
AssetFileStatus GetStatus()
get asset file status
Definition: AssetManifest.h:65
void SignalComplete(int nResult=0)
fire the complete signal, causing all callback to be invoked and then delete all callbacks.
Definition: AssetManifest.cpp:427
AssetFileStatus m_nStatus
asset file status.
Definition: AssetManifest.h:56
SyncFile_Callback_t * m_sync_callback
the sync call back.
Definition: AssetManifest.h:53
AssetFileTypeEnum m_file_type
the asset file type
Definition: AssetManifest.h:48
int GetFileSize()
get the compressed file size of the asset entry.
Definition: AssetManifest.h:157