jak-project/common/util/FileUtil.cpp
Tyler Wilding 7b6d732a77
goalc: Add TCP server socket in REPL process (#1335)
* goalc: cleanup goalc's main method and add nrepl listener socket

* deps: add standalone ASIO for sockets

* lint: formatting

* common: make a common interface for creating a server socket

* goalc: setup new repl server

* deps: remove asio

* goalc: debug issues, nrepl is working again

* git: rename files

* attempt to fix linux function call

* test

* scripts: make the error message even more obvious....

* goalc: make suggested changes, still can't reconnect properly

* game: pull out single-client logic from XSocketServer

* nrepl: supports multiple clients and disconnection/reconnects

* goalc: some minor fixes for tests

* goalc: save repl history when the compiler reloads

* common: add include for linux networking

* a few small changes to fix tests

* is it the assert?

* change thread start order and add a print to an assert

Co-authored-by: water <awaterford111445@gmail.com>
2022-05-06 18:19:37 -04:00

475 lines
13 KiB
C++

/*!
* @file FileUtil.cpp
* Utility functions for reading and writing files.
*/
#include "FileUtil.h"
#include <iostream>
#include <filesystem>
#include <cstdio> /* defines FILENAME_MAX */
#include <fstream>
#include <sstream>
#include <cstdlib>
#include "common/util/BinaryReader.h"
#include "BinaryWriter.h"
#include "common/common_types.h"
// 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
#include "third-party/fpng/fpng.cpp"
#include "third-party/fpng/fpng.h"
#include "third-party/fmt/core.h"
#include "third-party/lzokay/lzokay.hpp"
#ifdef _WIN32
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#else
#include <unistd.h>
#include <cstring>
#endif
#include "common/util/Assert.h"
namespace file_util {
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
}
std::filesystem::path get_user_game_dir() {
// TODO - i anticipate UTF-8 problems on windows with our current FS api
return get_user_home_dir() / "OpenGOAL";
}
std::filesystem::path get_user_settings_dir() {
return get_user_game_dir() / "jak1" / "settings";
}
std::filesystem::path get_user_memcard_dir() {
return get_user_game_dir() / "jak1" / "saves";
}
struct {
bool initialized = false;
std::filesystem::path path_to_data;
} gFilePathInfo;
/*!
* Get the path to the current executable.
*/
std::string get_current_executable_path() {
#ifdef _WIN32
char buffer[FILENAME_MAX];
GetModuleFileNameA(NULL, buffer, FILENAME_MAX);
return std::string(buffer);
#else
// do Linux stuff
char buffer[FILENAME_MAX + 1];
auto len = readlink("/proc/self/exe", buffer,
FILENAME_MAX); // /proc/self acts like a "virtual folder" containing
// information about the current process
buffer[len] = '\0';
return std::string(buffer);
#endif
}
/*!
* See if the current executable is somewhere in jak-project/. If so, return the path to jak-project
*/
std::optional<std::string> try_get_jak_project_path() {
std::string my_path = get_current_executable_path();
std::string::size_type pos =
std::string(my_path).rfind("jak-project"); // Strip file path down to /jak-project/ directory
if (pos == std::string::npos) {
return {};
}
return std::string(my_path).substr(
0, pos + 11); // + 12 to include "/jak-project" in the returned filepath
}
std::optional<std::filesystem::path> try_get_data_dir() {
std::filesystem::path my_path = get_current_executable_path();
auto data_dir = my_path.parent_path() / "data";
if (std::filesystem::exists(data_dir) && std::filesystem::is_directory(data_dir)) {
return data_dir;
} else {
return {};
}
}
bool setup_project_path(std::optional<std::filesystem::path> project_path_override) {
if (gFilePathInfo.initialized) {
return true;
}
if (project_path_override) {
gFilePathInfo.path_to_data = *project_path_override;
gFilePathInfo.initialized = true;
fmt::print("Using explicitly set project path: {}\n", project_path_override->string());
return true;
}
auto data_path = try_get_data_dir();
if (data_path) {
gFilePathInfo.path_to_data = *data_path;
gFilePathInfo.initialized = true;
fmt::print("Using data path: {}\n", data_path->string());
return true;
}
auto development_repo_path = try_get_jak_project_path();
if (development_repo_path) {
gFilePathInfo.path_to_data = *development_repo_path;
gFilePathInfo.initialized = true;
fmt::print("Using development repo path: {}\n", *development_repo_path);
return true;
}
fmt::print("Failed to initialize project path.\n");
return false;
}
std::filesystem::path get_jak_project_dir() {
ASSERT(gFilePathInfo.initialized);
return gFilePathInfo.path_to_data;
}
std::string get_file_path(const std::vector<std::string>& input) {
// TODO - clean this behaviour up, it causes unexpected behaviour when working with files
// the project path should be explicitly provided by whatever if needed
// TEMP HACK
// - if the provided path is absolute, don't add the project path
if (input.size() == 1 && std::filesystem::path(input.at(0)).is_absolute()) {
return input.at(0);
}
auto current_path = file_util::get_jak_project_dir();
for (auto& str : input) {
current_path /= str;
}
return current_path.string();
}
bool create_dir_if_needed(const std::string& path) {
if (!std::filesystem::is_directory(path)) {
std::filesystem::create_directories(path);
return true;
}
return false;
}
bool create_dir_if_needed_for_file(const std::string& path) {
return std::filesystem::create_directories(std::filesystem::path(path).parent_path());
}
void write_binary_file(const std::string& name, const void* data, size_t size) {
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) {
fclose(fp);
throw std::runtime_error("couldn't write file " + name);
}
fclose(fp);
}
void write_rgba_png(const std::string& name, void* data, int w, int h) {
auto flags = 0;
auto ok = fpng::fpng_encode_image_to_file(name.c_str(), data, w, h, 4, flags);
if (!ok) {
throw std::runtime_error("couldn't save png file " + name);
}
}
void write_text_file(const std::string& file_name, const std::string& text) {
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);
}
std::vector<uint8_t> read_binary_file(const std::string& filename) {
// 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));
}
auto fp = fopen(filename.c_str(), "rb");
if (!fp)
throw std::runtime_error("File " + filename +
" cannot be opened: " + std::string(strerror(errno)));
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) {
fclose(fp);
throw std::runtime_error("File " + filename + " cannot be read");
}
fclose(fp);
return data;
}
std::string read_text_file(const std::string& path) {
std::ifstream file(path);
if (!file.good()) {
throw std::runtime_error("couldn't open " + path);
}
std::stringstream ss;
ss << file.rdbuf();
return ss.str();
}
bool is_printable_char(char c) {
return c >= ' ' && c <= '~';
}
std::string combine_path(const std::string& parent, const std::string& child) {
return parent + "/" + child;
}
bool file_exists(const std::string& path) {
return std::filesystem::exists(path);
}
std::string base_name(const std::string& filename) {
size_t pos = 0;
ASSERT(!filename.empty());
for (size_t i = filename.size() - 1; i-- > 0;) {
if (filename.at(i) == '/' || filename.at(i) == '\\') {
pos = (i + 1);
break;
}
}
return filename.substr(pos);
}
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] >= 'a' && dst[i] <= 'z') {
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;
}
void assert_file_exists(const char* path, const char* error_message) {
if (!std::filesystem::exists(path)) {
ASSERT_MSG(false, fmt::format("File {} was not found: {}", path, error_message));
}
}
/*!
* 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);
ASSERT(ok == lzokay::EResult::Success);
compressed_reader.ffwd(chunk_size);
output_offset += bytes_written;
} else {
// nope - sometimes chunk_size is bigger than MAX, but we should still use max.
// ASSERT(chunk_size == MAX_CHUNK_SIZE);
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;
}
} // namespace file_util