Skip to content

Commit

Permalink
implement config and injector
Browse files Browse the repository at this point in the history
  • Loading branch information
xan1242 committed Jan 4, 2025
1 parent 83f207f commit f6932a9
Show file tree
Hide file tree
Showing 12 changed files with 1,961 additions and 173 deletions.
2 changes: 1 addition & 1 deletion CarRandomizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "FEPlayerCarDB.hpp"
#include "EAXSound.hpp"
#include "NIS.hpp"
#include "../includes/injector/injector.hpp"
#include "injector/injector.hpp"
#include <iostream>
#include <unordered_map>
#include <string>
Expand Down
14 changes: 0 additions & 14 deletions CompileTimeHash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,4 @@ constexpr uint32_t stringhash32(const char* str, size_t string_length)

#define STRINGHASH32(str) (compiler_stringhash32<stringhash32(str, constexpr_strlen(str))>)

template <uint32_t V>
static constexpr uint32_t compiler_bStringHash = V;
constexpr uint32_t bCompilerStringHash(const char* text) {
uint32_t result = 0xFFFFFFFF;

for (size_t i = 0; text[i] != '\0'; ++i) {
result = 33 * result + static_cast<uint32_t>(text[i]);
}

return result;
}

#define BSTRINGHASH(str) (compiler_bStringHash<bCompilerStringHash(str)>)

#endif
2 changes: 1 addition & 1 deletion DebugVehicleSelection.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "framework.h"
#include "DebugVehicleSelection.hpp"
#include "../includes/injector/injector.hpp"
#include "injector/injector.hpp"
#include <iostream>

namespace DebugVehicleSelection
Expand Down
2 changes: 1 addition & 1 deletion EAXSound.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "framework.h"
#include "EAXSound.hpp"
#include "../includes/injector/injector.hpp"
#include "injector/injector.hpp"
#include <iostream>

namespace EAXSound
Expand Down
6 changes: 6 additions & 0 deletions NFSC_CarRandomizer.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[Main]
NoSound = 0 ; Disables sound (use this if the game is unstable, but make sure to disable movies as well! You can disable movies by renaming the MOVIES folder.)
NoRegular = 0 ; Excludes the regular cars from the randomizer list.
NoTraffic = 0 ; Excludes the traffic cars from the randomizer list.
NoCops = 0 ; Excludes the cop cars from the randomizer list.
IncludeSemis = 0 ; Includes the semis in the randomizer list.
2 changes: 1 addition & 1 deletion UserProfile.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "framework.h"
#include "UserProfile.hpp"
#include "../includes/injector/injector.hpp"
#include "injector/injector.hpp"
#include <iostream>

namespace UserProfile
Expand Down
229 changes: 74 additions & 155 deletions dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,197 +9,117 @@

#include <filesystem>

#include "../includes/injector/injector.hpp"
#include "injector/injector.hpp"
#include "mINI/src/mini/ini.h"

uintptr_t pInitializeEverything;

static void PostInit()
static bool ReadIniBoolValue(mINI::INIStructure& ini, const std::string& section, const std::string& key, bool& val)
{
#ifdef _DEBUG
freopen("CON", "wb", stdout);
freopen("CON", "wb", stderr);
#endif

DebugVehicleSelection::Init();
FEPlayerCarDB::Init();
UserProfile::Init();
EAXSound::Init();
NIS::Init();

CarRandomizer::Init();
}

static bool isNoSoundEnabled_Arg(int argc, char* argv[]) {
for (int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], "-nosound") == 0)
{
return true;
}
}
return false;
}

static bool isNoTrafficCarsEnabled_Arg(int argc, char* argv[]) {
for (int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], "-rndcars_notraffic") == 0)
{
return true;
}
}
return false;
}


static bool isTrafficSemisEnabled_Arg(int argc, char* argv[]) {
for (int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], "-rndcars_includesemis") == 0)
if (!ini.has(section))
return val;

if (!ini[section].has(key))
return val;

// trim inline comment
std::string valStr = ini[section][key];
size_t posComment = valStr.find(';');
if (posComment != valStr.npos)
{
valStr = valStr.substr(0, posComment);

// trim space
size_t posSpace = valStr.find(' ');
if (posSpace != valStr.npos)
{
return true;
valStr = valStr.substr(0, posSpace);
}
}
return false;
}

static bool isNoCopCarsEnabled_Arg(int argc, char* argv[]) {
for (int i = 1; i < argc; ++i)
if (valStr == "true")
{
if (strcmp(argv[i], "-rndcars_nocops") == 0)
{
return true;
}
}
return false;
}

static bool isNoRegularCarsEnabled_Arg(int argc, char* argv[]) {
for (int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], "-rndcars_noregular") == 0)
{
return true;
}
val = true;
return val;
}
return false;
}

