Welcome to hackvshack.net Forum!
Download Free HvH CS2/CS:GO Cheats, CFG, LUA/JS Scripts, and More!
Register

Backend Beginner Source Engine How to Retrieve Interfaces in CS2

yowi0

yowio
User ID:
5497
Messages:
20
Reactions:
21
Badges:
11
REP:
−0/5+
Level:
81

๐Ÿค” What Are Interfaces in CS2?​

Interfaces in CS2 are a way to interact with different parts of the game engine. They provide functions that allow us to access and modify game behavior. Each interface represents a different system in the game, such as:
  • EngineClient - Handles interactions with the game engine.
  • MaterialSystem - Controls materials and rendering.
To use these interfaces, we need a way to retrieve them from the gameโ€™s memory, which is what we will implement here.

๐Ÿ”จ Step 1: Creating the Interface Register Structure​

The game stores a linked list of interfaces in memory. Each interface is registered using a structure. Our first step is to define this structure:
Interface Register Structure:
Expand Collapse Copy
struct InterfaceReg {
    std::add_pointer_t<uintptr_t()> createFn;
    const char* name;
    InterfaceReg* next;
};

๐Ÿ” Explanation:​

  • createFn: This is a function pointer that, when called, returns a pointer to the interface instance.
  • name: Stores the name of the interface (e.g., "GameResourceServiceClientV001").
  • next: Points to the next Interface Register in the linked list.
This structure allows us to navigate through the list of available interfaces.

๐Ÿงพ Step 2: Defining the GetInterface Function​

To retrieve an interface, we need to:
  1. Get the module (DLL) where the interface is stored.
  2. Locate the CreateInterface function.
  3. Locate the interface list and iterate through it to find the one we need.
Here is the function to do this:
GetInterface Function:
Expand Collapse Copy
constexpr int CREATE_INTERFACE_OFFSET = 3;
constexpr int LIST_OFFSET = 7;

uintptr_t GetInterface(const char* moduleName, const char* interfaceName) {
    uintptr_t res = 0;

    // Step 1: Get the module (DLL) where the interface is stored.
    HMODULE moduleBase = GetModuleHandleA(moduleName);
    if (!moduleBase) return res;

    // Step 2: Locate the CreateInterface function.
    using CreateInterfaceFn = void* (*)(const char*, int*);
    const auto createInterface = reinterpret_cast<CreateInterfaceFn>(GetProcAddress(moduleBase, "CreateInterface"));

    // Step 3: Locate the interface list and iterate through it to find the one we need.
    const uintptr_t list = reinterpret_cast<uintptr_t>(createInterface) + *reinterpret_cast<int32_t*>(reinterpret_cast<uintptr_t>(createInterface) + CREATE_INTERFACE_OFFSET) + LIST_OFFSET;
    for (const InterfaceReg* current = *reinterpret_cast<InterfaceReg**>(list); current; current = current->next)
    {
        if (std::string_view(current->name).find(interfaceName) != std::string_view::npos)
        {
            res = current->createFn();
        }
    }

    return res;
}

๐Ÿ” Breakdown of Steps:​

  1. Retrieve the Module: GetModuleHandleA(moduleName) gets a handle to the DLL (e.g., "engine2.dll").
  2. Find CreateInterface: GetProcAddress(moduleBase, "CreateInterface") retrieves the function used to obtain interfaces.
  3. Locate the Interface List: We cast CreateInterface into a pointer and find the linked list of InterfaceReg.
  4. Search for the Desired Interface: We loop through the list, comparing names to find the one we need.
  5. Return the Interface Pointer: Once found, we return it by calling createFn().

๐Ÿš€ Step 3: Using an Interface in CS2​

Once we have our GetInterface function, we can retrieve and use any interface. For example, to get the GameResourceService interface:

Using GetInterface Function:
Expand Collapse Copy
    uintptr_t gameResourceService = GetInterface("engine2.dll", "GameResourceServiceClientV001");

    if (gameResourceService) {
        std::cout << "Successfully retrieved GameResourceService!\n";
    }
    else {
        std::cerr << "Failed to retrieve GameResourceService.\n";
    }

๐Ÿ” Explanation:​

  • We call GetInterface("engine2.dll", "GameResourceServiceClientV001").
  • If successful, gameResourceService will hold a pointer to the GameResourceService interface.
  • We can now cast it to the correct type and use it to interact with it.

And that should be it! Thank you for reading, and good luck!
 
Last edited:

Who has read this thread (Total: 0) in last 1 hours View details