libBeatsaber/src/beatmap.cpp

177 lines
4.6 KiB
C++

#include "beatmapimpl.h"
#include <iostream> //debug print
#if BEATSABERZIPSUPPORT == 1
#include <zipios/zipiosexceptions.hpp>
#endif
#include "beatsetimpl.h"
#include "filereaderimpl.h"
namespace Beatsaber {
bool BeatMapImpl::load() {
// try to open info.dat
std::shared_ptr<std::istream> stream = reader->getFileStream("info.dat");
try {
(*stream) >> infobase;
//load the beatset
const json& beatsets = infobase["_difficultyBeatmapSets"];
if(beatsets.is_array()) {
beatSets.reserve(beatsets.size());
//load all the beatsets
for(const json& jsonbeatset : beatsets) {
beatSets.push_back(std::make_shared<BeatSetImpl>(weak_from_this(), reader, jsonbeatset));
}
}
return true;
} catch(const nlohmann::detail::exception& e) { //catch any json errors
std::cout << "Could not read BeatMap: " << e.what() << std::endl;
}
return false;
}
BeatMapImpl::BeatMapImpl(std::shared_ptr<FileReader> r) : reader(r) {}
BeatMapImpl::~BeatMapImpl() {}
std::string BeatMapImpl::getVersion() const {
return infobase.value("_version", "");
}
std::string BeatMapImpl::getSongName() const {
return infobase.value("_songName", "");
}
std::string BeatMapImpl::getSubName() const {
return infobase.value("_songSubName", "");
}
std::string BeatMapImpl::getSongAuthorName() const {
return infobase.value("_songAuthorName", "");
}
std::string BeatMapImpl::getMapperName() const {
return infobase.value("_levelAuthorName", "");
}
double BeatMapImpl::getBPM() const {
return infobase.value("_beatsPerMinute", -1);
}
double BeatMapImpl::getTimeOffset() const {
return infobase.value("_songTimeOffset", 0);
}
double BeatMapImpl::getSchuffle() const {
return infobase.value("_shuffle", 0);
}
double BeatMapImpl::getSchufflePeriod() const {
return infobase.value("_shufflePeriod", 0);
}
double BeatMapImpl::getPreviewStart() const {
return infobase.value("_previewStartTime", 0);
}
double BeatMapImpl::getPreviewDuration() const {
return infobase.value("_previewDuration", 0);
}
std::string BeatMapImpl::getSongFilename() const {
return infobase.value("_songFilename", "");
}
std::string BeatMapImpl::getImageFilename() const {
return infobase.value("_coverImageFilename", "");
}
std::string BeatMapImpl::getEnvName() const {
return infobase.value("_environmentName", "");
}
std::string BeatMapImpl::get360EnvName() const {
return infobase.value("_allDirectionsEnvironmentName", "");
}
std::string BeatMapImpl::getCustomData() const {
if(infobase.contains("_customData")) {
return infobase["_customData"].dump();
}
return "";
}
const std::vector<std::shared_ptr<BeatSet>>& BeatMapImpl::getBeatSets() const {
return beatSets;
}
std::uint32_t BeatMapImpl::getBeatSetCount() const {
return beatSets.size();
}
const std::shared_ptr<BeatSet> BeatMapImpl::getBeatSet(BeatmapCharacteristic::BeatmapCharacteristic characteristic) const {
for(auto it = beatSets.begin(); it != beatSets.end(); ++it) {
if((*it)->getCharacteristic() == characteristic) {
return *it;
}
}
return nullptr;
}
std::shared_ptr<BeatSet> BeatMapImpl::getBeatSet(BeatmapCharacteristic::BeatmapCharacteristic characteristic) {
for(auto it = beatSets.begin(); it != beatSets.end(); ++it) {
if((*it)->getCharacteristic() == characteristic) {
return *it;
}
}
return nullptr;
}
void BeatMapImpl::printDebug() const {
std::cout << "SongName: " << getSongName() << " - " << getSongAuthorName() << " (" << getSubName() << ")"
<< "\n Mapper: " << getMapperName()
<< "\n Version: " << getVersion()
<< "\n BPM: " << getBPM() << " offset: " << getTimeOffset()
<< "\n Schuffle " << getSchuffle() << " period: " << getSchufflePeriod()
<< "\n PreviewStart: " << getPreviewStart() << " duration: " << getPreviewDuration()
<< "\n SongFile: " << getSongFilename()
<< "\n ImageFile: " << getImageFilename()
<< "\n EnvName: " << getEnvName() << " 360Env: " << get360EnvName()
<< "\n customData: " << getCustomData()
<< "\n BeatSetCount: " << getBeatSetCount()
<< std::endl;
for(auto it : beatSets) {
it->printDebug();
}
}
std::shared_ptr<BeatMap> BeatMap::loadFromFolder(const std::string& folderPath) {
auto map = std::make_shared<BeatMapImpl>(std::make_shared<FolderReader>(folderPath));
if(!map->load()) return nullptr;
return map;
}
std::shared_ptr<BeatMap> BeatMap::loadFromZip(const std::string& zipPath) {
#if BEATSABERZIPSUPPORT == 1
try {
auto map = std::make_shared<BeatMapImpl>(std::make_shared<ZipReader>(zipPath));
if(!map->load()) return nullptr;
return map;
} catch(zipios::Exception& e) {
return nullptr;
}
#else
// Zip not supported
std::cerr << "Zip not supported" << std::endl;
return nullptr;
#endif
}
}