Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
665 changes: 665 additions & 0 deletions docs/lua-api.md

Large diffs are not rendered by default.

30,760 changes: 30,760 additions & 0 deletions docs/natives.lua

Large diffs are not rendered by default.

1,518 changes: 1,518 additions & 0 deletions docs/yimmenu_v2.lua

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/core/commands/Command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ namespace YimMenu

public:
Command(std::string name, std::string label, std::string description, int num_args = 0);
virtual ~Command() = default;
void Call();

// Lua-created commands override this so runtime commands don't leave entries behind in the config file.
virtual bool ShouldSaveState() const
{
return true;
}

virtual void SaveState(nlohmann::json& value) {};
virtual void LoadState(nlohmann::json& value) {};

Expand Down
18 changes: 18 additions & 0 deletions src/core/commands/Commands.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Commands.hpp"
#include "Command.hpp"
#include "BoolCommand.hpp"
#include "LoopedCommand.hpp"
#include "core/backend/ScriptMgr.hpp"

Expand Down Expand Up @@ -35,6 +36,17 @@ namespace YimMenu
m_LoopedCommands.push_back(command);
}

void Commands::RemoveCommandImpl(Command* command)
{
if (!command)
return;

m_Commands.erase(command->GetHash());

std::erase(m_BoolCommands, dynamic_cast<BoolCommand*>(command));
std::erase(m_LoopedCommands, dynamic_cast<LoopedCommand*>(command));
}

