Skip to content

Commit

Permalink
improve gdps support
Browse files Browse the repository at this point in the history
  • Loading branch information
Cvolton committed Nov 17, 2024
1 parent 9763bda commit 49608d8
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
9 changes: 2 additions & 7 deletions src/layers/ExtendedLevelInfo.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
#include "ExtendedLevelInfo.h"
#include "UnregisteredProfileLayer.h"
#include "PaginatedFLAlert.h"
#include "../utils.hpp"
#include "../managers/BetterInfoStats.h"
//#include "../managers/BetterInfoStatsV2.h"
#include "../managers/BetterInfoCache.h"

#include <deque>
#include <algorithm>
#include <thread>
#include <string>
#include <iterator>

ExtendedLevelInfo* ExtendedLevelInfo::create(GJGameLevel* level){
auto ret = new ExtendedLevelInfo();
Expand Down Expand Up @@ -96,8 +91,8 @@ void ExtendedLevelInfo::refreshInfoTexts() {

m_primary = infoText.str();
infoText.str("");
infoText << "\n<cj>Uploaded</c>: " << TimeUtils::isoTimeToString(m_uploadDateEstimated)
<< "\n<cg>Objects</c>: " << LevelMetadata::zeroIfNA(m_level->m_objectCount)
if(!ServerUtils::isGDPS()) infoText << "\n<cj>Uploaded</c>: " << TimeUtils::isoTimeToString(m_uploadDateEstimated);
infoText << "\n<cg>Objects</c>: " << LevelMetadata::zeroIfNA(m_level->m_objectCount)
<< "\n<cg>Objects (est.)</c>: " << LevelMetadata::zeroIfNA(m_objectsEstimated) //i have no idea what the 0 and 11 mean, i just copied them from PlayLayer::init
<< "\n<cy>Feature Score</c>: " << LevelMetadata::zeroIfNA(m_level->m_featured)
<< "\n<co>Two-player</c>: " << LevelMetadata::boolString(m_level->m_twoPlayerMode)
Expand Down
18 changes: 16 additions & 2 deletions src/utils/LevelMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
#include <sstream>
#include <iomanip>

bool isRobTopStyleDate(const std::string& date){
std::array<const char*, 7> times = {"second", "minute", "hour", "day", "week", "month", "year"};
for(auto time : times) if(date.contains(time)) {
return true;
}

return false;
}

std::string LevelMetadata::getGameVersionName(int version){
if(version < 1 || version > 99) return std::string("NA");

Expand All @@ -24,10 +33,13 @@ std::string LevelMetadata::getGameVersionName(int version){
return contentStream.str();
}

std::string LevelMetadata::stringDate(std::string date){
std::string LevelMetadata::stringDate(const std::string& date){
if(date == "") return "NA";
std::ostringstream stream;
stream << date << " ago";

stream << date;
if(isRobTopStyleDate(date)) stream << " ago";

return stream.str();
}

Expand Down Expand Up @@ -84,6 +96,8 @@ std::string LevelMetadata::zeroIfNA(int value){
}

std::string LevelMetadata::addPlus(std::string date) {
if(!isRobTopStyleDate(date)) return date;

auto spaceIt = date.find_first_of(' ');
if(spaceIt != std::string::npos) date.insert(date.cbegin() + spaceIt, '+');
return date;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/LevelMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace LevelMetadata {
BI_DLL std::string getGameVersionName(int version);
BI_DLL std::string stringDate(std::string date);
BI_DLL std::string stringDate(const std::string& date);
BI_DLL const char* getDifficultyIcon(int stars);
BI_DLL const char* getDemonDifficultyIcon(int demonDifficulty);
BI_DLL std::string passwordString(int password);
Expand Down
16 changes: 16 additions & 0 deletions src/utils/ServerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,28 @@ static inline std::unordered_map<std::string, Ref<cocos2d::CCArray>> s_cache;
static inline std::unordered_map<std::string, web::WebTask> s_requests;

static inline std::shared_mutex s_requestsMutex;
static bool s_isGDPS = false;

bool ServerUtils::isGDPS() {
return getBaseURL() != "https://www.boomlings.com/database";
}

web::WebRequest ServerUtils::getBaseRequest(bool setUserAgent) {
return web::WebRequest().userAgent(setUserAgent ? fmt::format("BetterInfo {} / Geode {}", Mod::get()->getVersion().toVString(true), Loader::get()->getVersion().toVString(true)) : "");
}

std::string ServerUtils::getBaseURL() {
// A cleaner solution for this would be a Server API dependency
// however I'd rather avoid depending on a mod that 90% of users
// don't actually use
if(Loader::get()->isModLoaded("km7dev.gdps-switcher")) {
auto url = Loader::get()->getLoadedMod("km7dev.gdps-switcher")->getSavedValue<std::string>("server");
if(!url.empty()) {
while(url.ends_with("/")) url.pop_back();
return url;
}
}

// The addresses are pointing to "https://www.boomlings.com/database/getGJLevels21.php"
// in the main game executable
char* originalUrl = nullptr;
Expand Down
1 change: 1 addition & 0 deletions src/utils/ServerUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using namespace geode::prelude;

namespace ServerUtils {
BI_DLL bool isGDPS();
BI_DLL web::WebRequest getBaseRequest(bool setUserAgent = true);
BI_DLL std::string getBaseURL();
BI_DLL std::string getBasePostString(bool includeAccount = true);
Expand Down

0 comments on commit 49608d8

Please sign in to comment.