changed to FileReader

This commit is contained in:
MrBesen 2021-06-04 10:41:24 +02:00
parent fd6f908671
commit 340096b20f
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
7 changed files with 28 additions and 22 deletions

View File

@ -3,9 +3,10 @@
#include "beatlevel.h" #include "beatlevel.h"
#include <nlohmann/json_fwd.hpp> #include <nlohmann/json_fwd.hpp>
using json = nlohmann::json; using json = nlohmann::json;
#include "filereader.h"
namespace Beatsaber { namespace Beatsaber {
class BeatLevelImpl : public BeatLevel, public std::enable_shared_from_this<BeatLevelImpl> { class BeatLevelImpl : public BeatLevel, public std::enable_shared_from_this<BeatLevelImpl> {
@ -22,7 +23,7 @@ protected:
const json& base; const json& base;
public: public:
BeatLevelImpl(std::weak_ptr<BeatSet> p, const json& j); BeatLevelImpl(std::weak_ptr<BeatSet> p, std::shared_ptr<FileReader> r, const json& j);
virtual ~BeatLevelImpl(); virtual ~BeatLevelImpl();
virtual Difficulty::Difficulty getDifficulty() const override; virtual Difficulty::Difficulty getDifficulty() const override;

View File

@ -6,6 +6,7 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include "beatset.h" #include "beatset.h"
#include "filereader.h"
using json = nlohmann::json; using json = nlohmann::json;
@ -14,8 +15,7 @@ namespace Beatsaber {
class BeatMapImpl : public BeatMap, public std::enable_shared_from_this<BeatMapImpl> { class BeatMapImpl : public BeatMap, public std::enable_shared_from_this<BeatMapImpl> {
protected: protected:
const std::string path; std::shared_ptr<FileReader> reader;
const bool isZip;
json infobase; json infobase;
std::vector<std::shared_ptr<BeatSet>> beatSets; std::vector<std::shared_ptr<BeatSet>> beatSets;
@ -23,7 +23,7 @@ protected:
bool load(); bool load();
public: public:
BeatMapImpl(const std::string& path, bool isZip); BeatMapImpl(std::shared_ptr<FileReader> r);
~BeatMapImpl(); ~BeatMapImpl();
virtual std::string getVersion() const override; virtual std::string getVersion() const override;

View File

@ -1,9 +1,10 @@
#include "beatset.h" #include "beatset.h"
#include <nlohmann/json_fwd.hpp> #include <nlohmann/json_fwd.hpp>
using json = nlohmann::json; using json = nlohmann::json;
#include "filereader.h"
namespace Beatsaber { namespace Beatsaber {
class BeatSetImpl : public BeatSet, public std::enable_shared_from_this<BeatSetImpl> { class BeatSetImpl : public BeatSet, public std::enable_shared_from_this<BeatSetImpl> {
@ -15,7 +16,7 @@ protected:
std::vector<std::shared_ptr<BeatLevel>> level; std::vector<std::shared_ptr<BeatLevel>> level;
public: public:
BeatSetImpl(std::weak_ptr<BeatMap> p, const json& j); BeatSetImpl(std::weak_ptr<BeatMap> p, std::shared_ptr<FileReader> r, const json& j);
BeatSetImpl(const BeatSetImpl& c) = default; BeatSetImpl(const BeatSetImpl& c) = default;
BeatSetImpl() = default; BeatSetImpl() = default;
virtual ~BeatSetImpl(); virtual ~BeatSetImpl();

View File

@ -7,7 +7,7 @@
namespace Beatsaber { namespace Beatsaber {
BeatLevelImpl::BeatLevelImpl(std::weak_ptr<BeatSet> p, const json& j) : parent(p), base(j) { BeatLevelImpl::BeatLevelImpl(std::weak_ptr<BeatSet> p, std::shared_ptr<FileReader> r, const json& j) : parent(p), base(j) {
dif = Difficulty::getByString(j.value("_difficulty", "")); dif = Difficulty::getByString(j.value("_difficulty", ""));
diffRank = j.value("_difficultyRank", 0); diffRank = j.value("_difficultyRank", 0);
filename = j.value("_beatmapFilename", ""); filename = j.value("_beatmapFilename", "");
@ -16,7 +16,14 @@ BeatLevelImpl::BeatLevelImpl(std::weak_ptr<BeatSet> p, const json& j) : parent(p
//load level content //load level content
if(!filename.empty()) { if(!filename.empty()) {
try {
std::shared_ptr<std::istream> stream = r->getFileStream(filename);
json json;
(*stream) >> json;
//TODO
} catch(...) {
std::cout << "Could not load difficulty: " << filename << std::endl;
}
} }
} }

View File

@ -1,20 +1,17 @@
#include "beatmapimpl.h" #include "beatmapimpl.h"
#include <fstream>
#include <iostream> //debug print #include <iostream> //debug print
#include "beatsetimpl.h" #include "beatsetimpl.h"
#include "filereaderimpl.h"
namespace Beatsaber { namespace Beatsaber {
bool BeatMapImpl::load() { bool BeatMapImpl::load() {
// try to open info.dat // try to open info.dat
if(isZip) { std::shared_ptr<std::istream> stream = reader->getFileStream("info.dat");
return false;
}
std::ifstream info(path + "/info.dat"); //try diffrent filenames if not found?
try { try {
info >> infobase; (*stream) >> infobase;
//load the beatset //load the beatset
const json& beatsets = infobase["_difficultyBeatmapSets"]; const json& beatsets = infobase["_difficultyBeatmapSets"];
@ -23,7 +20,7 @@ bool BeatMapImpl::load() {
//load all the beatsets //load all the beatsets
for(const json& jsonbeatset : beatsets) { for(const json& jsonbeatset : beatsets) {
beatSets.push_back(std::make_shared<BeatSetImpl>(weak_from_this(), jsonbeatset)); beatSets.push_back(std::make_shared<BeatSetImpl>(weak_from_this(), reader, jsonbeatset));
} }
} }
@ -34,7 +31,7 @@ bool BeatMapImpl::load() {
return false; return false;
} }
BeatMapImpl::BeatMapImpl(const std::string& path, bool isZip) : path(path), isZip(isZip) {} BeatMapImpl::BeatMapImpl(std::shared_ptr<FileReader> r) : reader(r) {}
BeatMapImpl::~BeatMapImpl() {} BeatMapImpl::~BeatMapImpl() {}
@ -152,14 +149,14 @@ void BeatMapImpl::printDebug() const {
std::shared_ptr<BeatMap> BeatMap::loadFromFolder(const std::string& folderPath) { std::shared_ptr<BeatMap> BeatMap::loadFromFolder(const std::string& folderPath) {
auto map = std::make_shared<BeatMapImpl>(folderPath, false); auto map = std::make_shared<BeatMapImpl>(std::make_shared<FolderReader>(folderPath));
if(!map->load()) return nullptr; if(!map->load()) return nullptr;
return map; return map;
} }
std::shared_ptr<BeatMap> BeatMap::loadFromZip(const std::string& zipPath) { std::shared_ptr<BeatMap> BeatMap::loadFromZip(const std::string& zipPath) {
#if BEATSABERZIPSUPPORT == 1 #if BEATSABERZIPSUPPORT == 1
auto map = std::make_shared<BeatMapImpl>(zipPath, true); auto map = std::make_shared<BeatMapImpl>(std::make_shared<ZipReader>(zipPath));
if(!map->load()) return nullptr; if(!map->load()) return nullptr;
return map; return map;
#else #else

View File

@ -7,7 +7,7 @@
namespace Beatsaber { namespace Beatsaber {
BeatSetImpl::BeatSetImpl(std::weak_ptr<BeatMap> p, const json& j) : parent(p) { BeatSetImpl::BeatSetImpl(std::weak_ptr<BeatMap> p, std::shared_ptr<FileReader> r, const json& j) : parent(p) {
try { try {
characteristic = BeatmapCharacteristic::getByString(j["_beatmapCharacteristicName"]); characteristic = BeatmapCharacteristic::getByString(j["_beatmapCharacteristicName"]);
@ -17,7 +17,7 @@ BeatSetImpl::BeatSetImpl(std::weak_ptr<BeatMap> p, const json& j) : parent(p) {
level.reserve(arr.size()); level.reserve(arr.size());
std::cout << "Try to load: " << arr.size() << std::endl; std::cout << "Try to load: " << arr.size() << std::endl;
for(const json& beat : arr) { for(const json& beat : arr) {
level.push_back(std::make_shared<BeatLevelImpl>(weak_from_this(), beat)); level.push_back(std::make_shared<BeatLevelImpl>(weak_from_this(), r, beat));
} }
} }
} catch(std::exception& e) { } catch(std::exception& e) {

View File

@ -30,7 +30,7 @@ int main(int argc, char** argv) {
printf("\033[1;93m%d\033[0;1m/%d failed\n", failcount, testcount); printf("\033[1;93m%d\033[0;1m/%d failed\n", failcount, testcount);
//simple read test //simple read test
std::shared_ptr<Beatsaber::BeatMap> bmap = Beatsaber::BeatMap::loadFromFolder("/media/satassd/steam/steamapps/common/Beat Saber/Beat Saber_Data/CustomLevels/1dd (Portal - Still Alive (Uppermost Remix) - kryptikos)"); std::shared_ptr<Beatsaber::BeatMap> bmap = Beatsaber::BeatMap::loadFromFolder("/home/yannis/Nextcloud/yannis/Beatsaber/BeatSaverLevel/1dd (Portal - Still Alive (Uppermost Remix) - kryptikos)");
if(!bmap) std::cout << "Could not load File" << std::endl; if(!bmap) std::cout << "Could not load File" << std::endl;
else bmap->printDebug(); else bmap->printDebug();