void Commands::EnableBoolCommandsImpl()
{
for (auto& command : m_BoolCommands)
Expand All @@ -60,6 +72,9 @@ namespace YimMenu
{
for (auto& command : m_Commands)
{
if (!command.second->ShouldSaveState())
continue;

if (!state.contains(command.second->GetName()))
state[command.second->GetName()] = nlohmann::json::object();

Expand All @@ -71,6 +86,9 @@ namespace YimMenu
{
for (auto& command : m_Commands)
{
if (!command.second->ShouldSaveState())
continue;

if (state.contains(command.second->GetName()))
command.second->LoadState(state[command.second->GetName()]);
}
Expand Down
6 changes: 6 additions & 0 deletions src/core/commands/Commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ namespace YimMenu
{
GetInstance().AddLoopedCommandImpl(command);
}

static void RemoveCommand(Command* command)
{
GetInstance().RemoveCommandImpl(command);
}

static void RunLoopedCommands()
{
Expand Down Expand Up @@ -75,6 +80,7 @@ namespace YimMenu
void AddCommandImpl(Command* command);
void AddBoolCommandImpl(BoolCommand* command);
void AddLoopedCommandImpl(LoopedCommand* command);
void RemoveCommandImpl(Command* command);
void EnableBoolCommandsImpl();
void RunLoopedCommandsImpl();
Command* GetCommandImpl(joaat_t hash);
Expand Down
10 changes: 10 additions & 0 deletions src/core/frontend/manager/Category.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ namespace YimMenu
m_Items.insert(m_Items.begin(), std::move(item));
}

void RemoveItem(const std::shared_ptr<UIItem>& item)
{
std::erase(m_Items, item);
}

const std::vector<std::shared_ptr<UIItem>>& GetItems() const
{
return m_Items;
}

void Draw();
int GetLength();

Expand Down
7 changes: 7 additions & 0 deletions src/core/frontend/manager/Submenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ namespace YimMenu
m_Categories.push_back(std::move(category));
}

void Submenu::RemoveCategory(const std::shared_ptr<Category>& category)
{
std::erase(m_Categories, category);
if (m_ActiveCategory == category)
m_ActiveCategory = m_Categories.empty() ? nullptr : m_Categories.front();
}

void Submenu::DrawCategorySelectors()
{
for (auto& category : m_Categories)
Expand Down
1 change: 1 addition & 0 deletions src/core/frontend/manager/Submenu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace YimMenu
}

void AddCategory(std::shared_ptr<Category>&& category);
void RemoveCategory(const std::shared_ptr<Category>& category);
void DrawCategorySelectors();
void SetActiveCategory(const std::shared_ptr<Category> category);
void Draw();
Expand Down
8 changes: 8 additions & 0 deletions src/core/frontend/manager/UIManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ namespace YimMenu
m_Submenus.push_back(std::move(submenu));
}

void UIManager::RemoveSubmenuImpl(const std::shared_ptr<Submenu>& submenu)
{
std::erase(m_Submenus, submenu);

if (m_ActiveSubmenu == submenu)
m_ActiveSubmenu = m_Submenus.empty() ? nullptr : m_Submenus.front();
}

void UIManager::SetActiveSubmenuImpl(const std::shared_ptr<Submenu> submenu)
{
m_ActiveSubmenu = submenu;
Expand Down
6 changes: 6 additions & 0 deletions src/core/frontend/manager/UIManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ namespace YimMenu
GetInstance().AddSubmenuImpl(std::move(submenu));
}

static void RemoveSubmenu(const std::shared_ptr<Submenu>& submenu)
{
GetInstance().RemoveSubmenuImpl(submenu);
}

static void SetActiveSubmenu(const std::shared_ptr<Submenu> submenu)
{
GetInstance().SetActiveSubmenuImpl(submenu);
Expand Down Expand Up @@ -75,6 +80,7 @@ namespace YimMenu
}

void AddSubmenuImpl(const std::shared_ptr<Submenu>&& submenu);
void RemoveSubmenuImpl(const std::shared_ptr<Submenu>& submenu);
void SetActiveSubmenuImpl(const std::shared_ptr<Submenu> submenu);
void DrawImpl();
std::shared_ptr<Submenu> GetActiveSubmenuImpl();
Expand Down
7 changes: 7 additions & 0 deletions src/core/memory/PatternScanner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ namespace YimMenu

template<Signature S>
void Add(const Pattern<S>& pattern, const PatternFunc& func);

// Runtime-pattern overload for lua scripts
inline void Add(const IPattern& pattern, const PatternFunc& func)
{
m_Patterns.push_back(std::make_pair(&pattern, func));
}

bool Scan();

private:
Expand Down
199 changes: 199 additions & 0 deletions src/core/scripting/LuaCommands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
#include "LuaCommands.hpp"
#include "LuaScript.hpp"
#include "LuaUserInterface.hpp"
#include "core/commands/Commands.hpp"

namespace YimMenu
{
static void UnrefIfValid(LuaUserInterface* iface, int ref)
{
if (ref == LUA_NOREF || !iface)
return;
auto* script = iface->GetScript();
if (!script)
return;
auto* state = script->GetState();
if (!state)
return;
luaL_unref(state, LUA_REGISTRYINDEX, ref);
}

LuaCommand::LuaCommand(LuaUserInterface* iface, std::string name, std::string label, std::string description, int on_call) :
Command(std::move(name), std::move(label), std::move(description)),
m_Interface(iface),
m_OnCall(on_call)
{
}

LuaCommand::~LuaCommand()
{
Commands::RemoveCommand(this);
UnrefIfValid(m_Interface, m_OnCall);
}

void LuaCommand::OnCall()
{
if (m_Interface && m_OnCall != LUA_NOREF)
m_Interface->QueueCoroutine(m_OnCall, false);
}

LuaBoolCommand::LuaBoolCommand(LuaUserInterface* iface, std::string name, std::string label, std::string description, bool default_value, int on_enable, int on_disable) :
BoolCommand(std::move(name), std::move(label), std::move(description), default_value),
m_Interface(iface),
m_OnEnable(on_enable),
m_OnDisable(on_disable)
{
}

LuaBoolCommand::~LuaBoolCommand()
{
Commands::RemoveCommand(this);
UnrefIfValid(m_Interface, m_OnEnable);
UnrefIfValid(m_Interface, m_OnDisable);
}

void LuaBoolCommand::OnEnable()
{
if (m_Interface && m_OnEnable != LUA_NOREF)
m_Interface->QueueCoroutine(m_OnEnable, true);
}

void LuaBoolCommand::OnDisable()
{
if (m_Interface && m_OnDisable != LUA_NOREF)
m_Interface->QueueCoroutine(m_OnDisable, true);
}

LuaLoopedCommand::LuaLoopedCommand(LuaUserInterface* iface, std::string name, std::string label, std::string description, int on_tick, int on_enable, int on_disable) :
LoopedCommand(std::move(name), std::move(label), std::move(description)),
m_Interface(iface),
m_OnTick(on_tick),
m_OnEnable(on_enable),
m_OnDisable(on_disable)
{
}

LuaLoopedCommand::~LuaLoopedCommand()
{
if (m_TickRegistered && m_Interface && m_OnTick != LUA_NOREF)
m_Interface->RemoveTickFunction(m_OnTick);

Commands::RemoveCommand(this);

UnrefIfValid(m_Interface, m_OnTick);
UnrefIfValid(m_Interface, m_OnEnable);
UnrefIfValid(m_Interface, m_OnDisable);
}

void LuaLoopedCommand::OnEnable()
{
if (!m_Interface)
return;

if (m_OnTick != LUA_NOREF && !m_TickRegistered)
{
m_Interface->AddTickFunction(m_OnTick);
m_TickRegistered = true;
}
if (m_OnEnable != LUA_NOREF)
m_Interface->QueueCoroutine(m_OnEnable, true);
}

void LuaLoopedCommand::OnDisable()
{
if (!m_Interface)
return;

if (m_TickRegistered)
{
m_Interface->RemoveTickFunction(m_OnTick);
m_TickRegistered = false;
}
if (m_OnDisable != LUA_NOREF)
m_Interface->QueueCoroutine(m_OnDisable, true);
}

LuaIntCommand::LuaIntCommand(LuaUserInterface* iface, std::string name, std::string label, std::string description, std::optional<int> min, std::optional<int> max, int default_value, int on_change) :
IntCommand(std::move(name), std::move(label), std::move(description), min, max, default_value),
m_Interface(iface),
m_OnChange(on_change)
{
}

LuaIntCommand::~LuaIntCommand()
{
Commands::RemoveCommand(this);
UnrefIfValid(m_Interface, m_OnChange);
}

void LuaIntCommand::OnChange()
{
if (m_Interface && m_OnChange != LUA_NOREF)
{
CallbackArg arg;
arg.kind = CallbackArg::Kind::Int;
arg.i = GetState();
m_Interface->QueueCoroutine(m_OnChange, true, arg);
}
}

LuaFloatCommand::LuaFloatCommand(LuaUserInterface* iface, std::string name, std::string label, std::string description, std::optional<float> min, std::optional<float> max, float default_value, int on_change) :
FloatCommand(std::move(name), std::move(label), std::move(description), min, max, default_value),
m_Interface(iface),
m_OnChange(on_change)
{
}

LuaFloatCommand::~LuaFloatCommand()
{
Commands::RemoveCommand(this);
UnrefIfValid(m_Interface, m_OnChange);
}

void LuaFloatCommand::OnChange()
{
if (m_Interface && m_OnChange != LUA_NOREF)
{
CallbackArg arg;
arg.kind = CallbackArg::Kind::Number;
arg.n = GetState();
m_Interface->QueueCoroutine(m_OnChange, true, arg);
}
}

LuaListCommand::LuaListCommand(LuaUserInterface* iface, std::string name, std::string label, std::string description, std::vector<std::pair<int, std::string>> entries, int default_value, int on_change) :
ListCommand(std::move(name), std::move(label), std::move(description), {}, default_value),
m_Interface(iface),
m_OnChange(on_change)
{
m_LabelStorage.reserve(entries.size());

std::vector<std::pair<int, const char*>> list;
list.reserve(entries.size());

for (auto& [k, v] : entries)
{
m_LabelStorage.push_back(std::move(v));
list.emplace_back(k, m_LabelStorage.back().c_str());
}

SetList(std::move(list));
}

LuaListCommand::~LuaListCommand()
{
Commands::RemoveCommand(this);
UnrefIfValid(m_Interface, m_OnChange);
}

void LuaListCommand::OnChange()
{
if (m_Interface && m_OnChange != LUA_NOREF)
{
CallbackArg arg;
arg.kind = CallbackArg::Kind::Int;
arg.i = GetState();
m_Interface->QueueCoroutine(m_OnChange, true, arg);
}
}
}
Loading
Loading