From 88b5d10e53764137ecb9fa755e9fc354b386aac4 Mon Sep 17 00:00:00 2001 From: Zachary Hall Date: Tue, 24 Dec 2024 11:24:47 -0800 Subject: [PATCH] Add extended Prism Launcher cat pack support --- cats.hpp | 4 +-- main.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/cats.hpp b/cats.hpp index 498582c..bfcce9e 100644 --- a/cats.hpp +++ b/cats.hpp @@ -38,9 +38,9 @@ enum class CatDataType { class CatData { std::optional mem; fs::path path; - std::string name; CatDataType type; public: + std::string name; inline std::string get_name() { return name; } @@ -85,4 +85,4 @@ class CatData { this->path = path; } }; -extern std::vector &get_cat_data(); \ No newline at end of file +extern std::vector &get_cat_data(); diff --git a/main.cpp b/main.cpp index fa236ea..afed6e5 100644 --- a/main.cpp +++ b/main.cpp @@ -17,6 +17,8 @@ #endif #include "web_functions.hpp" #include "cats.hpp" +#include +#include using namespace Looper; using namespace Looper::Options; using namespace Looper::Log; @@ -191,6 +193,92 @@ extern "C" int looper_run_as_executable(std::vector args) { for (auto const&dir_entry : std::filesystem::directory_iterator(baseDir)) { if (dir_entry.is_regular_file()) { cat_data.push_back(CatData(dir_entry.path())); + } else if (dir_entry.is_directory()) { + fs::path json_path = dir_entry.path() / fs::path("catpack.json"); + if (fs::exists(json_path)) { + std::ifstream stream(json_path); + Json::Value config; + stream >> config; + std::string name = dir_entry.path().stem().string(); + if (config.isMember("name")) { + name = config["name"].asString(); + } + if (!config.isMember("default")) { + continue; + } + std::string str = config["default"].asString(); + if (config.isMember("variants")) { + Json::Value variants = config["variants"]; + int prev_score = INT_MAX; + auto now = std::chrono::system_clock::now(); + time_t now_tt = std::chrono::system_clock::to_time_t(now); + tm local_tm = *localtime(&now_tt); + auto cur_day = local_tm.tm_mday; + auto cur_month = local_tm.tm_mon; + auto cur_yday = local_tm.tm_yday; + DEBUG.writefln("Current date: %d ([MM/DD] %02d/%02d)", cur_yday, cur_month + 1, cur_day); + + for (auto &variant : variants) { + if (variant.isMember("startTime") && variant.isMember("endTime")) { + Json::Value startTime = variant["startTime"]; + Json::Value endTime = variant["endTime"]; + int score = 0; + int start = 0; + int end = 0; + tm start_tm; + tm end_tm; + if (startTime.isMember("day") && startTime.isMember("month")) { + int day = startTime["day"].asInt(); + int month = startTime["month"].asInt(); + tm tmp{}; + tmp.tm_year = local_tm.tm_year; + tmp.tm_mon = month - 1; + tmp.tm_mday = day; + time_t t = std::mktime(&tmp); + tmp = *std::localtime(&t); + start = tmp.tm_yday; + start_tm = tmp; + } + if (endTime.isMember("day") && endTime.isMember("month")) { + int day = endTime["day"].asInt(); + int month = endTime["month"].asInt(); + tm tmp{}; + tmp.tm_year = local_tm.tm_year; + tmp.tm_mon = month - 1; + tmp.tm_mday = day; + time_t t = std::mktime(&tmp); + tmp = *std::localtime(&t); + end = tmp.tm_yday; + end_tm = tmp; + } + if (variant.isMember("path")) DEBUG.writefln("Variant %s: %d-%d ([MM/DD] %02d/%02d-%02d/%02d)", variant["path"].asCString(), start, end, start_tm.tm_mon+1, start_tm.tm_mday, end_tm.tm_mon+1, end_tm.tm_mday); + if (end < start) { + if (cur_yday > end && cur_yday < start) continue; + score = end - start + 365; + } else { + if (cur_yday > end || cur_yday < start) continue; + score = end - start; + } + if (variant.isMember("path")) { + if (prev_score > score) { + auto newPath = variant["path"].asString(); + if (!fs::exists(newPath)) { + continue; + } + str = newPath; + prev_score = score; + } + } + } + } + } + fs::path usedPath = dir_entry.path() / fs::path(str); + if (fs::exists(usedPath)) { + auto data = CatData(usedPath); + data.name = name; + cat_data.push_back(data); + } + } } } #endif