Skip to content

Commit

Permalink
Major changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alphalaneous committed Jan 6, 2025
1 parent e97ee17 commit 7daa390
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 33 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.9.0
- Conditionals (If statements) (See Wiki)
- Root node shortcut (See Wiki)
- More variables (See Wiki)
- Updated Rift

## 1.8.8
- Implement a fix for another mod's mistake :P

Expand Down
2 changes: 1 addition & 1 deletion cmake/IncludeLibs.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.21)
add_library(third_party INTERFACE)

CPMAddPackage("gh:EclipseMenu/rift#b8b31d6")
CPMAddPackage("gh:EclipseMenu/rift#60c9a83")

if (CMAKE_BUILD_TYPE STREQUAL "Debug" AND WIN32)
target_compile_definitions(rift PRIVATE _HAS_ITERATOR_DEBUGGING=0)
Expand Down
2 changes: 1 addition & 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.8",
"version": "v1.9.0",
"id": "alphalaneous.happy_textures",
"name": "Happy Textures :3",
"developer": "Alphalaneous",
Expand Down
93 changes: 91 additions & 2 deletions src/LabelValues.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <Geode/Geode.hpp>
#include <rift.hpp>
#include "Macros.h"
#include "nodes/LevelBrowserLayer.h"


using namespace geode::prelude;

Expand Down Expand Up @@ -256,7 +258,90 @@ namespace LabelValues {
return LABEL("level_length_value", "null");
}

static std::unordered_map<std::string, rift::Value> getValueMap(std::string original) {
static std::pair<std::string, rift::Value> getLevelSearchType() {
LevelBrowserLayer* levelBrowserLayer = MyLevelBrowserLayer::get();
if (levelBrowserLayer) {
return LABEL("level_search_type", (int)levelBrowserLayer->m_searchObject->m_searchType);
}
return LABEL("level_search_type", "null");
}

static std::pair<std::string, rift::Value> getLevelSearchMode() {
LevelBrowserLayer* levelBrowserLayer = MyLevelBrowserLayer::get();
if (levelBrowserLayer) {
return LABEL("level_search_mode", (int)levelBrowserLayer->m_searchObject->m_searchMode);
}
return LABEL("level_search_mode", "null");
}

static std::pair<std::string, rift::Value> getPlayLayerPercent() {
PlayLayer* playLayer = PlayLayer::get();
if (playLayer) {
return LABEL("playlayer_percent", playLayer->getCurrentPercent());
}
return LABEL("playlayer_percent", "null");
}

static std::pair<std::string, rift::Value> getPlayLayerPlatformer() {
PlayLayer* playLayer = PlayLayer::get();
if (playLayer) {
return LABEL("playlayer_is_platformer", playLayer->m_isPlatformer);
}
return LABEL("playlayer_is_platformer", "null");
}

static std::unordered_map<std::string, rift::Value> getNodeMap(CCNode* node) {

std::string font = "";
std::string text = "";
if (CCLabelBMFont* textNode = typeinfo_cast<CCLabelBMFont*>(node)) {
font = textNode->getFntFile();
text = textNode->getString();
}
std::string sprite = "";
if (CCSprite* spriteNode = typeinfo_cast<CCSprite*>(node)) {
sprite = Utils::getSpriteName(spriteNode);
}

ccColor3B color = {0, 0, 0};
short opacity = 0;
if (CCNodeRGBA* nodeRGBA = typeinfo_cast<CCNodeRGBA*>(node)) {
color = nodeRGBA->getColor();
opacity = nodeRGBA->getOpacity();
}

CCSize winSize = CCDirector::get()->getWinSize();

return {
LABEL("node_position_x", node->getPosition().x),
LABEL("node_position_y", node->getPosition().y),
LABEL("node_scale", node->getScale()),
LABEL("node_scale_x", node->getScaleX()),
LABEL("node_scale_y", node->getScaleY()),
LABEL("node_anchor_x", node->getAnchorPoint().x),
LABEL("node_anchor_y", node->getAnchorPoint().y),
LABEL("node_ignore_anchor_pos", node->isIgnoreAnchorPointForPosition()),
LABEL("node_content_size_width", node->getContentSize().width),
LABEL("node_content_size_height", node->getContentSize().height),
LABEL("node_visibility", node->isVisible()),
LABEL("node_rotation", node->getRotation()),
LABEL("node_rotation_x", node->getRotationX()),
LABEL("node_rotation_y", node->getRotationY()),
LABEL("node_skew_x", node->getSkewX()),
LABEL("node_skew_y", node->getSkewY()),
LABEL("node_z_order", node->getZOrder()),
LABEL("node_parent_id", node->getParent() ? node->getParent()->getID() : ""),
LABEL("node_font", font),
LABEL("node_text", text),
LABEL("node_sprite", sprite),
LABEL("node_color", "#" + geode::cocos::cc3bToHexString(color)),
LABEL("node_opacity", opacity),
LABEL("window_size_width", winSize.width),
LABEL("window_size_height", winSize.height),
};
}

static std::unordered_map<std::string, rift::Value> getValueMap(std::string original = "") {
return {
getUsername(),
getGameVersion(),
Expand Down Expand Up @@ -305,7 +390,11 @@ namespace LabelValues {
getLevelJumps(),
getLevelPassword(),
getLevelStars(),
getLevelLengthValue()
getLevelLengthValue(),
getLevelSearchType(),
getLevelSearchMode(),
getPlayLayerPlatformer(),
getPlayLayerPercent()
};
}
};
55 changes: 50 additions & 5 deletions src/UIModding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,30 @@ void UIModding::setContentSize(CCNode* node, matjson::Value attributes) {
}
}

