File size: 7,005 Bytes
17f5b5c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
#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;
// 检查路径是否以exe结尾,如果不是,则添加exe名称
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() {
// 检查OCR可执行文件路径是否设置
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;
}
// 初始化MMMojo环境
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);
} |