Skip to content

Commit

Permalink
GetDirListing actually can filter dirs and files; fix exts breaking s…
Browse files Browse the repository at this point in the history
…ong load

this fixes the crashing that occurs when you put a random audio file in a pack folder. this also fixes the crashes that occur if you name a song folder with a regular file extension. this also fixes errors that occur when naming a song folder as an image. and everything in between kind of. these song folders should load properly if they are real song folders. if you happen to do it in a particularly wrong way and song load ends prematurely you also get a very obvious warning in the log
  • Loading branch information
poco0317 committed Feb 28, 2023
1 parent 77ee5a6 commit 71991a7
Show file tree
Hide file tree
Showing 31 changed files with 287 additions and 148 deletions.
2 changes: 1 addition & 1 deletion src/Etterna/Actor/Base/ActorUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ ActorUtil::ResolvePath(std::string& sPath,
const auto ft = FILEMAN->GetFileType(sPath);
if (ft != RageFileManager::TYPE_FILE && ft != RageFileManager::TYPE_DIR) {
std::vector<std::string> asPaths;
GetDirListing(sPath + "*", asPaths, false, true); // return path too
FILEMAN->GetDirListing(sPath + "*", asPaths, false, true); // return path too

if (asPaths.empty()) {
if (optional) {
Expand Down
25 changes: 17 additions & 8 deletions src/Etterna/Actor/GameplayAndMenus/BGAnimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "BGAnimationLayer.h"
#include "Etterna/FileTypes/IniFile.h"
#include "Etterna/Singletons/PrefsManager.h"
#include "RageUtil/File/RageFileManager.h"
#include "RageUtil/Utils/RageUtil.h"

#include <algorithm>
Expand Down Expand Up @@ -105,14 +106,22 @@ BGAnimation::LoadFromAniDir(const std::string& _sAniDir)
std::vector<std::string> asImagePaths;
ASSERT(sAniDir != "");

GetDirListing(sAniDir + "*.png", asImagePaths, false, true);
GetDirListing(sAniDir + "*.jpg", asImagePaths, false, true);
GetDirListing(sAniDir + "*.jpeg", asImagePaths, false, true);
GetDirListing(sAniDir + "*.gif", asImagePaths, false, true);
GetDirListing(sAniDir + "*.ogv", asImagePaths, false, true);
GetDirListing(sAniDir + "*.avi", asImagePaths, false, true);
GetDirListing(sAniDir + "*.mpg", asImagePaths, false, true);
GetDirListing(sAniDir + "*.mpeg", asImagePaths, false, true);
FILEMAN->GetDirListing(
sAniDir + "*.png", asImagePaths, ONLY_FILE, true);
FILEMAN->GetDirListing(
sAniDir + "*.jpg", asImagePaths, ONLY_FILE, true);
FILEMAN->GetDirListing(
sAniDir + "*.jpeg", asImagePaths, ONLY_FILE, true);
FILEMAN->GetDirListing(
sAniDir + "*.gif", asImagePaths, ONLY_FILE, true);
FILEMAN->GetDirListing(
sAniDir + "*.ogv", asImagePaths, ONLY_FILE, true);
FILEMAN->GetDirListing(
sAniDir + "*.avi", asImagePaths, ONLY_FILE, true);
FILEMAN->GetDirListing(
sAniDir + "*.mpg", asImagePaths, ONLY_FILE, true);
FILEMAN->GetDirListing(
sAniDir + "*.mpeg", asImagePaths, ONLY_FILE, true);

SortStringArray(asImagePaths);

Expand Down
3 changes: 2 additions & 1 deletion src/Etterna/Models/Fonts/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "FontCharmaps.h"
#include "Core/Services/Locator.hpp"
#include "RageUtil/Graphics/RageTextureManager.h"
#include "RageUtil/File/RageFileManager.h"
#include "RageUtil/Utils/RageUtil.h"
#include "Etterna/Singletons/ThemeManager.h"
#include "arch/Dialog/Dialog.h"
Expand Down Expand Up @@ -466,7 +467,7 @@ Font::GetFontPaths(const std::string& sFontIniPath,
{
std::string sPrefix = SetExtension(sFontIniPath, "");
std::vector<std::string> asFiles;
GetDirListing(sPrefix + "*", asFiles, false, true);
FILEMAN->GetDirListing(sPrefix + "*", asFiles, false, true);

for (auto& asFile : asFiles) {
if (!EqualsNoCase(tail(asFile, 4), ".ini"))
Expand Down
30 changes: 16 additions & 14 deletions src/Etterna/Models/Misc/BackgroundUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ BackgroundUtil::GetBackgroundEffects(const std::string& _sName,
sName = "*";

vsPathsOut.clear();
GetDirListing(
BACKGROUND_EFFECTS_DIR + sName + ".lua", vsPathsOut, false, true);
FILEMAN->GetDirListing(
BACKGROUND_EFFECTS_DIR + sName + ".lua", vsPathsOut, ONLY_FILE, true);

vsNamesOut.clear();
for (auto& s : vsPathsOut)
Expand All @@ -172,10 +172,10 @@ BackgroundUtil::GetBackgroundTransitions(const std::string& _sName,
sName = "*";

vsPathsOut.clear();
GetDirListing(
BACKGROUND_TRANSITIONS_DIR + sName + ".xml", vsPathsOut, false, true);
GetDirListing(
BACKGROUND_TRANSITIONS_DIR + sName + ".lua", vsPathsOut, false, true);
FILEMAN->GetDirListing(
BACKGROUND_TRANSITIONS_DIR + sName + ".xml", vsPathsOut, ONLY_FILE, true);
FILEMAN->GetDirListing(
BACKGROUND_TRANSITIONS_DIR + sName + ".lua", vsPathsOut, ONLY_FILE, true);

vsNamesOut.clear();
for (auto& s : vsPathsOut)
Expand All @@ -190,9 +190,9 @@ BackgroundUtil::GetSongBGAnimations(const Song* pSong,
{
vsPathsOut.clear();
if (sMatch.empty()) {
GetDirListing(pSong->GetSongDir() + "*", vsPathsOut, true, true);
FILEMAN->GetDirListing(pSong->GetSongDir() + "*", vsPathsOut, true, true);
} else {
GetDirListing(pSong->GetSongDir() + sMatch, vsPathsOut, true, true);
FILEMAN->GetDirListing(pSong->GetSongDir() + sMatch, vsPathsOut, true, true);
}

vsNamesOut.clear();
Expand All @@ -212,10 +212,11 @@ BackgroundUtil::GetSongMovies(const Song* pSong,
pSong->GetSongDir() + sMatch,
ActorUtil::GetTypeExtensionList(FT_Movie),
vsPathsOut,
false,
ONLY_FILE,
true);
} else {
GetDirListing(pSong->GetSongDir() + sMatch, vsPathsOut, false, true);
FILEMAN->GetDirListing(
pSong->GetSongDir() + sMatch, vsPathsOut, ONLY_FILE, true);
}

vsNamesOut.clear();
Expand All @@ -235,10 +236,11 @@ BackgroundUtil::GetSongBitmaps(const Song* pSong,
pSong->GetSongDir() + sMatch,
ActorUtil::GetTypeExtensionList(FT_Bitmap),
vsPathsOut,
false,
ONLY_FILE,
true);
} else {
GetDirListing(pSong->GetSongDir() + sMatch, vsPathsOut, false, true);
FILEMAN->GetDirListing(
pSong->GetSongDir() + sMatch, vsPathsOut, ONLY_FILE, true);
}

vsNamesOut.clear();
Expand All @@ -253,8 +255,8 @@ BackgroundUtil::GetGlobalBGAnimations(const Song* pSong,
std::vector<std::string>& vsNamesOut)
{
vsPathsOut.clear();
GetDirListing(BG_ANIMS_DIR + sMatch + "*", vsPathsOut, true, true);
GetDirListing(BG_ANIMS_DIR + sMatch + "*.xml", vsPathsOut, false, true);
FILEMAN->GetDirListing(BG_ANIMS_DIR + sMatch + "*", vsPathsOut, true, true);
FILEMAN->GetDirListing(BG_ANIMS_DIR + sMatch + "*.xml", vsPathsOut, ONLY_FILE, true);

vsNamesOut.clear();
for (auto& s : vsPathsOut)
Expand Down
2 changes: 1 addition & 1 deletion src/Etterna/Models/Misc/ImageCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ EmptyDir(std::string dir)
ASSERT(dir[dir.size() - 1] == '/');

std::vector<std::string> asCacheFileNames;
GetDirListing(dir, asCacheFileNames);
FILEMAN->GetDirListing(dir, asCacheFileNames);
for (auto& asCacheFileName : asCacheFileNames) {
if (!IsADirectory(dir + asCacheFileName))
FILEMAN->Remove(dir + asCacheFileName);
Expand Down
3 changes: 2 additions & 1 deletion src/Etterna/Models/Misc/Profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,8 @@ Profile::MakeUniqueFileNameNoExtension(const std::string& sDir,
FILEMAN->FlushDirCache(sDir);
// Find a file name for the screenshot
std::vector<std::string> files;
GetDirListing(sDir + sFileNameBeginning + "*", files, false, false);
FILEMAN->GetDirListing(
sDir + sFileNameBeginning + "*", files, false, false);
sort(files.begin(), files.end());

auto iIndex = 0;
Expand Down
9 changes: 5 additions & 4 deletions src/Etterna/Models/Misc/RandomSample.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Etterna/Globals/global.h"
#include "Core/Services/Locator.hpp"
#include "RageUtil/Sound/RageSound.h"
#include "RageUtil/File/RageFileManager.h"
#include "RageUtil/Utils/RageUtil.h"
#include "RandomSample.h"
#include "Etterna/Globals/rngthing.h"
Expand Down Expand Up @@ -43,10 +44,10 @@ RandomSample::LoadSoundDir(std::string sDir, int iMaxToLoad)
ensure_slash_at_end(sDir);

std::vector<std::string> arraySoundFiles;
GetDirListing(sDir + "*.mp3", arraySoundFiles);
GetDirListing(sDir + "*.oga", arraySoundFiles);
GetDirListing(sDir + "*.ogg", arraySoundFiles);
GetDirListing(sDir + "*.wav", arraySoundFiles);
FILEMAN->GetDirListing(sDir + "*.mp3", arraySoundFiles, ONLY_FILE);
FILEMAN->GetDirListing(sDir + "*.oga", arraySoundFiles, ONLY_FILE);
FILEMAN->GetDirListing(sDir + "*.ogg", arraySoundFiles, ONLY_FILE);
FILEMAN->GetDirListing(sDir + "*.wav", arraySoundFiles, ONLY_FILE);

std::shuffle(
arraySoundFiles.begin(), arraySoundFiles.end(), g_RandomNumberGenerator);
Expand Down
10 changes: 5 additions & 5 deletions src/Etterna/Models/NoteLoaders/NotesLoaderBMS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ void
BMSLoader::GetApplicableFiles(const std::string& sPath,
std::vector<std::string>& out)
{
GetDirListing(sPath + std::string("*.bms"), out);
GetDirListing(sPath + std::string("*.bme"), out);
GetDirListing(sPath + std::string("*.bml"), out);
GetDirListing(sPath + std::string("*.pms"), out);
FILEMAN->GetDirListing(sPath + std::string("*.bms"), out, ONLY_FILE);
FILEMAN->GetDirListing(sPath + std::string("*.bme"), out, ONLY_FILE);
FILEMAN->GetDirListing(sPath + std::string("*.bml"), out, ONLY_FILE);
FILEMAN->GetDirListing(sPath + std::string("*.pms"), out, ONLY_FILE);
}

/*===========================================================================*/
Expand Down Expand Up @@ -774,7 +774,7 @@ BMSSong::PrecacheBackgrounds(const std::string& dir)
ActorUtil::AddTypeExtensionsToList(FT_Movie, exts);
ActorUtil::AddTypeExtensionsToList(FT_Bitmap, exts);
FILEMAN->GetDirListingWithMultipleExtensions(
dir + std::string("*."), exts, arrayPossibleFiles);
dir + std::string("*."), exts, arrayPossibleFiles, ONLY_FILE);

for (auto& arrayPossibleFile : arrayPossibleFiles) {
for (auto& ext : exts) {
Expand Down
3 changes: 2 additions & 1 deletion src/Etterna/Models/NoteLoaders/NotesLoaderDWI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "NotesLoader.h"
#include "NotesLoaderDWI.h"
#include "Etterna/Singletons/PrefsManager.h"
#include "RageUtil/File/RageFileManager.h"
#include "RageUtil/Utils/RageUtil.h"
#include "RageUtil/Utils/RageUtil_CharConversions.h"
#include "Etterna/Models/Songs/Song.h"
Expand Down Expand Up @@ -568,7 +569,7 @@ void
DWILoader::GetApplicableFiles(const std::string& sPath,
std::vector<std::string>& out)
{
GetDirListing(sPath + std::string("*.dwi"), out);
FILEMAN->GetDirListing(sPath + std::string("*.dwi"), out, ONLY_FILE);
}

bool
Expand Down
27 changes: 16 additions & 11 deletions src/Etterna/Models/NoteLoaders/NotesLoaderKSF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Etterna/Models/NoteData/NoteData.h"
#include "Etterna/Models/Misc/NoteTypes.h"
#include "NotesLoaderKSF.h"
#include "RageUtil/File/RageFileManager.h"
#include "RageUtil/Utils/RageUtil.h"
#include "RageUtil/Utils/RageUtil_CharConversions.h"
#include "Etterna/Models/Songs/Song.h"
Expand Down Expand Up @@ -495,14 +496,18 @@ LoadGlobalData(const std::string& sPath, Song& out, bool& bKIUCompliant)
// changed up there in case of something is found inside the SONGFILE tag in
// the head ksf -DaisuMaster search for music with song in the file name
std::vector<std::string> arrayPossibleMusic;
GetDirListing(out.GetSongDir() + std::string("song.mp3"),
arrayPossibleMusic);
GetDirListing(out.GetSongDir() + std::string("song.oga"),
arrayPossibleMusic);
GetDirListing(out.GetSongDir() + std::string("song.ogg"),
arrayPossibleMusic);
GetDirListing(out.GetSongDir() + std::string("song.wav"),
arrayPossibleMusic);
FILEMAN->GetDirListing(out.GetSongDir() + std::string("song.mp3"),
arrayPossibleMusic,
ONLY_FILE);
FILEMAN->GetDirListing(out.GetSongDir() + std::string("song.oga"),
arrayPossibleMusic,
ONLY_FILE);
FILEMAN->GetDirListing(out.GetSongDir() + std::string("song.ogg"),
arrayPossibleMusic,
ONLY_FILE);
FILEMAN->GetDirListing(out.GetSongDir() + std::string("song.wav"),
arrayPossibleMusic,
ONLY_FILE);

if (!arrayPossibleMusic.empty()) // we found a match
out.m_sMusicFile = arrayPossibleMusic[0];
Expand Down Expand Up @@ -644,7 +649,7 @@ void
KSFLoader::GetApplicableFiles(const std::string& sPath,
std::vector<std::string>& out)
{
GetDirListing(sPath + std::string("*.ksf"), out);
FILEMAN->GetDirListing(sPath + std::string("*.ksf"), out, ONLY_FILE);
}

bool
Expand All @@ -666,10 +671,10 @@ KSFLoader::LoadNoteDataFromSimfile(const std::string& cachePath, Steps& out)
bool
KSFLoader::LoadFromDir(const std::string& sDir, Song& out)
{
// LOG->Trace("KSFLoader::LoadFromDir(%s)", sDir.c_str());

std::vector<std::string> arrayKSFFileNames;
GetDirListing(sDir + std::string("*.ksf"), arrayKSFFileNames);
FILEMAN->GetDirListing(
sDir + std::string("*.ksf"), arrayKSFFileNames, ONLY_FILE);

// We shouldn't have been called to begin with if there were no KSFs.
ASSERT(arrayKSFFileNames.size() != 0);
Expand Down
3 changes: 2 additions & 1 deletion src/Etterna/Models/NoteLoaders/NotesLoaderOSU.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Etterna/Globals/global.h"
#include "Etterna/Models/NoteData/NoteData.h"
#include "NotesLoaderOSU.h"
#include "RageUtil/File/RageFileManager.h"
#include "RageUtil/File/RageFile.h"
#include "RageUtil/Utils/RageUtil_CharConversions.h"
#include "Etterna/Models/Songs/Song.h"
Expand Down Expand Up @@ -278,7 +279,7 @@ void
OsuLoader::GetApplicableFiles(const std::string& sPath,
std::vector<std::string>& out)
{
GetDirListing(sPath + std::string("*.osu"), out);
FILEMAN->GetDirListing(sPath + std::string("*.osu"), out, ONLY_FILE);
}

int
Expand Down
3 changes: 2 additions & 1 deletion src/Etterna/Models/NoteLoaders/NotesLoaderSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,8 @@ SMLoader::LoadFromSimfile(const std::string& sPath, Song& out, bool bFromCache)
void
SMLoader::GetApplicableFiles(const std::string& sPath, std::vector<std::string>& out)
{
GetDirListing(sPath + std::string("*" + this->GetFileExtension()), out);
FILEMAN->GetDirListing(
sPath + std::string("*" + this->GetFileExtension()), out, ONLY_FILE);
}

void
Expand Down
45 changes: 30 additions & 15 deletions src/Etterna/Models/Songs/Song.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,19 +333,33 @@ Song::LoadFromSongDir(std::string sDir, Calc* calc)
// save song dir
m_sSongDir = sDir;

// if (!SONGINDEX->LoadSongFromCache(this, sDir)) {
// There was no entry in the cache for this song, or it was out of date.
// Let's load it from a file, then write a cache entry.
// load it from a file, then write a cache entry.
if (!NotesLoader::LoadFromDir(sDir, *this, BlacklistedImages)) {
Locator::getLogger()->debug(
"Song {} has no SSC, SM, SMA, DWI, BMS, KSF, or OSU files.", sDir);
if (!starts_with(sDir, "/")) {
Locator::getLogger()->warn(
"Directory {} appears to be a pack with a loose audio file. This "
"can crash the game! The rest of this pack folder will be "
"ignored!! If you are missing songs, this may be why!",
sDir);
} else {
Locator::getLogger()->info("Song {} has no SSC, SM, SMA, DWI, BMS, "
"KSF, or OSU files - ignoring",
sDir);
}
return false;

// OLD STUFF RELEVANT TO EDITORS (nonexistent in this game for now)
/*
std::vector<std::string> vs;
FILEMAN->GetDirListingWithMultipleExtensions(
sDir, ActorUtil::GetTypeExtensionList(FT_Sound), vs, false, false);
sDir,
ActorUtil::GetTypeExtensionList(FT_Sound),
vs,
ONLY_FILE,
false);
const auto bHasMusic = !vs.empty();
const auto bHasMusic = !vs.empty();
if (!bHasMusic) {
Locator::getLogger()->info(
"Song {} has no usable files detected. Ignoring this song directory.",
Expand All @@ -359,6 +373,7 @@ Song::LoadFromSongDir(std::string sDir, Calc* calc)
this->m_sSongFileName = sDir + songName;
// Continue on with a blank Song so that people can make adjustments
// using the editor.
*/
}

TidyUpData(false, true, calc);
Expand Down Expand Up @@ -590,7 +605,7 @@ Song::TidyUpData(bool from_cache, bool /* duringCache */, Calc* calc)
// different things we need. -Kyz
std::vector<std::string> song_dir_listing;
FILEMAN->GetDirListing(
m_sSongDir + "*", song_dir_listing, false, false);
m_sSongDir + "*", song_dir_listing, ONLY_FILE, false);
std::vector<std::string> music_list;
std::vector<std::string> image_list;
std::vector<std::string> movie_list;
Expand Down Expand Up @@ -1137,12 +1152,12 @@ Song::Save()
std::vector<std::string> backedDotOldFileNames;
std::vector<std::string> backedOrigFileNames;
std::vector<std::string> arrayOldFileNames;
GetDirListing(m_sSongDir + "*.bms", arrayOldFileNames);
GetDirListing(m_sSongDir + "*.pms", arrayOldFileNames);
GetDirListing(m_sSongDir + "*.ksf", arrayOldFileNames);
GetDirListing(m_sSongDir + "*.sm", arrayOldFileNames);
GetDirListing(m_sSongDir + "*.dwi", arrayOldFileNames);
GetDirListing(m_sSongDir + "*.osu", arrayOldFileNames);
FILEMAN->GetDirListing(m_sSongDir + "*.bms", arrayOldFileNames);
FILEMAN->GetDirListing(m_sSongDir + "*.pms", arrayOldFileNames);
FILEMAN->GetDirListing(m_sSongDir + "*.ksf", arrayOldFileNames);
FILEMAN->GetDirListing(m_sSongDir + "*.sm", arrayOldFileNames);
FILEMAN->GetDirListing(m_sSongDir + "*.dwi", arrayOldFileNames);
FILEMAN->GetDirListing(m_sSongDir + "*.osu", arrayOldFileNames);
for (auto& arrayOldFileName : arrayOldFileNames) {
const auto sOldPath = m_sSongDir + arrayOldFileName;
const auto sNewPath = sOldPath + ".old";
Expand Down Expand Up @@ -1367,7 +1382,7 @@ Song::GetCacheFile(const std::string& sType)

// Get all image files and put them into a vector.
std::vector<std::string> song_dir_listing;
FILEMAN->GetDirListing(m_sSongDir + "*", song_dir_listing, false, false);
FILEMAN->GetDirListing(m_sSongDir + "*", song_dir_listing, ONLY_FILE, false);
std::vector<std::string> image_list;
auto fill_exts = ActorUtil::GetTypeExtensionList(FT_Bitmap);
for (const auto& Image : song_dir_listing) {
Expand Down
Loading

0 comments on commit 71991a7

Please sign in to comment.