BVB Source Codes

CRYENGINE Show ReadOnlyChunkFile.cpp Source code

Return Download CRYENGINE: download ReadOnlyChunkFile.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. // -------------------------------------------------------------------------
  4. //  File name:   ReadOnlyChunkFile.h
  5. //  Created:     2004/11/15 by Timur
  6. //
  7. ////////////////////////////////////////////////////////////////////////////
  8.  
  9. #include "StdAfx.h"
  10. #include "ReadOnlyChunkFile.h"
  11. #include "ChunkFileComponents.h"
  12. #include "ChunkFileReaders.h"
  13.  
  14. #define MAX_CHUNKS_NUM 10000000
  15.  
  16. #if !defined(FUNCTION_PROFILER_3DENGINE)
  17.         #define FUNCTION_PROFILER_3DENGINE
  18. #endif
  19.  
  20. //////////////////////////////////////////////////////////////////////////
  21. CReadOnlyChunkFile::CReadOnlyChunkFile(bool bCopyFileData, bool bNoWarningMode)
  22. {
  23.         m_pFileBuffer = 0;
  24.         m_nBufferSize = 0;
  25.  
  26.         m_bNoWarningMode = bNoWarningMode;
  27.  
  28.         m_bOwnFileBuffer = false;
  29.         m_bLoaded = false;
  30.         m_bCopyFileData = bCopyFileData;
  31.  
  32.         m_hFile = 0;
  33. }
  34.  
  35. CReadOnlyChunkFile::~CReadOnlyChunkFile()
  36. {
  37.         CloseFile();
  38.         FreeBuffer();
  39. }
  40.  
  41. //////////////////////////////////////////////////////////////////////////
  42. void CReadOnlyChunkFile::CloseFile()
  43. {
  44.         if (m_hFile)
  45.         {
  46.                 gEnv->pCryPak->FClose(m_hFile);
  47.                 m_hFile = 0;
  48.         }
  49. }
  50.  
  51. //////////////////////////////////////////////////////////////////////////
  52. void CReadOnlyChunkFile::FreeBuffer()
  53. {
  54.         if (m_pFileBuffer && m_bOwnFileBuffer)
  55.         {
  56.                 delete[] m_pFileBuffer;
  57.         }
  58.         m_pFileBuffer = 0;
  59.         m_nBufferSize = 0;
  60.         m_bOwnFileBuffer = false;
  61.         m_bLoaded = false;
  62. }
  63.  
  64. //////////////////////////////////////////////////////////////////////////
  65. CReadOnlyChunkFile::ChunkDesc* CReadOnlyChunkFile::GetChunk(int nIndex)
  66. {
  67.         assert(size_t(nIndex) < m_chunks.size());
  68.         return &m_chunks[nIndex];
  69. }
  70.  
  71. //////////////////////////////////////////////////////////////////////////
  72. const CReadOnlyChunkFile::ChunkDesc* CReadOnlyChunkFile::GetChunk(int nIndex) const
  73. {
  74.         assert(size_t(nIndex) < m_chunks.size());
  75.         return &m_chunks[nIndex];
  76. }
  77.  
  78. // number of chunks
  79. int CReadOnlyChunkFile::NumChunks() const
  80. {
  81.         return (int)m_chunks.size();
  82. }
  83.  
  84. //////////////////////////////////////////////////////////////////////////
  85. CReadOnlyChunkFile::ChunkDesc* CReadOnlyChunkFile::FindChunkByType(ChunkTypes nChunkType)
  86. {
  87.         for (size_t i = 0, count = m_chunks.size(); i < count; ++i)
  88.         {
  89.                 if (m_chunks[i].chunkType == nChunkType)
  90.                 {
  91.                         return &m_chunks[i];
  92.                 }
  93.         }
  94.         return 0;
  95. }
  96.  
  97. //////////////////////////////////////////////////////////////////////////
  98. CReadOnlyChunkFile::ChunkDesc* CReadOnlyChunkFile::FindChunkById(int id)
  99. {
  100.         ChunkDesc chunkToFind;
  101.         chunkToFind.chunkId = id;
  102.  
  103.         std::vector<ChunkDesc>::iterator it = std::lower_bound(m_chunks.begin(), m_chunks.end(), chunkToFind, IChunkFile::ChunkDesc::LessId);
  104.         if (it != m_chunks.end() && id == (*it).chunkId)
  105.         {
  106.                 return &(*it);
  107.         }
  108.         return 0;
  109. }
  110.  
  111. //////////////////////////////////////////////////////////////////////////
  112. bool CReadOnlyChunkFile::ReadChunkTableFromBuffer()
  113. {
  114.         FUNCTION_PROFILER_3DENGINE;
  115.  
  116.         if (m_pFileBuffer == 0)
  117.         {
  118.                 m_LastError.Format("Unexpected empty buffer");
  119.                 return false;
  120.         }
  121.  
  122.         {
  123.                 ChunkFile::MemoryReader f;
  124.  
  125.                 if (!f.Start(m_pFileBuffer, m_nBufferSize))
  126.                 {
  127.                         m_LastError.Format("Empty memory chunk file");
  128.                         return false;
  129.                 }
  130.  
  131.                 bool bStripHeaders = false;
  132.                 const char* err = 0;
  133.  
  134.                 err = ChunkFile::GetChunkTableEntries_0x746(&f, m_chunks);
  135.                 if (err)
  136.                 {
  137.                         err = ChunkFile::GetChunkTableEntries_0x744_0x745(&f, m_chunks);
  138.                         bStripHeaders = true;
  139.                 }
  140.  
  141.                 if (!err)
  142.                 {
  143.                         for (size_t i = 0; i < m_chunks.size(); ++i)
  144.                         {
  145.                                 ChunkDesc& cd = m_chunks[i];
  146.                                 cd.data = m_pFileBuffer + cd.fileOffset;
  147.                         }
  148.                         if (bStripHeaders)
  149.                         {
  150.                                 err = ChunkFile::StripChunkHeaders_0x744_0x745(&f, m_chunks);
  151.                         }
  152.                 }
  153.  
  154.                 if (err)
  155.                 {
  156.                         m_LastError = err;
  157.                         return false;
  158.                 }
  159.         }
  160.  
  161.         // Sort chunks by Id, for faster queries later (see FindChunkById()).
  162.         std::sort(m_chunks.begin(), m_chunks.end(), IChunkFile::ChunkDesc::LessId);
  163.  
  164.         return true;
  165. }
  166.  
  167. //////////////////////////////////////////////////////////////////////////
  168. bool CReadOnlyChunkFile::Read(const char* filename)
  169. {
  170.         FUNCTION_PROFILER_3DENGINE;
  171.  
  172.         CloseFile();
  173.         FreeBuffer();
  174.  
  175.         m_hFile = gEnv->pCryPak->FOpen(filename, "rb", (m_bNoWarningMode ? ICryPak::FOPEN_HINT_QUIET : 0));
  176.         if (!m_hFile)
  177.         {
  178.                 m_LastError.Format("Failed to open file '%s'", filename);
  179.                 return false;
  180.         }
  181.  
  182.         size_t nFileSize = 0;
  183.  
  184.         if (m_bCopyFileData)
  185.         {
  186.                 nFileSize = gEnv->pCryPak->FGetSize(m_hFile);
  187.                 m_pFileBuffer = new char[nFileSize];
  188.                 m_bOwnFileBuffer = true;
  189.                 if (gEnv->pCryPak->FReadRawAll(m_pFileBuffer, nFileSize, m_hFile) != nFileSize)
  190.                 {
  191.                         m_LastError.Format("Failed to read %u bytes from file '%s'", (uint)nFileSize, filename);
  192.                         return false;
  193.                 }
  194.         }
  195.         else
  196.         {
  197.                 m_pFileBuffer = (char*)gEnv->pCryPak->FGetCachedFileData(m_hFile, nFileSize);
  198.                 m_bOwnFileBuffer = false;
  199.         }
  200.  
  201.         if (!m_pFileBuffer)
  202.         {
  203.                 m_LastError.Format("Failed to get memory for file '%s'", filename);
  204.                 return false;
  205.         }
  206.  
  207.         m_nBufferSize = nFileSize;
  208.  
  209.         if (!ReadChunkTableFromBuffer())
  210.         {
  211.                 return false;
  212.         }
  213.  
  214.         m_bLoaded = true;
  215.  
  216.         return true;
  217. }
  218.  
  219. //////////////////////////////////////////////////////////////////////////
  220. bool CReadOnlyChunkFile::ReadFromMemory(const void* pData, int nDataSize)
  221. {
  222.         FUNCTION_PROFILER_3DENGINE;
  223.  
  224.         CloseFile();
  225.         FreeBuffer();
  226.  
  227.         m_pFileBuffer = (char*)pData;
  228.         m_bOwnFileBuffer = false;
  229.         m_nBufferSize = nDataSize;
  230.  
  231.         if (!ReadChunkTableFromBuffer())
  232.         {
  233.                 return false;
  234.         }
  235.         m_bLoaded = true;
  236.         return true;
  237. }
  238.  
downloadReadOnlyChunkFile.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top