From 322077ce936614f5eda4c4d8acf06475030117bb Mon Sep 17 00:00:00 2001 From: IMXNOOBX Date: Sat, 30 Sep 2023 16:09:38 +0200 Subject: [PATCH] Fixed brain issue with the auto updater --- memory-external/classes/auto_updater.cpp | 58 ++++++++++++++++-------- memory-external/classes/auto_updater.hpp | 1 + memory-external/classes/config.cpp | 7 +-- memory-external/classes/config.hpp | 1 + 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/memory-external/classes/auto_updater.cpp b/memory-external/classes/auto_updater.cpp index b35da06..f6b8577 100644 --- a/memory-external/classes/auto_updater.cpp +++ b/memory-external/classes/auto_updater.cpp @@ -20,37 +20,57 @@ namespace updater { auto currentTime = std::chrono::system_clock::now(); - fs::file_time_type lastModifiedTime = fs::last_write_time("offsets.json"); - auto lastModifiedClockTime = std::chrono::time_point_cast( - lastModifiedTime - fs::file_time_type::clock::now() + std::chrono::system_clock::now()); - std::chrono::system_clock::time_point commitTimePoint = std::chrono::system_clock::from_time_t(std::mktime(&commit_date)); - // Check if the local file is older than the last GitHub commit - if (lastModifiedClockTime < commitTimePoint) { - std::cout << "[updater] Local file is older than the last GitHub commit." << std::endl; - if (prompt_update) { - std::cout << "[updater] Do you want to download the latest offsets? (y/n): "; - char response; - std::cin >> response; - if (response == 'Y' || response == 'y') { - if (download_file(raw_updated_offets.c_str(), "offsets.json")) { - std::cout << "[updater] Successfully downloaded latest offsets.json file\n" << std::endl; - return true; - } - else { - std::cout << "[updater] Error: Failed to download file, try downloading manually from " << raw_updated_offets << "\n" << std::endl; + if (file_good("offsets.json")) { + fs::file_time_type lastModifiedTime = fs::last_write_time("offsets.json"); + auto lastModifiedClockTime = std::chrono::time_point_cast( + lastModifiedTime - fs::file_time_type::clock::now() + std::chrono::system_clock::now()); + + // Check if the local file is older than the last GitHub commit + if (lastModifiedClockTime < commitTimePoint) { + std::cout << "[updater] Local file is older than the last GitHub commit." << std::endl; + if (prompt_update) { + std::cout << "[updater] Do you want to download the latest offsets? (y/n): "; + char response; + std::cin >> response; + if (response == 'Y' || response == 'y') { + if (download_file(raw_updated_offets.c_str(), "offsets.json")) { + std::cout << "[updater] Successfully downloaded latest offsets.json file\n" << std::endl; + return true; + } + else { + std::cout << "[updater] Error: Failed to download file, try downloading manually from " << raw_updated_offets << "\n" << std::endl; + } } } } + else { + std::cout << "[updater] Local file is up to date.\n" << std::endl; + } } else { - std::cout << "[updater] Local file is up to date.\n" << std::endl; + std::cout << "[updater] Do you want to download the latest offsets? (y/n): "; + char response; + std::cin >> response; + if (response == 'Y' || response == 'y') { + if (download_file(raw_updated_offets.c_str(), "offsets.json")) { + std::cout << "[updater] Successfully downloaded latest offsets.json file\n" << std::endl; + return true; + } + else { + std::cout << "[updater] Error: Failed to download file, try downloading manually from " << raw_updated_offets << "\n" << std::endl; + } + } } return false; } + bool file_good(const std::string& name) { + std::ifstream f(name.c_str()); + return f.good(); + } bool get_last_commit_date(json& commit) { HINTERNET hInternet, hConnect; diff --git a/memory-external/classes/auto_updater.hpp b/memory-external/classes/auto_updater.hpp index b019d1e..7c7af31 100644 --- a/memory-external/classes/auto_updater.hpp +++ b/memory-external/classes/auto_updater.hpp @@ -19,6 +19,7 @@ namespace updater { bool check_and_update(bool prompt_update); bool get_last_commit_date(json& commit); bool download_file(const char* url, const char* localPath); + bool file_good(const std::string& name); inline std::string github_repo_api = "https://api.github.com/repos/IMXNOOBX/cs2-external-esp/commits"; inline std::string raw_updated_offets = "https://github.com/IMXNOOBX/cs2-external-esp/raw/main/offsets/offsets.json"; diff --git a/memory-external/classes/config.cpp b/memory-external/classes/config.cpp index e191ee9..970bc8c 100644 --- a/memory-external/classes/config.cpp +++ b/memory-external/classes/config.cpp @@ -4,13 +4,8 @@ namespace config { using json = nlohmann::json; const std::string file_path = "offsets.json"; - bool file_exsits(const std::string& name) { - std::ifstream f(name.c_str()); - return f.good(); - } - bool read() { - if (!file_exsits(file_path)) { + if (!updater::file_good(file_path)) { save(); return false; } diff --git a/memory-external/classes/config.hpp b/memory-external/classes/config.hpp index 8374006..24d5b91 100644 --- a/memory-external/classes/config.hpp +++ b/memory-external/classes/config.hpp @@ -1,6 +1,7 @@ #pragma once #include #include "json.hpp" +#include "auto_updater.hpp" namespace config { extern bool read();