forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSongManager.cpp
2305 lines (2012 loc) · 66.5 KB
/
SongManager.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 "SongManager.h"
#include "arch/LoadingWindow/LoadingWindow.h"
#include "ActorUtil.h"
#include "AnnouncerManager.h"
#include "BackgroundUtil.h"
#include "BannerCache.h"
#include "CommonMetrics.h"
#include "Course.h"
#include "CourseLoaderCRS.h"
#include "CourseUtil.h"
#include "GameManager.h"
#include "GameState.h"
#include "LocalizedString.h"
#include "MsdFile.h"
#include "NoteSkinManager.h"
#include "NotesLoaderDWI.h"
#include "NotesLoaderSSC.h"
#include "NotesLoaderSM.h"
#include "PrefsManager.h"
#include "Profile.h"
#include "ProfileManager.h"
#include "RageFile.h"
#include "RageFileManager.h"
#include "RageLog.h"
#include "Song.h"
#include "SongCacheIndex.h"
#include "SongUtil.h"
#include "Sprite.h"
#include "StatsManager.h"
#include "Steps.h"
#include "StepsUtil.h"
#include "Style.h"
#include "ThemeManager.h"
#include "TitleSubstitution.h"
#include "TrailUtil.h"
#include "UnlockManager.h"
#include "SpecialFiles.h"
#include <unordered_map>
using std::vector;
SongManager* SONGMAN = nullptr; // global and accessible from anywhere in our program
const std::string ADDITIONAL_SONGS_DIR = "/AdditionalSongs/";
const std::string ADDITIONAL_COURSES_DIR = "/AdditionalCourses/";
const std::string EDIT_SUBDIR = "Edits/";
/** @brief The file that contains various random attacks. */
const std::string ATTACK_FILE = "/Data/RandomAttacks.txt";
static const ThemeMetric<Rage::Color> EXTRA_COLOR ( "SongManager", "ExtraColor" );
static const ThemeMetric<int> EXTRA_COLOR_METER ( "SongManager", "ExtraColorMeter" );
static const ThemeMetric<bool> USE_PREFERRED_SORT_COLOR ( "SongManager", "UsePreferredSortColor" );
static const ThemeMetric<bool> USE_UNLOCK_COLOR ( "SongManager", "UseUnlockColor" );
static const ThemeMetric<Rage::Color> UNLOCK_COLOR ( "SongManager", "UnlockColor" );
static const ThemeMetric<bool> MOVE_UNLOCKS_TO_BOTTOM_OF_PREFERRED_SORT ( "SongManager", "MoveUnlocksToBottomOfPreferredSort" );
static const ThemeMetric<int> EXTRA_STAGE2_DIFFICULTY_MAX ( "SongManager", "ExtraStage2DifficultyMax" );
static Preference<std::string> g_sDisabledSongs( "DisabledSongs", "" );
static Preference<bool> g_bHideIncompleteCourses( "HideIncompleteCourses", false );
std::string SONG_GROUP_COLOR_NAME( size_t i ) { return fmt::sprintf( "SongGroupColor%i", (int) i+1 ); }
std::string COURSE_GROUP_COLOR_NAME( size_t i ) { return fmt::sprintf( "CourseGroupColor%i", (int) i+1 ); }
std::string profile_song_group_color_name(size_t i) { return fmt::sprintf("ProfileSongGroupColor%i", (int)i+1); }
static const float next_loading_window_update= 0.02f;
SongManager::SongManager()
{
// Register with Lua.
{
Lua *L = LUA->Get();
lua_pushstring( L, "SONGMAN" );
this->PushSelf( L );
lua_settable( L, LUA_GLOBALSINDEX );
LUA->Release( L );
}
NUM_SONG_GROUP_COLORS .Load( "SongManager", "NumSongGroupColors" );
SONG_GROUP_COLOR .Load( "SongManager", SONG_GROUP_COLOR_NAME, NUM_SONG_GROUP_COLORS );
NUM_COURSE_GROUP_COLORS .Load( "SongManager", "NumCourseGroupColors" );
COURSE_GROUP_COLOR .Load( "SongManager", COURSE_GROUP_COLOR_NAME, NUM_COURSE_GROUP_COLORS );
num_profile_song_group_colors.Load("SongManager", "NumProfileSongGroupColors");
profile_song_group_colors.Load("SongManager", profile_song_group_color_name, num_profile_song_group_colors);
}
SongManager::~SongManager()
{
// Unregister with Lua.
LUA->UnsetGlobal( "SONGMAN" );
// Courses depend on Songs and Songs don't depend on Courses.
// So, delete the Courses first.
FreeCourses();
FreeSongs();
}
void SongManager::InitAll( LoadingWindow *ld )
{
auto never_cache = Rage::split(PREFSMAN->m_NeverCacheList.Get(), ",");
for (auto &group: never_cache)
{
m_GroupsToNeverCache.insert(group);
}
InitSongsFromDisk( ld );
InitCoursesFromDisk( ld );
InitAutogenCourses();
InitRandomAttacks();
}
static LocalizedString RELOADING ( "SongManager", "Reloading..." );
static LocalizedString UNLOADING_SONGS ( "SongManager", "Unloading songs..." );
static LocalizedString UNLOADING_COURSES ( "SongManager", "Unloading courses..." );
void SongManager::Reload( bool bAllowFastLoad, LoadingWindow *ld )
{
FILEMAN->FlushDirCache( SpecialFiles::SONGS_DIR );
FILEMAN->FlushDirCache( ADDITIONAL_SONGS_DIR );
FILEMAN->FlushDirCache( SpecialFiles::COURSES_DIR );
FILEMAN->FlushDirCache( ADDITIONAL_COURSES_DIR );
FILEMAN->FlushDirCache( EDIT_SUBDIR );
if( ld )
ld->SetText( RELOADING.GetValue() );
// save scores before unloading songs, or the scores will be lost
PROFILEMAN->SaveMachineProfile();
if( ld )
ld->SetText( UNLOADING_COURSES.GetValue() );
FreeCourses();
if( ld )
ld->SetText( UNLOADING_SONGS.GetValue() );
FreeSongs();
const bool OldVal = PREFSMAN->m_bFastLoad;
if( !bAllowFastLoad )
PREFSMAN->m_bFastLoad.Set( false );
InitAll( ld );
// reload scores and unlocks afterward
PROFILEMAN->LoadMachineProfile();
UNLOCKMAN->Reload();
if( !bAllowFastLoad )
PREFSMAN->m_bFastLoad.Set( OldVal );
UpdatePreferredSort();
}
void SongManager::InitSongsFromDisk( LoadingWindow *ld )
{
RageTimer tm;
// Tell SONGINDEX to not write the cache index file every time a song adds
// an entry. -Kyz
SONGINDEX->delay_save_cache= true;
LoadStepManiaSongDir( SpecialFiles::SONGS_DIR, ld );
const bool bOldVal = PREFSMAN->m_bFastLoad;
PREFSMAN->m_bFastLoad.Set( PREFSMAN->m_bFastLoadAdditionalSongs );
LoadStepManiaSongDir( ADDITIONAL_SONGS_DIR, ld );
PREFSMAN->m_bFastLoad.Set( bOldVal );
LoadEnabledSongsFromPref();
SONGINDEX->SaveCacheIndex();
SONGINDEX->delay_save_cache= false;
LOG->Trace( "Found %d songs in %f seconds.", (int)m_pSongs.size(), tm.GetDeltaTime() );
}
static LocalizedString FOLDER_CONTAINS_MUSIC_FILES( "SongManager", "The folder \"%s\" appears to be a song folder. All song folders must reside in a group folder. For example, \"Songs/Originals/My Song\"." );
void SongManager::AddGroup(std::string dir, std::string group_dir_name)
{
if(m_song_groups.find(dir) != m_song_groups.end())
{
return; // the group is already added
}
// Look for a group banner in this group folder
vector<std::string> images;
std::string slash_group_name= dir + group_dir_name + "/";
FILEMAN->GetDirListingWithMultipleExtensions(slash_group_name,
ActorUtil::GetTypeExtensionList(FT_Bitmap), images);
std::string banner_path;
if(!images.empty())
{
banner_path= slash_group_name + images[0];
}
else
{
banner_path= get_possible_group_banner(group_dir_name);
}
/* Other group graphics are a bit trickier, and usually don't exist.
* A themer has a few options, namely checking the aspect ratio and
* operating on it. -aj
* TODO: Once the files are implemented in Song, bring the extensions
* from there into here. -aj */
// Group background
//vector<std::string> arrayGroupBackgrounds;
//GetDirListing( sDir+sGroupDirName+"/*-bg.png", arrayGroupBanners );
//GetDirListing( sDir+sGroupDirName+"/*-bg.jpg", arrayGroupBanners );
//GetDirListing( sDir+sGroupDirName+"/*-bg.jpeg", arrayGroupBanners );
//GetDirListing( sDir+sGroupDirName+"/*-bg.gif", arrayGroupBanners );
//GetDirListing( sDir+sGroupDirName+"/*-bg.bmp", arrayGroupBanners );
/*
std::string sBackgroundPath;
if( !arrayGroupBackgrounds.empty() )
sBackgroundPath = sDir+sGroupDirName+"/"+arrayGroupBackgrounds[0];
else
{
// Look for a group background in the parent folder
GetDirListing( sDir+sGroupDirName+"-bg.png", arrayGroupBackgrounds );
GetDirListing( sDir+sGroupDirName+"-bg.jpg", arrayGroupBackgrounds );
GetDirListing( sDir+sGroupDirName+"-bg.jpeg", arrayGroupBackgrounds );
GetDirListing( sDir+sGroupDirName+"-bg.gif", arrayGroupBackgrounds );
GetDirListing( sDir+sGroupDirName+"-bg.bmp", arrayGroupBackgrounds );
if( !arrayGroupBackgrounds.empty() )
sBackgroundPath = sDir+arrayGroupBackgrounds[0];
}
*/
/*
LOG->Trace( "Group banner for '%s' is '%s'.", sGroupDirName.c_str(),
sBannerPath != ""? sBannerPath.c_str():"(none)" );
*/
GroupEntry entry= {m_song_groups.size(), banner_path};
m_song_groups.insert(make_pair(group_dir_name, entry));
m_song_group_names.push_back(group_dir_name);
//m_sSongGroupBackgroundPaths.push_back( sBackgroundPath );
}
static LocalizedString LOADING_SONGS ( "SongManager", "Loading songs..." );
void SongManager::LoadStepManiaSongDir( std::string sDir, LoadingWindow *ld )
{
// Compositors and other stuff can impose some overhead on updating the
// loading window, which slows down startup time for some people.
// loading_window_last_update_time provides a timer so the loading window
// isn't updated after every song and course. -Kyz
RageTimer loading_window_last_update_time;
loading_window_last_update_time.Touch();
// Make sure sDir has a trailing slash.
if (!Rage::ends_with(sDir, "/"))
{
sDir += "/";
}
// Find all group directories in "Songs" folder
vector<std::string> arrayGroupDirs;
GetDirListing( sDir+"*", arrayGroupDirs, true );
StripCvsAndSvn( arrayGroupDirs );
StripMacResourceForks( arrayGroupDirs );
SortStringArray( arrayGroupDirs );
// TODO: Add a function to FILEMAN for getting dirs and files in separate
// vectors, and use that to combine the above GetDirListing and this one,
// so the list is only fetched once. -Kyz
m_possible_group_banners.clear();
vector<std::string> images_in_dir;
FILEMAN->GetDirListingWithMultipleExtensions(sDir,
ActorUtil::GetTypeExtensionList(FT_Bitmap), images_in_dir);
for(auto&& image : images_in_dir)
{
m_possible_group_banners.insert(make_pair(
GetFileNameWithoutExtension(image), sDir + image));
}
int const updates_per_group= 50;
if(ld)
{
ld->SetIndeterminate(false);
ld->SetTotalWork(arrayGroupDirs.size() * updates_per_group);
}
auto const & audio_exts= ActorUtil::GetTypeExtensionList(FT_Sound);
for(size_t group_index= 0; group_index < arrayGroupDirs.size(); ++group_index)
{
auto& group_name= arrayGroupDirs[group_index];
vector<std::string> song_folders;
GetDirListing(sDir + group_name + "/*", song_folders, true, true);
StripCvsAndSvn(song_folders);
StripMacResourceForks(song_folders);
// Song Select sorts songs by name, already, doesn't it? Why do the song
// names need to be sorted during loading? -Kyz
// SortStringArray(song_folders);
LOG->Trace("Attempting to load %i songs from \"%s\"",
int(song_folders.size()), sDir+group_name);
int loaded= 0;
SongPointerVector& index_entry = m_mapSongGroupIndex[group_name];
auto group_base_name= Rage::base_name(group_name);
for(size_t song_index= 0; song_index < song_folders.size(); ++song_index)
{
auto& song_dir_name= song_folders[song_index];
// Check to see if they put a song directly inside the group folder.
// TODO: If this check fails, log a warning instead of crashing.
// -Unknown
// I think crashing is the only way to get the message across so people
// put songs in the right place. -Kyz
std::string const ext= GetExtension(song_dir_name);
if(!ext.empty())
{
for(auto&& aud : audio_exts)
{
if(ext == aud)
{
RageException::Throw(
FOLDER_CONTAINS_MUSIC_FILES.GetValue(), sDir.c_str());
}
}
}
// this is a song directory. Load a new song.
if(ld && loading_window_last_update_time.Ago() > next_loading_window_update)
{
loading_window_last_update_time.Touch();
ld->SetProgress((group_index * updates_per_group) +
(Rage::scale(int(song_index), 0, int(song_folders.size()), 0, updates_per_group)));
ld->SetText(LOADING_SONGS.GetValue() +
fmt::sprintf("\n%s\n%s",
group_base_name.c_str(),
Rage::base_name(song_dir_name).c_str()));
}
Song* new_song= new Song;
if(!new_song->LoadFromSongDir(song_dir_name))
{
// The song failed to load.
delete new_song;
continue;
}
AddSongToList(new_song);
index_entry.push_back(new_song);
++loaded;
}
LOG->Trace("Loaded %i songs from \"%s\"", loaded, (sDir+group_name).c_str());
// Don't add the group name if we didn't load any songs in this group.
if(loaded <= 0) { continue; }
// Add this group to the group array.
AddGroup(sDir, group_name);
// Cache and load the group banner. (and background if it has one -aj)
BANNERCACHE->CacheBanner(GetSongGroupBannerPath(group_name));
// Load the group sym links (if any)
LoadGroupSymLinks(sDir, group_name);
}
m_possible_group_banners.clear();
if( ld ) {
ld->SetIndeterminate( true );
}
}
// Instead of "symlinks", songs should have membership in multiple groups. -Chris
void SongManager::LoadGroupSymLinks(std::string sDir, std::string sGroupFolder)
{
// Find all symlink files in this folder
vector<std::string> arraySymLinks;
GetDirListing( sDir+sGroupFolder+"/*.include", arraySymLinks, false );
SortStringArray( arraySymLinks );
SongPointerVector& index_entry = m_mapSongGroupIndex[sGroupFolder];
for (auto &symlink: arraySymLinks)
{
MsdFile msdF;
msdF.ReadFile( sDir+sGroupFolder+"/"+symlink.c_str(), false ); // don't unescape
std::string sSymDestination = msdF.GetParam(0,1); // Should only be 1 value & param...period.
Song* pNewSong = new Song;
if( !pNewSong->LoadFromSongDir( sSymDestination ) )
{
delete pNewSong; // The song failed to load.
}
else
{
const vector<Steps*>& vpSteps = pNewSong->GetAllSteps();
while( vpSteps.size() )
pNewSong->DeleteSteps( vpSteps[0] );
FOREACH_BackgroundLayer( i )
pNewSong->GetBackgroundChanges(i).clear();
pNewSong->m_bIsSymLink = true; // Very important so we don't double-parse later
pNewSong->m_sGroupName = sGroupFolder;
AddSongToList(pNewSong);
index_entry.push_back( pNewSong );
}
}
}
void SongManager::PreloadSongImages()
{
if( PREFSMAN->m_BannerCache != BNCACHE_FULL )
return;
/* Load textures before unloading old ones, so we don't reload textures
* that we don't need to. */
RageTexturePreloader preload;
const vector<Song*> &songs = GetAllSongs();
for (auto const *song: songs)
{
if( !song->HasBanner() )
{
continue;
}
const RageTextureID ID = Sprite::SongBannerTexture( song->GetBannerPath() );
preload.Load( ID );
}
vector<Course*> courses;
GetAllCourses( courses, false );
for (auto const *course: courses)
{
if( !course->HasBanner() )
{
continue;
}
const RageTextureID ID = Sprite::SongBannerTexture( course->GetBannerPath() );
preload.Load( ID );
}
preload.Swap( m_TexturePreload );
}
void SongManager::FreeSongs()
{
m_song_groups.clear();
//m_sSongGroupBackgroundPaths.clear();
for (auto *song: m_pSongs)
{
Rage::safe_delete( song );
}
m_pSongs.clear();
m_SongsByDir.clear();
// also free the songs that have been deleted from disk
for ( unsigned i=0; i<m_pDeletedSongs.size(); ++i )
Rage::safe_delete( m_pDeletedSongs[i] );
m_pDeletedSongs.clear();
m_mapSongGroupIndex.clear();
m_pPopularSongs.clear();
m_pShuffledSongs.clear();
}
void SongManager::UnlistSong(Song *song)
{
// cannot immediately free song data, as it is needed temporarily for smooth audio transitions, etc.
// Instead, remove it from the m_pSongs list and store it in a special place where it can safely be deleted later.
m_pDeletedSongs.push_back(song);
// remove all occurences of the song in each of our song vectors
vector<Song*>* songVectors[3] = { &m_pSongs, &m_pPopularSongs, &m_pShuffledSongs };
for (int songVecIdx=0; songVecIdx<3; ++songVecIdx) {
vector<Song*>& v = *songVectors[songVecIdx];
for (size_t i = 0; i < v.size(); ++i) {
if (v[i] == song) {
v.erase(v.begin()+i);
--i;
}
}
}
}
bool SongManager::IsGroupNeverCached(const std::string& group) const
{
return m_GroupsToNeverCache.find(group) != m_GroupsToNeverCache.end();
}
std::string SongManager::GetSongGroupBannerPath(std::string const& group_name) const
{
auto entry= m_song_groups.find(group_name);
if(entry != m_song_groups.end())
{
return entry->second.banner_path;
}
return std::string();
}
/*
std::string SongManager::GetSongGroupBackgroundPath( std::string sSongGroup ) const
{
for( unsigned i = 0; i < m_sSongGroupNames.size(); ++i )
{
if( sSongGroup == m_sSongGroupNames[i] )
return m_sSongGroupBackgroundPaths[i];
}
return std::string();
}
*/
void SongManager::GetSongGroupNames( vector<std::string> &AddTo ) const
{
AddTo.reserve(AddTo.size() + m_song_group_names.size());
for(auto&& entry : m_song_group_names)
{
AddTo.push_back(entry);
}
}
bool SongManager::DoesSongGroupExist(std::string const& group_name) const
{
return m_song_groups.find(group_name) != m_song_groups.end();
}
Rage::Color SongManager::GetSongGroupColor(std::string const& group_name) const
{
auto entry= m_song_groups.find(group_name);
if(entry != m_song_groups.end())
{
return SONG_GROUP_COLOR.GetValue(entry->second.id % NUM_SONG_GROUP_COLORS);
}
FOREACH_EnabledPlayer(pn)
{
Profile* prof= PROFILEMAN->GetProfile(pn);
if(prof != nullptr)
{
if(prof->GetDisplayNameOrHighScoreName() == group_name)
{
return profile_song_group_colors.GetValue(pn % num_profile_song_group_colors);
}
}
}
LuaHelpers::ReportScriptError(fmt::sprintf(
"requested color for song group '%s' that doesn't exist",
group_name.c_str()));
return Rage::Color(1,1,1,1);
}
Rage::Color SongManager::GetSongColor( const Song* pSong ) const
{
ASSERT( pSong != nullptr );
// protected by royal freem corporation. any modification/removal of
// this code will result in prosecution.
if( pSong->m_sMainTitle == "DVNO")
return Rage::Color(1.0f,0.8f,0.0f,1.0f);
// end royal freem protection
// Use unlock color if applicable
const UnlockEntry *pUE = UNLOCKMAN->FindSong( pSong );
if( pUE && USE_UNLOCK_COLOR.GetValue() )
return UNLOCK_COLOR.GetValue();
if( USE_PREFERRED_SORT_COLOR )
{
for (auto v = m_vPreferredSongSort.begin(); v != m_vPreferredSongSort.end(); ++v)
{
for (auto const *s: v->vpSongs)
{
if( s == pSong )
{
int i = v - m_vPreferredSongSort.begin();
return SONG_GROUP_COLOR.GetValue( i%NUM_SONG_GROUP_COLORS );
}
}
}
int i = m_vPreferredSongSort.size();
return SONG_GROUP_COLOR.GetValue( i%NUM_SONG_GROUP_COLORS );
}
else // TODO: Have a better fallback plan with colors?
{
/* XXX: Previously, this matched all notes, which set a song to "extra"
* if it had any 10-foot steps at all, even edits or doubles.
*
* For now, only look at notes for the current note type. This means
* that if a song has 10-foot steps on Doubles, it'll only show up red
* in Doubles. That's not too bad, I think. This will also change it
* in the song scroll, which is a little odd but harmless.
*
* XXX: Ack. This means this function can only be called when we have
* a style set up, which is too restrictive. How to handle this? */
//const StepsType st = GAMESTATE->GetCurrentStyle()->m_StepsType;
const vector<Steps*>& vpSteps = pSong->GetAllSteps();
for (auto const *pSteps: vpSteps)
{
switch( pSteps->GetDifficulty() )
{
case Difficulty_Challenge:
case Difficulty_Edit:
continue;
default: break;
}
//if(pSteps->m_StepsType != st)
// continue;
if( pSteps->GetMeter() >= EXTRA_COLOR_METER )
return (Rage::Color)EXTRA_COLOR;
}
return GetSongGroupColor( pSong->m_sGroupName );
}
}
std::string SongManager::GetCourseGroupBannerPath( const std::string &sCourseGroup ) const
{
auto iter = m_mapCourseGroupToInfo.find(sCourseGroup);
if( iter == m_mapCourseGroupToInfo.end() )
{
LuaHelpers::ReportScriptError(fmt::sprintf("requested banner for course group '%s' that doesn't exist",sCourseGroup.c_str()));
return std::string();
}
else
{
return iter->second.m_sBannerPath;
}
}
void SongManager::GetCourseGroupNames( vector<std::string> &AddTo ) const
{
for (auto const &iter: m_mapCourseGroupToInfo)
{
AddTo.push_back( iter.first );
}
}
bool SongManager::DoesCourseGroupExist( const std::string &sCourseGroup ) const
{
return m_mapCourseGroupToInfo.find(sCourseGroup) != m_mapCourseGroupToInfo.end();
}
Rage::Color SongManager::GetCourseGroupColor( const std::string &sCourseGroup ) const
{
int iIndex = 0;
for (auto const &iter: m_mapCourseGroupToInfo)
{
if( iter.first == sCourseGroup )
{
return SONG_GROUP_COLOR.GetValue( iIndex%NUM_SONG_GROUP_COLORS );
}
iIndex++;
}
LuaHelpers::ReportScriptError(fmt::sprintf("requested color for course group '%s' that doesn't exist",sCourseGroup.c_str()));
return Rage::Color(1,1,1,1);
}
Rage::Color SongManager::GetCourseColor( const Course* pCourse ) const
{
// Use unlock color if applicable
const UnlockEntry *pUE = UNLOCKMAN->FindCourse( pCourse );
if( pUE && USE_UNLOCK_COLOR.GetValue() )
return UNLOCK_COLOR.GetValue();
if( USE_PREFERRED_SORT_COLOR )
{
for (auto v = m_vPreferredCourseSort.begin(); v != m_vPreferredCourseSort.end(); ++v)
{
for (auto const *s: *v)
{
if( s == pCourse )
{
int i = v - m_vPreferredCourseSort.begin();
CHECKPOINT_M( fmt::sprintf( "%i, NUM_COURSE_GROUP_COLORS = %i", i, NUM_COURSE_GROUP_COLORS.GetValue()) );
return COURSE_GROUP_COLOR.GetValue( i % NUM_COURSE_GROUP_COLORS );
}
}
}
int i = m_vPreferredCourseSort.size();
CHECKPOINT_M( fmt::sprintf( "%i, NUM_COURSE_GROUP_COLORS = %i", i, NUM_COURSE_GROUP_COLORS.GetValue()) );
return COURSE_GROUP_COLOR.GetValue( i % NUM_COURSE_GROUP_COLORS );
}
else
{
return GetCourseGroupColor( pCourse->m_sGroupName );
}
}
void SongManager::ResetGroupColors()
{
// Reload song/course group colors to prevent a crash when switching
// themes in-game. (apparently not, though.) -aj
SONG_GROUP_COLOR.Clear();
COURSE_GROUP_COLOR.Clear();
NUM_SONG_GROUP_COLORS .Load( "SongManager", "NumSongGroupColors" );
SONG_GROUP_COLOR .Load( "SongManager", SONG_GROUP_COLOR_NAME, NUM_SONG_GROUP_COLORS );
NUM_COURSE_GROUP_COLORS .Load( "SongManager", "NumCourseGroupColors" );
COURSE_GROUP_COLOR .Load( "SongManager", COURSE_GROUP_COLOR_NAME, NUM_COURSE_GROUP_COLORS );
}
const vector<Song*> &SongManager::GetSongs( const std::string &sGroupName ) const
{
static const vector<Song*> vEmpty;
if( sGroupName == GROUP_ALL )
return m_pSongs;
auto iter = m_mapSongGroupIndex.find(sGroupName);
if ( iter != m_mapSongGroupIndex.end() )
return iter->second;
FOREACH_EnabledPlayer(pn)
{
Profile* prof= PROFILEMAN->GetProfile(pn);
if(prof != nullptr)
{
if(prof->GetDisplayNameOrHighScoreName() == sGroupName)
{
return prof->m_songs;
}
}
}
return vEmpty;
}
void SongManager::GetPreferredSortSongs( vector<Song*> &AddTo ) const
{
if( m_vPreferredSongSort.empty() )
{
AddTo.insert( AddTo.end(), m_pSongs.begin(), m_pSongs.end() );
return;
}
for (auto const &v: m_vPreferredSongSort)
{
AddTo.insert( AddTo.end(), v.vpSongs.begin(), v.vpSongs.end() );
}
}
std::string SongManager::SongToPreferredSortSectionName( const Song *pSong ) const
{
for (auto const &v: m_vPreferredSongSort)
{
for (auto const *s: v.vpSongs)
{
if( s == pSong )
{
return v.sName;
}
}
}
return std::string();
}
void SongManager::GetPreferredSortCourses( CourseType ct, vector<Course*> &AddTo, bool bIncludeAutogen ) const
{
if( m_vPreferredCourseSort.empty() )
{
GetCourses( ct, AddTo, bIncludeAutogen );
return;
}
for (auto const &v: m_vPreferredCourseSort)
{
for (auto *pCourse: v)
{
if( pCourse->GetCourseType() == ct )
{
AddTo.push_back( pCourse );
}
}
}
}
int SongManager::GetNumSongs() const
{
return m_pSongs.size();
}
int SongManager::GetNumLockedSongs() const
{
auto isSongLocked = [](Song const *s) {
return UNLOCKMAN->SongIsLocked(s);
};
return std::count_if(m_pSongs.begin(), m_pSongs.end(), isSongLocked);
}
int SongManager::GetNumUnlockedSongs() const
{
auto isSongLocked = [](Song const *s) {
return UNLOCKMAN->SongIsLocked(s) & ~LOCKED_LOCK;
};
return std::count_if(m_pSongs.begin(), m_pSongs.end(), isSongLocked);
}
int SongManager::GetNumSelectableAndUnlockedSongs() const
{
auto isSongLocked = [](Song const *s) {
return UNLOCKMAN->SongIsLocked(s) & ~(LOCKED_LOCK | LOCKED_SELECTABLE);
};
return std::count_if(m_pSongs.begin(), m_pSongs.end(), isSongLocked);
}
int SongManager::GetNumAdditionalSongs() const
{
auto wasLoadedFromAdditional = [this](Song const *s) {
return WasLoadedFromAdditionalSongs(s);
};
return std::count_if(m_pSongs.begin(), m_pSongs.end(), wasLoadedFromAdditional);
}
int SongManager::GetNumSongGroups() const
{
return m_song_groups.size();
}
int SongManager::GetNumCourses() const
{
return m_pCourses.size();
}
int SongManager::GetNumAdditionalCourses() const
{
auto wasLoadedFromAdditional = [this](Course const *c) {
return WasLoadedFromAdditionalCourses(c);
};
return std::count_if(m_pCourses.begin(), m_pCourses.end(), wasLoadedFromAdditional);
}
int SongManager::GetNumCourseGroups() const
{
return m_mapCourseGroupToInfo.size();
}
std::string SongManager::ShortenGroupName( std::string sLongGroupName )
{
static TitleSubst tsub("Groups");
TitleFields title;
title.Title = sLongGroupName;
tsub.Subst( title );
return title.Title;
}
static LocalizedString LOADING_COURSES ( "SongManager", "Loading courses..." );
void SongManager::InitCoursesFromDisk( LoadingWindow *ld )
{
LOG->Trace( "Loading courses." );
RageTimer loading_window_last_update_time;
loading_window_last_update_time.Touch();
vector<std::string> vsCourseDirs;
vsCourseDirs.push_back( SpecialFiles::COURSES_DIR );
vsCourseDirs.push_back( ADDITIONAL_COURSES_DIR );
vector<std::string> vsCourseGroupNames;
for (auto const &sDir: vsCourseDirs)
{
// Find all group directories in Courses dir
GetDirListing( sDir + "*", vsCourseGroupNames, true, true );
StripCvsAndSvn( vsCourseGroupNames );
StripMacResourceForks( vsCourseGroupNames );
}
// Search for courses both in COURSES_DIR and in subdirectories
vsCourseGroupNames.push_back( SpecialFiles::COURSES_DIR );
SortStringArray( vsCourseGroupNames );
int courseIndex = 0;
for (auto const &sCourseGroup: vsCourseGroupNames)
{
// Find all CRS files in this group directory
vector<std::string> vsCoursePaths;
GetDirListing( sCourseGroup + "/*.crs", vsCoursePaths, false, true );
SortStringArray( vsCoursePaths );
if( ld )
{
ld->SetIndeterminate( false );
ld->SetTotalWork( vsCoursePaths.size() );
}
auto base_course_group= Rage::base_name(sCourseGroup);
for (auto const &sCoursePath: vsCoursePaths)
{
if(ld && loading_window_last_update_time.Ago() > next_loading_window_update)
{
loading_window_last_update_time.Touch();
ld->SetProgress(courseIndex);
ld->SetText( LOADING_COURSES.GetValue()+fmt::sprintf("\n%s\n%s",
base_course_group.c_str(),
Rage::base_name(sCoursePath).c_str()));
}
Course* pCourse = new Course;
CourseLoaderCRS::LoadFromCRSFile( sCoursePath, *pCourse );
if( g_bHideIncompleteCourses.Get() && pCourse->m_bIncomplete )
{
delete pCourse;
continue;
}
m_pCourses.push_back( pCourse );
courseIndex++;
}
}
if( ld ) {
ld->SetIndeterminate( true );
}
RefreshCourseGroupInfo();
}
void SongManager::InitAutogenCourses()
{
// Create group courses for Endless and Nonstop
vector<std::string> saGroupNames;
this->GetSongGroupNames( saGroupNames );
Course* pCourse;
for (auto const &sGroupName: saGroupNames)
{
// Generate random courses from each group.
pCourse = new Course;
CourseUtil::AutogenEndlessFromGroup( sGroupName, Difficulty_Medium, *pCourse );
pCourse->m_sScripter = "Autogen";
m_pCourses.push_back( pCourse );
pCourse = new Course;
CourseUtil::AutogenNonstopFromGroup( sGroupName, Difficulty_Medium, *pCourse );
pCourse->m_sScripter = "Autogen";
m_pCourses.push_back( pCourse );
}
vector<Song*> apCourseSongs = GetAllSongs();
// Generate "All Songs" endless course.
pCourse = new Course;
CourseUtil::AutogenEndlessFromGroup( "", Difficulty_Medium, *pCourse );
pCourse->m_sScripter = "Autogen";
m_pCourses.push_back( pCourse );
/* Generate Oni courses from artists. Only create courses if we have at least
* four songs from an artist; create 3- and 4-song courses. */
{
/* We normally sort by translit artist. However, display artist is more
* consistent. For example, transliterated Japanese names are alternately
* spelled given- and family-name first, but display titles are more consistent. */
vector<Song*> apSongs = this->GetAllSongs();
SongUtil::SortSongPointerArrayByDisplayArtist( apSongs );
std::string sCurArtist = "";
std::string sCurArtistTranslit = "";
int iCurArtistCount = 0;
vector<Song *> aSongs;
unsigned i = 0;
Rage::ci_ascii_string unknownArtist{ "Unknown artist" };
do {
std::string sArtist = i >= apSongs.size()? std::string(""): apSongs[i]->GetDisplayArtist();
std::string sTranslitArtist = i >= apSongs.size()? std::string(""): apSongs[i]->GetTranslitArtist();
Rage::ci_ascii_string ciArtist{ sArtist.c_str() };
if( i < apSongs.size() && ciArtist == sCurArtist )
{
aSongs.push_back( apSongs[i] );
++iCurArtistCount;
continue;
}
/* Different artist, or we're at the end. If we have enough entries for
* the last artist, add it. Skip blanks and "Unknown artist". */
if( iCurArtistCount >= 3 && sCurArtistTranslit != "" &&
unknownArtist != sCurArtist && unknownArtist != sCurArtistTranslit )
{
pCourse = new Course;
CourseUtil::AutogenOniFromArtist( sCurArtist, sCurArtistTranslit, aSongs, Difficulty_Hard, *pCourse );
pCourse->m_sScripter = "Autogen";
m_pCourses.push_back( pCourse );
}
aSongs.clear();
if( i < apSongs.size() )
{
sCurArtist = sArtist;
sCurArtistTranslit = sTranslitArtist;
iCurArtistCount = 1;
aSongs.push_back( apSongs[i] );
}
} while( i++ < apSongs.size() );
}
}
void SongManager::InitRandomAttacks()
{
GAMESTATE->m_RandomAttacks.clear();
if( !IsAFile(ATTACK_FILE) )
LOG->Trace( "File Data/RandomAttacks.txt was not found" );
else
{
MsdFile msd;
if( !msd.ReadFile( ATTACK_FILE, true ) )
LuaHelpers::ReportScriptErrorFmt( "Error opening file '%s' for reading: %s.", ATTACK_FILE.c_str(), msd.GetError().c_str() );
else
{
for( unsigned i=0; i<msd.GetNumValues(); i++ )
{