From 59d79309017d6f647fe7b49e5d28e52cf967fe68 Mon Sep 17 00:00:00 2001 From: Luis Michaelis Date: Wed, 8 Nov 2023 16:31:06 +0100 Subject: [PATCH] refactor(DaedalusSymbol): make value getters/setters take regular pointers where applicable --- changelog.md | 1 + include/zenkit/DaedalusScript.hh | 30 +++++++++++--------------- src/DaedalusScript.cc | 22 ++++++++----------- src/DaedalusVm.cc | 36 ++++++++++++++++---------------- 4 files changed, 40 insertions(+), 49 deletions(-) diff --git a/changelog.md b/changelog.md index bc0d90ef..da15b712 100644 --- a/changelog.md +++ b/changelog.md @@ -29,6 +29,7 @@ Here are the changes not related to the renamed API. * `phoenix::model_script::parse_binary` has been removed in favour of `phoenix::model_script::parse` * `phoenix::vobs::camera_lock_mode` has been removed in favour of `phoenix::vobs::sprite_alignment` * `phoenix::vob::camera_alignment` has been removed in favour of `phoenix::vob::sprite_camera_facing_mode` +* `phoenix::symbol::{get,set}_{string,int,float}` now take a raw pointer as context. ### Features * [cfec0051] XZEN-encoded worlds are now supported. Thanks, @ThielHater! diff --git a/include/zenkit/DaedalusScript.hh b/include/zenkit/DaedalusScript.hh index 9b8f5f7f..2d97dd64 100644 --- a/include/zenkit/DaedalusScript.hh +++ b/include/zenkit/DaedalusScript.hh @@ -344,13 +344,13 @@ namespace zenkit { friend class DaedalusSymbol; virtual void set_int(DaedalusSymbol const& sym, uint16_t index, std::int32_t value) = 0; - virtual std::int32_t get_int(DaedalusSymbol const& sym, uint16_t index) = 0; + virtual std::int32_t get_int(DaedalusSymbol const& sym, uint16_t index) const = 0; virtual void set_float(DaedalusSymbol const& sym, uint16_t index, float value) = 0; - virtual float get_float(DaedalusSymbol const& sym, uint16_t index) = 0; + virtual float get_float(DaedalusSymbol const& sym, uint16_t index) const = 0; virtual void set_string(DaedalusSymbol const& sym, uint16_t index, std::string_view value) = 0; - virtual std::string const& get_string(DaedalusSymbol const& sym, uint16_t index) = 0; + virtual std::string const& get_string(DaedalusSymbol const& sym, uint16_t index) const = 0; }; /// \brief The base class for all exceptions thrown by interacting with a script. @@ -494,8 +494,8 @@ namespace zenkit { /// \throws DaedalusUnboundMemberAccess if this symbol has not been registered yet /// \throws DaedalusIllegalContextType if this symbol #is_registered_to a different type than the type of \p /// context. - [[nodiscard]] ZKAPI std::string const& - get_string(std::uint16_t index = 0, std::shared_ptr const& context = nullptr) const; + [[nodiscard]] ZKAPI std::string const& get_string(std::uint16_t index = 0, + DaedalusInstance const* context = nullptr) const; /// \brief Validates that the symbol is a float and retrieves it's value in the given context. /// \param index The index of the value to get. @@ -507,8 +507,7 @@ namespace zenkit { /// \throws DaedalusUnboundMemberAccess if this symbol has not been registered yet /// \throws DaedalusIllegalContextType if this symbol #is_registered_to a different type than the type of \p /// context. - [[nodiscard]] ZKAPI float get_float(std::uint16_t index = 0, - std::shared_ptr const& context = nullptr) const; + [[nodiscard]] ZKAPI float get_float(std::uint16_t index = 0, DaedalusInstance const* context = nullptr) const; /// \brief Validates that the symbol is an int and retrieves it's value in the given context. /// \param index The index of the value to get. @@ -521,7 +520,7 @@ namespace zenkit { /// \throws DaedalusIllegalContextType if this symbol #is_registered_to a different type than the type of \p /// context. [[nodiscard]] ZKAPI std::int32_t get_int(std::uint16_t index = 0, - std::shared_ptr const& context = nullptr) const; + DaedalusInstance const* context = nullptr) const; /// \brief Validates that the symbol is an instance and retrieves it's value /// \return The instance associated with the symbol. @@ -540,9 +539,7 @@ namespace zenkit { /// \throws DaedalusUnboundMemberAccess if this symbol has not been registered yet /// \throws DaedalusIllegalContextType if this symbol #is_registered_to a different type than the type of \p /// context. - ZKAPI void set_string(std::string_view value, - std::uint16_t index = 0, - std::shared_ptr const& context = nullptr); + ZKAPI void set_string(std::string_view value, std::uint16_t index = 0, DaedalusInstance* context = nullptr); /// \brief Validates that the symbol is a float and not constant and sets it's value in the given context. /// \param value The new value to set. @@ -554,8 +551,7 @@ namespace zenkit { /// \throws DaedalusUnboundMemberAccess if this symbol has not been registered yet /// \throws DaedalusIllegalContextType if this symbol #is_registered_to a different type than the type of \p /// context. - ZKAPI void - set_float(float value, std::uint16_t index = 0, std::shared_ptr const& context = nullptr); + ZKAPI void set_float(float value, std::uint16_t index = 0, DaedalusInstance* context = nullptr); /// \brief Validates that the symbol is an int and not constant and sets it's value in the given context. /// \param value The new value to set. @@ -567,9 +563,7 @@ namespace zenkit { /// \throws DaedalusUnboundMemberAccess if this symbol has not been registered yet /// \throws DaedalusIllegalContextType if this symbol #is_registered_to a different type than the type of \p /// context. - ZKAPI void set_int(std::int32_t value, - std::uint16_t index = 0, - std::shared_ptr const& context = nullptr); + ZKAPI void set_int(std::int32_t value, std::uint16_t index = 0, DaedalusInstance* context = nullptr); /// \brief Validates that the symbol is an instance and sets it's value /// \param inst The instance value to set @@ -703,7 +697,7 @@ namespace zenkit { protected: template - inline T const* get_member_ptr(std::uint16_t index, std::shared_ptr const& context) const { + inline T const* get_member_ptr(std::uint16_t index, DaedalusInstance const* context) const { if (!_m_registered_to) throw DaedalusUnboundMemberAccess(this); if (*_m_registered_to != *context->_m_type) throw DaedalusIllegalContextType {this, *context->_m_type}; @@ -713,7 +707,7 @@ namespace zenkit { } template - inline T* get_member_ptr(std::uint16_t index, std::shared_ptr const& context) { + inline T* get_member_ptr(std::uint16_t index, DaedalusInstance* context) { if (!_m_registered_to) throw DaedalusUnboundMemberAccess(this); if (*_m_registered_to != *context->_m_type) throw DaedalusIllegalContextType {this, *context->_m_type}; diff --git a/src/DaedalusScript.cc b/src/DaedalusScript.cc index afc7d1c2..c9495835 100644 --- a/src/DaedalusScript.cc +++ b/src/DaedalusScript.cc @@ -433,8 +433,7 @@ namespace zenkit { } } - std::string const& DaedalusSymbol::get_string(std::uint16_t index, - std::shared_ptr const& context) const { + std::string const& DaedalusSymbol::get_string(std::uint16_t index, DaedalusInstance const* context) const { if (type() != DaedalusDataType::STRING) { throw DaedalusIllegalTypeAccess(this, DaedalusDataType::STRING); } @@ -449,7 +448,7 @@ namespace zenkit { if (context->symbol_index() == static_cast(-1) && context->_m_type == &typeid(DaedalusTransientInstance)) { - return reinterpret_cast(*context).get_string(*this, index); + return reinterpret_cast(*context).get_string(*this, index); } return *get_member_ptr(index, context); @@ -458,7 +457,7 @@ namespace zenkit { } } - float DaedalusSymbol::get_float(std::uint16_t index, std::shared_ptr const& context) const { + float DaedalusSymbol::get_float(std::uint16_t index, DaedalusInstance const* context) const { if (type() != DaedalusDataType::FLOAT) { throw DaedalusIllegalTypeAccess(this, DaedalusDataType::FLOAT); } @@ -473,7 +472,7 @@ namespace zenkit { if (context->symbol_index() == static_cast(-1) && context->_m_type == &typeid(DaedalusTransientInstance)) { - return reinterpret_cast(*context).get_float(*this, index); + return reinterpret_cast(*context).get_float(*this, index); } return *get_member_ptr(static_cast(index), context); @@ -482,7 +481,7 @@ namespace zenkit { } } - std::int32_t DaedalusSymbol::get_int(std::uint16_t index, std::shared_ptr const& context) const { + std::int32_t DaedalusSymbol::get_int(std::uint16_t index, DaedalusInstance const* context) const { if (type() != DaedalusDataType::INT && type() != DaedalusDataType::FUNCTION) { throw DaedalusIllegalTypeAccess(this, DaedalusDataType::INT); } @@ -497,7 +496,7 @@ namespace zenkit { if (context->symbol_index() == static_cast(-1) && context->_m_type == &typeid(DaedalusTransientInstance)) { - return reinterpret_cast(*context).get_int(*this, index); + return reinterpret_cast(*context).get_int(*this, index); } return *get_member_ptr(index, context); @@ -506,9 +505,7 @@ namespace zenkit { } } - void DaedalusSymbol::set_string(std::string_view value, - std::uint16_t index, - std::shared_ptr const& context) { + void DaedalusSymbol::set_string(std::string_view value, std::uint16_t index, DaedalusInstance* context) { if (type() != DaedalusDataType::STRING) { throw DaedalusIllegalTypeAccess(this, DaedalusDataType::STRING); } @@ -533,7 +530,7 @@ namespace zenkit { } } - void DaedalusSymbol::set_float(float value, std::uint16_t index, std::shared_ptr const& context) { + void DaedalusSymbol::set_float(float value, std::uint16_t index, DaedalusInstance* context) { if (type() != DaedalusDataType::FLOAT) { throw DaedalusIllegalTypeAccess(this, DaedalusDataType::FLOAT); } @@ -558,8 +555,7 @@ namespace zenkit { } } - void - DaedalusSymbol::set_int(std::int32_t value, std::uint16_t index, std::shared_ptr const& context) { + void DaedalusSymbol::set_int(std::int32_t value, std::uint16_t index, DaedalusInstance* context) { if (type() != DaedalusDataType::INT && type() != DaedalusDataType::FUNCTION) { throw DaedalusIllegalTypeAccess(this, DaedalusDataType::INT); } diff --git a/src/DaedalusVm.cc b/src/DaedalusVm.cc index f8dcb5d2..fac3d7f2 100644 --- a/src/DaedalusVm.cc +++ b/src/DaedalusVm.cc @@ -317,8 +317,8 @@ namespace zenkit { if (!ref->is_member() || context != nullptr || !(_m_flags & DaedalusVmExecutionFlag::ALLOW_NULL_INSTANCE_ACCESS)) { - auto result = ref->get_int(idx, context) + value; - ref->set_int(result, idx, context); + auto result = ref->get_int(idx, context.get()) + value; + ref->set_int(result, idx, context.get()); } else if (ref->is_member()) { ZKLOGE("DaedalusVm", "Accessing member \"%s\" without an instance set", ref->name().c_str()); } @@ -335,8 +335,8 @@ namespace zenkit { if (!ref->is_member() || context != nullptr || !(_m_flags & DaedalusVmExecutionFlag::ALLOW_NULL_INSTANCE_ACCESS)) { - auto result = ref->get_int(idx, context) - value; - ref->set_int(result, idx, context); + auto result = ref->get_int(idx, context.get()) - value; + ref->set_int(result, idx, context.get()); } else if (ref->is_member()) { ZKLOGE("DaedalusVm", "Accessing member \"%s\" without an instance set", ref->name().c_str()); } @@ -352,8 +352,8 @@ namespace zenkit { if (!ref->is_member() || context != nullptr || !(_m_flags & DaedalusVmExecutionFlag::ALLOW_NULL_INSTANCE_ACCESS)) { - auto result = ref->get_int(idx, context) * value; - ref->set_int(result, idx, context); + auto result = ref->get_int(idx, context.get()) * value; + ref->set_int(result, idx, context.get()); } else if (ref->is_member()) { ZKLOGE("DaedalusVm", "Accessing member \"%s\" without an instance set", ref->name().c_str()); } @@ -374,8 +374,8 @@ namespace zenkit { if (!ref->is_member() || context != nullptr || !(_m_flags & DaedalusVmExecutionFlag::ALLOW_NULL_INSTANCE_ACCESS)) { - auto result = ref->get_int(idx, context) / value; - ref->set_int(result, idx, context); + auto result = ref->get_int(idx, context.get()) / value; + ref->set_int(result, idx, context.get()); } else if (ref->is_member()) { ZKLOGE("DaedalusVm", "Accessing member \"%s\" without an instance set", ref->name().c_str()); } @@ -574,7 +574,7 @@ namespace zenkit { return empty; } - return s->get_string(i, context); + return s->get_string(i, context.get()); } void DaedalusVm::jump(std::uint32_t address) { @@ -658,16 +658,16 @@ namespace zenkit { switch (ref->type()) { case DaedalusDataType::FLOAT: - value = std::to_string(ref->get_float(v.index, _m_instance)); + value = std::to_string(ref->get_float(v.index, _m_instance.get())); break; case DaedalusDataType::INT: - value = std::to_string(ref->get_int(v.index, _m_instance)); + value = std::to_string(ref->get_int(v.index, _m_instance.get())); break; case DaedalusDataType::STRING: - value = "'" + ref->get_string(v.index, _m_instance) + "'"; + value = "'" + ref->get_string(v.index, _m_instance.get()) + "'"; break; case DaedalusDataType::FUNCTION: { - auto index = ref->get_int(v.index, _m_instance); + auto index = ref->get_int(v.index, _m_instance.get()); auto sym = find_symbol_by_index(static_cast(index)); value = "&" + sym->name(); break; @@ -818,7 +818,7 @@ namespace zenkit { return 0; } - return sym->get_int(index, context); + return sym->get_int(index, context.get()); } float DaedalusVm::get_float(std::shared_ptr& context, @@ -837,7 +837,7 @@ namespace zenkit { return 0; } - return sym->get_float(index, context); + return sym->get_float(index, context.get()); } void DaedalusVm::set_int(std::shared_ptr& context, @@ -850,7 +850,7 @@ namespace zenkit { if (!ref->is_member() || context != nullptr || !(_m_flags & DaedalusVmExecutionFlag::ALLOW_NULL_INSTANCE_ACCESS)) { - ref->set_int(value, index, context); + ref->set_int(value, index, context.get()); } else if (ref->is_member()) { ZKLOGE("DaedalusVm", "Accessing member \"%s\" without an instance set", ref->name().c_str()); } @@ -866,7 +866,7 @@ namespace zenkit { if (!ref->is_member() || context != nullptr || !(_m_flags & DaedalusVmExecutionFlag::ALLOW_NULL_INSTANCE_ACCESS)) { - ref->set_float(value, index, context); + ref->set_float(value, index, context.get()); } else if (ref->is_member()) { ZKLOGE("DaedalusVm", "Accessing member \"%s\" without an instance set", ref->name().c_str()); } @@ -882,7 +882,7 @@ namespace zenkit { if (!ref->is_member() || context != nullptr || !(_m_flags & DaedalusVmExecutionFlag::ALLOW_NULL_INSTANCE_ACCESS)) { - ref->set_string(value, index, context); + ref->set_string(value, index, context.get()); } else if (ref->is_member()) { ZKLOGE("DaedalusVm", "Accessing member \"%s\" without an instance set", ref->name().c_str()); }