Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Linux/macOS: allow storage of game data in XDG standard path
  • Loading branch information
Tatsh committed Oct 5, 2020
1 parent c31e3e6 commit 1c2a3cfc4d42aadbe7c090b78a6e8ed8ab9649c1
Showing with 68 additions and 3 deletions.
  1. +15 −1 src/core/CdStreamPosix.cpp
  2. +47 −2 src/core/FileMgr.cpp
  3. +3 −0 src/core/FileMgr.h
  4. +3 −0 src/core/config.h
@@ -17,6 +17,9 @@
#include "CdStream.h"
#include "rwcore.h"
#include "RwHelper.h"
#ifdef XDG_ROOT
#include "FileMgr.h"
#endif

#define CDDEBUG(f, ...) debug ("%s: " f "\n", "cdvd_stream", ## __VA_ARGS__)
#define CDTRACE(f, ...) printf("%s: " f "\n", "cdvd_stream", ## __VA_ARGS__)
@@ -139,12 +142,23 @@ CdStreamInitThread(void)
#endif
}

static const char *gta3ImgPath = "models/gta3.img";

void
CdStreamInit(int32 numChannels)
{
struct statvfs fsInfo;
char imgPath[128] = {'\0'};

if((statvfs("models/gta3.img", &fsInfo)) < 0)
#ifdef XDG_ROOT
const char *rootDir = CFileMgr::GetRootDirName();
strncpy(imgPath, rootDir, strlen(rootDir) - 1);
strcat(imgPath, "/");
strcat(imgPath, gta3ImgPath);
#else
strcpy(imgPath, gta3ImgPath);
#endif
if((statvfs(imgPath, &fsInfo)) < 0)
{
CDTRACE("can't get filesystem info");
ASSERT(0);
@@ -197,11 +197,45 @@ myfeof(int fd)
char CFileMgr::ms_rootDirName[128] = {'\0'};
char CFileMgr::ms_dirName[128];

#ifdef XDG_ROOT
void CFileMgr::GetHomeDirectory(char *homeDir) {
char **p = environ;
while (*p++)
{
if (!strncmp("HOME=", *p, 5)) {
strcpy(homeDir, *p + 5);
break;
}
}
}
#endif

void
CFileMgr::Initialise(void)
{
#ifdef XDG_ROOT
char homeDir[255];
bzero(homeDir, 255);
GetHomeDirectory(homeDir);
// Build up ${HOME}/.local/share/re3
strcat(homeDir, "/.local");
struct stat buf;
if (stat(homeDir, &buf) < 0) {
assert(mkdir(homeDir, 0755) == 0);
}
strcat(homeDir, "/share");
if (stat(homeDir, &buf) < 0) {
assert(mkdir(homeDir, 0755) == 0);
}
strcat(homeDir, "/re3");
if (stat(homeDir, &buf) < 0) {
assert(mkdir(homeDir, 0755) == 0);
}
strcpy(ms_rootDirName, homeDir);
#else
_getcwd(ms_rootDirName, 128);
strcat(ms_rootDirName, "\\");
#endif
strcat(ms_rootDirName, "\\");
}

void
@@ -264,7 +298,18 @@ CFileMgr::LoadFile(const char *file, uint8 *buf, int unused, const char *mode)
int
CFileMgr::OpenFile(const char *file, const char *mode)
{
return myfopen(file, mode);
#ifdef XDG_ROOT
char fixedPath[128] = {'\0'};
bzero(fixedPath, 128);
if (ms_dirName[0] == '\0') {
strcpy(ms_dirName, ms_rootDirName);
}
strncpy(fixedPath, ms_dirName, strlen(ms_dirName));
strcat(fixedPath, file);
return myfopen(fixedPath, mode);
#else
return myfopen(file, mode);
#endif
}

int
@@ -20,4 +20,7 @@ class CFileMgr
static int CloseFile(int fd);
static int GetErrorReadWrite(int fd);
static char *GetRootDirName() { return ms_rootDirName; }
#ifdef XDG_ROOT
static void GetHomeDirectory(char *homeDir);
#endif
};
@@ -311,3 +311,6 @@ enum Config {
#define PC_PARTICLE
#define VC_PED_PORTS // To not process collisions always. But should be tested if that's really beneficial
#endif

// Store GTA 3 files in ${HOME}/.local/share/re3
// #define XDG_ROOT

0 comments on commit 1c2a3cf

Please sign in to comment.