Skip to content

Commit

Permalink
Refactor Platform file functions to use vfs filestreams
Browse files Browse the repository at this point in the history
And remove all the previously hacked in paths for libretro since the
Platform file functions *just* works

This should also fix some files ending up in the wrong path
  • Loading branch information
Myaats committed Aug 11, 2019
1 parent 933ae89 commit 7fe9d50
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 110 deletions.
24 changes: 4 additions & 20 deletions src/NDS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ char platformDirSeparator = '/';

#ifdef __LIBRETRO__
#include <streams/file_stream_transforms.h>

extern char retro_base_directory[4096];
extern char retro_game_path[4096];
extern bool retro_firmware_status;
#endif

Expand Down Expand Up @@ -398,16 +395,9 @@ void Reset()
FILE* f;
u32 i;

#ifdef __LIBRETRO__
char path[2048];
sprintf(path, "%s%cbios9.bin", retro_base_directory, platformDirSeparator);
f = fopen(path, "rb");
if (f)
retro_firmware_status = true;
else
retro_firmware_status = false;
#else
f = Platform::OpenLocalFile("bios9.bin", "rb");
#ifdef __LIBRETRO__
f ? retro_firmware_status &= true : retro_firmware_status &= false;
#endif
LastSysClockCycles = 0;
if (!f)
Expand All @@ -426,15 +416,9 @@ void Reset()
fclose(f);
}

#ifdef __LIBRETRO__
sprintf(path, "%s%cbios7.bin", retro_base_directory, platformDirSeparator);
f = fopen(path, "rb");
if (f)
retro_firmware_status = true;
else
retro_firmware_status = false;
#else
f = Platform::OpenLocalFile("bios7.bin", "rb");
#ifdef __LIBRETRO__
f ? retro_firmware_status &= true : retro_firmware_status &= false;
#endif

if (!f)
Expand Down
9 changes: 5 additions & 4 deletions src/NDSCart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "Platform.h"

#ifdef __LIBRETRO__
#include "streams/file_stream_transforms.h"
#include "streams/memory_stream.h"
#include "romlist.h"
#endif
Expand Down Expand Up @@ -883,10 +884,10 @@ bool ReadROMParams(u32 gamecode, u32* params)
}

#ifdef __LIBRETRO__
#undef fclose
#undef fread
#undef fseek
#undef ftell
#define fclose rfclose
#define fread rfread
#define fseek rfseek
#define ftell rftell
#endif

bool LoadROM(const char* path, const char* sram, bool direct)
Expand Down
4 changes: 4 additions & 0 deletions src/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

#include "types.h"

#ifdef __LIBRETRO__
#include <streams/file_stream_transforms.h>
#endif

