Advertisement
expired6978

TRI Load

Mar 10th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.39 KB | None | 0 0
  1.  
  2. bool MorphCache::CacheFile(const char * relativePath)
  3. {
  4.     BSFixedString filePath(relativePath);
  5.     if(relativePath == "")
  6.         return false;
  7.  
  8.     FileMap::iterator it = m_data.find(filePath);
  9.     if (it != m_data.end()) {
  10.         it->second.accessed = std::time(nullptr);
  11.         return false;
  12.     }
  13.  
  14. #ifdef _DEBUG
  15.     _MESSAGE("%s - Parsing: %s", __FUNCTION__, filePath.data);
  16. #endif
  17.  
  18.     BSResourceNiBinaryStream binaryStream(filePath.data);
  19.     if(binaryStream.IsValid())
  20.     {
  21.         TriShapeMap trishapeMap;
  22.  
  23.         UInt32 fileFormat = 0;
  24.         trishapeMap.memoryUsage += binaryStream.Read((char *)&fileFormat, sizeof(UInt32));
  25.  
  26.         bool packed = false;
  27.         if (fileFormat != 'TRI\0' && fileFormat != 'TRIP')
  28.             return false;
  29.  
  30.         if (fileFormat == 'TRIP')
  31.             packed = true;
  32.  
  33.        
  34.  
  35.         UInt32 trishapeCount = 0;
  36.         if (!packed)
  37.             trishapeMap.memoryUsage += binaryStream.Read((char *)&trishapeCount, sizeof(UInt32));
  38.         else
  39.             trishapeMap.memoryUsage += binaryStream.Read((char *)&trishapeCount, sizeof(UInt16));
  40.  
  41.         char trishapeNameRaw[MAX_PATH];
  42.         for (UInt32 i = 0; i < trishapeCount; i++)
  43.         {
  44.             memset(trishapeNameRaw, 0, MAX_PATH);
  45.  
  46.             UInt8 size = 0;
  47.             trishapeMap.memoryUsage += binaryStream.Read((char *)&size, sizeof(UInt8));
  48.             trishapeMap.memoryUsage += binaryStream.Read(trishapeNameRaw, size);
  49.             BSFixedString trishapeName(trishapeNameRaw);
  50.  
  51. #ifdef _DEBUG
  52.             _MESSAGE("%s - Reading TriShape %s", __FUNCTION__, trishapeName.data);
  53. #endif
  54.  
  55.             if (!packed) {
  56.                 UInt32 trishapeBlockSize = 0;
  57.                 trishapeMap.memoryUsage += binaryStream.Read((char *)&trishapeBlockSize, sizeof(UInt32));
  58.             }
  59.  
  60.             char morphNameRaw[MAX_PATH];
  61.  
  62.             BodyMorphMap morphMap;
  63.  
  64.             UInt32 morphCount = 0;
  65.             if (!packed)
  66.                 trishapeMap.memoryUsage += binaryStream.Read((char *)&morphCount, sizeof(UInt32));
  67.             else
  68.                 trishapeMap.memoryUsage += binaryStream.Read((char *)&morphCount, sizeof(UInt16));
  69.  
  70.             for (UInt32 j = 0; j < morphCount; j++)
  71.             {
  72.                 memset(morphNameRaw, 0, MAX_PATH);
  73.  
  74.                 UInt8 tsize = 0;
  75.                 trishapeMap.memoryUsage += binaryStream.Read((char *)&tsize, sizeof(UInt8));
  76.                 trishapeMap.memoryUsage += binaryStream.Read(morphNameRaw, tsize);
  77.                 BSFixedString morphName(morphNameRaw);
  78.  
  79. #ifdef _DEBUG
  80.                 _MESSAGE("%s - Reading Morph %s at (%08X)", __FUNCTION__, morphName.data, binaryStream.GetOffset());
  81. #endif
  82.                 if (tsize == 0) {
  83.                     _WARNING("%s - %s - Read empty name morph at (%08X)", __FUNCTION__, filePath.data, binaryStream.GetOffset());
  84.                 }
  85.  
  86.                 if (!packed) {
  87.                     UInt32 morphBlockSize = 0;
  88.                     trishapeMap.memoryUsage += binaryStream.Read((char *)&morphBlockSize, sizeof(UInt32));
  89.                 }
  90.  
  91.                 UInt32 vertexNum = 0;
  92.                 float multiplier = 0.0f;
  93.                 if(!packed) {
  94.                     trishapeMap.memoryUsage += binaryStream.Read((char *)&vertexNum, sizeof(UInt32));
  95.                 }
  96.                 else {
  97.                     trishapeMap.memoryUsage += binaryStream.Read((char *)&multiplier, sizeof(float));
  98.                     trishapeMap.memoryUsage += binaryStream.Read((char *)&vertexNum, sizeof(UInt16));
  99.                 }
  100.  
  101.                 if (vertexNum == 0) {
  102.                     _WARNING("%s - %s - Read morph %s on %s with no vertices at (%08X)", __FUNCTION__, filePath.data, morphName.data, trishapeName.data, binaryStream.GetOffset());
  103.                 }
  104.                 if (multiplier == 0.0f) {
  105.                     _WARNING("%s - %s - Read morph %s on %s with zero multiplier at (%08X)", __FUNCTION__, filePath.data, morphName.data, trishapeName.data, binaryStream.GetOffset());
  106.                 }
  107.  
  108. #ifdef _DEBUG
  109.                 _MESSAGE("%s - Total Vertices read: %d at (%08X)", __FUNCTION__, vertexNum, binaryStream.GetOffset());
  110. #endif
  111.                 if (vertexNum > std::numeric_limits<UInt16>::max())
  112.                 {
  113.                     _ERROR("%s - %s - Too many vertices for %s on %s read: %d at (%08X)", __FUNCTION__, filePath.data, morphName.data, vertexNum, trishapeName.data, binaryStream.GetOffset());
  114.                     return false;
  115.                 }
  116.  
  117.                 TriShapeVertexDataPtr vertexData;
  118.                 TriShapeFullVertexDataPtr fullVertexData;
  119.                 TriShapePackedVertexDataPtr packedVertexData;
  120.                 if (!packed)
  121.                 {
  122.                     fullVertexData = std::make_shared<TriShapeFullVertexData>();
  123.                     for (UInt32 k = 0; k < vertexNum; k++)
  124.                     {
  125.                         TriShapeVertexDelta vertexDelta;
  126.                         trishapeMap.memoryUsage += binaryStream.Read((char *)&vertexDelta.index, sizeof(UInt32));
  127.                         trishapeMap.memoryUsage += binaryStream.Read((char *)&vertexDelta.diff, sizeof(NiPoint3));
  128.                         fullVertexData->m_vertexDeltas.push_back(vertexDelta);
  129.                     }
  130.  
  131.                     vertexData = fullVertexData;
  132.                 }
  133.                 else
  134.                 {
  135.                     packedVertexData = std::make_shared<TriShapePackedVertexData>();
  136.                     packedVertexData->m_multiplier = multiplier;
  137.  
  138.                     for (UInt32 k = 0; k < vertexNum; k++)
  139.                     {
  140.                         TriShapePackedVertexDelta vertexDelta;
  141.                         trishapeMap.memoryUsage += binaryStream.Read((char *)&vertexDelta.index, sizeof(UInt16));
  142.                         trishapeMap.memoryUsage += binaryStream.Read((char *)&vertexDelta.x, sizeof(SInt16));
  143.                         trishapeMap.memoryUsage += binaryStream.Read((char *)&vertexDelta.y, sizeof(SInt16));
  144.                         trishapeMap.memoryUsage += binaryStream.Read((char *)&vertexDelta.z, sizeof(SInt16));
  145.  
  146.                         packedVertexData->m_vertexDeltas.push_back(vertexDelta);
  147.                     }
  148.  
  149.                     vertexData = packedVertexData;
  150.                 }
  151.  
  152.                 morphMap.emplace(morphName, vertexData);
  153.             }
  154.  
  155.             trishapeMap.emplace(trishapeName, morphMap);
  156.            
  157.         }
  158.  
  159.         trishapeMap.accessed = std::time(nullptr);
  160.  
  161.         Lock();
  162.         m_data.emplace(relativePath, trishapeMap);
  163.         totalMemory += trishapeMap.memoryUsage;
  164.         Release();
  165.         return true;
  166.     }
  167.  
  168.  
  169.     return false;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement