2021-10-01 00:55:14 +02:00
|
|
|
#include <std_include.hpp>
|
|
|
|
#include "execution.hpp"
|
|
|
|
#include "types.hpp"
|
2021-10-21 00:21:50 +02:00
|
|
|
#include "script_value.hpp"
|
2021-10-01 00:55:14 +02:00
|
|
|
|
|
|
|
namespace ui_scripting
|
|
|
|
{
|
|
|
|
/***************************************************************
|
|
|
|
* Constructors
|
|
|
|
**************************************************************/
|
|
|
|
|
2021-10-21 00:21:50 +02:00
|
|
|
script_value::script_value(const game::hks::HksObject& value)
|
2021-10-01 00:55:14 +02:00
|
|
|
: value_(value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-21 00:21:50 +02:00
|
|
|
script_value::script_value(const int value)
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
game::hks::HksObject obj{};
|
|
|
|
obj.t = game::hks::TNUMBER;
|
|
|
|
obj.v.number = static_cast<float>(value);
|
|
|
|
|
|
|
|
this->value_ = obj;
|
|
|
|
}
|
|
|
|
|
2021-10-21 00:21:50 +02:00
|
|
|
script_value::script_value(const unsigned int value)
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
game::hks::HksObject obj{};
|
|
|
|
obj.t = game::hks::TNUMBER;
|
|
|
|
obj.v.number = static_cast<float>(value);
|
|
|
|
|
|
|
|
this->value_ = obj;
|
|
|
|
}
|
|
|
|
|
2021-10-21 00:21:50 +02:00
|
|
|
script_value::script_value(const bool value)
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
2021-10-21 00:21:50 +02:00
|
|
|
game::hks::HksObject obj{};
|
|
|
|
obj.t = game::hks::TBOOLEAN;
|
|
|
|
obj.v.boolean = value;
|
|
|
|
|
|
|
|
this->value_ = obj;
|
2021-10-01 00:55:14 +02:00
|
|
|
}
|
|
|
|
|
2021-10-21 00:21:50 +02:00
|
|
|
script_value::script_value(const float value)
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
game::hks::HksObject obj{};
|
|
|
|
obj.t = game::hks::TNUMBER;
|
|
|
|
obj.v.number = static_cast<float>(value);
|
|
|
|
|
|
|
|
this->value_ = obj;
|
|
|
|
}
|
|
|
|
|
2021-10-21 00:21:50 +02:00
|
|
|
script_value::script_value(const double value)
|
|
|
|
: script_value(static_cast<float>(value))
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-21 00:21:50 +02:00
|
|
|
script_value::script_value(const char* value)
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
game::hks::HksObject obj{};
|
|
|
|
|
|
|
|
const auto state = *game::hks::lua_state;
|
2021-12-03 20:39:23 +01:00
|
|
|
state->m_apistack.top = state->m_apistack.base;
|
|
|
|
|
2022-01-31 08:48:56 +01:00
|
|
|
game::hks::hksi_lua_pushlstring(state, value, static_cast<unsigned int>(strlen(value)));
|
|
|
|
obj = state->m_apistack.top[-1];
|
|
|
|
|
|
|
|
this->value_ = obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
script_value::script_value(const char* value, unsigned int len)
|
|
|
|
{
|
|
|
|
game::hks::HksObject obj{};
|
|
|
|
|
|
|
|
const auto state = *game::hks::lua_state;
|
|
|
|
state->m_apistack.top = state->m_apistack.base;
|
|
|
|
|
|
|
|
game::hks::hksi_lua_pushlstring(state, value, len);
|
2021-10-01 00:55:14 +02:00
|
|
|
obj = state->m_apistack.top[-1];
|
|
|
|
|
|
|
|
this->value_ = obj;
|
|
|
|
}
|
|
|
|
|
2021-10-21 00:21:50 +02:00
|
|
|
script_value::script_value(const std::string& value)
|
2022-01-31 08:48:56 +01:00
|
|
|
: script_value(value.data(), static_cast<unsigned int>(value.size()))
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-21 00:21:50 +02:00
|
|
|
script_value::script_value(const lightuserdata& value)
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
this->value_.t = game::hks::TLIGHTUSERDATA;
|
|
|
|
this->value_.v.ptr = value.ptr;
|
|
|
|
}
|
|
|
|
|
2021-10-21 00:21:50 +02:00
|
|
|
script_value::script_value(const userdata& value)
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
this->value_.t = game::hks::TUSERDATA;
|
|
|
|
this->value_.v.ptr = value.ptr;
|
|
|
|
}
|
|
|
|
|
2021-10-21 00:21:50 +02:00
|
|
|
script_value::script_value(const table& value)
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
this->value_.t = game::hks::TTABLE;
|
|
|
|
this->value_.v.ptr = value.ptr;
|
|
|
|
}
|
|
|
|
|
2021-10-21 00:21:50 +02:00
|
|
|
script_value::script_value(const function& value)
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
2021-10-02 01:08:04 +02:00
|
|
|
this->value_.t = value.type;
|
2021-10-01 00:55:14 +02:00
|
|
|
this->value_.v.ptr = value.ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************
|
|
|
|
* Integer
|
|
|
|
**************************************************************/
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
bool script_value::is<int>() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
2021-10-21 00:21:50 +02:00
|
|
|
const auto number = this->get_raw().v.number;
|
|
|
|
return this->get_raw().t == game::hks::TNUMBER && static_cast<int>(number) == number;
|
2021-10-01 00:55:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
bool script_value::is<unsigned int>() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
return this->is<int>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
int script_value::get() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
2021-10-21 00:21:50 +02:00
|
|
|
return static_cast<int>(this->get_raw().v.number);
|
2021-10-01 00:55:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
unsigned int script_value::get() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
2021-10-21 00:21:50 +02:00
|
|
|
return static_cast<unsigned int>(this->get_raw().v.number);
|
2021-10-01 00:55:14 +02:00
|
|
|
}
|
|
|
|
|
2021-10-21 00:21:50 +02:00
|
|
|
/***************************************************************
|
|
|
|
* Boolean
|
|
|
|
**************************************************************/
|
|
|
|
|
2021-10-01 00:55:14 +02:00
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
bool script_value::is<bool>() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
2021-10-21 00:21:50 +02:00
|
|
|
return this->get_raw().t == game::hks::TBOOLEAN;
|
2021-10-01 00:55:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
bool script_value::get() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
2021-10-21 00:21:50 +02:00
|
|
|
return this->get_raw().v.boolean;
|
2021-10-01 00:55:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************
|
|
|
|
* Float
|
|
|
|
**************************************************************/
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
bool script_value::is<float>() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
return this->get_raw().t == game::hks::TNUMBER;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
bool script_value::is<double>() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
return this->is<float>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
float script_value::get() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
return this->get_raw().v.number;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
double script_value::get() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
return static_cast<double>(this->get_raw().v.number);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************
|
|
|
|
* String
|
|
|
|
**************************************************************/
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
bool script_value::is<const char*>() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
return this->get_raw().t == game::hks::TSTRING;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
bool script_value::is<std::string>() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
return this->is<const char*>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
const char* script_value::get() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
return this->get_raw().v.str->m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
std::string script_value::get() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
return this->get<const char*>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************
|
|
|
|
* Lightuserdata
|
|
|
|
**************************************************************/
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
bool script_value::is<lightuserdata>() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
return this->get_raw().t == game::hks::TLIGHTUSERDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
lightuserdata script_value::get() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
return this->get_raw().v.ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************
|
|
|
|
* Userdata
|
|
|
|
**************************************************************/
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
bool script_value::is<userdata>() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
return this->get_raw().t == game::hks::TUSERDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
userdata script_value::get() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
return this->get_raw().v.ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************
|
|
|
|
* Table
|
|
|
|
**************************************************************/
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
bool script_value::is<table>() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
return this->get_raw().t == game::hks::TTABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
table script_value::get() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
return this->get_raw().v.table;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************
|
|
|
|
* Function
|
|
|
|
**************************************************************/
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
bool script_value::is<function>() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
2021-10-02 01:08:04 +02:00
|
|
|
return this->get_raw().t == game::hks::TIFUNCTION
|
|
|
|
|| this->get_raw().t == game::hks::TCFUNCTION;
|
2021-10-01 00:55:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2021-10-21 00:21:50 +02:00
|
|
|
function script_value::get() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
2021-10-21 00:21:50 +02:00
|
|
|
return { this->get_raw().v.cClosure, this->get_raw().t };
|
2021-10-01 00:55:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************
|
|
|
|
*
|
|
|
|
**************************************************************/
|
|
|
|
|
2021-10-21 00:21:50 +02:00
|
|
|
const game::hks::HksObject& script_value::get_raw() const
|
2021-10-01 00:55:14 +02:00
|
|
|
{
|
|
|
|
return this->value_;
|
|
|
|
}
|
2021-10-21 00:21:50 +02:00
|
|
|
|
|
|
|
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<std::string>() == other.get<std::string>();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->get_raw().v.native == other.get_raw().v.native;
|
|
|
|
}
|
2021-10-01 00:55:14 +02:00
|
|
|
}
|