namespace Platform
{

Expand Down
19 changes: 5 additions & 14 deletions src/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "boolean.h"
#include <streams/file_stream_transforms.h>

extern char retro_base_directory[4096];
extern bool retro_firmware_status;
#endif

Expand Down Expand Up @@ -99,13 +98,9 @@ void Reset()
if (Firmware) delete[] Firmware;
Firmware = NULL;

FILE* f = Platform::OpenLocalFile("firmware.bin", "rb");
#ifdef __LIBRETRO__
char path[2047];
sprintf(path, "%s%cfirmware.bin", retro_base_directory, platformDirSeparator);
FILE* f = fopen(path, "rb");
f ? retro_firmware_status &= true : retro_firmware_status &= false;
#else
FILE* f = Platform::OpenLocalFile("firmware.bin", "rb");
#endif
if (!f)
{
Expand Down Expand Up @@ -144,16 +139,12 @@ void Reset()
fclose(f);

// take a backup
#ifdef __LIBRETRO__
sprintf(path, "%s%cfirmware.bin.bak", retro_base_directory, platformDirSeparator);
#else
sprintf(path, "firmware.bin.bak");
#endif
f = fopen(path, "rb");
const char* firmbkp = "firmware.bin.bak";
f = Platform::OpenLocalFile(firmbkp, "rb");
if (f) fclose(f);
else
{
f = fopen(path, "wb");
f = Platform::OpenLocalFile(firmbkp, "wb");
fwrite(Firmware, 1, FirmwareLength, f);
fclose(f);
}
Expand Down Expand Up @@ -344,7 +335,7 @@ void Write(u8 val, u32 hold)

if (!hold && (CurCmd == 0x02 || CurCmd == 0x0A))
{
FILE* f = fopen("firmware.bin", "r+b");
FILE* f = Platform::OpenLocalFile("firmware.bin", "r+b");
if (f)
{
u32 cutoff = 0x7FA00 & FirmwareMask;
Expand Down
84 changes: 12 additions & 72 deletions src/libretro/libretro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "SPU.h"
#include "libretro.h"
#include <streams/file_stream.h>
#include <streams/file_stream_transforms.h>
#include <streams/memory_stream.h>
#include <file/file_path.h>

Expand Down Expand Up @@ -133,9 +134,9 @@ static TouchMode current_touch_mode = TouchMode::Disabled;

static struct retro_log_callback logging;
static retro_log_printf_t log_cb;
char retro_base_directory[4096];
char retro_saves_directory[4096];
bool retro_firmware_status;
static char retro_base_directory[4096];
static char retro_saves_directory[4096];
bool retro_firmware_status = true;

std::string game_path;
std::string save_path;
Expand Down Expand Up @@ -208,7 +209,11 @@ FILE* OpenFile(const char* path, const char* mode, bool mustexist)
if (mustexist)
{
ret = fopen(path, "rb");
if (ret) ret = freopen(path, mode, ret);
if (ret)
{
fclose(ret);
ret = fopen(path, mode);
}
}
else
ret = fopen(path, mode);
Expand All @@ -218,74 +223,9 @@ FILE* OpenFile(const char* path, const char* mode, bool mustexist)

FILE* OpenLocalFile(const char* path, const char* mode)
{
bool relpath = false;
int pathlen = strlen(path);

#ifdef __WIN32__
if (pathlen > 3)
{
if (path[1] == ':' && path[2] == '\\')
return OpenFile(path, mode);
}
#else
if (pathlen > 1)
{
if (path[0] == '/')
return OpenFile(path, mode);
}
#endif

if (pathlen >= 3)
{
if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || path[2] == '\\'))
relpath = true;
}

int emudirlen = strlen(retro_base_directory);
char* emudirpath;
if (emudirlen)
{
int len = emudirlen + 1 + pathlen + 1;
emudirpath = new char[len];
strncpy(&emudirpath[0], retro_base_directory, emudirlen - 1);
emudirpath[emudirlen] = '/';
strncpy(&emudirpath[emudirlen+1], path, pathlen - 1);
emudirpath[emudirlen+1+pathlen] = '\0';
}
else
{
emudirpath = new char[pathlen+1];
strncpy(&emudirpath[0], path, pathlen - 1);
emudirpath[pathlen] = '\0';
}

// Locations are application directory, and AppData/melonDS on Windows or XDG_CONFIG_HOME/melonds on Linux

FILE* f;

// First check current working directory
f = OpenFile(path, mode, true);
if (f) { delete[] emudirpath; return f; }

// then emu directory
f = OpenFile(emudirpath, mode, true);
if (f) { delete[] emudirpath; return f; }

if (!relpath)
{
std::string fullpath = std::string(retro_base_directory) + "/" + path;
f = OpenFile(fullpath.c_str(), mode, true);
if (f) { delete[] emudirpath; return f; }
}

if (mode[0] != 'r')
{
f = OpenFile(emudirpath, mode);
if (f) { delete[] emudirpath; return f; }
}

delete[] emudirpath;
return NULL;
std::string fullpath = std::string(retro_base_directory) + std::string(1, platformDirSeparator) + std::string(path);
FILE* f = OpenFile(fullpath.c_str(), mode, true);
return f;
}

void StopEmu()
Expand Down

0 comments on commit 7fe9d50

Please sign in to comment.