Fixed brain issue with the auto updater

This commit is contained in:
IMXNOOBX
2023-09-30 16:09:38 +02:00
parent e24bb1edb5
commit 322077ce93
4 changed files with 42 additions and 25 deletions
+39 -19
View File
@@ -20,37 +20,57 @@ namespace updater {
auto currentTime = std::chrono::system_clock::now(); 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<std::chrono::system_clock::duration>(
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)); 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 (file_good("offsets.json")) {
if (lastModifiedClockTime < commitTimePoint) { fs::file_time_type lastModifiedTime = fs::last_write_time("offsets.json");
std::cout << "[updater] Local file is older than the last GitHub commit." << std::endl; auto lastModifiedClockTime = std::chrono::time_point_cast<std::chrono::system_clock::duration>(
if (prompt_update) { lastModifiedTime - fs::file_time_type::clock::now() + std::chrono::system_clock::now());
std::cout << "[updater] Do you want to download the latest offsets? (y/n): ";
char response; // Check if the local file is older than the last GitHub commit
std::cin >> response; if (lastModifiedClockTime < commitTimePoint) {
if (response == 'Y' || response == 'y') { std::cout << "[updater] Local file is older than the last GitHub commit." << std::endl;
if (download_file(raw_updated_offets.c_str(), "offsets.json")) { if (prompt_update) {
std::cout << "[updater] Successfully downloaded latest offsets.json file\n" << std::endl; std::cout << "[updater] Do you want to download the latest offsets? (y/n): ";
return true; char response;
} std::cin >> response;
else { if (response == 'Y' || response == 'y') {
std::cout << "[updater] Error: Failed to download file, try downloading manually from " << raw_updated_offets << "\n" << std::endl; 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 { 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; 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) { bool get_last_commit_date(json& commit) {
HINTERNET hInternet, hConnect; HINTERNET hInternet, hConnect;
+1
View File
@@ -19,6 +19,7 @@ namespace updater {
bool check_and_update(bool prompt_update); bool check_and_update(bool prompt_update);
bool get_last_commit_date(json& commit); bool get_last_commit_date(json& commit);
bool download_file(const char* url, const char* localPath); 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 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"; inline std::string raw_updated_offets = "https://github.com/IMXNOOBX/cs2-external-esp/raw/main/offsets/offsets.json";
+1 -6
View File
@@ -4,13 +4,8 @@ namespace config {
using json = nlohmann::json; using json = nlohmann::json;
const std::string file_path = "offsets.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() { bool read() {
if (!file_exsits(file_path)) { if (!updater::file_good(file_path)) {
save(); save();
return false; return false;
} }
+1
View File
@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <fstream> #include <fstream>
#include "json.hpp" #include "json.hpp"
#include "auto_updater.hpp"
namespace config { namespace config {
extern bool read(); extern bool read();