Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool MorphCache::CacheFile(const char * relativePath)
- {
- BSFixedString filePath(relativePath);
- if(relativePath == "")
- return false;
- FileMap::iterator it = m_data.find(filePath);
- if (it != m_data.end()) {
- it->second.accessed = std::time(nullptr);
- return false;
- }
- #ifdef _DEBUG
- _MESSAGE("%s - Parsing: %s", __FUNCTION__, filePath.data);
- #endif
- BSResourceNiBinaryStream binaryStream(filePath.data);
- if(binaryStream.IsValid())
- {
- TriShapeMap trishapeMap;
- UInt32 fileFormat = 0;
- trishapeMap.memoryUsage += binaryStream.Read((char *)&fileFormat, sizeof(UInt32));
- bool packed = false;
- if (fileFormat != 'TRI\0' && fileFormat != 'TRIP')
- return false;
- if (fileFormat == 'TRIP')
- packed = true;
- UInt32 trishapeCount = 0;
- if (!packed)
- trishapeMap.memoryUsage += binaryStream.Read((char *)&trishapeCount, sizeof(UInt32));
- else
- trishapeMap.memoryUsage += binaryStream.Read((char *)&trishapeCount, sizeof(UInt16));
- char trishapeNameRaw[MAX_PATH];
- for (UInt32 i = 0; i < trishapeCount; i++)
- {
- memset(trishapeNameRaw, 0, MAX_PATH);
- UInt8 size = 0;
- trishapeMap.memoryUsage += binaryStream.Read((char *)&size, sizeof(UInt8));
- trishapeMap.memoryUsage += binaryStream.Read(trishapeNameRaw, size);
- BSFixedString trishapeName(trishapeNameRaw);
- #ifdef _DEBUG
- _MESSAGE("%s - Reading TriShape %s", __FUNCTION__, trishapeName.data);
- #endif
- if (!packed) {
- UInt32 trishapeBlockSize = 0;
- trishapeMap.memoryUsage += binaryStream.Read((char *)&trishapeBlockSize, sizeof(UInt32));
- }
- char morphNameRaw[MAX_PATH];
- BodyMorphMap morphMap;
- UInt32 morphCount = 0;
- if (!packed)
- trishapeMap.memoryUsage += binaryStream.Read((char *)&morphCount, sizeof(UInt32));
- else
- trishapeMap.memoryUsage += binaryStream.Read((char *)&morphCount, sizeof(UInt16));
- for (UInt32 j = 0; j < morphCount; j++)
- {
- memset(morphNameRaw, 0, MAX_PATH);
- UInt8 tsize = 0;
- trishapeMap.memoryUsage += binaryStream.Read((char *)&tsize, sizeof(UInt8));
- trishapeMap.memoryUsage += binaryStream.Read(morphNameRaw, tsize);
- BSFixedString morphName(morphNameRaw);
- #ifdef _DEBUG
- _MESSAGE("%s - Reading Morph %s at (%08X)", __FUNCTION__, morphName.data, binaryStream.GetOffset());
- #endif
- if (tsize == 0) {
- _WARNING("%s - %s - Read empty name morph at (%08X)", __FUNCTION__, filePath.data, binaryStream.GetOffset());
- }
- if (!packed) {
- UInt32 morphBlockSize = 0;
- trishapeMap.memoryUsage += binaryStream.Read((char *)&morphBlockSize, sizeof(UInt32));
- }
- UInt32 vertexNum = 0;
- float multiplier = 0.0f;
- if(!packed) {
- trishapeMap.memoryUsage += binaryStream.Read((char *)&vertexNum, sizeof(UInt32));
- }
- else {
- trishapeMap.memoryUsage += binaryStream.Read((char *)&multiplier, sizeof(float));
- trishapeMap.memoryUsage += binaryStream.Read((char *)&vertexNum, sizeof(UInt16));
- }
- if (vertexNum == 0) {
- _WARNING("%s - %s - Read morph %s on %s with no vertices at (%08X)", __FUNCTION__, filePath.data, morphName.data, trishapeName.data, binaryStream.GetOffset());
- }
- if (multiplier == 0.0f) {
- _WARNING("%s - %s - Read morph %s on %s with zero multiplier at (%08X)", __FUNCTION__, filePath.data, morphName.data, trishapeName.data, binaryStream.GetOffset());
- }
- #ifdef _DEBUG
- _MESSAGE("%s - Total Vertices read: %d at (%08X)", __FUNCTION__, vertexNum, binaryStream.GetOffset());
- #endif
- if (vertexNum > std::numeric_limits<UInt16>::max())
- {
- _ERROR("%s - %s - Too many vertices for %s on %s read: %d at (%08X)", __FUNCTION__, filePath.data, morphName.data, vertexNum, trishapeName.data, binaryStream.GetOffset());
- return false;
- }
- TriShapeVertexDataPtr vertexData;
- TriShapeFullVertexDataPtr fullVertexData;
- TriShapePackedVertexDataPtr packedVertexData;
- if (!packed)
- {
- fullVertexData = std::make_shared<TriShapeFullVertexData>();
- for (UInt32 k = 0; k < vertexNum; k++)
- {
- TriShapeVertexDelta vertexDelta;
- trishapeMap.memoryUsage += binaryStream.Read((char *)&vertexDelta.index, sizeof(UInt32));
- trishapeMap.memoryUsage += binaryStream.Read((char *)&vertexDelta.diff, sizeof(NiPoint3));
- fullVertexData->m_vertexDeltas.push_back(vertexDelta);
- }
- vertexData = fullVertexData;
- }
- else
- {
- packedVertexData = std::make_shared<TriShapePackedVertexData>();
- packedVertexData->m_multiplier = multiplier;
- for (UInt32 k = 0; k < vertexNum; k++)
- {
- TriShapePackedVertexDelta vertexDelta;
- trishapeMap.memoryUsage += binaryStream.Read((char *)&vertexDelta.index, sizeof(UInt16));
- trishapeMap.memoryUsage += binaryStream.Read((char *)&vertexDelta.x, sizeof(SInt16));
- trishapeMap.memoryUsage += binaryStream.Read((char *)&vertexDelta.y, sizeof(SInt16));
- trishapeMap.memoryUsage += binaryStream.Read((char *)&vertexDelta.z, sizeof(SInt16));
- packedVertexData->m_vertexDeltas.push_back(vertexDelta);
- }
- vertexData = packedVertexData;
- }
- morphMap.emplace(morphName, vertexData);
- }
- trishapeMap.emplace(trishapeName, morphMap);
- }
- trishapeMap.accessed = std::time(nullptr);
- Lock();
- m_data.emplace(relativePath, trishapeMap);
- totalMemory += trishapeMap.memoryUsage;
- Release();
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement