csplugincommon/shader/shadercachehelper.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 2008 by Frank Richter 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public 00015 License along with this library; if not, write to the Free 00016 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 */ 00018 00019 #ifndef __CS_CSPLUGINCOMMON_SHADER_SHADERCACHEHELPER_H__ 00020 #define __CS_CSPLUGINCOMMON_SHADER_SHADERCACHEHELPER_H__ 00021 00025 #include "iutil/hiercache.h" 00026 00027 #include "csutil/fifo.h" 00028 #include "csutil/md5.h" 00029 #include "csutil/memfile.h" 00030 #include "csutil/mempool.h" 00031 #include "csutil/ref.h" 00032 #include "csutil/set.h" 00033 00034 struct iDataBuffer; 00035 struct iDocumentNode; 00036 struct iDocumentSystem; 00037 struct iFile; 00038 struct iObjectRegistry; 00039 00043 namespace CS 00044 { 00045 namespace PluginCommon 00046 { 00047 namespace ShaderCacheHelper 00048 { 00054 class CS_CRYSTALSPACE_EXPORT ShaderDocHasher 00055 { 00056 struct DocStackEntry 00057 { 00058 csRef<iDocumentNode> docNode; 00059 csRef<iDataBuffer> sourceData; 00060 csString fullPath; 00061 00062 CS::Utility::Checksum::MD5::Digest ComputeHash (); 00063 }; 00064 csFIFO<DocStackEntry> scanStack; 00065 void PushReferencedFiles (DocStackEntry& entry); 00066 void PushReferencedFiles (iDocumentNode* node); 00067 bool AddFile (const char* filename); 00068 00069 csRef<iDocumentSystem> docSys; 00070 csRef<iVFS> vfs; 00071 csMemFile actualHashes; 00072 csSet<csString> seenFiles; 00073 public: 00078 ShaderDocHasher (iObjectRegistry* objReg, iDocumentNode* doc); 00079 00081 csPtr<iDataBuffer> GetHashStream (); 00083 bool ValidateHashStream (iDataBuffer* stream); 00084 }; 00085 00087 CS_CRYSTALSPACE_EXPORT bool WriteDataBuffer (iFile* file, iDataBuffer* buf); 00089 CS_CRYSTALSPACE_EXPORT csPtr<iDataBuffer> ReadDataBuffer (iFile* file); 00090 00092 CS_CRYSTALSPACE_EXPORT bool WriteString (iFile* file, const char* str); 00094 CS_CRYSTALSPACE_EXPORT csString ReadString (iFile* file); 00095 00097 class CS_CRYSTALSPACE_EXPORT StringStoreWriter 00098 { 00099 csMemFile strings; 00100 csHash<uint32, csString> stringPositions; 00101 csRef<iFile> file; 00102 size_t headPos; 00103 public: 00108 bool StartUse (iFile* file); 00113 bool EndUse (); 00114 00116 uint32 GetID (const char* string); 00117 }; 00118 00120 class CS_CRYSTALSPACE_EXPORT StringStoreReader 00121 { 00122 csRef<iFile> file; 00123 const char* stringBlock; 00124 csRef<iDataBuffer> blockBuf; 00125 size_t endPos; 00126 public: 00132 bool StartUse (iFile* file); 00138 bool EndUse (); 00139 00141 const char* GetString (uint32 id) const; 00142 }; 00143 00151 class CS_CRYSTALSPACE_EXPORT MicroArchive 00152 { 00153 csRef<iDataBuffer> originalData; 00154 csMemoryPool addedNames; 00155 struct Entry 00156 { 00158 const char* name; 00160 csRef<iDataBuffer> data; 00162 size_t size; 00164 size_t offset; 00165 }; 00166 csArray<Entry> entries; 00167 bool dirty; 00168 00169 iDataBuffer* GetEntryData (Entry& entry); 00170 Entry* FindEntry (const char* id); 00171 public: 00172 MicroArchive() : dirty (false) {} 00173 00174 bool Read (iFile* file); 00175 bool Write (iFile* file); 00176 00177 iDataBuffer* ReadEntry (const char* id); 00178 bool WriteEntry (const char* id, iDataBuffer* data); 00179 bool DeleteEntry (const char* id); 00180 void DeleteAllEntries (); 00181 00182 size_t GetEntriesNum () const 00183 { return entries.GetSize(); } 00184 const char* GetEntryName (size_t index) const 00185 { return entries[index].name; } 00186 iDataBuffer* GetEntryData (size_t index) 00187 { return GetEntryData (entries[index]); } 00188 void DeleteEntry (size_t i) { entries.DeleteIndex (i); } 00189 }; 00190 00195 class CS_CRYSTALSPACE_EXPORT MicroArchiveCache : 00196 public scfImplementation1<MicroArchiveCache, iHierarchicalCache> 00197 { 00198 MicroArchive archive; 00199 csRef<iHierarchicalCache> parentCache; 00200 csString cacheItem; 00201 public: 00207 MicroArchiveCache (iHierarchicalCache* parentCache, 00208 const char* cacheItem); 00209 ~MicroArchiveCache(); 00210 00213 virtual bool CacheData (const void* data, size_t size, 00214 const char* path); 00215 virtual csPtr<iDataBuffer> ReadCache (const char* path); 00216 virtual bool ClearCache (const char* path); 00217 virtual void Flush (); 00218 virtual csPtr<iHierarchicalCache> GetRootedCache (const char* base); 00219 virtual csPtr<iStringArray> GetSubItems (const char* path); 00220 virtual iHierarchicalCache* GetTopCache(); 00221 virtual bool IsCacheWriteable() const 00222 { return parentCache->IsCacheWriteable(); } 00224 }; 00225 } // namespace ShaderCacheHelper 00226 } // namespace PluginCommon 00227 } // namespace CS 00228 00231 #endif // __CS_CSPLUGINCOMMON_SHADER_SHADERCACHEHELPER_H__ 00232
Generated for Crystal Space 2.0 by doxygen 1.6.1