2017-01-20 08:36:52 -05:00
|
|
|
#pragma once
|
|
|
|
|
2017-01-19 16:23:59 -05:00
|
|
|
namespace Components
|
|
|
|
{
|
|
|
|
class FileSystem : public Component
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
class AbstractFile
|
|
|
|
{
|
|
|
|
public:
|
2022-08-11 06:31:19 -04:00
|
|
|
virtual ~AbstractFile() = default;
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
virtual bool exists() = 0;
|
|
|
|
virtual std::string getName() = 0;
|
|
|
|
virtual std::string& getBuffer() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class File : public AbstractFile
|
|
|
|
{
|
|
|
|
public:
|
2022-07-20 18:22:49 -04:00
|
|
|
File() = default;
|
2022-08-11 06:31:19 -04:00
|
|
|
File(std::string file) : filePath{std::move(file)} { this->read(); }
|
|
|
|
File(std::string file, Game::FsThread thread) : filePath{std::move(file)} { this->read(thread); }
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2022-08-11 06:31:19 -04:00
|
|
|
bool exists() override { return !this->buffer.empty(); }
|
|
|
|
std::string getName() override { return this->filePath; }
|
|
|
|
std::string& getBuffer() override { return this->buffer; }
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string filePath;
|
|
|
|
std::string buffer;
|
|
|
|
|
2022-07-20 18:22:49 -04:00
|
|
|
void read(Game::FsThread thread = Game::FS_THREAD_MAIN);
|
2017-01-19 16:23:59 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class RawFile : public AbstractFile
|
|
|
|
{
|
|
|
|
public:
|
2022-08-11 06:31:19 -04:00
|
|
|
RawFile() = default;
|
|
|
|
RawFile(std::string file) : filePath(std::move(file)) { this->read(); }
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2022-08-11 06:31:19 -04:00
|
|
|
bool exists() override { return !this->buffer.empty(); }
|
|
|
|
std::string getName() override { return this->filePath; }
|
|
|
|
std::string& getBuffer() override { return this->buffer; }
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string filePath;
|
|
|
|
std::string buffer;
|
|
|
|
|
|
|
|
void read();
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileReader
|
|
|
|
{
|
|
|
|
public:
|
2022-08-11 06:31:19 -04:00
|
|
|
FileReader() : handle(0), size(-1), name() {}
|
2018-12-17 08:29:18 -05:00
|
|
|
FileReader(const std::string& file);
|
2017-01-19 16:23:59 -05:00
|
|
|
~FileReader();
|
|
|
|
|
|
|
|
bool exists();
|
|
|
|
std::string getName();
|
|
|
|
std::string getBuffer();
|
|
|
|
int getSize();
|
|
|
|
bool read(void* buffer, size_t size);
|
|
|
|
void seek(int offset, int origin);
|
|
|
|
|
|
|
|
private:
|
|
|
|
int handle;
|
|
|
|
int size;
|
|
|
|
std::string name;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileWriter
|
|
|
|
{
|
|
|
|
public:
|
2022-08-11 06:31:19 -04:00
|
|
|
FileWriter(std::string file, bool append = false) : handle(0), filePath(std::move(file)) { this->open(append); }
|
|
|
|
~FileWriter() { this->close(); }
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2018-12-17 08:29:18 -05:00
|
|
|
void write(const std::string& data);
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
int handle;
|
|
|
|
std::string filePath;
|
|
|
|
|
|
|
|
void open(bool append = false);
|
|
|
|
void close();
|
|
|
|
};
|
|
|
|
|
|
|
|
FileSystem();
|
|
|
|
~FileSystem();
|
|
|
|
|
2022-08-02 07:24:22 -04:00
|
|
|
static std::filesystem::path GetAppdataPath();
|
2018-12-17 08:29:18 -05:00
|
|
|
static std::vector<std::string> GetFileList(const std::string& path, const std::string& extension);
|
|
|
|
static std::vector<std::string> GetSysFileList(const std::string& path, const std::string& extension, bool folders = false);
|
2022-08-11 06:44:03 -04:00
|
|
|
static bool _DeleteFile(const std::string& folder, const std::string& file);
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
static std::mutex Mutex;
|
|
|
|
static std::recursive_mutex FSMutex;
|
|
|
|
static Utils::Memory::Allocator MemAllocator;
|
|
|
|
|
|
|
|
static int ReadFile(const char* path, char** buffer);
|
|
|
|
static char* AllocateFile(int size);
|
|
|
|
static void FreeFile(void* buffer);
|
|
|
|
|
|
|
|
static void RegisterFolder(const char* folder);
|
|
|
|
|
|
|
|
static void RegisterFolders();
|
|
|
|
static void StartupStub();
|
|
|
|
static int ExecIsFSStub(const char* execFilename);
|
|
|
|
|
|
|
|
static void FsStartupSync(const char* a1);
|
|
|
|
static void FsRestartSync(int a1, int a2);
|
2019-12-26 07:24:48 -05:00
|
|
|
static void FsShutdownSync(int a1);
|
2017-01-19 16:23:59 -05:00
|
|
|
static void DelayLoadImagesSync();
|
|
|
|
static int LoadTextureSync(Game::GfxImageLoadDef **loadDef, Game::GfxImage *image);
|
2017-04-06 16:22:30 -04:00
|
|
|
|
|
|
|
static void IwdFreeStub(Game::iwd_t* iwd);
|
2017-01-19 16:23:59 -05:00
|
|
|
};
|
|
|
|
}
|