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,205 +1,276 @@
#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
{ {
/*************************************************************** /***************************************************************
* Lightuserdata * Lightuserdata
**************************************************************/ **************************************************************/
lightuserdata::lightuserdata(void* ptr_) lightuserdata::lightuserdata(void* ptr_)
: ptr(ptr_) : ptr(ptr_)
{ {
} }
/*************************************************************** /***************************************************************
* Userdata * Userdata
**************************************************************/ **************************************************************/
userdata::userdata(void* ptr_) userdata::userdata(void* ptr_)
: ptr(ptr_) : ptr(ptr_)
{ {
} this->add();
}
void userdata::set(const script_value& key, const script_value& value) const userdata::userdata(const userdata& other)
{ {
set_field(*this, key, value); this->operator=(other);
} }
script_value userdata::get(const script_value& key) const userdata::userdata(userdata&& other) noexcept
{ {
return get_field(*this, key); this->ptr = other.ptr;
} this->ref = other.ref;
other.ref = 0;
}
/*************************************************************** userdata::~userdata()
* Table {
**************************************************************/ this->release();
}
table::table() userdata& userdata::operator=(const userdata& other)
{ {
const auto state = *game::hks::lua_state; if (&other != this)
this->ptr = game::hks::Hashtable_Create(state, 0, 0); {
this->add(); this->release();
} this->ptr = other.ptr;
this->ref = other.ref;
this->add();
}
table::table(game::hks::HashTable* ptr_) return *this;
: ptr(ptr_) }
{
this->add();
}
table::table(const table& other) : table(other.ptr) userdata& userdata::operator=(userdata&& other) noexcept
{ {
} if (&other != this)
{
this->release();
this->ptr = other.ptr;
this->ref = other.ref;
other.ref = 0;
}
table::table(table&& other) noexcept return *this;
{ }
this->ptr = other.ptr;
this->ref = other.ref;
other.ref = 0;
}
table::~table() void userdata::add()
{ {
this->release(); game::hks::HksObject value{};
} value.v.ptr = this->ptr;
value.t = game::hks::TUSERDATA;
table& table::operator=(const table& other) const auto state = *game::hks::lua_state;
{ state->m_apistack.top = state->m_apistack.base;
if (&other != this)
{
this->release();
this->ptr = other.ptr;
this->ref = other.ref;
this->add();
}
return *this; push_value(value);
}
table& table::operator=(table&& other) noexcept this->ref = game::hks::hksi_luaL_ref(*game::hks::lua_state, -10000);
{ }
if (&other != this)
{
this->release();
this->ptr = other.ptr;
this->ref = other.ref;
other.ref = 0;
}
return *this; void userdata::release()
} {
if (this->ref)
{
game::hks::hksi_luaL_unref(*game::hks::lua_state, -10000, this->ref);
}
}
void table::add() void userdata::set(const script_value& key, const script_value& value) const
{ {
game::hks::HksObject value{}; set_field(*this, key, value);
value.v.table = this->ptr; }
value.t = game::hks::TTABLE;
stack_isolation _; script_value userdata::get(const script_value& key) const
push_value(value); {
return get_field(*this, key);
}
this->ref = game::hks::hksi_luaL_ref(*game::hks::lua_state, -10000); /***************************************************************
} * Table
**************************************************************/
void table::release() table::table()
{ {
if (this->ref) const auto state = *game::hks::lua_state;
{ this->ptr = game::hks::Hashtable_Create(state, 0, 0);
game::hks::hksi_luaL_unref(*game::hks::lua_state, -10000, this->ref); this->add();
} }
}
void table::set(const script_value& key, const script_value& value) const table::table(game::hks::HashTable* ptr_)
{ : ptr(ptr_)
set_field(*this, key, value); {
} this->add();
}
script_value table::get(const script_value& key) const table::table(const table& other)
{ {
return get_field(*this, key); this->operator=(other);
} }
/*************************************************************** table::table(table&& other) noexcept
* Function {
**************************************************************/ this->ptr = other.ptr;
this->ref = other.ref;
other.ref = 0;
}
function::function(game::hks::cclosure* ptr_, game::hks::HksObjectType type_) table::~table()
: ptr(ptr_) {
, type(type_) this->release();
{ }
this->add();
}
function::function(const function& other) : function(other.ptr, other.type) table& table::operator=(const table& other)
{ {
} if (&other != this)
{
this->release();
this->ptr = other.ptr;
this->ref = other.ref;
this->add();
}
function::function(function&& other) noexcept return *this;
{ }
this->ptr = other.ptr;
this->type = other.type;
this->ref = other.ref;
other.ref = 0;
}
function::~function() table& table::operator=(table&& other) noexcept
{ {
this->release(); if (&other != this)
} {
this->release();
this->ptr = other.ptr;
this->ref = other.ref;
other.ref = 0;
}
function& function::operator=(const function& other) return *this;
{ }
if (&other != this)
{
this->release();
this->ptr = other.ptr;
this->type = other.type;
this->ref = other.ref;
this->add();
}
return *this; void table::add()
} {
game::hks::HksObject value{};
value.v.table = this->ptr;
value.t = game::hks::TTABLE;
function& function::operator=(function&& other) noexcept const auto state = *game::hks::lua_state;
{ state->m_apistack.top = state->m_apistack.base;
if (&other != this)
{
this->release();
this->ptr = other.ptr;
this->type = other.type;
this->ref = other.ref;
other.ref = 0;
}
return *this; push_value(value);
}
void function::add() this->ref = game::hks::hksi_luaL_ref(*game::hks::lua_state, -10000);
{ }
game::hks::HksObject value{};
value.v.cClosure = this->ptr;
value.t = this->type;
stack_isolation _; void table::release()
push_value(value); {
if (this->ref)
{
game::hks::hksi_luaL_unref(*game::hks::lua_state, -10000, this->ref);
}
}
this->ref = game::hks::hksi_luaL_ref(*game::hks::lua_state, -10000); void table::set(const script_value& key, const script_value& value) const
} {
set_field(*this, key, value);
}
void function::release() script_value table::get(const script_value& key) const
{ {
if (this->ref) return get_field(*this, key);
{ }
game::hks::hksi_luaL_unref(*game::hks::lua_state, -10000, this->ref);
}
}
arguments function::call(const arguments& arguments) const /***************************************************************
{ * Function
return call_script_function(*this, arguments); **************************************************************/
}
function::function(game::hks::cclosure* ptr_, game::hks::HksObjectType type_)
: ptr(ptr_)
, type(type_)
{
this->add();
}
function::function(const function& other)
{
this->operator=(other);
}
function::function(function&& other) noexcept
{
this->ptr = other.ptr;
this->type = other.type;
this->ref = other.ref;
other.ref = 0;
}
function::~function()
{
this->release();
}
function& function::operator=(const function& other)
{
if (&other != this)
{
this->release();
this->ptr = other.ptr;
this->type = other.type;
this->ref = other.ref;
this->add();
}
return *this;
}
function& function::operator=(function&& other) noexcept
{
if (&other != this)
{
this->release();
this->ptr = other.ptr;
this->type = other.type;
this->ref = other.ref;
other.ref = 0;
}
return *this;
}
void function::add()
{
game::hks::HksObject value{};
value.v.cClosure = this->ptr;
value.t = this->type;
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 function::release()
{
if (this->ref)
{
game::hks::hksi_luaL_unref(*game::hks::lua_state, -10000, this->ref);
}
}
arguments function::call(const arguments& arguments) const
{
return call_script_function(*this, arguments);
}
} }

