fix setting of parent

This commit is contained in:
mrbesen 2021-06-12 20:17:24 +02:00
parent 03cd4b3822
commit 3bce6bdfd9
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
5 changed files with 48 additions and 23 deletions

View File

@ -20,6 +20,7 @@ protected:
double njs; double njs;
double nso; double nso;
std::shared_ptr<FileReader> reader;
const json& base; const json& base;
std::vector<Note> notes; std::vector<Note> notes;
@ -29,6 +30,8 @@ public:
BeatLevelImpl(std::weak_ptr<BeatSet> p, std::shared_ptr<FileReader> r, const json& j); BeatLevelImpl(std::weak_ptr<BeatSet> p, std::shared_ptr<FileReader> r, const json& j);
virtual ~BeatLevelImpl(); virtual ~BeatLevelImpl();
bool load();
virtual Difficulty::Difficulty getDifficulty() const override; virtual Difficulty::Difficulty getDifficulty() const override;
virtual int32_t getDifficultyRank() const override; virtual int32_t getDifficultyRank() const override;
virtual std::string getFilename() const override; virtual std::string getFilename() const override;

View File

@ -11,6 +11,8 @@ class BeatSetImpl : public BeatSet, public std::enable_shared_from_this<BeatSetI
protected: protected:
std::weak_ptr<BeatMap> parent; std::weak_ptr<BeatMap> parent;
std::shared_ptr<FileReader> reader;
const json& base;
BeatmapCharacteristic::BeatmapCharacteristic characteristic; BeatmapCharacteristic::BeatmapCharacteristic characteristic;
std::vector<std::shared_ptr<BeatLevel>> level; std::vector<std::shared_ptr<BeatLevel>> level;
@ -21,6 +23,8 @@ public:
BeatSetImpl() = default; BeatSetImpl() = default;
virtual ~BeatSetImpl(); virtual ~BeatSetImpl();
bool load();
virtual void printDebug() const override; virtual void printDebug() const override;
virtual BeatmapCharacteristic::BeatmapCharacteristic getCharacteristic() const override; virtual BeatmapCharacteristic::BeatmapCharacteristic getCharacteristic() const override;

View File

@ -9,24 +9,31 @@
namespace Beatsaber { namespace Beatsaber {
BeatLevelImpl::BeatLevelImpl(std::weak_ptr<BeatSet> p, std::shared_ptr<FileReader> r, const json& j) : parent(p), base(j) { BeatLevelImpl::BeatLevelImpl(std::weak_ptr<BeatSet> p, std::shared_ptr<FileReader> r, const json& j) : parent(p), reader(r), base(j) {
dif = Difficulty::getByString(j.value("_difficulty", "")); }
diffRank = j.value("_difficultyRank", 0);
filename = j.value("_beatmapFilename", ""); BeatLevelImpl::~BeatLevelImpl() {}
njs = j.value("_noteJumpMovementSpeed", -1);
nso = j.value("_noteJumpStartBeatOffset", 0); bool BeatLevelImpl::load() {
dif = Difficulty::getByString(base.value("_difficulty", ""));
diffRank = base.value("_difficultyRank", 0);
filename = base.value("_beatmapFilename", "");
njs = base.value("_noteJumpMovementSpeed", -1);
nso = base.value("_noteJumpStartBeatOffset", 0);
//load level content //load level content
if(!filename.empty()) { if(!filename.empty()) {
try { try {
std::shared_ptr<std::istream> stream = r->getFileStream(filename); std::shared_ptr<std::istream> stream = reader->getFileStream(filename);
json diffjson; json diffjson;
(*stream) >> diffjson; (*stream) >> diffjson;
const std::string& version = diffjson["_version"]; if(diffjson.contains("_version") && diffjson["_version"].is_string()) {
if(version != "2.0.0") { const std::string& version = diffjson["_version"];
//not supported if(version != "2.0.0") {
return; //not supported
return false;
}
} }
//load notes //load notes
@ -46,18 +53,19 @@ BeatLevelImpl::BeatLevelImpl(std::weak_ptr<BeatSet> p, std::shared_ptr<FileReade
walls.push_back(wall); walls.push_back(wall);
} }
} }
} catch(...) {
return true;
} catch(const std::exception& exc) {
std::string mapname = "<unknown>"; std::string mapname = "<unknown>";
auto par = parent.lock(); auto par = parent.lock();
if(par) if(par)
mapname = par->getBeatMap()->getSongName(); mapname = par->getBeatMap()->getSongName();
std::cout << "Could not load difficulty: " << filename << " from: " << mapname << std::endl; //is the direct access after p.lock() a problem? is the parent always loaded? std::cout << "Could not load difficulty: " << filename << " from: " << mapname << " " << exc.what() << std::endl; //is the direct access after p.lock() a problem? is the parent always loaded?
} }
} }
return false;
} }
BeatLevelImpl::~BeatLevelImpl() {}
Difficulty::Difficulty BeatLevelImpl::getDifficulty() const { Difficulty::Difficulty BeatLevelImpl::getDifficulty() const {
return dif; return dif;
} }

View File

@ -24,7 +24,10 @@ 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(), reader, jsonbeatset)); auto beatset = std::make_shared<BeatSetImpl>(weak_from_this(), reader, jsonbeatset);
if(beatset->load())
beatSets.push_back(beatset);
//return false on false?
} }
} }

View File

@ -7,27 +7,34 @@
namespace Beatsaber { namespace Beatsaber {
BeatSetImpl::BeatSetImpl(std::weak_ptr<BeatMap> p, std::shared_ptr<FileReader> r, const json& j) : parent(p) { BeatSetImpl::BeatSetImpl(std::weak_ptr<BeatMap> p, std::shared_ptr<FileReader> r, const json& j) : parent(p), reader(r), base(j) {}
BeatSetImpl::~BeatSetImpl() {}
bool BeatSetImpl::load() {
try { try {
characteristic = BeatmapCharacteristic::getByString(j["_beatmapCharacteristicName"]); characteristic = BeatmapCharacteristic::getByString(base["_beatmapCharacteristicName"]);
//load level //load level
const json& arr = j["_difficultyBeatmaps"]; const json& arr = base["_difficultyBeatmaps"];
if(arr.is_array()) { if(arr.is_array()) {
level.reserve(arr.size()); level.reserve(arr.size());
//std::cout << "Try to load: " << arr.size() << " BeatLevel" << std::endl; //std::cout << "Try to load: " << arr.size() << " BeatLevel" << std::endl;
for(const json& beat : arr) { for(const json& beat : arr) {
level.push_back(std::make_shared<BeatLevelImpl>(weak_from_this(), r, beat)); auto beatlevel = std::make_shared<BeatLevelImpl>(weak_from_this(), reader, beat);
if(beatlevel->load())
level.push_back(beatlevel);
// return false on false?
} }
return true;
} }
} catch(std::exception& e) { } catch(std::exception& e) {
std::cout << "Could not read BeatSet: " << e.what() << std::endl; std::cout << "Could not read BeatSet: " << e.what() << std::endl;
characteristic = BeatmapCharacteristic::NONE; characteristic = BeatmapCharacteristic::NONE;
} }
return false;
} }
BeatSetImpl::~BeatSetImpl() {}
void BeatSetImpl::printDebug() const { void BeatSetImpl::printDebug() const {
std::cout << " Characteristic: " << BeatmapCharacteristic::toString(characteristic) << std::endl; std::cout << " Characteristic: " << BeatmapCharacteristic::toString(characteristic) << std::endl;