Renamed FS Library from StrawFile to MoonFS & cleaned the code

This commit is contained in:
KiritoDev 2021-05-23 11:04:37 -05:00
parent c5ba2b8a87
commit dd43b387f6
6 changed files with 127 additions and 99 deletions

View File

@ -305,8 +305,8 @@ SRC_DIRS += src/moon/libs/miniz
# Moon64 LIB [nlohmann json]
SRC_DIRS += src/moon/libs/nlohmann
# Moon64 LIB [Miniz-Wrapper]
SRC_DIRS += src/moon/zip
# Moon64 LIB [MoonFS API]
SRC_DIRS += src/moon/fs
################################

110
src/moon/fs/moonfs.cpp Normal file
View File

@ -0,0 +1,110 @@
#include "moonfs.h"
#include "moon/libs/miniz/zip_file.hpp"
#include <sys/types.h>
#include <sys/stat.h>
#include <algorithm>
#include <filesystem>
using namespace std;
using namespace miniz_cpp;
namespace fs = std::filesystem;
bool isDirectory;
zip_file zipFile;
MoonFS::MoonFS(string path){
this->path = FSUtils::normalize(path);
this->type = fs::is_directory(this->path) ? DIRECTORY : ZIP;
}
namespace FSUtils {
string normalize(string path){
replace(path.begin(), path.end(), '\\', '/');
return path;
}
string joinPath(string base, string file){
return normalize((fs::path(base) / fs::path(file)).string());
}
}
void MoonFS::open(){
switch(this->type){
case ZIP:
zipFile.load(this->path);
break;
}
}
bool MoonFS::exists(string path){
switch(this->type){
case DIRECTORY:
return fs::exists(FSUtils::joinPath(this->path, path));
case ZIP:
return zipFile.has_file(path);
}
}
vector<string> MoonFS::entries(){
switch(this->type){
case DIRECTORY: {
vector<string> fileList;
for (auto & entry : fs::recursive_directory_iterator(this->path)){
string path = FSUtils::normalize(entry.path().string());
fileList.push_back(path.substr(path.find(this->path) + this->path.length() + 1));
}
return fileList;
}
case ZIP:
return zipFile.namelist();
}
}
string MoonFS::read(string file){
switch(this->type){
case DIRECTORY: {
std::ifstream in(FSUtils::joinPath(this->path, file), std::ios::in | std::ios::binary);
if (in)
return(std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()));
}
case ZIP:
return zipFile.read(file);
}
}
void MoonFS::read(string file, TextureFileEntry *entry){
switch(this->type){
case DIRECTORY: {
char *data;
long size;
FILE* f = fopen(FSUtils::joinPath(this->path, file).c_str(), "rb");
if(f == NULL){
cout << "ERROR: Failed to open file " << file << endl;
return;
}
fseek(f, 0, SEEK_END);
size = ftell(f);
rewind(f);
data = new char[size];
fread(data, sizeof(char), size, f);
entry->data = data;
entry->size = size;
fclose(f);
break;
}
case ZIP:
zipFile.read_texture(file, &entry);
break;
}
}
string MoonFS::getPath(){
return this->path;
}

View File

@ -1,18 +1,23 @@
#ifndef StrawZipLoader
#define StrawZipLoader
#ifndef MoonFSAPI
#define MoonFSAPI
#include "moon/mod-engine/interfaces/file-entry.h"
#include <vector>
#include <string>
namespace MoonFS {
namespace FSUtils {
std::string normalize(std::string path);
std::string joinPath(std::string base, std::string file);
}
class StrawFile {
enum FileType {
ZIP,
DIRECTORY
};
class MoonFS {
public:
StrawFile(std::string path);
MoonFS(std::string path);
void open();
bool exists(std::string path);
std::vector<std::string> entries();
@ -22,6 +27,7 @@ public:
void close();
protected:
std::string path;
FileType type;
};
#endif

View File

@ -5,7 +5,7 @@
#include "textures/mod-texture.h"
#include "shaders/mod-shaders.h"
#include "moon/zip/straw.h"
#include "moon/fs/moonfs.h"
#include "moon/libs/nlohmann/json.hpp"
using json = nlohmann::json;
using namespace std;
@ -27,7 +27,7 @@ namespace Moon {
vector<BitModule*> addons;
void loadAddon(string addonPath){
StrawFile file(addonPath);
MoonFS file(addonPath);
file.open();
cout << "Loading addon: " << addonPath << endl;

View File

@ -1,6 +1,6 @@
#include "mod-texture.h"
#include "moon/zip/straw.h"
#include "moon/fs/moonfs.h"
#include "moon/libs/nlohmann/json.hpp"
#include "moon/mod-engine/engine.h"
#include "moon/mod-engine/hooks/hook.h"
@ -80,7 +80,7 @@ namespace MoonInternal {
if(fileEntry != NULL){
if(fileEntry->data != NULL) data = fileEntry;
else if(!fileEntry->path.empty()){
StrawFile file(addon->path);
MoonFS file(addon->path);
file.open();
TextureFileEntry *newData = new TextureFileEntry();
file.read(fileEntry->path, newData);

View File

@ -1,88 +0,0 @@
#include "straw.h"
#include "moon/libs/miniz/zip_file.hpp"
#include <sys/types.h>
#include <sys/stat.h>
#include <algorithm>
#include <filesystem>
using namespace std;
using namespace miniz_cpp;
namespace fs = std::filesystem;
bool isDirectory;
zip_file zipFile;
StrawFile::StrawFile(string path){
this->path = MoonFS::normalize(path);
}
namespace MoonFS {
string normalize(string path){
replace(path.begin(), path.end(), '\\', '/');
return path;
}
string joinPath(string base, string file){
return normalize((fs::path(base) / fs::path(file)).string());
}
}
void StrawFile::open(){
if(!(isDirectory = fs::is_directory(this->path)))
zipFile.load(this->path);
}
bool StrawFile::exists(string path){
return isDirectory ? fs::exists(MoonFS::joinPath(this->path, path)) : zipFile.has_file(path);
}
vector<string> StrawFile::entries(){
if(isDirectory) {
vector<string> fileList;
for (auto & entry : fs::recursive_directory_iterator(this->path)){
string path = MoonFS::normalize(entry.path().string());
fileList.push_back(path.substr(path.find(this->path) + this->path.length() + 1));
}
return fileList;
}
return zipFile.namelist();
}
string StrawFile::read(string file){
if(isDirectory){
std::ifstream in(MoonFS::joinPath(this->path, file), std::ios::in | std::ios::binary);
if (in)
return(std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()));
}
return zipFile.read(file);
}
void StrawFile::read(string file, TextureFileEntry *entry){
if(isDirectory){
char *data;
long size;
FILE* f = fopen(MoonFS::joinPath(this->path, file).c_str(), "rb");
fseek(f, 0, SEEK_END);
size = ftell(f);
rewind(f);
data = new char[size];
fread(data, sizeof(char), size, f);
entry->data = data;
entry->size = size;
fclose(f);
return;
}
zipFile.read_texture(file, &entry);
}
string StrawFile::getPath(){
return this->path;
}