2020-11-28 01:49:13 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2021-01-27 21:26:19 +00:00
|
|
|
#include <libreborn/libreborn.h>
|
2020-11-28 01:49:13 +00:00
|
|
|
|
2022-04-22 23:38:15 +00:00
|
|
|
#include "override.h"
|
2021-06-17 21:32:24 +00:00
|
|
|
#include "../home/home.h"
|
|
|
|
|
2022-04-22 23:38:15 +00:00
|
|
|
// Hook access
|
|
|
|
HOOK(access, int, (const char *pathname, int mode)) {
|
|
|
|
char *new_path = override_get_path(pathname);
|
|
|
|
// Open File
|
|
|
|
ensure_access();
|
|
|
|
int ret = (*real_access)(new_path != NULL ? new_path : pathname, mode);
|
|
|
|
// Free Data
|
|
|
|
if (new_path != NULL) {
|
|
|
|
free(new_path);
|
|
|
|
}
|
|
|
|
// Return
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-09 23:47:31 +00:00
|
|
|
// Get Override Path For File (If It Exists)
|
2021-09-12 03:18:12 +00:00
|
|
|
char *override_get_path(const char *filename) {
|
2021-06-17 21:32:24 +00:00
|
|
|
// Get MCPI Home Path
|
|
|
|
char *home_path = home_get();
|
2020-11-28 01:49:13 +00:00
|
|
|
// Get Asset Override Path
|
|
|
|
char *overrides = NULL;
|
2021-06-17 21:32:24 +00:00
|
|
|
safe_asprintf(&overrides, "%s/overrides", home_path);
|
2021-09-12 03:18:12 +00:00
|
|
|
|
2022-04-22 23:38:15 +00:00
|
|
|
// Data Prefiix
|
|
|
|
char *data_prefix = "data/";
|
|
|
|
int data_prefix_length = strlen(data_prefix);
|
2021-09-12 03:18:12 +00:00
|
|
|
|
2022-04-22 23:38:15 +00:00
|
|
|
// Folders To Check
|
|
|
|
char *asset_folders[] = {
|
|
|
|
overrides,
|
|
|
|
getenv("MCPI_REBORN_ASSETS_PATH"),
|
|
|
|
getenv("MCPI_VANILLA_ASSETS_PATH"),
|
|
|
|
NULL
|
|
|
|
};
|
2021-09-12 03:18:12 +00:00
|
|
|
|
|
|
|
// Check For Override
|
2020-11-28 01:49:13 +00:00
|
|
|
char *new_path = NULL;
|
2022-04-22 23:38:15 +00:00
|
|
|
if (starts_with(filename, data_prefix)) {
|
|
|
|
// Test Asset Folders
|
|
|
|
for (int i = 0; asset_folders[i] != NULL; i++) {
|
|
|
|
safe_asprintf(&new_path, "%s/%s", asset_folders[i], &filename[data_prefix_length]);
|
|
|
|
ensure_access();
|
|
|
|
if ((*real_access)(new_path, F_OK) == -1) {
|
|
|
|
// Not Found In Asset Folder
|
2020-11-28 01:49:13 +00:00
|
|
|
free(new_path);
|
|
|
|
new_path = NULL;
|
2022-04-22 23:38:15 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
// Found
|
|
|
|
break;
|
2020-11-28 01:49:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-12 03:18:12 +00:00
|
|
|
|
2022-04-22 23:38:15 +00:00
|
|
|
// Free
|
2020-11-28 01:49:13 +00:00
|
|
|
free(overrides);
|
2021-09-12 03:18:12 +00:00
|
|
|
|
2020-11-28 01:49:13 +00:00
|
|
|
// Return
|
|
|
|
return new_path;
|
|
|
|
}
|
|
|
|
|
2022-04-22 23:38:15 +00:00
|
|
|
// Hook fopen
|
2020-11-28 01:49:13 +00:00
|
|
|
HOOK(fopen, FILE *, (const char *filename, const char *mode)) {
|
2021-09-12 03:18:12 +00:00
|
|
|
char *new_path = override_get_path(filename);
|
2020-11-28 01:49:13 +00:00
|
|
|
// Open File
|
|
|
|
ensure_fopen();
|
|
|
|
FILE *file = (*real_fopen)(new_path != NULL ? new_path : filename, mode);
|
|
|
|
// Free Data
|
|
|
|
if (new_path != NULL) {
|
|
|
|
free(new_path);
|
|
|
|
}
|
|
|
|
// Return File
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2022-04-22 23:38:15 +00:00
|
|
|
// Hook fopen64
|
2020-11-28 01:49:13 +00:00
|
|
|
HOOK(fopen64, FILE *, (const char *filename, const char *mode)) {
|
2021-09-12 03:18:12 +00:00
|
|
|
char *new_path = override_get_path(filename);
|
2020-11-28 01:49:13 +00:00
|
|
|
// Open File
|
|
|
|
ensure_fopen64();
|
|
|
|
FILE *file = (*real_fopen64)(new_path != NULL ? new_path : filename, mode);
|
|
|
|
// Free Data
|
|
|
|
if (new_path != NULL) {
|
|
|
|
free(new_path);
|
|
|
|
}
|
|
|
|
// Return File
|
|
|
|
return file;
|
2021-06-17 21:32:24 +00:00
|
|
|
}
|