CheatMenuSA/CheatMenu/Util.cpp

250 lines
6.0 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"
2021-07-17 03:51:02 -04:00
#include "CFileLoader.h"
struct RwD3D9Raster
{
union
{
IDirect3DTexture9* texture;
IDirect3DSurface9* surface;
};
unsigned char* palette;
unsigned char alpha;
unsigned char cubeTextureFlags; /* 0x01 IS_CUBEMAP_TEX */
unsigned char textureFlags; /* 0x10 IS_COMPRESSED */
unsigned char lockedLevel;
IDirect3DSurface9* lockedSurface;
D3DLOCKED_RECT lockedRect;
D3DFORMAT format;
IDirect3DSwapChain9* swapChain;
HWND* hwnd;
};
struct RwRasterEx : public RwRaster
{
RwD3D9Raster *renderResource;
};
void Util::LoadTextureDirectory(SSearchData& data, char *path)
{
RwTexDictionary* pRwTexDictionary = &data.txd;
pRwTexDictionary = CFileLoader::LoadTexDictionary(path);
if (pRwTexDictionary)
{
RwTexDictionaryForAllTextures(pRwTexDictionary, [](RwTexture* tex, void* data)
{
SSearchData* sdata = reinterpret_cast<SSearchData*>(data);
sdata->m_ImagesList.push_back(std::make_unique<STextureStructure>());
sdata->m_ImagesList.back().get()->m_pRwTexture = tex;
sdata->m_ImagesList.back().get()->m_pTexture = GetTextureFromRaster(tex);
std::stringstream ss(tex->name);
std::string str;
getline(ss, str, '$');
sdata->m_ImagesList.back().get()->m_CategoryName = str;
if (!std::count(sdata->m_Categories.begin(), sdata->m_Categories.end(), str))
{
sdata->m_Categories.push_back(str);
}
getline(ss, str);
sdata->m_ImagesList.back().get()->m_FileName = str;
return tex;
}, &data);
}
}
void* Util::GetTextureFromRaster(RwTexture *pTexture)
{
RwRasterEx* raster = (RwRasterEx*)(&pTexture->raster->parent);
return (&raster->renderResource->texture);
}
// Thanks DKPac22
RwTexture* Util::LoadTextureFromMemory(char* data, unsigned int size)
{
patch::SetChar(0x7CF9CA, rwSTREAMMEMORY);
RwMemory memoryImage;
RwInt32 width, height, depth, flags;
memoryImage.start = (RwUInt8*)data;
memoryImage.length = size;
RwImage* image = RtPNGImageRead((char*)&memoryImage);
RwImageFindRasterFormat(image, 4, &width, &height, &depth, &flags);
RwRaster* raster = RwRasterCreate(width, height, depth, flags);
RwRasterSetFromImage(raster, image);
RwImageDestroy(image);
patch::SetChar(0x7CF9CA, rwSTREAMFILENAME);
return RwTextureCreate(raster);
}
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
}
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;
}
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
}