From d5718be4224e23ddf97703eda1996782ed779a35 Mon Sep 17 00:00:00 2001 From: HJfod <60038575+HJfod@users.noreply.github.com> Date: Mon, 9 Sep 2024 22:29:47 +0300 Subject: [PATCH] add PlatformID::getCovered --- loader/include/Geode/platform/platform.hpp | 25 ++++++++---- loader/src/utils/PlatformID.cpp | 46 ++++++++++++++-------- 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/loader/include/Geode/platform/platform.hpp b/loader/include/Geode/platform/platform.hpp index 2b5df536..c8d52484 100644 --- a/loader/include/Geode/platform/platform.hpp +++ b/loader/include/Geode/platform/platform.hpp @@ -114,15 +114,16 @@ namespace geode { class PlatformID { public: + // todo in v4: make these flags and add archless Mac and Android as well as Desktop and Mobile and remove Linux enum { - Unknown = -1, - Windows, - MacIntel, - MacArm, - iOS, - Android32, - Android64, - Linux, + Unknown = -1, + Windows = 0, + MacIntel = 1, + MacArm = 2, + iOS = 3, + Android32 = 4, + Android64 = 5, + Linux = 6, }; using Type = decltype(Unknown); @@ -172,7 +173,14 @@ namespace geode { */ static GEODE_DLL bool coveredBy(const char* str, PlatformID t); static GEODE_DLL bool coveredBy(std::string const& str, PlatformID t); + /** + * Returns the list of platforms covered by this string name. For + * example, "android" would return both Android32 and Android64 + * todo in v4: deprecate this as the flagged version deals with this + */ + static GEODE_DLL std::vector getCovered(std::string_view str); + // todo in v4: this does not need to be constexpr in the header. dllexport it static constexpr char const* toString(Type lp) { switch (lp) { case Unknown: return "Unknown"; @@ -188,6 +196,7 @@ namespace geode { return "Undefined"; } + // todo in v4: this does not need to be constexpr in the header. dllexport it static constexpr char const* toShortString(Type lp, bool ignoreArch = false) { switch (lp) { case Unknown: return "unknown"; diff --git a/loader/src/utils/PlatformID.cpp b/loader/src/utils/PlatformID.cpp index 02ec045c..49a746c5 100644 --- a/loader/src/utils/PlatformID.cpp +++ b/loader/src/utils/PlatformID.cpp @@ -5,6 +5,12 @@ using namespace geode::prelude; PlatformID PlatformID::from(const char* str) { + // todo in v4: this should just be + // "win" -> Windows + // "mac", "mac-intel", "mac-arm" -> Mac + // "ios" -> iOS + // "android", "android32", "android64" -> Android + // no linux switch (hash(str)) { case hash("win"): case hash("Windows"): @@ -33,29 +39,35 @@ PlatformID PlatformID::from(const char* str) { } bool PlatformID::coveredBy(const char* str, PlatformID t) { - switch (hash(str)) { - case hash("win"): return t == PlatformID::Windows; - - case hash("mac"): return t == PlatformID::MacIntel || t == PlatformID::MacArm; - case hash("mac-intel"): return t == PlatformID::MacIntel; - case hash("mac-arm"): return t == PlatformID::MacArm; - - case hash("ios"): return t == PlatformID::iOS; - - case hash("android"): return t == PlatformID::Android32 || t == PlatformID::Android64; - case hash("android32"): return t == PlatformID::Android32; - case hash("android64"): return t == PlatformID::Android64; - - case hash("linux"): return t == PlatformID::Linux; - - default: return false; - } + // todo in v4: this is ridiculously inefficient currently - in v4 just use a flag check! + return ranges::contains(getCovered(str), t); } bool PlatformID::coveredBy(std::string const& str, PlatformID t) { return PlatformID::coveredBy(str.c_str(), t); } +std::vector PlatformID::getCovered(std::string_view str) { + switch (hash(str)) { + case hash("win"): return { PlatformID::Windows }; + + case hash("mac"): return { PlatformID::MacIntel, PlatformID::MacArm }; + case hash("mac-intel"): return { PlatformID::MacIntel }; + case hash("mac-arm"): return { PlatformID::MacArm }; + + case hash("ios"): return { PlatformID::iOS }; + + case hash("android"): return { PlatformID::Android32, PlatformID::Android64 }; + case hash("android32"): return { PlatformID::Android32 }; + case hash("android64"): return { PlatformID::Android64 }; + + // todo in v4: no linux + case hash("linux"): return { PlatformID::Linux }; + + default: return {}; + } +} + PlatformID PlatformID::from(std::string const& str) { return PlatformID::from(str.c_str()); }