From f88bfc5d7d0fcc233406389d91d24f80407793ca Mon Sep 17 00:00:00 2001 From: Cvolton Date: Fri, 15 Sep 2023 02:53:17 +0200 Subject: [PATCH] load time for level string for non-saved levels --- src/hooks/LevelInfoLayer.cpp | 32 ++++++----- src/utils.cpp | 103 ++++++++++++++++++----------------- 2 files changed, 73 insertions(+), 62 deletions(-) diff --git a/src/hooks/LevelInfoLayer.cpp b/src/hooks/LevelInfoLayer.cpp index c2ab1f4..7a78384 100644 --- a/src/hooks/LevelInfoLayer.cpp +++ b/src/hooks/LevelInfoLayer.cpp @@ -23,27 +23,33 @@ class $modify(LevelInfoLayer) { auto cache = BetterInfoCache::sharedState(); cache->storeDatesForLevel(this->m_level); + auto label = typeinfo_cast(getChildByID("length-label")); + if(label) { + auto bmFont = CCLabelBMFont::create("Loading", "bigFont.fnt"); + bmFont->setID("bi-exact-time"); + bmFont->setPosition({label->getPositionX() + 1, label->getPositionY() - 2.f}); //193 - 185 + bmFont->setAnchorPoint({0,1}); + bmFont->setScale(0.325f); + addChild(bmFont); + label->setPositionY(label->getPositionY() + 6.f); + } + + return true; + } + + void updateLabelValues() { + LevelInfoLayer::updateLabelValues(); std::thread([this](){ auto wt = ExtendedLevelInfo::workingTime(std::round(BetterInfo::timeForLevelString(m_level->m_levelString))); Loader::get()->queueInMainThread([this, wt]() { auto label = typeinfo_cast(getChildByID("length-label")); - if(label) { + auto bmFont = typeinfo_cast(getChildByID("bi-exact-time")); + if(label && bmFont) { + bmFont->setString(fmt::format("{}", wt).c_str()); //label->setString(fmt::format("{} ({})", label->getString(), wt).c_str()); - auto bmFont = CCLabelBMFont::create(fmt::format("{}", wt).c_str(), "bigFont.fnt"); - bmFont->setID("bi-exact-time"); - bmFont->setPosition({label->getPositionX() + 1, label->getPositionY() - 2.f}); //todo: position properly - label->setPositionY(label->getPositionY() + 6.f); - - //193 - 185 - bmFont->setAnchorPoint({0,1}); - bmFont->setScale(0.325f); - addChild(bmFont); } }); }).detach(); - - - return true; } void onViewProfile(CCObject* sender) { diff --git a/src/utils.cpp b/src/utils.cpp index 4d8c6df..600be5a 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -526,59 +526,64 @@ inline uint64_t timeInMs() { } float BetterInfo::timeForLevelString(const std::string& levelString) { - //todo: checked portals - auto a = timeInMs(); - - auto decompressString = decodeBase64Gzip(levelString); - auto c = timeInMs(); - std::stringstream responseStream(decompressString); - std::string currentObject; - std::string currentKey; - std::string keyID; - - std::stringstream objectStream; - float prevPortalX = 0; - int prevPortalId = 0; - - float timeFull = 0; - - float maxPos = 0; - while(getline(responseStream, currentObject, ';')){ - size_t i = 0; - int objID = 0; - float xPos = 0; - - objectStream.clear(); - objectStream << currentObject; - objectStream.seekp(0); - objectStream.seekg(0); - //std::stringstream objectStream(currentObject); - while(getline(objectStream, currentKey, ',')) { - - - if(i % 2 == 0) keyID = currentKey; - else { - if(keyID == "1") objID = std::stoi(currentKey); - else if(keyID == "2") xPos = std::stof(currentKey); - else if(keyID == "kA4") prevPortalId = speedToPortalId(std::stoi(currentKey)); + try { + //todo: checked portals + auto a = timeInMs(); + + auto decompressString = decodeBase64Gzip(levelString); + auto c = timeInMs(); + std::stringstream responseStream(decompressString); + std::string currentObject; + std::string currentKey; + std::string keyID; + + std::stringstream objectStream; + float prevPortalX = 0; + int prevPortalId = 0; + + float timeFull = 0; + + float maxPos = 0; + while(getline(responseStream, currentObject, ';')){ + size_t i = 0; + int objID = 0; + float xPos = 0; + + objectStream.clear(); + objectStream << currentObject; + objectStream.seekp(0); + objectStream.seekg(0); + //std::stringstream objectStream(currentObject); + while(getline(objectStream, currentKey, ',')) { + + + if(i % 2 == 0) keyID = currentKey; + else { + if(keyID == "1") objID = std::stoi(currentKey); + else if(keyID == "2") xPos = std::stof(currentKey); + else if(keyID == "kA4") prevPortalId = speedToPortalId(std::stoi(currentKey)); + } + i++; + + if(xPos != 0 && objID != 0) break; } - i++; - if(xPos != 0 && objID != 0) break; - } + if(maxPos < xPos) maxPos = xPos; + if(!objectIDIsSpeedPortal(objID)) continue; - if(maxPos < xPos) maxPos = xPos; - if(!objectIDIsSpeedPortal(objID)) continue; + timeFull += (xPos - prevPortalX) / travelForPortalId(prevPortalId); + prevPortalId = objID; + prevPortalX = xPos; + } - timeFull += (xPos - prevPortalX) / travelForPortalId(prevPortalId); - prevPortalId = objID; - prevPortalX = xPos; + timeFull += (maxPos - prevPortalX) / travelForPortalId(prevPortalId); + auto b = timeInMs() - a; + log::info("time spent decompressing: {}", c - a); + log::info("time spent: {}", b); + return timeFull; + } catch(std::exception e) { + log::error("An exception has occured while calculating time for levelString: {}", e.what()); + return 0; } - timeFull += (maxPos - prevPortalX) / travelForPortalId(prevPortalId); - auto b = timeInMs() - a; - log::info("time spent decompressing: {}", c - a); - log::info("time spent: {}", b); - return timeFull; - } \ No newline at end of file