Merge branch 'pr/208'

This commit is contained in:
Callow 2022-07-13 11:48:41 +03:00
commit 7f93c90255

View File

@ -18,79 +18,110 @@ bool OpenGenshinProcess(HANDLE* phProcess, HANDLE* phThread);
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
Logger::SetLevel(Logger::Level::Debug, Logger::LoggerType::ConsoleLogger); Logger::SetLevel(Logger::Level::Debug, Logger::LoggerType::ConsoleLogger);
auto path = std::filesystem::path(argv[0]).parent_path(); auto path = std::filesystem::path(argv[0]).parent_path();
current_path(path); current_path(path);
WaitForCloseProcess(GlobalGenshinProcName); WaitForCloseProcess(GlobalGenshinProcName);
WaitForCloseProcess(ChinaGenshinProcName); WaitForCloseProcess(ChinaGenshinProcName);
Sleep(1000); // Wait for unloading all dlls. Sleep(1000); // Wait for unloading all dlls.
ini.SetUnicode(); ini.SetUnicode();
ini.LoadFile("cfg.ini"); ini.LoadFile("cfg.ini");
HANDLE hProcess, hThread; HANDLE hProcess, hThread;
if (!OpenGenshinProcess(&hProcess, &hThread)) if (!OpenGenshinProcess(&hProcess, &hThread))
{ {
std::cout << "Failed to open GenshinImpact process." << std::endl; std::cout << "Failed to open GenshinImpact process." << std::endl;
return 1; system("pause");
} return 1;
}
current_path(path); current_path(path);
ini.SaveFile("cfg.ini"); ini.SaveFile("cfg.ini");
std::string filename = (argc == 2 ? argv[1] : "CLibrary.dll"); std::string filename = (argc == 2 ? argv[1] : "CLibrary.dll");
std::filesystem::path currentDllPath = std::filesystem::current_path() / filename; std::filesystem::path currentDllPath = std::filesystem::current_path() / filename;
#ifdef _DEBUG #ifdef _DEBUG
std::filesystem::path tempDllPath = std::filesystem::temp_directory_path() / filename; std::filesystem::path tempDllPath = std::filesystem::temp_directory_path() / filename;
std::error_code ec; std::error_code ec;
std::filesystem::copy_file(currentDllPath, tempDllPath, std::filesystem::copy_options::update_existing, ec); std::filesystem::copy_file(currentDllPath, tempDllPath, std::filesystem::copy_options::update_existing, ec);
if (ec) if (ec)
{ {
LOG_ERROR("Copy dll failed: %s", ec.message().c_str()); LOG_ERROR("Copy dll failed: %s", ec.message().c_str());
std::system("pause"); std::system("pause");
} }
InjectDLL(hProcess, tempDllPath.string()); InjectDLL(hProcess, tempDllPath.string());
#else #else
InjectDLL(hProcess, currentDllPath.string()); InjectDLL(hProcess, currentDllPath.string());
#endif #endif
Sleep(2000); Sleep(2000);
ResumeThread(hThread); ResumeThread(hThread);
CloseHandle(hProcess); CloseHandle(hProcess);
} }
bool OpenGenshinProcess(HANDLE *phProcess, HANDLE* phThread) bool OpenGenshinProcess(HANDLE* phProcess, HANDLE* phThread)
{ {
STARTUPINFOA startInfo{};
PROCESS_INFORMATION processInformation{};
auto filePath = util::GetOrSelectPath(ini, "Inject", "GenshinPath", "genshin path", "Executable\0GenshinImpact.exe;YuanShen.exe\0"); HANDLE hToken;
auto commandline = ini.GetValue("Inject", "GenshinCommandLine"); BOOL TokenRet = OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken);
if (!TokenRet) {
LPSTR lpstr = commandline == nullptr ? nullptr : const_cast<LPSTR>(commandline); LOG_LAST_ERROR("Privilege escalation failed!");
return false;
if (!filePath) }
return false; bool OpenRet = false;
auto filePath = util::GetOrSelectPath(ini, "Inject", "GenshinPath", "genshin path", "Executable\0GenshinImpact.exe;YuanShen.exe\0");
BOOL result = CreateProcessA(filePath->c_str(), auto commandline = ini.GetValue("Inject", "GenshinCommandLine");
lpstr, 0, 0, FALSE, CREATE_SUSPENDED, nullptr, nullptr, &startInfo, &processInformation); LPSTR lpstr = commandline == nullptr ? nullptr : const_cast<LPSTR>(commandline);
if (result == FALSE) if (!filePath)
{ return false;
LOG_LAST_ERROR("Failed to create game process."); DWORD pid = FindProcessId("explorer.exe");
LOG_ERROR("If you have problem with GenshinImpact.exe path. You can change it manually in cfg.ini."); if (pid == 0)
return false; {
} LOG_ERROR("Can't find 'explorer' pid!");
return false;
ini.SaveFile("cfg.ini"); }
std::string CurrentDirectory = filePath.value();
*phThread = processInformation.hThread; int pos = CurrentDirectory.rfind("\\", CurrentDirectory.length());
*phProcess = processInformation.hProcess; CurrentDirectory = CurrentDirectory.substr(0, pos);
return true; LOG_INFO("%s", CurrentDirectory.data());
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
STARTUPINFOEXA si;
ZeroMemory(&si, sizeof(si));
si.StartupInfo.cb = sizeof(si);
SIZE_T lpsize = 0;
InitializeProcThreadAttributeList(NULL, 1, 0, &lpsize);
char* temp = new char[lpsize];
LPPROC_THREAD_ATTRIBUTE_LIST AttributeList = (LPPROC_THREAD_ATTRIBUTE_LIST)temp;
InitializeProcThreadAttributeList(AttributeList, 1, 0, &lpsize);
if (!UpdateProcThreadAttribute(AttributeList, 0, PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, &handle, sizeof(HANDLE), NULL, NULL))
{
LOG_WARNING("UpdateProcThreadAttribute failed ! (%d).\n", GetLastError());
}
si.lpAttributeList = AttributeList;
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
BOOL result = CreateProcessAsUserA(hToken, const_cast<LPSTR>(filePath->data()), lpstr,
0, 0, 0, EXTENDED_STARTUPINFO_PRESENT | CREATE_SUSPENDED, 0, (LPSTR)CurrentDirectory.data(), (LPSTARTUPINFOA)&si, &pi);
if (result == FALSE)
{
LOG_LAST_ERROR("Failed to create game process.");
LOG_ERROR("If you have problem with GenshinImpact.exe path. You can change it manually in cfg.ini.");
goto End;
}
ini.SaveFile("cfg.ini");
*phThread = pi.hThread;
*phProcess = pi.hProcess;
OpenRet = true;
End:
DeleteProcThreadAttributeList(AttributeList);
delete temp;
return OpenRet;
} }