void UIModding::evaluateIf(CCNode* node, matjson::Value ifArray) {
if (!node) return;

if (ifArray.isArray()) {
for (matjson::Value v : ifArray.asArray().unwrap()) {
if (v.contains("statement")) {
if (v["statement"].isString()) {
std::string statement = v["statement"].asString().unwrapOrDefault();
auto vars = LabelValues::getValueMap("");
auto nodeVars = LabelValues::getNodeMap(node);
vars.insert(nodeVars.begin(), nodeVars.end());

auto result = rift::evaluate(statement, vars);
bool evaluated = result.unwrapOr("false").toBoolean();

if (evaluated) {
handleModifications(node, v);
}
}
}
}
}
}

void UIModding::handleModifications(CCNode* node, matjson::Value nodeObject) {

if (DataNode* data = typeinfo_cast<DataNode*>(node)){
Expand All @@ -1252,6 +1276,13 @@ void UIModding::handleModifications(CCNode* node, matjson::Value nodeObject) {

if (!node) return;

if (nodeObject.contains("if")) {
matjson::Value ifArray = nodeObject["if"];
if (ifArray.isArray()) {
evaluateIf(node, ifArray);
}
}

if (nodeObject.contains("attributes")) {
matjson::Value nodeAttributes = nodeObject["attributes"];
if (nodeAttributes.isObject()) {
Expand Down Expand Up @@ -1314,6 +1345,17 @@ void UIModding::handleModifications(CCNode* node, matjson::Value nodeObject) {
}
}
}
if (nodeObject.contains("root")) {
matjson::Value parentVal = nodeObject["root"];
if (parentVal.isObject()) {
parentVal["_pack-name"] = nodeObject["_pack-name"];
CCNode* parentResult = node;
while (parentResult->getParent()) {
parentResult = parentResult->getParent();
}
handleModifications(parentResult, parentVal);
}
}

if (nodeObject.contains("children")) {
matjson::Value childrenVal = nodeObject["children"];
Expand Down Expand Up @@ -1542,6 +1584,13 @@ void UIModding::doUICheck(CCNode* node, bool afterTransition) {

matjson::Value expandedValue = value.unwrap();
if (expandedValue.isObject()) {
if (afterTransition) {
if (expandedValue.contains("after-transition") && expandedValue["after-transition"].isObject()) {
expandedValue = expandedValue["after-transition"];
}
else return;
}

std::string fullPathStr = CCFileUtils::sharedFileUtils()->fullPathForFilename(path.c_str(), false);

std::filesystem::path fullPath(fullPathStr);
Expand All @@ -1554,11 +1603,7 @@ void UIModding::doUICheck(CCNode* node, bool afterTransition) {
}
std::replace(name.begin(), name.end(), ' ', '-');

if (afterTransition) {
if (expandedValue.contains("after-transition") && expandedValue["after-transition"].isObject()) {
expandedValue = expandedValue["after-transition"];
}
}


expandedValue["_pack-name"] = name.substr(0, name.find_last_of("."));;

Expand Down
2 changes: 2 additions & 0 deletions src/UIModding.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class UIModding {
std::vector<std::filesystem::path> activePackCache;
std::unordered_map<std::string, bool> filenameCache;
std::unordered_map<CCTexture2D*, std::string> textureToNameMap;
LevelBrowserLayer* levelBrowserLayer;

std::vector<FileWatcher*> listeners;
Ref<CCArray> removalQueue = CCArray::create();
Expand Down Expand Up @@ -73,6 +74,7 @@ class UIModding {
AxisAlignment getAxisAlignment(std::string name);
std::optional<ColorData> getColors(std::string name);
void updateColors(CCNode* node, std::string name);
void evaluateIf(CCNode* node, matjson::Value ifArray);

static UIModding* get() {

Expand Down
1 change: 1 addition & 0 deletions src/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ namespace Utils {
}

static std::string getSpriteName(CCSprite* sprite) {

if (auto texture = sprite->getTexture()) {
for (auto [key, frame] : CCDictionaryExt<std::string, CCSpriteFrame*>(CCSpriteFrameCache::sharedSpriteFrameCache()->m_pSpriteFrames)) {
if (frame->getTexture() == texture && frame->getRect() == sprite->getTextureRect()) return key;
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
#include "nodes/CCSprite.h"
#include "nodes/CCSpriteBatchNode.h"
#include "nodes/CCDirector.h"
#include "nodes/LevelBrowserLayer.h"
#include "BackgroundColors.h"
35 changes: 25 additions & 10 deletions src/nodes/CCLabelBMFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,39 @@ class $modify(MyCCLabelBMFont, CCLabelBMFont) {
bool m_isLimited = false;
SEL_SCHEDULE m_schedule;
bool m_isHappyTexturesModified;
std::unique_ptr<rift::Script> m_riftScript;
bool m_compiled = false;
};

static std::string riftString(MyCCLabelBMFont* self, std::string input) {
if (self->m_fields->m_isHappyTexturesModified) {
rift::Result<rift::Script*> scriptRes = rift::compile(std::string_view(input));
if (!scriptRes.isError()) {
rift::Script* script = scriptRes.unwrap();
auto vars = LabelValues::getValueMap(self->getString());
std::string newNewString = script->run(vars);
delete script;
return newNewString;
std::string riftString(std::string input) {
auto fields = m_fields.self();

if (fields->m_isHappyTexturesModified) {
if (!fields->m_compiled) {
auto compiled = rift::compile(std::string_view(input));
if (compiled) {
fields->m_riftScript = std::move(compiled.unwrap());
}
fields->m_compiled = true;
}
if (fields->m_riftScript) {
auto vars = LabelValues::getValueMap(getString());
auto res = fields->m_riftScript->run(vars);
if (res) {
std::string newNewString = res.unwrap();
return newNewString;
}
}
}
return input;
}

void setString(const char *newString, bool needUpdateLabel) {
CCLabelBMFont::setString(riftString(this, newString).c_str(), needUpdateLabel);
auto fields = m_fields.self();
if (std::string_view(newString) != std::string_view(getString())) {
fields->m_compiled = false;
}
CCLabelBMFont::setString(riftString(newString).c_str(), needUpdateLabel);
}

void setHappyTexturesModified(bool refresh = false) {
Expand Down
3 changes: 0 additions & 3 deletions src/nodes/CCMenuItemSpriteExtra.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ class $modify(EventCCMenuItemSpriteExtra, CCMenuItemSpriteExtra) {
}

void selected() {

SAFE_RUN(
if (!m_fields->overrideOnClick) {
CCMenuItemSpriteExtra::selected();
Expand All @@ -92,7 +91,6 @@ class $modify(EventCCMenuItemSpriteExtra, CCMenuItemSpriteExtra) {
}

void unselected() {

SAFE_RUN(
if (!m_fields->overrideOnRelease) {
CCMenuItemSpriteExtra::unselected();
Expand All @@ -102,7 +100,6 @@ class $modify(EventCCMenuItemSpriteExtra, CCMenuItemSpriteExtra) {
}

void activate() {

SAFE_RUN(
if (!m_fields->overrideOnActivate) {
CCMenuItemSpriteExtra::activate();
Expand Down
5 changes: 5 additions & 0 deletions src/nodes/CCScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@ class SceneHandler : public CCObject {
if (m_handledNodes.find(node) != m_handledNodes.end()) continue;
m_handledNodes.insert(node);

if (node->getUserObject("checked"_spr)) continue;
if (node->getID() == "MenuLayer") continue; // hardcoded for now

UIModding::get()->doUICheck(node);
node->setUserObject("checked"_spr, CCBool::create(true));
}
}

void update(float dt) {
auto scene = CCDirector::sharedDirector()->getRunningScene();
if (CCTransitionScene* trans = typeinfo_cast<CCTransitionScene*>(scene)) {
scene = public_cast(trans, m_pInScene);
}

if (scene && UIModding::get()->doModify) {
this->checkForUpdates(scene);
Expand Down
26 changes: 26 additions & 0 deletions src/nodes/LevelBrowserLayer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <Geode/Geode.hpp>
#include <Geode/modify/LevelBrowserLayer.hpp>
#include "../UIModding.h"

using namespace geode::prelude;

class $modify(MyLevelBrowserLayer, LevelBrowserLayer) {

struct Fields {
~Fields() {
UIModding::get()->levelBrowserLayer = nullptr;
}
};

static LevelBrowserLayer* get() {
return UIModding::get()->levelBrowserLayer;
}

bool init(GJSearchObject* p0) {
UIModding::get()->levelBrowserLayer = this;
return LevelBrowserLayer::init(p0);
}

};
Loading

0 comments on commit 7daa390

Please sign in to comment.