CheatMenuSA/CheatMenu/Util.cpp

278 lines
7.1 KiB
C++
Raw Normal View History

2020-12-02 16:19:16 -05:00
#include "pch.h"
#include "Util.h"
2021-04-07 16:35:21 -04:00
#include "../Depend/imgui/stb_image.h"
2021-04-25 14:59:14 -04:00
#include <CCutsceneMgr.h>
#include "psapi.h"
2020-12-02 16:19:16 -05:00
void Util::ClearCharTasksVehCheck(CPed* ped)
{
uint hped = CPools::GetPedRef(ped);
uint hveh = NULL;
bool veh_engine = true;
2020-12-02 16:19:16 -05:00
2021-02-15 18:58:02 -05:00
if (ped->m_nPedFlags.bInVehicle)
{
2020-12-02 16:19:16 -05:00
hveh = CPools::GetVehicleRef(ped->m_pVehicle);
veh_engine = ped->m_pVehicle->m_nVehicleFlags.bEngineOn;
}
2020-12-02 16:19:16 -05:00
Command<Commands::CLEAR_CHAR_TASKS_IMMEDIATELY>(hped);
if (hveh)
{
2020-12-02 16:19:16 -05:00
Command<Commands::TASK_WARP_CHAR_INTO_CAR_AS_DRIVER>(hped, hveh);
ped->m_pVehicle->m_nVehicleFlags.bEngineOn = veh_engine;
}
2020-12-02 16:19:16 -05:00
}
2021-06-18 12:49:11 -04:00
void Util::ReleaseTextures(std::vector<std::unique_ptr<STextureStructure>>& store_vec)
{
2021-03-16 03:12:59 -04:00
// 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();
// }
}
2021-06-18 12:49:11 -04:00
void Util::LoadTexturesInDirRecursive(const char* path, const char* file_ext, std::vector<std::string>& category_vec,
std::vector<std::unique_ptr<STextureStructure>>& store_vec)
2020-12-02 16:19:16 -05:00
{
std::string folder = "";
2021-02-24 16:54:45 -05:00
for (auto& p : fs::recursive_directory_iterator(path))
2020-12-02 16:19:16 -05:00
{
if (p.path().extension() == file_ext)
{
2021-06-18 12:49:11 -04:00
store_vec.push_back(std::make_unique<STextureStructure>());
2020-12-02 16:19:16 -05:00
HRESULT hr = -1;
2021-03-16 03:12:59 -04:00
2020-12-02 16:19:16 -05:00
if (Globals::renderer == Render_DirectX9)
2021-06-18 12:49:11 -04:00
hr = D3DXCreateTextureFromFileA(GetD3DDevice(), p.path().string().c_str(),
reinterpret_cast<PDIRECT3DTEXTURE9*>(&store_vec.back().get()->m_pTexture));
2021-03-16 03:12:59 -04:00
else
2020-12-02 16:19:16 -05:00
{
2021-06-18 12:49:11 -04:00
if (LoadTextureFromFileDx11(p.path().string().c_str(),
reinterpret_cast<ID3D11ShaderResourceView**>(&store_vec.back().get()->
m_pTexture)))
2020-12-02 16:19:16 -05:00
hr = S_OK;
}
if (hr == S_OK)
{
2021-06-18 12:49:11 -04:00
store_vec.back().get()->m_FileName = p.path().stem().string();
store_vec.back().get()->m_CategoryName = folder;
2020-12-02 16:19:16 -05:00
}
else
{
2021-02-25 17:45:41 -05:00
flog << "Failed to load " << p.path().stem().string() << std::endl;
2020-12-02 16:19:16 -05:00
store_vec.pop_back();
}
}
else
{
folder = p.path().stem().string();
category_vec.push_back(folder);
}
}
}
bool Util::LoadTextureFromFileDx11(const char* filename, ID3D11ShaderResourceView** out_srv)
{
// Load from disk into a raw RGBA buffer
int image_width = 0;
int image_height = 0;
2021-06-18 12:49:11 -04:00
unsigned char* image_data = stbi_load(filename, &image_width, &image_height, nullptr, 4);
if (image_data == nullptr)
2020-12-02 16:19:16 -05:00
return false;
// Create texture
D3D11_TEXTURE2D_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.Width = image_width;
desc.Height = image_height;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;
2021-06-18 12:49:11 -04:00
ID3D11Texture2D* pTexture = nullptr;
2020-12-02 16:19:16 -05:00
D3D11_SUBRESOURCE_DATA subResource;
subResource.pSysMem = image_data;
subResource.SysMemPitch = desc.Width * 4;
subResource.SysMemSlicePitch = 0;
reinterpret_cast<ID3D11Device*>(Globals::device)->CreateTexture2D(&desc, &subResource, &pTexture);
2020-12-02 16:19:16 -05:00
// Create texture view
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
ZeroMemory(&srvDesc, sizeof(srvDesc));
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MipLevels = desc.MipLevels;
srvDesc.Texture2D.MostDetailedMip = 0;
reinterpret_cast<ID3D11Device*>(Globals::device)->CreateShaderResourceView(pTexture, &srvDesc, out_srv);
2020-12-02 16:19:16 -05:00
pTexture->Release();
stbi_image_free(image_data);
return true;
}
bool Util::IsOnMission()
{
2021-06-18 12:49:11 -04:00
return FindPlayerPed()->CanPlayerStartMission() && !*(patch::Get<char*>(0x5D5380, false) +
CTheScripts::OnAMissionFlag);
2020-12-02 16:19:16 -05:00
}
2021-04-25 14:59:14 -04:00
bool Util::IsOnCutscene()
{
return CCutsceneMgr::ms_running;
}
2021-02-24 16:54:45 -05:00
std::string Util::GetLocationName(CVector* pos)
2020-12-02 16:19:16 -05:00
{
int hplayer = CPools::GetPedRef(FindPlayerPed());
int interior = 0;
Command<Commands::GET_AREA_VISIBLE>(&interior);
std::string town = "San Andreas";
int city;
Command<Commands::GET_CITY_PLAYER_IS_IN>(&hplayer, &city);
switch (city)
{
2021-06-18 12:49:11 -04:00
case 0:
town = "CS";
break;
case 1:
town = "LS";
break;
case 2:
town = "SF";
break;
case 3:
town = "LV";
break;
2020-12-02 16:19:16 -05:00
}
if (interior == 0)
return CTheZones::FindSmallestZoneForPosition(*pos, true)->GetTranslatedName() + std::string(", ") + town;
2021-06-18 12:49:11 -04:00
return std::string("Interior ") + std::to_string(interior) + ", " + town;
2020-12-02 16:19:16 -05:00
}
int Util::GetLargestGangInZone()
{
int gang_id = 0, max_density = 0;
for (int i = 0; i != 10; ++i)
{
CVector pos = FindPlayerPed()->GetPosition();
2021-02-24 16:54:45 -05:00
CZoneExtraInfo* zone_info = CTheZones::GetZoneInfo(&pos, nullptr);
2020-12-02 16:19:16 -05:00
int density = zone_info->m_nGangDensity[i];
2020-12-02 16:19:16 -05:00
if (density > max_density)
{
max_density = density;
gang_id = i;
}
}
return gang_id;
}
// implemention of opcode 0AB5 (STORE_CLOSEST_ENTITIES)
2020-12-02 16:19:16 -05:00
// https://github.com/cleolibrary/CLEO4/blob/916d400f4a731ba1dd0ff16e52bdb056f42b7038/source/CCustomOpcodeSystem.cpp#L1671
CVehicle* Util::GetClosestVehicle()
2020-12-02 16:19:16 -05:00
{
2021-02-24 16:54:45 -05:00
CPlayerPed* player = FindPlayerPed();
CPedIntelligence* pedintel;
2020-12-02 16:19:16 -05:00
if (player && (pedintel = player->m_pIntelligence))
{
2021-02-24 16:54:45 -05:00
CVehicle* veh = nullptr;
2020-12-02 16:19:16 -05:00
for (int i = 0; i < 16; i++)
{
2021-06-18 12:49:11 -04:00
veh = static_cast<CVehicle*>(pedintel->m_vehicleScanner.m_apEntities[i]);
2020-12-02 16:19:16 -05:00
if (veh && !veh->m_nVehicleFlags.bFadeOut)
break;
veh = nullptr;
}
return veh;
}
return nullptr;
}
CPed* Util::GetClosestPed()
{
2021-02-24 16:54:45 -05:00
CPlayerPed* player = FindPlayerPed();
CPedIntelligence* pedintel;
if (player && (pedintel = player->m_pIntelligence))
{
2021-02-24 16:54:45 -05:00
CPed* ped = nullptr;
for (int i = 0; i < 16; i++)
{
2021-06-18 12:49:11 -04:00
ped = static_cast<CPed*>(pedintel->m_pedScanner.m_apEntities[i]);
if (ped && ped != player && (ped->m_nCreatedBy & 0xFF) == 1 && !ped->m_nPedFlags.bFadeOut)
break;
ped = nullptr;
}
return ped;
}
return nullptr;
}
2021-02-24 16:54:45 -05:00
void Util::RainbowValues(int& r, int& g, int& b, float speed)
2020-12-02 16:19:16 -05:00
{
2021-02-24 16:54:45 -05:00
int timer = CTimer::m_snTimeInMilliseconds / 150;
2020-12-02 16:19:16 -05:00
r = sin(timer * speed) * 127 + 128;
g = sin(timer * speed + 2) * 127 + 128;
b = sin(timer * speed + 4) * 127 + 128;
}
RwTexture* CreateRwTextureFromRwImage(RwImage* image)
{
RwInt32 width, height, depth, flags;
RwImageFindRasterFormat(image, 4, &width, &height, &depth, &flags);
RwRaster* raster = RwRasterCreate(width, height, depth, flags);
RwRasterSetFromImage(raster, image);
RwImageDestroy(image);
RwTexture* texture = RwTextureCreate(raster);
return texture;
}
RwTexture* Util::LoadTextureFromPngFile(fs::path path)
{
RwImage* image = RtPNGImageRead(path.string().c_str());
if (!image)
return nullptr;
RwTexture* texture = CreateRwTextureFromRwImage(image);
path.stem().string().copy(texture->name, sizeof(texture->name) - 1);
return texture;
}
void Util::GetCPUUsageInit()
{
2021-06-18 12:49:11 -04:00
PdhOpenQuery(nullptr, NULL, &cpuQuery);
PdhAddEnglishCounter(cpuQuery, "\\Processor(_Total)\\% Processor Time", NULL, &cpuTotal);
PdhCollectQueryData(cpuQuery);
}
double Util::GetCurrentCPUUsage()
{
PDH_FMT_COUNTERVALUE counterVal;
PdhCollectQueryData(cpuQuery);
2021-06-18 12:49:11 -04:00
PdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, nullptr, &counterVal);
return counterVal.doubleValue;
2021-06-18 12:49:11 -04:00
}