wip
This commit is contained in:
@@ -32,15 +32,104 @@ namespace Game
|
||||
{
|
||||
namespace
|
||||
{
|
||||
constexpr size_t ChunkSize = 1024;
|
||||
constexpr size_t MaxFileSize = ChunkSize * 1024 * 1024;
|
||||
constexpr size_t MaxChunkCount = MaxFileSize / ChunkSize;
|
||||
|
||||
bool BufferedFileRead(FILE* file, uint8_t* writePtr, size_t& totalReadCount)
|
||||
{
|
||||
for (int32_t i = 0; i < MaxChunkCount; ++i)
|
||||
{
|
||||
size_t readCount = std::fread(writePtr, 1, ChunkSize, file);
|
||||
writePtr += readCount;
|
||||
totalReadCount += readCount;
|
||||
|
||||
if (readCount != ChunkSize)
|
||||
{
|
||||
if (std::feof(file))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
int err = std::ferror(file);
|
||||
if (err != 0)
|
||||
{
|
||||
LOG_ERROR("Error reading file: %i", err);
|
||||
return false;
|
||||
}
|
||||
|
||||
LOG_ERROR("This should never happen!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!std::feof(file))
|
||||
{
|
||||
LOG_ERROR("File too big to be read!");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const bgfx::Memory* LoadBinaryFile(const char* path, int32_t retryCount = 1)
|
||||
{
|
||||
FILE* file = nullptr;
|
||||
for (int32_t i = 0; i < retryCount; ++i)
|
||||
{
|
||||
file = std::fopen(path, "rb");
|
||||
if (file == nullptr)
|
||||
{
|
||||
if (i < retryCount - 1)
|
||||
{
|
||||
std::this_thread::sleep_for(100ms);
|
||||
LOG_WARN("Failed to open file, retrying...");
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("Failed to open file!");
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
uint8_t* dataPtr = AllocateScratch(MaxFileSize);
|
||||
if (dataPtr == nullptr)
|
||||
{
|
||||
LOG_ERROR("Failed to load file, exceeded scratch memory! %s", path);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
uint64_t totalReadCount = 0;
|
||||
bool success = BufferedFileRead(file, dataPtr, totalReadCount);
|
||||
std::fclose(file);
|
||||
|
||||
if (!success)
|
||||
{
|
||||
LOG_ERROR("Failed to read file %s", path);
|
||||
ResizeLastScratchAlloc(0);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!ResizeLastScratchAlloc(totalReadCount))
|
||||
{
|
||||
LOG_ERROR("This should never happen!");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return bgfx::makeRef(dataPtr, totalReadCount);
|
||||
}
|
||||
|
||||
const bgfx::Memory* loadFile(const char* path, bool appendZero = false, int32_t retryCount = 1)
|
||||
{
|
||||
FILE* file;
|
||||
for (int32_t i = 0; i < retryCount; ++i)
|
||||
{
|
||||
file = fopen(path, "rb");
|
||||
file = std::fopen(path, "rb");
|
||||
if (file == nullptr && i < retryCount - 1)
|
||||
{
|
||||
std::this_thread::sleep_for(100ms);
|
||||
LOG_WARN("Failed to open file, retrying...");
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -48,11 +137,12 @@ namespace Game
|
||||
long fileSize = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
|
||||
long fileSizeX = appendZero ? fileSize + 1 : fileSize;
|
||||
long fileSizeX = appendZero ? (fileSize + 1) : fileSize;
|
||||
void* rawMem = AllocateScratch(fileSizeX);
|
||||
if (rawMem == nullptr)
|
||||
{
|
||||
LOG_ERROR("Failed to load file, exceeded scratch memory! %s", path);
|
||||
return nullptr;
|
||||
}
|
||||
const bgfx::Memory* mem = bgfx::makeRef(rawMem, fileSizeX);
|
||||
fread(mem->data, 1, fileSize, file);
|
||||
@@ -127,7 +217,7 @@ namespace Game
|
||||
bgfx::TextureHandle handle = BGFX_INVALID_HANDLE;
|
||||
bx::Error err;
|
||||
|
||||
const bgfx::Memory* data = loadFile(_filePath.getCPtr());
|
||||
const bgfx::Memory* data = LoadBinaryFile(_filePath.getCPtr());
|
||||
if (data == nullptr || data->data == nullptr || data->size == 0)
|
||||
{
|
||||
LOG_WARN("Failed to find image %s", _filePath.getCPtr());
|
||||
|
||||
Reference in New Issue
Block a user