LUI fixes I forgot to port

This commit is contained in:
Federico Cecchetto 2021-12-03 20:39:23 +01:00
parent 80261a1e2b
commit 0c1279658d
7 changed files with 312 additions and 254 deletions

View File

@ -1,6 +1,5 @@
#include <std_include.hpp> #include <std_include.hpp>
#include "execution.hpp" #include "execution.hpp"
#include "stack_isolation.hpp"
#include "component/ui_scripting.hpp" #include "component/ui_scripting.hpp"
#include <utils/string.hpp> #include <utils/string.hpp>
@ -41,8 +40,8 @@ namespace ui_scripting
arguments call_script_function(const function& function, const arguments& arguments) arguments call_script_function(const function& function, const arguments& arguments)
{ {
const auto state = *game::hks::lua_state; const auto state = *game::hks::lua_state;
state->m_apistack.top = state->m_apistack.base;
stack_isolation _;
push_value(function); push_value(function);
for (auto i = arguments.begin(); i != arguments.end(); ++i) for (auto i = arguments.begin(); i != arguments.end(); ++i)
{ {
@ -67,8 +66,8 @@ namespace ui_scripting
script_value get_field(const userdata& self, const script_value& key) script_value get_field(const userdata& self, const script_value& key)
{ {
const auto state = *game::hks::lua_state; const auto state = *game::hks::lua_state;
state->m_apistack.top = state->m_apistack.base;
stack_isolation _;
push_value(key); push_value(key);
const auto _1 = gsl::finally(&disable_error_hook); const auto _1 = gsl::finally(&disable_error_hook);
@ -93,8 +92,8 @@ namespace ui_scripting
script_value get_field(const table& self, const script_value& key) script_value get_field(const table& self, const script_value& key)
{ {
const auto state = *game::hks::lua_state; const auto state = *game::hks::lua_state;
state->m_apistack.top = state->m_apistack.base;
stack_isolation _;
push_value(key); push_value(key);
const auto _1 = gsl::finally(&disable_error_hook); const auto _1 = gsl::finally(&disable_error_hook);
@ -119,8 +118,7 @@ namespace ui_scripting
void set_field(const userdata& self, const script_value& key, const script_value& value) void set_field(const userdata& self, const script_value& key, const script_value& value)
{ {
const auto state = *game::hks::lua_state; const auto state = *game::hks::lua_state;
state->m_apistack.top = state->m_apistack.base;
stack_isolation _;
const auto _1 = gsl::finally(&disable_error_hook); const auto _1 = gsl::finally(&disable_error_hook);
enable_error_hook(); enable_error_hook();
@ -142,8 +140,7 @@ namespace ui_scripting
void set_field(const table& self, const script_value& key, const script_value& value) void set_field(const table& self, const script_value& key, const script_value& value)
{ {
const auto state = *game::hks::lua_state; const auto state = *game::hks::lua_state;
state->m_apistack.top = state->m_apistack.base;
stack_isolation _;
const auto _1 = gsl::finally(&disable_error_hook); const auto _1 = gsl::finally(&disable_error_hook);
enable_error_hook(); enable_error_hook();
@ -170,7 +167,9 @@ namespace ui_scripting
throw std::runtime_error("Function " + name + " not found"); throw std::runtime_error("Function " + name + " not found");
} }
stack_isolation _; const auto state = *game::hks::lua_state;
state->m_apistack.top = state->m_apistack.base;
push_value(self); push_value(self);
for (auto i = arguments.begin(); i != arguments.end(); ++i) for (auto i = arguments.begin(); i != arguments.end(); ++i)
{ {
@ -202,7 +201,9 @@ namespace ui_scripting
throw std::runtime_error("Function " + name + " not found"); throw std::runtime_error("Function " + name + " not found");
} }
stack_isolation _; const auto state = *game::hks::lua_state;
state->m_apistack.top = state->m_apistack.base;
for (auto i = arguments.begin(); i != arguments.end(); ++i) for (auto i = arguments.begin(); i != arguments.end(); ++i)
{ {
push_value(*i); push_value(*i);

View File

@ -1,7 +1,6 @@
#include <std_include.hpp> #include <std_include.hpp>
#include "value_conversion.hpp" #include "value_conversion.hpp"
#include "../execution.hpp" #include "../execution.hpp"
#include "../stack_isolation.hpp"
#include "../../../component/ui_scripting.hpp" #include "../../../component/ui_scripting.hpp"
namespace ui_scripting::lua namespace ui_scripting::lua

View File

@ -1,7 +1,6 @@
#include <std_include.hpp> #include <std_include.hpp>
#include "execution.hpp" #include "execution.hpp"
#include "types.hpp" #include "types.hpp"
#include "stack_isolation.hpp"
#include "script_value.hpp" #include "script_value.hpp"
namespace ui_scripting namespace ui_scripting
@ -59,9 +58,10 @@ namespace ui_scripting
script_value::script_value(const char* value) script_value::script_value(const char* value)
{ {
game::hks::HksObject obj{}; game::hks::HksObject obj{};
stack_isolation _;
const auto state = *game::hks::lua_state; const auto state = *game::hks::lua_state;
state->m_apistack.top = state->m_apistack.base;
game::hks::hksi_lua_pushlstring(state, value, (unsigned int)strlen(value)); game::hks::hksi_lua_pushlstring(state, value, (unsigned int)strlen(value));
obj = state->m_apistack.top[-1]; obj = state->m_apistack.top[-1];

View File

@ -1,11 +0,0 @@
#include <std_include.hpp>
#include "stack_isolation.hpp"
namespace ui_scripting
{
stack_isolation::stack_isolation()
{
const auto state = *game::hks::lua_state;
state->m_apistack.top = state->m_apistack.base;
}
}

View File

@ -1,16 +0,0 @@
#pragma once
#include "game/game.hpp"
namespace ui_scripting
{
class stack_isolation final
{
public:
stack_isolation();
stack_isolation(stack_isolation&&) = delete;
stack_isolation(const stack_isolation&) = delete;
stack_isolation& operator=(stack_isolation&&) = delete;
stack_isolation& operator=(const stack_isolation&) = delete;
};
}

View File

@ -1,7 +1,6 @@
#include <std_include.hpp> #include <std_include.hpp>
#include "types.hpp" #include "types.hpp"
#include "execution.hpp" #include "execution.hpp"
#include "stack_isolation.hpp"
namespace ui_scripting namespace ui_scripting
{ {
@ -21,6 +20,72 @@ namespace ui_scripting
userdata::userdata(void* ptr_) userdata::userdata(void* ptr_)
: ptr(ptr_) : ptr(ptr_)
{ {
this->add();
}
userdata::userdata(const userdata& other)
{
this->operator=(other);
}
userdata::userdata(userdata&& other) noexcept
{
this->ptr = other.ptr;
this->ref = other.ref;
other.ref = 0;
}
userdata::~userdata()
{
this->release();
}
userdata& userdata::operator=(const userdata& other)
{
if (&other != this)
{
this->release();
this->ptr = other.ptr;
this->ref = other.ref;
this->add();
}
return *this;
}
userdata& userdata::operator=(userdata&& other) noexcept
{
if (&other != this)
{
this->release();
this->ptr = other.ptr;
this->ref = other.ref;
other.ref = 0;
}
return *this;
}
void userdata::add()
{
game::hks::HksObject value{};
value.v.ptr = this->ptr;
value.t = game::hks::TUSERDATA;
const auto state = *game::hks::lua_state;
state->m_apistack.top = state->m_apistack.base;
push_value(value);
this->ref = game::hks::hksi_luaL_ref(*game::hks::lua_state, -10000);
}
void userdata::release()
{
if (this->ref)
{
game::hks::hksi_luaL_unref(*game::hks::lua_state, -10000, this->ref);
}
} }
void userdata::set(const script_value& key, const script_value& value) const void userdata::set(const script_value& key, const script_value& value) const
@ -50,8 +115,9 @@ namespace ui_scripting
this->add(); this->add();
} }
table::table(const table& other) : table(other.ptr) table::table(const table& other)
{ {
this->operator=(other);
} }
table::table(table&& other) noexcept table::table(table&& other) noexcept
@ -98,7 +164,9 @@ namespace ui_scripting
value.v.table = this->ptr; value.v.table = this->ptr;
value.t = game::hks::TTABLE; value.t = game::hks::TTABLE;
stack_isolation _; const auto state = *game::hks::lua_state;
state->m_apistack.top = state->m_apistack.base;
push_value(value); push_value(value);
this->ref = game::hks::hksi_luaL_ref(*game::hks::lua_state, -10000); this->ref = game::hks::hksi_luaL_ref(*game::hks::lua_state, -10000);
@ -133,8 +201,9 @@ namespace ui_scripting
this->add(); this->add();
} }
function::function(const function& other) : function(other.ptr, other.type) function::function(const function& other)
{ {
this->operator=(other);
} }
function::function(function&& other) noexcept function::function(function&& other) noexcept
@ -184,7 +253,9 @@ namespace ui_scripting
value.v.cClosure = this->ptr; value.v.cClosure = this->ptr;
value.t = this->type; value.t = this->type;
stack_isolation _; const auto state = *game::hks::lua_state;
state->m_apistack.top = state->m_apistack.base;
push_value(value); push_value(value);
this->ref = game::hks::hksi_luaL_ref(*game::hks::lua_state, -10000); this->ref = game::hks::hksi_luaL_ref(*game::hks::lua_state, -10000);

View File

@ -16,10 +16,24 @@ namespace ui_scripting
public: public:
userdata(void*); userdata(void*);
userdata(const userdata& other);
userdata(userdata&& other) noexcept;
~userdata();
userdata& operator=(const userdata& other);
userdata& operator=(userdata&& other) noexcept;
script_value get(const script_value& key) const; script_value get(const script_value& key) const;
void set(const script_value& key, const script_value& value) const; void set(const script_value& key, const script_value& value) const;
void* ptr; void* ptr;
private:
void add();
void release();
int ref{};
}; };
class table class table
@ -45,7 +59,7 @@ namespace ui_scripting
void add(); void add();
void release(); void release();
int ref; int ref{};
}; };
class function class function
@ -70,6 +84,6 @@ namespace ui_scripting
void add(); void add();
void release(); void release();
int ref; int ref{};
}; };
} }