Skip to content

Commit 56b1ed5

Browse files
committed
Added features (10 JAN 22)
Added ini option ChangeScriptedCars Added ability to change only parked cars
1 parent 27e4b5a commit 56b1ed5

5 files changed

Lines changed: 100 additions & 34 deletions

File tree

ModelVariations/LogUtil.cpp

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
#include <iomanip>
44
#include <fstream>
55
#include <sstream>
6+
#include <Windows.h>
67
#include <shlwapi.h>
78
#include <bcrypt.h>
89
#include <Psapi.h>
910

11+
#pragma comment (lib, "Advapi32.lib")
1012
#pragma comment (lib, "bcrypt.lib")
1113
#pragma comment (lib, "shlwapi.lib")
1214

@@ -41,7 +43,7 @@ std::string hashFile(const char* filename, int& filesize)
4143
BCRYPT_ALG_HANDLE hProvider = NULL;
4244
BCRYPT_HASH_HANDLE ctx = NULL;
4345

44-
BCryptOpenAlgorithmProvider(&hProvider, BCRYPT_SHA256_ALGORITHM, NULL, BCRYPT_HASH_REUSABLE_FLAG);
46+
BCryptOpenAlgorithmProvider(&hProvider, BCRYPT_SHA256_ALGORITHM, NULL, 0);
4547

4648
BCryptCreateHash(hProvider, &ctx, NULL, 0, NULL, 0, 0);
4749
if (ctx != NULL)
@@ -93,12 +95,12 @@ std::string getParentModuleName(unsigned int address)
9395
return emptyString;
9496
}
9597

96-
void checkCallModified(const char *callName, unsigned int originalAddress)
98+
void checkCallModified(const char *callName, unsigned int originalAddress, bool directAddress = false)
9799
{
98100
if (callChecks.find(*((unsigned int*)originalAddress)) != callChecks.end())
99101
return;
100102

101-
unsigned int changedAddress = getAddressFromCall((BYTE*)originalAddress);
103+
unsigned int changedAddress = (directAddress == false) ? getAddressFromCall((BYTE*)originalAddress) : directAddress;
102104
std::string moduleName = getParentModuleName(changedAddress);
103105
unsigned int baseAddress = 0;
104106

@@ -129,6 +131,26 @@ void checkCallModified(const char *callName, unsigned int originalAddress)
129131
logfile << moduleName << " 0x" << baseAddress << std::endl;
130132
}
131133

134+
std::string getWindowsVersion()
135+
{
136+
std::string retString;
137+
char str[255] = {};
138+
DWORD cbData = 254;
139+
HKEY hkey;
140+
141+
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS)
142+
{
143+
if (RegQueryValueEx(hkey, "CurrentBuild", NULL, NULL, (LPBYTE)str, &cbData) == ERROR_SUCCESS)
144+
{
145+
retString += "OS build ";
146+
retString += str;
147+
}
148+
RegCloseKey(hkey);
149+
}
150+
151+
152+
return retString;
153+
}
132154