static void InitializeEverything_Hook(int argc, char** argv)
{
bool bNoSound = isNoSoundEnabled_Arg(argc, argv);
if (bNoSound)
*reinterpret_cast<int*>(0x00A631B8) = 0;

if (isNoTrafficCarsEnabled_Arg(argc, argv))
CarRandomizer::SetExcludeTrafficCars(true);

if (isTrafficSemisEnabled_Arg(argc, argv))
CarRandomizer::SetIncludeTrafficSemis(true);

if (isNoCopCarsEnabled_Arg(argc, argv))
CarRandomizer::SetExcludeCopCars(true);

if (isNoRegularCarsEnabled_Arg(argc, argv))
CarRandomizer::SetExcludeRegularCars(true);

reinterpret_cast<void(*)(int, char**)>(pInitializeEverything)(argc, argv);

PostInit();
}

static bool ShouldDisableSound()
{
try
{
std::filesystem::path chkPath = ModPath::GetThisModulePath<std::filesystem::path>().parent_path();
chkPath /= "nosound.txt";

return std::filesystem::exists(chkPath);
int in_val = std::stoi(valStr, nullptr, 0);
val = in_val != 0;
}
catch (...)
{
return false;
return val;
}

return val;
}

static bool ShouldDisableTrafficList()
static void InitConfig()
{
try
{
std::filesystem::path chkPath = ModPath::GetThisModulePath<std::filesystem::path>().parent_path();
chkPath /= "rndcars_notraffic.txt";
std::filesystem::path iniPath = ModPath::GetThisModulePath<std::filesystem::path>().replace_extension("ini");

mINI::INIFile iniFile(iniPath);
mINI::INIStructure ini;

if (!iniFile.read(ini))
return;

if (!ini.has("Main"))
return;

bool bNoSound = false;
bool bNoRegular = false;
bool bNoTraffic = false;
bool bNoCops = false;
bool bIncludeSemis = false;

ReadIniBoolValue(ini, "Main", "NoSound", bNoSound);
ReadIniBoolValue(ini, "Main", "NoRegular", bNoRegular);
ReadIniBoolValue(ini, "Main", "NoTraffic", bNoTraffic);
ReadIniBoolValue(ini, "Main", "NoCops", bNoCops);
ReadIniBoolValue(ini, "Main", "IncludeSemis", bIncludeSemis);

CarRandomizer::SetExcludeRegularCars(bNoRegular);
CarRandomizer::SetExcludeTrafficCars(bNoTraffic);
CarRandomizer::SetExcludeCopCars(bNoCops);
CarRandomizer::SetIncludeTrafficSemis(bIncludeSemis);

return std::filesystem::exists(chkPath);
}
catch (...)
if (bNoSound)
{
return false;
uintptr_t loc_A631B8 = 0xA631B8;
*reinterpret_cast<int*>(loc_A631B8) = 0;
}
}

static bool ShouldEnableTrafficSemiList()
static void PostInit()
{
try
{
std::filesystem::path chkPath = ModPath::GetThisModulePath<std::filesystem::path>().parent_path();
chkPath /= "rndcars_includesemis.txt";
#ifdef _DEBUG
freopen("CON", "wb", stdout);
freopen("CON", "wb", stderr);
#endif

return std::filesystem::exists(chkPath);
}
catch (...)
{
return false;
}
}
InitConfig();

static bool ShouldDisableCopList()
{
try
{
std::filesystem::path chkPath = ModPath::GetThisModulePath<std::filesystem::path>().parent_path();
chkPath /= "rndcars_nocops.txt";
DebugVehicleSelection::Init();
FEPlayerCarDB::Init();
UserProfile::Init();
EAXSound::Init();
NIS::Init();

return std::filesystem::exists(chkPath);
}
catch (...)
{
return false;
}
CarRandomizer::Init();
}

static bool ShouldDisableRegularList()
static void InitializeEverything_Hook(int argc, char** argv)
{
try
{
std::filesystem::path chkPath = ModPath::GetThisModulePath<std::filesystem::path>().parent_path();
chkPath /= "rndcars_noregular.txt";
reinterpret_cast<void(*)(int, char**)>(pInitializeEverything)(argc, argv);

return std::filesystem::exists(chkPath);
}
catch (...)
{
return false;
}
PostInit();
}

static void PreInit()
{
if (ShouldDisableSound())
*reinterpret_cast<int*>(0x00A631B8) = 0;

if (ShouldDisableTrafficList())
CarRandomizer::SetExcludeTrafficCars(true);

if (ShouldEnableTrafficSemiList())
CarRandomizer::SetIncludeTrafficSemis(true);

if (ShouldDisableCopList())
CarRandomizer::SetExcludeCopCars(true);

if (ShouldDisableRegularList())
CarRandomizer::SetExcludeRegularCars(true);

uintptr_t loc_6B8EBC = 0x6B8EBC;

pInitializeEverything = static_cast<uintptr_t>(injector::GetBranchDestination(loc_6B8EBC));
Expand All @@ -214,4 +134,3 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID lpReserved)
}
return TRUE;
}

Loading

0 comments on commit f6932a9

Please sign in to comment.