- add `CCFileUtils::get`
 - turns out i was doing semver wrong. i'm deeply sorry. read: https://www.twitlonger.com/show/n_1ss44g2
 - fix MenuLayer crashes related to node IDs
 - remove `Loader::updateModResourcePaths` and `Loader::updateResourcePaths`. Loader minimum mod version is still v0.4.0 however, as you should not have used these ever.
 - rework how mod resources are added
This commit is contained in:
HJfod 2022-10-15 20:19:20 +03:00
parent fe76cccbee
commit 4b842e5f2a
9 changed files with 88 additions and 89 deletions
VERSION
loader
include/Geode
cocos/cocos2dx/platform
loader
src

View file

@ -1 +1 @@
0.4.8
0.5.0

View file

@ -64,6 +64,10 @@ public:
* @js getInstance
*/
static CCFileUtils* sharedFileUtils();
GEODE_ADD(
static CCFileUtils* get();
);
/**
* Destroys the instance of CCFileUtils.

View file

@ -182,8 +182,6 @@ namespace geode {
* Mod::m_addResourcesToSearchPath to true
* first
*/
void updateModResourcePaths(Mod*);
void updateResourcePaths();
void updateModResources(Mod* mod);
void updateResources();

View file

@ -25,4 +25,8 @@ CCEGLView* CCEGLView::get() {
return CCDirector::sharedDirector()->getOpenGLView();
}
CCFileUtils* CCFileUtils::get() {
return CCFileUtils::sharedFileUtils();
}
#pragma warning(pop)

View file

