update readme

This commit is contained in:
SlynxCZ 2026-01-30 10:31:43 +04:00
parent 923edbbba0
commit 8e0e61acd1

145
README.md
View file

@ -1,23 +1,27 @@
# Ray-Trace # Ray-Trace
**Shared ray tracing interface for Metamod:Source & CounterStrikeSharp plugins** **Shared ray tracing interface for Metamod:Source & CounterStrikeSharp
plugins**
------------------------------------------------------------------------ ------------------------------------------------------------------------
## Overview ## Overview
`Ray-Trace` is a lightweight **Metamod interface module** for `Ray-Trace` is a lightweight **Metamod interface module** for\
**Counter-Strike 2** servers. **Counter-Strike 2** servers.
It exposes a shared interface: `CRayTraceInterface001` which can be consumed from: It exposes a shared interface: `CRayTraceInterface001` which can be
consumed from:
- Native **Metamod C++ plugins** - Native **Metamod C++ plugins**
- Managed **CounterStrikeSharp C# plugins** - Managed **CounterStrikeSharp C# plugins**
The interface is implemented as a **C++ virtual class** and is accessed The interface is implemented as a **C++ virtual class** and is accessed
from C# by calling its **vtable functions directly** using a native handle. from C# by calling its **vtable functions directly** using a native
handle.
The goal is to provide a **single tracing backend** usable from both worlds The goal is to provide a **single tracing backend** usable from both
worlds\
without duplicating engine detours. without duplicating engine detours.
------------------------------------------------------------------------ ------------------------------------------------------------------------
@ -69,13 +73,14 @@ public:
) = 0; ) = 0;
}; };
``` ```
**Return value:**
- true → trace hit something, TraceResult is valid **Return value:** - true → trace hit something, TraceResult is valid\
- false → no hit - false → no hit
------------------------------------------------------------------------ ------------------------------------------------------------------------
## Getting the interface ## Getting the interface
**C++ (Metamod plugin)** **C++ (Metamod plugin)**
``` cpp ``` cpp
@ -102,6 +107,7 @@ bool LoadRayTrace()
``` ```
**C# (CounterStrikeSharp plugin)** **C# (CounterStrikeSharp plugin)**
``` csharp ``` csharp
private nint g_pRayTraceHandle = nint.Zero; private nint g_pRayTraceHandle = nint.Zero;
private bool g_bRayTraceLoaded = false; private bool g_bRayTraceLoaded = false;
@ -119,12 +125,16 @@ public override void Load(bool hotReload)
g_bRayTraceLoaded = true; g_bRayTraceLoaded = true;
} }
``` ```
The returned handle is a pointer to the native CRayTraceInterface object.
The returned handle is a pointer to the native CRayTraceInterface
object.
------------------------------------------------------------------------ ------------------------------------------------------------------------
## Calling methods from C++ (Metamod) ## Calling methods from C++ (Metamod)
**TraceShape example** **TraceShape example**
``` cpp ``` cpp
Vector vecOrigin{}; Vector vecOrigin{};
QAngle angView{}; QAngle angView{};
@ -156,6 +166,7 @@ if (g_pRayTrace && g_bRayTraceLoaded)
``` ```
**TraceEndShape example** **TraceEndShape example**
``` cpp ``` cpp
TraceResult traceResult{}; TraceResult traceResult{};
@ -169,6 +180,7 @@ bool bHit = g_pRayTrace->TraceEndShape(
``` ```
**TraceShapeEx (low-level)** **TraceShapeEx (low-level)**
``` cpp ``` cpp
Ray_t ray{}; Ray_t ray{};
CTraceFilter filter( CTraceFilter filter(
@ -191,6 +203,7 @@ bool bHit = g_pRayTrace->TraceShapeEx(
------------------------------------------------------------------------ ------------------------------------------------------------------------
## Calling methods from C# (CounterStrikeSharp plugin) ## Calling methods from C# (CounterStrikeSharp plugin)
``` csharp ``` csharp
private delegate bool TraceShapeFn( private delegate bool TraceShapeFn(
nint pThis, nint pThis,
@ -235,11 +248,79 @@ public bool TraceShape(
------------------------------------------------------------------------ ------------------------------------------------------------------------
## Memory allocation from C# (Important)
When calling `TraceShape` or `TraceEndShape` from C#, the plugin **must
allocate native memory** for the following structures:
- `TraceOptions`
- `TraceResult`
These parameters are native pointers in C++ (`TraceOptions*` and
`TraceResult*`) and must remain valid for the duration of the call.
The recommended and safest approach is using **stackalloc** (or unsafe
stack variables) to provide native memory on the stack.
Failing to allocate valid memory for these parameters will result in
crashes or undefined behavior.
### Example (C# stackalloc)
``` csharp
[StructLayout(LayoutKind.Sequential, Pack = 8)]
public struct TraceOptions
{
public ulong InteractsWith;
public ulong InteractsExclude;
public int DrawBeam;
}
[StructLayout(LayoutKind.Sequential, Pack = 8)]
public struct TraceResult
{
public Vector EndPos;
public nint HitEntity;
public float Fraction;
public int AllSolid;
public Vector Normal;
}
unsafe
{
Vector origin = player.Position;
QAngle angles = player.ViewAngles;
TraceOptions* opts = stackalloc TraceOptions[1];
opts->InteractsWith = (ulong)MASK_SHOT_FULL;
opts->InteractsExclude = 0;
opts->DrawBeam = 0;
TraceResult* result = stackalloc TraceResult[1];
bool hit = _traceShape!(
g_pRayTraceHandle,
(nint)&origin,
(nint)&angles,
nint.Zero,
(nint)opts,
(nint)result
);
if (hit)
{
Console.WriteLine($"Hit at: {result->EndPos}");
}
}
```
------------------------------------------------------------------------
## Low-level usage from C# (Ray_t & CTraceFilter) ## Low-level usage from C# (Ray_t & CTraceFilter)
When using the low-level method `TraceShapeEx` from a C# plugin, the plugin When using the low-level method `TraceShapeEx` from a C# plugin, the
must provide its own native-compatible implementations of the following plugin must provide its own native-compatible implementations of the
engine structures: following engine structures:
- `Ray_t` - `Ray_t`
- `CTraceFilter` - `CTraceFilter`
@ -249,8 +330,8 @@ must be recreated in managed code with correct memory layout.
### Ray_t (C#) ### Ray_t (C#)
The C# plugin must define a struct that matches the native `Ray_t` layout The C# plugin must define a struct that matches the native `Ray_t`
used by the engine. layout used by the engine.
Example (simplified): Example (simplified):
@ -264,18 +345,20 @@ public struct Ray_t
public byte m_IsSwept; public byte m_IsSwept;
} }
``` ```
(Exact layout depends on the engine version and must match native memory.)
**CTraceFilter (C#)** (Exact layout depends on the engine version and must match native
memory.)
**CTraceFilter (C#)**\
For `CTraceFilter`, the plugin must: For `CTraceFilter`, the plugin must:
- Define a managed struct matching the native layout. - Define a managed struct matching the native layout.
- Resolve the CTraceFilter vtable pointer using a signature scan. - Resolve the CTraceFilter vtable pointer using a signature scan.
- Assign the resolved vtable to the struct before calling TraceShapeEx. - Assign the resolved vtable to the struct before calling
TraceShapeEx.
This is required because CTraceFilter is a polymorphic C++ class and its
virtual table is not directly exposed to managed code.
Example concept: Example concept:
``` csharp ``` csharp
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public unsafe struct CTraceFilter public unsafe struct CTraceFilter
@ -285,19 +368,18 @@ public unsafe struct CTraceFilter
public T ...; public T ...;
} }
``` ```
The vtable pointer must be resolved at runtime using a signature scan
against the game binary.
**Important notes** **Important notes** - This setup is only required when using the
- This setup is only required when using the low-level API: low-level API: - TraceShapeEx(...) - High-level functions (TraceShape,
- TraceShapeEx(...) TraceEndShape) do not require custom Ray_t or CTraceFilter handling from
- High-level functions (TraceShape, TraceEndShape) do not require custom Ray_t or CTraceFilter handling from C#. C#. - Incorrect structure layout or invalid vtable resolution will
- Incorrect structure layout or invalid vtable resolution will result in crashes or undefined behavior. result in crashes or undefined behavior. - This is considered an
- This is considered an advanced use case intended for engine-level plugins. advanced use case intended for engine-level plugins.
------------------------------------------------------------------------ ------------------------------------------------------------------------
## Notes about ABI & Destructor ## Notes about ABI & Destructor
- CRayTraceInterface has a virtual destructor. - CRayTraceInterface has a virtual destructor.
- The object is owned by the Ray-Trace Metamod module. - The object is owned by the Ray-Trace Metamod module.
- Plugins must never call delete on the interface pointer. - Plugins must never call delete on the interface pointer.
@ -305,10 +387,13 @@ against the game binary.
- All parameters are passed as native pointers (nint). - All parameters are passed as native pointers (nint).
# Build # Build
## Requirements ## Requirements
- HL2SDK-CS2 - HL2SDK-CS2
- Metamod:Source - Metamod:Source
- CMake - CMake
``` bash ``` bash
git clone https://github.com/FUNPLAY-pro-CS2/Ray-Trace.git git clone https://github.com/FUNPLAY-pro-CS2/Ray-Trace.git
cd Ray-Trace cd Ray-Trace
@ -319,9 +404,11 @@ docker compose -f docker/docker-compose.yml up
------------------------------------------------------------------------ ------------------------------------------------------------------------
## License ## License
GPLv3
GPLv3\
https://www.gnu.org/licenses/gpl-3.0.en.html https://www.gnu.org/licenses/gpl-3.0.en.html
## Author ## Author
**Michal "Slynx" Přikryl**
**Michal "Slynx" Přikryl**\
https://slynxdev.cz https://slynxdev.cz