2024-08-08 13:12:37 -07:00
|
|
|
#pragma once
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <SDL.h>
|
|
|
|
#include <vector>
|
|
|
|
extern "C" {
|
|
|
|
#include <vgmstream.h>
|
|
|
|
}
|
|
|
|
enum class SeekType {
|
|
|
|
SET,
|
|
|
|
CUR,
|
|
|
|
END
|
|
|
|
};
|
|
|
|
class File {
|
|
|
|
protected:
|
|
|
|
size_t len;
|
|
|
|
public:
|
|
|
|
const char *name;
|
|
|
|
File(const char *fname);
|
|
|
|
inline virtual void open(const char *fname) { name = fname; }
|
2024-09-16 15:05:53 -07:00
|
|
|
inline virtual void close() { }
|
|
|
|
virtual size_t read(void *ptr, size_t size, size_t len) = 0;
|
2024-08-08 13:12:37 -07:00
|
|
|
virtual void seek(size_t pos, SeekType seek_type) = 0;
|
|
|
|
virtual size_t get_len();
|
|
|
|
virtual size_t get_pos() = 0;
|
2024-09-16 15:05:53 -07:00
|
|
|
inline virtual bool is_open() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
inline virtual ~File() {
|
|
|
|
if (is_open()) close();
|
|
|
|
}
|
2024-08-08 13:12:37 -07:00
|
|
|
};
|
|
|
|
class CFile : public File {
|
|
|
|
protected:
|
|
|
|
FILE *file;
|
|
|
|
public:
|
|
|
|
CFile(const char *fname);
|
|
|
|
void open(const char *fname) override;
|
|
|
|
void close() override;
|
2024-09-16 15:05:53 -07:00
|
|
|
size_t read(void *ptr, size_t size, size_t len) override;
|
2024-08-08 13:12:37 -07:00
|
|
|
void seek(size_t pos, SeekType seek_type) override;
|
|
|
|
size_t get_pos() override;
|
|
|
|
bool is_open() override;
|
|
|
|
};
|
|
|
|
class HttpFile : public File {
|
|
|
|
std::vector<uint8_t> data;
|
|
|
|
public:
|
|
|
|
HttpFile(const char *url);
|
|
|
|
void open(const char *url) override;
|
|
|
|
void close() override;
|
2024-09-16 15:05:53 -07:00
|
|
|
size_t read(void *ptr, size_t size, size_t len) override;
|
2024-08-08 13:12:37 -07:00
|
|
|
void seek(size_t pos, SeekType type) override;
|
|
|
|
size_t get_pos();
|
|
|
|
bool is_open();
|
|
|
|
};
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
class AndroidFile : public File {
|
|
|
|
protected:
|
|
|
|
void *handle;
|
|
|
|
public:
|
|
|
|
AndroidFile(const char *fname);
|
|
|
|
void open(const char *fname) override;
|
|
|
|
void close() override;
|
2024-09-16 15:05:53 -07:00
|
|
|
size_t read(void *ptr, size_t size, size_t len) override;
|
2024-08-08 13:12:37 -07:00
|
|
|
void seek(size_t pos, SeekType seek_type) override;
|
|
|
|
size_t get_pos() override;
|
|
|
|
bool is_open() override;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
#define FILE_TYPE AndroidFile
|
|
|
|
#else
|
|
|
|
#define FILE_TYPE CFile
|
|
|
|
#endif
|
|
|
|
STREAMFILE *get_sf_from_file(File *file);
|
|
|
|
SDL_RWops *get_sdl_file(File *file);
|
|
|
|
File *open_file(const char *fname);
|
|
|
|
File *open_url(const char *url);
|