Skip to content

Commit

Permalink
Add SDL pattern finder to PatternFinders struct
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed May 18, 2024
1 parent 665688e commit 19ed696
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 31 deletions.
28 changes: 8 additions & 20 deletions Source/GlobalContext/GlobalContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,17 @@

class GlobalContext {
public:
template <typename SdlPatterns>
explicit GlobalContext(std::span<std::byte> memoryStorage, SdlDll sdlDll, SdlPatterns sdlPatterns) noexcept
explicit GlobalContext(std::span<std::byte> memoryStorage, DynamicLibrary clientDLL, DynamicLibrary panoramaDLL, SdlDll sdlDll) noexcept
: _freeRegionList{memoryStorage}
, deferredCompleteContext{sdlDll, sdlPatterns}
, deferredCompleteContext{clientDLL, panoramaDLL, sdlDll}
{
}

static void initializeInstance() noexcept
{
if (!globalContext.isInitialized()) {
alignas(FreeMemoryRegionList::minimumAlignment()) static constinit std::byte storage[build::MEMORY_CAPACITY];
const SdlDll sdlDll;
const PatternFinder<PatternNotFoundLogger> sdlPatternFinder{sdlDll.getCodeSection().raw()};
globalContext.initialize(storage, sdlDll, SdlPatterns<PatternFinder<PatternNotFoundLogger>>{sdlPatternFinder});
globalContext.initialize(storage, DynamicLibrary{cs2::CLIENT_DLL}, DynamicLibrary{cs2::PANORAMA_DLL}, SdlDll{});
globalContext->deferredCompleteContext.partial().enableIfValid();
}
}
Expand Down Expand Up @@ -67,22 +64,13 @@ class GlobalContext {
if (deferredCompleteContext.isComplete())
return;

const DynamicLibrary panoramaDLL{cs2::PANORAMA_DLL};
const DynamicLibrary clientDLL{cs2::CLIENT_DLL};

const PatternFinder<PatternNotFoundLogger> clientPatternFinder{clientDLL.getCodeSection().raw()};
const PatternFinder<PatternNotFoundLogger> panoramaPatternFinder{panoramaDLL.getCodeSection().raw()};
const PatternFinder<PatternNotFoundLogger> soundSystemPatternFinder{DynamicLibrary{cs2::SOUNDSYSTEM_DLL}.getCodeSection().raw()};
const PatternFinder<PatternNotFoundLogger> fileSystemPatternFinder{DynamicLibrary{cs2::FILESYSTEM_DLL}.getCodeSection().raw()};
const PatternFinder<PatternNotFoundLogger> tier0PatternFinder{DynamicLibrary{cs2::TIER0_DLL}.getCodeSection().raw()};

const PatternFinders patternFinders{clientPatternFinder, tier0PatternFinder, soundSystemPatternFinder, fileSystemPatternFinder, panoramaPatternFinder};
const auto partialContext = deferredCompleteContext.partial();

deferredCompleteContext.makeComplete(
deferredCompleteContext.partial().peepEventsHook,
clientDLL,
panoramaDLL,
MemoryPatterns{patternFinders}
partialContext.peepEventsHook,
partialContext.clientDLL,
partialContext.panoramaDLL,
MemoryPatterns{partialContext.patternFinders}
);

fullContext().panoramaGUI.init(fullContext().getFeatureHelpers().mainMenuProvider);
Expand Down
19 changes: 16 additions & 3 deletions Source/GlobalContext/PartialGlobalContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,30 @@

#include <CS2/Constants/DllNames.h>
#include <Hooks/PeepEventsHook.h>
#include <MemoryPatterns/MemoryPatterns.h>
#include <MemoryPatterns/PatternFinders.h>
#include <Platform/DynamicLibrary.h>
#include <Platform/Macros/PlatformSpecific.h>
#include <SDL/SdlDll.h>

struct PartialGlobalContext {
template <typename SdlPatterns>
explicit PartialGlobalContext(SdlDll sdlDll, SdlPatterns sdlPatterns) noexcept
: peepEventsHook{sdlPatterns.peepEventsPointer(sdlDll.peepEvents())}
explicit PartialGlobalContext(DynamicLibrary clientDLL, DynamicLibrary panoramaDLL, SdlDll sdlDLL) noexcept
: patternFinders{
.clientPatternFinder{clientDLL.getCodeSection().raw()},
.tier0PatternFinder{DynamicLibrary{cs2::TIER0_DLL}.getCodeSection().raw()},
.soundSystemPatternFinder{DynamicLibrary{cs2::SOUNDSYSTEM_DLL}.getCodeSection().raw()},
.fileSystemPatternFinder{DynamicLibrary{cs2::FILESYSTEM_DLL}.getCodeSection().raw()},
.panoramaPatternFinder{panoramaDLL.getCodeSection().raw()},
.sdlPatternFinder{sdlDLL.getCodeSection().raw()}}
, clientDLL{clientDLL}
, panoramaDLL{panoramaDLL}
, peepEventsHook{MemoryPatterns{patternFinders}.sdlPatterns().peepEventsPointer(sdlDLL.peepEvents())}
{
}

PatternFinders patternFinders;
DynamicLibrary clientDLL;
DynamicLibrary panoramaDLL;
PeepEventsHook peepEventsHook;

void enableIfValid() noexcept
Expand Down
7 changes: 3 additions & 4 deletions Source/MemoryPatterns/Linux/SdlPatternsLinux.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
#include <MemorySearch/BytePatternLiteral.h>
#include <SDL/SdlFunctions.h>

template <typename SdlPatternFinder>
template <typename PatternFinders>
struct SdlPatterns {
// FIXME: use PatternFinders, requires change to GlobalContext initialization
const SdlPatternFinder& sdlPatternFinder;
const PatternFinders& patternFinders;

[[nodiscard]] sdl3::SDL_PeepEvents** peepEventsPointer(sdl3::SDL_PeepEvents* peepEvents) const noexcept
{
return sdlPatternFinder.matchPatternAtAddress((void*)peepEvents, "FF 25 ? ? ? ?"_pat).add(2).abs().template as<sdl3::SDL_PeepEvents**>();
return patternFinders.sdlPatternFinder.matchPatternAtAddress((void*)peepEvents, "FF 25 ? ? ? ?"_pat).add(2).abs().template as<sdl3::SDL_PeepEvents**>();
}
};
1 change: 1 addition & 0 deletions Source/MemoryPatterns/MemoryPatterns.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct MemoryPatterns {
MEMORY_PATTERNS(PlantedC4Patterns, plantedC4Patterns)
MEMORY_PATTERNS(PlayerControllerPatterns, playerControllerPatterns)
MEMORY_PATTERNS(PlayerPawnPatterns, playerPawnPatterns)
MEMORY_PATTERNS(SdlPatterns, sdlPatterns)
MEMORY_PATTERNS(SoundSystemPatterns, soundSystemPatterns)
MEMORY_PATTERNS(TopLevelWindowPatterns, topLevelWindowPatterns)
MEMORY_PATTERNS(WeaponServicesPatterns, weaponServicesPatterns)
Expand Down
1 change: 1 addition & 0 deletions Source/MemoryPatterns/PatternFinders.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ struct PatternFinders {
PatternFinder<PatternNotFoundLogger> soundSystemPatternFinder;
PatternFinder<PatternNotFoundLogger> fileSystemPatternFinder;
PatternFinder<PatternNotFoundLogger> panoramaPatternFinder;
PatternFinder<PatternNotFoundLogger> sdlPatternFinder;
};
7 changes: 3 additions & 4 deletions Source/MemoryPatterns/Windows/SdlPatternWindows.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
#include <MemorySearch/BytePatternLiteral.h>
#include <SDL/SdlFunctions.h>

template <typename SdlPatternFinder>
template <typename PatternFinders>
struct SdlPatterns {
// FIXME: use PatternFinders, requires change to GlobalContext initialization
const SdlPatternFinder& sdlPatternFinder;
const PatternFinders& patternFinders;

[[nodiscard]] sdl3::SDL_PeepEvents** peepEventsPointer(sdl3::SDL_PeepEvents* peepEvents) const noexcept
{
return sdlPatternFinder.matchPatternAtAddress((void*)peepEvents, "48 FF 25 ? ? ? ?"_pat).add(3).abs().template as<sdl3::SDL_PeepEvents**>();
return patternFinders.sdlPatternFinder.matchPatternAtAddress((void*)peepEvents, "48 FF 25 ? ? ? ?"_pat).add(3).abs().template as<sdl3::SDL_PeepEvents**>();
}
};

0 comments on commit 19ed696

Please sign in to comment.