#include #include "execution.hpp" #include "types.hpp" #include "stack_isolation.hpp" #include "script_value.hpp" namespace ui_scripting { /*************************************************************** * Constructors **************************************************************/ script_value::script_value(const game::hks::HksObject& value) : value_(value) { } script_value::script_value(const int value) { game::hks::HksObject obj{}; obj.t = game::hks::TNUMBER; obj.v.number = static_cast(value); this->value_ = obj; } script_value::script_value(const unsigned int value) { game::hks::HksObject obj{}; obj.t = game::hks::TNUMBER; obj.v.number = static_cast(value); this->value_ = obj; } script_value::script_value(const bool value) { game::hks::HksObject obj{}; obj.t = game::hks::TBOOLEAN; obj.v.boolean = value; this->value_ = obj; } script_value::script_value(const float value) { game::hks::HksObject obj{}; obj.t = game::hks::TNUMBER; obj.v.number = static_cast(value); this->value_ = obj; } script_value::script_value(const double value) : script_value(static_cast(value)) { } script_value::script_value(const char* value) { game::hks::HksObject obj{}; stack_isolation _; const auto state = *game::hks::lua_state; game::hks::hksi_lua_pushlstring(state, value, (unsigned int)strlen(value)); obj = state->m_apistack.top[-1]; this->value_ = obj; } script_value::script_value(const std::string& value) : script_value(value.data()) { } script_value::script_value(const lightuserdata& value) { this->value_.t = game::hks::TLIGHTUSERDATA; this->value_.v.ptr = value.ptr; } script_value::script_value(const userdata& value) { this->value_.t = game::hks::TUSERDATA; this->value_.v.ptr = value.ptr; } script_value::script_value(const table& value) { this->value_.t = game::hks::TTABLE; this->value_.v.ptr = value.ptr; } script_value::script_value(const function& value) { this->value_.t = value.type; this->value_.v.ptr = value.ptr; } /*************************************************************** * Integer **************************************************************/ template <> bool script_value::is() const { const auto number = this->get_raw().v.number; return this->get_raw().t == game::hks::TNUMBER && static_cast(number) == number; } template <> bool script_value::is() const { return this->is(); } template <> int script_value::get() const { return static_cast(this->get_raw().v.number); } template <> unsigned int script_value::get() const { return static_cast(this->get_raw().v.number); } /*************************************************************** * Boolean **************************************************************/ template <> bool script_value::is() const { return this->get_raw().t == game::hks::TBOOLEAN; } template <> bool script_value::get() const { return this->get_raw().v.boolean; } /*************************************************************** * Float **************************************************************/ template <> bool script_value::is() const { return this->get_raw().t == game::hks::TNUMBER; } template <> bool script_value::is() const { return this->is(); } template <> float script_value::get() const { return this->get_raw().v.number; } template <> double script_value::get() const { return static_cast(this->get_raw().v.number); } /*************************************************************** * String **************************************************************/ template <> bool script_value::is() const { return this->get_raw().t == game::hks::TSTRING; } template <> bool script_value::is() const { return this->is(); } template <> const char* script_value::get() const { return this->get_raw().v.str->m_data; } template <> std::string script_value::get() const { return this->get(); } /*************************************************************** * Lightuserdata **************************************************************/ template <> bool script_value::is() const { return this->get_raw().t == game::hks::TLIGHTUSERDATA; } template <> lightuserdata script_value::get() const { return this->get_raw().v.ptr; } /*************************************************************** * Userdata **************************************************************/ template <> bool script_value::is() const { return this->get_raw().t == game::hks::TUSERDATA; } template <> userdata script_value::get() const { return this->get_raw().v.ptr; } /*************************************************************** * Table **************************************************************/ template <> bool script_value::is() const { return this->get_raw().t == game::hks::TTABLE; } template <> table script_value::get() const { return this->get_raw().v.table; } /*************************************************************** * Function **************************************************************/ template <> bool script_value::is() const { return this->get_raw().t == game::hks::TIFUNCTION || this->get_raw().t == game::hks::TCFUNCTION; } template <> function script_value::get() const { return { this->get_raw().v.cClosure, this->get_raw().t }; } /*************************************************************** * **************************************************************/ const game::hks::HksObject& script_value::get_raw() const { return this->value_; } bool script_value::operator==(const script_value& other) { if (this->get_raw().t != other.get_raw().t) { return false; } if (this->get_raw().t == game::hks::TSTRING) { return this->get() == other.get(); } return this->get_raw().v.native == other.get_raw().v.native; } }