Skip to content

Commit

Permalink
move InstallSmzip to SongManager and make it less bad
Browse files Browse the repository at this point in the history
hhhhhhhhhhgfbvc nb
  • Loading branch information
poco0317 committed Jun 11, 2024
1 parent 11a6eae commit 19c36e8
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 106 deletions.
2 changes: 1 addition & 1 deletion src/Etterna/Screen/Others/ScreenInstallOverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
102 changes: 1 addition & 101 deletions src/Etterna/Singletons/DownloadManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ static Preference<unsigned int> maxDLPerSecond(
static Preference<unsigned int> maxDLPerSecondGameplay(
"maximumBytesDownloadedPerSecondDuringGameplay",
1000000);
static Preference<unsigned int> downloadPacksToAdditionalSongs(
"downloadPacksToAdditionalSongs",
0);
static Preference<unsigned int> maxPacksToDownloadAtOnce(
"parallelDownloadsAllowed",
1);
Expand All @@ -74,7 +71,6 @@ static Preference<std::string> uiHomePage("BaseUIUrl", "https://beta.etternaonli
static Preference<unsigned int> 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";

Expand Down Expand Up @@ -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<std::string>("Failed to mount " + sZipFile).c_str());
std::vector<std::string> 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<std::string> vsFiles;
{
std::vector<std::string> vsRawFiles;
GetDirListingRecursive(doot, "*", vsRawFiles);

if (vsRawFiles.empty()) {
FILEMAN->Unmount("zip", sZipFile, TEMP_ZIP_MOUNT_POINT);
return false;
}

std::vector<std::string> 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<unsigned char> 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<int>(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()
{
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 0 additions & 4 deletions src/Etterna/Singletons/DownloadManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
70 changes: 70 additions & 0 deletions src/Etterna/Singletons/SongManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ static Preference<std::string> g_sDisabledSongs("DisabledSongs", "");
static Preference<bool> PlaylistsAreSongGroups("PlaylistsAreSongGroups", false);
static Preference<bool> CacheZipsContainAllAssets("CacheZipsContainAllAssets",
true);
static Preference<unsigned int> downloadPacksToAdditionalSongs(
"downloadPacksToAdditionalSongs",
0);
static const std::string TEMP_ZIP_MOUNT_POINT = "/@temp-zip/";

auto
SONG_GROUP_COLOR_NAME(size_t i) -> std::string
Expand Down Expand Up @@ -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<std::string> 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",
Expand Down
4 changes: 4 additions & 0 deletions src/Etterna/Singletons/SongManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ class SongManager
void AddKeyedPointers(Song* new_song);
std::unordered_map<std::string, Song*> SongsByKey;
std::unordered_map<std::string, Steps*> StepsByKey;

/////
// External static util
static bool InstallSmzip(const std::string& zipFile);

protected:
void LoadStepManiaSongDir(std::string sDir, LoadingWindow* ld);
Expand Down

0 comments on commit 19c36e8

Please sign in to comment.