Skip to content

Commit

Permalink
UObjectHook: Add "Destroy Component" button
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Oct 22, 2023
1 parent de2a83d commit 0aed972
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
46 changes: 46 additions & 0 deletions shared/sdk/AActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,36 @@
#include "AActor.hpp"

namespace sdk {
UClass* UActorComponent::static_class() {
return sdk::find_uobject<UClass>(L"Class /Script/Engine.ActorComponent");
}

AActor* UActorComponent::get_owner() {
static const auto func = UActorComponent::static_class()->find_function(L"GetOwner");

if (func == nullptr) {
return nullptr;
}

struct {
AActor* ReturnValue;
} params;

params.ReturnValue = nullptr;

this->process_event(func, &params);

return params.ReturnValue;
}

void UActorComponent::destroy_component() {
auto owner = this->get_owner();

if (owner != nullptr) {
owner->destroy_component(this);
}
}

UClass* AActor::static_class() {
return sdk::find_uobject<UClass>(L"Class /Script/Engine.Actor");
}
Expand Down Expand Up @@ -213,4 +243,20 @@ std::vector<UActorComponent*> AActor::get_all_components() {

return get_components_by_class(actor_component_t);
}

void AActor::destroy_component(UActorComponent* component) {
static const auto func = AActor::static_class()->find_function(L"K2_DestroyComponent");

if (func == nullptr) {
return;
}

struct {
UActorComponent* Component;
} params;

params.Component = component;

this->process_event(func, &params);
}
}
9 changes: 8 additions & 1 deletion shared/sdk/AActor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
namespace sdk {
class UCameraComponent;
class USceneComponent;
class AActor;
class UActorComponent : public UObject {

public:
static UClass* static_class();

AActor* get_owner();
void destroy_component();
};

class AActor : public UObject {
Expand All @@ -32,6 +37,8 @@ class AActor : public UObject {
std::vector<UActorComponent*> get_components_by_class(UClass* uclass);
std::vector<UActorComponent*> get_all_components();

void destroy_component(UActorComponent* component);

protected:
};
}
10 changes: 10 additions & 0 deletions src/mods/UObjectHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ void UObjectHook::ui_handle_object(sdk::UObject* object) {
return a->get_fname().to_string() < b->get_fname().to_string();
});

if (uclass->is_a(sdk::UActorComponent::static_class())) {
if (ImGui::Button("Destroy Component")) {
auto comp = (sdk::UActorComponent*)object;

comp->destroy_component();
}
}

if (uclass->is_a(sdk::AActor::static_class())) {
auto actor = (sdk::AActor*)object;

Expand Down Expand Up @@ -398,6 +406,8 @@ void UObjectHook::ui_handle_object(sdk::UObject* object) {
ImGui::DragInt(utility::narrow(prop->get_field_name().to_string()).data(), &value, 1);
}
break;

// TODO: handle bitfields
case "BoolProperty"_fnv:
{
auto& value = *(bool*)((uintptr_t)object + ((sdk::FProperty*)prop)->get_offset());
Expand Down

0 comments on commit 0aed972

Please sign in to comment.