Skip to content

Commit

Permalink
New game version update (1.586441.SNOW_DLC_14 (Patch 32.2))
Browse files Browse the repository at this point in the history
* INI config class
* Option for disabling in-game shifting, so that it won't interfere with the mod
  • Loading branch information
Ferrster committed Nov 3, 2024
1 parent e2d1fb8 commit ef9cc8a
Show file tree
Hide file tree
Showing 13 changed files with 345 additions and 187 deletions.
18 changes: 11 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(SMGM_NO_CONSOLE OFF CACHE BOOL "Do not use console window for log display")
set(SMGM_USE_DETOURS ON CACHE BOOL "Use detours for hooking")

add_compile_definitions(
_SILENCE_ALL_MS_EXT_DEPRECATION_WARNINGS
)

if(SMGM_USE_DETOURS)
set(SMGM_PATH_TO_VCVARS64 "G:\\Programs\\Visual Studio 19\\VC\\Auxiliary\\Build\\vcvars64.bat" CACHE PATH "Path to vcvars64.bat. Needed to build detours library")
include(ExternalProject)
ExternalProject_Add(DetoursLib
GIT_REPOSITORY https://github.com/microsoft/Detours.git
GIT_TAG origin/main
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
GIT_REPOSITORY https://github.com/microsoft/Detours.git
GIT_TAG origin/main
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
)
ExternalProject_Get_Property(DetoursLib SOURCE_DIR)
ExternalProject_Add_Step(DetoursLib _build
COMMAND ${PROJECT_SOURCE_DIR}/misc/build_detours.bat ${SMGM_PATH_TO_VCVARS64} ${SOURCE_DIR}
ALWAYS ON
COMMAND ${PROJECT_SOURCE_DIR}/misc/build_detours.bat ${SMGM_PATH_TO_VCVARS64} ${SOURCE_DIR}
ALWAYS ON
)
link_directories(${SOURCE_DIR}/lib.X64)
include_directories(${SOURCE_DIR}/include)
Expand Down
62 changes: 32 additions & 30 deletions src/dll/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,58 @@ find_package(Boost 1.80.0 CONFIG REQUIRED)

# find_package(Boost 1.80 COMPONENTS log log_setup REQUIRED)
add_library(${TARGET_NAME} SHARED
game_data/game_data.h
game_data/game_data.cxx
game_data/data_types.h
game_data/vehicle.cxx
utils/logging.h
utils/logging.cxx
utils/format_helpers.h
utils/format_helpers.cxx
utils/input_reader.h
utils/input_reader.cxx
utils/hooks.h
utils/hooks.cxx
custom_functions.h
custom_functions.cxx
main.cxx
game_data/game_data.h
game_data/game_data.cxx
game_data/data_types.h
game_data/vehicle.cxx
config/ini_config.hpp
config/ini_config.cpp
utils/logging.h
utils/logging.cxx
utils/format_helpers.h
utils/format_helpers.cxx
utils/input_reader.h
utils/input_reader.cxx
utils/hooks.h
utils/hooks.cxx
custom_functions.h
custom_functions.cxx
main.cxx
)
target_compile_definitions(${TARGET_NAME}
PUBLIC BOOST_USE_WINAPI_VERSION=1536 # <= Win Vista
PUBLIC NOMINMAX
PRIVATE _CRT_SECURE_NO_WARNINGS
PUBLIC SMGM_TITLE="${PROJECT_NAME}"
PUBLIC BOOST_USE_WINAPI_VERSION=1536 # <= Win Vista
PUBLIC NOMINMAX
PRIVATE _CRT_SECURE_NO_WARNINGS
PUBLIC SMGM_TITLE="${PROJECT_NAME}"
)

if(SMGM_NO_CONSOLE)
target_compile_definitions(${TARGET_NAME}
PRIVATE SMGM_NO_CONSOLE
PRIVATE SMGM_NO_CONSOLE
)
endif()

if(SMGM_USE_DETOURS)
target_compile_definitions(${TARGET_NAME}
PRIVATE SMGM_USE_DETOURS
PRIVATE SMGM_USE_DETOURS
)
target_link_libraries(${TARGET_NAME}
PRIVATE detours
PRIVATE detours
)
add_dependencies(${TARGET_NAME} DetoursLib)
endif()

target_link_directories(${TARGET_NAME}
PRIVATE ${PROJECT_SOURCE_DIR}/lib
PRIVATE ${PROJECT_SOURCE_DIR}/lib
)
target_include_directories(${TARGET_NAME}
PRIVATE ${BOOST_INCLUDE_DIR}
PRIVATE ${SPDLOG_INCLUDE_DIR}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
PRIVATE ${BOOST_INCLUDE_DIR}
PRIVATE ${SPDLOG_INCLUDE_DIR}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
)
target_link_libraries(${TARGET_NAME}
PUBLIC fmt::fmt
PUBLIC spdlog::spdlog
PUBLIC Boost::boost
PUBLIC xinput
PUBLIC fmt::fmt
PUBLIC spdlog::spdlog
PUBLIC Boost::boost
PUBLIC xinput
)
52 changes: 52 additions & 0 deletions src/dll/config/ini_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "config/ini_config.hpp"

