forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSongUtil.cpp
1265 lines (1103 loc) · 35.7 KB
/
SongUtil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "global.h"
#include "SongUtil.h"
#include "Song.h"
#include "Steps.h"
#include "GameState.h"
#include "Style.h"
#include "ProfileManager.h"
#include "Profile.h"
#include "PrefsManager.h"
#include "RageString.hpp"
#include "SongManager.h"
#include "XmlFile.h"
#include "UnlockManager.h"
#include "ThemeMetric.h"
#include "LocalizedString.h"
#include "RageLog.h"
#include "GameManager.h"
#include "CommonMetrics.h"
#include "LuaBinding.h"
#include "EnumHelper.h"
#include "RageFmtWrap.h"
#include <unordered_map>
using std::vector;
ThemeMetric<int> SORT_BPM_DIVISION ( "MusicWheel", "SortBPMDivision" );
ThemeMetric<int> SORT_LENGTH_DIVISION ( "MusicWheel", "SortLengthDivision" );
ThemeMetric<bool> SHOW_SECTIONS_IN_BPM_SORT ( "MusicWheel", "ShowSectionsInBPMSort" );
ThemeMetric<bool> SHOW_SECTIONS_IN_LENGTH_SORT ( "MusicWheel", "ShowSectionsInLengthSort" );
bool SongCriteria::Matches( const Song *pSong ) const
{
if( !m_sGroupName.empty() && m_sGroupName != pSong->m_sGroupName )
return false;
if( UNLOCKMAN->SongIsLocked(pSong) & LOCKED_DISABLED )
return false;
if( m_bUseSongGenreAllowedList )
{
if( find(m_vsSongGenreAllowedList.begin(),m_vsSongGenreAllowedList.end(),pSong->m_sGenre) == m_vsSongGenreAllowedList.end() )
return false;
}
switch( m_Selectable )
{
DEFAULT_FAIL(m_Selectable);
case Selectable_Yes:
if( UNLOCKMAN && UNLOCKMAN->SongIsLocked(pSong) & LOCKED_SELECTABLE )
return false;
break;
case Selectable_No:
if( UNLOCKMAN && !(UNLOCKMAN->SongIsLocked(pSong) & LOCKED_SELECTABLE) )
return false;
break;
case Selectable_DontCare:
break;
}
if( m_bUseSongAllowedList )
{
if( find(m_vpSongAllowedList.begin(),m_vpSongAllowedList.end(),pSong) == m_vpSongAllowedList.end() )
return false;
}
if( m_iMaxStagesForSong != -1 && GAMESTATE->GetNumStagesMultiplierForSong(pSong) > m_iMaxStagesForSong )
return false;
switch( m_Tutorial )
{
DEFAULT_FAIL(m_Tutorial);
case Tutorial_Yes:
if( !pSong->IsTutorial() )
return false;
break;
case Tutorial_No:
if( pSong->IsTutorial() )
return false;
break;
case Tutorial_DontCare:
break;
}
switch( m_Locked )
{
DEFAULT_FAIL(m_Locked);
case Locked_Locked:
if( UNLOCKMAN && !(UNLOCKMAN->SongIsLocked(pSong) & LOCKED_LOCK) )
return false;
break;
case Locked_Unlocked:
if( UNLOCKMAN && UNLOCKMAN->SongIsLocked(pSong) & LOCKED_LOCK )
return false;
break;
case Locked_DontCare:
break;
}
return true;
}
void SongUtil::GetSteps(
const Song *pSong,
vector<Steps*>& arrayAddTo,
StepsType st,
Difficulty dc,
int iMeterLow,
int iMeterHigh,
const std::string &sDescription,
const std::string &sCredit,
bool bIncludeAutoGen,
unsigned uHash,
int iMaxToGet
)
{
if( !iMaxToGet )
return;
const vector<Steps*> &vpSteps = st == StepsType_Invalid ? pSong->GetAllSteps() : pSong->GetStepsByStepsType(st);
for (auto *pSteps: vpSteps)
{
if( dc != Difficulty_Invalid && dc != pSteps->GetDifficulty() )
continue;
if( iMeterLow != -1 && iMeterLow > pSteps->GetMeter() )
continue;
if( iMeterHigh != -1 && iMeterHigh < pSteps->GetMeter() )
continue;
if( sDescription.size() && sDescription != pSteps->GetDescription() )
continue;
if( sCredit.size() && sCredit != pSteps->GetCredit() )
continue;
if( uHash != 0 && uHash != pSteps->GetHash() )
continue;
if( !bIncludeAutoGen && pSteps->IsAutogen() )
continue;
arrayAddTo.push_back( pSteps );
if( iMaxToGet != -1 )
{
--iMaxToGet;
if( !iMaxToGet )
break;
}
}
}
Steps* SongUtil::GetOneSteps(
const Song *pSong,
StepsType st,
Difficulty dc,
int iMeterLow,
int iMeterHigh,
const std::string &sDescription,
const std::string &sCredit,
unsigned uHash,
bool bIncludeAutoGen
)
{
vector<Steps*> vpSteps;
GetSteps( pSong, vpSteps, st, dc, iMeterLow, iMeterHigh, sDescription, sCredit, bIncludeAutoGen, uHash, 1 ); // get max 1
if( vpSteps.empty() )
return nullptr;
else
return vpSteps[0];
}
Steps* SongUtil::GetStepsByDifficulty( const Song *pSong, StepsType st, Difficulty dc, bool bIncludeAutoGen )
{
const vector<Steps*>& vpSteps = (st == StepsType_Invalid)? pSong->GetAllSteps() : pSong->GetStepsByStepsType(st);
for (auto *pSteps: vpSteps)
{
if( dc != Difficulty_Invalid && dc != pSteps->GetDifficulty() )
continue;
if( !bIncludeAutoGen && pSteps->IsAutogen() )
continue;
return pSteps;
}
return nullptr;
}
Steps* SongUtil::GetStepsByMeter( const Song *pSong, StepsType st, int iMeterLow, int iMeterHigh )
{
const vector<Steps*>& vpSteps = (st == StepsType_Invalid)? pSong->GetAllSteps() : pSong->GetStepsByStepsType(st);
for (auto *pSteps: vpSteps)
{
if( iMeterLow > pSteps->GetMeter() )
continue;
if( iMeterHigh < pSteps->GetMeter() )
continue;
return pSteps;
}
return nullptr;
}
Steps* SongUtil::GetStepsByDescription( const Song *pSong, StepsType st, std::string sDescription )
{
vector<Steps*> vNotes;
GetSteps( pSong, vNotes, st, Difficulty_Invalid, -1, -1, sDescription, "" );
if( vNotes.size() == 0 )
return nullptr;
else
return vNotes[0];
}
Steps* SongUtil::GetStepsByCredit( const Song *pSong, StepsType st, std::string sCredit )
{
vector<Steps*> vNotes;
GetSteps(pSong, vNotes, st, Difficulty_Invalid, -1, -1, "", sCredit );
if( vNotes.size() == 0 )
return nullptr;
else
return vNotes[0];
}
Steps* SongUtil::GetClosestNotes( const Song *pSong, StepsType st, Difficulty dc, bool bIgnoreLocked )
{
ASSERT( dc != Difficulty_Invalid );
const vector<Steps*>& vpSteps = (st == StepsType_Invalid)? pSong->GetAllSteps() : pSong->GetStepsByStepsType(st);
Steps *pClosest = nullptr;
int iClosestDistance = 999;
for (auto *pSteps: vpSteps)
{
if( pSteps->GetDifficulty() == Difficulty_Edit && dc != Difficulty_Edit )
continue;
if( bIgnoreLocked && UNLOCKMAN->StepsIsLocked(pSong,pSteps) )
continue;
int iDistance = abs(dc - pSteps->GetDifficulty());
if( iDistance < iClosestDistance )
{
pClosest = pSteps;
iClosestDistance = iDistance;
}
}
return pClosest;
}
/* Make any duplicate difficulties edits. (Note that BMS files do a first pass
* on this; see BMSLoader::SlideDuplicateDifficulties.) */
void SongUtil::AdjustDuplicateSteps( Song *pSong )
{
FOREACH_ENUM( StepsType, st )
{
FOREACH_ENUM( Difficulty, dc )
{
if( dc == Difficulty_Edit )
continue;
vector<Steps*> vSteps;
SongUtil::GetSteps( pSong, vSteps, st, dc );
/* Delete steps that are completely identical. This happened due to a
* bug in an earlier version. */
DeleteDuplicateSteps( pSong, vSteps );
char const *songTitle = pSong->GetDisplayFullTitle().c_str();
CHECKPOINT_M(fmt::sprintf("Duplicate steps from %s removed.", songTitle));
StepsUtil::SortNotesArrayByDifficulty( vSteps );
CHECKPOINT_M(fmt::sprintf("Charts from %s sorted.", songTitle));
for( unsigned k=1; k<vSteps.size(); k++ )
{
vSteps[k]->SetDifficulty( Difficulty_Edit );
if( vSteps[k]->GetDescription() == "" )
{
/* "Hard Edit" */
std::string EditName = Capitalize( DifficultyToString(dc) ) + " Edit";
vSteps[k]->SetDescription( EditName );
}
}
}
/* XXX: Don't allow edits to have descriptions that look like regular difficulties.
* These are confusing, and they're ambiguous when passed to GetStepsByID. */
}
}
/**
* @brief Remove the initial whitespace characters.
* @param s the string to left trim.
* @return the trimmed string.
*/
static std::string RemoveInitialWhitespace( std::string s )
{
size_t i = s.find_first_not_of(" \t\r\n");
if( i != s.npos )
s.erase( 0, i );
return s;
}
/* This is called within TidyUpData, before autogen notes are added. */
void SongUtil::DeleteDuplicateSteps( Song *pSong, vector<Steps*> &vSteps )
{
/* vSteps have the same StepsType and Difficulty. Delete them if they have the
* same m_sDescription, m_sCredit, m_iMeter and SMNoteData. */
for( unsigned i=0; i<vSteps.size(); i++ )
{
const Steps *s1 = vSteps[i];
for( unsigned j=i+1; j<vSteps.size(); j++ )
{
const Steps *s2 = vSteps[j];
if( s1->GetDescription() != s2->GetDescription() )
continue;
if( s1->GetCredit() != s2->GetCredit() )
continue;
if( s1->GetMeter() != s2->GetMeter() )
continue;
/* Compare, ignoring whitespace. */
std::string sSMNoteData1;
s1->GetSMNoteData( sSMNoteData1 );
std::string sSMNoteData2;
s2->GetSMNoteData( sSMNoteData2 );
if( RemoveInitialWhitespace(sSMNoteData1) != RemoveInitialWhitespace(sSMNoteData2) )
continue;
auto diffString = DifficultyToString(s2->GetDifficulty());
auto logStatement = fmt::format("Removed {0}: duplicate steps in song \"{1}\" with description \"{2}\", step author \"{3}\", and meter \"{4}\"",
diffString, pSong->GetSongDir(), s1->GetDescription(), s1->GetCredit(), s1->GetMeter()
);
LOG->Trace(logStatement );
pSong->DeleteSteps( s2, false );
vSteps.erase(vSteps.begin()+j);
--j;
}
}
}
/////////////////////////////////////
// Sorting
/////////////////////////////////////
static LocalizedString SORT_NOT_AVAILABLE( "Sort", "NotAvailable" );
static LocalizedString SORT_OTHER ( "Sort", "Other" );
/* Just calculating GetNumTimesPlayed within the sort is pretty slow, so let's precompute
* it. (This could be generalized with a template.) */
static std::unordered_map<const Song*, std::string> g_mapSongSortVal;
static bool CompareSongPointersBySortValueAscending( const Song *pSong1, const Song *pSong2 )
{
return g_mapSongSortVal[pSong1] < g_mapSongSortVal[pSong2];
}
static bool CompareSongPointersBySortValueDescending( const Song *pSong1, const Song *pSong2 )
{
return g_mapSongSortVal[pSong1] > g_mapSongSortVal[pSong2];
}
std::string SongUtil::MakeSortString( std::string s )
{
s = Rage::make_upper(s);
// Make sure that non-alphanumeric strings are placed at the very end.
if( s.size() > 0 )
{
if( s[0] == '.' ) // like the song ".59"
s.erase(s.begin());
if( (s[0] < 'A' || s[0] > 'Z') && (s[0] < '0' || s[0] > '9') )
s = char(126) + s;
}
return s;
}
static bool CompareSongPointersByTitle( const Song *pSong1, const Song *pSong2 )
{
// Prefer transliterations to full titles
std::string s1 = pSong1->GetTranslitMainTitle();
std::string s2 = pSong2->GetTranslitMainTitle();
if( s1 == s2 )
{
s1 = pSong1->GetTranslitSubTitle();
s2 = pSong2->GetTranslitSubTitle();
}
s1 = SongUtil::MakeSortString(s1);
s2 = SongUtil::MakeSortString(s2);
// TODO: Use a std::string comparison.
int ret = strcmp( s1.c_str(), s2.c_str() );
if(ret < 0) return true;
if(ret > 0) return false;
/* The titles are the same. Ensure we get a consistent ordering
* by comparing the unique SongFilePaths. */
Rage::ci_ascii_string a{ pSong1->GetSongFilePath().c_str() };
Rage::ci_ascii_string b{ pSong2->GetSongFilePath().c_str() };
return a < b;
}
void SongUtil::SortSongPointerArrayByTitle( vector<Song*> &vpSongsInOut )
{
sort( vpSongsInOut.begin(), vpSongsInOut.end(), CompareSongPointersByTitle );
}
static bool CompareSongPointersByBPM( const Song *pSong1, const Song *pSong2 )
{
DisplayBpms bpms1, bpms2;
pSong1->GetDisplayBpms( bpms1 );
pSong2->GetDisplayBpms( bpms2 );
if( bpms1.GetMax() < bpms2.GetMax() )
return true;
if( bpms1.GetMax() > bpms2.GetMax() )
return false;
return CompareStringsAsc( pSong1->GetSongFilePath(), pSong2->GetSongFilePath() );
}
void SongUtil::SortSongPointerArrayByBPM( vector<Song*> &vpSongsInOut )
{
sort( vpSongsInOut.begin(), vpSongsInOut.end(), CompareSongPointersByBPM );
}
static bool CompareSongPointersByLength( const Song *pSong1, const Song *pSong2 )
{
float length1, length2;
length1 = pSong1->m_fMusicLengthSeconds;
length2 = pSong2->m_fMusicLengthSeconds;
if( length1 < length2 )
return true;
if( length1 > length2 )
return false;
return CompareStringsAsc( pSong1->GetSongFilePath(), pSong2->GetSongFilePath() );
}
void SongUtil::SortSongPointerArrayByLength( vector<Song*> &vpSongsInOut )
{
sort( vpSongsInOut.begin(), vpSongsInOut.end(), CompareSongPointersByLength );
}
void AppendOctal( int n, int digits, std::string &out )
{
for( int p = digits-1; p >= 0; --p )
{
const int shift = p*3;
int n2 = (n >> shift) & 0x7;
out.insert( out.end(), static_cast<char>(n2+'0') );
}
}
static bool CompDescending( const std::pair<Song *, std::string> &a, const std::pair<Song *, std::string> &b )
{
return a.second > b.second;
}
static bool CompAscending( const std::pair<Song *, std::string> &a, const std::pair<Song *, std::string> &b )
{
return a.second < b.second;
}
void SongUtil::SortSongPointerArrayByGrades( vector<Song*> &vpSongsInOut, bool bDescending )
{
/* Optimize by pre-writing a string to compare, since doing
* GetNumNotesWithGrade inside the sort is too slow. */
typedef std::pair< Song *, std::string > val;
vector<val> vals;
vals.reserve( vpSongsInOut.size() );
for (auto *pSong: vpSongsInOut)
{
int iCounts[NUM_Grade];
const Profile *pProfile = PROFILEMAN->GetMachineProfile();
ASSERT( pProfile != nullptr );
pProfile->GetGrades( pSong, GAMESTATE->GetCurrentStyle(GAMESTATE->GetMasterPlayerNumber())->m_StepsType, iCounts );
std::string foo;
foo.reserve(256);
for( int g=Grade_Tier01; g<NUM_Grade; ++g )
AppendOctal( iCounts[g], 3, foo );
vals.push_back( val(pSong, foo) );
}
sort( vals.begin(), vals.end(), bDescending ? CompDescending : CompAscending );
for( unsigned i = 0; i < vpSongsInOut.size(); ++i )
{
vpSongsInOut[i] = vals[i].first;
}
}
void SongUtil::SortSongPointerArrayByArtist( vector<Song*> &vpSongsInOut )
{
for (auto *song: vpSongsInOut)
{
g_mapSongSortVal[song] = MakeSortString( song->GetTranslitArtist() );
}
stable_sort( vpSongsInOut.begin(), vpSongsInOut.end(), CompareSongPointersBySortValueAscending );
}
/* This is for internal use, not display; sorting by Unicode codepoints isn't very
* interesting for display. */
void SongUtil::SortSongPointerArrayByDisplayArtist( vector<Song*> &vpSongsInOut )
{
for (auto *song: vpSongsInOut)
{
g_mapSongSortVal[song] = MakeSortString( song->GetDisplayArtist() );
}
stable_sort( vpSongsInOut.begin(), vpSongsInOut.end(), CompareSongPointersBySortValueAscending );
}
static int CompareSongPointersByGenre(const Song *pSong1, const Song *pSong2)
{
return pSong1->m_sGenre < pSong2->m_sGenre;
}
void SongUtil::SortSongPointerArrayByGenre( vector<Song*> &vpSongsInOut )
{
stable_sort( vpSongsInOut.begin(), vpSongsInOut.end(), CompareSongPointersByGenre );
}
int SongUtil::CompareSongPointersByGroup(const Song *pSong1, const Song *pSong2)
{
return pSong1->m_sGroupName < pSong2->m_sGroupName;
}
static int CompareSongPointersByGroupAndTitle( const Song *pSong1, const Song *pSong2 )
{
const std::string &sGroup1 = pSong1->m_sGroupName;
const std::string &sGroup2 = pSong2->m_sGroupName;
if( sGroup1 < sGroup2 )
return true;
if( sGroup1 > sGroup2 )
return false;
/* Same group; compare by name. */
return CompareSongPointersByTitle( pSong1, pSong2 );
}
void SongUtil::SortSongPointerArrayByGroupAndTitle( vector<Song*> &vpSongsInOut )
{
sort( vpSongsInOut.begin(), vpSongsInOut.end(), CompareSongPointersByGroupAndTitle );
}
void SongUtil::SortSongPointerArrayByNumPlays( vector<Song*> &vpSongsInOut, ProfileSlot slot, bool bDescending )
{
if( !PROFILEMAN->IsPersistentProfile(slot) )
return; // nothing to do since we don't have data
Profile* pProfile = PROFILEMAN->GetProfile(slot);
SortSongPointerArrayByNumPlays( vpSongsInOut, pProfile, bDescending );
}
void SongUtil::SortSongPointerArrayByNumPlays( vector<Song*> &vpSongsInOut, const Profile* pProfile, bool bDescending )
{
ASSERT( pProfile != nullptr );
for (auto *song: vpSongsInOut)
{
g_mapSongSortVal[song] = fmt::sprintf("%9i", pProfile->GetSongNumTimesPlayed(song));
}
stable_sort( vpSongsInOut.begin(), vpSongsInOut.end(), bDescending ? CompareSongPointersBySortValueDescending : CompareSongPointersBySortValueAscending );
g_mapSongSortVal.clear();
}
std::string SongUtil::GetSectionNameFromSongAndSort( const Song* pSong, SortOrder so )
{
if( pSong == nullptr )
return std::string();
switch( so )
{
case SORT_PREFERRED:
return SONGMAN->SongToPreferredSortSectionName( pSong );
case SORT_GROUP:
// guaranteed not empty
return pSong->m_sGroupName;
case SORT_TITLE:
case SORT_ARTIST:
{
std::string s;
switch( so )
{
case SORT_TITLE: s = pSong->GetTranslitMainTitle(); break;
case SORT_ARTIST: s = pSong->GetTranslitArtist(); break;
default:
FAIL_M(fmt::sprintf("Unexpected SortOrder: %i", so));
}
s = MakeSortString(s); // resulting string will be uppercase
if (s.empty())
{
return "";
}
if (s[0] >= '0' && s[0] <= '9')
{
return "0-9";
}
if (s[0] < 'A' || s[0] > 'Z')
{
return SORT_OTHER.GetValue();
}
return Rage::head(s, 1);
}
case SORT_GENRE:
if( !pSong->m_sGenre.empty() )
return pSong->m_sGenre;
return SORT_NOT_AVAILABLE.GetValue();
case SORT_BPM:
{
if( SHOW_SECTIONS_IN_BPM_SORT )
{
const int iBPMGroupSize = SORT_BPM_DIVISION;
DisplayBpms bpms;
pSong->GetDisplayBpms( bpms );
int iMaxBPM = (int)bpms.GetMax();
iMaxBPM += iBPMGroupSize - (iMaxBPM%iBPMGroupSize) - 1;
return fmt::sprintf("%03d-%03d",iMaxBPM-(iBPMGroupSize-1), iMaxBPM);
}
else
return std::string();
}
case SORT_LENGTH:
{
if( SHOW_SECTIONS_IN_LENGTH_SORT )
{
const int iSortLengthSize = SORT_LENGTH_DIVISION;
int iMaxLength = static_cast<int>(pSong->m_fMusicLengthSeconds);
iMaxLength += (iSortLengthSize - (iMaxLength%iSortLengthSize) - 1);
int iMinLength = iMaxLength - (iSortLengthSize-1);
return fmt::sprintf("%s-%s",
SecondsToMMSS(static_cast<float>(iMinLength)).c_str(),
SecondsToMMSS(static_cast<float>(iMaxLength)).c_str());
}
else
return std::string();
}
case SORT_POPULARITY:
case SORT_RECENT:
return std::string();
case SORT_TOP_GRADES:
{
int iCounts[NUM_Grade];
PROFILEMAN->GetMachineProfile()->GetGrades( pSong, GAMESTATE->GetCurrentStyle(GAMESTATE->GetMasterPlayerNumber())->m_StepsType, iCounts );
for( int i=Grade_Tier01; i<NUM_Grade; ++i )
{
Grade g = (Grade)i;
if( iCounts[i] > 0 )
return fmt::sprintf( "%4s x %d", GradeToLocalizedString(g).c_str(), iCounts[i] );
}
return GradeToLocalizedString( Grade_NoData );
}
case SORT_BEGINNER_METER:
case SORT_EASY_METER:
case SORT_MEDIUM_METER:
case SORT_HARD_METER:
case SORT_CHALLENGE_METER:
case SORT_DOUBLE_EASY_METER:
case SORT_DOUBLE_MEDIUM_METER:
case SORT_DOUBLE_HARD_METER:
case SORT_DOUBLE_CHALLENGE_METER:
{
StepsType st;
Difficulty dc;
SongUtil::GetStepsTypeAndDifficultyFromSortOrder( so, st, dc );
Steps* pSteps = GetStepsByDifficulty(pSong,st,dc);
if( pSteps && !UNLOCKMAN->StepsIsLocked(pSong,pSteps) )
return fmt::sprintf("%02d", pSteps->GetMeter() );
return SORT_NOT_AVAILABLE.GetValue();
}
case SORT_MODE_MENU:
return std::string();
case SORT_ALL_COURSES:
case SORT_NONSTOP_COURSES:
case SORT_ONI_COURSES:
case SORT_ENDLESS_COURSES:
default:
FAIL_M(fmt::sprintf("Invalid SortOrder: %i", so));
}
}
void SongUtil::SortSongPointerArrayBySectionName( vector<Song*> &vpSongsInOut, SortOrder so )
{
std::string sOther = SORT_OTHER.GetValue();
for (auto *song: vpSongsInOut)
{
std::string val = GetSectionNameFromSongAndSort( song, so );
// Make sure 0-9 comes first and OTHER comes last.
if( val == "0-9" ) val = "0";
else if( val == sOther ) val = "2";
else val = "1" + MakeSortString(val);
g_mapSongSortVal[song] = val;
}
stable_sort( vpSongsInOut.begin(), vpSongsInOut.end(), CompareSongPointersBySortValueAscending );
g_mapSongSortVal.clear();
}
void SongUtil::SortSongPointerArrayByStepsTypeAndMeter( vector<Song*> &vpSongsInOut, StepsType st, Difficulty dc )
{
g_mapSongSortVal.clear();
for (auto *song: vpSongsInOut)
{
// Ignore locked steps.
const Steps* pSteps = GetClosestNotes( song, st, dc, true );
std::string &s = g_mapSongSortVal[song];
s = fmt::sprintf("%03d", pSteps ? pSteps->GetMeter() : 0);
/* pSteps may not be exactly the difficulty we want; for example, we
* may be sorting by Hard difficulty and a song may have no Hard steps.
* In this case, we can end up with unintuitive ties; for example, pSteps
* may be Medium with a meter of 5, which will sort it among the 5-meter
* Hard songs. Break the tie, by adding the difficulty to the sort as
* well. That way, we'll always put Medium 5s before Hard 5s. If all
* songs are using the preferred difficulty (dc), this will be a no-op. */
s += fmt::sprintf( "%c", (pSteps? pSteps->GetDifficulty():0) + '0' );
if( PREFSMAN->m_bSubSortByNumSteps )
s += fmt::sprintf("%06.0f",pSteps ? pSteps->GetRadarValues(PLAYER_1)[RadarCategory_TapsAndHolds] : 0);
}
stable_sort( vpSongsInOut.begin(), vpSongsInOut.end(), CompareSongPointersBySortValueAscending );
}
void SongUtil::SortByMostRecentlyPlayedForMachine( vector<Song*> &vpSongsInOut )
{
Profile *pProfile = PROFILEMAN->GetMachineProfile();
for (auto *s: vpSongsInOut)
{
int iNumTimesPlayed = pProfile->GetSongNumTimesPlayed( s );
std::string val = iNumTimesPlayed ? pProfile->GetSongLastPlayedDateTime(s).GetString() : "0";
g_mapSongSortVal[s] = val;
}
stable_sort( vpSongsInOut.begin(), vpSongsInOut.end(), CompareSongPointersBySortValueDescending );
g_mapSongSortVal.clear();
}
bool SongUtil::IsEditDescriptionUnique( const Song* pSong, StepsType st, const std::string &sPreferredDescription, const Steps *pExclude )
{
for (auto *pSteps: pSong->GetAllSteps())
{
if( pSteps->GetDifficulty() != Difficulty_Edit )
continue;
if( pSteps->m_StepsType != st )
continue;
if( pSteps == pExclude )
continue;
if( pSteps->GetDescription() == sPreferredDescription )
return false;
}
return true;
}
bool SongUtil::IsChartNameUnique( const Song* pSong, StepsType st, const std::string &name, const Steps *pExclude )
{
for (auto *pSteps: pSong->GetAllSteps())
{
if( pSteps->m_StepsType != st )
continue;
if( pSteps == pExclude )
continue;
if( pSteps->GetChartName() == name )
return false;
}
return true;
}
std::string SongUtil::MakeUniqueEditDescription( const Song *pSong, StepsType st, const std::string &sPreferredDescription )
{
if( IsEditDescriptionUnique( pSong, st, sPreferredDescription, nullptr ) )
return sPreferredDescription;
std::string sTemp;
for( int i=0; i<1000; i++ )
{
// make name "My Edit" -> "My Edit2"
std::string sNum = fmt::sprintf("%d", i+1);
sTemp = Rage::head(sPreferredDescription, MAX_STEPS_DESCRIPTION_LENGTH - sNum.size()) + sNum;
if (IsEditDescriptionUnique(pSong, st, sTemp, nullptr))
{
return sTemp;
}
}
// Edit limit guards should prevent this
FAIL_M("Exceeded limit of 1000 edits per song");
}
static LocalizedString YOU_MUST_SUPPLY_NAME ( "SongUtil", "You must supply a name for your new edit." );
static LocalizedString CHART_NAME_CONFLICTS ("SongUtil", "The name you chose conflicts with another chart. Please use a different name.");
static LocalizedString EDIT_NAME_CONFLICTS ( "SongUtil", "The name you chose conflicts with another edit. Please use a different name." );
static LocalizedString CHART_NAME_CANNOT_CONTAIN ("SongUtil", "The chart name cannot contain any of the following characters: %s" );
static LocalizedString EDIT_NAME_CANNOT_CONTAIN ( "SongUtil", "The edit name cannot contain any of the following characters: %s" );
bool SongUtil::ValidateCurrentEditStepsDescription( const std::string &sAnswer, std::string &sErrorOut )
{
Steps *pSteps = GAMESTATE->m_pCurSteps[PLAYER_1];
Song *pSong = pSteps->m_pSong;
ASSERT( pSteps->IsAnEdit() );
if( sAnswer.empty() )
{
sErrorOut = YOU_MUST_SUPPLY_NAME.GetValue();
return false;
}
static const std::string sInvalidChars = "\\/:*?\"<>|";
if( strpbrk(sAnswer.c_str(), sInvalidChars.c_str()) != nullptr )
{
sErrorOut = rage_fmt_wrapper(EDIT_NAME_CANNOT_CONTAIN, sInvalidChars.c_str() );
return false;
}
// Steps name must be unique for this song.
vector<Steps*> v;
GetSteps( pSong, v, StepsType_Invalid, Difficulty_Edit );
for (auto *s: v)
{
if( pSteps == s )
continue; // don't compare name against ourself
if( s->GetDescription() == sAnswer )
{
sErrorOut = EDIT_NAME_CONFLICTS.GetValue();
return false;
}
}
return true;
}
bool SongUtil::ValidateCurrentStepsDescription( const std::string &sAnswer, std::string &sErrorOut )
{
if( sAnswer.empty() )
return true;
/* Don't allow duplicate edit names within the same StepsType; edit names
* uniquely identify the edit. */
Steps *pSteps = GAMESTATE->m_pCurSteps[PLAYER_1];
// If unchanged:
if( pSteps->GetDescription() == sAnswer )
return true;
if( pSteps->IsAnEdit() )
{
return SongUtil::ValidateCurrentEditStepsDescription( sAnswer, sErrorOut );
}
return true;
}
bool SongUtil::ValidateCurrentStepsChartName(const std::string &answer, std::string &error)
{
if (answer.empty()) return true;
static const std::string sInvalidChars = "\\/:*?\"<>|";
if( strpbrk(answer.c_str(), sInvalidChars.c_str()) != nullptr )
{
error = rage_fmt_wrapper(CHART_NAME_CANNOT_CONTAIN, sInvalidChars.c_str() );
return false;
}
/* Don't allow duplicate title names within the same StepsType.
* We need some way of identifying the unique charts. */
Steps *pSteps = GAMESTATE->m_pCurSteps[PLAYER_1];
if (pSteps->GetChartName() == answer) return true;
// TODO next commit: borrow code from EditStepsDescription.
bool result = SongUtil::IsChartNameUnique(GAMESTATE->get_curr_song(), pSteps->m_StepsType,
answer, pSteps);
if (!result)
{
error = CHART_NAME_CONFLICTS.GetValue();
}
return result;
}
static LocalizedString AUTHOR_NAME_CANNOT_CONTAIN( "SongUtil", "The step author's name cannot contain any of the following characters: %s" );
bool SongUtil::ValidateCurrentStepsCredit( const std::string &sAnswer, std::string &sErrorOut )
{
if( sAnswer.empty() )
return true;
Steps *pSteps = GAMESTATE->m_pCurSteps[PLAYER_1];
// If unchanged:
if( pSteps->GetCredit() == sAnswer )
return true;
// Borrow from EditDescription testing. Perhaps this should be abstracted? -Wolfman2000
static const std::string sInvalidChars = "\\/:*?\"<>|";
if( strpbrk(sAnswer.c_str(), sInvalidChars.c_str()) != nullptr )
{
sErrorOut = rage_fmt_wrapper(AUTHOR_NAME_CANNOT_CONTAIN, sInvalidChars.c_str() );
return false;
}
return true;
}
static LocalizedString PREVIEW_DOES_NOT_EXIST("SongUtil", "The preview file '%s' does not exist.");
bool SongUtil::ValidateCurrentSongPreview(const std::string& answer, std::string& error)
{
if(answer.empty())
{ return true; }
Song* song= GAMESTATE->get_curr_song();
std::string real_file= song->m_PreviewFile;
song->m_PreviewFile= answer;
std::string path= song->GetPreviewMusicPath();
bool valid= DoesFileExist(path);
song->m_PreviewFile= real_file;
if(!valid)
{
error= rage_fmt_wrapper(PREVIEW_DOES_NOT_EXIST, answer.c_str());
}
return valid;
}
static LocalizedString MUSIC_DOES_NOT_EXIST("SongUtil", "The music file '%s' does not exist.");
bool SongUtil::ValidateCurrentStepsMusic(const std::string &answer, std::string &error)
{
if(answer.empty())
return true;
Steps *pSteps = GAMESTATE->m_pCurSteps[PLAYER_1];
std::string real_file= pSteps->GetMusicFile();
pSteps->SetMusicFile(answer);
std::string path= pSteps->GetMusicPath();
bool valid= DoesFileExist(path);
pSteps->SetMusicFile(real_file);
if(!valid)
{
error= rage_fmt_wrapper(MUSIC_DOES_NOT_EXIST, answer.c_str());
}
return valid;
}
void SongUtil::GetAllSongGenres( vector<std::string> &vsOut )
{
std::set<std::string> genres;
for (auto *song: SONGMAN->GetAllSongs())
{
if( !song->m_sGenre.empty() )
{
genres.insert( song->m_sGenre );
}
}
for (auto const &s: genres)
{
vsOut.push_back( s );
}
}
void SongUtil::FilterSongs( const SongCriteria &sc, const vector<Song*> &in,
vector<Song*> &out, bool doCareAboutGame )
{
out.reserve( in.size() );
for (auto *s: in)
{
if( sc.Matches( s ) && (!doCareAboutGame || IsSongPlayable(s) ) )
{
out.push_back( s );
}
}
}
void SongUtil::GetPlayableStepsTypes( const Song *pSong, std::set<StepsType> &vOut )
{
vector<const Style*> vpPossibleStyles;
// If AutoSetStyle, or a Style hasn't been chosen, check StepsTypes for all Styles.
if( CommonMetrics::AUTO_SET_STYLE || GAMESTATE->GetCurrentStyle(PLAYER_INVALID) == nullptr )
GAMEMAN->GetCompatibleStyles( GAMESTATE->m_pCurGame, GAMESTATE->GetNumPlayersEnabled(), vpPossibleStyles );
else
vpPossibleStyles.push_back( GAMESTATE->GetCurrentStyle(PLAYER_INVALID) );
// Only allow OneSide Styles in Workout
if( GAMESTATE->m_bMultiplayer )
{
for( int i=vpPossibleStyles.size()-1; i>=0; i-- )
{
const Style *pStyle = vpPossibleStyles[i];
switch( pStyle->m_StyleType )
{
DEFAULT_FAIL( pStyle->m_StyleType );
case StyleType_OnePlayerOneSide: