MCPcopy Create free account
hub / github.com/defold/defold / LoadResourceToBufferWithOffset

Function LoadResourceToBufferWithOffset

engine/resource/src/resource.cpp:587–649  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

585}
586
587Result LoadResourceToBufferWithOffset(HFactory factory, const char* path, const char* original_name, uint32_t offset, uint32_t size, uint32_t* resource_size, uint32_t* buffer_size, LoadBufferType* buffer)
588{
589 DM_PROFILE(__FUNCTION__);
590
591 char normalized_path[RESOURCE_PATH_MAX];
592 GetCanonicalPath(path, normalized_path, sizeof(normalized_path)); // normalize the path
593
594 // Let's find the resource in the current mounts
595
596 dmhash_t normalized_path_hash = dmHashString64(normalized_path);
597 // TODO: It might be good to get the HMount for the resource, and use that for both GetResourceSize and ReadResource
598 // Otherwise, there's a small chance of a race condition between the two calls. (i.e. a mount gets added or removed)
599
600 uint32_t file_size; // The full size of the resources
601 dmResource::Result r = dmResourceMounts::GetResourceSize(factory->m_Mounts, normalized_path_hash, normalized_path, &file_size);
602 if (r == dmResource::RESULT_OK)
603 {
604 *resource_size = file_size;
605
606 uint32_t bytes_to_read = file_size;
607 bool is_streaming = false;
608 if (size != RESOURCE_INVALID_PRELOAD_SIZE)
609 {
610 bytes_to_read = dmMath::Min(file_size, size);
611 is_streaming = true;
612 }
613
614 if (buffer->Capacity() < bytes_to_read) {
615 buffer->SetCapacity(bytes_to_read);
616 }
617 buffer->SetSize(bytes_to_read);
618
619 // Only actually read the resource if we requested any bytes
620 uint32_t nread = 0;
621 if (bytes_to_read > 0)
622 {
623 if (is_streaming)
624 {
625 // no decryption or decompressing is done on these resources
626 r = dmResourceMounts::ReadResourcePartial(factory->m_Mounts, normalized_path_hash, normalized_path, offset, bytes_to_read, (uint8_t*)buffer->Begin(), &nread);
627 }
628 else
629 {
630 // The non streaming code path supports unpacking compressed resources
631 r = dmResourceMounts::ReadResource(factory->m_Mounts, normalized_path_hash, normalized_path, (uint8_t*)buffer->Begin(), buffer->Size());
632 nread = buffer->Size();
633 }
634 }
635
636 if (r == dmResource::RESULT_OK)
637 {
638 buffer->SetSize(nread); // If we read fewer bytes than previously set
639 *buffer_size = nread;
640 return RESULT_OK;
641 }
642 else
643 {
644 buffer->SetSize(0);

Callers 3

JobProcessFunction · 0.85
LoadResourceToBufferFunction · 0.85
LoadResourceFunction · 0.85

Calls 10

GetCanonicalPathFunction · 0.85
MinFunction · 0.85
ReadResourcePartialFunction · 0.85
ReadResourceFunction · 0.85
GetResourceSizeFunction · 0.70
CapacityMethod · 0.45
SetCapacityMethod · 0.45
SetSizeMethod · 0.45
BeginMethod · 0.45
SizeMethod · 0.45

Tested by

no test coverage detected