View File

@ -4,72 +4,86 @@
namespace ui_scripting namespace ui_scripting
{ {
class lightuserdata class lightuserdata
{ {
public: public:
lightuserdata(void*); lightuserdata(void*);
void* ptr; void* ptr;
}; };
class userdata class userdata
{ {
public: public:
userdata(void*); userdata(void*);
script_value get(const script_value& key) const; userdata(const userdata& other);
void set(const script_value& key, const script_value& value) const; userdata(userdata&& other) noexcept;
void* ptr; ~userdata();
};
class table userdata& operator=(const userdata& other);
{ userdata& operator=(userdata&& other) noexcept;
public:
table();
table(game::hks::HashTable* ptr_);
table(const table& other); script_value get(const script_value& key) const;
table(table&& other) noexcept; void set(const script_value& key, const script_value& value) const;
~table(); void* ptr;
table& operator=(const table& other); private:
table& operator=(table&& other) noexcept; void add();
void release();
script_value get(const script_value& key) const; int ref{};
void set(const script_value& key, const script_value& value) const; };
game::hks::HashTable* ptr; class table
{
public:
table();
table(game::hks::HashTable* ptr_);
private: table(const table& other);
void add(); table(table&& other) noexcept;
void release();
int ref; ~table();
};
class function table& operator=(const table& other);
{ table& operator=(table&& other) noexcept;
public:
function(game::hks::cclosure*, game::hks::HksObjectType);
function(const function& other); script_value get(const script_value& key) const;
function(function&& other) noexcept; void set(const script_value& key, const script_value& value) const;
~function(); game::hks::HashTable* ptr;
function& operator=(const function& other); private:
function& operator=(function&& other) noexcept; void add();
void release();
arguments call(const arguments& arguments) const; int ref{};
};
game::hks::cclosure* ptr; class function
game::hks::HksObjectType type; {
public:
function(game::hks::cclosure*, game::hks::HksObjectType);
private: function(const function& other);
void add(); function(function&& other) noexcept;
void release();
int ref; ~function();
};
function& operator=(const function& other);
function& operator=(function&& other) noexcept;
arguments call(const arguments& arguments) const;
game::hks::cclosure* ptr;
game::hks::HksObjectType type;
private:
void add();
void release();
int ref{};
};
} }