From 19c36e8723dc7daa52789f6914da1f871c1b604d Mon Sep 17 00:00:00 2001 From: Barinade Date: Tue, 11 Jun 2024 02:02:12 -0500 Subject: [PATCH] move InstallSmzip to SongManager and make it less bad hhhhhhhhhhgfbvc nb --- .../Screen/Others/ScreenInstallOverlay.cpp | 2 +- src/Etterna/Singletons/DownloadManager.cpp | 102 +----------------- src/Etterna/Singletons/DownloadManager.h | 4 - src/Etterna/Singletons/SongManager.cpp | 70 ++++++++++++ src/Etterna/Singletons/SongManager.h | 4 + 5 files changed, 76 insertions(+), 106 deletions(-) diff --git a/src/Etterna/Screen/Others/ScreenInstallOverlay.cpp b/src/Etterna/Screen/Others/ScreenInstallOverlay.cpp index 0d12470565..27e436f1ce 100644 --- a/src/Etterna/Screen/Others/ScreenInstallOverlay.cpp +++ b/src/Etterna/Screen/Others/ScreenInstallOverlay.cpp @@ -43,7 +43,7 @@ InstallSmzipOsArg(const std::string& sOsZipFile) if (!FILEMAN->Mount("dir", sOsDir, TEMP_OS_MOUNT_POINT)) FAIL_M("Failed to mount " + sOsDir); - DownloadManager::InstallSmzip(TEMP_OS_MOUNT_POINT + sFilename + sExt); + SongManager::InstallSmzip(TEMP_OS_MOUNT_POINT + sFilename + sExt); FILEMAN->Unmount("dir", sOsDir, TEMP_OS_MOUNT_POINT); } diff --git a/src/Etterna/Singletons/DownloadManager.cpp b/src/Etterna/Singletons/DownloadManager.cpp index c948d6a78a..b49ac513a0 100644 --- a/src/Etterna/Singletons/DownloadManager.cpp +++ b/src/Etterna/Singletons/DownloadManager.cpp @@ -55,9 +55,6 @@ static Preference maxDLPerSecond( static Preference maxDLPerSecondGameplay( "maximumBytesDownloadedPerSecondDuringGameplay", 1000000); -static Preference downloadPacksToAdditionalSongs( - "downloadPacksToAdditionalSongs", - 0); static Preference maxPacksToDownloadAtOnce( "parallelDownloadsAllowed", 1); @@ -74,7 +71,6 @@ static Preference uiHomePage("BaseUIUrl", "https://beta.etternaonli static Preference automaticSync("automaticScoreSync", 1); // -static const std::string TEMP_ZIP_MOUNT_POINT = "/@temp-zip/"; static const std::string DL_DIR = SpecialFiles::CACHE_DIR + "Downloads/"; static const std::string wife3_rescore_upload_flag = "rescoredw3"; @@ -180,102 +176,6 @@ ComputerIdentity() return computerName + ":_:" + userName; } -bool -DownloadManager::InstallSmzip(const std::string& sZipFile) -{ - if (!FILEMAN->Mount("zip", sZipFile, TEMP_ZIP_MOUNT_POINT)) - FAIL_M(static_cast("Failed to mount " + sZipFile).c_str()); - std::vector v_packs; - FILEMAN->GetDirListing(TEMP_ZIP_MOUNT_POINT + "*", v_packs, ONLY_DIR, true); - - std::string doot = TEMP_ZIP_MOUNT_POINT; - if (v_packs.size() > 1) { - // attempt to whitelist pack name, this - // should be pretty simple/safe solution for - // a lot of pad packs -mina - doot += sZipFile.substr(sZipFile.find_last_of('/') + 1); - doot = doot.substr(0, doot.length() - 4) + "/"; - } - - std::vector vsFiles; - { - std::vector vsRawFiles; - GetDirListingRecursive(doot, "*", vsRawFiles); - - if (vsRawFiles.empty()) { - FILEMAN->Unmount("zip", sZipFile, TEMP_ZIP_MOUNT_POINT); - return false; - } - - std::vector vsPrettyFiles; - for (auto& s : vsRawFiles) { - if (EqualsNoCase(GetExtension(s), "ctl")) - continue; - - vsFiles.push_back(s); - - std::string s2 = - tail(s, s.length() - TEMP_ZIP_MOUNT_POINT.length()); - vsPrettyFiles.push_back(s2); - } - sort(vsPrettyFiles.begin(), vsPrettyFiles.end()); - } - std::string sResult = "Success installing " + sZipFile; - std::string extractTo = - downloadPacksToAdditionalSongs ? "AdditionalSongs/" : "Songs/"; - for (auto& sSrcFile : vsFiles) { - std::string sDestFile = sSrcFile; - sDestFile = tail(std::string(sDestFile.c_str()), - sDestFile.length() - TEMP_ZIP_MOUNT_POINT.length()); - - // forcibly convert the path string to ASCII/ANSI/whatever - // basically remove everything that isnt normal - // and dont care about locales - std::vector bytes(sDestFile.begin(), sDestFile.end()); - bytes.push_back('\0'); - std::string res{}; - auto hashName = false; - for (auto i = 0; i < bytes.size(); i++) { - auto c = bytes.at(i); - if (c > 122) { - hashName = true; - break; - } else { - res.push_back(c); - } - } - if (res.length() > 256) { - res = res.substr(0, 255); - } - if (hashName) { - // take a partial hash of the filename - auto ext = GetExtension(sDestFile); - res = BinaryToHex(CryptManager::GetSHA1ForString(sDestFile)); - if (res.length() > 10) { - res = - res.substr(0, std::min(static_cast(res.length()), 15)); - } - res = res + ext; - - } - - sDestFile = res; - - std::string sDir, sThrowAway; - splitpath(sDestFile, sDir, sThrowAway, sThrowAway); - - if (!FileCopy(sSrcFile, extractTo + sDestFile)) { - sResult = "Error extracting " + sDestFile; - break; - } - } - - FILEMAN->Unmount("zip", sZipFile, TEMP_ZIP_MOUNT_POINT); - - SCREENMAN->SystemMessage(sResult); - return true; -} - inline void EmptyTempDLFileDir() { @@ -6137,7 +6037,7 @@ Download::Install() { Core::Platform::requestUserAttention(); Message* msg; - if (!DownloadManager::InstallSmzip(m_TempFileName)) + if (!SongManager::InstallSmzip(m_TempFileName)) msg = new Message("DownloadFailed"); else msg = new Message("PackDownloaded"); diff --git a/src/Etterna/Singletons/DownloadManager.h b/src/Etterna/Singletons/DownloadManager.h index 13002da306..9f9071c341 100644 --- a/src/Etterna/Singletons/DownloadManager.h +++ b/src/Etterna/Singletons/DownloadManager.h @@ -447,10 +447,6 @@ class DownloadManager void UpdateHTTP(float fDeltaSeconds); void UpdateGameplayState(bool gameplay); - ///// - // External static util - static bool InstallSmzip(const std::string& zipFile); - ///// // Lua external access void PushSelf(lua_State* L); diff --git a/src/Etterna/Singletons/SongManager.cpp b/src/Etterna/Singletons/SongManager.cpp index ccf7df7268..9fdaf99a84 100644 --- a/src/Etterna/Singletons/SongManager.cpp +++ b/src/Etterna/Singletons/SongManager.cpp @@ -74,6 +74,10 @@ static Preference g_sDisabledSongs("DisabledSongs", ""); static Preference PlaylistsAreSongGroups("PlaylistsAreSongGroups", false); static Preference CacheZipsContainAllAssets("CacheZipsContainAllAssets", true); +static Preference downloadPacksToAdditionalSongs( + "downloadPacksToAdditionalSongs", + 0); +static const std::string TEMP_ZIP_MOUNT_POINT = "/@temp-zip/"; auto SONG_GROUP_COLOR_NAME(size_t i) -> std::string @@ -125,6 +129,72 @@ SongManager::InitAll(LoadingWindow* ld) LoadCalcTestNode(); } +bool +SongManager::InstallSmzip(const std::string& sZipFile) +{ + miniz_cpp::zip_file fi(sZipFile); + // no ext + auto zipfilename = + sZipFile.substr(sZipFile.find_last_of('/') + 1, sZipFile.length() - 4); + // path being extracted + std::string doot = sZipFile; + + auto names = fi.namelist(); + std::set folders{}; + for (auto& name : names) { + if (name.find_first_of('/') == std::string::npos) { + // this is probably a random file in the root + continue; + } + auto realname = name.substr(0, name.find_first_of('/')); + folders.emplace(realname); + } + if (folders.size() > 0) { + auto foundCandidate = false; + if (folders.size() == 1) { + doot = *folders.begin(); + foundCandidate = true; + } else { + for (auto& fn : folders) { + if (make_lower(zipfilename) == make_lower(fn)) { + doot = fn; + foundCandidate = true; + break; + } + } + if (!foundCandidate) { + doot = *folders.begin(); + } + } + if (!foundCandidate) { + Locator::getLogger()->error("Couldn't find good enough folder to " + "extract in {}. Picked a random one ({})", sZipFile, doot); + } + } + else { + Locator::getLogger()->error("Found no folders in zip {}...", sZipFile); + return false; + } + + std::string extractTo = + downloadPacksToAdditionalSongs ? "/AdditionalSongs/" : "/Songs/"; + + auto filecnt = 0; + for (auto& member : names) { + if (member.starts_with(doot + "/")) { + fi.extract(member, + FILEMAN->ResolveSongFolder( + extractTo, downloadPacksToAdditionalSongs)); + filecnt++; + } + } + + auto msg = fmt::format( + "Finished extracting {} files for pack {}", filecnt, doot); + SCREENMAN->SystemMessage(msg); + return true; +} + static LocalizedString RELOADING("SongManager", "Reloading..."); static LocalizedString UNLOADING_SONGS("SongManager", "Unloading songs..."); static LocalizedString SANITY_CHECKING_GROUPS("SongManager", diff --git a/src/Etterna/Singletons/SongManager.h b/src/Etterna/Singletons/SongManager.h index 673a87fcff..45a6f899a2 100644 --- a/src/Etterna/Singletons/SongManager.h +++ b/src/Etterna/Singletons/SongManager.h @@ -167,6 +167,10 @@ class SongManager void AddKeyedPointers(Song* new_song); std::unordered_map SongsByKey; std::unordered_map StepsByKey; + + ///// + // External static util + static bool InstallSmzip(const std::string& zipFile); protected: void LoadStepManiaSongDir(std::string sDir, LoadingWindow* ld);