Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions source/abstract/File.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "File.hh"

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>

namespace Abstract::File {
Expand All @@ -11,10 +14,52 @@ u8 *Load(const char *path, size_t &size) {
path++;
}

snprintf(filepath, sizeof(filepath), "./%s", path);
const char *root = std::getenv("KINOKO_FILESYSTEM_ROOT");

if (root && root[0] != '\0') {
// path relative to KINOKO_FILESYSTEM_ROOT
#ifdef _WIN32
const char separator = '\\';
#else
const char separator = '/';
#endif

size_t rootLen = std::strlen(root);
bool rootEndsWithSep = root[rootLen - 1] == separator;

if (rootEndsWithSep) {
std::snprintf(filepath, sizeof(filepath), "%s%s", root, path);
} else {
std::snprintf(filepath, sizeof(filepath), "%s%c%s", root, separator, path);
}
} else {
// Path relative to current working directory
std::snprintf(filepath, sizeof(filepath), "./%s", path);
}

std::ifstream file(filepath, std::ios::binary);
if (!file) {
PANIC("Failed to load file %s! (Was KINOKO_FILESYSTEM_ROOT specified properly?)", path);
}

file.seekg(0, std::ios::end);
size = file.tellg();
file.seekg(0, std::ios::beg);

u8 *buffer = new u8[size];
file.read(reinterpret_cast<char *>(buffer), size);

return buffer;
}

u8 *LoadHost(const char *path, size_t &size) {
char filepath[256];

std::snprintf(filepath, sizeof(filepath), "./%s", path);

std::ifstream file(filepath, std::ios::binary);
if (!file) {
PANIC("File with provided path %s was not loaded correctly!", path);
PANIC("Failed to load file %s!", path);
}

file.seekg(0, std::ios::end);
Expand Down
3 changes: 3 additions & 0 deletions source/abstract/File.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace Abstract::File {

/// @brief Loads kinoko game files
[[nodiscard]] u8 *Load(const char *path, size_t &size);
/// @brief Loads files from host system
[[nodiscard]] u8 *LoadHost(const char *path, size_t &size);
void Append(const char *path, const char *data, size_t size);
int Remove(const char *path);

Expand Down
3 changes: 2 additions & 1 deletion source/host/KReplaySystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ void KReplaySystem::parseOptions(int argc, char **argv) {
ASSERT(i + 1 < argc);

m_currentGhostFileName = argv[++i];
m_currentRawGhost = Abstract::File::Load(m_currentGhostFileName, m_currentRawGhostSize);
m_currentRawGhost =
Abstract::File::LoadHost(m_currentGhostFileName, m_currentRawGhostSize);

if (m_currentRawGhostSize < System::RKG_HEADER_SIZE ||
m_currentRawGhostSize > sizeof(System::RawGhostFile)) {
Expand Down
6 changes: 3 additions & 3 deletions source/host/KTestSystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void KTestSystem::parseOptions(int argc, char **argv) {
ASSERT(i + 1 < argc);

size_t size;
u8 *data = Abstract::File::Load(argv[++i], size);
u8 *data = Abstract::File::LoadHost(argv[++i], size);

if (size == 0) {
PANIC("Failed to load suite data!");
Expand Down Expand Up @@ -258,7 +258,7 @@ void KTestSystem::startNextTestCase() {
constexpr u32 KRKG_SIGNATURE = 0x4b524b47; // KRKG

size_t size;
u8 *krkg = Abstract::File::Load(getCurrentTestCase().krkgPath.data(), size);
u8 *krkg = Abstract::File::LoadHost(getCurrentTestCase().krkgPath.data(), size);
m_stream = EGG::RamStream(krkg, static_cast<u32>(size));
m_currentFrame = -1;
m_sync = true;
Expand Down Expand Up @@ -463,7 +463,7 @@ const KTestSystem::TestCase &KTestSystem::getCurrentTestCase() const {
/// @param arg Unused optional argument.
void KTestSystem::OnInit(System::RaceConfig *config, void * /* arg */) {
size_t size;
u8 *rkg = Abstract::File::Load(Instance()->getCurrentTestCase().rkgPath.data(), size);
u8 *rkg = Abstract::File::LoadHost(Instance()->getCurrentTestCase().rkgPath.data(), size);
config->setGhost(rkg);
delete[] rkg;

Expand Down