From 54e9d8e9ddb0b0d29936b88a78d923ea8421e99f Mon Sep 17 00:00:00 2001 From: Grinch_ Date: Thu, 11 Mar 2021 03:10:59 +0600 Subject: [PATCH] Add update checker --- .vscode/c_cpp_properties.json | 1 + .vscode/settings.json | 5 ++++- CMakeLists.txt | 3 +++ src/CheatMenu.cpp | 11 +++++++++-- src/Menu.cpp | 15 ++++++++++++--- src/MenuInfo.h | 3 ++- src/Updater.cpp | 25 +++++++++++++++++++++++++ src/Updater.h | 19 +++++++++++++++++++ src/VehExtender.h | 11 +++++++---- src/pch.cpp | 2 ++ src/pch.h | 5 +++-- tests/Test.cpp | 35 +++++++---------------------------- 12 files changed, 94 insertions(+), 41 deletions(-) create mode 100644 src/Updater.cpp create mode 100644 src/Updater.h diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 2b62a23..67b1880 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -11,6 +11,7 @@ "${PLUGIN_SDK_DIR}/shared/game" ], "defines": [ + "CONSOLE=std::cout", "GTASA", "_DEBUG", "UNICODE", diff --git a/.vscode/settings.json b/.vscode/settings.json index 4af6456..f1cf608 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -76,7 +76,10 @@ "thread": "cpp", "trampoline.h": "c", "minhook.h": "c", - "*.rh": "cpp" + "*.rh": "cpp", + "bit": "cpp", + "stop_token": "cpp", + "iostream": "cpp" }, "C_Cpp.errorSquiggles": "Enabled", "C_Cpp.intelliSenseEngineFallback": "Enabled" diff --git a/CMakeLists.txt b/CMakeLists.txt index 7568bd4..7994f8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,8 @@ set(src_files "src/Teleport.h" "src/Ui.cpp" "src/Ui.h" + "src/Updater.h" + "src/Updater.cpp" "src/Util.cpp" "src/Util.h" "src/Vehicle.cpp" @@ -175,6 +177,7 @@ d3dx9 d3d11 d3dx11 XInput9_1_0 +urlmon Depend ) diff --git a/src/CheatMenu.cpp b/src/CheatMenu.cpp index 97ca17a..ba7081f 100644 --- a/src/CheatMenu.cpp +++ b/src/CheatMenu.cpp @@ -2,6 +2,7 @@ #include "CheatMenu.h" #include "MenuInfo.h" #include "Ui.h" +#include "Updater.h" void CheatMenu::DrawWindow() { @@ -184,7 +185,7 @@ void MenuThread(void* param) MessageBox(RsGlobal.ps->window, "SilentPatch isn't installed. Exiting CheatMenu.", "CheatMenu", MB_ICONERROR); return; } - + flog << "Starting...\nVersion: " MENU_TITLE "\nAuthor: Grinch_\nDiscord: " DISCORD_INVITE "\nMore Info: " GITHUB_LINK "\n" << std::endl; CFastman92limitAdjuster::Init(); CheatMenu *menu = new CheatMenu; @@ -194,6 +195,12 @@ void MenuThread(void* param) Sleep(100); if (KeyPressed(VK_LSHIFT) && KeyPressed(VK_BACK)) break; + + if (Updater::state == UPDATER_CHECKING) + { + Updater::CheckForUpdates(); + Updater::state = UPDATER_IDLE; + } } delete menu; @@ -212,7 +219,7 @@ void MenuThread(void* param) BOOL WINAPI DllMain(HINSTANCE hDllHandle, DWORD nReason, LPVOID Reserved) { if (nReason == DLL_PROCESS_ATTACH) - { + { uint gameVersion = GetGameVersion(); if (gameVersion == GAME_10US_HOODLUM || gameVersion == GAME_10US_COMPACT) CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)&MenuThread, NULL, NULL, NULL); diff --git a/src/Menu.cpp b/src/Menu.cpp index 189a8fd..053cafb 100644 --- a/src/Menu.cpp +++ b/src/Menu.cpp @@ -6,6 +6,7 @@ #include "Vehicle.h" #include "Ui.h" #include "Util.h" +#include "Updater.h" Menu::Menu() { @@ -433,12 +434,20 @@ void Menu::Draw() { ImGui::Spacing(); - if (ImGui::Button("Discord server", ImVec2(Ui::GetSize(2)))) + if (ImGui::Button("Check update", ImVec2(Ui::GetSize(3)))) + { + if (Updater::state == UPDATER_IDLE) + Updater::state = UPDATER_CHECKING; + } + + ImGui::SameLine(); + + if (ImGui::Button("Discord server", ImVec2(Ui::GetSize(3)))) ShellExecute(NULL, "open", DISCORD_INVITE, NULL, NULL, SW_SHOWNORMAL); ImGui::SameLine(); - if (ImGui::Button("GitHub repo", ImVec2(Ui::GetSize(2)))) + if (ImGui::Button("GitHub repo", ImVec2(Ui::GetSize(3)))) ShellExecute(NULL, "open", GITHUB_LINK, NULL, NULL, SW_SHOWNORMAL); ImGui::Spacing(); @@ -451,7 +460,7 @@ void Menu::Draw() ImGui::Text((std::string("Version: ") + MENU_VERSION).c_str()); ImGui::NextColumn(); - ImGui::Text(std::string("Imgui: " + std::string(ImGui::GetVersion())).c_str()); + ImGui::Text(std::string("ImGui: " + std::string(ImGui::GetVersion())).c_str()); ImGui::Text((std::string("Build: ") + BUILD_NUMBER).c_str()); ImGui::Columns(1); diff --git a/src/MenuInfo.h b/src/MenuInfo.h index 6e630dd..32a85e5 100644 --- a/src/MenuInfo.h +++ b/src/MenuInfo.h @@ -1,5 +1,6 @@ #pragma once #define MENU_NAME "Cheat Menu" -#define MENU_VERSION "2.6-beta" +#define MENU_VERSION_NUMBER "2.3" +#define MENU_VERSION MENU_VERSION_NUMBER"-beta" #define BUILD_NUMBER "20210308" #define MENU_TITLE MENU_NAME " v" MENU_VERSION "(" BUILD_NUMBER ")" \ No newline at end of file diff --git a/src/Updater.cpp b/src/Updater.cpp new file mode 100644 index 0000000..fc5b5cd --- /dev/null +++ b/src/Updater.cpp @@ -0,0 +1,25 @@ +#include "Updater.h" +#include "pch.h" +#include "MenuInfo.h" + +void Updater::CheckForUpdates() +{ + LPCSTR link = "https://api.github.com/repos/user-grinch/Cheat-Menu/tags"; + LPCSTR path = PLUGIN_PATH((char*)"CheatMenu\\json\\versioninfo.json"); + HRESULT res = URLDownloadToFile(NULL, link, path, 0, NULL); + + if (res == E_OUTOFMEMORY || res == INET_E_DOWNLOAD_FAILURE) + { + CHud::SetHelpMessage("Failed to check for updates",false,false,false); + return; + } + CJson verinfo = CJson("versioninfo"); + + // fetch the version number + std::string latest_version = verinfo.data.items().begin().value()["name"].get(); + + if (latest_version > MENU_VERSION_NUMBER) + CHud::SetHelpMessage("Update found",false,false,false); + else + CHud::SetHelpMessage("No update found.",false,false,false); +} \ No newline at end of file diff --git a/src/Updater.h b/src/Updater.h new file mode 100644 index 0000000..06b4110 --- /dev/null +++ b/src/Updater.h @@ -0,0 +1,19 @@ +#pragma once + +enum UPDATER_STATE +{ + UPDATER_IDLE, + UPDATER_CHECKING, + UPDATER_DOWNLOADING, + UPDATER_DOWNLOADED, + UPDATER_INSTALLING, + UPDATER_NO_UPDATE, + UPDATER_UPDATE_FOUND +}; + +class Updater +{ +public: + inline static UPDATER_STATE state = UPDATER_IDLE; + static void CheckForUpdates(); +}; diff --git a/src/VehExtender.h b/src/VehExtender.h index 5307427..bbd1c4e 100644 --- a/src/VehExtender.h +++ b/src/VehExtender.h @@ -11,25 +11,28 @@ template class VehExtender { private: - inline static std::vector> data; + inline static std::vector> data{}; public: static void RemoveVehEntry(CVehicle *pVeh) { - for (auto it = data.begin(); it < data.end(); ++it) + for (auto it = data.begin(); it != data.end(); it++) { if (it->first == pVeh) + { data.erase(it); + break; + } } } VehExtender() { - plugin::Events::vehicleCtorEvent.after += RemoveVehEntry; + plugin::Events::vehicleDtorEvent.before += RemoveVehEntry; } ~VehExtender() { - plugin::Events::vehicleCtorEvent.after -= RemoveVehEntry; + plugin::Events::vehicleDtorEvent.before -= RemoveVehEntry; } VehExtender(const VehExtender&) = delete; diff --git a/src/pch.cpp b/src/pch.cpp index 1d9f38c..15c3143 100644 --- a/src/pch.cpp +++ b/src/pch.cpp @@ -1 +1,3 @@ #include "pch.h" +std::ofstream flog = std::ofstream("CheatMenu.log"); +CJson config = CJson("config"); diff --git a/src/pch.h b/src/pch.h index 17fc879..dce5955 100644 --- a/src/pch.h +++ b/src/pch.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -87,8 +88,8 @@ struct Globals inline static void* device = nullptr; }; -inline static std::ofstream flog = std::ofstream("CheatMenu.log"); -inline static CJson config = CJson("config"); +extern std::ofstream flog; +extern CJson config; struct TextureStructure { diff --git a/tests/Test.cpp b/tests/Test.cpp index d8328d3..8b647cd 100644 --- a/tests/Test.cpp +++ b/tests/Test.cpp @@ -1,31 +1,10 @@ -#include "plugin.h" -#include "CHud.h" +#include -using namespace plugin; - -void DoStuff(); - -class Test +BOOL WINAPI DllMain(HINSTANCE hDllHandle, DWORD nReason, LPVOID Reserved) { -public: - - Test() - { - Events::processScriptsEvent += DoStuff; - } - ~Test() - { - Events::processScriptsEvent -= DoStuff; - } -} test; - -void DoStuff() -{ - CHud::SetMessage((char*)"Test"); - - if (KeyPressed(VK_TAB)) - { - test.~Test(); - } -} + if (nReason == DLL_PROCESS_ATTACH) + MessageBox(NULL, "SilentPatch isn't installed. Exiting CheatMenu.", "CheatMenu", MB_ICONERROR); + + return TRUE; +} \ No newline at end of file