|
|
#include "XPluginManager.h" |
|
|
#include <stdexcept> |
|
|
#include <iostream> |
|
|
|
|
|
XPluginManager::XPluginManager() : isMmmojoEnvInited(false), intPtrMmmojoEnv(nullptr), intPtrUserData(nullptr), intPtrOcrExePath(nullptr) { |
|
|
} |
|
|
|
|
|
XPluginManager::~XPluginManager() { |
|
|
if (isMmmojoEnvInited) { |
|
|
StopMmMojoEnv(); |
|
|
} |
|
|
|
|
|
|
|
|
if (intPtrOcrExePath != nullptr) { |
|
|
free(intPtrOcrExePath); |
|
|
intPtrOcrExePath = nullptr; |
|
|
} |
|
|
} |
|
|
|
|
|
void XPluginManager::SetExePath(const std::string& exePath) { |
|
|
const std::string ocrExeName = "WeChatOCR.exe"; |
|
|
std::string fullPath = exePath; |
|
|
|
|
|
|
|
|
if (fullPath.find(ocrExeName) == std::string::npos) { |
|
|
if (fullPath.back() != '\\') { |
|
|
fullPath += "\\"; |
|
|
} |
|
|
fullPath += ocrExeName; |
|
|
} |
|
|
|
|
|
|
|
|
FILE* file = fopen(fullPath.c_str(), "r"); |
|
|
if (!file) { |
|
|
throw std::runtime_error("指定的 " + ocrExeName + " 路径不存在!"); |
|
|
} |
|
|
fclose(file); |
|
|
|
|
|
|
|
|
if (intPtrOcrExePath != nullptr) { |
|
|
free(intPtrOcrExePath); |
|
|
} |
|
|
|
|
|
|
|
|
size_t len = fullPath.length() + 1; |
|
|
intPtrOcrExePath = malloc(len * sizeof(wchar_t)); |
|
|
if (intPtrOcrExePath == nullptr) { |
|
|
throw std::runtime_error("内存分配失败"); |
|
|
} |
|
|
|
|
|
|
|
|
mbstowcs((wchar_t*)intPtrOcrExePath, fullPath.c_str(), len); |
|
|
} |
|
|
|
|
|
void XPluginManager::AppendSwitchNativeCmdLine(const std::string& arg, const std::string& value) { |
|
|
m_switch_native[arg] = value; |
|
|
} |
|
|
|
|
|
void XPluginManager::SetCommandLine(const std::vector<std::string>& cmdline) { |
|
|
cmdLines = cmdline; |
|
|
} |
|
|
|
|
|
void XPluginManager::SetOneCallback(const std::string& name, void* func) { |
|
|
callbacks[name] = func; |
|
|
} |
|
|
|
|
|
void XPluginManager::SetCallbacks(const std::map<std::string, void*>& callBacks) { |
|
|
for (const auto& callback : callBacks) { |
|
|
callbacks[callback.first] = callback.second; |
|
|
} |
|
|
} |
|
|
|
|
|
void XPluginManager::SetCallbackUsrData(void* cbUsrData) { |
|
|
intPtrUserData = cbUsrData; |
|
|
} |
|
|
|
|
|
void XPluginManager::InitMmMojoEnv() { |
|
|
|
|
|
if (intPtrOcrExePath == nullptr) { |
|
|
throw std::runtime_error("OCR可执行文件路径未设置"); |
|
|
} |
|
|
|
|
|
|
|
|
wchar_t* exePath = (wchar_t*)intPtrOcrExePath; |
|
|
FILE* file = _wfopen(exePath, L"r"); |
|
|
if (!file) { |
|
|
throw std::runtime_error("给定的 WeChatOcr.exe 路径错误"); |
|
|
} |
|
|
fclose(file); |
|
|
|
|
|
|
|
|
if (isMmmojoEnvInited && intPtrMmmojoEnv != nullptr) { |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
MmmojoDll::InitializeMMMojo(0, nullptr); |
|
|
intPtrMmmojoEnv = MmmojoDll::CreateMMMojoEnvironment(); |
|
|
if (intPtrMmmojoEnv == nullptr) { |
|
|
throw std::runtime_error("CreateMMMojoEnvironment 失败!"); |
|
|
} |
|
|
|
|
|
|
|
|
MmmojoDll::SetMMMojoEnvironmentCallbacks(intPtrMmmojoEnv, (int)MMMojoCallbackType::kMMUserData, intPtrUserData); |
|
|
|
|
|
|
|
|
SetDefaultCallbacks(); |
|
|
|
|
|
|
|
|
MmmojoDll::SetMMMojoEnvironmentInitParams(intPtrMmmojoEnv, (int)MMMojoEnvironmentInitParamType::kMMHostProcess, (void*)1); |
|
|
MmmojoDll::SetMMMojoEnvironmentInitParams(intPtrMmmojoEnv, (int)MMMojoEnvironmentInitParamType::kMMExePath, intPtrOcrExePath); |
|
|
|
|
|
|
|
|
for (const auto& item : m_switch_native) { |
|
|
|
|
|
char* keyPtr = _strdup(item.first.c_str()); |
|
|
wchar_t* valuePtr = (wchar_t*)malloc((item.second.length() + 1) * sizeof(wchar_t)); |
|
|
mbstowcs(valuePtr, item.second.c_str(), item.second.length() + 1); |
|
|
|
|
|
|
|
|
MmmojoDll::AppendMMSubProcessSwitchNative(intPtrMmmojoEnv, keyPtr, valuePtr); |
|
|
|
|
|
|
|
|
free(keyPtr); |
|
|
free(valuePtr); |
|
|
} |
|
|
|
|
|
|
|
|
MmmojoDll::StartMMMojoEnvironment(intPtrMmmojoEnv); |
|
|
isMmmojoEnvInited = true; |
|
|
} |
|
|
|
|
|
void XPluginManager::SetDefaultCallbacks() { |
|
|
|
|
|
for (int type = 1; type <= (int)MMMojoCallbackType::kMMRemoteMojoError; type++) { |
|
|
try { |
|
|
std::string fName; |
|
|
switch ((MMMojoCallbackType)type) { |
|
|
case MMMojoCallbackType::kMMReadPush: fName = "kMMReadPush"; break; |
|
|
case MMMojoCallbackType::kMMReadPull: fName = "kMMReadPull"; break; |
|
|
case MMMojoCallbackType::kMMReadShared: fName = "kMMReadShared"; break; |
|
|
case MMMojoCallbackType::kMMRemoteConnect: fName = "kMMRemoteConnect"; break; |
|
|
case MMMojoCallbackType::kMMRemoteDisconnect: fName = "kMMRemoteDisconnect"; break; |
|
|
case MMMojoCallbackType::kMMRemoteProcessLaunched: fName = "kMMRemoteProcessLaunched"; break; |
|
|
case MMMojoCallbackType::kMMRemoteProcessLaunchFailed: fName = "kMMRemoteProcessLaunchFailed"; break; |
|
|
case MMMojoCallbackType::kMMRemoteMojoError: fName = "kMMRemoteMojoError"; break; |
|
|
default: continue; |
|
|
} |
|
|
|
|
|
void* callback = defaultCallbacks.callbacks[fName]; |
|
|
|
|
|
|
|
|
auto it = callbacks.find(fName); |
|
|
if (it != callbacks.end()) { |
|
|
callback = it->second; |
|
|
} |
|
|
|
|
|
|
|
|
MmmojoDll::SetMMMojoEnvironmentCallbacks(intPtrMmmojoEnv, type, callback); |
|
|
} |
|
|
catch (const std::exception& ex) { |
|
|
std::cerr << ex.what() << std::endl; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
void XPluginManager::StopMmMojoEnv() { |
|
|
if (!isMmmojoEnvInited || intPtrMmmojoEnv == nullptr) { |
|
|
return; |
|
|
} |
|
|
|
|
|
MmmojoDll::StopMMMojoEnvironment(intPtrMmmojoEnv); |
|
|
MmmojoDll::RemoveMMMojoEnvironment(intPtrMmmojoEnv); |
|
|
intPtrMmmojoEnv = nullptr; |
|
|
isMmmojoEnvInited = false; |
|
|
} |
|
|
|
|
|
void XPluginManager::SendPbSerializedData(const unsigned char* pbData, int pbSize, int method, int sync, unsigned int requestId) { |
|
|
void* writeInfo = MmmojoDll::CreateMMMojoWriteInfo(method, sync, requestId); |
|
|
void* request = MmmojoDll::GetMMMojoWriteInfoRequest(writeInfo, (unsigned int)pbSize); |
|
|
|
|
|
|
|
|
memcpy(request, pbData, pbSize); |
|
|
|
|
|
|
|
|
MmmojoDll::SendMMMojoWriteInfo(intPtrMmmojoEnv, writeInfo); |
|
|
} |
|
|
|
|
|
void* XPluginManager::GetPbSerializedData(void* requestInfo, unsigned int* dataSize) { |
|
|
return MmmojoDll::GetMMMojoReadInfoRequest(requestInfo, dataSize); |
|
|
} |
|
|
|
|
|
void* XPluginManager::GetReadInfoAttachData(void* requestInfo, unsigned int* dataSize) { |
|
|
return MmmojoDll::GetMMMojoReadInfoAttach(requestInfo, dataSize); |
|
|
} |
|
|
|
|
|
void XPluginManager::RemoveReadInfo(void* requestInfo) { |
|
|
MmmojoDll::RemoveMMMojoReadInfo(requestInfo); |
|
|
} |