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
|
|
|
|
2022-12-01 07:30:16 +01:00
|
|
|
#include "Alloc.h"
|
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
|
|
|
{
|
2021-09-24 06:55:58 +02:00
|
|
|
#ifdef VVV_COMPILEMUSIC
|
2021-09-07 03:56:39 +02:00
|
|
|
numberofHeaders = 0;
|
2021-09-24 06:55:58 +02:00
|
|
|
#endif
|
2021-09-07 03:56:39 +02: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)
|
|
|
|
{
|
2021-09-07 03:56:39 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
memblock = (char*) SDL_malloc(size);
|
|
|
|
if (memblock == NULL)
|
|
|
|
{
|
|
|
|
VVV_exit(1);
|
|
|
|
}
|
|
|
|
fread(memblock, 1, size, file);
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
vlog_info("The complete file size: %li", size);
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
vlog_info("Unable to open file");
|
|
|
|
}
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void binaryBlob::writeBinaryBlob(const char* _name)
|
|
|
|
{
|
2021-09-07 03:56:39 +02:00
|
|
|
FILE *file = fopen(_name, "wb");
|
|
|
|
if (file != NULL)
|
|
|
|
{
|
|
|
|
fwrite((char*) &m_headers, 1, sizeof(m_headers), file);
|
|
|
|
|
|
|
|
for (int i = 0; i < numberofHeaders; i += 1)
|
|
|
|
{
|
|
|
|
fwrite(m_memblocks[i], 1, m_headers[i].size, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2021-09-07 03:56:39 +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
|
|
|
{
|
2021-09-07 03:56:39 +02:00
|
|
|
for (size_t i = 0; i < SDL_arraysize(m_headers); i += 1)
|
|
|
|
{
|
2021-09-24 07:35:52 +02:00
|
|
|
if (m_memblocks[i] != NULL)
|
|
|
|
{
|
2022-12-01 07:30:16 +01:00
|
|
|
VVV_free(m_memblocks[i]);
|
2021-09-24 07:35:52 +02:00
|
|
|
}
|
2021-09-07 03:56:39 +02: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)
|
|
|
|
{
|
2021-09-07 03:56:39 +02:00
|
|
|
for (size_t i = 0; i < SDL_arraysize(m_headers); i += 1)
|
|
|
|
{
|
|
|
|
if (SDL_strcmp(_name, m_headers[i].name) == 0 && m_headers[i].valid)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int binaryBlob::getSize(int _index)
|
|
|
|
{
|
2021-09-07 03:56:39 +02:00
|
|
|
if (!INBOUNDS_ARR(_index, m_headers))
|
|
|
|
{
|
|
|
|
vlog_error("getSize() out-of-bounds!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return m_headers[_index].size;
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char* binaryBlob::getAddress(int _index)
|
|
|
|
{
|
2021-09-07 03:56:39 +02:00
|
|
|
if (!INBOUNDS_ARR(_index, m_memblocks))
|
|
|
|
{
|
|
|
|
vlog_error("getAddress() out-of-bounds!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return m_memblocks[_index];
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
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-09-07 03:56:39 +02:00
|
|
|
size_t* idx;
|
2021-02-18 08:48:41 +01:00
|
|
|
|
2021-09-07 03:56:39 +02:00
|
|
|
if (start == NULL)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-18 08:48:41 +01:00
|
|
|
|
2021-09-07 03:56:39 +02:00
|
|
|
for (idx = start; *idx < SDL_arraysize(m_headers); *idx += 1)
|
|
|
|
{
|
|
|
|
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-09-07 03:56:39 +02:00
|
|
|
TRACK_NAMES(_)
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2020-07-01 00:27:57 +02:00
|
|
|
}
|