2020-11-21 12:52:38 -05:00
|
|
|
/*!
|
|
|
|
* @file FileUtil.cpp
|
|
|
|
* Utility functions for reading and writing files.
|
|
|
|
*/
|
|
|
|
|
2020-09-09 00:54:16 -04:00
|
|
|
#include "FileUtil.h"
|
|
|
|
#include <iostream>
|
2020-11-21 12:52:38 -05:00
|
|
|
#include <filesystem>
|
|
|
|
#include <cstdio> /* defines FILENAME_MAX */
|
2020-09-10 20:03:31 -04:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
2021-03-06 16:06:08 -05:00
|
|
|
#include <cstdlib>
|
|
|
|
#include "common/util/BinaryReader.h"
|
2020-11-16 19:57:45 -05:00
|
|
|
#include "BinaryWriter.h"
|
|
|
|
#include "common/common_types.h"
|
2022-02-16 22:13:18 -05:00
|
|
|
|
|
|
|
// This disables the use of PCLMULQDQ which is probably ok, but let's just be safe and disable it
|
|
|
|
// because nobody will care if png compression is 10% slower.
|
|
|
|
#define FPNG_NO_SSE 1
|
2022-02-12 17:48:50 -05:00
|
|
|
#include "third-party/fpng/fpng.cpp"
|
2022-02-16 22:13:18 -05:00
|
|
|
|
2022-02-12 17:48:50 -05:00
|
|
|
#include "third-party/fpng/fpng.h"
|
2021-07-24 11:17:27 -04:00
|
|
|
#include "third-party/fmt/core.h"
|
2021-03-06 16:06:08 -05:00
|
|
|
#include "third-party/lzokay/lzokay.hpp"
|
2020-09-09 00:54:16 -04:00
|
|
|
|
2020-09-09 02:41:45 -04:00
|
|
|
#ifdef _WIN32
|
2020-09-10 06:07:23 -04:00
|
|
|
#include <Windows.h>
|
2020-09-09 02:41:45 -04:00
|
|
|
#else
|
2020-09-09 18:35:29 -04:00
|
|
|
#include <unistd.h>
|
2020-09-12 20:41:12 -04:00
|
|
|
#include <cstring>
|
2020-09-09 02:41:45 -04:00
|
|
|
#endif
|
2022-02-08 19:02:47 -05:00
|
|
|
#include "common/util/Assert.h"
|
2020-09-09 00:54:16 -04:00
|
|
|
|
2020-11-21 15:58:51 -05:00
|
|
|
namespace file_util {
|
2021-03-03 00:05:13 -05:00
|
|
|
std::filesystem::path get_user_home_dir() {
|
|
|
|
#ifdef _WIN32
|
|
|
|
// NOTE - on older systems, this may case issues if it cannot be found!
|
|
|
|
std::string home_dir = std::getenv("USERPROFILE");
|
|
|
|
return std::filesystem::path(home_dir);
|
|
|
|
#else
|
|
|
|
std::string home_dir = std::getenv("HOME");
|
|
|
|
return std::filesystem::path(home_dir);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-21 15:58:51 -05:00
|
|
|
std::string get_project_path() {
|
2020-09-10 06:07:23 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
char buffer[FILENAME_MAX];
|
|
|
|
GetModuleFileNameA(NULL, buffer, FILENAME_MAX);
|
2020-09-12 20:41:12 -04:00
|
|
|
std::string::size_type pos =
|
|
|
|
std::string(buffer).rfind("jak-project"); // Strip file path down to \jak-project\ directory
|
2020-09-10 17:24:03 -04:00
|
|
|
return std::string(buffer).substr(
|
2020-09-12 20:41:12 -04:00
|
|
|
0, pos + 11); // + 12 to include "\jak-project" in the returned filepath
|
2020-09-10 20:03:31 -04:00
|
|
|
#else
|
|
|
|
// do Linux stuff
|
2020-09-17 21:47:52 -04:00
|
|
|
char buffer[FILENAME_MAX + 1];
|
|
|
|
auto len = readlink("/proc/self/exe", buffer,
|
|
|
|
FILENAME_MAX); // /proc/self acts like a "virtual folder" containing
|
2020-11-21 15:58:51 -05:00
|
|
|
// information about the current process
|
2020-09-17 21:47:52 -04:00
|
|
|
buffer[len] = '\0';
|
2020-09-12 20:41:12 -04:00
|
|
|
std::string::size_type pos =
|
|
|
|
std::string(buffer).rfind("jak-project"); // Strip file path down to /jak-project/ directory
|
2020-09-10 17:24:03 -04:00
|
|
|
return std::string(buffer).substr(
|
2020-09-12 20:41:12 -04:00
|
|
|
0, pos + 11); // + 12 to include "/jak-project" in the returned filepath
|
2020-09-10 06:07:23 -04:00
|
|
|
#endif
|
2020-09-09 02:41:45 -04:00
|
|
|
}
|
2020-09-09 00:54:16 -04:00
|
|
|
|
2020-11-21 15:58:51 -05:00
|
|
|
std::string get_file_path(const std::vector<std::string>& input) {
|
2020-09-10 20:03:31 -04:00
|
|
|
std::string currentPath = file_util::get_project_path();
|
2020-09-09 02:41:45 -04:00
|
|
|
char dirSeparator;
|
|
|
|
|
2020-09-09 18:35:29 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
dirSeparator = '\\';
|
|
|
|
#else
|
|
|
|
dirSeparator = '/';
|
|
|
|
#endif
|
2020-09-09 02:41:45 -04:00
|
|
|
|
|
|
|
std::string filePath = currentPath;
|
2020-09-10 20:03:31 -04:00
|
|
|
for (int i = 0; i < int(input.size()); i++) {
|
2020-09-09 18:35:29 -04:00
|
|
|
filePath = filePath + dirSeparator + input[i];
|
|
|
|
}
|
2020-09-09 00:54:16 -04:00
|
|
|
|
2020-09-09 02:41:45 -04:00
|
|
|
return filePath;
|
|
|
|
}
|
2020-09-10 20:03:31 -04:00
|
|
|
|
2020-11-21 15:58:51 -05:00
|
|
|
bool create_dir_if_needed(const std::string& path) {
|
2020-11-21 12:52:38 -05:00
|
|
|
if (!std::filesystem::is_directory(path)) {
|
|
|
|
std::filesystem::create_directories(path);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-05 19:41:09 -05:00
|
|
|
void write_binary_file(const std::string& name, const void* data, size_t size) {
|
2020-09-10 20:03:31 -04:00
|
|
|
FILE* fp = fopen(name.c_str(), "wb");
|
|
|
|
if (!fp) {
|
|
|
|
throw std::runtime_error("couldn't open file " + name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fwrite(data, size, 1, fp) != 1) {
|
2021-11-12 22:00:41 -05:00
|
|
|
fclose(fp);
|
2020-09-10 20:03:31 -04:00
|
|
|
throw std::runtime_error("couldn't write file " + name);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2022-02-15 18:42:48 -05:00
|
|
|
void write_rgba_png(const std::string& name, void* data, int w, int h) {
|
2022-02-12 17:48:50 -05:00
|
|
|
auto flags = 0;
|
2020-11-16 19:57:45 -05:00
|
|
|
|
2022-02-12 17:48:50 -05:00
|
|
|
auto ok = fpng::fpng_encode_image_to_file(name.c_str(), data, w, h, 4, flags);
|
2020-11-16 19:57:45 -05:00
|
|
|
|
2022-02-12 17:48:50 -05:00
|
|
|
if (!ok) {
|
|
|
|
throw std::runtime_error("couldn't save png file " + name);
|
|
|
|
}
|
2020-11-16 19:57:45 -05:00
|
|
|
}
|
|
|
|
|
2020-11-21 15:58:51 -05:00
|
|
|
void write_text_file(const std::string& file_name, const std::string& text) {
|
2020-09-10 20:03:31 -04:00
|
|
|
FILE* fp = fopen(file_name.c_str(), "w");
|
|
|
|
if (!fp) {
|
|
|
|
printf("Failed to fopen %s\n", file_name.c_str());
|
|
|
|
throw std::runtime_error("Failed to open file");
|
|
|
|
}
|
|
|
|
fprintf(fp, "%s\n", text.c_str());
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2020-11-21 15:58:51 -05:00
|
|
|
std::vector<uint8_t> read_binary_file(const std::string& filename) {
|
2021-07-24 11:17:27 -04:00
|
|
|
// make sure file exists and isn't a directory
|
|
|
|
std::filesystem::path path(filename);
|
|
|
|
|
|
|
|
auto status = std::filesystem::status(std::filesystem::path(filename));
|
|
|
|
|
|
|
|
if (!std::filesystem::exists(status)) {
|
|
|
|
throw std::runtime_error(fmt::format("File {} cannot be opened: does not exist.", filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status.type() != std::filesystem::file_type::regular &&
|
|
|
|
status.type() != std::filesystem::file_type::symlink) {
|
|
|
|
throw std::runtime_error(
|
|
|
|
fmt::format("File {} cannot be opened: not a regular file or symlink.", filename));
|
|
|
|
}
|
|
|
|
|
2020-09-10 20:03:31 -04:00
|
|
|
auto fp = fopen(filename.c_str(), "rb");
|
|
|
|
if (!fp)
|
2020-09-12 20:41:12 -04:00
|
|
|
throw std::runtime_error("File " + filename +
|
|
|
|
" cannot be opened: " + std::string(strerror(errno)));
|
2020-09-10 20:03:31 -04:00
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
auto len = ftell(fp);
|
|
|
|
rewind(fp);
|
|
|
|
|
|
|
|
std::vector<uint8_t> data;
|
|
|
|
data.resize(len);
|
|
|
|
|
|
|
|
if (fread(data.data(), len, 1, fp) != 1) {
|
2021-11-12 22:00:41 -05:00
|
|
|
fclose(fp);
|
2020-09-10 20:03:31 -04:00
|
|
|
throw std::runtime_error("File " + filename + " cannot be read");
|
|
|
|
}
|
2020-09-12 20:41:12 -04:00
|
|
|
fclose(fp);
|
2020-09-10 20:03:31 -04:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-11-21 15:58:51 -05:00
|
|
|
std::string read_text_file(const std::string& path) {
|
2020-09-10 20:03:31 -04:00
|
|
|
std::ifstream file(path);
|
|
|
|
if (!file.good()) {
|
|
|
|
throw std::runtime_error("couldn't open " + path);
|
|
|
|
}
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << file.rdbuf();
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
2020-11-21 15:58:51 -05:00
|
|
|
bool is_printable_char(char c) {
|
2020-09-10 20:03:31 -04:00
|
|
|
return c >= ' ' && c <= '~';
|
2020-10-15 20:59:30 -04:00
|
|
|
}
|
2020-10-25 02:26:35 -04:00
|
|
|
|
2020-11-21 15:58:51 -05:00
|
|
|
std::string combine_path(const std::string& parent, const std::string& child) {
|
2020-10-25 02:26:35 -04:00
|
|
|
return parent + "/" + child;
|
|
|
|
}
|
|
|
|
|
2022-02-12 17:48:50 -05:00
|
|
|
bool file_exists(const std::string& path) {
|
|
|
|
return std::filesystem::exists(path);
|
|
|
|
}
|
|
|
|
|
2020-11-21 15:58:51 -05:00
|
|
|
std::string base_name(const std::string& filename) {
|
2020-10-25 02:26:35 -04:00
|
|
|
size_t pos = 0;
|
2022-02-08 19:02:47 -05:00
|
|
|
ASSERT(!filename.empty());
|
2020-10-25 02:26:35 -04:00
|
|
|
for (size_t i = filename.size() - 1; i-- > 0;) {
|
2021-04-10 21:17:12 -04:00
|
|
|
if (filename.at(i) == '/' || filename.at(i) == '\\') {
|
2020-10-25 02:26:35 -04:00
|
|
|
pos = (i + 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filename.substr(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool sInitCrc = false;
|
|
|
|
static uint32_t crc_table[0x100];
|
|
|
|
|
2020-11-21 15:58:51 -05:00
|
|
|
void init_crc() {
|
2020-10-25 02:26:35 -04:00
|
|
|
for (uint32_t i = 0; i < 0x100; i++) {
|
|
|
|
uint32_t n = i << 24u;
|
|
|
|
for (uint32_t j = 0; j < 8; j++)
|
|
|
|
n = n & 0x80000000 ? (n << 1u) ^ 0x04c11db7u : (n << 1u);
|
|
|
|
crc_table[i] = n;
|
|
|
|
}
|
|
|
|
sInitCrc = true;
|
|
|
|
}
|
|
|
|
|
2020-11-21 15:58:51 -05:00
|
|
|
uint32_t crc32(const uint8_t* data, size_t size) {
|
2022-02-08 19:02:47 -05:00
|
|
|
ASSERT(sInitCrc);
|
2020-10-25 02:26:35 -04:00
|
|
|
uint32_t crc = 0;
|
|
|
|
for (size_t i = size; i != 0; i--, data++) {
|
|
|
|
crc = crc_table[crc >> 24u] ^ ((crc << 8u) | *data);
|
|
|
|
}
|
|
|
|
return ~crc;
|
|
|
|
}
|
|
|
|
|
2020-11-21 15:58:51 -05:00
|
|
|
uint32_t crc32(const std::vector<uint8_t>& data) {
|
2020-10-25 02:26:35 -04:00
|
|
|
return crc32(data.data(), data.size());
|
|
|
|
}
|
2020-11-21 15:58:51 -05:00
|
|
|
|
|
|
|
void ISONameFromAnimationName(char* dst, const char* src) {
|
|
|
|
// The Animation Name is a bunch of words separated by dashes
|
|
|
|
|
|
|
|
// copy first two chars of the first word exactly
|
|
|
|
dst[0] = src[0];
|
|
|
|
dst[1] = src[1];
|
|
|
|
s32 i = 2; // 2 chars added to dst.
|
|
|
|
|
|
|
|
// skip ahead to the first dash (or \0 if there's no dashes)
|
|
|
|
const char* src_ptr = src;
|
|
|
|
while (*src_ptr && *src_ptr != '-') {
|
|
|
|
src_ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the points to the next dash (or \0 if there's none).
|
|
|
|
const char* next_ptr = src_ptr;
|
|
|
|
if (*src_ptr) {
|
|
|
|
// loop over words (next_ptr points to dash before word, i counts chars in dest)
|
|
|
|
while (src_ptr = next_ptr + 1, i < 8) {
|
|
|
|
// scan next_ptr forward to next dash
|
|
|
|
next_ptr = src_ptr;
|
|
|
|
while (*next_ptr && *next_ptr != '-') {
|
|
|
|
next_ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// there's no next word, so break (the current word will be handled there)
|
|
|
|
if (!*next_ptr)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// add a char for the current word:
|
|
|
|
char char_to_add;
|
|
|
|
if (next_ptr[-1] < '0' || next_ptr[-1] > '9') {
|
|
|
|
// word doesn't end in a number.
|
|
|
|
|
|
|
|
// some special case words map to special letters (likely to avoid animation name conflicts)
|
|
|
|
if (next_ptr - src_ptr == 10 && !memcmp(src_ptr, "resolution", 10)) {
|
|
|
|
char_to_add = 'z';
|
|
|
|
} else if (next_ptr - src_ptr == 6 && !memcmp(src_ptr, "accept", 6)) {
|
|
|
|
char_to_add = 'y';
|
|
|
|
} else if (next_ptr - src_ptr == 6 && !memcmp(src_ptr, "reject", 6)) {
|
|
|
|
char_to_add = 'n';
|
|
|
|
} else {
|
|
|
|
// not a special case, just take the first letter.
|
|
|
|
char_to_add = *src_ptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// the current word ends in a number, just use this number (I think usually the whole word
|
|
|
|
// is just a number)
|
|
|
|
char_to_add = next_ptr[-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
dst[i++] = char_to_add;
|
|
|
|
}
|
|
|
|
|
|
|
|
// here we ran out of room in dest, or words in source.
|
|
|
|
// if there's still room in dest and chars in source, just add them
|
|
|
|
while (*src_ptr && (i < 8)) {
|
|
|
|
dst[i] = *src_ptr;
|
|
|
|
src_ptr++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pad with spaces (for ISO Name)
|
|
|
|
while (i < 8) {
|
|
|
|
dst[i++] = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
// upper case
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
if (dst[i] > '`' && dst[i] < '{') {
|
|
|
|
dst[i] -= 0x20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// append file extension
|
|
|
|
strcpy(dst + 8, "STR");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MakeISOName(char* dst, const char* src) {
|
|
|
|
int i = 0;
|
|
|
|
const char* src_ptr = src;
|
|
|
|
char* dst_ptr = dst;
|
|
|
|
|
|
|
|
// copy name and upper case
|
|
|
|
while ((i < 8) && (*src_ptr) && (*src_ptr != '.')) {
|
|
|
|
char c = *src_ptr;
|
|
|
|
src_ptr++;
|
|
|
|
if (('`' < c) && (c < '{')) { // lower case
|
|
|
|
c -= 0x20;
|
|
|
|
}
|
|
|
|
*dst_ptr = c;
|
|
|
|
dst_ptr++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pad out name with spaces
|
|
|
|
while (i < 8) {
|
|
|
|
*dst_ptr = ' ';
|
|
|
|
dst_ptr++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// increment past period
|
|
|
|
if (*src_ptr == '.')
|
|
|
|
src_ptr++;
|
|
|
|
|
|
|
|
// same for extension
|
|
|
|
while (i < 11 && (*src_ptr)) {
|
|
|
|
char c = *src_ptr;
|
|
|
|
src_ptr++;
|
|
|
|
if (('`' < c) && (c < '{')) { // lower case
|
|
|
|
c -= 0x20;
|
|
|
|
}
|
|
|
|
*dst_ptr = c;
|
|
|
|
dst_ptr++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (i < 11) {
|
|
|
|
*dst_ptr = ' ';
|
|
|
|
dst_ptr++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
*dst_ptr = 0;
|
|
|
|
}
|
2020-11-22 12:59:55 -05:00
|
|
|
|
|
|
|
void assert_file_exists(const char* path, const char* error_message) {
|
|
|
|
if (!std::filesystem::exists(path)) {
|
|
|
|
fprintf(stderr, "File %s was not found: %s\n", path, error_message);
|
2022-02-08 19:02:47 -05:00
|
|
|
ASSERT(false);
|
2020-11-22 12:59:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 16:06:08 -05:00
|
|
|
/*!
|
|
|
|
* Check if the given DGO header (or entire file) is compressed.
|
|
|
|
*/
|
|
|
|
bool dgo_header_is_compressed(const std::vector<u8>& data) {
|
|
|
|
const char compressed_header[] = "oZlB";
|
|
|
|
bool is_compressed = true;
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (compressed_header[i] != data.at(i)) {
|
|
|
|
is_compressed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return is_compressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Decompress a DGO. Resulting data will start at the DGO header.
|
|
|
|
*/
|
|
|
|
std::vector<u8> decompress_dgo(const std::vector<u8>& data_in) {
|
|
|
|
constexpr int MAX_CHUNK_SIZE = 0x8000;
|
|
|
|
BinaryReader compressed_reader(data_in);
|
|
|
|
// seek past oZlB
|
|
|
|
compressed_reader.ffwd(4);
|
|
|
|
std::size_t decompressed_size = compressed_reader.read<uint32_t>();
|
|
|
|
std::vector<uint8_t> decompressed_data;
|
|
|
|
decompressed_data.resize(decompressed_size);
|
|
|
|
size_t output_offset = 0;
|
|
|
|
while (true) {
|
|
|
|
// seek past alignment bytes and read the next chunk size
|
|
|
|
uint32_t chunk_size = 0;
|
|
|
|
while (!chunk_size) {
|
|
|
|
chunk_size = compressed_reader.read<uint32_t>();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chunk_size < MAX_CHUNK_SIZE) {
|
|
|
|
std::size_t bytes_written = 0;
|
|
|
|
lzokay::EResult ok = lzokay::decompress(
|
|
|
|
compressed_reader.here(), chunk_size, decompressed_data.data() + output_offset,
|
|
|
|
decompressed_data.size() - output_offset, bytes_written);
|
2022-02-08 19:02:47 -05:00
|
|
|
ASSERT(ok == lzokay::EResult::Success);
|
2021-03-06 16:06:08 -05:00
|
|
|
compressed_reader.ffwd(chunk_size);
|
|
|
|
output_offset += bytes_written;
|
|
|
|
} else {
|
|
|
|
// nope - sometimes chunk_size is bigger than MAX, but we should still use max.
|
2022-02-08 19:02:47 -05:00
|
|
|
// ASSERT(chunk_size == MAX_CHUNK_SIZE);
|
2021-03-06 16:06:08 -05:00
|
|
|
memcpy(decompressed_data.data() + output_offset, compressed_reader.here(), MAX_CHUNK_SIZE);
|
|
|
|
compressed_reader.ffwd(MAX_CHUNK_SIZE);
|
|
|
|
output_offset += MAX_CHUNK_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output_offset >= decompressed_size)
|
|
|
|
break;
|
|
|
|
while (compressed_reader.get_seek() % 4) {
|
|
|
|
compressed_reader.ffwd(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return decompressed_data;
|
|
|
|
}
|
|
|
|
|
2020-11-21 15:58:51 -05:00
|
|
|
} // namespace file_util
|