From bfd1e6940e945b86bdb6cc2105815dcfebbf8001 Mon Sep 17 00:00:00 2001 From: Explodingbill Date: Mon, 26 Feb 2024 19:48:43 +1100 Subject: [PATCH] ggez --- src/Client/AndroidUI.h | 36 +++++++++++++++++++++++++++++++++++- src/Client/Replay.cpp | 10 ++++++---- src/Client/Window.h | 2 +- src/Hacks/Speedhack.cpp | 36 +++++++++++++++++++++++++++++++++++- 4 files changed, 77 insertions(+), 7 deletions(-) diff --git a/src/Client/AndroidUI.h b/src/Client/AndroidUI.h index 7056932..96ec505 100644 --- a/src/Client/AndroidUI.h +++ b/src/Client/AndroidUI.h @@ -55,6 +55,40 @@ class AndroidUI : public cocos2d::CCLayerColor { this->removeFromParent(); } + template + Result anumFromString(std::string_view const str, int base = 10) { + if constexpr (std::is_floating_point_v + #if defined(__cpp_lib_to_chars) + && false + #endif + ) { + Num val; + char* strEnd; + errno = 0; + if (std::setlocale(LC_NUMERIC, "C")) { + if constexpr (std::is_same_v) val = std::strtof(str.data(), &strEnd); + else if constexpr (std::is_same_v) val = std::strtod(str.data(), &strEnd); + else if constexpr (std::is_same_v) val = std::strtold(str.data(), &strEnd); + if (errno == ERANGE) return Err("Number is too large to fit"); + else if (strEnd == str.data()) return Err("String is not a number"); + else return Ok(val); + } + else return Err("Failed to set locale"); + } + else { + Num result; + std::from_chars_result res; + if constexpr (std::is_floating_point_v) res = std::from_chars(str.data(), str.data() + str.size(), result); + else res = std::from_chars(str.data(), str.data() + str.size(), result, base); + + auto [_, ec] = res; + if (ec == std::errc()) return Ok(result); + else if (ec == std::errc::invalid_argument) return Err("String is not a number"); + else if (ec == std::errc::result_out_of_range) return Err("Number is too large to fit"); + else return Err("Unknown error"); + } + } + CCAction* getEnterAction(CCNode* panel) { @@ -64,7 +98,7 @@ class AndroidUI : public cocos2d::CCLayerColor { { if (Client::GetModuleEnabled("speedhack-enabled")) { - auto x = numFromString(SpeedhackTop::instance->text); + auto x = anumFromString(SpeedhackTop::instance->text); if (x.isOk()) { diff --git a/src/Client/Replay.cpp b/src/Client/Replay.cpp index 0563cd6..b38ad9c 100644 --- a/src/Client/Replay.cpp +++ b/src/Client/Replay.cpp @@ -30,7 +30,7 @@ float dta; } };*/ -class $modify (CheckpointObjectExt, CheckpointObject) +/*class $modify (CheckpointObjectExt, CheckpointObject) { float dt; int frame; @@ -45,7 +45,7 @@ class $modify (CheckpointObjectExt, CheckpointObject) return true; } -};/* +};*/ class $modify (PlayLayer) { @@ -92,6 +92,8 @@ class $modify (PlayLayer) { PlayLayer::loadFromCheckpoint(p0); + /* + GJReplayManager::dt = as(p0)->m_fields->dt; GJReplayManager::frame = as(p0)->m_fields->frame; @@ -108,7 +110,7 @@ class $modify (PlayLayer) } GJReplayManager::replay.inputs = myvec; - } + }*/ } }; @@ -124,4 +126,4 @@ class $modify(GJBaseGameLayer) { if (GJReplayManager::recording) GJReplayManager::replay.inputs.push_back(MyInput(m_gameState.m_unk1f8, button, !player1, push, plr->m_position.x, plr->m_position.y, GJReplayManager::dt, plr->m_platformerXVelocity, plr->m_yVelocity, plr->getRotation())); } -};*/ \ No newline at end of file +}; \ No newline at end of file diff --git a/src/Client/Window.h b/src/Client/Window.h index f4b990c..9ac1e67 100644 --- a/src/Client/Window.h +++ b/src/Client/Window.h @@ -244,7 +244,7 @@ class Speedhack : public Window//, public TextInputDelegate Num val; char* strEnd; errno = 0; - if (std::setlocale(LC_NUMERIC, /*"en_US.utf8"*/"C")) { + if (std::setlocale(LC_NUMERIC, "C")) { if constexpr (std::is_same_v) val = std::strtof(str.data(), &strEnd); else if constexpr (std::is_same_v) val = std::strtod(str.data(), &strEnd); else if constexpr (std::is_same_v) val = std::strtold(str.data(), &strEnd); diff --git a/src/Hacks/Speedhack.cpp b/src/Hacks/Speedhack.cpp index 74be587..27d2118 100644 --- a/src/Hacks/Speedhack.cpp +++ b/src/Hacks/Speedhack.cpp @@ -6,6 +6,40 @@ using namespace geode::prelude; FMOD::ChannelGroup* masterGroup; +template +Result anumFromString(std::string_view const str, int base = 10) { + if constexpr (std::is_floating_point_v + #if defined(__cpp_lib_to_chars) + && false + #endif + ) { + Num val; + char* strEnd; + errno = 0; + if (std::setlocale(LC_NUMERIC, "C")) { + if constexpr (std::is_same_v) val = std::strtof(str.data(), &strEnd); + else if constexpr (std::is_same_v) val = std::strtod(str.data(), &strEnd); + else if constexpr (std::is_same_v) val = std::strtold(str.data(), &strEnd); + if (errno == ERANGE) return Err("Number is too large to fit"); + else if (strEnd == str.data()) return Err("String is not a number"); + else return Ok(val); + } + else return Err("Failed to set locale"); + } + else { + Num result; + std::from_chars_result res; + if constexpr (std::is_floating_point_v) res = std::from_chars(str.data(), str.data() + str.size(), result); + else res = std::from_chars(str.data(), str.data() + str.size(), result, base); + + auto [_, ec] = res; + if (ec == std::errc()) return Ok(result); + else if (ec == std::errc::invalid_argument) return Err("String is not a number"); + else if (ec == std::errc::result_out_of_range) return Err("Number is too large to fit"); + else return Err("Unknown error"); + } +} + class $modify (CCScheduler) { void update(float dt) @@ -24,7 +58,7 @@ class $modify (CCScheduler) { float v = 1.0f; - auto x = numFromString(SpeedhackTop::instance->text); + auto x = anumFromString(SpeedhackTop::instance->text); if (x.isOk()) {