BVB Source Codes

CRYENGINE Show XMLCPB_StringTableWriter.cpp Source code

Return Download CRYENGINE: download XMLCPB_StringTableWriter.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. /*************************************************************************
  4. *************************************************************************/
  5.  
  6. #include "StdAfx.h"
  7. #include "XMLCPB_StringTableWriter.h"
  8. #include "XMLCPB_Writer.h"
  9. #include "CryActionCVars.h"
  10.  
  11. using namespace XMLCPB;
  12.  
  13. const StringID CStringTableWriter::SEARCHING_ID = 0Xfffffffe;
  14.  
  15. //////////////////////////////////////////////////////////////////////////
  16.  
  17. CStringTableWriter::CStringTableWriter(int maxNumStrings, int bufferSize)
  18.         : m_buffer(bufferSize)
  19.         , m_sortedIndexes(256, StringHasher(GetThis()), StringHasher(GetThis()))
  20.         , m_pWriter(nullptr)
  21.         , m_maxNumStrings(maxNumStrings)
  22.         , m_pCheckString(nullptr)
  23. {
  24.         m_checkAddr.hash = 0;
  25. }
  26.  
  27. CStringTableWriter::~CStringTableWriter()
  28. {
  29. #ifdef _DEBUG
  30.         if (CCryActionCVars::Get().g_saveLoadExtendedLog != 0)
  31.         {
  32.                 // Check hash function for collisions and examine hash_map bucket sizes
  33.                 size_t size = m_sortedIndexes.size();
  34.                 size_t bucket_count = m_sortedIndexes.bucket_count();
  35.                 size_t max_bucket_count = m_sortedIndexes.max_bucket_count();
  36.                 CryLog("CStringTableWriter size: %" PRISIZE_T ", bucket_count: %" PRISIZE_T ", max_bucket_count: %" PRISIZE_T, size, bucket_count, max_bucket_count);
  37.                 float load_factor = m_sortedIndexes.load_factor();
  38.                 CryLog("load_factor %f", load_factor);
  39.         }
  40. #endif
  41. }
  42.  
  43. void CStringTableWriter::Init(CWriter* pWriter)
  44. {
  45.         m_pWriter = pWriter;
  46.         m_buffer.Init(pWriter, false);    // we never want a stringTableWriter buffer to be streamed, because we need it along the whole process.
  47. }
  48.  
  49. void CStringTableWriter::CreateStringsFromConstants()
  50. {
  51.         for (int i = 0; i < DT_NUM_CONST_STR; i++)
  52.         {
  53.                 const char* pString = GetConstantString(i);
  54.                 StringID id = AddString(pString, StringHasher::HashString(pString));
  55.                 assert(id == i);
  56.         }
  57. }
  58.  
  59. //////////////////////////////////////////////////////////////////////////
  60. // AddString could be used directly instead, with some changes. But then it would be very inefficient because it would need to copy every to-compare string first into the buffer.
  61. StringID CStringTableWriter::GetStringID(const char* pString, bool addIfCantFind)
  62. {
  63.         m_pCheckString = pString;  // the internal std::set comparison code will use this string when trying to find SEARCHING_ID
  64.         m_checkAddr.hash = StringHasher::HashString(pString);
  65.         StringHasherType::const_iterator iter = m_sortedIndexes.find(SEARCHING_ID);
  66.         m_pCheckString = NULL;
  67.  
  68.         // found the string
  69.         if (iter != m_sortedIndexes.end())
  70.                 //return *iter;
  71.                 return *iter;
  72.  
  73.         // didn't find
  74.         if (addIfCantFind)
  75.         {
  76.                 StringID ID = AddString(pString, m_checkAddr.hash);
  77.                 return ID;
  78.         }
  79.         else
  80.                 return XMLCPB_INVALID_ID;
  81. }
  82.  
  83. //////////////////////////////////////////////////////////////////////////
  84.  
  85. StringID CStringTableWriter::AddString(const char* pString, size_t hash)
  86. {
  87.         assert(m_stringAddrs.size() < m_maxNumStrings);
  88.         assert(!HasStringID(pString));
  89.  
  90.         int size = strlen(pString) + 1;
  91.  
  92.         StringID addStringId = m_stringAddrs.size();
  93.         m_stringAddrs.push_back(SAddr());
  94.         SAddr& stringAddr = m_stringAddrs.back();
  95.         m_buffer.AddDataNoSplit(stringAddr, pString, size);    // no split = can not be internally split in different internal buffers. Because we want to be able to read it in one go with the translated pointer
  96.  
  97.         stringAddr.hash = hash;
  98.  
  99.         m_sortedIndexes.insert(addStringId);
  100.  
  101.         return addStringId;
  102. }
  103.  
  104. //////////////////////////////////////////////////////////////////////////
  105. // debug and statistics purposes
  106. void CStringTableWriter::LogStrings()
  107. {
  108.         /*      StringHasherType::const_iterator iter = m_sortedIndexes.begin();
  109.  
  110.            while (iter!=m_sortedIndexes.end())
  111.            {
  112.             const StringID& stringId = *iter;
  113.             string str;
  114.             str.Format( "---string: %s  offset: %d\n", GetStringRealPointer(stringId), m_buffer.ConvertSegmentedAddrToFlatAddr( m_stringAddrs[i] ) );
  115.             gEnv->pCryPak->FWrite( str.c_str(), str.size()+1. pFile )
  116.            ++iter;
  117.            }
  118.          */
  119.         /*      for (uint32 i=0; i<GetNumStrings(); i++)
  120.            {
  121.             string str;
  122.            //           str.Format( "%s %d\n", GetStringRealPointer(i), m_buffer.ConvertSegmentedAddrToFlatAddr( m_stringAddrs[i] ) );
  123.             str.Format( "%d\n", m_buffer.ConvertSegmentedAddrToFlatAddr( m_stringAddrs[i] ) );
  124.             gEnv->pCryPak->FWrite( str.c_str(), str.size(), pFile );
  125.            } */
  126. }
  127.  
  128. //////////////////////////////////////////////////////////////////////////
  129.  
  130. void CStringTableWriter::FillFileHeaderInfo(SFileHeader::SStringTable& info)
  131. {
  132.         info.m_numStrings = m_stringAddrs.size();
  133.         info.m_sizeStringData = m_buffer.GetUsedMemory();
  134. }
  135.  
  136. //////////////////////////////////////////////////////////////////////////
  137.  
  138. void CStringTableWriter::CalculateFlatAddrs(FlatAddrVec& outFlatAddrs)
  139. {
  140.         outFlatAddrs.resize(m_stringAddrs.size());
  141.  
  142.         for (int i = 0; i < outFlatAddrs.size(); ++i)
  143.                 outFlatAddrs[i] = m_buffer.ConvertSegmentedAddrToFlatAddr(m_stringAddrs[i]);
  144. }
  145.  
  146. //////////////////////////////////////////////////////////////////////////
  147.  
  148. void CStringTableWriter::WriteToFile()
  149. {
  150.         if (m_stringAddrs.empty())
  151.                 return;
  152.  
  153.         // TODO: could reuse the same memory space than the segmented addrs. the table is not going to be used after it anyway. that could be about 32k less in writing
  154.         FlatAddrVec flatAddrs;
  155.         CalculateFlatAddrs(flatAddrs);
  156.         for (size_t i = 0, size = flatAddrs.size(); i < size; ++i)
  157.                 SwapIntegerValue(flatAddrs[i]);
  158.  
  159.         assert(!flatAddrs.empty());
  160.  
  161.         FlatAddr* p = &(flatAddrs[0]);
  162.  
  163.         m_pWriter->WriteDataIntoFile(p, sizeof(FlatAddr) * flatAddrs.size());
  164.         m_buffer.WriteToFile();
  165. }
  166.  
  167. //////////////////////////////////////////////////////////////////////////
  168.  
  169. void CStringTableWriter::WriteToMemory(uint8*& rpData, uint32& outWriteLoc)
  170. {
  171.         if (m_stringAddrs.empty())
  172.                 return;
  173.  
  174.         // TODO: could reuse the same memory space than the segmented addrs. the table is not going to be used after it anyway. that could be about 32k less in writing
  175.         FlatAddrVec flatAddrs;
  176.         CalculateFlatAddrs(flatAddrs);
  177.  
  178.         assert(!flatAddrs.empty());
  179.  
  180.         FlatAddr* p = &(flatAddrs[0]);
  181.  
  182.         m_pWriter->WriteDataIntoMemory(rpData, p, sizeof(FlatAddr) * flatAddrs.size(), outWriteLoc);
  183.         m_buffer.WriteToMemory(rpData, outWriteLoc);
  184. }
  185.  
  186. //////////////////////////////////////////////////////////////////////////
  187.  
  188. uint32 CStringTableWriter::GetDataSize()
  189. {
  190.         if (m_stringAddrs.empty())
  191.                 return 0;
  192.  
  193.         uint32 uSize = sizeof(FlatAddr) * GetNumStrings();
  194.         uSize += m_buffer.GetDataSize();
  195.  
  196.         return uSize;
  197. }
  198.  
  199. //////////////////////////////////////////////////////////////////////////
  200. #ifdef XMLCPB_CHECK_HARDCODED_LIMITS
  201. const char* CStringTableWriter::GetStringSafe(StringID stringId) const
  202. {
  203.         if (stringId < m_stringAddrs.size())
  204.                 return GetString(stringId);
  205.         else
  206.                 return "<Invalid String>";
  207. }
  208. #endif
  209.  
downloadXMLCPB_StringTableWriter.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