Skip to content

Commit

Permalink
SDK: Add try_cast for cObj derived types
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Aug 18, 2022
1 parent 23852f7 commit a0f7eb5
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions shared/sdk/Behavior.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ namespace sdk {
class Entity;

class Behavior : public sdk::Obj {
public:
static constexpr std::string_view class_name() {
return "class Behavior";
}

public:
virtual void pad4() = 0;
virtual void pad5() = 0;
Expand Down
5 changes: 5 additions & 0 deletions shared/sdk/BehaviorAppBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

namespace sdk {
class BehaviorAppBase : public sdk::Behavior {
public:
static constexpr std::string_view class_name() {
return "class BehaviorAppBase";
}

public:
__forceinline ::regenny::BehaviorAppBase* regenny() const {
return (::regenny::BehaviorAppBase*)this;
Expand Down
5 changes: 5 additions & 0 deletions shared/sdk/EmBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

namespace sdk {
class EmBase : public sdk::BehaviorAppBase {
public:
static constexpr std::string_view class_name() {
return "class EmBase";
}

public:
OBJECT_SCRIPT_FUNCTION(EmBase, addExpDirect, void) // base + 0x574040
OBJECT_SCRIPT_FUNCTION(EmBase, callEnemy, void) // base + 0x537010
Expand Down
5 changes: 5 additions & 0 deletions shared/sdk/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
namespace sdk {
// cModel
class Model : public sdk::Parts {
public:
static constexpr std::string_view class_name() {
return "class cModel";
}

public:
virtual ~Model() = 0;
virtual void model1(...) = 0;
Expand Down
5 changes: 5 additions & 0 deletions shared/sdk/NPC.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

namespace sdk {
class NPC : public BehaviorAppBase {
public:
static constexpr std::string_view class_name() {
return "class NPC";
}

public:
OBJECT_SCRIPT_FUNCTION(NPC, changeSetTypeFollow, void) // base + 0x7008f0
OBJECT_SCRIPT_FUNCTION(NPC, changeSetTypeIdle, void) // base + 0x7009f0
Expand Down
29 changes: 29 additions & 0 deletions shared/sdk/Obj.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

namespace sdk {
class Obj : public sdk::Model {
public:
static constexpr std::string_view class_name() {
return "class cObj";
}

public:
// Vtable starts at index 3
virtual NierRTTI2* get_rtti() = 0;
Expand All @@ -27,6 +32,21 @@ class Obj : public sdk::Model {
return utility::rtti::derives_from(this, name);
}

template<typename T>
bool is() const {
return utility::rtti::derives_from(this, T::class_name());
}

template<typename T>
T* try_cast() {
return this->is<T>() ? static_cast<T*>(this) : nullptr;
}

template<typename T>
const T* try_cast() const {
return this->is<const T>() ? static_cast<const T*>(this) : nullptr;
}

bool is_pl0000() const {
return is("class Pl0000");
}
Expand All @@ -39,13 +59,22 @@ class Obj : public sdk::Model {
return is("class BehaviorAppBase");
}

bool is_em_base() const {
return is("class EmBase");
}

bool is_npc() const {
return is("class NPC");
}

auto& obj_flags() {
return regenny()->obj_flags;
}

auto& model_index() {
return regenny()->model_index;
}

private:
uint8_t detail_obj_data[sizeof(::regenny::Obj) - sizeof(Model)];
};
Expand Down
4 changes: 4 additions & 0 deletions shared/sdk/Pl0000.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
namespace sdk {
class Pl0000 : public sdk::BehaviorAppBase {
public:
static constexpr std::string_view class_name() {
return "class Pl0000";
}

enum EButtonIndex {
INDEX_ATTACK_LIGHT = 0,
INDEX_ATTACK_HEAVY = 1,
Expand Down

0 comments on commit a0f7eb5

Please sign in to comment.