@ -62,6 +62,23 @@ static void updateIndexProgress(
}
}
template<class T = CCNode>
requires std::is_base_of_v<CCNode, T>
T* setIDSafe(CCNode* node, int index, const char* id) {
if constexpr (std::is_same_v<CCNode, T>) {
if (auto child = getChild(node, index)) {
child->setID(id);
return child;
}
} else {
if (auto child = getChildOfType<T>(node, index)) {
child->setID(id);
return child;
}
}
return nullptr;
}
#include <Geode/modify/MenuLayer.hpp>
class $modify(CustomMenuLayer, MenuLayer) {
void destructor() {
@ -73,34 +90,26 @@ class $modify(CustomMenuLayer, MenuLayer) {
if (!MenuLayer::init())
return false;
Loader::get()->updateResourcePaths();
auto setIDSafe = +[](CCNode* node, int index, const char* id) {
if (auto child = getChild(node, index)) {
child->setID(id);
}
};
// set IDs to everything
this->setID("main-menu-layer");
setIDSafe(this, 0, "main-menu-bg");
getChildOfType<CCSprite>(this, 0)->setID("main-title");
setIDSafe<CCSprite>(this, 0, "main-title");
if (PlatformToolbox::isControllerConnected()) {
getChildOfType<CCSprite>(this, 1)->setID("play-gamepad-icon");
getChildOfType<CCSprite>(this, 2)->setID("editor-gamepad-icon");
getChildOfType<CCSprite>(this, 3)->setID("icon-kit-gamepad-icon");
setIDSafe<CCSprite>(this, 1, "play-gamepad-icon");
setIDSafe<CCSprite>(this, 2, "editor-gamepad-icon");
setIDSafe<CCSprite>(this, 3, "icon-kit-gamepad-icon");
getChildOfType<CCSprite>(this, 4)->setID("settings-gamepad-icon");
getChildOfType<CCSprite>(this, 5)->setID("mouse-gamepad-icon");
getChildOfType<CCSprite>(this, 6)->setID("click-gamepad-icon");
setIDSafe<CCSprite>(this, 4, "settings-gamepad-icon");
setIDSafe<CCSprite>(this, 5, "mouse-gamepad-icon");
setIDSafe<CCSprite>(this, 6, "click-gamepad-icon");
getChildOfType<CCLabelBMFont>(this, 0)->setID("mouse-gamepad-label");
getChildOfType<CCLabelBMFont>(this, 1)->setID("click-gamepad-label");
setIDSafe<CCLabelBMFont>(this, 0, "mouse-gamepad-label");
setIDSafe<CCLabelBMFont>(this, 1, "click-gamepad-label");
getChildOfType<CCLabelBMFont>(this, 2)->setID("player-username");
setIDSafe<CCLabelBMFont>(this, 2, "player-username");
} else {
getChildOfType<CCLabelBMFont>(this, 0)->setID("player-username");
setIDSafe<CCLabelBMFont>(this, 0, "player-username");
}
if (auto menu = getChildOfType<CCMenu>(this, 0)) {
menu->setID("main-menu");

View file

@ -1,21 +1,13 @@
#include <Geode/loader/Loader.hpp>
#include <Geode/modify/LoadingLayer.hpp>
USE_GEODE_NAMESPACE();
#include <Geode/modify/GameManager.hpp>
class $modify(GameManager) {
void reloadAllStep2() {
GameManager::reloadAllStep2();
Loader::get()->updateResourcePaths();
}
};
#include <Geode/modify/LoadingLayer.hpp>
class $modify(LoadingLayer) {
void loadAssets() {
LoadingLayer::loadAssets();
if (this->m_loadStep == 5) {
Loader::get()->updateResourcePaths();
// this is in case the user refreshes texture quality at runtime
if (this->m_loadStep == 12) {
Loader::get()->updateResources();
}
}

View file

@ -40,6 +40,9 @@ InternalMod::InternalMod() : Mod(getInternalModInfo()) {
m_saveDirPath = Loader::get()->getGeodeSaveDirectory() / GEODE_MOD_DIRECTORY / m_info.m_id;
ghc::filesystem::create_directories(m_saveDirPath);
// make sure spritesheets get added
m_addResourcesToSearchPath = true;
auto sett = this->loadSettings();
if (!sett) {
log::log(Severity::Error, this, "{}", sett.error());

View file

@ -49,67 +49,67 @@ void Loader::createDirectories() {
m_logStream = std::ofstream(logDir / log::generateLogName());
}
void Loader::updateResourcePaths() {
log::debug("Updating resources paths");
// add own geode/resources directory
CCFileUtils::sharedFileUtils()->addSearchPath(
(this->getGeodeDirectory() / GEODE_RESOURCE_DIRECTORY).string().c_str()
);
// add geode/temp for accessing root resources in mods
auto tempDir = this->getGeodeDirectory() / GEODE_TEMP_DIRECTORY;
CCFileUtils::sharedFileUtils()->addSearchPath(tempDir.string().c_str());
// add geode/temp/mod.id/resources for accessing additional resources in mods
for (auto& [_, mod] : m_mods) {
this->updateModResourcePaths(mod);
}
}
void Loader::updateModResourcePaths(Mod* mod) {
if (mod->m_addResourcesToSearchPath) {
CCFileUtils::sharedFileUtils()->addSearchPath(
(this->getGeodeDirectory() /
GEODE_TEMP_DIRECTORY /
mod->getID() /
"resources"
).string().c_str()
);
log::debug("Added resources path for {}", mod->getID());
}
}
void Loader::updateModResources(Mod* mod) {
if (!mod->m_addResourcesToSearchPath) {
log::debug("Mod {} doesn't have resources, skipping", mod->getID());
return;
}
auto searchPath = this->getGeodeDirectory() /
GEODE_TEMP_DIRECTORY / mod->getID() / "resources";
// if resources already added
if (vector::contains(CCFileUtils::get()->getSearchPaths(), searchPath.string())) {
return;
}
log::debug("Adding resources for {}", mod->getID());
// add search path
CCFileUtils::get()->addSearchPath(searchPath.string().c_str());
// add spritesheets
for (auto const& sheet : mod->m_info.m_spritesheets) {
auto png = sheet + ".png";
auto plist = sheet + ".plist";
auto ccfu = CCFileUtils::sharedFileUtils();
if (
png == std::string(
ccfu->fullPathForFilename(png.c_str(), false)
) ||
plist == std::string(
ccfu->fullPathForFilename(plist.c_str(), false)
)
png == std::string(ccfu->fullPathForFilename(png.c_str(), false)) ||
plist == std::string(ccfu->fullPathForFilename(plist.c_str(), false))
) {
log::warn("The resource dir of \"{}\" is missing \"{}\" png and/or plist files", mod->m_info.m_id, sheet);
log::warn(
"The resource dir of \"{}\" is missing \"{}\" png and/or plist files",
mod->m_info.m_id, sheet
);
} else {
CCTextureCache::sharedTextureCache()->addImage(png.c_str(), false);
CCSpriteFrameCache::sharedSpriteFrameCache()
->addSpriteFramesWithFile(plist.c_str());
log::debug("Added resources for {}", mod->getID());
}
}
}
void Loader::updateResources() {
log::debug("Adding mod resources");
log::debug("Adding resources");
// add own spritesheets
this->updateModResources(InternalMod::get());
// if resources already added
if (!vector::contains(
CCFileUtils::get()->getSearchPaths(),
(this->getGeodeDirectory() / GEODE_RESOURCE_DIRECTORY).string()
)) {
// add own geode/resources directory
CCFileUtils::get()->addSearchPath(
(this->getGeodeDirectory() / GEODE_RESOURCE_DIRECTORY).string().c_str()
);
// add geode/temp for accessing root resources in mods
auto tempDir = this->getGeodeDirectory() / GEODE_TEMP_DIRECTORY;
CCFileUtils::get()->addSearchPath(tempDir.string().c_str());
// add own spritesheets
this->updateModResources(InternalMod::get());
}
// add mods' spritesheets
for (auto const& [_, mod] : m_mods) {
@ -353,12 +353,6 @@ bool Loader::setup() {
this->loadSettings();
this->refreshMods();
// add resources on startup
this->queueInGDThread([]() {
Loader::get()->updateResourcePaths();
Loader::get()->updateResources();
});
m_isSetup = true;
return true;
@ -454,12 +448,8 @@ size_t Loader::getFieldIndexForClass(size_t hash) {
}
VersionInfo Loader::minModVersion() {
// patches are always backwards-compatible. if not, we have failed
return VersionInfo {
Loader::getVersion().getMajor(),
Loader::getVersion().getMinor(),
0,
};
// Remember to update when deleting features!
return VersionInfo { 0, 4, 0 };
}
VersionInfo Loader::maxModVersion() {

View file

@ -51,7 +51,6 @@ Result<Mod*> Loader::loadModFromFile(std::string const& path) {
// add mod resources
this->queueInGDThread([this, mod]() {
this->updateModResourcePaths(mod);
this->updateModResources(mod);
});