Skip to content

Commit

Permalink
refactor(DaedalusSymbol): make value getters/setters take regular poi…
Browse files Browse the repository at this point in the history
…nters where applicable
  • Loading branch information
lmichaelis committed Nov 8, 2023
1 parent d8701dc commit 59d7930
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 49 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
30 changes: 12 additions & 18 deletions include/zenkit/DaedalusScript.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<DaedalusInstance> 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.
Expand All @@ -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<DaedalusInstance> 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.
Expand All @@ -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<DaedalusInstance> 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.
Expand All @@ -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<DaedalusInstance> 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.
Expand All @@ -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<DaedalusInstance> 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.
Expand All @@ -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<DaedalusInstance> 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
Expand Down Expand Up @@ -703,7 +697,7 @@ namespace zenkit {

protected:
template <typename T>
inline T const* get_member_ptr(std::uint16_t index, std::shared_ptr<DaedalusInstance> 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};

Expand All @@ -713,7 +707,7 @@ namespace zenkit {
}

template <typename T>
inline T* get_member_ptr(std::uint16_t index, std::shared_ptr<DaedalusInstance> 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};

Expand Down
22 changes: 9 additions & 13 deletions src/DaedalusScript.cc
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,7 @@ namespace zenkit {
}
}

std::string const& DaedalusSymbol::get_string(std::uint16_t index,
std::shared_ptr<DaedalusInstance> 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);
}
Expand All @@ -449,7 +448,7 @@ namespace zenkit {

if (context->symbol_index() == static_cast<uint32_t>(-1) &&
context->_m_type == &typeid(DaedalusTransientInstance)) {
return reinterpret_cast<DaedalusTransientInstance&>(*context).get_string(*this, index);
return reinterpret_cast<DaedalusTransientInstance const&>(*context).get_string(*this, index);
}

return *get_member_ptr<std::string>(index, context);
Expand All @@ -458,7 +457,7 @@ namespace zenkit {
}
}

float DaedalusSymbol::get_float(std::uint16_t index, std::shared_ptr<DaedalusInstance> const& context) const {
float DaedalusSymbol::get_float(std::uint16_t index, DaedalusInstance const* context) const {
if (type() != DaedalusDataType::FLOAT) {
throw DaedalusIllegalTypeAccess(this, DaedalusDataType::FLOAT);
}
Expand All @@ -473,7 +472,7 @@ namespace zenkit {

if (context->symbol_index() == static_cast<uint32_t>(-1) &&
context->_m_type == &typeid(DaedalusTransientInstance)) {
return reinterpret_cast<DaedalusTransientInstance&>(*context).get_float(*this, index);
return reinterpret_cast<DaedalusTransientInstance const&>(*context).get_float(*this, index);
}

return *get_member_ptr<float>(static_cast<uint8_t>(index), context);
Expand All @@ -482,7 +481,7 @@ namespace zenkit {
}
}

std::int32_t DaedalusSymbol::get_int(std::uint16_t index, std::shared_ptr<DaedalusInstance> 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);
}
Expand All @@ -497,7 +496,7 @@ namespace zenkit {

if (context->symbol_index() == static_cast<uint32_t>(-1) &&
context->_m_type == &typeid(DaedalusTransientInstance)) {
return reinterpret_cast<DaedalusTransientInstance&>(*context).get_int(*this, index);
return reinterpret_cast<DaedalusTransientInstance const&>(*context).get_int(*this, index);
}

return *get_member_ptr<std::int32_t>(index, context);
Expand All @@ -506,9 +505,7 @@ namespace zenkit {
}
}

void DaedalusSymbol::set_string(std::string_view value,
std::uint16_t index,
std::shared_ptr<DaedalusInstance> 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);
}
Expand All @@ -533,7 +530,7 @@ namespace zenkit {
}
}

void DaedalusSymbol::set_float(float value, std::uint16_t index, std::shared_ptr<DaedalusInstance> const& context) {
void DaedalusSymbol::set_float(float value, std::uint16_t index, DaedalusInstance* context) {
if (type() != DaedalusDataType::FLOAT) {
throw DaedalusIllegalTypeAccess(this, DaedalusDataType::FLOAT);
}
Expand All @@ -558,8 +555,7 @@ namespace zenkit {
}
}

void
DaedalusSymbol::set_int(std::int32_t value, std::uint16_t index, std::shared_ptr<DaedalusInstance> 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);
}
Expand Down
36 changes: 18 additions & 18 deletions src/DaedalusVm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<uint32_t>(index));
value = "&" + sym->name();
break;
Expand Down Expand Up @@ -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<DaedalusInstance>& context,
Expand All @@ -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<DaedalusInstance>& context,
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand Down

0 comments on commit 59d7930

Please sign in to comment.