Skip to content

Commit

Permalink
UObjectHook: Fix object tracking errors that caused crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Jul 17, 2024
1 parent fb387e1 commit 0f75215
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/mods/UObjectHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,21 +248,23 @@ void UObjectHook::add_new_object(sdk::UObjectBase* object) {
m_reusable_meta_objects.pop_back();
} else {
meta_object = std::make_unique<MetaObject>();
meta_object->super_classes.reserve(16);
}

m_objects.insert(object);
meta_object->super_classes.clear();
meta_object->full_name = object->get_full_name();
meta_object->uclass = object->get_class();

m_meta_objects[object] = std::move(meta_object);

m_most_recent_objects.push_front((sdk::UObject*)object);

if (m_most_recent_objects.size() > 50) {
m_most_recent_objects.pop_back();
}

for (auto super = (sdk::UStruct*)object->get_class(); super != nullptr; super = super->get_super_struct()) {
meta_object->super_classes.push_back((sdk::UClass*)super);

m_objects_by_class[(sdk::UClass*)super].insert(object);

if (auto it = m_on_creation_add_component_jobs.find((sdk::UClass*)super); it != m_on_creation_add_component_jobs.end()) {
Expand All @@ -289,6 +291,8 @@ void UObjectHook::add_new_object(sdk::UObjectBase* object) {
});
}
}

m_meta_objects[object] = std::move(meta_object);

#ifdef VERBOSE_UOBJECTHOOK
SPDLOG_INFO("Adding object {:x} {:s}", (uintptr_t)object, utility::narrow(m_meta_objects[object]->full_name));
Expand Down Expand Up @@ -3543,19 +3547,21 @@ void* UObjectHook::destructor(sdk::UObjectBase* object, void* rdx, void* r8, voi
hook->m_camera_attach.object = nullptr;
}

for (auto super = (sdk::UStruct*)it->second->uclass; super != nullptr;) {
/*for (auto super = (sdk::UStruct*)it->second->uclass; super != nullptr;) {
hook->m_objects_by_class[(sdk::UClass*)super].erase(object);
// Just make sure we don't do any operations on super because it might be invalid...
if (!hook->m_objects.contains(super)) {
//SPDLOG_ERROR("Super for {:x} is not valid", (uintptr_t)object);
SPDLOG_ERROR("Super for {:x} is not valid", (uintptr_t)object);
break;
}
super = super->get_super_struct();
}
}*/

//hook->m_objects_by_class[it->second->uclass].erase(object);
for (auto super : it->second->super_classes) {
hook->m_objects_by_class[super].erase(object);
}

hook->m_reusable_meta_objects.push_back(std::move(it->second));
hook->m_meta_objects.erase(object);
Expand Down
1 change: 1 addition & 0 deletions src/mods/UObjectHook.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ class UObjectHook : public Mod {
struct MetaObject {
std::wstring full_name{};
sdk::UClass* uclass{nullptr};
std::vector<sdk::UClass*> super_classes{};
};

std::unordered_set<sdk::UObjectBase*> m_objects{};
Expand Down

0 comments on commit 0f75215

Please sign in to comment.