Skip to content

Commit

Permalink
Find glow scene objects by scene objects they are attached to
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed Sep 3, 2024
1 parent c898b28 commit 6cf86ce
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 44 deletions.
1 change: 1 addition & 0 deletions Source/CS2/Classes/Glow.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum SceneObjectFlags {
struct CGlowHelperSceneObject : CSceneObject {
using entity = C_BaseEntity*;
using flags = std::uint8_t;
using attachedSceneObject = CSceneObject*;
};

#if IS_WIN64()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ class PlayerOutlineGlow {

void applyGlow(auto&& baseEntity, cs2::Color color) const noexcept
{
context.getGlowSceneObjectFor(baseEntity).apply(baseEntity, color);
auto&& sceneObject = baseEntity.renderComponent().sceneObjectUpdaters()[0].sceneObject();
auto&& glowSceneObject = context.getGlowSceneObjectFor(sceneObject);
glowSceneObject.apply(sceneObject, color);
glowSceneObject.setGlowEntity(baseEntity);
}

[[nodiscard]] cs2::Color getColor(auto&& playerPawn) const noexcept
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class PlayerOutlineGlowContext {
return hookContext.template make<PlayerOutlineGlowCondition>();
}

[[nodiscard]] decltype(auto) getGlowSceneObjectFor(auto&& baseEntity) const noexcept
[[nodiscard]] decltype(auto) getGlowSceneObjectFor(auto&& sceneObject) const noexcept
{
return hookContext.template make<GlowSceneObjects>().getGlowSceneObject(baseEntity.handle());
return hookContext.template make<GlowSceneObjects>().getGlowSceneObject(sceneObject);
}

[[nodiscard]] auto& viewRenderHook() const noexcept
Expand Down
1 change: 1 addition & 0 deletions Source/GameClasses/OffsetTypes/GlowSceneObjectOffset.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ template <typename FieldType, typename OffsetType>
using GlowSceneObjectOffset = FieldOffset<cs2::CGlowHelperSceneObject, FieldType, OffsetType>;

using OffsetToGlowSceneObjectEntity = GlowSceneObjectOffset<cs2::CGlowHelperSceneObject::entity, std::int32_t>;
using OffsetToGlowSceneObjectAttachedSceneObject = GlowSceneObjectOffset<cs2::CGlowHelperSceneObject::attachedSceneObject, std::int32_t>;
using OffsetToSceneObjectFlags = GlowSceneObjectOffset<cs2::CGlowHelperSceneObject::flags, WIN64_LINUX(std::int32_t, std::int8_t)>;
2 changes: 2 additions & 0 deletions Source/GameDependencies/GlowSceneObjectDeps.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ struct GlowSceneObjectDeps {
explicit GlowSceneObjectDeps(const GlowSceneObjectPatterns& glowSceneObjectPatterns) noexcept
: offsetToGlowSceneObjectEntity{glowSceneObjectPatterns.offsetToGlowSceneObjectEntity()}
, offsetToSceneObjectFlags{glowSceneObjectPatterns.offsetToSceneObjectFlags()}
, offsetToGlowSceneObjectAttachedSceneObject{glowSceneObjectPatterns.offsetToGlowSceneObjectAttachedSceneObject()}
{
}

OffsetToGlowSceneObjectEntity offsetToGlowSceneObjectEntity;
OffsetToSceneObjectFlags offsetToSceneObjectFlags;
OffsetToGlowSceneObjectAttachedSceneObject offsetToGlowSceneObjectAttachedSceneObject;
};
5 changes: 5 additions & 0 deletions Source/MemoryPatterns/Linux/GlowSceneObjectPatternsLinux.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ struct GlowSceneObjectPatterns {
{
return patternFinders.sceneSystemPatternFinder("06 ? ? ? ? ? 0F 85 ? ? ? ? 41 F6 44 24 ?"_pat).add(16).template readOffset<OffsetToSceneObjectFlags>();
}

[[nodiscard]] OffsetToGlowSceneObjectAttachedSceneObject offsetToGlowSceneObjectAttachedSceneObject() const noexcept
{
return patternFinders.clientPatternFinder("89 98 ? ? ? ? 49 8B ? 48"_pat).add(2).template readOffset<OffsetToGlowSceneObjectAttachedSceneObject>();
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ struct GlowSceneObjectPatterns {
{
return patternFinders.sceneSystemPatternFinder("CC F6 83 ? ? ? ?"_pat).add(3).template readOffset<OffsetToSceneObjectFlags>();
}

[[nodiscard]] OffsetToGlowSceneObjectAttachedSceneObject offsetToGlowSceneObjectAttachedSceneObject() const noexcept
{
return patternFinders.clientPatternFinder("48 89 ? 48 89 ? ? ? ? ? 48 8B ? 48 8B"_pat).add(6).template readOffset<OffsetToGlowSceneObjectAttachedSceneObject>();
}
};
16 changes: 10 additions & 6 deletions Source/OutlineGlow/GlowSceneObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@ class GlowSceneObject {
{
}

void apply(auto&& entity, cs2::Color color) const noexcept
void apply(auto&& sceneObject, cs2::Color color) const noexcept
{
context.applyGlow(entitySceneObject(entity), color);
context.setGlowEntity(entity);
context.applyGlow(sceneObject, color);
}

private:
[[nodiscard]] decltype(auto) entitySceneObject(auto&& entity) const noexcept
void setGlowEntity(auto&& entity) const noexcept
{
return entity.renderComponent().sceneObjectUpdaters()[0].sceneObject();
context.glowEntity() = entity;
}

[[nodiscard]] decltype(auto) getAttachedSceneObject() const noexcept
{
return context.attachedSceneObject().valueOr(nullptr);
}

private:
Context context;
};

Expand Down
26 changes: 18 additions & 8 deletions Source/OutlineGlow/GlowSceneObjectContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,41 @@ class GlowSceneObjectContext {
return;

if (const auto manageGlowSceneObject = hookContext.gameDependencies().manageGlowSceneObject) {
cs2::CGlowHelperSceneObject* glowSceneObject{glowSceneObjectPointer->value()};
cs2::CGlowHelperSceneObject* tempGlowSceneObject{glowSceneObject()};
cs2::CGlowHelperSceneObject* dummy{nullptr};
float colorFloat[4]{color.r() / 255.0f, color.g() / 255.0f, color.b() / 255.0f, color.a() / 255.0f};

#if IS_WIN64()
manageGlowSceneObject(&glowSceneObject, &dummy, sceneObject, colorFloat, 0, 0, 3, 1.0f);
manageGlowSceneObject(&tempGlowSceneObject, &dummy, sceneObject, colorFloat, 0, 0, 3, 1.0f);
#elif IS_LINUX()
double colorDouble[2];
static_assert(sizeof(colorFloat) == sizeof(colorDouble));
std::memcpy(colorDouble, colorFloat, sizeof(colorFloat));
manageGlowSceneObject(&glowSceneObject, &dummy, sceneObject, 3, colorDouble[0], colorDouble[1], 0.0f, 0.0f, 1.0f);
manageGlowSceneObject(&tempGlowSceneObject, &dummy, sceneObject, 3, colorDouble[0], colorDouble[1], 0.0f, 0.0f, 1.0f);
#endif

glowSceneObjectPointer->setValue(glowSceneObject);
glowSceneObjectPointer->setValue(tempGlowSceneObject);
}
}

void setGlowEntity(auto&& entity) const noexcept
[[nodiscard]] decltype(auto) glowEntity() const noexcept
{
if (glowSceneObjectPointer && glowSceneObjectPointer->value()) {
hookContext.gameDependencies().glowSceneObjectDeps.offsetToGlowSceneObjectEntity.of(glowSceneObjectPointer->value()) = static_cast<cs2::C_BaseEntity*>(entity);
}
return hookContext.gameDependencies().glowSceneObjectDeps.offsetToGlowSceneObjectEntity.of(glowSceneObject());
}

[[nodiscard]] decltype(auto) attachedSceneObject() const noexcept
{
return hookContext.gameDependencies().glowSceneObjectDeps.offsetToGlowSceneObjectAttachedSceneObject.of(glowSceneObject());
}

private:
[[nodiscard]] cs2::CGlowHelperSceneObject* glowSceneObject() const noexcept
{
if (glowSceneObjectPointer)
return glowSceneObjectPointer->value();
return nullptr;
}

HookContext& hookContext;
GlowSceneObjectPointer* glowSceneObjectPointer;
};
36 changes: 11 additions & 25 deletions Source/OutlineGlow/GlowSceneObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class GlowSceneObjects {
{
}

[[nodiscard]] decltype(auto) getGlowSceneObject(cs2::CEntityHandle entityHandle) const noexcept
[[nodiscard]] decltype(auto) getGlowSceneObject(cs2::CSceneObject* sceneObject) const noexcept
{
return hookContext.template make<GlowSceneObject>(glowSceneObjectByIndex(findOrInsertEntityHandle(entityHandle)));
return hookContext.template make<GlowSceneObject>(glowSceneObjectByIndex(findOrInsertGlowSceneObject(sceneObject)));
}

void removeUnreferencedObjects() const noexcept
Expand All @@ -26,7 +26,6 @@ class GlowSceneObjects {
deleteSceneObject(sceneObjectPointer.value());
} else {
sceneObjectPointer.clearReferenced();
state().entityHandles[writeIndex] = state().entityHandles[readIndex];
state().glowSceneObjects[writeIndex] = sceneObjectPointer;
++writeIndex;
}
Expand All @@ -44,7 +43,6 @@ class GlowSceneObjects {
state().size = 0;

if (state().capacity > 0) {
MemoryAllocator<cs2::CEntityHandle[]>::deallocate(state().entityHandles, state().capacity);
MemoryAllocator<GlowSceneObjectPointer[]>::deallocate(state().glowSceneObjects, state().capacity);
state().capacity = 0;
}
Expand All @@ -66,57 +64,45 @@ class GlowSceneObjects {
return nullptr;
}

[[nodiscard]] auto findOrInsertEntityHandle(cs2::CEntityHandle handle) const noexcept
[[nodiscard]] auto findOrInsertGlowSceneObject(cs2::CSceneObject* sceneObject) const noexcept
{
if (const auto index = findEntityHandle(handle); index != GlowSceneObjectsState::kInvalidIndex)
if (const auto index = findAttachedSceneObject(sceneObject); index != GlowSceneObjectsState::kInvalidIndex)
return index;
return insertEntityHandle(handle);
return insertGlowSceneObject();
}

[[nodiscard]] auto findEntityHandle(cs2::CEntityHandle handle) const noexcept
[[nodiscard]] auto findAttachedSceneObject(cs2::CSceneObject* sceneObject) const noexcept
{
for (GlowSceneObjectsState::SizeType i = 0; i < state().size; ++i) {
if (state().entityHandles[i] == handle)
if (hookContext.template make<GlowSceneObject>(&state().glowSceneObjects[i]).getAttachedSceneObject() == sceneObject)
return i;
}
return GlowSceneObjectsState::kInvalidIndex;
}

[[nodiscard]] auto insertEntityHandle(cs2::CEntityHandle handle) const noexcept
[[nodiscard]] auto insertGlowSceneObject() const noexcept
{
if (state().size < state().capacity) {
const auto index = state().size;
state().entityHandles[index] = handle;
state().glowSceneObjects[index] = nullptr;
++state().size;
return index;
} else {
const auto newCapacity = static_cast<GlowSceneObjectsState::SizeType>(state().capacity + 10);
const auto newEntityHandles = reinterpret_cast<cs2::CEntityHandle*>(MemoryAllocator<cs2::CEntityHandle[]>::allocate(newCapacity));
const auto newGlowSceneObjects = reinterpret_cast<GlowSceneObjectPointer*>(MemoryAllocator<GlowSceneObjectPointer[]>::allocate(newCapacity));
if (newEntityHandles && newGlowSceneObjects) {
if (state().size > 0) {
std::memcpy(newEntityHandles, state().entityHandles, state().size * sizeof(cs2::CEntityHandle));
if (newGlowSceneObjects) {
if (state().size > 0)
std::memcpy(newGlowSceneObjects, state().glowSceneObjects, state().size * sizeof(GlowSceneObjectPointer));
}

if (state().capacity > 0) {
MemoryAllocator<cs2::CEntityHandle[]>::deallocate(state().entityHandles, state().capacity);
if (state().capacity > 0)
MemoryAllocator<GlowSceneObjectPointer[]>::deallocate(state().glowSceneObjects, state().capacity);
}

state().capacity = newCapacity;
state().entityHandles = newEntityHandles;
state().glowSceneObjects = newGlowSceneObjects;
const auto index = state().size;
state().entityHandles[index] = handle;
state().glowSceneObjects[index] = nullptr;
++state().size;
return index;
} else if (newEntityHandles) {
MemoryAllocator<cs2::CEntityHandle[]>::deallocate(newEntityHandles, newCapacity);
} else if (newGlowSceneObjects) {
MemoryAllocator<GlowSceneObjectPointer[]>::deallocate(newGlowSceneObjects, newCapacity);
}
}
return GlowSceneObjectsState::kInvalidIndex;
Expand Down
2 changes: 0 additions & 2 deletions Source/OutlineGlow/GlowSceneObjectsState.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

#include <cstdint>

#include <CS2/Classes/EntitySystem/CEntityHandle.h>
#include "GlowSceneObjectPointer.h"

struct GlowSceneObjectsState {
using SizeType = std::uint16_t;
static constexpr auto kInvalidIndex = static_cast<SizeType>(-1);

cs2::CEntityHandle* entityHandles{nullptr};
GlowSceneObjectPointer* glowSceneObjects{nullptr};
SizeType size{0};
SizeType capacity{0};
Expand Down

0 comments on commit 6cf86ce

Please sign in to comment.