1
0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-06-25 05:58:30 +02:00

Abstract binary blob loading to FileSystemUtils

This seems to be a comment left by Ethan that he never got around to. So
I did it for him.

What I've done is made it so FileSystemUtils.cpp knows what a binary
blob is, and moved the binary blob loading code directly to
FileSystemUtils.cpp. To do this, I removed the private access modifier
from binaryBlob - I don't think we'll need it, and anyways when we move
to C we can't use it.

Along the way, I also cleaned up the style of the function a bit - the
null termination offset is no longer hardcoded, and the function no
longer mixes code and declarations together in the same block.

I also noticed that when printing all the filenames at the end, a single
invalid header would stop the whole loop instead of just being skipped
over... this seems to be a bug to me, so I've made it so invalid headers
just get skipped over instead of stopping the whole loop.

In FileSystemUtils.h, I used a forward declaration. In hindsight,
incomplete forward declarations should basically always be done in
header files if possible, otherwise this introduces the possibility of
transitive includes - if a file includes this header and it does a full
include, the file is silently able to use the full header, whereas if
it's a forward declaration, then the moment the file tries to use the
full header it fails, and then it's forced to include the full header
for itself. But uh, that's a code cleanup for later.
This commit is contained in:
Misa 2021-04-13 00:29:13 -07:00 committed by Ethan Lee
parent a64f75a880
commit 3f46c5ac5c
4 changed files with 94 additions and 62 deletions

View File

@ -1,10 +1,10 @@
#include "BinaryBlob.h"
#include <physfs.h> /* FIXME: Abstract to FileSystemUtils! */
#include <SDL.h>
#include <stdio.h>
#include "Exit.h"
#include "FileSystemUtils.h"
#include "UtilityClass.h"
binaryBlob::binaryBlob(void)
@ -77,66 +77,7 @@ void binaryBlob::writeBinaryBlob(const char* _name)
bool binaryBlob::unPackBinary(const char* name)
{
PHYSFS_sint64 size;
PHYSFS_File *handle = PHYSFS_openRead(name);
if (handle == NULL)
{
printf("Unable to open file %s\n", name);
return false;
}
size = PHYSFS_fileLength(handle);
PHYSFS_readBytes(handle, &m_headers, sizeof(m_headers));
int offset = 0 + (sizeof(m_headers));
for (size_t i = 0; i < SDL_arraysize(m_headers); i += 1)
{
/* Name can be stupid, just needs to be terminated */
m_headers[i].name[47] = '\0';
if (m_headers[i].valid & ~0x1 || !m_headers[i].valid)
{
m_headers[i].valid = false;
continue; /* Must be EXACTLY 1 or 0 */
}
if (m_headers[i].size < 1)
{
m_headers[i].valid = false;
continue; /* Must be nonzero and positive */
}
if ((offset + m_headers[i].size) > size)
{
m_headers[i].valid = false;
continue; /* Bogus size value */
}
PHYSFS_seek(handle, offset);
m_memblocks[i] = (char*) SDL_malloc(m_headers[i].size);
if (m_memblocks[i] == NULL)
{
VVV_exit(1); /* Oh god we're out of memory, just bail */
}
PHYSFS_readBytes(handle, m_memblocks[i], m_headers[i].size);
offset += m_headers[i].size;
}
PHYSFS_close(handle);
printf("The complete reloaded file size: %lli\n", size);
for (size_t i = 0; i < SDL_arraysize(m_headers); i += 1)
{
if (m_headers[i].valid == false)
{
break;
}
printf("%s unpacked\n", m_headers[i].name);
}
return true;
return FILESYSTEM_loadBinaryBlob(this, name);
}
void binaryBlob::clear(void)

View File

@ -57,7 +57,6 @@ public:
static const int max_headers = 128;
private:
int numberofHeaders;
resourceheader m_headers[max_headers];
char* m_memblocks[max_headers];

View File

@ -7,6 +7,7 @@
#include <tinyxml2.h>
#include <vector>
#include "BinaryBlob.h"
#include "Exit.h"
#include "Graphics.h"
#include "Maths.h"
@ -533,6 +534,91 @@ void FILESYSTEM_freeMemory(unsigned char **mem)
*mem = NULL;
}
bool FILESYSTEM_loadBinaryBlob(binaryBlob* blob, const char* filename)
{
PHYSFS_sint64 size;
PHYSFS_File* handle;
int offset;
size_t i;
if (blob == NULL || filename == NULL)
{
return false;
}
handle = PHYSFS_openRead(filename);
if (handle == NULL)
{
printf("Unable to open file %s\n", filename);
return false;
}
size = PHYSFS_fileLength(handle);
PHYSFS_readBytes(
handle,
&blob->m_headers,
sizeof(blob->m_headers)
);
offset = sizeof(blob->m_headers);
for (i = 0; i < SDL_arraysize(blob->m_headers); ++i)
{
resourceheader* header = &blob->m_headers[i];
char** memblock = &blob->m_memblocks[i];
/* Name can be stupid, just needs to be terminated */
static const size_t last_char = sizeof(header->name) - 1;
header->name[last_char] = '\0';
if (header->valid & ~0x1 || !header->valid)
{
goto fail; /* Must be EXACTLY 1 or 0 */
}
if (header->size < 1)
{
goto fail; /* Must be nonzero and positive */
}
if (offset + header->size > size)
{
goto fail; /* Bogus size value */
}
PHYSFS_seek(handle, offset);
*memblock = (char*) SDL_malloc(header->size);
if (*memblock == NULL)
{
VVV_exit(1); /* Oh god we're out of memory, just bail */
}
PHYSFS_readBytes(handle, *memblock, header->size);
offset += header->size;
continue;
fail:
header->valid = false;
}
PHYSFS_close(handle);
printf("The complete reloaded file size: %lli\n", size);
for (i = 0; i < SDL_arraysize(blob->m_headers); ++i)
{
const resourceheader* header = &blob->m_headers[i];
if (!header->valid)
{
continue;
}
printf("%s unpacked\n", header->name);
}
return true;
}
bool FILESYSTEM_saveTiXml2Document(const char *name, tinyxml2::XMLDocument& doc)
{
/* XMLDocument.SaveFile doesn't account for Unicode paths, PHYSFS does */

View File

@ -1,6 +1,9 @@
#ifndef FILESYSTEMUTILS_H
#define FILESYSTEMUTILS_H
/* Forward declaration */
class binaryBlob;
#include <stddef.h>
// Forward declaration, including the entirety of tinyxml2.h across all files this file is included in is unnecessary
@ -29,6 +32,9 @@ void FILESYSTEM_loadAssetToMemory(
const bool addnull
);
void FILESYSTEM_freeMemory(unsigned char **mem);
bool FILESYSTEM_loadBinaryBlob(binaryBlob* blob, const char* filename);
bool FILESYSTEM_saveTiXml2Document(const char *name, tinyxml2::XMLDocument& doc);
bool FILESYSTEM_loadTiXml2Document(const char *name, tinyxml2::XMLDocument& doc);