Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reuse code to read files, and let autodetect where look for #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
31 changes: 8 additions & 23 deletions libmod/source/modplayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ static int m_TrackDat_num;
static TrackData *m_TrackDat; // Stores info for each track being played
static RowData *m_CurrentRow; // Pointer to the current row being played
static int m_bPlaying; // Set to true when a mod is being played
static u8 *data;
int size = 0;
static u8 *data = NULL;

//////////////////////////////////////////////////////////////////////
// These are the public functions
//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -183,8 +183,7 @@ void Mod_FreeTune()

// Tear down all the mallocs done
//free the file itself
if (data)
free(data);
if (data) free(data), data = NULL;
// Free patterns
for (i = 0; i < m_Patterns_num; i++) {
for (row = 0; row < 64; row++)
Expand Down Expand Up @@ -251,25 +250,11 @@ int Mod_Load(char *filename)
int index = 0;
int numsamples;
char modname[21];
int fd;
if ((fd = ps4LinkOpen(filename, O_RDONLY, 0)) > 0) {
// opened file, so get size now
size = ps4LinkLseek(fd, 0, SEEK_END);
ps4LinkLseek(fd, 0, SEEK_SET);
data = (unsigned char *) malloc(size + 8);
memset(data, 0, size + 8);
if (data != 0) { // Read file in
ps4LinkRead(fd, data, size);
} else {
printf("Error allocing\n");
ps4LinkClose(fd);
return 0;
}
// Close file
ps4LinkClose(fd);
} else { //if we couldn't open the file
return 0;
}

// allocate 8 bytes more than readed
data = DataFromFile(filename, 8);
if(!data)
return 0;

//BPM_RATE = 130;
BPM_RATE = 125; //PAL
Expand Down
36 changes: 2 additions & 34 deletions liborbis2d/source/orbis2dImagePng.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,41 +163,9 @@ Orbis2dTexture *orbis2dLoadPngFromHost(const char *path)

Orbis2dTexture *orbis2dLoadPngFromHost_v2(const char *path)
{
int fd; // descriptor to manage file from host0
int filesize; // variable to control file size
uint8_t *buf=NULL; // buffer for read from host0 file

// we open file in read only from host0 ps4sh include the full path with host0:/.......
fd=ps4LinkOpen(path,O_RDONLY,0);

if(fd<0) //If we can't open file from host0 print the error and return
{
debugNetPrintf(DEBUG,"[PS4LINK] ps4LinkOpen returned error %d\n",fd);
return NULL;
}
filesize=ps4LinkLseek(fd,0,SEEK_END); // Seek to end to get file size
if(filesize<0) // If we get an error print it and return
{
debugNetPrintf(DEBUG,"[PS4LINK] ps4LinkSeek returned error %d\n",fd);
ps4LinkClose(fd);
return NULL;
}
uint8_t *buf=DataFromFile(path, 0);

ps4LinkLseek(fd,0,SEEK_SET); // Seek back to start
buf=malloc(filesize); // Reserve memory for read buffer
if(!buf)
return NULL;

int numread=ps4LinkRead(fd,buf,filesize); //Read filsesize bytes to buf
ps4LinkClose(fd); //Close file

if(numread!=filesize) //if we don't get filesize bytes we are in trouble
{
debugNetPrintf(DEBUG,"[PS4LINK] ps4LinkRead returned error %d\n",numread);
return NULL;
}

return orbis2dLoadPngFromBuffer(buf); //create png from buf
return orbis2dLoadPngFromBuffer(buf); // create png from buf
}

// uses standard open/lseek/read/close to access sandbox'ed content
Expand Down
1 change: 1 addition & 0 deletions libps4link/include/ps4link.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ typedef struct OrbisDirEntry
char name[256];
}OrbisDirEntry;

uint8_t *DataFromFile(const char *path, int additional_size);

int ps4LinkOpen(const char *file, int flags, int mode);
int ps4LinkClose(int fd);
Expand Down
68 changes: 67 additions & 1 deletion libps4link/source/ps4link.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <net.h>
#include <sys/socket.h>
#include <debugnet.h>

#include <sys/fcntl.h>


#include "ps4link_internal.h"
Expand Down Expand Up @@ -317,3 +317,69 @@ void ps4LinkFinish()
debugNetFinish();
}
}

/*
uses standard open/lseek/read/close to access sandbox'ed content,
if patch starts with host0:/... , file will be readed via ps4link
*/
uint8_t *DataFromFile(const char *path, int additional_size)
{
int fd; // descriptor to manage file from /mnt/sanbox/...
int filesize = 0; // variable to control file size
int numread = 0;
int use_host = 0;
uint8_t *buf = NULL; // buffer for read from file


if(!memcmp(path, "host0:", 6)) use_host=1; // set flag

debugNetPrintf(INFO,"DataFromFile(%s), use_host:%d, add:%d\n",path,use_host,additional_size);

// we open file in read only
if(use_host) fd=ps4LinkOpen(path,O_RDONLY,0);
else fd=open(path,O_RDONLY);

if(fd<0) //If we can't open file, print the error and return
{
debugNetPrintf(DEBUG,"open file returned error %d\n",fd);
return NULL;
}

// Seek to end to get file size
if(use_host) filesize=ps4LinkLseek(fd,0,SEEK_END);
else filesize=lseek(fd,0,SEEK_END);

if(filesize<0) // If we get an error print it and return
{
debugNetPrintf(DEBUG,"lseek returned error %d\n",fd);
if(buf) free(buf), buf = NULL;
goto end;
}

buf=malloc(filesize + additional_size); // Reserve memory for read buffer
if(!buf)
return NULL;

memset(buf, 0, filesize + additional_size); // wipe

// Seek back to start and read filsesize bytes to buf
if(use_host) {
ps4LinkLseek(fd,0,SEEK_SET);
numread=ps4LinkRead(fd,buf,filesize);
} else {
lseek(fd,0,SEEK_SET);
numread=read(fd,buf,filesize);
}

end:
if(use_host) ps4LinkClose(fd);
else close(fd);

if(numread!=filesize) // if we don't get filesize bytes we are in trouble
{
debugNetPrintf(DEBUG,"read returned error %d\n",numread);
if(buf) free(buf), buf = NULL;
}

return buf; // remember to free()
}