Commit 1124e169 authored by Vladimir Serbinenko's avatar Vladimir Serbinenko
Browse files

Migrate to VFS interface

parent 1d4dc49f
......@@ -34,3 +34,17 @@ SOURCES_C := \
$(GUI)/retro/SDL_gfxPrimitives.c \
$(GUI)/retro/retro_surface.c \
$(GUI)/app.c
ifneq ($(STATIC_LINKING), 1)
SOURCES_C += \
$(CORE_DIR)/libretro-common/compat/compat_strl.c \
$(CORE_DIR)/libretro-common/compat/fopen_utf8.c \
$(CORE_DIR)/libretro-common/encodings/encoding_utf.c \
$(CORE_DIR)/libretro-common/file/file_path.c \
$(CORE_DIR)/libretro-common/file/retro_dirent.c \
$(CORE_DIR)/libretro-common/time/rtime.c \
$(CORE_DIR)/libretro-common/streams/file_stream.c \
$(CORE_DIR)/libretro-common/string/stdstring.c \
$(CORE_DIR)/libretro-common/vfs/vfs_implementation.c
endif
......@@ -8,7 +8,9 @@
#include <features_cpu.h>
#endif
#include "libretro.h"
#include <libretro.h>
#include <streams/file_stream.h>
#include <retro_dirent.h>
#include "libretro-core.h"
#include "MACkeymap.h"
#include "vkbd.i"
......@@ -45,6 +47,7 @@ char slash = '/';
#endif
bool retro_load_ok = false;
struct retro_vfs_interface *vfs_interface;
char RETRO_DIR[512];
......@@ -117,15 +120,22 @@ static char CMDFILE[512];
int loadcmdfile(char *argv)
{
int res = 0;
FILE *fp = fopen(argv,"r");
if (fp)
{
if ( fgets (CMDFILE , 512 , fp) != NULL )
res=1;
fclose (fp);
memset(CMDFILE, 0, sizeof(CMDFILE));
RFILE *h = filestream_open(argv, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE);
char *p;
if (h) {
filestream_read(h, CMDFILE, sizeof(CMDFILE) - 1);
filestream_close(h);
res = 1;
}
p = strchr(CMDFILE, '\n');
if (p)
*p = '\0';
p = strchr(CMDFILE, '\r');
if (p)
*p = '\0';
return res;
}
......@@ -354,6 +364,15 @@ void retro_set_environment(retro_environment_t cb)
cb(RETRO_ENVIRONMENT_SET_SUPPORT_NO_GAME, &no_content);
cb(RETRO_ENVIRONMENT_SET_VARIABLES, variables);
struct retro_vfs_interface_info vfs_interface_info;
vfs_interface_info.required_interface_version = 3;
vfs_interface_info.iface = NULL;
if(cb(RETRO_ENVIRONMENT_GET_VFS_INTERFACE, &vfs_interface_info)) {
vfs_interface = vfs_interface_info.iface;
}
dirent_vfs_init(&vfs_interface_info);
filestream_vfs_init(&vfs_interface_info);
}
static void update_variables(void)
......
......@@ -105,6 +105,7 @@ extern int VIRTUAL_WIDTH;
extern int retrow ;
extern int retroh ;
extern int minivmac_statusbar;
extern struct retro_vfs_interface *vfs_interface;
//FUNCS
extern void mainloop_retro(void);
......
......@@ -36,7 +36,6 @@ int LOADCONTENT=-1;
int LDRIVE=8;
#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_IMPLEMENTATION
......
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include <retro_dirent.h>
/* ===============================================================
*
......@@ -45,6 +48,7 @@ die(const char *fmt, ...)
exit(EXIT_FAILURE);
}
#ifndef __LIBRETRO__
static char*
file_load(const char* path, size_t* siz)
{
......@@ -59,6 +63,7 @@ file_load(const char* path, size_t* siz)
fclose(fd);
return buf;
}
#endif
static char*
str_duplicate(const char *src)
......@@ -88,10 +93,9 @@ dir_list(const char *dir, int return_subdirs, size_t *count)
size_t n = 0;
char buffer[MAX_PATH_LEN];
char **results = NULL;
const DIR *none = NULL;
size_t capacity = 32;
size_t size;
DIR *z;
struct RDIR *zh = NULL;
assert(dir);
assert(count);
......@@ -103,24 +107,21 @@ dir_list(const char *dir, int return_subdirs, size_t *count)
size = 0;
z = opendir(dir);
if (z != none) {
zh = retro_opendir(dir);
if (zh != NULL) {
int nonempty = 1;
struct dirent *data = readdir(z);
nonempty = (data != NULL);
nonempty = retro_readdir(zh);
if (!nonempty) return NULL;
do {
DIR *y;
char *p;
int is_subdir;
if (data->d_name[0] == '.')
int is_subdir = 0;
const char *name = NULL;
name = retro_dirent_get_name(zh);
if (name[0] == '.')
continue;
strncpy(buffer + n, data->d_name, MAX_PATH_LEN-n);
y = opendir(buffer);
is_subdir = (y != NULL);
if (y != NULL) closedir(y);
is_subdir = retro_dirent_is_dir(zh, NULL);
if ((return_subdirs && is_subdir) || (!is_subdir && !return_subdirs)){
if (!size) {
......@@ -132,13 +133,12 @@ dir_list(const char *dir, int return_subdirs, size_t *count)
assert(results);
if (!results) free(old);
}
p = str_duplicate(data->d_name);
p = str_duplicate(name);
results[size++] = p;
}
} while ((data = readdir(z)) != NULL);
} while (zh != NULL && retro_readdir(zh));
}
if (z) closedir(z);
retro_closedir(zh);
*count = size;
return results;
}
......
......@@ -8,7 +8,6 @@
//#warning just an SDL wrapper for use SDL surface/maprgba in the core.
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
/* The number of elements in a table */
......
......@@ -29,6 +29,10 @@
#include <time.h>
#include <stdlib.h>
#include <retro_inline.h>
#include <libretro.h>
#include <streams/file_stream.h>
#include "libretro-core.h"
#include "CNFGRAPI.h"
#include "SYSDEPNS.h"
......@@ -238,7 +242,7 @@ LOCALVAR const ui3b Native2MacRomanTab[] = {
#endif
#if IncludePbufs
LOCALFUNC tMacErr NativeTextToMacRomanPbuf(char *x, tPbuf *r)
LOCALFUNC tMacErr NativeTextToMacRomanPbuf(const char *x, tPbuf *r)
{
if (NULL == x) {
return mnvm_miscErr;
......@@ -250,7 +254,7 @@ LOCALFUNC tMacErr NativeTextToMacRomanPbuf(char *x, tPbuf *r)
if (NULL == p) {
return mnvm_miscErr;
} else {
ui3b *p0 = (ui3b *)x;
const ui3b *p0 = (const ui3b *)x;
ui3b *p1 = (ui3b *)p;
int i;
......@@ -369,10 +373,7 @@ LOCALPROC NativeStrFromCStr(char *r, char *s)
#define NotAfileRef NULL
LOCALVAR FILE *Drives[NumDrives]; /* open disk image files */
#if IncludeSonyGetName || IncludeSonyNew
LOCALVAR char *DriveNames[NumDrives];
#endif
LOCALVAR RFILE *Drives[NumDrives]; /* open disk image files */
LOCALPROC InitDrives(void)
{
......@@ -384,9 +385,6 @@ LOCALPROC InitDrives(void)
for (i = 0; i < NumDrives; ++i) {
Drives[i] = NotAfileRef;
#if IncludeSonyGetName || IncludeSonyNew
DriveNames[i] = NULL;
#endif
}
}
......@@ -395,14 +393,14 @@ GLOBALFUNC tMacErr vSonyTransfer(blnr IsWrite, ui3p Buffer,
ui5r *Sony_ActCount)
{
tMacErr err = mnvm_miscErr;
FILE *refnum = Drives[Drive_No];
RFILE *refnum = Drives[Drive_No];
ui5r NewSony_Count = 0;
if (0 == fseek(refnum, Sony_Start, SEEK_SET)) {
if (filestream_seek(refnum, Sony_Start, RETRO_VFS_SEEK_POSITION_START) >= 0) {
if (IsWrite) {
NewSony_Count = fwrite(Buffer, 1, Sony_Count, refnum);
NewSony_Count = filestream_write(refnum, Buffer, Sony_Count);
} else {
NewSony_Count = fread(Buffer, 1, Sony_Count, refnum);
NewSony_Count = filestream_read(refnum, Buffer, Sony_Count);
}
if (NewSony_Count == Sony_Count) {
......@@ -420,42 +418,34 @@ GLOBALFUNC tMacErr vSonyTransfer(blnr IsWrite, ui3p Buffer,
GLOBALFUNC tMacErr vSonyGetSize(tDrive Drive_No, ui5r *Sony_Count)
{
tMacErr err = mnvm_miscErr;
FILE *refnum = Drives[Drive_No];
long v;
if (0 == fseek(refnum, 0, SEEK_END)) {
v = ftell(refnum);
if (v >= 0) {
*Sony_Count = v;
err = mnvm_noErr;
}
}
RFILE *refnum = Drives[Drive_No];
int64_t sz = filestream_get_size(refnum);
if (sz < 0)
return mnvm_miscErr;
return err; /*& figure out what really to return &*/
*Sony_Count = sz;
return mnvm_noErr;
}
LOCALFUNC tMacErr vSonyEject0(tDrive Drive_No, blnr deleteit)
{
FILE *refnum = Drives[Drive_No];
RFILE *refnum = Drives[Drive_No];
char *s = deleteit ? strdup(filestream_get_path(refnum)) : NULL;
DiskEjectedNotify(Drive_No);
fclose(refnum);
filestream_close(refnum);
Drives[Drive_No] = NotAfileRef; /* not really needed */
#if IncludeSonyGetName || IncludeSonyNew
{
char *s = DriveNames[Drive_No];
if (NULL != s) {
if (deleteit) {
if (NULL != s) {
if (deleteit) {
if (vfs_interface)
vfs_interface->remove(s);
else
remove(s);
}
free(s);
DriveNames[Drive_No] = NULL; /* not really needed */
}
free(s);
}
#endif
return mnvm_noErr;
}
......@@ -486,11 +476,11 @@ LOCALPROC UnInitDrives(void)
#if IncludeSonyGetName
GLOBALFUNC tMacErr vSonyGetName(tDrive Drive_No, tPbuf *r)
{
char *drivepath = DriveNames[Drive_No];
const char *drivepath = filestream_get_path(Drives[Drive_No]);
if (NULL == drivepath) {
return mnvm_miscErr;
} else {
char *s = strrchr(drivepath, '/');
const char *s = strrchr(drivepath, '/');
if (NULL == s) {
s = drivepath;
} else {
......@@ -501,7 +491,7 @@ GLOBALFUNC tMacErr vSonyGetName(tDrive Drive_No, tPbuf *r)
}
#endif
LOCALFUNC blnr Sony_Insert0(FILE *refnum, blnr locked,
LOCALFUNC blnr Sony_Insert0(RFILE *refnum, blnr locked,
char *drivepath)
{
tDrive Drive_No;
......@@ -517,24 +507,12 @@ LOCALFUNC blnr Sony_Insert0(FILE *refnum, blnr locked,
{
Drives[Drive_No] = refnum;
DiskInsertNotify(Drive_No, locked);
#if IncludeSonyGetName || IncludeSonyNew
{
ui5b L = strlen(drivepath);
char *p = malloc(L + 1);
if (p != NULL) {
(void) memcpy(p, drivepath, L + 1);
}
DriveNames[Drive_No] = p;
}
#endif
IsOk = trueblnr;
}
}
if (! IsOk) {
fclose(refnum);
filestream_close(refnum);
}
return IsOk;
......@@ -544,10 +522,10 @@ LOCALFUNC blnr Sony_Insert0(FILE *refnum, blnr locked,
{
blnr locked = falseblnr;
/* printf("Sony_Insert1 %s\n", drivepath); */
FILE *refnum = fopen(drivepath, "rb+");
RFILE *refnum = filestream_open(drivepath, RETRO_VFS_FILE_ACCESS_READ_WRITE | RETRO_VFS_FILE_ACCESS_UPDATE_EXISTING, RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (NULL == refnum) {
locked = trueblnr;
refnum = fopen(drivepath, "rb");
refnum = filestream_open(drivepath, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE);
}
if (NULL == refnum) {
if (! silentfail) {
......@@ -602,7 +580,7 @@ LOCALFUNC blnr LoadInitialImages(void)
}
#if IncludeSonyNew
LOCALFUNC blnr WriteZero(FILE *refnum, ui5b L)
LOCALFUNC blnr WriteZero(RFILE *refnum, ui5b L)
{
#define ZeroBufferSize 2048
ui5b i;
......@@ -612,7 +590,7 @@ LOCALFUNC blnr WriteZero(FILE *refnum, ui5b L)
while (L > 0) {
i = (L > ZeroBufferSize) ? ZeroBufferSize : L;
if (fwrite(buffer, 1, i, refnum) != i) {
if (filestream_write(refnum, buffer, i) != i) {
return falseblnr;
}
L -= i;
......@@ -625,7 +603,7 @@ LOCALFUNC blnr WriteZero(FILE *refnum, ui5b L)
LOCALPROC MakeNewDisk0(ui5b L, char *drivepath)
{
blnr IsOk = falseblnr;
FILE *refnum = fopen(drivepath, "wb+");
RFILE *refnum = filestream_open(drivepath, RETRO_VFS_FILE_ACCESS_READ_WRITE, RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (NULL == refnum) {
MacMsg(kStrOpenFailTitle, kStrOpenFailMessage, falseblnr);
} else {
......@@ -634,10 +612,13 @@ LOCALPROC MakeNewDisk0(ui5b L, char *drivepath)
refnum = NULL;
}
if (refnum != NULL) {
fclose(refnum);
filestream_close(refnum);
}
if (! IsOk) {
(void) remove(drivepath);
if (vfs_interface)
vfs_interface->remove(drivepath);
else
(void) remove(drivepath);
}
}
}
......@@ -691,16 +672,16 @@ LOCALVAR char *rom_path = NULL;
LOCALFUNC tMacErr LoadMacRomFrom(char *path)
{
tMacErr err;
FILE *ROM_File;
RFILE *ROM_File;
int File_Size;
ROM_File = fopen(path, "rb");
ROM_File = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (NULL == ROM_File) {
err = mnvm_fnfErr;
} else {
File_Size = fread(ROM, 1, kROM_Size, ROM_File);
File_Size = filestream_read(ROM_File, ROM, kROM_Size);
if (File_Size != kROM_Size) {
if (feof(ROM_File)) {
if (filestream_eof(ROM_File)) {
err = mnvm_eofErr;
} else {
err = mnvm_miscErr;
......@@ -708,7 +689,7 @@ LOCALFUNC tMacErr LoadMacRomFrom(char *path)
} else {
err = mnvm_noErr;
}
fclose(ROM_File);
filestream_close(ROM_File);
}
return err;
......@@ -829,6 +810,8 @@ LOCALFUNC blnr LoadMacRom(void)
#if UseActvCode
#error If you activate this please add vfs_interface support
#define ActvCodeFileName "act_1"
LOCALFUNC tMacErr ActvCodeFileLoad(ui3p p)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment