2008-05-26 15:29:21 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define _CRT_SECURE_NO_DEPRECATE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <io.h>
|
|
|
|
|
|
|
|
#include <foobar2000.h>
|
2017-12-31 04:15:34 +01:00
|
|
|
#include <ATLHelpers/ATLHelpersLean.h>
|
2008-05-26 15:29:21 +02:00
|
|
|
#include <shared.h>
|
|
|
|
|
|
|
|
extern "C" {
|
2021-09-05 17:53:47 +02:00
|
|
|
#include "../src/streamfile.h"
|
|
|
|
//#include "../src/vgmstream.h"
|
2008-05-26 15:29:21 +02:00
|
|
|
#include "../src/util.h"
|
|
|
|
}
|
|
|
|
#include "foo_vgmstream.h"
|
|
|
|
|
|
|
|
|
2017-08-13 22:26:40 +02:00
|
|
|
/* a STREAMFILE that operates via foobar's file service using a buffer */
|
2018-08-24 18:48:42 +02:00
|
|
|
typedef struct {
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE vt; /* callbacks */
|
2018-08-24 18:48:42 +02:00
|
|
|
|
2019-09-15 15:47:41 +02:00
|
|
|
bool m_file_opened; /* if foobar IO service opened the file */
|
2018-08-24 18:48:42 +02:00
|
|
|
service_ptr_t<file> m_file; /* foobar IO service */
|
|
|
|
abort_callback * p_abort; /* foobar error stuff */
|
2021-09-05 17:53:47 +02:00
|
|
|
char* name; /* IO filename */
|
|
|
|
offv_t offset; /* last read offset (info) */
|
|
|
|
offv_t buf_offset; /* current buffer data start */
|
|
|
|
uint8_t* buf; /* data buffer */
|
|
|
|
size_t buf_size; /* max buffer size */
|
|
|
|
size_t valid_size; /* current buffer size */
|
|
|
|
size_t file_size; /* buffered file size */
|
2018-08-24 18:48:42 +02:00
|
|
|
} FOO_STREAMFILE;
|
2017-08-13 22:26:40 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static STREAMFILE* open_foo_streamfile_buffer(const char* const filename, size_t buf_size, abort_callback* p_abort, t_filestats* stats);
|
|
|
|
static STREAMFILE* open_foo_streamfile_buffer_by_file(service_ptr_t<file> m_file, bool m_file_opened, const char* const filename, size_t buf_size, abort_callback* p_abort);
|
2017-08-13 22:26:40 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static size_t foo_read(FOO_STREAMFILE* sf, uint8_t* dst, offv_t offset, size_t length) {
|
|
|
|
size_t read_total = 0;
|
2018-08-24 18:48:42 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
if (!sf || !sf->m_file_opened || !dst || length <= 0 || offset < 0)
|
2018-08-24 18:48:42 +02:00
|
|
|
return 0;
|
2008-05-26 15:29:21 +02:00
|
|
|
|
2017-04-29 16:03:11 +02:00
|
|
|
/* is the part of the requested length in the buffer? */
|
2021-09-05 17:53:47 +02:00
|
|
|
if (offset >= sf->buf_offset && offset < sf->buf_offset + sf->valid_size) {
|
|
|
|
size_t buf_limit;
|
|
|
|
int buf_into = (int)(offset - sf->buf_offset);
|
|
|
|
|
|
|
|
buf_limit = sf->valid_size - buf_into;
|
|
|
|
if (buf_limit > length)
|
|
|
|
buf_limit = length;
|
|
|
|
|
|
|
|
memcpy(dst, sf->buf + buf_into, buf_limit);
|
|
|
|
read_total += buf_limit;
|
|
|
|
length -= buf_limit;
|
|
|
|
offset += buf_limit;
|
|
|
|
dst += buf_limit;
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 18:48:42 +02:00
|
|
|
|
2017-04-29 16:03:11 +02:00
|
|
|
/* read the rest of the requested length */
|
|
|
|
while (length > 0) {
|
2021-09-05 17:53:47 +02:00
|
|
|
size_t buf_limit;
|
2018-08-24 18:48:42 +02:00
|
|
|
|
|
|
|
/* ignore requests at EOF */
|
2021-09-05 17:53:47 +02:00
|
|
|
if (offset >= sf->file_size) {
|
|
|
|
//offset = sf->file_size; /* seems fseek doesn't clamp offset */
|
|
|
|
//VGM_ASSERT_ONCE(offset > sf->file_size, "STDIO: reading over file_size 0x%x @ 0x%lx + 0x%x\n", sf->file_size, offset, length);
|
2018-08-24 18:48:42 +02:00
|
|
|
break;
|
2017-01-15 20:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* position to new offset */
|
2010-02-26 08:45:06 +01:00
|
|
|
try {
|
2021-09-05 17:53:47 +02:00
|
|
|
sf->m_file->seek(offset, *sf->p_abort);
|
2017-01-15 20:58:47 +01:00
|
|
|
} catch (...) {
|
2018-08-24 18:48:42 +02:00
|
|
|
break; /* this shouldn't happen in our code */
|
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
/* fill the buffer (offset now is beyond buf_offset) */
|
2018-08-24 18:48:42 +02:00
|
|
|
try {
|
2021-09-05 17:53:47 +02:00
|
|
|
sf->buf_offset = offset;
|
|
|
|
sf->valid_size = sf->m_file->read(sf->buf, sf->buf_size, *sf->p_abort);
|
2018-08-24 18:48:42 +02:00
|
|
|
} catch(...) {
|
|
|
|
break; /* improbable? */
|
2017-01-15 20:58:47 +01:00
|
|
|
}
|
2008-05-26 15:29:21 +02:00
|
|
|
|
|
|
|
/* decide how much must be read this time */
|
2021-09-05 17:53:47 +02:00
|
|
|
if (length > sf->buf_size)
|
|
|
|
buf_limit = sf->buf_size;
|
2017-04-29 16:03:11 +02:00
|
|
|
else
|
2021-09-05 17:53:47 +02:00
|
|
|
buf_limit = length;
|
2008-05-26 15:29:21 +02:00
|
|
|
|
2018-08-24 18:48:42 +02:00
|
|
|
/* give up on partial reads (EOF) */
|
2021-09-05 17:53:47 +02:00
|
|
|
if (sf->valid_size < buf_limit) {
|
|
|
|
memcpy(dst, sf->buf, sf->valid_size);
|
|
|
|
offset += sf->valid_size;
|
|
|
|
read_total += sf->valid_size;
|
2018-08-24 18:48:42 +02:00
|
|
|
break;
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* use the new buffer */
|
2021-09-05 17:53:47 +02:00
|
|
|
memcpy(dst, sf->buf, buf_limit);
|
|
|
|
offset += buf_limit;
|
|
|
|
read_total += buf_limit;
|
|
|
|
length -= buf_limit;
|
|
|
|
dst += buf_limit;
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
sf->offset = offset; /* last fread offset */
|
|
|
|
return read_total;
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static size_t foo_get_size(FOO_STREAMFILE* sf) {
|
|
|
|
return sf->file_size;
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static offv_t foo_get_offset(FOO_STREAMFILE* sf) {
|
|
|
|
return sf->offset;
|
2018-08-24 18:48:42 +02:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static void foo_get_name(FOO_STREAMFILE* sf, char* name, size_t name_size) {
|
|
|
|
/* Most crap only cares about the filename itself */
|
|
|
|
size_t ourlen = strlen(sf->name);
|
|
|
|
if (ourlen > name_size) {
|
|
|
|
if (name_size) strcpy(name, sf->name + ourlen - name_size + 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strcpy(name, sf->name);
|
|
|
|
}
|
2018-08-24 18:48:42 +02:00
|
|
|
}
|
2021-09-05 17:53:47 +02:00
|
|
|
static void foo_close(FOO_STREAMFILE* sf) {
|
|
|
|
sf->m_file.release(); //release alloc'ed ptr
|
|
|
|
free(sf->name);
|
|
|
|
free(sf->buf);
|
|
|
|
free(sf);
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static STREAMFILE* foo_open(FOO_STREAMFILE* sf, const char* const filename,size_t buf_size) {
|
2017-01-15 20:58:47 +01:00
|
|
|
service_ptr_t<file> m_file;
|
2008-05-26 15:29:21 +02:00
|
|
|
|
|
|
|
if (!filename)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// if same name, duplicate the file pointer we already have open
|
2021-09-05 17:53:47 +02:00
|
|
|
if (sf->m_file_opened && !strcmp(sf->name,filename)) {
|
|
|
|
m_file = sf->m_file; //copy?
|
2018-08-24 18:48:42 +02:00
|
|
|
{
|
2021-09-05 17:53:47 +02:00
|
|
|
STREAMFILE* new_sf = open_foo_streamfile_buffer_by_file(m_file, sf->m_file_opened, filename, buf_size, sf->p_abort);
|
|
|
|
if (new_sf) {
|
|
|
|
return new_sf;
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
|
|
|
// failure, close it and try the default path (which will probably fail a second time)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-15 15:47:41 +02:00
|
|
|
// a normal open, open a new file
|
2021-09-05 17:53:47 +02:00
|
|
|
return open_foo_streamfile_buffer(filename,buf_size,sf->p_abort,NULL);
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static STREAMFILE* open_foo_streamfile_buffer_by_file(service_ptr_t<file> m_file, bool m_file_opened, const char* const filename, size_t buf_size, abort_callback* p_abort) {
|
|
|
|
uint8_t* buf;
|
|
|
|
FOO_STREAMFILE* this_sf;
|
2008-05-26 15:29:21 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
buf = (uint8_t*) calloc(buf_size, sizeof(uint8_t));
|
|
|
|
if (!buf) goto fail;
|
2008-05-26 15:29:21 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf = (FOO_STREAMFILE*) calloc(1, sizeof(FOO_STREAMFILE));
|
|
|
|
if (!this_sf) goto fail;
|
2008-05-26 15:29:21 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->vt.read = (size_t (__cdecl *)(_STREAMFILE*, uint8_t*, offv_t, size_t)) foo_read;
|
|
|
|
this_sf->vt.get_size = (size_t (__cdecl *)(_STREAMFILE*)) foo_get_size;
|
|
|
|
this_sf->vt.get_offset = (offv_t (__cdecl *)(_STREAMFILE*)) foo_get_offset;
|
|
|
|
this_sf->vt.get_name = (void (__cdecl *)(_STREAMFILE*, char*, size_t)) foo_get_name;
|
|
|
|
this_sf->vt.open = (_STREAMFILE* (__cdecl *)(_STREAMFILE* ,const char* const, size_t)) foo_open;
|
|
|
|
this_sf->vt.close = (void (__cdecl *)(_STREAMFILE* )) foo_close;
|
2008-05-26 15:29:21 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->m_file_opened = m_file_opened;
|
|
|
|
this_sf->m_file = m_file;
|
|
|
|
this_sf->p_abort = p_abort;
|
|
|
|
this_sf->buf_size = buf_size;
|
|
|
|
this_sf->buf = buf;
|
2008-05-26 15:29:21 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->name = strdup(filename);
|
|
|
|
if (!this_sf->name) goto fail;
|
2017-01-15 20:58:47 +01:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
/* cache file_size */
|
|
|
|
if (this_sf->m_file_opened)
|
|
|
|
this_sf->file_size = this_sf->m_file->get_size(*this_sf->p_abort);
|
2019-09-15 15:47:41 +02:00
|
|
|
else
|
2021-09-05 17:53:47 +02:00
|
|
|
this_sf->file_size = 0;
|
2008-05-26 15:29:21 +02:00
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
return &this_sf->vt;
|
2018-08-24 18:48:42 +02:00
|
|
|
|
|
|
|
fail:
|
2021-09-05 17:53:47 +02:00
|
|
|
free(buf);
|
|
|
|
free(this_sf);
|
2018-08-24 18:48:42 +02:00
|
|
|
return NULL;
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
static STREAMFILE* open_foo_streamfile_buffer(const char* const filename, size_t buf_size, abort_callback* p_abort, t_filestats* stats) {
|
2021-04-18 13:11:41 +02:00
|
|
|
STREAMFILE* sf = NULL;
|
2008-05-26 15:29:21 +02:00
|
|
|
service_ptr_t<file> infile;
|
2019-09-15 15:47:41 +02:00
|
|
|
bool infile_exists;
|
2008-05-26 15:29:21 +02:00
|
|
|
|
2021-04-18 13:11:41 +02:00
|
|
|
try {
|
|
|
|
infile_exists = filesystem::g_exists(filename, *p_abort);
|
|
|
|
if (!infile_exists) {
|
|
|
|
/* allow non-existing files in some cases */
|
|
|
|
if (!vgmstream_is_virtual_filename(filename))
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-05-26 15:29:21 +02:00
|
|
|
|
2021-04-18 13:11:41 +02:00
|
|
|
if (infile_exists) {
|
|
|
|
filesystem::g_open_read(infile, filename, *p_abort);
|
|
|
|
if(stats) *stats = infile->get_stats(*p_abort);
|
|
|
|
}
|
|
|
|
|
2021-09-05 17:53:47 +02:00
|
|
|
sf = open_foo_streamfile_buffer_by_file(infile, infile_exists, filename, buf_size, p_abort);
|
2021-04-18 13:11:41 +02:00
|
|
|
if (!sf) {
|
|
|
|
//m_file.release(); //refcounted and cleaned after it goes out of scope
|
|
|
|
}
|
2008-05-26 15:29:21 +02:00
|
|
|
|
2021-04-18 13:11:41 +02:00
|
|
|
} catch (...) {
|
|
|
|
/* somehow foobar2000 throws an exception on g_exists when filename has a double \
|
|
|
|
* (traditionally Windows treats that like a single slash and fopen handles it fine) */
|
|
|
|
return NULL;
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:11:41 +02:00
|
|
|
return sf;
|
2008-05-26 15:29:21 +02:00
|
|
|
}
|
2018-08-24 18:48:42 +02:00
|
|
|
|
2021-04-18 13:11:41 +02:00
|
|
|
STREAMFILE* open_foo_streamfile(const char* const filename, abort_callback* p_abort, t_filestats* stats) {
|
|
|
|
return open_foo_streamfile_buffer(filename, STREAMFILE_DEFAULT_BUFFER_SIZE, p_abort, stats);
|
2018-08-24 18:48:42 +02:00
|
|
|
}
|