diff --git a/.clang-format b/.clang-format index 974e428d..c075a60a 100644 --- a/.clang-format +++ b/.clang-format @@ -3,6 +3,7 @@ IndentWidth: 4 AlignAfterOpenBracket: BlockIndent AlignEscapedNewlines: Left +AlignConsecutiveAssignments: false AlignOperands: DontAlign AlignTrailingComments: false @@ -22,7 +23,7 @@ AlwaysBreakAfterReturnType: None AlwaysBreakBeforeMultilineStrings: true AlwaysBreakTemplateDeclarations: Yes -BinPackArguments: true +BinPackArguments: false BinPackParameters: true BraceWrapping: @@ -49,7 +50,7 @@ BraceWrapping: BreakBeforeBinaryOperators: None BreakBeforeBraces: Custom BreakBeforeConceptDeclarations: true -BreakBeforeTernaryOperators: true +BreakBeforeTernaryOperators: false BreakConstructorInitializers: AfterColon BreakInheritanceList: AfterColon BreakStringLiterals: true @@ -57,7 +58,7 @@ ColumnLimit: 100 CompactNamespaces: false ConstructorInitializerIndentWidth: 4 ContinuationIndentWidth: 4 -Cpp11BracedListStyle: false +Cpp11BracedListStyle: true DeriveLineEnding: false UseCRLF: false @@ -103,7 +104,7 @@ PenaltyBreakFirstLessLess: 120 PenaltyBreakString: 1000 PenaltyBreakOpenParenthesis: 0 PenaltyBreakTemplateDeclaration: 10 -PenaltyExcessCharacter: 1000000 +PenaltyExcessCharacter: 10 PenaltyReturnTypeOnItsOwnLine: 1000000 PenaltyIndentedWhitespace: 0 @@ -122,7 +123,7 @@ SpaceAfterTemplateKeyword: true SpaceAroundPointerQualifiers: Default SpaceBeforeAssignmentOperators: true SpaceBeforeCaseColon: false -SpaceBeforeCpp11BracedList: true +SpaceBeforeCpp11BracedList: false SpaceBeforeCtorInitializerColon: true SpaceBeforeInheritanceColon: true SpaceBeforeParens: ControlStatements diff --git a/loader/include/Geode/DefaultInclude.hpp b/loader/include/Geode/DefaultInclude.hpp index 71f58cf0..23a7a8e8 100644 --- a/loader/include/Geode/DefaultInclude.hpp +++ b/loader/include/Geode/DefaultInclude.hpp @@ -69,45 +69,120 @@ namespace cocos2d::extension {} #define GEODE_EXPAND(x) x #define GEODE_INVOKE(macro, ...) GEODE_EXPAND(macro(__VA_ARGS__)) -#define GEODE_FILL_CONSTRUCTOR(Class_, Offset_) \ - Class_(std::monostate, size_t fill) : \ - Class_({}, std::memset(reinterpret_cast(this) + Offset_, 0, fill - Offset_)) { \ - } \ +#define GEODE_FILL_CONSTRUCTOR(Class_, Offset_) \ + Class_(std::monostate, size_t fill) : \ + Class_({}, std::memset(reinterpret_cast(this) + Offset_, 0, fill - Offset_)) {} \ Class_(std::monostate, void*) -#define GEODE_MONOSTATE_CONSTRUCTOR_BEGIN(Class_) \ - GEODE_MACOS(GEODE_FILL_CONSTRUCTOR(Class_, 0) {}) \ - GEODE_IOS(GEODE_FILL_CONSTRUCTOR(Class_, 0) {}) +#define GEODE_MONOSTATE_CONSTRUCTOR_BEGIN(Class_) \ + GEODE_MACOS(GEODE_FILL_CONSTRUCTOR(Class_, 0){}) \ + GEODE_IOS(GEODE_FILL_CONSTRUCTOR(Class_, 0){}) -#define GEODE_MONOSTATE_CONSTRUCTOR_COCOS(Class_, Base_) \ - GEODE_MACOS(Class_(std::monostate, size_t fill) : Base_({}, fill) {}) \ - GEODE_IOS(Class_(std::monostate, size_t fill) : Base_({}, fill) {}) +#define GEODE_MONOSTATE_CONSTRUCTOR_COCOS(Class_, Base_) \ + GEODE_MACOS(Class_(std::monostate, size_t fill) : Base_({}, fill){}) \ + GEODE_IOS(Class_(std::monostate, size_t fill) : Base_({}, fill){}) -#define GEODE_MONOSTATE_CONSTRUCTOR_GD(Class_, Base_) \ - GEODE_WINDOWS(Class_(std::monostate, size_t fill) : Base_({}, fill) {}) \ - GEODE_MACOS(Class_(std::monostate, size_t fill) : Base_({}, fill) {}) \ - GEODE_IOS(Class_(std::monostate, size_t fill) : Base_({}, fill) {}) +#define GEODE_MONOSTATE_CONSTRUCTOR_GD(Class_, Base_) \ + GEODE_WINDOWS(Class_(std::monostate, size_t fill) : Base_({}, fill){}) \ + GEODE_MACOS(Class_(std::monostate, size_t fill) : Base_({}, fill){}) \ + GEODE_IOS(Class_(std::monostate, size_t fill) : Base_({}, fill){}) -#define GEODE_MONOSTATE_CONSTRUCTOR_CUTOFF(Class_, Base_) \ - GEODE_WINDOWS(GEODE_FILL_CONSTRUCTOR(Class_, sizeof(Base_)) : Base_() {}) \ - GEODE_MACOS(Class_(std::monostate, size_t fill) : Base_({}, fill) {}) \ - GEODE_IOS(Class_(std::monostate, size_t fill) : Base_({}, fill) {}) +#define GEODE_MONOSTATE_CONSTRUCTOR_CUTOFF(Class_, Base_) \ + GEODE_WINDOWS(GEODE_FILL_CONSTRUCTOR(Class_, sizeof(Base_)) : Base_(){}) \ + GEODE_MACOS(Class_(std::monostate, size_t fill) : Base_({}, fill){}) \ + GEODE_IOS(Class_(std::monostate, size_t fill) : Base_({}, fill){}) #define GEODE_NUMBER_OF_ARGS(...) \ GEODE_EXPAND(GEODE_NUMBER_OF_ARGS_(__VA_ARGS__, GEODE_NUMBER_SEQUENCE(), )) #define GEODE_NUMBER_OF_ARGS_(...) GEODE_EXPAND(GEODE_NUMBER_OF_ARGS_N(__VA_ARGS__)) -#define GEODE_NUMBER_OF_ARGS_N( \ - _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \ - _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, \ - _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, \ - _59, _60, _61, _62, _63, N, ... \ -) \ +#define GEODE_NUMBER_OF_ARGS_N( \ + _1, \ + _2, \ + _3, \ + _4, \ + _5, \ + _6, \ + _7, \ + _8, \ + _9, \ + _10, \ + _11, \ + _12, \ + _13, \ + _14, \ + _15, \ + _16, \ + _17, \ + _18, \ + _19, \ + _20, \ + _21, \ + _22, \ + _23, \ + _24, \ + _25, \ + _26, \ + _27, \ + _28, \ + _29, \ + _30, \ + _31, \ + _32, \ + _33, \ + _34, \ + _35, \ + _36, \ + _37, \ + _38, \ + _39, \ + _40, \ + _41, \ + _42, \ + _43, \ + _44, \ + _45, \ + _46, \ + _47, \ + _48, \ + _49, \ + _50, \ + _51, \ + _52, \ + _53, \ + _54, \ + _55, \ + _56, \ + _57, \ + _58, \ + _59, \ + _60, \ + _61, \ + _62, \ + _63, \ + N, \ + ... \ +) \ N #define GEODE_NUMBER_SEQUENCE() \ 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \ 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, \ 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 +#define $execute \ + template \ + void GEODE_CONCAT(geodeExecFunction, __LINE__)(); \ + namespace { \ + struct GEODE_CONCAT(ExecFuncUnique, __LINE__) {}; \ + } \ + static inline auto GEODE_CONCAT(Exec, __LINE__) = \ + (Loader::get()->scheduleOnModLoad( \ + getMod(), \ + &GEODE_CONCAT(geodeExecFunction, __LINE__) < GEODE_CONCAT(ExecFuncUnique, __LINE__) > \ + ), \ + 0); \ + template \ + void GEODE_CONCAT(geodeExecFunction, __LINE__)() + // #define GEODE_NEST1(macro, begin) \ // macro(GEODE_CONCAT(begin, 0)), \ // macro(GEODE_CONCAT(begin, 1)), \ diff --git a/loader/include/Geode/loader/Event.hpp b/loader/include/Geode/loader/Event.hpp index 22e5e4da..7b902895 100644 --- a/loader/include/Geode/loader/Event.hpp +++ b/loader/include/Geode/loader/Event.hpp @@ -1,5 +1,6 @@ #pragma once +#include "../utils/casts.hpp" #include "Mod.hpp" #include @@ -25,9 +26,9 @@ namespace geode { template struct to_member; - template + template struct to_member { - using value = R(C::*)(Args...); + using value = R (C::*)(Args...); }; template @@ -43,10 +44,9 @@ namespace geode { return fn(e); } }; - + template - concept is_filter = - std::is_base_of_v, T> && + concept is_filter = std::is_base_of_v, T> && requires(T a) { a.handle(std::declval(), std::declval()); }; @@ -55,14 +55,14 @@ namespace geode { class EventListener : public EventListenerProtocol { public: using Callback = typename T::Callback; - template requires std::is_class_v + template + requires std::is_class_v using MemberFn = typename to_member::value; ListenerResult passThrough(Event* e) override { - if (m_callback) { - if (auto myev = typeinfo_cast(e)) { - return m_filter.handle(m_callback, myev); - } + // it is so silly to use dynamic cast in an interbinary context + if (auto myev = cast::typeinfo_cast(e)) { + return m_filter.handle(m_callback, myev); } return ListenerResult::Propagate; } @@ -70,15 +70,19 @@ namespace geode { EventListener(T filter = T()) { this->enable(); } - EventListener(std::function fn, T filter = T()) : m_callback(fn), m_filter(filter) { + + EventListener(std::function fn, T filter = T()) : + m_callback(fn), m_filter(filter) { this->enable(); } + EventListener(Callback* fnptr, T filter = T()) : m_callback(fnptr), m_filter(filter) { this->enable(); } template - EventListener(C* cls, MemberFn fn, T filter = T()) : EventListener(std::bind(fn, cls, std::placeholders::_1), filter) { + EventListener(C* cls, MemberFn fn, T filter = T()) : + EventListener(std::bind(fn, cls, std::placeholders::_1), filter) { this->enable(); } @@ -86,11 +90,13 @@ namespace geode { std::cout << "this: " << this << "\n"; m_callback = fn; } + template void bind(C* cls, MemberFn fn) { std::cout << "this: " << this << "\n"; m_callback = std::bind(fn, cls, std::placeholders::_1); } + protected: std::function m_callback = nullptr; T m_filter; diff --git a/loader/include/Geode/loader/ModEvent.hpp b/loader/include/Geode/loader/ModEvent.hpp index 2b48e104..5dc1b3b7 100644 --- a/loader/include/Geode/loader/ModEvent.hpp +++ b/loader/include/Geode/loader/ModEvent.hpp @@ -1,9 +1,10 @@ #pragma once #include "Event.hpp" -#include #include "Mod.hpp" +#include + namespace geode { enum class ModEventType { Loaded, @@ -18,25 +19,25 @@ namespace geode { protected: ModEventType m_type; Mod* m_mod; - + public: ModStateEvent(Mod* mod, ModEventType type); ModEventType getType() const; Mod* getMod() const; }; - class GEODE_DLL ModStateFilter : public EventFilter { - public: - using Callback = void(ModStateEvent*); - - protected: + class GEODE_DLL ModStateFilter : public EventFilter { + public: + using Callback = void(ModStateEvent*); + + protected: ModEventType m_type; Mod* m_mod; - - public: + + public: ListenerResult handle(std::function fn, ModStateEvent* event); - ModStateFilter(Mod* mod, ModEventType type); - }; + ModStateFilter(Mod* mod, ModEventType type); + }; } #define $on_mod(type) \ diff --git a/loader/include/Geode/modify/InternalMacros.hpp b/loader/include/Geode/modify/InternalMacros.hpp index 90ad0975..8ddde752 100644 --- a/loader/include/Geode/modify/InternalMacros.hpp +++ b/loader/include/Geode/modify/InternalMacros.hpp @@ -55,21 +55,3 @@ * Useful for callbacks */ #define $cls std::remove_pointer::type - -#define GEODE_ONLY_FIELD(type, field_, default_) private: field field_ = default_; public: -#define GEODE_INTERNAL_FIELD(type, field, name) inline type& name() { return this->*field; } -//#define GEODE_EXTERNAL_FIELD(type, field, name) static inline type& name##From(void* self) { return reinterpret_cast(self)->*field; } -#define GEODE_FIELD(type, field, name, default_) GEODE_ONLY_FIELD(type, field, default_) GEODE_INTERNAL_FIELD(type, field, name) //GEODE_EXTERNAL_FIELD(type, field, name) - - -#define $execute \ -template \ -void GEODE_CONCAT(geodeExecFunction, __LINE__)(); \ -namespace { \ - struct GEODE_CONCAT(ExecFuncUnique, __LINE__) {}; \ -} \ -static inline auto GEODE_CONCAT(Exec, __LINE__) = (Loader::get()->scheduleOnModLoad(\ - nullptr, &GEODE_CONCAT(geodeExecFunction, __LINE__) \ -), 0); \ -template \ -void GEODE_CONCAT(geodeExecFunction, __LINE__)() diff --git a/loader/include/Geode/utils/JsonValidation.hpp b/loader/include/Geode/utils/JsonValidation.hpp index db9c011b..9de90ac1 100644 --- a/loader/include/Geode/utils/JsonValidation.hpp +++ b/loader/include/Geode/utils/JsonValidation.hpp @@ -1,7 +1,7 @@ #pragma once -#include "../loader/Log.hpp" #include "../external/json/json.hpp" +#include "../loader/Log.hpp" #include #include @@ -15,9 +15,7 @@ namespace geode { template struct is_iterable< - T, - std::void_t< - decltype(std::begin(std::declval())), decltype(std::end(std::declval()))>> : + T, std::void_t())), decltype(std::end(std::declval()))>> : std::true_type {}; template @@ -79,7 +77,7 @@ namespace geode { } template - using JsonValueValidator = bool (*)(T const&); + using JsonValueValidator = std::function; template struct JsonMaybeObject; @@ -175,6 +173,11 @@ namespace geode { return *this; } + template + JsonMaybeValue& validate(bool (*validator)(T const&)) { + return this->validate(std::function(validator)); + } + template JsonMaybeValue& inferType() { if (this->isError() || !m_inferType) return *this; diff --git a/loader/include/Geode/utils/cocos.hpp b/loader/include/Geode/utils/cocos.hpp index 5650c2b3..8d3941a1 100644 --- a/loader/include/Geode/utils/cocos.hpp +++ b/loader/include/Geode/utils/cocos.hpp @@ -1,7 +1,7 @@ #pragma once -#include "casts.hpp" #include "../external/json/json.hpp" +#include "casts.hpp" #include "general.hpp" #include "../DefaultInclude.hpp" #include @@ -140,11 +140,11 @@ namespace geode { } static cocos2d::CCSize operator-(cocos2d::CCSize const& size, float f) { - return { size.width - f, size.height - f }; + return {size.width - f, size.height - f}; } static cocos2d::CCSize operator-(cocos2d::CCSize const& size) { - return { -size.width, -size.height }; + return {-size.width, -size.height}; } static bool operator==(cocos2d::CCPoint const& p1, cocos2d::CCPoint const& p2) { @@ -284,6 +284,7 @@ namespace geode { bool operator==(T* other) const { return m_obj == other; } + bool operator==(Ref const& other) const { return m_obj == other.m_obj; } @@ -291,6 +292,7 @@ namespace geode { bool operator!=(T* other) const { return m_obj != other; } + bool operator!=(Ref const& other) const { return m_obj != other.m_obj; } @@ -299,6 +301,7 @@ namespace geode { bool operator<(Ref const& other) const { return m_obj < other.m_obj; } + bool operator>(Ref const& other) const { return m_obj > other.m_obj; } @@ -436,13 +439,13 @@ namespace geode::cocos { */ GEODE_DLL cocos2d::CCScene* switchToScene(cocos2d::CCLayer* layer); - using CreateLayerFunc = cocos2d::CCLayer*(*)(); + using CreateLayerFunc = std::function; /** - * Reload textures, overwriting the scene to return to after the loading + * Reload textures, overwriting the scene to return to after the loading * screen is finished - * @param returnTo A function that returns a new layer. After loading is - * finished, the game switches to the given layer instead of MenuLayer. + * @param returnTo A function that returns a new layer. After loading is + * finished, the game switches to the given layer instead of MenuLayer. * Leave nullptr to enable default behaviour */ GEODE_DLL void reloadTextures(CreateLayerFunc returnTo = nullptr); @@ -454,9 +457,7 @@ namespace geode::cocos { * @param def Default size * @param min Minimum size */ - GEODE_DLL void limitNodeSize( - cocos2d::CCNode* node, cocos2d::CCSize const& size, float def, float min - ); + GEODE_DLL void limitNodeSize(cocos2d::CCNode* node, cocos2d::CCSize const& size, float def, float min); /** * Checks if a node is visible (recursively @@ -479,12 +480,12 @@ namespace geode::cocos { GEODE_DLL cocos2d::CCNode* getChildByTagRecursive(cocos2d::CCNode* node, int tag); /** - * Get first node that conforms to the predicate + * Get first node that conforms to the predicate * by traversing children recursively - * + * * @param node Parent node * @param predicate Predicate used to evaluate nodes - * @return Child node if one is found, or null if + * @return Child node if one is found, or null if * there is none */ template @@ -498,8 +499,7 @@ namespace geode::cocos { for (int i = 0; i < children->count(); ++i) { auto newParent = static_cast(children->objectAtIndex(i)); auto child = findFirstChildRecursive(newParent, predicate); - if (child) - return child; + if (child) return child; } return nullptr; @@ -603,13 +603,18 @@ namespace geode::cocos { } inline cocos2d::ccColor4B invert4B(cocos2d::ccColor4B const& color) { - return { static_cast(255 - color.r), static_cast(255 - color.g), - static_cast(255 - color.b), color.a }; + return { + static_cast(255 - color.r), + static_cast(255 - color.g), + static_cast(255 - color.b), + color.a}; } inline cocos2d::ccColor3B invert3B(cocos2d::ccColor3B const& color) { - return { static_cast(255 - color.r), static_cast(255 - color.g), - static_cast(255 - color.b) }; + return { + static_cast(255 - color.r), + static_cast(255 - color.g), + static_cast(255 - color.b)}; } inline cocos2d::ccColor3B lighten3B(cocos2d::ccColor3B const& color, int amount) { @@ -625,34 +630,38 @@ namespace geode::cocos { } inline cocos2d::ccColor3B to3B(cocos2d::ccColor4B const& color) { - return { color.r, color.g, color.b }; + return {color.r, color.g, color.b}; } inline cocos2d::ccColor4B to4B(cocos2d::ccColor3B const& color, GLubyte alpha = 255) { - return { color.r, color.g, color.b, alpha }; + return {color.r, color.g, color.b, alpha}; } inline cocos2d::ccColor4F to4F(cocos2d::ccColor4B const& color) { - return { color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f }; + return {color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f}; } constexpr cocos2d::ccColor3B cc3x(int hexValue) { if (hexValue <= 0xf) - return cocos2d::ccColor3B { static_cast(hexValue * 17), - static_cast(hexValue * 17), - static_cast(hexValue * 17) }; + return cocos2d::ccColor3B{ + static_cast(hexValue * 17), + static_cast(hexValue * 17), + static_cast(hexValue * 17)}; if (hexValue <= 0xff) - return cocos2d::ccColor3B { static_cast(hexValue), - static_cast(hexValue), - static_cast(hexValue) }; + return cocos2d::ccColor3B{ + static_cast(hexValue), + static_cast(hexValue), + static_cast(hexValue)}; if (hexValue <= 0xfff) - return cocos2d::ccColor3B { static_cast((hexValue >> 8 & 0xf) * 17), - static_cast((hexValue >> 4 & 0xf) * 17), - static_cast((hexValue >> 0 & 0xf) * 17) }; + return cocos2d::ccColor3B{ + static_cast((hexValue >> 8 & 0xf) * 17), + static_cast((hexValue >> 4 & 0xf) * 17), + static_cast((hexValue >> 0 & 0xf) * 17)}; else - return cocos2d::ccColor3B { static_cast(hexValue >> 16 & 0xff), - static_cast(hexValue >> 8 & 0xff), - static_cast(hexValue >> 0 & 0xff) }; + return cocos2d::ccColor3B{ + static_cast(hexValue >> 16 & 0xff), + static_cast(hexValue >> 8 & 0xff), + static_cast(hexValue >> 0 & 0xff)}; } GEODE_DLL Result cc3bFromHexString(std::string const& hexValue); @@ -669,9 +678,7 @@ namespace geode::cocos { } template >> - static cocos2d::CCArray* vectorToCCArray( - std::vector const& vec, std::function convFunc - ) { + static cocos2d::CCArray* vectorToCCArray(std::vector const& vec, std::function convFunc) { auto res = cocos2d::CCArray::createWithCapacity(vec.size()); for (auto const& item : vec) res->addObject(convFunc(item)); @@ -681,8 +688,7 @@ namespace geode::cocos { template >> std::vector ccArrayToVector(cocos2d::CCArray* arr) { return std::vector( - reinterpret_cast(arr->data->arr), - reinterpret_cast(arr->data->arr) + arr->data->num + reinterpret_cast(arr->data->arr), reinterpret_cast(arr->data->arr) + arr->data->num ); } @@ -699,9 +705,7 @@ namespace geode::cocos { template < typename K, typename V, typename C, typename = std::enable_if_t || std::is_same_v>> - static cocos2d::CCDictionary* mapToCCDict( - std::map const& map, std::function convFunc - ) { + static cocos2d::CCDictionary* mapToCCDict(std::map const& map, std::function convFunc) { auto res = cocos2d::CCDictionary::create(); for (auto const& [key, value] : map) res->setObject(value, convFunc(key)); @@ -726,7 +730,7 @@ namespace geode::cocos { // std specializations namespace std { // enables using Ref as the key in unordered_map etc. - template + template struct hash> { size_t operator()(geode::Ref const& ref) const { return std::hash()(ref.data()); @@ -798,6 +802,7 @@ namespace geode::cocos { } return CCArrayIterator(reinterpret_cast(m_arr->data->arr) + m_arr->count()); } + size_t size() const { return m_arr ? m_arr->count() : 0; } @@ -830,10 +835,10 @@ namespace geode::cocos { std::pair operator*() { if constexpr (std::is_same::value) { - return { m_ptr->getStrKey(), static_cast(m_ptr->getObject()) }; + return {m_ptr->getStrKey(), static_cast(m_ptr->getObject())}; } else { - return { m_ptr->getIntKey(), static_cast(m_ptr->getObject()) }; + return {m_ptr->getIntKey(), static_cast(m_ptr->getObject())}; } } @@ -998,7 +1003,7 @@ namespace geode::cocos { using Selector = Ret (Base::*)(Args...); using Holder = LambdaHolder; - static inline Holder s_selector {}; + static inline Holder s_selector{}; Ret selector(Args... args) { return s_selector(std::forward(args)...); diff --git a/loader/include/Geode/utils/web.hpp b/loader/include/Geode/utils/web.hpp index 94b08f4a..8cbe6935 100644 --- a/loader/include/Geode/utils/web.hpp +++ b/loader/include/Geode/utils/web.hpp @@ -1,8 +1,8 @@ #pragma once #include "../DefaultInclude.hpp" -#include "Result.hpp" #include "../external/json/json.hpp" +#include "Result.hpp" #include "general.hpp" #include @@ -10,7 +10,7 @@ namespace geode::utils::web { GEODE_DLL void openLinkInBrowser(std::string const& url); - + using FileProgressCallback = std::function; /** @@ -38,8 +38,7 @@ namespace geode::utils::web { * @returns Returned data as JSON, or error on error */ GEODE_DLL Result<> fetchFile( - std::string const& url, ghc::filesystem::path const& into, - FileProgressCallback prog = nullptr + std::string const& url, ghc::filesystem::path const& into, FileProgressCallback prog = nullptr ); /** diff --git a/loader/src/cocos2d-ext/zip/zip.cpp b/loader/src/cocos2d-ext/zip/zip.cpp index ad37b22e..30490bce 100644 --- a/loader/src/cocos2d-ext/zip/zip.cpp +++ b/loader/src/cocos2d-ext/zip/zip.cpp @@ -2,7 +2,8 @@ Version 1.1, February 14h, 2010 part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html ) - Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html ) + Copyright (C) 1998-2010 Gilles Vollant (minizip) ( + http://www.winimage.com/zLibDll/minizip.html ) Modifications for Zip64 support Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com ) @@ -12,15 +13,17 @@ Changes Oct-2009 - Mathias Svensson - Remove old C style function prototypes Oct-2009 - Mathias Svensson - Added Zip64 Support when creating new file archives - Oct-2009 - Mathias Svensson - Did some code cleanup and refactoring to get better overview of some functions. - Oct-2009 - Mathias Svensson - Added zipRemoveExtraInfoBlock to strip extra field data from its ZIP64 data - It is used when recreting zip archive with RAW when deleting items from a zip. - ZIP64 data is automatically added to items that needs it, and existing ZIP64 data need to be removed. - Oct-2009 - Mathias Svensson - Added support for BZIP2 as compression mode (bzip2 lib is required) - Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer + Oct-2009 - Mathias Svensson - Did some code cleanup and refactoring to get better overview of + some functions. Oct-2009 - Mathias Svensson - Added zipRemoveExtraInfoBlock to strip extra field + data from its ZIP64 data It is used when recreting zip archive with RAW when deleting items from + a zip. ZIP64 data is automatically added to items that needs it, and existing ZIP64 data need to + be removed. Oct-2009 - Mathias Svensson - Added support for BZIP2 as compression mode (bzip2 lib + is required) Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer */ +// clang-format off + #ifndef NOCRYPT #define NOCRYPT #endif @@ -1071,7 +1074,7 @@ extern int ZEXPORT zipOpenNewFileInZip4_64 (zipFile file, const char* filename, int err = ZIP_OK; # ifdef NOCRYPT - (crcForCrypting); + (void)(crcForCrypting); if (password != NULL) return ZIP_PARAMERROR; # endif @@ -2006,3 +2009,5 @@ extern int ZEXPORT zipRemoveExtraInfoBlock (char* pData, int* dataLen, short sHe } } // end of namespace cocos2d + +// clang-format on diff --git a/loader/src/loader/Event.cpp b/loader/src/loader/Event.cpp index 3d71d8af..98713df7 100644 --- a/loader/src/loader/Event.cpp +++ b/loader/src/loader/Event.cpp @@ -15,7 +15,7 @@ void EventListenerProtocol::disable() { } EventListenerProtocol::~EventListenerProtocol() { - this->disable(); + this->disable(); } Event::~Event() {} diff --git a/loader/src/loader/Mod.cpp b/loader/src/loader/Mod.cpp index 352eb847..eebe81c5 100644 --- a/loader/src/loader/Mod.cpp +++ b/loader/src/loader/Mod.cpp @@ -125,7 +125,8 @@ Result<> Mod::loadData() { } else { log::log( - Severity::Warning, this, + Severity::Warning, + this, "Encountered unknown setting \"{}\" while loading " "settings", key diff --git a/loader/src/loader/ModEvent.cpp b/loader/src/loader/ModEvent.cpp index 85ed083f..753d8e5d 100644 --- a/loader/src/loader/ModEvent.cpp +++ b/loader/src/loader/ModEvent.cpp @@ -19,7 +19,4 @@ ListenerResult ModStateFilter::handle(std::function fn, ModStateEvent* return ListenerResult::Propagate; } -ModStateFilter::ModStateFilter( - Mod* mod, - ModEventType type -) : m_mod(mod), m_type(type) {} +ModStateFilter::ModStateFilter(Mod* mod, ModEventType type) : m_mod(mod), m_type(type) {} diff --git a/loader/src/main.cpp b/loader/src/main.cpp index 9a26ba3b..2a57c3a6 100644 --- a/loader/src/main.cpp +++ b/loader/src/main.cpp @@ -1,11 +1,11 @@ #include "../core/Core.hpp" +#include #include #include #include -#include #include -#include +#include #include #include #include @@ -102,27 +102,25 @@ BOOL WINAPI DllMain(HINSTANCE lib, DWORD reason, LPVOID) { #define $_ GEODE_CONCAT(unnamedVar_, __LINE__) -static auto $_ = listenForSettingChanges( - "show-platform-console", - [](BoolSetting* setting) { +static auto $_ = + listenForSettingChanges("show-platform-console", [](BoolSetting* setting) { if (setting->getValue()) { Loader::get()->openPlatformConsole(); } else { Loader::get()->closePlatfromConsole(); } - } -); + }); -static auto $_ = listenForIPC("ipc-test", +[](IPCEvent* event) -> nlohmann::json { +static auto $_ = listenForIPC("ipc-test", [](IPCEvent* event) -> nlohmann::json { return "Hello from Geode!"; }); -static auto $_ = listenForIPC("loader-info", +[](IPCEvent* event) -> nlohmann::json { +static auto $_ = listenForIPC("loader-info", [](IPCEvent* event) -> nlohmann::json { return Loader::get()->getInternalMod()->getModInfo(); }); -static auto $_ = listenForIPC("list-mods", +[](IPCEvent* event) -> nlohmann::json { +static auto $_ = listenForIPC("list-mods", [](IPCEvent* event) -> nlohmann::json { std::vector res; auto args = event->getMessageData(); @@ -133,18 +131,14 @@ static auto $_ = listenForIPC("list-mods", +[](IPCEvent* event) -> nlohmann::jso auto dontIncludeLoader = root.has("dont-include-loader").template get(); if (!dontIncludeLoader) { - res.push_back(includeRunTimeInfo ? - Loader::get()->getInternalMod()->getRuntimeInfo() : - Loader::get()->getInternalMod()->getModInfo().toJSON() + res.push_back( + includeRunTimeInfo ? Loader::get()->getInternalMod()->getRuntimeInfo() : + Loader::get()->getInternalMod()->getModInfo().toJSON() ); } for (auto& mod : Loader::get()->getAllMods()) { - res.push_back( - includeRunTimeInfo ? - mod->getRuntimeInfo() : - mod->getModInfo().toJSON() - ); + res.push_back(includeRunTimeInfo ? mod->getRuntimeInfo() : mod->getModInfo().toJSON()); } return res; diff --git a/loader/src/platform/mac/InternalLoader.cpp b/loader/src/platform/mac/InternalLoader.cpp index ca54d58e..8c791e2a 100644 --- a/loader/src/platform/mac/InternalLoader.cpp +++ b/loader/src/platform/mac/InternalLoader.cpp @@ -1,12 +1,12 @@ -#include -#include -#include -#include #include +#include +#include +#include +#include #ifdef GEODE_IS_MACOS -#include + #include void InternalLoader::platformMessageBox(char const* title, std::string const& info) { CFStringRef cfTitle = CFStringCreateWithCString(NULL, title, kCFStringEncodingUTF8); @@ -29,46 +29,37 @@ void InternalLoader::closePlatformConsole() { m_platformConsoleOpen = false; } -CFDataRef msgPortCallback( - CFMessagePortRef port, - SInt32 messageID, - CFDataRef data, - void* info -) { - if(!CFDataGetLength(data)) - return NULL; +CFDataRef msgPortCallback(CFMessagePortRef port, SInt32 messageID, CFDataRef data, void* info) { + if (!CFDataGetLength(data)) return NULL; - std::string cdata( - reinterpret_cast(CFDataGetBytePtr(data)), - CFDataGetLength(data) - ); + std::string cdata(reinterpret_cast(CFDataGetBytePtr(data)), CFDataGetLength(data)); std::string reply = InternalLoader::processRawIPC(port, cdata); - return CFDataCreate(NULL, (const UInt8*)reply.data(), reply.size()); + return CFDataCreate(NULL, (UInt8 const*)reply.data(), reply.size()); } void InternalLoader::setupIPC() { std::thread([]() { CFStringRef portName = CFStringCreateWithCString(NULL, IPC_PORT_NAME, kCFStringEncodingUTF8); - CFMessagePortRef localPort = CFMessagePortCreateLocal( - NULL, - portName, - msgPortCallback, - NULL, - NULL - ); + CFMessagePortRef localPort = + CFMessagePortCreateLocal(NULL, portName, msgPortCallback, NULL, NULL); + if (localPort == NULL) { + log::warn("Unable to create port, quitting IPC"); + return; + } CFRunLoopSourceRef runLoopSource = CFMessagePortCreateRunLoopSource(NULL, localPort, 0); - CFRunLoopAddSource( - CFRunLoopGetCurrent(), - runLoopSource, - kCFRunLoopCommonModes - ); + if (runLoopSource == NULL) { + log::warn("Unable to create loop source, quitting IPC"); + return; + } + + CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes); CFRunLoopRun(); CFRelease(localPort); }).detach(); - log::log(Severity::Warning, InternalMod::get(), "IPC is not supported on this platform"); + log::debug("IPC set up"); } #endif diff --git a/loader/src/platform/windows/InternalLoader.cpp b/loader/src/platform/windows/InternalLoader.cpp index 638b084f..ad07754f 100644 --- a/loader/src/platform/windows/InternalLoader.cpp +++ b/loader/src/platform/windows/InternalLoader.cpp @@ -1,8 +1,8 @@ -#include #include #include -#include +#include #include +#include USE_GEODE_NAMESPACE(); @@ -77,10 +77,10 @@ void InternalLoader::setupIPC() { nullptr ); if (pipe == INVALID_HANDLE_VALUE) { - // todo: Rn this quits IPC, but we might wanna change that later - // to just continue trying. however, I'm assuming that if - // CreateNamedPipeA fails, then it will probably fail again if - // you try right after, so changing the break; to continue; might + // todo: Rn this quits IPC, but we might wanna change that later + // to just continue trying. however, I'm assuming that if + // CreateNamedPipeA fails, then it will probably fail again if + // you try right after, so changing the break; to continue; might // just result in the console getting filled with error messages log::warn("Unable to create pipe, quitting IPC"); break; @@ -89,14 +89,15 @@ void InternalLoader::setupIPC() { if (ConnectNamedPipe(pipe, nullptr)) { // log::debug("Got connection, creating thread"); std::thread(&ipcPipeThread, pipe).detach(); - } else { + } + else { // log::debug("No connection, cleaning pipe"); CloseHandle(pipe); } } }).detach(); - log::log(Severity::Debug, InternalMod::get(), "IPC set up"); + log::debug("IPC set up"); } #endif diff --git a/loader/src/utils/JsonValidation.cpp b/loader/src/utils/JsonValidation.cpp index 044ab481..3303e564 100644 --- a/loader/src/utils/JsonValidation.cpp +++ b/loader/src/utils/JsonValidation.cpp @@ -205,14 +205,12 @@ JsonMaybeValue JsonMaybeValue::at(size_t i) { return *this; } return JsonMaybeValue( - self().m_checker, self().m_json.at(i), self().m_hierarchy + "." + std::to_string(i), - self().m_hasValue + self().m_checker, self().m_json.at(i), self().m_hierarchy + "." + std::to_string(i), self().m_hasValue ); } template -typename JsonMaybeValue::template Iterator> JsonMaybeValue< - Json>::iterate() { +typename JsonMaybeValue::template Iterator> JsonMaybeValue::iterate() { this->as(); Iterator> iter; if (this->isError()) return iter; @@ -226,8 +224,8 @@ typename JsonMaybeValue::template Iterator> JsonMaybe } template -typename JsonMaybeValue::template Iterator>> -JsonMaybeValue::items() { +typename JsonMaybeValue::template Iterator>> JsonMaybeValue< + Json>::items() { this->as(); Iterator>> iter; if (this->isError()) return iter; @@ -235,9 +233,7 @@ JsonMaybeValue::items() { for (auto& [k, v] : self().m_json.items()) { iter.m_values.emplace_back( k, - JsonMaybeValue( - self().m_checker, v, self().m_hierarchy + "." + k, self().m_hasValue - ) + JsonMaybeValue(self().m_checker, v, self().m_hierarchy + "." + k, self().m_hasValue) ); } diff --git a/loader/src/utils/addresser.cpp b/loader/src/utils/addresser.cpp index a7c102f2..d9a4c9b8 100644 --- a/loader/src/utils/addresser.cpp +++ b/loader/src/utils/addresser.cpp @@ -14,8 +14,7 @@ macro(GEODE_CONCAT(begin, 7)), macro(GEODE_CONCAT(begin, 8)), \ macro(GEODE_CONCAT(begin, 9)), macro(GEODE_CONCAT(begin, a)), \ macro(GEODE_CONCAT(begin, b)), macro(GEODE_CONCAT(begin, c)), \ - macro(GEODE_CONCAT(begin, d)), macro(GEODE_CONCAT(begin, e)), \ - macro(GEODE_CONCAT(begin, f)) + macro(GEODE_CONCAT(begin, d)), macro(GEODE_CONCAT(begin, e)), macro(GEODE_CONCAT(begin, f)) #define GEODE_ADDRESSER_NEST2(macro, begin) \ GEODE_ADDRESSER_NEST1(macro, GEODE_CONCAT(begin, 0)), \ @@ -53,13 +52,13 @@ namespace { using table_table_t = intptr_t[0x100]; struct GEODE_HIDDEN ThunkTable { - static inline thunk0_table_t table = { GEODE_ADDRESSER_THUNK0_SET() }; + static inline thunk0_table_t table = {GEODE_ADDRESSER_THUNK0_SET()}; }; class GEODE_HIDDEN TableTable { friend class geode::addresser::Addresser; - static inline table_table_t table = { GEODE_ADDRESSER_TABLE_SET() }; + static inline table_table_t table = {GEODE_ADDRESSER_TABLE_SET()}; }; } diff --git a/loader/src/utils/cocos.cpp b/loader/src/utils/cocos.cpp index ac2a6fef..7f79ef30 100644 --- a/loader/src/utils/cocos.cpp +++ b/loader/src/utils/cocos.cpp @@ -4,10 +4,10 @@ USE_GEODE_NAMESPACE(); void cocos2d::to_json(nlohmann::json& json, ccColor3B const& color) { - json = nlohmann::json { - { "r", color.r }, - { "g", color.g }, - { "b", color.b }, + json = nlohmann::json{ + {"r", color.r}, + {"g", color.g}, + {"b", color.b}, }; } @@ -20,9 +20,7 @@ void cocos2d::from_json(nlohmann::json const& json, ccColor3B& color) { json.at(2).get_to(color.b); } else { - throw nlohmann::json::type_error::create( - 0, "Expected color array to have 3 items", json - ); + throw nlohmann::json::type_error::create(0, "Expected color array to have 3 items", json); } } // object @@ -57,11 +55,11 @@ void cocos2d::from_json(nlohmann::json const& json, ccColor3B& color) { } void cocos2d::to_json(nlohmann::json& json, ccColor4B const& color) { - json = nlohmann::json { - { "r", color.r }, - { "g", color.g }, - { "b", color.b }, - { "a", color.a }, + json = nlohmann::json{ + {"r", color.r}, + {"g", color.g}, + {"b", color.b}, + {"a", color.a}, }; } @@ -75,9 +73,7 @@ void cocos2d::from_json(nlohmann::json const& json, ccColor4B& color) { json.at(3).get_to(color.a); } else { - throw nlohmann::json::type_error::create( - 0, "Expected color array to have 4 items", json - ); + throw nlohmann::json::type_error::create(0, "Expected color array to have 4 items", json); } } // object @@ -245,7 +241,7 @@ CCRect geode::cocos::calculateNodeCoverage(std::vector const& nodes) { for (auto child : nodes) { auto pos = child->getPosition() - child->getScaledContentSize() * child->getAnchorPoint(); auto csize = child->getPosition() + - child->getScaledContentSize() * (CCPoint { 1.f, 1.f } - child->getAnchorPoint()); + child->getScaledContentSize() * (CCPoint{1.f, 1.f} - child->getAnchorPoint()); if (pos.x < coverage.origin.x) { coverage.origin.x = pos.x; } @@ -267,7 +263,7 @@ CCRect geode::cocos::calculateNodeCoverage(CCArray* nodes) { for (auto child : CCArrayExt(nodes)) { auto pos = child->getPosition() - child->getScaledContentSize() * child->getAnchorPoint(); auto csize = child->getPosition() + - child->getScaledContentSize() * (CCPoint { 1.f, 1.f } - child->getAnchorPoint()); + child->getScaledContentSize() * (CCPoint{1.f, 1.f} - child->getAnchorPoint()); if (pos.x < coverage.origin.x) { coverage.origin.x = pos.x; } @@ -288,9 +284,7 @@ CCRect geode::cocos::calculateChildCoverage(CCNode* parent) { return calculateNodeCoverage(parent->getChildren()); } -void geode::cocos::limitNodeSize( - cocos2d::CCNode* spr, cocos2d::CCSize const& size, float def, float min -) { +void geode::cocos::limitNodeSize(cocos2d::CCNode* spr, cocos2d::CCSize const& size, float def, float min) { spr->setScale(1.f); auto [cwidth, cheight] = spr->getContentSize(); diff --git a/loader/src/utils/file.cpp b/loader/src/utils/file.cpp index 0414e6e4..67bc8e76 100644 --- a/loader/src/utils/file.cpp +++ b/loader/src/utils/file.cpp @@ -164,12 +164,14 @@ public: // Read file and add to entries unz_file_pos pos; if (unzGetFilePos(m_zip, &pos) == UNZ_OK) { - m_entries.insert({ fileName, - ZipEntry { - .m_pos = pos, - .m_compressedSize = fileInfo.compressed_size, - .m_uncompressedSize = fileInfo.uncompressed_size, - } }); + m_entries.insert({ + fileName, + ZipEntry { + .m_pos = pos, + .m_compressedSize = fileInfo.compressed_size, + .m_uncompressedSize = fileInfo.uncompressed_size, + }, + }); } // Read next file, or break on error if (unzGoToNextFile64(m_zip, &fileInfo, fileName, sizeof(fileName) - 1) != UNZ_OK) { diff --git a/loader/src/utils/string.cpp b/loader/src/utils/string.cpp index 27a7ecd1..1200acc5 100644 --- a/loader/src/utils/string.cpp +++ b/loader/src/utils/string.cpp @@ -91,9 +91,7 @@ std::wstring utils::string::toUpper(std::wstring const& str) { return utils::string::toUpperIP(ret); } -std::string& utils::string::replaceIP( - std::string& str, std::string const& orig, std::string const& repl -) { +std::string& utils::string::replaceIP(std::string& str, std::string const& orig, std::string const& repl) { std::string::size_type n = 0; while ((n = str.find(orig, n)) != std::string::npos) { str.replace(n, orig.size(), repl); @@ -248,7 +246,8 @@ std::wstring& utils::string::trimLeftIP(std::wstring& str) { std::string& utils::string::trimRightIP(std::string& str) { str.erase( std::find_if( - str.rbegin(), str.rend(), + str.rbegin(), + str.rend(), [](auto ch) { return !std::isspace(ch); } @@ -261,7 +260,8 @@ std::string& utils::string::trimRightIP(std::string& str) { std::wstring& utils::string::trimRightIP(std::wstring& str) { str.erase( std::find_if( - str.rbegin(), str.rend(), + str.rbegin(), + str.rend(), [](auto ch) { return !std::isspace(ch); } diff --git a/loader/src/utils/web.cpp b/loader/src/utils/web.cpp index dc362d9b..8d25333b 100644 --- a/loader/src/utils/web.cpp +++ b/loader/src/utils/web.cpp @@ -125,7 +125,7 @@ Result web::fetch(std::string const& url) { return Err("Error getting info: " + std::string(curl_easy_strerror(res))); } -static std::unordered_map RUNNING_REQUESTS {}; +static std::unordered_map RUNNING_REQUESTS{}; static std::mutex RUNNING_REQUESTS_MUTEX; SentAsyncWebRequest::SentAsyncWebRequest(AsyncWebRequest const& req, std::string const& id) : @@ -190,10 +190,11 @@ SentAsyncWebRequest::SentAsyncWebRequest(AsyncWebRequest const& req, std::string struct ProgressData { SentAsyncWebRequest* self; std::ofstream* file; - } data { this, file.get() }; + } data{this, file.get()}; curl_easy_setopt( - curl, CURLOPT_PROGRESSFUNCTION, + curl, + CURLOPT_PROGRESSFUNCTION, +[](void* ptr, double total, double now, double, double) -> int { auto data = static_cast(ptr); while (data->self->m_paused) {} @@ -350,7 +351,7 @@ SentAsyncWebRequestHandle AsyncWebRequest::send() { else { auto id = m_joinID.value_or("__anon_request_" + std::to_string(COUNTER++)); ret = std::make_shared(*this, id); - RUNNING_REQUESTS.insert({ id, ret }); + RUNNING_REQUESTS.insert({id, ret}); } // resume all running requests