Skip to content

Commit

Permalink
Update CPU/IO/Thread priority code, add version.ini, update exe check
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmollohan committed Dec 30, 2024
1 parent 5627297 commit 4f4f517
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 32 deletions.
41 changes: 20 additions & 21 deletions src/dll_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@

BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved) {
VersionWrapper::Initialise();
Utilities::SettingsParser::SetConfigFilePath(hModule);
Settings::LoadSettings();
Utilities::Processes::SetHighPriority();
if (Utilities::Processes::IsCompatibleExe()) {
Utilities::SettingsParser::SetConfigFilePath(hModule);
Settings::LoadSettings();
Utilities::Processes::SetPriorityLevels();

std::string exeName = Utilities::Processes::GetExeName();
if (Utilities::String::EqualsIgnoreCase(exeName, "Outlaws.exe") || Utilities::String::EqualsIgnoreCase(exeName, "Outlaws_Plus.exe")) {
switch (dwReason) {
case DLL_PROCESS_ATTACH:
MinHookHandler::Initialise();
DiskCacheEnabler::Enable();
ModLoader::Enable();
MinHookHandler::EnableAllHooks();
ScriptLoader::LoadScripts();
break;
case DLL_PROCESS_DETACH:
DiskCacheEnabler::Disable();
ModLoader::Disable();
MinHookHandler::Shutdown();
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_ATTACH:
MinHookHandler::Initialise();
DiskCacheEnabler::Enable();
ModLoader::Enable();
MinHookHandler::EnableAllHooks();
ScriptLoader::LoadScripts();
break;
case DLL_PROCESS_DETACH:
DiskCacheEnabler::Disable();
ModLoader::Disable();
MinHookHandler::Shutdown();
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
Expand Down
7 changes: 6 additions & 1 deletion src/settings.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "settings.hpp"
#include "utilities.hpp"

bool Settings::EnableMods = false;
bool Settings::EnableScripts = false;
bool Settings::CreateFileA = false;
bool Settings::CreateFileW = false;
std::string Settings::CPUPriority = "normal";
std::string Settings::IOPriority = "normal";
std::string Settings::ThreadPriority = "normal";

void Settings::LoadSettings() {
using SettingsParser = Utilities::SettingsParser;
Expand All @@ -13,4 +15,7 @@ void Settings::LoadSettings() {
EnableScripts = SettingsParser::GetBoolean("Settings", "EnableScripts", false);
CreateFileA = SettingsParser::GetBoolean("DiskCacheEnabler", "CreateFileA", false);
CreateFileW = SettingsParser::GetBoolean("DiskCacheEnabler", "CreateFileW", false);
CPUPriority = SettingsParser::GetString("Priorities", "CPUPriority", "normal");
IOPriority = SettingsParser::GetString("Priorities", "IOPriority", "normal");
ThreadPriority = SettingsParser::GetString("Priorities", "ThreadPriority", "normal");
}
6 changes: 6 additions & 0 deletions src/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#define VC_EXTRALEAN
#define WIN32_LEAN_AND_MEAN

#include <string>
#include "utilities.hpp"

class Settings {
public:
static void LoadSettings();
Expand All @@ -11,4 +14,7 @@ class Settings {
static bool EnableScripts;
static bool CreateFileA;
static bool CreateFileW;
static std::string CPUPriority;
static std::string IOPriority;
static std::string ThreadPriority;
};
72 changes: 66 additions & 6 deletions src/utilities.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#include "utilities.hpp"

const char* Utilities::Processes::allowedExes[] = {
"Outlaws.exe",
"Outlaws_Plus.exe",
"AFOP.exe",
"AFOP_Plus.exe"
};

bool Utilities::Files::LocalFileExists(LPCSTR file_path) {
DWORD dwAttrib = GetFileAttributesA(file_path);
return (dwAttrib != INVALID_FILE_ATTRIBUTES && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
Expand Down Expand Up @@ -93,12 +100,59 @@ std::string Utilities::Processes::GetExeName() {
return exeName;
}

void Utilities::Processes::SetHighPriority() {
bool Utilities::Processes::IsCompatibleExe() {
std::string exeName = Utilities::Processes::GetExeName();
for (const char* allowedExe : allowedExes) {
if (Utilities::String::EqualsIgnoreCase(exeName, allowedExe)) {
return true;
}
}
return false;
}

void Utilities::Processes::SetPriorityLevels() {
HANDLE hProcess = GetCurrentProcess();
SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS);
SetProcessPriorityBoost(hProcess, FALSE);
HANDLE hThread = GetCurrentThread();
SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST);

// Set CPU Priority
SetProcessPriorityBoost(hProcess, FALSE);
if (Settings::CPUPriority == "normal") {
SetPriorityClass(hProcess, NORMAL_PRIORITY_CLASS);
} else if (Settings::CPUPriority == "medium") {
SetPriorityClass(hProcess, ABOVE_NORMAL_PRIORITY_CLASS);
} else if (Settings::CPUPriority == "high") {
SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS);
}

// Set Thread Priority
if (Settings::ThreadPriority == "normal") {
SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL);
} else if (Settings::ThreadPriority == "medium") {
SetThreadPriority(hThread, THREAD_PRIORITY_ABOVE_NORMAL);
} else if (Settings::ThreadPriority == "high") {
SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST);
}

// Set IO Priority
HMODULE hNtDll = LoadLibrary(L"ntdll.dll");
if (hNtDll) {
auto NtSetInformationThread = (NtSetInformationThread_t)GetProcAddress(hNtDll, "NtSetInformationThread");
if (NtSetInformationThread) {
IO_PRIORITY_HINT ioPriorityHint = IoPriorityNormal;

if (Settings::IOPriority == "high") {
ioPriorityHint = IoPriorityHigh;
}

NTSTATUS status = NtSetInformationThread(
hThread,
ThreadIoPriority,
&ioPriorityHint,
sizeof(IO_PRIORITY_HINT)
);
}
FreeLibrary(hNtDll);
}
}

std::string Utilities::SettingsParser::configFilePath = "";
Expand Down Expand Up @@ -126,7 +180,7 @@ bool Utilities::SettingsParser::GetBoolean(const std::string& section, const std
DWORD length = GetPrivateProfileStringA(
section.c_str(),
key.c_str(),
defaultValue ? "true" : "false", // Default value
defaultValue ? "true" : "false",
result,
sizeof(result),
configFilePath.c_str()
Expand All @@ -145,7 +199,7 @@ bool Utilities::SettingsParser::GetBoolean(const std::string& section, const std
value.erase(value.find_last_not_of(" \t") + 1); // Right trim

// Convert to lowercase for case-insensitive comparison
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
Utilities::String::ToLower(value);

// Interpret the result as a boolean
return value == "true" || value == "1" || value == "yes" || value == "on";
Expand Down Expand Up @@ -212,3 +266,9 @@ std::string Utilities::SettingsParser::GetString(const std::string& section, con
bool Utilities::String::EqualsIgnoreCase(const std::string& str1, const std::string& str2) {
return str1.size() == str2.size() && std::equal(str1.begin(), str1.end(), str2.begin(), [](char a, char b) { return std::tolower(a) == std::tolower(b); });
}

void Utilities::String::ToLower(std::string& str) {
for (size_t i = 0; i < str.size(); ++i) {
str[i] = std::tolower(static_cast<unsigned char>(str[i]));
}
}
30 changes: 26 additions & 4 deletions src/utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,27 @@
#define WIN32_LEAN_AND_MEAN

#include <Windows.h>
#include <algorithm>
#include <string>
#include <winternl.h>
#include "settings.hpp"

#define PATTERN_WILDCARD 0x00
#define ThreadIoPriority (THREADINFOCLASS)21

typedef enum _IO_PRIORITY_HINT {
IoPriorityVeryLow = 0,
IoPriorityLow,
IoPriorityNormal,
IoPriorityHigh,
IoPriorityCritical
} IO_PRIORITY_HINT;

typedef NTSTATUS(NTAPI* NtSetInformationThread_t)(
HANDLE ThreadHandle,
THREADINFOCLASS ThreadInformationClass,
PVOID ThreadInformation,
ULONG ThreadInformationLength
);

class Utilities {
public:
Expand All @@ -26,8 +43,8 @@ class Utilities {
MainModule();
~MainModule();

uintptr_t GetBaseAddress() const; // Gets the module base address
uintptr_t GetCodeSize() const; // Gets the size of code reported by the PE header
uintptr_t GetBaseAddress() const;
uintptr_t GetCodeSize() const;
size_t Begin() const;
size_t End() const;
std::string GetModulePath() const;
Expand All @@ -38,7 +55,11 @@ class Utilities {
static uintptr_t FindPatternAddress(const unsigned char* pattern, size_t patternLength);
static uintptr_t FindPatternAddressMask(const unsigned char* pattern, const unsigned char* mask, size_t patternLength);
static std::string GetExeName();
static void SetHighPriority();
static bool IsCompatibleExe();
static void SetPriorityLevels();

private:
static const char* allowedExes[];
};

class SettingsParser {
Expand All @@ -55,5 +76,6 @@ class Utilities {
class String {
public:
static bool EqualsIgnoreCase(const std::string& str1, const std::string& str2);
static void ToLower(std::string& str);
};
};
12 changes: 12 additions & 0 deletions version.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Settings]
EnableMods=true # Enables mod support (true/false, default: true)
EnableScripts=true # Enables loading DLL/ASI scripts (true/false, default: true)

[DiskCacheEnabler]
CreateFileA=true # Enables disk caching during calls to CreateFileA (true/false, default: true)
CreateFileW=true # Enables disk caching during calls to CreateFileW (true/false, default: true)

[Priorities]
CPUPriority=high # Sets the priority level of the CPU (normal/medium/high, default: high)
IOPriority=high # Sets the priority level of the IO (normal/high, default: high)
ThreadPriority=high # Sets the priority level of threads (normal/medium/high, default: high)

0 comments on commit 4f4f517

Please sign in to comment.