Some fixes
This commit is contained in:
parent
96304dbdf5
commit
be5d909847
@ -21,8 +21,6 @@
|
|||||||
|
|
||||||
namespace ui_scripting
|
namespace ui_scripting
|
||||||
{
|
{
|
||||||
std::unordered_map<std::string, std::unordered_map<std::string, game::hks::lua_function>> libs;
|
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
utils::hook::detour hksi_open_lib_hook;
|
utils::hook::detour hksi_open_lib_hook;
|
||||||
@ -33,15 +31,23 @@ namespace ui_scripting
|
|||||||
|
|
||||||
bool error_hook_enabled = false;
|
bool error_hook_enabled = false;
|
||||||
|
|
||||||
|
std::unordered_map<std::string, game::hks::lua_function> functions;
|
||||||
|
std::unordered_map<std::string, game::hks::lua_function> methods;
|
||||||
|
|
||||||
void* hksi_open_lib_stub(game::hks::lua_State* s, const char* libname, game::hks::luaL_Reg* l)
|
void* hksi_open_lib_stub(game::hks::lua_State* s, const char* libname, game::hks::luaL_Reg* l)
|
||||||
{
|
{
|
||||||
if (libname)
|
for (auto i = l; i->name; ++i)
|
||||||
{
|
{
|
||||||
libs[libname] = {};
|
if (i->name == "__gc"s)
|
||||||
for (auto i = l; i->name; ++i)
|
|
||||||
{
|
{
|
||||||
libs[libname][i->name] = i->function;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const auto lower = utils::string::to_lower(i->name);
|
||||||
|
|
||||||
|
libname
|
||||||
|
? functions[lower] = i->function
|
||||||
|
: methods[lower] = i->function;
|
||||||
}
|
}
|
||||||
|
|
||||||
return hksi_open_lib_hook.invoke<void*>(s, libname, l);
|
return hksi_open_lib_hook.invoke<void*>(s, libname, l);
|
||||||
@ -75,7 +81,8 @@ namespace ui_scripting
|
|||||||
ui_scripting::lua::engine::start();
|
ui_scripting::lua::engine::start();
|
||||||
});
|
});
|
||||||
|
|
||||||
libs = {};
|
functions = {};
|
||||||
|
methods = {};
|
||||||
return hks_start_hook.invoke<void*>(a1);
|
return hks_start_hook.invoke<void*>(a1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,8 +97,8 @@ namespace ui_scripting
|
|||||||
if (name != "( lua_CFunction )LUI_CoD_LuaCall_UIExpression"s)
|
if (name != "( lua_CFunction )LUI_CoD_LuaCall_UIExpression"s)
|
||||||
{
|
{
|
||||||
std::string name_ = name;
|
std::string name_ = name;
|
||||||
const auto sub = name_.substr(13, name_.size() - 14);
|
const auto sub = utils::string::to_lower(name_.substr(13, name_.size() - 14));
|
||||||
libs["Global"][sub] = f;
|
functions[sub] = f;
|
||||||
}
|
}
|
||||||
|
|
||||||
hksi_add_function_hook.invoke<void>(s, f, a3, name, a5);
|
hksi_add_function_hook.invoke<void>(s, f, a3, name, a5);
|
||||||
@ -111,20 +118,26 @@ namespace ui_scripting
|
|||||||
game::hks::lua_function find_function(const std::string& name)
|
game::hks::lua_function find_function(const std::string& name)
|
||||||
{
|
{
|
||||||
const auto lower = utils::string::to_lower(name);
|
const auto lower = utils::string::to_lower(name);
|
||||||
|
if (functions.find(lower) == functions.end())
|
||||||
for (const auto lib : libs)
|
|
||||||
{
|
{
|
||||||
for (const auto func : lib.second)
|
return 0;
|
||||||
{
|
|
||||||
const auto lower_ = utils::string::to_lower(func.first);
|
|
||||||
if (lower == lower_)
|
|
||||||
{
|
|
||||||
return func.second;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return functions[lower];
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
game::hks::lua_function find_method(const std::string& name)
|
||||||
|
{
|
||||||
|
const auto lower = utils::string::to_lower(name);
|
||||||
|
if (methods.find(lower) == methods.end())
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return methods[lower];
|
||||||
|
}
|
||||||
|
|
||||||
|
void notify(const event& e)
|
||||||
|
{
|
||||||
|
lua::engine::notify(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
class component final : public component_interface
|
class component final : public component_interface
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "game/ui_scripting/menu.hpp"
|
#include "game/ui_scripting/menu.hpp"
|
||||||
|
#include "game/ui_scripting/event.hpp"
|
||||||
|
|
||||||
namespace ui_scripting
|
namespace ui_scripting
|
||||||
{
|
{
|
||||||
extern std::unordered_map<std::string, std::unordered_map<std::string, game::hks::lua_function>> libs;
|
|
||||||
|
|
||||||
void enable_error_hook();
|
void enable_error_hook();
|
||||||
void disable_error_hook();
|
void disable_error_hook();
|
||||||
|
|
||||||
game::hks::lua_function find_function(const std::string& name);
|
game::hks::lua_function find_function(const std::string& name);
|
||||||
|
game::hks::lua_function find_method(const std::string& name);
|
||||||
|
|
||||||
|
void notify(event e);
|
||||||
}
|
}
|
||||||
|
@ -153,5 +153,7 @@ namespace game
|
|||||||
WEAK symbol<lua_State*> lua_state{0x19D83E8};
|
WEAK symbol<lua_State*> lua_state{0x19D83E8};
|
||||||
WEAK symbol<void(lua_State* s, const char* str, unsigned int l)> hksi_lua_pushlstring{0x287410};
|
WEAK symbol<void(lua_State* s, const char* str, unsigned int l)> hksi_lua_pushlstring{0x287410};
|
||||||
WEAK symbol<const char*(lua_State* s, const HksObject* obj, unsigned int* len)> hks_obj_tolstring{0x287410};
|
WEAK symbol<const char*(lua_State* s, const HksObject* obj, unsigned int* len)> hks_obj_tolstring{0x287410};
|
||||||
|
WEAK symbol<int(lua_State* s, const HksObject* obj, HksObject* ret)> hks_obj_getmetatable{0x2DA210};
|
||||||
|
WEAK symbol<HksObject*(HksObject* result, lua_State* s, const HksObject* table, const HksObject* key)> hks_obj_getfield{0x2D9E20};
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
#include <std_include.hpp>
|
#include <std_include.hpp>
|
||||||
#include "value.hpp"
|
#include "value.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>
|
||||||
@ -59,7 +59,6 @@ namespace ui_scripting
|
|||||||
case 5:
|
case 5:
|
||||||
{
|
{
|
||||||
const auto data = std::get<lightuserdata>(value_);
|
const auto data = std::get<lightuserdata>(value_);
|
||||||
|
|
||||||
state->top->t = game::hks::HksObjectType::TLIGHTUSERDATA;
|
state->top->t = game::hks::HksObjectType::TLIGHTUSERDATA;
|
||||||
state->top->v.ptr = data.ptr;
|
state->top->v.ptr = data.ptr;
|
||||||
state->top++;
|
state->top++;
|
||||||
@ -125,16 +124,15 @@ namespace ui_scripting
|
|||||||
throw std::runtime_error("Invalid lua state");
|
throw std::runtime_error("Invalid lua state");
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto state = *game::hks::lua_state;
|
|
||||||
state->top = state->base;
|
|
||||||
|
|
||||||
const auto function = ui_scripting::find_function(name);
|
const auto function = ui_scripting::find_function(name);
|
||||||
if (!function)
|
if (!function)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("Function " + name + " not found");
|
throw std::runtime_error("Function " + name + " not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto _ = gsl::finally([]()
|
stack_isolation _;
|
||||||
|
|
||||||
|
const auto __ = gsl::finally([]()
|
||||||
{
|
{
|
||||||
disable_error_hook();
|
disable_error_hook();
|
||||||
});
|
});
|
||||||
@ -156,6 +154,11 @@ namespace ui_scripting
|
|||||||
values.push_back(get_return_value(i));
|
values.push_back(get_return_value(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (values.size() == 0)
|
||||||
|
{
|
||||||
|
values.push_back({});
|
||||||
|
}
|
||||||
|
|
||||||
return values;
|
return values;
|
||||||
}
|
}
|
||||||
catch (const std::exception& e)
|
catch (const std::exception& e)
|
||||||
|
@ -515,7 +515,7 @@ namespace ui_scripting::lua
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lua::engine::notify(event);
|
notify(event);
|
||||||
};
|
};
|
||||||
|
|
||||||
element_type["hidden"] = sol::property(
|
element_type["hidden"] = sol::property(
|
||||||
@ -589,7 +589,7 @@ namespace ui_scripting::lua
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lua::engine::notify(event);
|
notify(event);
|
||||||
};
|
};
|
||||||
|
|
||||||
menu_type["addchild"] = [](const sol::this_state s, menu& menu, element& element)
|
menu_type["addchild"] = [](const sol::this_state s, menu& menu, element& element)
|
||||||
@ -640,7 +640,7 @@ namespace ui_scripting::lua
|
|||||||
event event;
|
event event;
|
||||||
event.element = &menu;
|
event.element = &menu;
|
||||||
event.name = "close";
|
event.name = "close";
|
||||||
handler.dispatch(event);
|
notify(event);
|
||||||
|
|
||||||
menu.open();
|
menu.open();
|
||||||
};
|
};
|
||||||
@ -650,7 +650,7 @@ namespace ui_scripting::lua
|
|||||||
event event;
|
event event;
|
||||||
event.element = &menu;
|
event.element = &menu;
|
||||||
event.name = "close";
|
event.name = "close";
|
||||||
handler.dispatch(event);
|
notify(event);
|
||||||
|
|
||||||
menu.close();
|
menu.close();
|
||||||
};
|
};
|
||||||
@ -809,7 +809,7 @@ namespace ui_scripting::lua
|
|||||||
event event;
|
event event;
|
||||||
event.element = menu;
|
event.element = menu;
|
||||||
event.name = "open";
|
event.name = "open";
|
||||||
handler.dispatch(event);
|
notify(event);
|
||||||
|
|
||||||
menu->open();
|
menu->open();
|
||||||
};
|
};
|
||||||
@ -826,7 +826,7 @@ namespace ui_scripting::lua
|
|||||||
event event;
|
event event;
|
||||||
event.element = menu;
|
event.element = menu;
|
||||||
event.name = "close";
|
event.name = "close";
|
||||||
handler.dispatch(event);
|
notify(event);
|
||||||
|
|
||||||
menu->close();
|
menu->close();
|
||||||
};
|
};
|
||||||
@ -953,11 +953,6 @@ namespace ui_scripting::lua
|
|||||||
::game::Dvar_SetCommand(hash, "", string_value.data());
|
::game::Dvar_SetCommand(hash, "", string_value.data());
|
||||||
};
|
};
|
||||||
|
|
||||||
game_type["playsound"] = [](const game&, const std::string& sound)
|
|
||||||
{
|
|
||||||
::game::UI_PlayLocalSoundAlias(0, sound.data());
|
|
||||||
};
|
|
||||||
|
|
||||||
game_type["getwindowsize"] = [](const game&, const sol::this_state s)
|
game_type["getwindowsize"] = [](const game&, const sol::this_state s)
|
||||||
{
|
{
|
||||||
const auto size = ::game::ScrPlace_GetViewPlacement()->realViewportSize;
|
const auto size = ::game::ScrPlace_GetViewPlacement()->realViewportSize;
|
||||||
@ -1001,14 +996,7 @@ namespace ui_scripting::lua
|
|||||||
}
|
}
|
||||||
|
|
||||||
const auto values = call(name, arguments);
|
const auto values = call(name, arguments);
|
||||||
if (values.size() == 0)
|
return sol::as_returns(values);
|
||||||
{
|
|
||||||
return sol::lua_value{s, sol::lua_nil};
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return sol::lua_value{s, sol::as_returns(values)};
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct player
|
struct player
|
||||||
|
@ -10,7 +10,6 @@
|
|||||||
#define SOL_PRINT_ERRORS 0
|
#define SOL_PRINT_ERRORS 0
|
||||||
#include <sol/sol.hpp>
|
#include <sol/sol.hpp>
|
||||||
|
|
||||||
#include "engine.hpp"
|
|
||||||
#include "scheduler.hpp"
|
#include "scheduler.hpp"
|
||||||
#include "event_handler.hpp"
|
#include "event_handler.hpp"
|
||||||
|
|
||||||
|
30
src/client/game/ui_scripting/stack_isolation.cpp
Normal file
30
src/client/game/ui_scripting/stack_isolation.cpp
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#include <std_include.hpp>
|
||||||
|
#include "stack_isolation.hpp"
|
||||||
|
|
||||||
|
namespace ui_scripting
|
||||||
|
{
|
||||||
|
stack_isolation::stack_isolation()
|
||||||
|
{
|
||||||
|
const auto state = *game::hks::lua_state;
|
||||||
|
|
||||||
|
this->top_ = state->top;
|
||||||
|
this->base_ = state->base;
|
||||||
|
this->alloc_top_ = state->alloc_top;
|
||||||
|
this->bottom_ = state->bottom;
|
||||||
|
|
||||||
|
state->top = this->stack_;
|
||||||
|
state->base = state->top;
|
||||||
|
state->alloc_top = &this->stack_[ARRAYSIZE(this->stack_) - 1];
|
||||||
|
state->bottom = state->top;
|
||||||
|
}
|
||||||
|
|
||||||
|
stack_isolation::~stack_isolation()
|
||||||
|
{
|
||||||
|
const auto state = *game::hks::lua_state;
|
||||||
|
|
||||||
|
state->top = this->top_;
|
||||||
|
state->base = this->base_;
|
||||||
|
state->alloc_top = this->alloc_top_;
|
||||||
|
state->bottom = this->bottom_;
|
||||||
|
}
|
||||||
|
}
|
25
src/client/game/ui_scripting/stack_isolation.hpp
Normal file
25
src/client/game/ui_scripting/stack_isolation.hpp
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "game/game.hpp"
|
||||||
|
|
||||||
|
namespace ui_scripting
|
||||||
|
{
|
||||||
|
class stack_isolation final
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
stack_isolation();
|
||||||
|
~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;
|
||||||
|
|
||||||
|
private:
|
||||||
|
game::hks::HksObject stack_[512]{};
|
||||||
|
|
||||||
|
game::hks::HksObject* top_;
|
||||||
|
game::hks::HksObject* base_;
|
||||||
|
game::hks::HksObject* alloc_top_;
|
||||||
|
game::hks::HksObject* bottom_;
|
||||||
|
};
|
||||||
|
}
|
@ -6,7 +6,6 @@ namespace ui_scripting
|
|||||||
struct lightuserdata
|
struct lightuserdata
|
||||||
{
|
{
|
||||||
void* ptr;
|
void* ptr;
|
||||||
|
|
||||||
bool operator==(const lightuserdata other) const noexcept
|
bool operator==(const lightuserdata other) const noexcept
|
||||||
{
|
{
|
||||||
return this->ptr == other.ptr;
|
return this->ptr == other.ptr;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user