-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor hooks to use a dedicated handler in client
This will also enable calling a hook without available configuration, closing #39
- Loading branch information
1 parent
0817a3c
commit 6fefda5
Showing
13 changed files
with
112 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "Handler.hpp" | ||
|
||
namespace Litr::Hook { | ||
|
||
Handler::Handler(const Ref<CLI::Instruction>& instruction) : m_Instruction(instruction) {} | ||
|
||
void Handler::Add(Code code, const std::vector<Value>& values, | ||
const Handler::HookCallback& callback) { | ||
LITR_PROFILE_FUNCTION(); | ||
|
||
for (auto&& value : values) { | ||
m_Hooks.emplace_back(code, value, callback); | ||
} | ||
} | ||
|
||
bool Handler::Execute() const { | ||
LITR_PROFILE_FUNCTION(); | ||
|
||
size_t offset{0}; | ||
|
||
while (offset < m_Instruction->Count()) { | ||
const auto code{static_cast<Code>(m_Instruction->Read(offset++))}; | ||
|
||
for (auto&& hook : m_Hooks) { | ||
if (code != hook.Code) continue; | ||
auto value{m_Instruction->ReadConstant(m_Instruction->Read(offset))}; | ||
if (value == hook.Value) { | ||
hook.Callback(m_Instruction); | ||
return true; | ||
} | ||
} | ||
|
||
if (code != Code::CLEAR) { | ||
offset++; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} // namespace Litr::Hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
|
||
#include "Core.hpp" | ||
|
||
namespace Litr::Hook { | ||
|
||
class Handler { | ||
using HookCallback = std::function<void(const Ref<CLI::Instruction>& instruction)>; | ||
using Code = CLI::Instruction::Code; | ||
using Value = CLI::Instruction::Value; | ||
|
||
struct Hook { | ||
CLI::Instruction::Code Code; | ||
CLI::Instruction::Value Value; | ||
HookCallback Callback; | ||
|
||
Hook(CLI::Instruction::Code code, CLI::Instruction::Value value, HookCallback callback) | ||
: Code(code), Value(std::move(value)), Callback(std::move(callback)) {} | ||
}; | ||
|
||
public: | ||
explicit Handler(const Ref<CLI::Instruction>& instruction); | ||
void Add(Code code, const std::vector<Value>& values, const HookCallback& callback); | ||
[[nodiscard]] bool Execute() const; | ||
|
||
private: | ||
const Ref<CLI::Instruction>& m_Instruction; | ||
std::vector<Hook> m_Hooks{}; | ||
}; | ||
|
||
} // namespace Litr::Hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters