Changes to updater

This commit is contained in:
Grinch_ 2021-03-16 13:12:59 +06:00
parent c0bb8d3e71
commit c459758abc
16 changed files with 167 additions and 131 deletions

View File

@ -22,7 +22,6 @@ private:
protected: protected:
Animation(); Animation();
~Animation() {};
public: public:
static void Draw(); static void Draw();

View File

@ -22,33 +22,9 @@ void CheatMenu::DrawWindow()
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(ImGui::GetWindowWidth() / 85, ImGui::GetWindowHeight() / 200)); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(ImGui::GetWindowWidth() / 85, ImGui::GetWindowHeight() / 200));
if (Updater::state == UPDATER_UPDATE_FOUND) if (Updater::state == UPDATER_UPDATE_FOUND)
{ Updater::ShowUpdateScreen();
ImGui::Button("New version is available",Ui::GetSize()); else
Ui::DrawHeaders(header);
if (ImGui::Button("Changelog",Ui::GetSize(3)))
ShellExecute(NULL, "open",
std::string("https://github.com/user-grinch/Cheat-Menu/releases/tag/" +
Updater::latest_version).c_str(), NULL, NULL, SW_SHOWNORMAL);
ImGui::SameLine();
if (ImGui::Button("Download",Ui::GetSize(3)))
Updater::state = UPDATER_DOWNLOADING;
ImGui::SameLine();
if (ImGui::Button("Hide",Ui::GetSize(3)))
Updater::state = UPDATER_IDLE;
}
if (Updater::state == UPDATER_DOWNLOADING)
ImGui::Button("Downloading update...",Ui::GetSize());
if (Updater::state == UPDATER_DOWNLOADED)
{
if (ImGui::Button("Update downloaded. Click to install.",Ui::GetSize()))
Updater::state = UPDATER_INSTALLING;
}
Ui::DrawHeaders(header);
Globals::menu_size = ImGui::GetWindowSize(); Globals::menu_size = ImGui::GetWindowSize();
config.SetValue("window.sizeX", Globals::menu_size.x); config.SetValue("window.sizeX", Globals::menu_size.x);
@ -216,44 +192,47 @@ void MenuThread(void* param)
flog << "Starting...\nVersion: " MENU_TITLE "\nAuthor: Grinch_\nDiscord: " DISCORD_INVITE "\nMore Info: " GITHUB_LINK "\n" << std::endl; flog << "Starting...\nVersion: " MENU_TITLE "\nAuthor: Grinch_\nDiscord: " DISCORD_INVITE "\nMore Info: " GITHUB_LINK "\n" << std::endl;
CFastman92limitAdjuster::Init(); CFastman92limitAdjuster::Init();
CheatMenu *menu = new CheatMenu; CheatMenu *menu = new CheatMenu;
Updater::CheckForUpdate();
while (true) while (true)
{ {
Sleep(100); Sleep(5000);
if (KeyPressed(VK_LSHIFT) && KeyPressed(VK_BACK)) // if (KeyPressed(VK_LSHIFT) && KeyPressed(VK_BACK))
break; // break;
if (Updater::state == UPDATER_CHECKING) if (Updater::state == UPDATER_CHECKING)
Updater::CheckForUpdate(); Updater::CheckForUpdate();
if (Updater::state == UPDATER_DOWNLOADING) // if (Updater::state == UPDATER_DOWNLOADING)
Updater::DownloadUpdate(); // Updater::DownloadUpdate();
if (Updater::state == UPDATER_INSTALLING) // if (Updater::state == UPDATER_INSTALLING)
{ // {
Updater::InstallUpdate(); // Updater::InstallUpdate();
break; // break;
} // }
} }
delete menu; // Globals::menu_closing = true;
// Sleep(500);
// delete menu;
// reset mouse patches // // reset mouse patches
patch::SetUChar(0x6194A0, 0xE9); // patch::SetUChar(0x6194A0, 0xE9);
patch::SetUChar(0x746ED0, 0xA1); // patch::SetUChar(0x746ED0, 0xA1);
patch::SetRaw(0x53F41F, (void*)"\x85\xC0\x0F\x8C", 4); // patch::SetRaw(0x53F41F, (void*)"\x85\xC0\x0F\x8C", 4);
if (Updater::state == UPDATER_INSTALLING) // if (Updater::state == UPDATER_INSTALLING)
{ // {
CHud::SetHelpMessage("Install complete, restarting menu!",false,false,false); // CHud::SetHelpMessage("Install complete, restarting menu!",false,false,false);
Updater::FinishUpdate(); // Updater::FinishUpdate();
} // }
else // else
{ // {
CHud::SetHelpMessage("CheatMenu unloaded",false,false,false); // CHud::SetHelpMessage("CheatMenu unloaded",false,false,false);
FreeLibraryAndExitThread(GetModuleHandle("CheatMenu.asi"),0); // FreeLibraryAndExitThread(GetModuleHandle("CheatMenu.asi"),0);
FreeLibraryAndExitThread(GetModuleHandle("CheatMenuNew.asi"),0); // FreeLibraryAndExitThread(GetModuleHandle("CheatMenuNew.asi"),0);
} // }
} }
BOOL WINAPI DllMain(HINSTANCE hDllHandle, DWORD nReason, LPVOID Reserved) BOOL WINAPI DllMain(HINSTANCE hDllHandle, DWORD nReason, LPVOID Reserved)

View File

@ -35,7 +35,7 @@ HRESULT Hook::Reset(IDirect3DDevice9* pDevice, D3DPRESENT_PARAMETERS* pPresentat
void Hook::Present(void* ptr) void Hook::Present(void* ptr)
{ {
if (!ImGui::GetCurrentContext()) if (!ImGui::GetCurrentContext() || Globals::menu_closing)
return; return;
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include "json.hpp" #include "../deps/json.hpp"
class CJson class CJson
{ {

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#define MENU_NAME "Cheat Menu" #define MENU_NAME "Cheat Menu"
#define MENU_VERSION_NUMBER "2.4" #define MENU_VERSION_NUMBER "2.3"
#define MENU_VERSION MENU_VERSION_NUMBER"-beta" #define MENU_VERSION MENU_VERSION_NUMBER"-beta"
#define BUILD_NUMBER "20210311" #define BUILD_NUMBER "20210311"
#define MENU_TITLE MENU_NAME " v" MENU_VERSION "(" BUILD_NUMBER ")" #define MENU_TITLE MENU_NAME " v" MENU_VERSION "(" BUILD_NUMBER ")"

View File

@ -46,7 +46,12 @@ Neon::Neon()
Neon::~Neon() Neon::~Neon()
{ {
Events::vehicleRenderEvent -= RenderEvent; Events::vehicleRenderEvent -= RenderEvent;
RwTextureDestroy(neon_texture);
if (neon_texture)
{
RwTextureDestroy(neon_texture);
neon_texture = nullptr;
}
} }
bool Neon::IsNeonInstalled(CVehicle* pVeh) bool Neon::IsNeonInstalled(CVehicle* pVeh)

View File

@ -20,6 +20,7 @@ Ped::Ped()
Ped::~Ped() Ped::~Ped()
{ {
Util::ReleaseTextures(peds_vec);
for (CPed* ped : spawn_ped::list) for (CPed* ped : spawn_ped::list)
{ {
CWorld::Remove(ped); CWorld::Remove(ped);

View File

@ -116,6 +116,11 @@ Player::Player()
}; };
} }
Player::~Player()
{
Util::ReleaseTextures(clothes_vec);
}
void Player::ChangePlayerCloth(std::string& name) void Player::ChangePlayerCloth(std::string& name)
{ {
std::stringstream ss(name); std::stringstream ss(name);

View File

@ -26,6 +26,7 @@ private:
public: public:
Player(); Player();
~Player();
static void ChangePlayerCloth(std::string& model); static void ChangePlayerCloth(std::string& model);
static void ChangePlayerModel(std::string& model); static void ChangePlayerModel(std::string& model);
static void Draw(); static void Draw();

View File

@ -1,5 +1,6 @@
#include "Updater.h" #include "Updater.h"
#include "pch.h" #include "pch.h"
#include "Ui.h"
#include "MenuInfo.h" #include "MenuInfo.h"
#include "..\deps\zip\zip.h" #include "..\deps\zip\zip.h"
@ -31,59 +32,92 @@ void Updater::CheckForUpdate()
} }
} }
void Updater::DownloadUpdate() // void Updater::DownloadUpdate()
// {
// std::string link = "https://github.com/user-grinch/Cheat-Menu/releases/download/" + latest_version + "/CheatMenu.zip";
// char* path = PLUGIN_PATH((char*)"update.zip");
// HRESULT res = S_OK;//URLDownloadToFile(NULL, link.c_str(), path, 0, NULL);
// if (res == E_OUTOFMEMORY || res == INET_E_DOWNLOAD_FAILURE)
// {
// CHud::SetHelpMessage("Failed download update",false,false,false);
// state = UPDATER_IDLE;
// return;
// }
// CHud::SetHelpMessage("Update downloaded successfully.",false,false,false);
// state = UPDATER_DOWNLOADED;
// }
// void Updater::InstallUpdate()
// {
// CHud::SetHelpMessage("Update in progress. Do not pause/close the game.",false,false,false);
// Sleep(100);
// // delete the old menu
// std::remove(PLUGIN_PATH((char*)"CheatMenu.asi.old"));
// std::string new_name = PLUGIN_PATH((char*)"CheatMenu.asi.old");
// std::string old_name = PLUGIN_PATH((char*)"CheatMenu.asi");
// std::rename(old_name.c_str(),new_name.c_str());
// fs::remove_all(PLUGIN_PATH((char*)"CheatMenu"));
// std::string dir = PLUGIN_PATH((char*)"");
// std::string file = PLUGIN_PATH((char*)"update.zip");
// zip_extract(file.c_str(),dir.c_str(),NULL,NULL);
// }
// static bool menu_loaded = false;
// static void LoadUpdatedMenu()
// {
// std::string new_name = PLUGIN_PATH((char*)"CheatMenuNew.asi");
// LoadLibrary(new_name.c_str());
// menu_loaded = true;
// }
// void Updater::FinishUpdate()
// {
// // kinda hacky, can't update twice on same instance ( should be unlikely anyways?)
// std::string new_name = PLUGIN_PATH((char*)"CheatMenuNew.asi");
// std::string old_name = PLUGIN_PATH((char*)"CheatMenu.asi");
// std::rename(old_name.c_str(),new_name.c_str());
// Events::processScriptsEvent += LoadUpdatedMenu;
// while (!menu_loaded)
// Sleep(1000);
// Events::processScriptsEvent -= LoadUpdatedMenu;
// std::rename(new_name.c_str(),old_name.c_str());
// }
void Updater::ShowUpdateScreen()
{ {
std::string link = "https://github.com/user-grinch/Cheat-Menu/releases/download/" + latest_version + "/CheatMenu.zip"; ImGui::Dummy(ImVec2(0,20));
char* path = PLUGIN_PATH((char*)"update.zip"); Ui::CenterdText("A new version of the mod is available.");
HRESULT res = URLDownloadToFile(NULL, link.c_str(), path, 0, NULL); Ui::CenterdText(std::string("Current version: ") + MENU_VERSION);
Ui::CenterdText("Latest version: " + Updater::latest_version);
ImGui::Dummy(ImVec2(0,10));
ImGui::TextWrapped("In order to keep using the menu, you need to update to the latest version."
" This is to ensure everything is using the most up-to-date version.");
ImGui::Dummy(ImVec2(0,10));
ImGui::TextWrapped("To know what changes are made or to download, click on the \"Download page\" button."
" Follow the instructions there. If you're still having issues, let me know on discord.");
if (res == E_OUTOFMEMORY || res == INET_E_DOWNLOAD_FAILURE) ImGui::Dummy(ImVec2(0,5));
{ if (ImGui::Button("Discord server", ImVec2(Ui::GetSize(2))))
CHud::SetHelpMessage("Failed download update",false,false,false); ShellExecute(NULL, "open", DISCORD_INVITE, NULL, NULL, SW_SHOWNORMAL);
state = UPDATER_IDLE;
return; ImGui::SameLine();
}
CHud::SetHelpMessage("Update downloaded successfully.",false,false,false); if (ImGui::Button("Download page",Ui::GetSize(2)))
state = UPDATER_DOWNLOADED; ShellExecute(NULL, "open", std::string("https://github.com/user-grinch/Cheat-Menu/releases/tag/" +
} Updater::latest_version).c_str(), NULL, NULL, SW_SHOWNORMAL);
void Updater::InstallUpdate() // if (Updater::state == UPDATER_DOWNLOADING)
{ // ImGui::Button("Downloading update...",Ui::GetSize());
CHud::SetHelpMessage("Updating in progress. Do not pause/close the game.",false,false,false);
Sleep(100); // if (Updater::state == UPDATER_DOWNLOADED)
// delete the old menu // {
std::remove(PLUGIN_PATH((char*)"CheatMenu.asi.old")); // if (ImGui::Button("Update downloaded. Click to install.",Ui::GetSize()))
std::string new_name = PLUGIN_PATH((char*)"CheatMenu.asi.old"); // Updater::state = UPDATER_INSTALLING;
std::string old_name = PLUGIN_PATH((char*)"CheatMenu.asi"); // }
std::rename(old_name.c_str(),new_name.c_str());
fs::remove_all(PLUGIN_PATH((char*)"CheatMenu"));
std::string dir = PLUGIN_PATH((char*)"");
std::string file = PLUGIN_PATH((char*)"update.zip");
zip_extract(file.c_str(),dir.c_str(),NULL,NULL);
}
static bool menu_loaded = false;
static void LoadUpdatedMenu()
{
std::string new_name = PLUGIN_PATH((char*)"CheatMenuNew.asi");
LoadLibrary(new_name.c_str());
menu_loaded = true;
}
void Updater::FinishUpdate()
{
// kinda hacky
std::string new_name = PLUGIN_PATH((char*)"CheatMenuNew.asi");
std::string old_name = PLUGIN_PATH((char*)"CheatMenu.asi");
std::rename(old_name.c_str(),new_name.c_str());
Events::processScriptsEvent += LoadUpdatedMenu;
while (!menu_loaded)
Sleep(1000);
Events::processScriptsEvent -= LoadUpdatedMenu;
std::rename(new_name.c_str(),old_name.c_str());
} }

View File

@ -15,9 +15,10 @@ class Updater
public: public:
inline static UPDATER_STATE state = UPDATER_IDLE; inline static UPDATER_STATE state = UPDATER_IDLE;
inline static std::string latest_version = ""; inline static std::string latest_version = "";
// TODO: fix memory leak
static void CheckForUpdate(); static void CheckForUpdate();
static void DownloadUpdate(); // static void DownloadUpdate();
static void InstallUpdate(); // static void InstallUpdate();
static void FinishUpdate(); // static void FinishUpdate();
static void ShowUpdateScreen();
}; };

View File

@ -1,6 +1,6 @@
#include "pch.h" #include "pch.h"
#include "Util.h" #include "Util.h"
#include "imgui/stb_image.h" #include "../deps/imgui/stb_image.h"
void Util::ClearCharTasksVehCheck(CPed* ped) void Util::ClearCharTasksVehCheck(CPed* ped)
{ {
@ -23,6 +23,20 @@ void Util::ClearCharTasksVehCheck(CPed* ped)
} }
} }
void Util::ReleaseTextures(std::vector<std::unique_ptr<TextureStructure>> &store_vec)
{
// for (auto &it : store_vec)
// {
// if (Globals::renderer == Render_DirectX9)
// {
// reinterpret_cast<IDirect3DTexture9*>(it->texture)->Release();
// it->texture = nullptr;
// }
// else
// reinterpret_cast<ID3D11ShaderResourceView*>(it->texture)->Release();
// }
}
void Util::LoadTexturesInDirRecursive(const char* path, const char* file_ext, std::vector<std::string>& category_vec, std::vector<std::unique_ptr<TextureStructure>>& store_vec) void Util::LoadTexturesInDirRecursive(const char* path, const char* file_ext, std::vector<std::string>& category_vec, std::vector<std::unique_ptr<TextureStructure>>& store_vec)
{ {
std::string folder = ""; std::string folder = "";
@ -32,10 +46,10 @@ void Util::LoadTexturesInDirRecursive(const char* path, const char* file_ext, st
{ {
store_vec.push_back(std::make_unique<TextureStructure>()); store_vec.push_back(std::make_unique<TextureStructure>());
HRESULT hr = -1; HRESULT hr = -1;
if (Globals::renderer == Render_DirectX9) if (Globals::renderer == Render_DirectX9)
hr = D3DXCreateTextureFromFileA(GetD3DDevice(), p.path().string().c_str(), reinterpret_cast<PDIRECT3DTEXTURE9*>(&store_vec.back().get()->texture)); hr = D3DXCreateTextureFromFileA(GetD3DDevice(), p.path().string().c_str(), reinterpret_cast<PDIRECT3DTEXTURE9*>(&store_vec.back().get()->texture));
else
if (Globals::renderer == Render_DirectX11)
{ {
if (LoadTextureFromFileDx11(p.path().string().c_str(), reinterpret_cast<ID3D11ShaderResourceView**>(&store_vec.back().get()->texture))) if (LoadTextureFromFileDx11(p.path().string().c_str(), reinterpret_cast<ID3D11ShaderResourceView**>(&store_vec.back().get()->texture)))
hr = S_OK; hr = S_OK;
@ -237,10 +251,3 @@ RwTexture* Util::LoadTextureFromPngFile(fs::path path)
path.stem().string().copy(texture->name, sizeof(texture->name) - 1); path.stem().string().copy(texture->name, sizeof(texture->name) - 1);
return texture; return texture;
} }
void Util::UnloadTexture(RwTexture* texture)
{
if (!texture)
return;
RwTextureDestroy(texture);
}

View File

@ -11,10 +11,10 @@ public:
static CVehicle *GetClosestVehicle(); static CVehicle *GetClosestVehicle();
static int GetLargestGangInZone(); static int GetLargestGangInZone();
static void LoadTexturesInDirRecursive(const char * path, const char * file_ext, std::vector<std::string>& category_vec, std::vector<std::unique_ptr<TextureStructure>> &store_vec); static void LoadTexturesInDirRecursive(const char * path, const char * file_ext, std::vector<std::string>& category_vec, std::vector<std::unique_ptr<TextureStructure>> &store_vec);
static void ReleaseTextures(std::vector<std::unique_ptr<TextureStructure>> &store_vec);
static bool IsOnMission(); static bool IsOnMission();
static std::string GetLocationName(CVector *pos); static std::string GetLocationName(CVector *pos);
static void RainbowValues(int &r, int&g, int &b, float speed); static void RainbowValues(int &r, int&g, int &b, float speed);
static RwTexture* LoadTextureFromPngFile(fs::path path); static RwTexture* LoadTextureFromPngFile(fs::path path);
static void UnloadTexture(RwTexture* texture9);
}; };

View File

@ -152,6 +152,13 @@ Vehicle::Vehicle()
}; };
} }
Vehicle::~Vehicle()
{
Util::ReleaseTextures(spawner::image_vec);
Util::ReleaseTextures(tune::image_vec);
Util::ReleaseTextures(texture9::image_vec);
}
void Vehicle::AddComponent(const std::string& component, const bool display_message) void Vehicle::AddComponent(const std::string& component, const bool display_message)
{ {
try { try {
@ -503,11 +510,6 @@ int Vehicle::GetModelFromName(const char* name)
return 0; return 0;
} }
Vehicle::~Vehicle()
{
}
void Vehicle::GenerateHandlingDataFile(int phandling) void Vehicle::GenerateHandlingDataFile(int phandling)
{ {
FILE* fp = fopen("handling.txt", "w"); FILE* fp = fopen("handling.txt", "w");

View File

@ -63,6 +63,7 @@ Weapon::Weapon()
Weapon::~Weapon() Weapon::~Weapon()
{ {
Util::ReleaseTextures(Weapon::weapon_vec);
} }
void Weapon::SetGangWeapon(std::string& weapon_type) void Weapon::SetGangWeapon(std::string& weapon_type)

View File

@ -52,12 +52,12 @@
#include "eVehicleClass.h" #include "eVehicleClass.h"
#include "extensions/Paths.h" #include "extensions/Paths.h"
#include "fla/IDaccess.h" #include "../deps/fla/IDaccess.h"
#include "imgui/imgui.h" #include "../deps/imgui/imgui.h"
#include "imgui/imgui_internal.h" #include "../deps/imgui/imgui_internal.h"
#include "imgui/imgui_impl_dx9.h" #include "../deps/imgui/imgui_impl_dx9.h"
#include "imgui/imgui_impl_dx11.h" #include "../deps/imgui/imgui_impl_dx11.h"
#include "imgui/imgui_impl_win32.h" #include "../deps/imgui/imgui_impl_win32.h"
#include "MoreEvents.h" #include "MoreEvents.h"
#include "Json.h" #include "Json.h"
@ -84,6 +84,7 @@ struct Globals
inline static bool show_menu = false; inline static bool show_menu = false;
inline static bool init_done = false; inline static bool init_done = false;
inline static bool game_init = false; inline static bool game_init = false;
inline static bool menu_closing = false;
inline static Renderer renderer = Render_Unknown; inline static Renderer renderer = Render_Unknown;
inline static void* device = nullptr; inline static void* device = nullptr;
}; };