#include "resource_manager.h"

#include <string>
#include <physfs.h>

#define BP_INSER_RESOURCES
#include "system_assets.h"

resource_manager::resource_manager() {
	PHYSFS_init(0);
	mount("assets", "/");
	PHYSFS_mountMemory(BP_system_assets, sizeof(BP_system_assets), 0, "system-assets", "/", 0);
}

void resource_manager::mount(std::string const& path, std::string const& mounting_point) {
	PHYSFS_mount(path.c_str(), mounting_point.c_str(), 0);
}

std::vector<uint8_t> resource_manager::read(std::filesystem::path const& path) {
	std::vector<uint8_t> data;

	PHYSFS_Stat stat;
	PHYSFS_stat(path.string().c_str(), &stat);

	data.resize(stat.filesize);
	auto file = PHYSFS_openRead(path.string().c_str());
	PHYSFS_readBytes(file, data.data(), stat.filesize);
	PHYSFS_close(file);
	return data;
}

bool resource_manager::resource_exists(std::filesystem::path const& path) {
	return PHYSFS_exists(path.string().c_str());
}
