Textractor_test/gui/ProfileManager.cpp

167 lines
4.2 KiB
C++
Raw Normal View History

2015-04-02 22:27:58 +08:00
#include "ProfileManager.h"
#include "profile/Profile.h"
#include "host/host.h"
#include "host/hookman.h"
#include "vnrhook/include/types.h"
#include "vnrhook/include/const.h"
#include "utility.h"
#include "profile/misc.h"
2015-04-02 22:27:58 +08:00
extern HookManager* man; // main.cpp
ProfileManager* pfman;
2018-07-13 23:47:16 +08:00
ProfileManager::ProfileManager()
2015-04-02 22:27:58 +08:00
{
LoadProfiles();
2015-04-02 22:27:58 +08:00
}
2015-04-02 22:27:58 +08:00
ProfileManager::~ProfileManager()
{
SaveProfiles();
2015-04-02 22:27:58 +08:00
}
Profile* ProfileManager::GetProfile(DWORD pid)
{
std::wstring path = GetProcessPath(pid);
if (!path.empty())
{
auto node = profile_tree.find(path);
if (node != profile_tree.end())
return node->second.get();
}
return NULL;
}
bool ProfileManager::CreateProfile(pugi::xml_node game)
2015-04-02 22:27:58 +08:00
{
auto file = game.child(L"File");
auto profile = game.child(L"Profile");
if (!file || !profile)
return false;
auto path = file.attribute(L"Path");
if (!path)
return false;
auto profile_title = game.attribute(L"Title");
auto title = profile_title ? profile_title.value() : L"";
auto pf = new Profile(title);
if (!pf->XmlReadProfile(profile))
return false;
CSLock lock(cs);
auto& oldProfile = profile_tree[path.value()];
if (!oldProfile)
oldProfile.swap(profile_ptr(pf));
return true;
2015-04-02 22:27:58 +08:00
}
Profile* ProfileManager::CreateProfile(DWORD pid)
2015-04-02 22:27:58 +08:00
{
CSLock lock(cs);
auto path = GetProcessPath(pid);
2015-04-02 22:27:58 +08:00
auto& pf = profile_tree[path];
if (!pf)
{
std::wstring title = GetProcessTitle(pid);
pf.reset(new Profile(title));
}
return pf.get();
}
void ProfileManager::WriteProfileXml(const std::wstring& path, Profile& pf, pugi::xml_node root)
{
auto game = root.append_child(L"Game");
auto file_node = game.append_child(L"File");
file_node.append_attribute(L"Path") = path.c_str();
auto profile_node = game.append_child(L"Profile");
pf.XmlWriteProfile(profile_node);
if (!pf.Title().empty())
{
if (!game.attribute(L"Title"))
game.append_attribute(L"Title");
game.attribute(L"Title") = pf.Title().c_str();
}
}
void ProfileManager::LoadProfiles()
2015-04-02 22:27:58 +08:00
{
pugi::xml_document doc;
2018-07-13 23:47:16 +08:00
UniqueHandle hFile(IthCreateFile(L"NextHooker_Profile.xml", GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING));
2015-04-02 22:27:58 +08:00
if (hFile.get() == INVALID_HANDLE_VALUE)
return;
DWORD size = GetFileSize(hFile.get(), NULL);
std::unique_ptr<char[]> buffer(new char[size]);
ReadFile(hFile.get(), buffer.get(), size, &size, NULL);
auto result = doc.load_buffer(buffer.get(), size);
if (!result)
return;
2018-07-13 23:47:16 +08:00
auto root = doc.root().child(L"NextHookerProfile");
2015-04-02 22:27:58 +08:00
if (!root)
return;
for (auto game = root.begin(); game != root.end(); ++game)
CreateProfile(*game);
2015-04-02 22:27:58 +08:00
}
void ProfileManager::SaveProfiles()
2015-04-02 22:27:58 +08:00
{
pugi::xml_document doc;
2018-07-13 23:47:16 +08:00
auto root = doc.append_child(L"NextHookerProfile");
2015-04-02 22:27:58 +08:00
for (auto it = profile_tree.begin(); it != profile_tree.end(); ++it) {
auto& path = it->first;
auto& profile = it->second;
WriteProfileXml(path, *profile, root);
}
2018-07-13 23:47:16 +08:00
UniqueHandle hFile(IthCreateFile(L"NextHooker_Profile.xml", GENERIC_WRITE, 0, CREATE_ALWAYS));
2015-04-02 22:27:58 +08:00
if (hFile.get() != INVALID_HANDLE_VALUE)
{
FileWriter fw(hFile.get());
doc.save(fw);
}
}
void ProfileManager::DeleteProfile(const std::wstring& path)
{
CSLock lock(cs);
profile_tree.erase(profile_tree.find(path));
}
Profile* ProfileManager::GetProfile(const std::wstring& path)
2015-04-02 22:27:58 +08:00
{
if (path.empty())
return nullptr;
2015-04-02 22:27:58 +08:00
auto it = profile_tree.find(path);
if (it == profile_tree.end())
return nullptr;
return it->second.get();
2015-04-02 22:27:58 +08:00
}
bool ProfileManager::HasProfile(const std::wstring& path)
{
return profile_tree.find(path) != profile_tree.end();
}
DWORD ProfileManager::CountProfiles()
2015-04-02 22:27:58 +08:00
{
return profile_tree.size();
}
2018-07-19 11:40:44 +08:00
DWORD SaveProcessProfile(TextThread* thread)
2015-04-02 22:27:58 +08:00
{
2018-07-19 11:40:44 +08:00
ThreadParameter tp = thread->GetThreadParameter();
std::wstring path = GetProcessPath(tp.pid);
2015-04-02 22:27:58 +08:00
if (path.empty())
return 0;
pugi::xml_document doc;
pugi::xml_node profile_node = doc.append_child(L"Profile");
2018-07-19 11:40:44 +08:00
Profile* pf = pfman->GetProfile(tp.pid);
2015-04-02 22:27:58 +08:00
if (pf != NULL)
pf->Clear();
else
2018-07-19 11:40:44 +08:00
pf = pfman->CreateProfile(tp.pid);
pf->AddHook(hook_ptr(new HookProfile(man->GetHookParam(tp.pid, tp.hook), man->GetHookName(tp.pid, tp.hook))));
pf->AddThread(thread_ptr(new ThreadProfile(man->GetHookName(tp.pid, tp.hook), tp.retn, tp.spl, tp.hook, 0, THREAD_MASK_RETN | THREAD_MASK_SPLIT, L"")));
pf->XmlReadProfile(profile_node);
2018-07-19 11:40:44 +08:00
2015-04-02 22:27:58 +08:00
return 0;
}