1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-11-18 08:59:42 +01:00

Refactor FILESYSTEM_mount[assets] to not use the STL

There is no need to use heap-allocated strings here, so I've refactored
them out. I've also cleaned up both of the functions a bit, because the
line spacing of the previous version was completely non-existent, brace
style was same-line instead of next-line, and the variable names were a
bit misleading (in FILESYSTEM_mountassets(), there is a `zippath` AND a
`zip_path`, which are two completely different variables).

Also, FILESYSTEM_mount() now prints an error message and bails if
PHYSFS_getRealDir() returns NULL, whereas it didn't do that before.
This commit is contained in:
Misa 2021-02-26 18:13:25 -08:00 committed by Ethan Lee
parent 452ef3b511
commit b2e748cad1

View file

@ -171,16 +171,30 @@ static bool FILESYSTEM_exists(const char *fname)
void FILESYSTEM_mount(const char *fname) void FILESYSTEM_mount(const char *fname)
{ {
std::string path(PHYSFS_getRealDir(fname)); const char* real_dir = PHYSFS_getRealDir(fname);
path += PHYSFS_getDirSeparator(); const char* dir_separator;
path += fname; char path[MAX_PATH];
if (!PHYSFS_mount(path.c_str(), NULL, 0))
if (real_dir == NULL)
{
printf(
"Could not mount %s: real directory doesn't exist\n",
fname
);
return;
}
dir_separator = PHYSFS_getDirSeparator();
SDL_snprintf(path, sizeof(path), "%s%s%s", real_dir, dir_separator, fname);
if (!PHYSFS_mount(path, NULL, 0))
{ {
printf("Error mounting: %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); printf("Error mounting: %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
} }
else else
{ {
graphics.assetdir = path.c_str(); graphics.assetdir = std::string(path);
} }
} }
@ -188,48 +202,97 @@ bool FILESYSTEM_assetsmounted = false;
void FILESYSTEM_mountassets(const char* path) void FILESYSTEM_mountassets(const char* path)
{ {
const std::string _path(path); const size_t path_size = SDL_strlen(path);
char filename[MAX_PATH];
char zip_data[MAX_PATH];
const char* zip_normal;
char dir[MAX_PATH];
std::string zippath = "levels/" + _path.substr(7,_path.size()-14) + ".data.zip"; SDL_strlcpy(
std::string dirpath = "levels/" + _path.substr(7,_path.size()-14) + "/"; filename,
std::string zip_path; &path[7],
const char* cstr = PHYSFS_getRealDir(_path.c_str()); VVV_min((path_size - 14) + 1, sizeof(filename))
);
if (cstr) { SDL_snprintf(
zip_path = cstr; zip_data,
} sizeof(zip_data),
"levels/%s.data.zip",
filename
);
zip_normal = PHYSFS_getRealDir(path);
SDL_snprintf(
dir,
sizeof(dir),
"levels/%s/",
filename
);
if (FILESYSTEM_exists(zip_data))
{
printf("Custom asset directory is .data.zip at %s\n", zip_data);
FILESYSTEM_mount(zip_data);
if (cstr && FILESYSTEM_exists(zippath.c_str())) {
printf("Custom asset directory is .data.zip at %s\n", zippath.c_str());
FILESYSTEM_mount(zippath.c_str());
graphics.reloadresources(); graphics.reloadresources();
FILESYSTEM_assetsmounted = true; FILESYSTEM_assetsmounted = true;
} else if (zip_path != "data.zip" && !endsWith(zip_path.c_str(), "/data.zip") && endsWith(zip_path.c_str(), ".zip")) { }
printf("Custom asset directory is .zip at %s\n", zip_path.c_str()); else if (zip_normal != NULL
PHYSFS_File* zip = PHYSFS_openRead(zip_path.c_str()); && SDL_strcmp(zip_normal, "data.zip") != 0
zip_path += ".data.zip"; && !endsWith(zip_normal, "/data.zip")
if (zip == NULL) { && endsWith(zip_normal, ".zip"))
{
PHYSFS_File* zip = PHYSFS_openRead(zip_normal);
printf("Custom asset directory is .zip at %s\n", zip_normal);
SDL_snprintf(
zip_data,
sizeof(zip_data),
"%s.data.zip",
zip_normal
);
if (zip == NULL)
{
printf( printf(
"Error loading .zip: %s\n", "Error loading .zip: %s\n",
PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()) PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())
); );
} else if (PHYSFS_mountHandle(zip, zip_path.c_str(), "/", 0) == 0) { }
else if (PHYSFS_mountHandle(zip, zip_data, "/", 0) == 0)
{
printf( printf(
"Error mounting .zip: %s\n", "Error mounting .zip: %s\n",
PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()) PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())
); );
} else {
graphics.assetdir = zip_path;
} }
else
{
graphics.assetdir = std::string(zip_data);
}
FILESYSTEM_assetsmounted = true; FILESYSTEM_assetsmounted = true;
graphics.reloadresources(); graphics.reloadresources();
} else if (FILESYSTEM_exists(dirpath.c_str())) { }
printf("Custom asset directory exists at %s\n",dirpath.c_str()); else if (FILESYSTEM_exists(dir))
FILESYSTEM_mount(dirpath.c_str()); {
printf("Custom asset directory exists at %s\n", dir);
FILESYSTEM_mount(dir);
graphics.reloadresources(); graphics.reloadresources();
FILESYSTEM_assetsmounted = true; FILESYSTEM_assetsmounted = true;
} else { }
else
{
puts("Custom asset directory does not exist"); puts("Custom asset directory does not exist");
FILESYSTEM_assetsmounted = false; FILESYSTEM_assetsmounted = false;
} }
} }