From 9a97d52e2d07fdd8b5ce6ba4f85a23c66555e249 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sun, 16 Jan 2022 10:42:09 +0100 Subject: [PATCH] Small fixes --- src/game/scripting/entity.cpp | 14 ++----- src/game/scripting/variable_value.cpp | 58 ++++++++++++++++++++++++--- src/game/scripting/variable_value.hpp | 18 +++++++-- src/utils/compression.cpp | 6 +-- 4 files changed, 72 insertions(+), 24 deletions(-) diff --git a/src/game/scripting/entity.cpp b/src/game/scripting/entity.cpp index 2cdc52f..484fce8 100644 --- a/src/game/scripting/entity.cpp +++ b/src/game/scripting/entity.cpp @@ -13,13 +13,7 @@ namespace game::scripting entity::entity(entity&& other) noexcept { - if (&other == this) return; - - this->context_ = other.context_; - this->entity_id_ = other.entity_id_; - - other.context_ = nullptr; - other.entity_id_ = 0; + this->operator=(std::move(other)); } entity::entity(context* context, const unsigned int entity_id) : context_(context), entity_id_(entity_id) @@ -36,7 +30,7 @@ namespace game::scripting { if (&other != this) { - this->release(); + this->~entity(); this->context_ = other.context_; this->entity_id_ = other.entity_id_; @@ -51,7 +45,7 @@ namespace game::scripting { if (&other != this) { - this->release(); + this->~entity(); this->context_ = other.context_; this->entity_id_ = other.entity_id_; @@ -114,7 +108,7 @@ namespace game::scripting { if (this->entity_id_) { - native::VariableValue value; + native::VariableValue value{}; value.type = native::SCRIPT_OBJECT; value.u.entityId = this->entity_id_; native::AddRefToValue(&value); diff --git a/src/game/scripting/variable_value.cpp b/src/game/scripting/variable_value.cpp index 8b798c7..b1d7f91 100644 --- a/src/game/scripting/variable_value.cpp +++ b/src/game/scripting/variable_value.cpp @@ -1,20 +1,68 @@ -#include "std_include.hpp" +#include #include "variable_value.hpp" namespace game::scripting { - variable_value::variable_value(native::VariableValue value) : value_(value) + variable_value::variable_value(const native::VariableValue& value) { - native::AddRefToValue(&value); + this->assign(value); + } + + variable_value::variable_value(const variable_value& other) noexcept + { + this->operator=(other); + } + + variable_value::variable_value(variable_value&& other) noexcept + { + this->operator=(std::move(other)); + } + + variable_value& variable_value::operator=(const variable_value& other) noexcept + { + if (this != &other) + { + this->release(); + this->assign(other.value_); + } + + return *this; + } + + variable_value& variable_value::operator=(variable_value&& other) noexcept + { + if (this != &other) + { + this->release(); + this->value_ = other.value_; + other.value_.type = native::SCRIPT_NONE; + } + + return *this; } variable_value::~variable_value() { - native::RemoveRefToValue(this->value_.type, this->value_.u); + this->release(); } - variable_value::operator native::VariableValue() const + const native::VariableValue& variable_value::get() const { return this->value_; } + + void variable_value::assign(const native::VariableValue& value) + { + this->value_ = value; + native::AddRefToValue(&this->value_); + } + + void variable_value::release() + { + if (this->value_.type != native::SCRIPT_NONE) + { + native::RemoveRefToValue(this->value_.type, this->value_.u); + this->value_.type = native::SCRIPT_NONE; + } + } } diff --git a/src/game/scripting/variable_value.hpp b/src/game/scripting/variable_value.hpp index fb2a8c6..34361a4 100644 --- a/src/game/scripting/variable_value.hpp +++ b/src/game/scripting/variable_value.hpp @@ -3,15 +3,25 @@ namespace game::scripting { - class variable_value final + class variable_value { public: - explicit variable_value(native::VariableValue value); + variable_value() = default; + variable_value(const native::VariableValue& value); + variable_value(const variable_value& other) noexcept; + variable_value(variable_value&& other) noexcept; + + variable_value& operator=(const variable_value& other) noexcept; + variable_value& operator=(variable_value&& other) noexcept; + ~variable_value(); - explicit operator native::VariableValue() const; + const native::VariableValue& get() const; private: - native::VariableValue value_; + void assign(const native::VariableValue& value); + void release(); + + native::VariableValue value_{{0}, native::SCRIPT_NONE}; }; } diff --git a/src/utils/compression.cpp b/src/utils/compression.cpp index 57dfed5..26991e3 100644 --- a/src/utils/compression.cpp +++ b/src/utils/compression.cpp @@ -7,11 +7,7 @@ namespace utils::compression std::string zlib::compress(const std::string& data) { memory::allocator allocator; - unsigned long length = (data.size() * 2); - if (!length) length = 2; - - if (length < 100) length *= 10; - + unsigned long length = compressBound(data.size()); const auto buffer = allocator.allocate_array(length); if (compress2(reinterpret_cast(buffer), &length, reinterpret_cast(const_cast(data.data())), data.size(),