mirror of
https://github.com/momo5502/emulator.git
synced 2026-01-18 19:23:56 +00:00
added more callbacks (#141)
This commit is contained in:
@@ -21,25 +21,21 @@ namespace utils
|
||||
{
|
||||
}
|
||||
|
||||
template <typename F, typename = std::enable_if_t<std::is_invocable_r_v<Ret, F, Args...>>>
|
||||
template <typename F>
|
||||
requires(std::is_invocable_r_v<Ret, F, Args...>)
|
||||
optional_function(F&& f)
|
||||
: func(std::forward<F>(f))
|
||||
{
|
||||
}
|
||||
|
||||
optional_function& operator=(std::function<Ret(Args...)> f)
|
||||
template <typename F>
|
||||
requires(std::is_invocable_r_v<Ret, F, Args...>)
|
||||
optional_function& operator=(F&& f)
|
||||
{
|
||||
func = std::move(f);
|
||||
func = std::forward<F>(f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires(!std::is_same_v<std::remove_cvref_t<T>, std::function<Ret(Args...)>>)
|
||||
optional_function& operator=(T&& t)
|
||||
{
|
||||
return this->operator=(std::function<Ret(Args...)>(std::forward<T>(t)));
|
||||
}
|
||||
|
||||
Ret operator()(Args... args) const
|
||||
{
|
||||
if (func)
|
||||
|
||||
@@ -95,6 +95,7 @@ mapped_module* module_manager::map_local_module(const std::filesystem::path& fil
|
||||
|
||||
const auto image_base = mod.image_base;
|
||||
const auto entry = this->modules_.try_emplace(image_base, std::move(mod));
|
||||
this->on_module_load(entry.first->second);
|
||||
return &entry.first->second;
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
@@ -146,6 +147,7 @@ bool module_manager::unmap(const uint64_t address, const logger& logger)
|
||||
|
||||
logger.log("Unmapping %s (0x%" PRIx64 ")\n", mod->second.path.generic_string().c_str(), mod->second.image_base);
|
||||
|
||||
this->on_module_unload(mod->second);
|
||||
unmap_module(*this->memory_, mod->second);
|
||||
this->modules_.erase(mod);
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "mapped_module.hpp"
|
||||
#include "../file_system.hpp"
|
||||
#include <utils/function.hpp>
|
||||
|
||||
class logger;
|
||||
|
||||
@@ -10,6 +11,9 @@ class module_manager
|
||||
{
|
||||
public:
|
||||
using module_map = std::map<uint64_t, mapped_module>;
|
||||
utils::optional_function<void(mapped_module& mod)> on_module_load{};
|
||||
utils::optional_function<void(mapped_module& mod)> on_module_unload{};
|
||||
|
||||
module_manager(memory_manager& memory, file_system& file_sys);
|
||||
|
||||
void map_main_modules(const windows_path& executable_path, const windows_path& ntdll_path,
|
||||
|
||||
@@ -201,5 +201,7 @@ handle process_context::create_thread(memory_manager& memory, const uint64_t sta
|
||||
const uint64_t stack_size)
|
||||
{
|
||||
emulator_thread t{memory, *this, start_address, argument, stack_size, ++this->spawned_thread_count};
|
||||
return this->threads.store(std::move(t));
|
||||
auto h = this->threads.store(std::move(t));
|
||||
on_create_thread(h, *this->threads.get(h));
|
||||
return h;
|
||||
}
|
||||
|
||||
@@ -44,6 +44,8 @@ struct process_context
|
||||
const emulator_settings& emu_settings, const mapped_module& executable, const mapped_module& ntdll,
|
||||
const apiset::container& apiset_container);
|
||||
|
||||
utils::optional_function<void(handle h, emulator_thread& thr)> on_create_thread{};
|
||||
|
||||
handle create_thread(memory_manager& memory, const uint64_t start_address, const uint64_t argument,
|
||||
const uint64_t stack_size);
|
||||
|
||||
|
||||
@@ -3613,6 +3613,7 @@ namespace
|
||||
}
|
||||
|
||||
thread->exit_status = exit_status;
|
||||
c.win_emu.callbacks.thread_terminated(thread_handle, *thread);
|
||||
if (thread == c.proc.active_thread)
|
||||
{
|
||||
c.win_emu.yield_thread();
|
||||
|
||||
@@ -220,6 +220,10 @@ void windows_emulator::setup_process(const application_settings& app_settings, c
|
||||
const auto& emu = this->emu();
|
||||
auto& context = this->process;
|
||||
|
||||
mod_manager.on_module_load = std::move(callbacks.module_loaded);
|
||||
mod_manager.on_module_unload = std::move(callbacks.module_unloaded);
|
||||
context.on_create_thread = std::move(callbacks.thread_created);
|
||||
|
||||
this->mod_manager.map_main_modules(app_settings.application, R"(C:\Windows\System32\ntdll.dll)",
|
||||
R"(C:\Windows\System32\win32u.dll)", this->log);
|
||||
|
||||
|
||||
@@ -23,6 +23,10 @@ struct emulator_callbacks
|
||||
std::string_view syscall_name, x64_emulator::pointer_type prev_address,
|
||||
std::string_view prev_mod_name)>
|
||||
outofline_syscall{};
|
||||
utils::optional_function<void(mapped_module& mod)> module_loaded{};
|
||||
utils::optional_function<void(mapped_module& mod)> module_unloaded{};
|
||||
utils::optional_function<void(handle h, emulator_thread& thr)> thread_created{};
|
||||
utils::optional_function<void(handle h, emulator_thread& thr)> thread_terminated{};
|
||||
};
|
||||
|
||||
struct application_settings
|
||||
@@ -53,7 +57,6 @@ class windows_emulator
|
||||
|
||||
public:
|
||||
std::filesystem::path emulation_root{};
|
||||
emulator_callbacks callbacks{};
|
||||
logger log{};
|
||||
file_system file_sys;
|
||||
memory_manager memory;
|
||||
@@ -61,6 +64,7 @@ class windows_emulator
|
||||
module_manager mod_manager;
|
||||
process_context process;
|
||||
syscall_dispatcher dispatcher;
|
||||
emulator_callbacks callbacks{};
|
||||
|
||||
windows_emulator(const emulator_settings& settings = {},
|
||||
std::unique_ptr<x64_emulator> emu = create_default_x64_emulator());
|
||||
|
||||
Reference in New Issue
Block a user