133155
void checkAllCalls()
134156
{
@@ -192,16 +214,28 @@ void checkAllCalls()
192214
checkCallModified("PossiblyRemoveVehicle", 0x60C4E8);
193215
checkCallModified("PossiblyRemoveVehicle", 0x42CD55);
194216

195-
if (enableLights == 1)
217+
if (changeScriptedCars == 1)
218+
checkCallModified("CreateCarForScript", 0x467B01);
219+
220+
if (enableSpecialFeatures == 1)
196221
{
197-
checkCallModified("RegisterCorona", 0x6ABA60);
198-
checkCallModified("RegisterCorona", 0x6ABB35);
199-
checkCallModified("RegisterCorona", 0x6ABC69);
222+
checkCallModified("ProcessControl", 0x871148, true);
223+
checkCallModified("PreRender", 0x871164, true);
200224
}
201225

202226
if (enableSiren == 1)
203227
checkCallModified("HasCarSiren", 0x6D8492);
204228

229+
if (enableLights == 1 && enableSpecialFeatures == 1 && enableSiren == 1)
230+
{
231+
checkCallModified("RegisterCorona", 0x6ABA60);
232+
checkCallModified("RegisterCorona", 0x6ABB35);
233+
checkCallModified("RegisterCorona", 0x6ABC69);
234+
235+
checkCallModified("AddLight", 0x6AB80F);
236+
checkCallModified("AddLight", 0x6ABBA6);
237+
}
238+
205239
if (disablePayAndSpray == 1)
206240
checkCallModified("IsCarSprayable", 0x44AC75);
207241

ModelVariations/LogUtil.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <string>
55
#include <iostream>
66

7-
#define MOD_VERSION "5.1"
7+
#define MOD_VERSION "5.2"
88
#ifdef _DEBUG
99
#define MOD_NAME "ModelVariations_d.asi"
1010
#else
@@ -20,8 +20,11 @@ extern int enableLights;
2020
extern bool enableSideMissions;
2121
extern int enableSiren;
2222
extern int disablePayAndSpray;
23+
extern int enableSpecialFeatures;
24+
extern int changeScriptedCars;
2325

2426
std::string hashFile(const char* filename, int& filesize);
2527
unsigned int getAddressFromCall(unsigned char* data);
26-
void checkCallModified(const char* callName, unsigned int originalAddress);
28+
void checkCallModified(const char* callName, unsigned int originalAddress, bool directAccess);
29+
std::string getWindowsVersion();
2730
void checkAllCalls();

ModelVariations/ModelVariations.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ std::map<short, std::pair<CVector, float>> LightPositions;
5252

5353
std::vector<short> vehCurrentVariations[212];
5454
std::vector<short> pedCurrentVariations[300];
55-
5655
std::vector<short> vehCarGenExclude;
5756

57+
std::set<short> parkedCars;
58+
5859
std::stack<CPed*> pedStack;
5960

6061
BYTE dealersFixed = 0;
@@ -71,6 +72,8 @@ int loadAllVehicles = 0;
7172
int enableLights = 0;
7273
int enableSiren = 0;
7374
int disablePayAndSpray = 0;
75+
int enableSpecialFeatures = 0;
76+
int changeScriptedCars = 0;
7477

7578
void(__fastcall *ProcessControlOriginal)(CAutomobile*) = NULL;
7679
void(__fastcall *PreRenderOriginal)(CAutomobile*) = NULL;
@@ -336,7 +339,7 @@ class ModelVariations {
336339
//logfile << std::endl;
337340

338341

339-
logfile << "Model Variations " MOD_VERSION << "\n\n\n";
342+
logfile << "Model Variations " MOD_VERSION << "\n" << getWindowsVersion() << "\n\n";
340343

341344
char exePath[256] = {};
342345
GetModuleFileName(NULL, exePath, 255);
@@ -563,22 +566,22 @@ class ModelVariations {
563566
{
564567
if (dealersFixed < 10)
565568
dealersFixed++;
566-
if (callsChecked < 100)
569+
if (callsChecked < 1000)
567570
callsChecked++;
568571

569-
if (callsChecked == 100 && enableLog == 1)
572+
if (callsChecked == 1000 && enableLog == 1)
570573
{
571574
checkAllCalls();
572-
callsChecked = 101;
575+
callsChecked = 0;
573576
}
574577

575578
if (dealersFixed == 10)
576579
{
577580
if (enableLog == 1)
578-
logfile << "Applying drug dealer fix..." << std::endl;
581+
logfile << "Applying drug dealer fix... ";
579582
drugDealerFix();
580583
if (enableLog == 1)
581-
logfile << std::endl;
584+
logfile << "OK" << std::endl;
582585
dealersFixed = 11;
583586
}
584587
};

ModelVariations/Vehicles.cpp

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "../../injector/assembly.hpp"
66

77
#include "CBoat.h"
8+
#include "CCarCtrl.h"
89
#include "CCarGenerator.h"
910
#include "CCoronas.h"
1011
#include "CGeneral.h"
@@ -83,13 +84,20 @@ int getVariationOriginalModel(int modelIndex)
8384
return originalModel;
8485
}
8586

86-
int getRandomVariation(int modelid)
87+
int getRandomVariation(int modelid, bool parked = false)
8788
{
8889
if (modelid < 400 || modelid > 611)
8990
return modelid;
9091
if (vehCurrentVariations[modelid - 400].empty())
9192
return modelid;
9293

94+
if (parked == false)
95+
{
96+
auto it = parkedCars.find(modelid);
97+
if (it != parkedCars.end())
98+
return modelid;
99+
}
100+
93101
int random = CGeneral::GetRandomNumberInRange(0, vehCurrentVariations[modelid - 400].size());
94102
int variationModel = vehCurrentVariations[modelid - 400][random];
95103
if (variationModel > -1)
@@ -106,6 +114,9 @@ void readVehicleIni()
106114
{
107115
for (int i = 400; i < 612; i++)
108116
{
117+
if (iniVeh.ReadInteger(std::to_string(i), "ChangeOnlyParked", 0) == 1)
118+
parkedCars.insert(i);
119+
109120
std::vector<short> vec = iniLineParser(VEHICLE_VARIATION, i, "Countryside", &iniVeh);
110121
vehVariations[i - 400][0] = vec;
111122
std::sort(vehVariations[i - 400][0].begin(), vehVariations[i - 400][0].end());
@@ -362,7 +373,7 @@ signed int __fastcall PickRandomCarHooked(CLoadedCarGroup* cargrp, void*, char a
362373
{
363374
if (cargrp == NULL)
364375
return -1;
365-
return getRandomVariation(callMethodOriginalAndReturn<signed int, address>(cargrp, a2, a3));
376+
return getRandomVariation(callMethodOriginalAndReturn<signed int, address>(cargrp, a2, a3), true);
366377
}
367378

368379
template <unsigned int address>
@@ -380,7 +391,7 @@ void __fastcall DoInternalProcessingHooked(CCarGenerator* park) //for non-random
380391
return;
381392
}
382393

383-
park->m_nModelId = getRandomVariation(park->m_nModelId);
394+
park->m_nModelId = getRandomVariation(park->m_nModelId, true);
384395
callMethodOriginal<address>(park);
385396
park->m_nModelId = model;
386397
return;
@@ -405,7 +416,7 @@ void __fastcall DoInternalProcessingHooked(CCarGenerator* park) //for non-random
405416
case 430: //Predator
406417
case 496: //Police Maverick
407418
case 488: //News Chopper
408-
park->m_nModelId = getRandomVariation(park->m_nModelId);
419+
park->m_nModelId = getRandomVariation(park->m_nModelId, true);
409420
callMethodOriginal<address>(park);
410421
park->m_nModelId = model;
411422
break;
@@ -775,6 +786,12 @@ void __cdecl PossiblyRemoveVehicleHooked(CVehicle* car)
775786

776787
}
777788

789+
template <unsigned int address>
790+
CVehicle* __cdecl CreateCarForScriptHooked(int modelId, float posX, float posY, float posZ, char doMissionCleanup)
791+
{
792+
return callOriginalAndReturn<CVehicle*, address>(getRandomVariation(modelId), posX, posY, posZ, doMissionCleanup);
793+
}
794+
778795
/*
779796
0x053A926
780797
void __declspec(naked) ()
@@ -1036,8 +1053,7 @@ void __fastcall CAutomobileRenderHooked(CAutomobile* veh)
10361053
void installVehicleHooks()
10371054
{
10381055
if (enableLog == 1)
1039-
logfile << "Installing vehicle hooks..." << std::endl;
1040-
1056+
logfile << "Installing vehicle hooks... ";
10411057

10421058
hookCall(0x43022A, ChooseModelHooked<0x43022A>); //CCarCtrl::GenerateOneRandomCar
10431059
//patch::RedirectJump(0x424E20, ChoosePoliceCarModelHooked);
@@ -1104,18 +1120,10 @@ void installVehicleHooks()
11041120
hookCall(0x60C4E8, PossiblyRemoveVehicleHooked<0x60C4E8>); //CPlayerPed::KeepAreaAroundPlayerClear
11051121
hookCall(0x42CD55, PossiblyRemoveVehicleHooked<0x42CD55>); //CCarCtrl::RemoveDistantCars
11061122

1123+
if (changeScriptedCars = iniVeh.ReadInteger("Settings", "ChangeScriptedCars", 0))
1124+
hookCall(0x467B01, CreateCarForScriptHooked<0x467B01>);
11071125

1108-
if (enableLights == 1)
1109-
{
1110-
hookCall(0x6ABA60, RegisterCoronaHooked<0x6ABA60>); //CAutomobile::PreRender
1111-
hookCall(0x6ABB35, RegisterCoronaHooked<0x6ABB35>); //CAutomobile::PreRender
1112-
hookCall(0x6ABC69, RegisterCoronaHooked<0x6ABC69>); //CAutomobile::PreRender
1113-
1114-
hookCall(0x6AB80F, AddLightHooked<0x6AB80F>); //CAutomobile::PreRender
1115-
hookCall(0x6ABBA6, AddLightHooked<0x6ABBA6>); //CAutomobile::PreRender
1116-
}
1117-
1118-
if (iniVeh.ReadInteger("Settings", "EnableSpecialFeatures", 0))
1126+
if (enableSpecialFeatures = iniVeh.ReadInteger("Settings", "EnableSpecialFeatures", 0))
11191127
{
11201128
void(__fastcall **p)(CAutomobile*) = reinterpret_cast<void(__fastcall**)(CAutomobile*)>(0x871148);
11211129
ProcessControlOriginal = *p;
@@ -1161,6 +1169,16 @@ void installVehicleHooks()
11611169
if ((enableSiren = iniVeh.ReadInteger("Settings", "EnableSiren", 0)) == 1)
11621170
patch::RedirectCall(0x6D8492, HasCarSiren); //CVehicle::UsesSiren
11631171

1172+
if (enableLights == 1 && enableSpecialFeatures == 1 && enableSiren == 1)
1173+
{
1174+
hookCall(0x6ABA60, RegisterCoronaHooked<0x6ABA60>); //CAutomobile::PreRender
1175+
hookCall(0x6ABB35, RegisterCoronaHooked<0x6ABB35>); //CAutomobile::PreRender
1176+
hookCall(0x6ABC69, RegisterCoronaHooked<0x6ABC69>); //CAutomobile::PreRender
1177+
1178+
hookCall(0x6AB80F, AddLightHooked<0x6AB80F>); //CAutomobile::PreRender
1179+
hookCall(0x6ABBA6, AddLightHooked<0x6ABBA6>); //CAutomobile::PreRender
1180+
}
1181+
11641182
if ((disablePayAndSpray = iniVeh.ReadInteger("Settings", "DisablePayAndSpray", 0)) == 1)
11651183
hookCall(0x44AC75, IsCarSprayableHooked<0x44AC75>); //CGarage::Update
11661184

@@ -1170,4 +1188,7 @@ void installVehicleHooks()
11701188
hookCall(0x48DA81, IsLawEnforcementVehicleHooked<0x48DA81>);
11711189
hookCall(0x469612, CollectParametersHooked<0x469612>);
11721190
}
1191+
1192+
if (enableLog == 1)
1193+
logfile << "OK" << std::endl;
11731194
}

ModelVariations/Vehicles.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extern std::ofstream logfile;
1616

1717
extern std::array<std::vector<short>, 16> vehVariations[212];
1818
extern std::array<std::vector<short>, 16> vehWantedVariations[212];
19+
extern std::map<short, short> vehOriginalModels;
1920
extern std::map<short, std::vector<short>> vehDrivers;
2021
extern std::map<short, std::vector<short>> vehPassengers;
2122
extern std::map<short, std::vector<short>> vehDriverGroups[9];
@@ -24,15 +25,19 @@ extern std::map<short, BYTE> modelNumGroups;
2425
extern std::map<short, std::pair<CVector, float>> LightPositions;
2526

2627
extern std::vector<short> vehCurrentVariations[212];
27-
extern std::map<short, short> vehOriginalModels;
2828
extern std::vector<short> vehCarGenExclude;
29+
30+
extern std::set<short> parkedCars;
31+
2932
extern int loadAllVehicles;
3033

3134
extern int enableLog;
3235
extern int changeCarGenerators;
3336
extern bool enableSideMissions;
3437
extern int enableAllSideMissions;
3538
extern int enableLights;
39+
extern int enableSpecialFeatures;
40+
extern int changeScriptedCars;
3641

3742
extern void(__fastcall *ProcessControlOriginal)(CAutomobile*);
3843
extern void(__fastcall *PreRenderOriginal)(CAutomobile*);

0 commit comments

Comments
 (0)