Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alphalaneous committed Dec 28, 2024
1 parent f4876d5 commit 7230167
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 85 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.8.5
- Cleanup filesystem code
- Fix a crash when there is an invalid path (hopefully)
- Fix an issue with CCSpriteWithHue. Do note that changing the wraith textures is still broken and has inconsistent behavior

## 1.8.4
- Actually fix loading individual sprites from init

Expand Down
7 changes: 6 additions & 1 deletion mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"android": "2.2074",
"mac": "2.2074"
},
"version": "v1.8.4",
"version": "v1.8.5",
"id": "alphalaneous.happy_textures",
"name": "Happy Textures :3",
"developer": "Alphalaneous",
Expand All @@ -16,6 +16,11 @@
"id": "geode.node-ids",
"version": ">=v1.15.0",
"importance": "required"
},
{
"id": "geode.texture-loader",
"version": ">=v1.7.0",
"importance": "suggested"
}
],
"resources": {
Expand Down
4 changes: 2 additions & 2 deletions src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class Config {
bool m_batchNodesEnabled = true;

void loadPackJsons() {
for (std::string packPath : Utils::getActivePacks()) {
std::filesystem::path json = std::filesystem::path{packPath} / "pack.json";
for (std::filesystem::path packPath : Utils::getActivePacks()) {
std::filesystem::path json = packPath / "pack.json";
if (!std::filesystem::exists(json)) continue;

std::ifstream inputFile(json);
Expand Down
7 changes: 3 additions & 4 deletions src/FileWatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ enum class FileStatus {created, modified, erased};
class FileWatcher {
public:

FileWatcher(std::string pathToWatch, std::chrono::duration<int, std::milli> delay) : m_pathToWatch(pathToWatch), m_delay{delay} {
FileWatcher(std::filesystem::path pathToWatch, std::chrono::duration<int, std::milli> delay) : m_pathToWatch(pathToWatch), m_delay{delay} {

if (std::filesystem::is_directory(pathToWatch)) {
auto path = std::filesystem::path{utils::string::replace(pathToWatch, "\\", "/")}.lexically_normal().make_preferred();
for (auto& file : std::filesystem::recursive_directory_iterator(path)) {
for (auto& file : std::filesystem::recursive_directory_iterator(pathToWatch)) {
m_paths[file.path()] = std::filesystem::last_write_time(file);
}
}
Expand Down Expand Up @@ -67,7 +66,7 @@ class FileWatcher {
private:
std::unordered_map<std::filesystem::path, std::filesystem::file_time_type> m_paths;
std::chrono::duration<int, std::milli> m_delay;
std::string m_pathToWatch = "";
std::filesystem::path m_pathToWatch = "";
bool m_running = true;

bool contains(const std::filesystem::path &key) {
Expand Down
44 changes: 0 additions & 44 deletions src/TextureLoader.hpp

This file was deleted.

32 changes: 15 additions & 17 deletions src/UIModding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,16 +914,15 @@ std::vector<std::string> generateValidSprites(std::string path, matjson::Value s
std::vector<std::string> validSprites;

if (!path.empty()) {
std::vector<std::string> packs = Utils::getActivePacks();
for (std::string packPath : packs) {
std::string sprPath = fmt::format("{}{}", packPath, path);
if (std::filesystem::is_directory(sprPath)) {
for (const auto& entry : std::filesystem::directory_iterator(sprPath)) {
std::string textureName = utils::string::split(entry.path().filename().string(), ".").at(0);
if (!utils::string::endsWith(textureName, "-hd") && !utils::string::endsWith(textureName, "-uhd")) {
std::string sprName = fmt::format("{}\\{}", path, entry.path().filename().string());
sprites.push_back(sprName);
}
std::vector<std::filesystem::path> packs = Utils::getActivePacks();
for (std::filesystem::path packPath : packs) {
std::filesystem::path sprPath = packPath / path;
if (!std::filesystem::is_directory(sprPath)) continue;
for (const auto& entry : std::filesystem::directory_iterator(sprPath)) {
std::string textureName = utils::string::split(entry.path().filename().string(), ".").at(0);
if (!utils::string::endsWith(textureName, "-hd") && !utils::string::endsWith(textureName, "-uhd")) {
std::string sprName = fmt::format("{}\\{}", path, entry.path().filename().string());
sprites.push_back(sprName);
}
}
}
Expand Down Expand Up @@ -1574,10 +1573,9 @@ void UIModding::startFileListeners() {
fw->stop();
}
listeners.clear();
std::vector<std::string> packs = Utils::getActivePacks();
for (std::string path : packs) {

std::string uiPath = fmt::format("{}{}", path, "ui\\");
std::vector<std::filesystem::path> packs = Utils::getActivePacks();
for (std::filesystem::path path : packs) {
std::filesystem::path uiPath = path / "ui";

FileWatcher* fw = new FileWatcher(uiPath, std::chrono::milliseconds(500));
listeners.push_back(fw);
Expand Down Expand Up @@ -1624,9 +1622,9 @@ AxisAlignment UIModding::getAxisAlignment(std::string name) {
}

void UIModding::loadNodeFiles() {
std::vector<std::string> packs = Utils::getActivePacks();
for (std::string path : packs) {
std::filesystem::path nodePath = std::filesystem::path{utils::string::replace(fmt::format("{}{}", path, "ui/nodes/"), "\\", "/")}.lexically_normal().make_preferred();
std::vector<std::filesystem::path> packs = Utils::getActivePacks();
for (std::filesystem::path path : packs) {
std::filesystem::path nodePath = path / "ui" / "nodes";
if (std::filesystem::is_directory(nodePath)) {
for (const auto& entry : std::filesystem::directory_iterator(nodePath)) {

Expand Down
6 changes: 4 additions & 2 deletions src/UIModding.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <Geode/Geode.hpp>
#include "FileWatcher.h"

using namespace geode::prelude;

struct ColorData {
Expand All @@ -18,9 +19,10 @@ class UIModding {
std::map<std::string, matjson::Value> uiCache;
std::map<std::string, ColorData> colorCache;
std::map<std::string, std::string> randomSprites;
std::vector<std::string> activePackCache;
std::vector<std::filesystem::path> activePackCache;
std::unordered_map<std::string, bool> filenameCache;
std::unordered_map<std::string, bool> spritesChanged;
std::unordered_map<CCSpriteFrame*, std::string> frameToNameMap;

std::vector<FileWatcher*> listeners;
Ref<CCArray> removalQueue = CCArray::create();
Expand Down Expand Up @@ -66,7 +68,7 @@ class UIModding {
void loadNodeFiles();
void doUICheck(CCNode* node);
void doUICheckForType(std::string name, CCNode* node);
std::vector<std::string> getActivePacks();
std::vector<std::filesystem::path> getActivePacks();
void startFileListeners();
AxisAlignment getAxisAlignment(std::string name);
std::optional<ColorData> getColors(std::string name);
Expand Down
39 changes: 25 additions & 14 deletions src/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "UIModding.h"
#include <random>
#include "Macros.h"
#include "TextureLoader.hpp"
#include <geode.texture-loader/include/TextureLoader.hpp>

using namespace geode::prelude;

Expand Down Expand Up @@ -119,8 +119,6 @@ namespace Utils {
#endif
}



static void setColorIfExists(CCRGBAProtocol* node, std::string colorId) {
if (!node) return;
std::optional<ColorData> dataOpt = UIModding::get()->getColors(colorId);
Expand All @@ -143,6 +141,17 @@ namespace Utils {
return "";
}

static std::string getSpriteName(CCSpriteFrame* spriteFrame) {
if (UIModding::get()->frameToNameMap.contains(spriteFrame)) return UIModding::get()->frameToNameMap[spriteFrame];
for (auto [key, frame] : CCDictionaryExt<std::string, CCSpriteFrame*>(CCSpriteFrameCache::sharedSpriteFrameCache()->m_pSpriteFrames)) {
if (spriteFrame == frame) {
UIModding::get()->frameToNameMap[spriteFrame] = key;
return key;
}
}
return "";
}

static std::string getHookPrioLatest(const std::string& name) {
int minPriority = 0;
const Mod* currentMod = Mod::get();
Expand Down Expand Up @@ -178,27 +187,28 @@ namespace Utils {
UIModding::get()->activePackCache.clear();
}

static std::vector<std::string> getActivePacks() {
static std::vector<std::filesystem::path> getActivePacks() {

if (!UIModding::get()->activePackCache.empty()) return UIModding::get()->activePackCache;

Mod* textureLoader = Loader::get()->getLoadedMod("geode.texture-loader");
if (textureLoader) {
if (textureLoader->getVersion() >= VersionInfo{1, 7, 0}) {
for(geode::texture_loader::Pack pack : geode::texture_loader::getAppliedPacks()) {
UIModding::get()->activePackCache.push_back(pack.resourcesPath.string() + "/");
for (geode::texture_loader::Pack pack : geode::texture_loader::getAppliedPacks()) {
UIModding::get()->activePackCache.push_back(pack.resourcesPath);
}
}
else {
log::info("Using old pack method. Update Texture Loader!");
for (matjson::Value value : textureLoader->getSavedValue<std::vector<matjson::Value>>("applied")) {
if (value.isObject() && value.contains("path") && value["path"].isString()) {
std::string path = value["path"].asString().unwrapOr("");
if (utils::string::endsWith(path, ".zip")) {
std::filesystem::path path = std::filesystem::path{utils::string::replace(value["path"].asString().unwrapOr(""), "\\", "/")};
if (path.extension().string() == ".zip") {
std::filesystem::path pathFs{path};
path = (textureLoader->getSaveDir() / "unzipped" / pathFs.filename()).string();
path = textureLoader->getSaveDir() / "unzipped" / pathFs.filename();
}
UIModding::get()->activePackCache.push_back(path + "/");
if (!std::filesystem::is_directory(path)) continue;
UIModding::get()->activePackCache.push_back(path);
}
}
}
Expand All @@ -209,7 +219,7 @@ namespace Utils {

static std::string qualityToNormal(std::string str) {
std::vector<std::string> fileParts = utils::string::split(str, ".");

if (fileParts.size() < 1) return str;
std::string suffix = fileParts[fileParts.size()-1];
std::string prefix = str.substr(0, str.size() - suffix.size() - 1);

Expand All @@ -225,12 +235,13 @@ namespace Utils {

static void reloadFileNames() {
UIModding::get()->filenameCache.clear();
for (std::string packPath : Utils::getActivePacks()) {

for (std::filesystem::path packPath : Utils::getActivePacks()) {
if (!std::filesystem::is_directory(packPath)) continue;
for (const auto& entry : std::filesystem::recursive_directory_iterator(packPath)) {
if (entry.is_regular_file()) {
std::string pathStr = entry.path().string();
std::string subStr = pathStr.substr(packPath.size());
std::string subStr = pathStr.substr(packPath.string().size() + 1);
log::info("subStr: {}", subStr);
UIModding::get()->filenameCache[qualityToNormal(utils::string::replace(subStr, "\\", "/"))] = true;
}
}
Expand Down
22 changes: 21 additions & 1 deletion src/nodes/CCSprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,29 @@

#include <Geode/Geode.hpp>
#include <Geode/modify/CCSprite.hpp>
#include <Geode/modify/CCSpriteWithHue.hpp>
#include "../Macros.h"

class $modify(MyCCSpriteWithHue, CCSpriteWithHue) {

static void onModify(auto& self) {
(void) self.setHookPriority("cocos2d::CCSpriteWithHue::initWithSpriteFrame", Priority::VeryEarly);
}

bool initWithSpriteFrame(cocos2d::CCSpriteFrame* p0) {
std::string frameName = Utils::getSpriteName(p0);

if (Utils::spriteExistsInPacks(frameName) && !UIModding::get()->spritesChanged[frameName]) {
CCSprite* spr = CCSprite::create(frameName.c_str());
auto spriteFrame = CCSpriteFrame::createWithTexture(spr->getTexture(), spr->getTextureRect());
CCSpriteFrameCache::get()->addSpriteFrame(spriteFrame, frameName.c_str());
UIModding::get()->spritesChanged[frameName] = true;
}

return CCSpriteWithHue::initWithSpriteFrame(p0);
}
};

class $modify(MyCCSprite, CCSprite) {

static void onModify(auto& self) {
Expand All @@ -19,7 +40,6 @@ class $modify(MyCCSprite, CCSprite) {
CCSpriteFrameCache::get()->addSpriteFrame(spriteFrame, pszSpriteFrameName);
UIModding::get()->spritesChanged[pszSpriteFrameName] = true;
}

return CCSprite::initWithSpriteFrameName(pszSpriteFrameName);
}

Expand Down
1 change: 1 addition & 0 deletions src/nodes/LoadingLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class $modify(MyLoadingLayer, LoadingLayer) {
UIModding::get()->uiCache.clear();
UIModding::get()->colorCache.clear();
UIModding::get()->spritesChanged.clear();
UIModding::get()->frameToNameMap.clear();

queueInMainThread([] {
Utils::clearActivePackCache();
Expand Down

0 comments on commit 7230167

Please sign in to comment.