Skip to content

Commit

Permalink
Plugins: Add dcast
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Mar 6, 2024
1 parent 10c453f commit 051a353
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/example_plugin/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,17 @@ class ExamplePlugin : public uevr::Plugin {

API::get()->log_info("Engine name: %s", uengine_name_narrow.c_str());

// Test if we can dcast to UObject.
{
const auto engine_as_object = engine->dcast<API::UObject>();

if (engine != nullptr) {
API::get()->log_info("Engine successfully dcast to UObject");
} else {
API::get()->log_error("Failed to dcast Engine to UObject");
}
}

// Go through all of engine's fields and log their names.
const auto engine_class_ours = (API::UStruct*)engine->get_class();
for (auto super = engine_class_ours; super != nullptr; super = super->get_super()) {
Expand Down
24 changes: 24 additions & 0 deletions include/uevr/API.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ class API {
template<typename T>
struct TArray;

// dynamic_cast variant
template<typename T>
static T* dcast(UObject* obj) {
if (obj == nullptr) {
return nullptr;
}

if (obj->is_a(T::static_class())) {
return static_cast<T*>(obj);
}

return nullptr;
}

template<typename T = UObject>
T* find_uobject(std::wstring_view name) {
static const auto fn = sdk()->uobject_array->find_uobject;
Expand Down Expand Up @@ -335,6 +349,16 @@ class API {
return c->get_fname()->to_string() + L' ' + obj_name;
}

// dynamic_cast variant
template<typename T>
T* dcast() {
if (this->is_a(T::static_class())) {
return static_cast<T*>(this);
}

return nullptr;
}

private:
static inline const UEVR_UObjectFunctions* s_functions{nullptr};
inline static const UEVR_UObjectFunctions* initialize() {
Expand Down

0 comments on commit 051a353

Please sign in to comment.