diff --git a/src/Ext/Techno/Body.Update.cpp b/src/Ext/Techno/Body.Update.cpp index 18a9eab8c9..22425852fa 100644 --- a/src/Ext/Techno/Body.Update.cpp +++ b/src/Ext/Techno/Body.Update.cpp @@ -738,14 +738,14 @@ void TechnoExt::KillSelf(TechnoClass* pThis, AutoDeathBehavior deathOption, Anim } default: //must be AutoDeathBehavior::Kill - if (IS_ARES_FUN_AVAILABLE(SpawnSurvivors)) + if (AresFunctions::SpawnSurvivors) { switch (pThis->WhatAmI()) { case AbstractType::Unit: case AbstractType::Aircraft: AresFunctions::SpawnSurvivors(static_cast(pThis), nullptr, false, false); - default:break; + default:; } } pThis->ReceiveDamage(&pThis->Health, 0, RulesClass::Instance->C4Warhead, nullptr, true, false, pThis->Owner); diff --git a/src/Ext/Techno/Body.cpp b/src/Ext/Techno/Body.cpp index 1bf745dd39..dd34bf636a 100644 --- a/src/Ext/Techno/Body.cpp +++ b/src/Ext/Techno/Body.cpp @@ -225,7 +225,7 @@ bool TechnoExt::AllowedTargetByZone(TechnoClass* pThis, TechnoClass* pTarget, Ta // BTW, who said it was merely a Type pointer replacement and he could make a better one than Ares? bool TechnoExt::ConvertToType(FootClass* pThis, TechnoTypeClass* pToType) { - if (IS_ARES_FUN_AVAILABLE(ConvertTypeTo)) + if (AresFunctions::ConvertTypeTo) return AresFunctions::ConvertTypeTo(pThis, pToType); // In case not using Ares 3.0. Only update necessary vanilla properties AbstractType rtti; diff --git a/src/Ext/TechnoType/Hooks.Drawing.cpp b/src/Ext/TechnoType/Hooks.Drawing.cpp index 304bedb61c..1422cf9c20 100644 --- a/src/Ext/TechnoType/Hooks.Drawing.cpp +++ b/src/Ext/TechnoType/Hooks.Drawing.cpp @@ -288,7 +288,7 @@ DEFINE_HOOK(0x73C47A, UnitClass_DrawAsVXL_Shadow, 0x5) { if (pType->NoSpawnAlt && pThis->SpawnManager && pThis->SpawnManager->CountDockedSpawns() == 0) { - if (CAN_USE_ARES && AresHelper::CanUseAres) + if (AresHelper::CanUseAres) { vxl_index_key.Invalidate();// I'd just assume most of the time we have spawn return &reinterpret_cast(pType->align_2FC)->NoSpawnAltVXL; @@ -373,7 +373,7 @@ DEFINE_HOOK(0x73C47A, UnitClass_DrawAsVXL_Shadow, 0x5) if (idx < 18) return &pType->ChargerTurrets[idx]; - if (CAN_USE_ARES && AresHelper::CanUseAres) + if (AresHelper::CanUseAres) { auto* aresTypeExt = reinterpret_cast(pType->align_2FC); return &aresTypeExt->ChargerTurrets[idx - 18]; @@ -390,7 +390,7 @@ DEFINE_HOOK(0x73C47A, UnitClass_DrawAsVXL_Shadow, 0x5) if (idx < 18) return &pType->ChargerBarrels[idx]; - if (CAN_USE_ARES && AresHelper::CanUseAres) + if (AresHelper::CanUseAres) { auto* aresTypeExt = reinterpret_cast(pType->align_2FC); return &aresTypeExt->ChargerBarrels[idx - 18]; diff --git a/src/Phobos.h b/src/Phobos.h index 720f48e95a..9ba1a63879 100644 --- a/src/Phobos.h +++ b/src/Phobos.h @@ -4,8 +4,6 @@ #include -#define CAN_USE_ARES 1 - class CCINIClass; class AbstractClass; diff --git a/src/Utilities/AresAddressTable.cpp b/src/Utilities/AresAddressTable.cpp index d7093860ee..5c7ea1b2e7 100644 --- a/src/Utilities/AresAddressTable.cpp +++ b/src/Utilities/AresAddressTable.cpp @@ -1,24 +1,35 @@ #pragma once #include "AresFunctions.h" +#include "AresHelper.h" +#include "Patch.h" +#define NOTE_ARES_FUN(name,reladdr) AresFunctions::name = reinterpret_cast(AresHelper::AresBaseAddress + reladdr) -const AresHelper::AresVersionFunctionMap AresHelper::AresFunctionOffsets = +decltype(AresFunctions::ConvertTypeTo) AresFunctions::ConvertTypeTo = nullptr; +decltype(AresFunctions::SpawnSurvivors) AresFunctions::SpawnSurvivors = nullptr; + +void AresFunctions::InitAres3_0() { + NOTE_ARES_FUN(ConvertTypeTo, 0x043650); + if constexpr (AresFunctions::AresWasWrongAboutSpawnSurvivors) { - AresHelper::Version::Ares30, - { - { ARES_FUN(ConvertTypeTo), 0x043650 }, - { ARES_FUN(SpawnSurvivors), 0x0464C0 }, - { ARES_FUN(HasFactory), 0x0217C0 }, - { ARES_FUN(CanBeBuiltAt), 0x03E3B0 }, - } - }, + Patch::Apply_RAW(AresHelper::AresBaseAddress + 0x4C0EB, { 0x5C }); + Patch::Apply_RAW(AresHelper::AresBaseAddress + 0x48C69, { 0x30 }); + } + else + NOTE_ARES_FUN(SpawnSurvivors, 0x0464C0); +} + +void AresFunctions::InitAres3_0p1() +{ + NOTE_ARES_FUN(ConvertTypeTo, 0x044130); + if constexpr (AresFunctions::AresWasWrongAboutSpawnSurvivors) { - AresHelper::Version::Ares30p, - { - { ARES_FUN(ConvertTypeTo), 0x044130 }, - { ARES_FUN(SpawnSurvivors), 0x047030 }, - { ARES_FUN(HasFactory), 0x0217C0 }, - { ARES_FUN(CanBeBuiltAt), 0x03E3B0 }, - } - }, -}; + Patch::Apply_RAW(AresHelper::AresBaseAddress + 0x4CD4B, { 0x5C }); + Patch::Apply_RAW(AresHelper::AresBaseAddress + 0x498B9, { 0x30 }); + } + else + NOTE_ARES_FUN(SpawnSurvivors, 0x047030); +} + +#undef NOTE_ARES_FUN + diff --git a/src/Utilities/AresFunctions.h b/src/Utilities/AresFunctions.h index 583d734320..7aecf0bef5 100644 --- a/src/Utilities/AresFunctions.h +++ b/src/Utilities/AresFunctions.h @@ -1,53 +1,22 @@ #pragma once -#include "AresHelper.h" - -#include -#include -#include - class TechnoClass; class TechnoTypeClass; class FootClass; class HouseClass; class BuildingTypeClass; -class AresTechnoTypeExt -{ -public: - __declspec(noinline) bool __thiscall CanBeBuiltAt(BuildingTypeClass* pBuildingType) { return 0; } -}; - - class AresFunctions { public: -#define CALL_ARES(name, ...) std::invoke(reinterpret_cast(AresHelper::AresFunctionOffsetsFinal[ARES_FUN(name)]), __VA_ARGS__) -#define THIS_CALL_ARES(name, ...) std::invoke(*reinterpret_cast(AresHelper::AresFunctionOffsetsFinal[ARES_FUN_M(name)]), __VA_ARGS__) - - // ??? - static bool ConvertTypeTo(TechnoClass* pFoot, TechnoTypeClass* pConvertTo) - { - return CALL_ARES(ConvertTypeTo, pFoot, pConvertTo); - } - + static void InitAres3_0(); + static void InitAres3_0p1(); // TechnoExt - static void SpawnSurvivors(FootClass* const pThis, TechnoClass* const pKiller, const bool Select, const bool IgnoreDefenses) - { - CALL_ARES(SpawnSurvivors, pThis, pKiller, Select, IgnoreDefenses); - } - - // HouseExt - static int HasFactory(int buffer, HouseClass* pOwner, TechnoTypeClass* pType, bool skipAircraft, bool requirePower, bool checkCanBuild, bool unknown) - { - return CALL_ARES(HasFactory, buffer, pOwner, pType, skipAircraft, requirePower, checkCanBuild, unknown); - } + static bool(__stdcall* ConvertTypeTo)(TechnoClass* pFoot, TechnoTypeClass* pConvertTo); - // TechnoTypeExt - static bool CanBeBuiltAt(AresTechnoTypeExt* pExt, BuildingTypeClass* pBuildingType) - { - return THIS_CALL_ARES(&AresTechnoTypeExt::CanBeBuiltAt, pExt, pBuildingType); - } + static void(__stdcall* SpawnSurvivors)(FootClass* pThis, TechnoClass* pKiller, bool Select, bool IgnoreDefenses); +private: + static constexpr bool _maybe = false; -#undef CALL_ARES -#undef THIS_CALL_ARES + static constexpr bool AresWasWrongAboutSpawnSurvivors = _maybe; + static constexpr bool AresWasWrongAboutAduction = true; }; diff --git a/src/Utilities/AresHelper.cpp b/src/Utilities/AresHelper.cpp index 0b180588c6..409bd2226b 100644 --- a/src/Utilities/AresHelper.cpp +++ b/src/Utilities/AresHelper.cpp @@ -1,5 +1,5 @@ #include "AresHelper.h" - +#include "AresFunctions.h" #include #include #include @@ -15,7 +15,6 @@ uintptr_t AresHelper::AresBaseAddress = 0x0; HMODULE AresHelper::AresDllHmodule = nullptr; AresHelper::Version AresHelper::AresVersion = AresHelper::Version::Unknown; bool AresHelper::CanUseAres = false; -AresHelper::AresFunctionMap AresHelper::AresFunctionOffsetsFinal; const AresHelper::AresTimestampMap AresHelper::AresTimestampBytes = { @@ -96,22 +95,14 @@ void AresHelper::Init() { case Version::Ares30: Debug::LogDeferred("[Phobos] Detected Ares 3.0.\n"); + AresFunctions::InitAres3_0(); break; case Version::Ares30p: Debug::LogDeferred("[Phobos] Detected Ares 3.0p1.\n"); + AresFunctions::InitAres3_0p1(); break; default: Debug::LogDeferred("[Phobos] Detected a version of Ares that is not supported by Phobos. Disabling integration.\n"); break; } - - constexpr const wchar_t* ARES_DLL = L"Ares.dll"; - if (CanUseAres && GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN, ARES_DLL, &AresDllHmodule)) - { - for (auto x : AresFunctionOffsets.at(AresVersion)) - if (x.second > 0) - AresFunctionOffsetsFinal[x.first] = AresBaseAddress + x.second; - else - AresFunctionOffsetsFinal[x.first] = 0; - } } diff --git a/src/Utilities/AresHelper.h b/src/Utilities/AresHelper.h index 7d7e28c125..07837b8765 100644 --- a/src/Utilities/AresHelper.h +++ b/src/Utilities/AresHelper.h @@ -4,10 +4,6 @@ #include #include -#define ARES_FUN(name) std::string(NAMEOF(AresFunctions::name)) -#define ARES_FUN_M(name) std::string(NAMEOF_MEMBER(name)) -#define IS_ARES_FUN_AVAILABLE(name) AresHelper::CanUseAres && (AresHelper::AresFunctionOffsetsFinal[ARES_FUN(name)] != 0) - class AresHelper { public: @@ -31,13 +27,6 @@ class AresHelper static uintptr_t AresBaseAddress; static uintptr_t PhobosBaseAddress; - typedef std::unordered_map AresFunctionMap; - typedef std::unordered_map AresVersionFunctionMap; - - // offsets of function addresses for each version - static const AresVersionFunctionMap AresFunctionOffsets; - // storage for absolute addresses of functions (module base + offset) - static AresFunctionMap AresFunctionOffsetsFinal; // numeric id of currently used version, zero-indexed, -1 is unknown or missing static Version AresVersion; // is Ares detected and version known?