From 22cc33b8a9716df344498a209271c298f0d4a2c4 Mon Sep 17 00:00:00 2001 From: kynex7510 Date: Thu, 13 Jun 2024 15:04:32 +0200 Subject: [PATCH] use string_view for sprite expansion --- loader/include/Geode/loader/Mod.hpp | 6 +++--- loader/include/Geode/utils/ColorProvider.hpp | 4 ++-- loader/src/loader/Mod.cpp | 2 +- loader/src/loader/ModImpl.cpp | 12 +++++++----- loader/src/loader/ModImpl.hpp | 3 ++- loader/src/ui/nodes/BasedButtonSprite.cpp | 2 +- 6 files changed, 16 insertions(+), 13 deletions(-) diff --git a/loader/include/Geode/loader/Mod.hpp b/loader/include/Geode/loader/Mod.hpp index c6f07a4e..91d1293a 100644 --- a/loader/include/Geode/loader/Mod.hpp +++ b/loader/include/Geode/loader/Mod.hpp @@ -444,7 +444,7 @@ namespace geode { */ bool hasUnresolvedIncompatibilities() const; - char const* expandSpriteName(char const* name); + std::string_view expandSpriteName(std::string_view name); /** * Get info about the mod as JSON @@ -466,6 +466,6 @@ namespace geode { }; } -GEODE_HIDDEN inline char const* operator"" _spr(char const* str, size_t) { - return geode::Mod::get()->expandSpriteName(str); +GEODE_HIDDEN inline char const* operator"" _spr(char const* str, size_t len) { + return geode::Mod::get()->expandSpriteName({ str, len }).data(); } diff --git a/loader/include/Geode/utils/ColorProvider.hpp b/loader/include/Geode/utils/ColorProvider.hpp index 1f662177..f9860ed8 100644 --- a/loader/include/Geode/utils/ColorProvider.hpp +++ b/loader/include/Geode/utils/ColorProvider.hpp @@ -122,8 +122,8 @@ GEODE_HIDDEN inline cocos2d::ccColor3B operator"" _cc3b_gd(const char* str, size return geode::ColorProvider::get()->color3b(str); } GEODE_HIDDEN inline cocos2d::ccColor4B operator"" _cc4b(const char* str, size_t) { - return geode::ColorProvider::get()->color(geode::Mod::get()->expandSpriteName(str)); + return geode::ColorProvider::get()->color(std::string(geode::Mod::get()->expandSpriteName(str))); } GEODE_HIDDEN inline cocos2d::ccColor3B operator"" _cc3b(const char* str, size_t) { - return geode::ColorProvider::get()->color3b(geode::Mod::get()->expandSpriteName(str)); + return geode::ColorProvider::get()->color3b(std::string(geode::Mod::get()->expandSpriteName(str))); } diff --git a/loader/src/loader/Mod.cpp b/loader/src/loader/Mod.cpp index 678b9bcc..d7fa3454 100644 --- a/loader/src/loader/Mod.cpp +++ b/loader/src/loader/Mod.cpp @@ -225,7 +225,7 @@ bool Mod::hasUnresolvedIncompatibilities() const { return m_impl->hasUnresolvedIncompatibilities(); } -char const* Mod::expandSpriteName(char const* name) { +std::string_view Mod::expandSpriteName(std::string_view name) { return m_impl->expandSpriteName(name); } diff --git a/loader/src/loader/ModImpl.cpp b/loader/src/loader/ModImpl.cpp index 89e4ace9..00277e02 100644 --- a/loader/src/loader/ModImpl.cpp +++ b/loader/src/loader/ModImpl.cpp @@ -18,6 +18,7 @@ #include #include #include +#include using namespace geode::prelude; @@ -743,14 +744,15 @@ std::filesystem::path Mod::Impl::getConfigDir(bool create) const { return dir; } -char const* Mod::Impl::expandSpriteName(char const* name) { - if (m_expandedSprites.count(name)) return m_expandedSprites[name]; +std::string_view Mod::Impl::expandSpriteName(std::string_view name) { + std::string nameKey(name); + if (m_expandedSprites.contains(nameKey)) return m_expandedSprites[nameKey]; - auto exp = new char[strlen(name) + 2 + m_metadata.getID().size()]; - auto exps = m_metadata.getID() + "/" + name; + auto exp = new char[name.size() + 2 + m_metadata.getID().size()]; + auto exps = (m_metadata.getID() + "/") + name.data(); memcpy(exp, exps.c_str(), exps.size() + 1); - m_expandedSprites[name] = exp; + m_expandedSprites[nameKey] = exp; return exp; } diff --git a/loader/src/loader/ModImpl.hpp b/loader/src/loader/ModImpl.hpp index 2a6ed55e..2346fd10 100644 --- a/loader/src/loader/ModImpl.hpp +++ b/loader/src/loader/ModImpl.hpp @@ -3,6 +3,7 @@ #include #include "ModPatch.hpp" #include +#include namespace geode { class Mod::Impl { @@ -148,7 +149,7 @@ namespace geode { Result<> loadBinary(); - char const* expandSpriteName(char const* name); + std::string_view expandSpriteName(std::string_view name); ModJson getRuntimeInfo() const; bool isLoggingEnabled() const; diff --git a/loader/src/ui/nodes/BasedButtonSprite.cpp b/loader/src/ui/nodes/BasedButtonSprite.cpp index 294bfadb..b601a988 100644 --- a/loader/src/ui/nodes/BasedButtonSprite.cpp +++ b/loader/src/ui/nodes/BasedButtonSprite.cpp @@ -182,7 +182,7 @@ static std::string baseEnumsToString(BaseType type, int size, int color) { bool BasedButtonSprite::init(CCNode* ontop, BaseType type, int size, int color) { if (!CCSprite::initWithSpriteFrameName( - Mod::get()->expandSpriteName(baseEnumsToString(type, size, color).c_str()) + Mod::get()->expandSpriteName(baseEnumsToString(type, size, color)).data() )) return false; m_type = type;