#include "utils/logging.h"

#include <boost/property_tree/ini_parser.hpp>

namespace smgm {
IniConfig::IniConfig() : m_path("smgm.ini") {}

IniConfig::IniConfig(const std::filesystem::path &path) : m_path(path) {
ReadFrom(path);
}

void IniConfig::SetConfigPath(const std::filesystem::path &path) {
m_path = path;
}

bool IniConfig::Read() { return ReadFrom(m_path); }

bool IniConfig::Write() { return WriteTo(m_path); }

bool IniConfig::ReadFrom(const std::filesystem::path &path) {
try {
read_ini(path.u8string(), m_config);
} catch (const std::exception &e) {
LOG_DEBUG(fmt::format("Failed to read config file: {}", e.what()));

return false;
}

WriteDefaultValues();

return true;
}

bool IniConfig::WriteTo(const std::filesystem::path &path) {
try {
write_ini(path.u8string(), m_config);
} catch (const std::exception &e) {
LOG_DEBUG(fmt::format("Failed to write config file: {}", e.what()));

return false;
}

return true;
}

void IniConfig::WriteDefaultValues() {
SetIfNotExists("SMGM.DisableGameShifting", true);
}

} // namespace smgm
64 changes: 64 additions & 0 deletions src/dll/config/ini_config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#pragma once

#include <filesystem>

#include <boost/property_tree/ptree.hpp>

