2021-06-01 11:47:41 +02:00
# include "beatlevelimpl.h"
2021-06-01 21:05:33 +02:00
# include <iostream>
2021-06-01 11:47:41 +02:00
# include <nlohmann/json.hpp>
# include "beatset.h"
2021-06-04 13:15:13 +02:00
# include "beatnoteimpl.h"
2021-06-12 18:04:14 +02:00
# include "beatmap.h" // for debug print
2021-06-01 11:47:41 +02:00
namespace Beatsaber {
2021-06-04 10:41:24 +02:00
BeatLevelImpl : : BeatLevelImpl ( std : : weak_ptr < BeatSet > p , std : : shared_ptr < FileReader > r , const json & j ) : parent ( p ) , base ( j ) {
2021-06-01 11:47:41 +02:00
dif = Difficulty : : getByString ( j . value ( " _difficulty " , " " ) ) ;
diffRank = j . value ( " _difficultyRank " , 0 ) ;
filename = j . value ( " _beatmapFilename " , " " ) ;
njs = j . value ( " _noteJumpMovementSpeed " , - 1 ) ;
nso = j . value ( " _noteJumpStartBeatOffset " , 0 ) ;
//load level content
if ( ! filename . empty ( ) ) {
2021-06-04 10:41:24 +02:00
try {
std : : shared_ptr < std : : istream > stream = r - > getFileStream ( filename ) ;
2021-06-04 13:15:13 +02:00
json diffjson ;
( * stream ) > > diffjson ;
const std : : string & version = diffjson [ " _version " ] ;
if ( version ! = " 2.0.0 " ) {
//not supported
return ;
}
//load notes
const json & jnotes = diffjson [ " _notes " ] ;
if ( jnotes . is_array ( ) ) {
notes . reserve ( jnotes . size ( ) ) ;
for ( const json & note : jnotes ) {
notes . push_back ( note ) ;
}
}
2021-06-04 14:53:31 +02:00
//load walls
const json & jwalls = diffjson [ " _obstacles " ] ;
if ( jwalls . is_array ( ) ) {
walls . reserve ( jwalls . size ( ) ) ;
for ( const json & wall : jwalls ) {
walls . push_back ( wall ) ;
}
}
2021-06-04 10:41:24 +02:00
} catch ( . . . ) {
2021-06-12 18:04:14 +02:00
std : : string mapname = " <unknown> " ;
auto par = parent . lock ( ) ;
if ( par )
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?
2021-06-04 10:41:24 +02:00
}
2021-06-01 11:47:41 +02:00
}
}
BeatLevelImpl : : ~ BeatLevelImpl ( ) { }
Difficulty : : Difficulty BeatLevelImpl : : getDifficulty ( ) const {
return dif ;
}
int32_t BeatLevelImpl : : getDifficultyRank ( ) const {
return diffRank ;
}
std : : string BeatLevelImpl : : getFilename ( ) const {
return filename ;
}
double BeatLevelImpl : : getNoteJumpSpeed ( ) const {
return njs ;
}
double BeatLevelImpl : : getNoteStartOffset ( ) const {
return nso ;
}
std : : string BeatLevelImpl : : getCustomData ( ) const {
if ( base . contains ( " _customData " ) ) {
return base [ " _customData " ] . dump ( ) ;
}
return " " ;
}
2021-06-04 13:15:13 +02:00
const std : : vector < Note > & BeatLevelImpl : : getNotes ( ) const {
return notes ;
}
2021-06-04 14:53:31 +02:00
const std : : vector < Wall > & BeatLevelImpl : : getWalls ( ) const {
return walls ;
}
2021-06-01 21:05:33 +02:00
void BeatLevelImpl : : printDebug ( ) const {
std : : cout < <
" Difficulty: " < < Difficulty : : toString ( dif ) < < " ( " < < diffRank < < " ) "
< < " \n Filename: " < < getFilename ( )
< < " \n njs: " < < getNoteJumpSpeed ( ) < < " nso: " < < getNoteStartOffset ( )
2021-06-04 14:53:31 +02:00
< < " \n noteCount: " < < notes . size ( ) < < " wallCount: " < < walls . size ( )
2021-06-04 20:27:20 +02:00
< < " \n CustomData: " < < getCustomData ( )
2021-06-01 21:05:33 +02:00
< < std : : endl ;
}
2021-06-01 11:47:41 +02:00
std : : shared_ptr < BeatSet > BeatLevelImpl : : getBeatSet ( ) const {
return parent . lock ( ) ;
}
std : : shared_ptr < BeatMap > BeatLevelImpl : : getBeatMap ( ) const {
return parent . lock ( ) - > getBeatMap ( ) ;
}
}