Skip to content

Commit

Permalink
SDK: Add UObject::find_property and get_property
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jul 9, 2023
1 parent 059a13d commit dd89684
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 2 deletions.
2 changes: 1 addition & 1 deletion shared/sdk/FProperty.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include <cstdint>

#include "UClass.hpp"
#include "FField.hpp"
#include "UClass.hpp"

namespace sdk {
class UProperty;
Expand Down
29 changes: 29 additions & 0 deletions shared/sdk/UClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "UProperty.hpp"
#include "FProperty.hpp"
#include "FField.hpp"
#include "FProperty.hpp"

#include "UClass.hpp"

Expand Down Expand Up @@ -556,4 +557,32 @@ void UScriptStruct::update_offsets() {
continue;
}
}

FProperty* UStruct::find_property(std::wstring_view name) const {
for (auto super = this; super != nullptr; super = (UClass*)super->get_super_struct()) {
for (auto child = super->get_child_properties(); child != nullptr; child = child->get_next()) {
SPDLOG_INFO("[UStruct] Checking child property {}", utility::narrow(child->get_field_name().to_string()));

if (child->get_field_name().to_string() == name) {
return (FProperty*)child;
}
}
}

return nullptr;
}

UProperty* UStruct::find_uproperty(std::wstring_view name) const {
for (auto super = this; super != nullptr; super = (UClass*)super->get_super_struct()) {
for (auto child = super->get_children(); child != nullptr; child = child->get_next()) {
SPDLOG_INFO("[UStruct] Checking child UProperty {}", utility::narrow(child->get_fname().to_string()));

if (child->get_fname().to_string() == name) {
return (UProperty*)child;
}
}
}

return nullptr;
}
}
5 changes: 5 additions & 0 deletions shared/sdk/UClass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace sdk {
class UClass;
class UStruct;
class FField;
class FProperty;
class UProperty;

class UField : public UObject {
public:
Expand Down Expand Up @@ -50,6 +52,9 @@ class UStruct : public UField {
return false;
}

FProperty* find_property(std::wstring_view name) const;
UProperty* find_uproperty(std::wstring_view name) const;

protected:
static void resolve_field_offsets(uint32_t child_search_start);
static void resolve_function_offsets(uint32_t child_search_start);
Expand Down
38 changes: 38 additions & 0 deletions shared/sdk/UObject.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "UObjectArray.hpp"

#include "UClass.hpp"
#include "FProperty.hpp"
#include "UProperty.hpp"
#include "UObject.hpp"

namespace sdk {
Expand All @@ -11,4 +13,40 @@ UClass* UObject::static_class() {
bool UObject::is_a(UClass* other) const {
return get_class()->is_a((UStruct*)other);
}

void* UObject::get_property_data(std::wstring_view name) const {
const auto klass = get_class();

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

const auto prop = klass->find_property(name);

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

const auto offset = prop->get_offset();

return (void*)((uintptr_t)this + offset);
}

void* UObject::get_uproperty_data(std::wstring_view name) const {
const auto klass = get_class();

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

const auto prop = klass->find_uproperty(name);

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

const auto offset = prop->get_offset();

return (void*)((uintptr_t)this + offset);
}
}
12 changes: 12 additions & 0 deletions shared/sdk/UObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ class UObject : public UObjectBase {
public:
static UClass* static_class();

void* get_property_data(std::wstring_view name) const;
template <typename T>
T& get_property(std::wstring_view name) const {
return *(T*)get_property_data(name);
}

void* get_uproperty_data(std::wstring_view name) const;
template <typename T>
T& get_uproperty(std::wstring_view name) const {
return *(T*)get_uproperty_data(name);
}

template <typename T>
bool is_a() const {
return is_a(T::static_class());
Expand Down
2 changes: 1 addition & 1 deletion shared/sdk/UObjectBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void UObjectBase::update_process_event_index() try {
return utility::ExhaustionResult::CONTINUE;
}

if (*displacement == 0x0 || IsBadReadPtr((void*)*displacement, sizeof(void*) * 4)) {
if (*displacement == 0x0 || IsBadReadPtr((void*)*displacement, sizeof(void*) * 10)) {
return utility::ExhaustionResult::CONTINUE;
}

Expand Down

0 comments on commit dd89684

Please sign in to comment.