LunaHook-mirror/LunaHook/enginecontrol.cpp

158 lines
4.1 KiB
C++
Raw Normal View History

2024-02-07 20:59:24 +08:00
2024-07-21 21:04:12 +08:00
WCHAR *processName, // cached
processPath[MAX_PATH]; // cached
2024-02-07 20:59:24 +08:00
WCHAR processName_lower[MAX_PATH];
uintptr_t processStartAddress, processStopAddress;
2024-07-21 21:04:12 +08:00
std::vector<ENGINE *> check_engines();
std::vector<ENGINE *> ignore_engines();
std::vector<ENGINE *> unsafe_check_atlast();
2024-02-07 20:59:24 +08:00
2024-07-21 21:04:12 +08:00
bool ENGINE::check_function()
{
2024-02-07 20:59:24 +08:00
switch (check_by)
{
2024-07-21 21:04:12 +08:00
case CHECK_BY::ALL_TRUE:
{
is_engine_certain = false;
return true;
}
case CHECK_BY::FILE:
{
return (Util::CheckFile(std::get<check_by_single>(check_by_target)));
}
case CHECK_BY::FILE_ALL:
{
auto _list = std::get<check_by_list>(check_by_target);
return std::all_of(_list.begin(), _list.end(), Util::CheckFile);
}
case CHECK_BY::FILE_ANY:
{
auto _list = std::get<check_by_list>(check_by_target);
return std::any_of(_list.begin(), _list.end(), Util::CheckFile);
}
case CHECK_BY::RESOURCE_STR:
{
return (Util::SearchResourceString(std::get<check_by_single>(check_by_target)));
}
case CHECK_BY::CUSTOM:
{
return std::get<check_by_custom_function>(check_by_target)();
}
default:
return false;
2024-02-07 20:59:24 +08:00
}
}
2024-07-21 21:04:12 +08:00
bool safematch(ENGINE *m)
{
bool matched = false;
__try
{
matched = m->check_function();
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
ConsoleOutput(Match_Error, m->getenginename());
// ConsoleOutput("match ERROR");
2024-02-07 20:59:24 +08:00
}
return matched;
}
2024-07-21 21:04:12 +08:00
bool safeattach(ENGINE *m)
{
bool attached = false;
__try
{
attached = m->attach_function();
2024-02-07 20:59:24 +08:00
}
2024-07-21 21:04:12 +08:00
__except (EXCEPTION_EXECUTE_HANDLER)
{
ConsoleOutput(Attach_Error, m->getenginename());
// ConsoleOutput("attach ERROR");
2024-02-07 20:59:24 +08:00
}
return attached;
}
2024-07-21 21:04:12 +08:00
bool checkengine()
{
2024-02-07 20:59:24 +08:00
2024-07-21 21:04:12 +08:00
auto engines = check_engines();
auto engineatlast = unsafe_check_atlast();
auto engineignore = ignore_engines();
std::vector<const char *> infomations = {
2024-02-07 20:59:24 +08:00
"match failed",
"attach failed",
2024-07-21 21:04:12 +08:00
"attach success"};
auto allengines = {engines, engineignore, engineatlast};
int total = [allengines]()
{int _=0;for(auto eng:allengines)_+=eng.size();return _; }();
int current = 0;
for (auto eng : allengines)
{
for (auto m : eng)
{
current += 1;
bool matched = safematch(m);
bool attached = matched && safeattach(m);
2024-02-07 20:59:24 +08:00
2024-07-21 21:04:12 +08:00
// ConsoleOutput("Progress %d/%d, checked engine %s, %s",current,total,m->getenginename(),infomations[matched+attached]);
// ConsoleOutput("Progress %d/%d, %s",current,total,infomations[matched+attached]);
if (matched == false)
continue;
ConsoleOutput(MatchedEngine, m->getenginename());
if (m->dontstop)
{
2024-02-07 20:59:24 +08:00
continue;
}
2024-07-21 21:04:12 +08:00
if (m->is_engine_certain)
{
ConsoleOutput(ConfirmStop, m->getenginename());
2024-02-07 20:59:24 +08:00
return attached;
}
2024-07-21 21:04:12 +08:00
if (attached)
{
ConsoleOutput(Attach_Stop, m->getenginename());
return true;
2024-02-07 20:59:24 +08:00
}
}
}
2024-07-21 21:04:12 +08:00
2024-02-07 20:59:24 +08:00
return false;
}
2024-07-21 21:04:12 +08:00
void HIJACK()
{
2024-02-07 20:59:24 +08:00
static bool once = false;
2024-07-21 21:04:12 +08:00
if (once)
return;
once = true;
2024-02-07 20:59:24 +08:00
GetModuleFileNameW(nullptr, processPath, MAX_PATH);
processName = wcsrchr(processPath, L'\\') + 1;
2024-07-21 21:04:12 +08:00
2024-02-07 20:59:24 +08:00
wcscpy_s(processName_lower, processName);
_wcslwr_s(processName_lower); // lower case
2024-07-21 21:04:12 +08:00
std::tie(processStartAddress, processStopAddress) = Util::QueryModuleLimits(GetModuleHandleW(nullptr), 0, 1 + PAGE_NOACCESS);
2024-02-07 20:59:24 +08:00
spDefault.minAddress = processStartAddress;
spDefault.maxAddress = processStopAddress;
ConsoleOutput(ProcessRange, processStartAddress, processStopAddress);
2024-07-21 21:04:12 +08:00
if (processStartAddress + 0x40000 > processStopAddress)
ConsoleOutput(WarningDummy);
bool result = false;
__try
{
result = checkengine();
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
ConsoleOutput(HIJACK_ERROR);
2024-02-07 20:59:24 +08:00
}
2024-07-21 21:04:12 +08:00
if (result == false)
{
2024-02-07 20:59:24 +08:00
PcHooks::hookOtherPcFunctions();
}
2024-07-21 21:04:12 +08:00
}