2020-01-01 21:29:24 +01:00
|
|
|
#include "BinaryBlob.h"
|
|
|
|
|
2020-07-19 21:43:29 +02:00
|
|
|
#include <SDL.h>
|
2021-02-24 00:21:29 +01:00
|
|
|
#ifdef VVV_COMPILEMUSIC
|
2020-01-01 21:29:24 +01:00
|
|
|
#include <stdio.h>
|
2021-02-24 00:21:29 +01:00
|
|
|
#endif
|
2020-01-01 21:29:24 +01:00
|
|
|
|
2021-02-16 03:53:17 +01:00
|
|
|
#include "Exit.h"
|
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.
2021-04-13 09:29:13 +02:00
|
|
|
#include "FileSystemUtils.h"
|
2020-08-14 10:54:19 +02:00
|
|
|
#include "UtilityClass.h"
|
2021-02-24 00:21:29 +01:00
|
|
|
#include "Vlogging.h"
|
2020-08-14 10:54:19 +02:00
|
|
|
|
Explicitly declare void for all void parameter functions (#628)
Apparently in C, if you have `void test();`, it's completely okay to do
`test(2);`. The function will take in the argument, but just discard it
and throw it away. It's like a trash can, and a rude one at that. If you
declare it like `void test(void);`, this is prevented.
This is not a problem in C++ - doing `void test();` and `test(2);` is
guaranteed to result in a compile error (this also means that right now,
at least in all `.cpp` files, nobody is ever calling a void parameter
function with arguments and having their arguments be thrown away).
However, we may not be using C++ in the future, so I just want to lay
down the precedent that if a function takes in no arguments, you must
explicitly declare it as such.
I would've added `-Wstrict-prototypes`, but it produces an annoying
warning message saying it doesn't work in C++ mode if you're compiling
in C++ mode. So it can be added later.
2021-02-25 23:23:59 +01:00
|
|
|
binaryBlob::binaryBlob(void)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
|
|
|
numberofHeaders = 0;
|
2021-02-14 02:24:20 +01:00
|
|
|
SDL_zeroa(m_headers);
|
|
|
|
SDL_zeroa(m_memblocks);
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef VVV_COMPILEMUSIC
|
|
|
|
void binaryBlob::AddFileToBinaryBlob(const char* _path)
|
|
|
|
{
|
|
|
|
long size;
|
|
|
|
char * memblock;
|
|
|
|
|
|
|
|
FILE *file = fopen(_path, "rb");
|
|
|
|
if (file != NULL)
|
|
|
|
{
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
|
|
size = ftell(file);
|
|
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
|
2020-08-14 10:56:08 +02:00
|
|
|
memblock = (char*) SDL_malloc(size);
|
Bail for all SDL_malloc() failures
Following Ethan's example of bailing (calling VVV_exit()) if
binaryBlob::unPackBinary() couldn't allocate memory, I've searched
through and found every SDL_malloc(), then made sure that if it returned
NULL, the caller would bail (because you can't do much when you're out
of memory).
There should probably be an error message printed when the process is
out of memory, but unPackBinary() doesn't print an error message for
being out of memory, so this can probably be added later. (Also we don't
really have a logging system, I'd like to have something like that added
in first before adding more messages.)
Also, this doesn't account for any allocators used by STL stuff, but
we're working on removing the STL, and allocation failure just results
in an abort anyway, so there's not really a problem there.
2021-02-16 04:00:18 +01:00
|
|
|
if (memblock == NULL)
|
|
|
|
{
|
|
|
|
VVV_exit(1);
|
|
|
|
}
|
2020-01-01 21:29:24 +01:00
|
|
|
fread(memblock, 1, size, file);
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
2021-02-24 00:21:29 +01:00
|
|
|
vlog_info("The complete file size: %li", size);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
|
|
|
m_memblocks[numberofHeaders] = memblock;
|
|
|
|
for (int i = 0; _path[i]; i += 1)
|
|
|
|
{
|
|
|
|
m_headers[numberofHeaders].name[i] = _path[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
m_headers[numberofHeaders].valid = true;
|
|
|
|
m_headers[numberofHeaders].size = size;
|
|
|
|
numberofHeaders += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-24 00:21:29 +01:00
|
|
|
vlog_info("Unable to open file");
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void binaryBlob::writeBinaryBlob(const char* _name)
|
|
|
|
{
|
|
|
|
FILE *file = fopen(_name, "wb");
|
|
|
|
if (file != NULL)
|
|
|
|
{
|
2020-07-03 12:23:54 +02:00
|
|
|
fwrite((char*) &m_headers, 1, sizeof(m_headers), file);
|
2020-01-01 21:29:24 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < numberofHeaders; i += 1)
|
|
|
|
{
|
|
|
|
fwrite(m_memblocks[i], 1, m_headers[i].size, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-24 00:21:29 +01:00
|
|
|
vlog_info("Unable to open new file for writing. Feels bad.");
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool binaryBlob::unPackBinary(const char* name)
|
|
|
|
{
|
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.
2021-04-13 09:29:13 +02:00
|
|
|
return FILESYSTEM_loadBinaryBlob(this, name);
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
Explicitly declare void for all void parameter functions (#628)
Apparently in C, if you have `void test();`, it's completely okay to do
`test(2);`. The function will take in the argument, but just discard it
and throw it away. It's like a trash can, and a rude one at that. If you
declare it like `void test(void);`, this is prevented.
This is not a problem in C++ - doing `void test();` and `test(2);` is
guaranteed to result in a compile error (this also means that right now,
at least in all `.cpp` files, nobody is ever calling a void parameter
function with arguments and having their arguments be thrown away).
However, we may not be using C++ in the future, so I just want to lay
down the precedent that if a function takes in no arguments, you must
explicitly declare it as such.
I would've added `-Wstrict-prototypes`, but it produces an annoying
warning message saying it doesn't work in C++ mode if you're compiling
in C++ mode. So it can be added later.
2021-02-25 23:23:59 +01:00
|
|
|
void binaryBlob::clear(void)
|
2020-06-07 22:11:35 +02:00
|
|
|
{
|
2020-07-03 12:23:54 +02:00
|
|
|
for (size_t i = 0; i < SDL_arraysize(m_headers); i += 1)
|
2020-06-07 22:11:35 +02:00
|
|
|
{
|
2021-02-14 02:20:48 +01:00
|
|
|
SDL_free(m_memblocks[i]);
|
2020-06-07 22:11:35 +02:00
|
|
|
}
|
2021-02-14 02:20:48 +01:00
|
|
|
SDL_zeroa(m_memblocks);
|
|
|
|
SDL_zeroa(m_headers);
|
2020-06-07 22:11:35 +02:00
|
|
|
}
|
|
|
|
|
2020-01-01 21:29:24 +01:00
|
|
|
int binaryBlob::getIndex(const char* _name)
|
|
|
|
{
|
2020-07-03 12:23:54 +02:00
|
|
|
for (size_t i = 0; i < SDL_arraysize(m_headers); i += 1)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
2020-08-14 10:56:08 +02:00
|
|
|
if (SDL_strcmp(_name, m_headers[i].name) == 0 && m_headers[i].valid)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int binaryBlob::getSize(int _index)
|
|
|
|
{
|
2020-08-14 10:54:19 +02:00
|
|
|
if (!INBOUNDS_ARR(_index, m_headers))
|
|
|
|
{
|
2021-02-24 00:21:29 +01:00
|
|
|
vlog_error("getSize() out-of-bounds!");
|
2020-08-14 10:54:19 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-01-01 21:29:24 +01:00
|
|
|
return m_headers[_index].size;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* binaryBlob::getAddress(int _index)
|
|
|
|
{
|
2020-08-14 10:54:19 +02:00
|
|
|
if (!INBOUNDS_ARR(_index, m_memblocks))
|
|
|
|
{
|
2021-02-24 00:21:29 +01:00
|
|
|
vlog_error("getAddress() out-of-bounds!");
|
2020-08-14 10:54:19 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-01-01 21:29:24 +01:00
|
|
|
return m_memblocks[_index];
|
|
|
|
}
|
2020-07-01 00:27:57 +02:00
|
|
|
|
2021-02-18 08:48:41 +01:00
|
|
|
bool binaryBlob::nextExtra(size_t* start)
|
2020-07-01 00:27:57 +02:00
|
|
|
{
|
2021-02-18 08:48:41 +01:00
|
|
|
size_t* idx;
|
|
|
|
|
|
|
|
if (start == NULL)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (idx = start; *idx < SDL_arraysize(m_headers); *idx += 1)
|
2020-07-01 00:27:57 +02:00
|
|
|
{
|
2021-02-18 08:48:41 +01:00
|
|
|
if (m_headers[*idx].valid
|
2020-07-30 23:06:07 +02:00
|
|
|
#define FOREACH_TRACK(_, track_name) && SDL_strcmp(m_headers[*idx].name, "data/" track_name) != 0
|
2021-02-16 01:38:10 +01:00
|
|
|
TRACK_NAMES(_)
|
2020-07-01 00:27:57 +02:00
|
|
|
) {
|
2021-02-18 08:48:41 +01:00
|
|
|
return true;
|
2020-07-01 00:27:57 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-18 08:48:41 +01:00
|
|
|
return false;
|
2020-07-01 00:27:57 +02:00
|
|
|
}
|