namespace smgm {
class IniConfig {
public:
IniConfig();
explicit IniConfig(const std::filesystem::path &path);

[[nodiscard]] const std::filesystem::path &GetConfigPath() const {
return m_path;
}

void SetConfigPath(const std::filesystem::path &path);

bool Read();

bool Write();

bool ReadFrom(const std::filesystem::path &path);

bool WriteTo(const std::filesystem::path &path);

[[nodiscard]] const auto &GetConfig() const { return m_config; }

[[nodiscard]] auto &GetConfig() { return m_config; }

template <typename T, typename PathT>
IniConfig &Set(const PathT &key, const T &value) {
GetConfig().put(key, value);

return *this;
}

template <typename T, typename PathT>
IniConfig &SetIfNotExists(const PathT &key, const T &value) {
auto &config = GetConfig();

if (config.find(key) == config.not_found()) {
config.add(key, value);
}

return *this;
}

template <typename T, typename PathT>
boost::optional<T> GetOptional(const PathT &key) {
return GetConfig().get_optional<T>(key);
}

template <typename T, typename PathT> T Get(const PathT &key) {
return GetConfig().get<T>(key);
}

protected:
void WriteDefaultValues();

std::filesystem::path m_path;
boost::property_tree::ptree m_config;
};
} // namespace smgm
2 changes: 1 addition & 1 deletion src/dll/game_data/data_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class combine_combineTruckAction {
char pad_0000[48]; // 0x0000
class Vehicle *Veh; // 0x0030
float PowerCoef; // 0x0038
bool AutoGearSwitch; // 0x003C
bool IsInAutoMode; // 0x003C
char pad_003D[3]; // 0x003D
float WheelTurn; // 0x0040
float Accel; // 0x0044
Expand Down
43 changes: 38 additions & 5 deletions src/dll/game_data/game_data.cxx
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#include "game_data/game_data.h"

#include "config/ini_config.hpp"
#include "game_data/data_types.h"
#include "utils/format_helpers.h"
#include "utils/hooks.h"
#include "utils/logging.h"

#include <algorithm>

// void SMGM_HOOK_NAME(SwitchAWD)(Vehicle *veh, bool enabled) {
// SwitchAWD(veh, enabled);
// }

extern smgm::IniConfig g_IniConfig;

bool SMGM_HOOK_NAME(ShiftGear)(Vehicle *veh, std::int32_t gear) {
LOG_DEBUG(fmt::format("[ {} ] Switching gear: {} => {}", FormatPointer(veh),
veh->TruckAction->Gear_1, gear));
Expand All @@ -20,11 +26,38 @@ std::int32_t SMGM_HOOK_NAME(GetMaxGear)(const Vehicle *veh) {
}

void SMGM_HOOK_NAME(ShiftToAutoGear)(Vehicle *veh) {
LOG_DEBUG(fmt::format("[ {} ] Switching to Auto gear", FormatPointer(veh)));
if (g_IniConfig.Get<bool>("SMGM.DisableGameShifting")) {
return;
}

SMGM_CALL_ORIG_FN(ShiftToAutoGear, veh);
veh->TruckAction->IsInAutoMode = false;
}

bool SMGM_HOOK_NAME(ShiftToReverse)(Vehicle *veh) {
if (g_IniConfig.Get<bool>("SMGM.DisableGameShifting")) {
return false;
}

return SMGM_CALL_ORIG_FN(ShiftToReverse, veh);
}

bool SMGM_HOOK_NAME(ShiftToNeutral)(Vehicle *veh) {
if (g_IniConfig.Get<bool>("SMGM.DisableGameShifting")) {
return false;
}

return SMGM_CALL_ORIG_FN(::ShiftToNeutral, veh);
}

bool SMGM_HOOK_NAME(ShiftToHigh)(Vehicle *veh) { return false; }

bool SMGM_HOOK_NAME(DisableAutoAndShift)(Vehicle *veh, std::int32_t gear) {
if (g_IniConfig.Get<bool>("SMGM.DisableGameShifting")) {
return false;
}

SMGM_CALL_ORIG_FN(::ShiftToAutoGear, veh);
veh->TruckAction->AutoGearSwitch = false;
SMGM_CALL_HOOK(ShiftGear, veh, 1);
return SMGM_CALL_ORIG_FN(DisableAutoAndShift, veh, gear);
}

void SMGM_HOOK_NAME(SetPowerCoef)(Vehicle *veh, float coef) {
Expand All @@ -38,6 +71,6 @@ void SMGM_HOOK_NAME(SetCurrentVehicle)(combine_TRUCK_CONTROL *truckCtrl,
LOG_DEBUG(fmt::format("Current vehicle changed to {}", FormatPointer(veh)));

if (veh) {
veh->TruckAction->AutoGearSwitch = false;
veh->TruckAction->IsInAutoMode = false;
}
}
19 changes: 12 additions & 7 deletions src/dll/game_data/game_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ inline const float PowerCoefLowGear = .45f;
inline const float PowerCoefLowPlusGear = 1.f;
inline const float PowerCoefLowMinusGear = .2f;

SMGM_DECLARE_PTR(0x29AB158, combine_TRUCK_CONTROL *, TruckControlPtr);
SMGM_DECLARE_PTR(0x29AF898, combine_TRUCK_CONTROL *, TruckControlPtr);
} // namespace GameRelatedData

// SMGM_GAME_FUNCTION(0xD5D0B0, void, SwitchAWD, Vehicle *, bool);
SMGM_GAME_FUNCTION(0xD511C0, bool, ShiftGear, Vehicle *, std::int32_t);
SMGM_GAME_FUNCTION(0xD50F20, std::int32_t, GetMaxGear, const Vehicle *);
SMGM_GAME_FUNCTION(0xD50F70, void, ShiftToAutoGear, Vehicle *);
SMGM_GAME_FUNCTION(0xD50360, void, SetPowerCoef, Vehicle *, float);
SMGM_GAME_FUNCTION(0xAC9830, void, SetCurrentVehicle, combine_TRUCK_CONTROL *,
Vehicle *);
SMGM_GAME_FUNCTION(0xD51A50, bool, ShiftGear, Vehicle *, std::int32_t);
SMGM_GAME_FUNCTION(0xD517B0, std::int32_t, GetMaxGear, const Vehicle *);
SMGM_GAME_FUNCTION(0xD51800, void, ShiftToAutoGear, Vehicle *);
SMGM_GAME_FUNCTION(0xB5A9C0, bool, ShiftToReverse, Vehicle *);
SMGM_GAME_FUNCTION(0xB5A700, bool, ShiftToNeutral, Vehicle *);
SMGM_GAME_FUNCTION(0xB5A5E0, bool, ShiftToHigh, Vehicle *);
SMGM_GAME_FUNCTION(0xD554F0, bool, DisableAutoAndShift, Vehicle *,
std::int32_t);
SMGM_GAME_FUNCTION(0xD50BF0, void, SetPowerCoef, Vehicle *, float);
SMGM_GAME_FUNCTION(0xAC9F30, void, SetCurrentVehicle, combine_TRUCK_CONTROL *,
Vehicle *);
5 changes: 3 additions & 2 deletions src/dll/game_data/vehicle.cxx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "config/ini_config.hpp"
#include "game_data/data_types.h"
#include "game_data/game_data.h"
#include "utils/hooks.h"
Expand All @@ -11,9 +12,9 @@ std::int32_t Vehicle::GetMaxGear() const {
}

bool Vehicle::ShiftToGear(std::int32_t targetGear, float powerCoef) {
std::int32_t gear = std::clamp(targetGear, -1, GetMaxGear() + 1);

const std::int32_t gear = std::clamp(targetGear, -1, GetMaxGear() + 1);
bool bSwitched = SMGM_CALL_HOOK(ShiftGear, this, gear);

if (bSwitched) {
SetPowerCoef(powerCoef);
}
Expand Down
Loading

0 comments on commit ef9cc8a

Please sign in to comment.