Skip to content

Commit

Permalink
fixed crashed for strings and images
Browse files Browse the repository at this point in the history
  • Loading branch information
tmarenko committed Jul 30, 2021
1 parent cd3448c commit 861ca6c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
67 changes: 43 additions & 24 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ BOOL CALLBACK EnumWindowsFunc(HWND hwnd, LPARAM lParam) {
}


void FindGenshinWindow(){
EnumWindows(EnumWindowsFunc, (LPARAM)(&gwi));
void FindGenshinWindow() {
EnumWindows(EnumWindowsFunc, (LPARAM) (&gwi));
genshinWindow = gwi.hwnd;

TCHAR buff[1024]{};
GetWindowText(genshinWindow, buff, 100);
if (!lstrcmp(buff, gwi.windowName)){
if (!gwi.active){
if (!lstrcmp(buff, gwi.windowName)) {
if (!gwi.active) {
gwi.active = true;
std::cout << "Ready for Paimon!" << std::endl;
}
} else{
if (gwi.active){
} else {
if (gwi.active) {
gwi.active = false;
gwi.hwnd = NULL;
std::cout << "Game was closed" << std::endl;
Expand Down Expand Up @@ -131,27 +131,30 @@ std::string GetTextFromImageByRect(const cv::Mat &image, const cv::Rect& rect) {


bool IsPaimonSpeaking() {
cv::Rect cropDefault = cv::Rect((int) (DEFAULT_DIALOGUE_NAME_POS.val[0] * gwi.width),
(int) (DEFAULT_DIALOGUE_NAME_POS.val[1] * gwi.height),
(int) (DEFAULT_DIALOGUE_NAME_POS.val[2] * gwi.width) - (int) (DEFAULT_DIALOGUE_NAME_POS.val[0] * gwi.width),
(int) (DEFAULT_DIALOGUE_NAME_POS.val[3] * gwi.height) - (int) (DEFAULT_DIALOGUE_NAME_POS.val[1] * gwi.height));
cv::Rect cropOut = cv::Rect((int) (OUT_DIALOGUE_NAME_POS.val[0] * gwi.width),
(int) (OUT_DIALOGUE_NAME_POS.val[1] * gwi.height),
(int) (OUT_DIALOGUE_NAME_POS.val[2] * gwi.width) - (int) (OUT_DIALOGUE_NAME_POS.val[0] * gwi.width),
(int) (OUT_DIALOGUE_NAME_POS.val[3] * gwi.height) - (int) (OUT_DIALOGUE_NAME_POS.val[1] * gwi.height));
GetFrame(gwi.width, gwi.height);
if (frame.empty())
return false;
cv::Rect cropDefault = cv::Rect((int) (DEFAULT_DIALOGUE_NAME_POS.val[0] * frame.cols),
(int) (DEFAULT_DIALOGUE_NAME_POS.val[1] * frame.rows),
(int) (DEFAULT_DIALOGUE_NAME_POS.val[2] * frame.cols) - (int) (DEFAULT_DIALOGUE_NAME_POS.val[0] * frame.cols),
(int) (DEFAULT_DIALOGUE_NAME_POS.val[3] * frame.rows) - (int) (DEFAULT_DIALOGUE_NAME_POS.val[1] * frame.rows));
cv::Rect cropOut = cv::Rect((int) (OUT_DIALOGUE_NAME_POS.val[0] * frame.cols),
(int) (OUT_DIALOGUE_NAME_POS.val[1] * frame.rows),
(int) (OUT_DIALOGUE_NAME_POS.val[2] * frame.cols) - (int) (OUT_DIALOGUE_NAME_POS.val[0] * frame.cols),
(int) (OUT_DIALOGUE_NAME_POS.val[3] * frame.rows) - (int) (OUT_DIALOGUE_NAME_POS.val[1] * frame.rows));
std::string defaultDialogue = GetTextFromImageByRect(frame, cropDefault);
std::string outDialogue = GetTextFromImageByRect(frame, cropOut);
return IsStringsSimilar(defaultDialogue, PAIMON_NAME) || IsStringsSimilar(outDialogue, PAIMON_NAME);
}


bool IsGenshinProcess(DWORD pid){
TCHAR buff[1024];
bool IsGenshinProcess(DWORD pid) {
TCHAR buff[1024];
HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
if (GetProcessImageFileName(handle, reinterpret_cast<LPSTR>(buff), sizeof(buff))){
return ((std::string)buff).find("GenshinImpact.exe") != std::string::npos;
if (GetProcessImageFileName(handle, reinterpret_cast<LPSTR>(buff), sizeof(buff))) {
return ((std::string) buff).find("GenshinImpact.exe") != std::string::npos;
}
return false;
}


Expand Down Expand Up @@ -209,34 +212,50 @@ HRESULT SetMuteGenshin(BOOL bMute) {
}


int PaimonShutUp(){
int PaimonShutUp() {
if (InitTesseract(NULL, "eng"))
return 1;

bool paimonWasHere = false;
std::cout << "Waiting for GenshinImpact.exe process." << std::endl;
while (!stop){
while (!stop) {
FindGenshinWindow();
if (!gwi.active)
continue;
bool isPaimonSpeaking = IsPaimonSpeaking();
if (isPaimonSpeaking && !paimonWasHere){
if (isPaimonSpeaking && !paimonWasHere) {
paimonWasHere = true;
std::cout << "Paimon, shut up!" << std::endl;
SetMuteGenshin(isPaimonSpeaking);
}
if (!isPaimonSpeaking && paimonWasHere){
if (!isPaimonSpeaking && paimonWasHere) {
paimonWasHere = false;
std::cout << "Unmuting the game." << std::endl;
SetMuteGenshin(isPaimonSpeaking);
}
}
DestroyTesseract();
SetMuteGenshin(false);
return 0;
}


int main() {
signal(SIGINT, keyHand);
return PaimonShutUp();
try {
signal(SIGINT, keyHand);
SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS);
return PaimonShutUp();
}
catch (const std::runtime_error &re) {
std::cerr << "Runtime error: " << re.what() << std::endl;
}
catch (const std::exception &ex) {
std::cerr << "Error occurred: " << ex.what() << std::endl;
}
catch (...) {
std::cerr << "Unknown failure occurred. Possible memory corruption" << std::endl;
}
DestroyTesseract();
system("pause");
return 1;
}
10 changes: 5 additions & 5 deletions tesseract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int InitTesseract(const char* dataPath, const char* language) {

void DestroyTesseract() {
if (tesseractApi) {
std::cout << "Destroying Tesseract lib." << std::endl;
std::cout << "Destroying Tesseract API." << std::endl;
tesseractApi->End();
delete tesseractApi;
}
Expand All @@ -34,10 +34,12 @@ std::string StripText(const std::string &input) {
return input;
auto start_it = input.begin();
auto end_it = input.rbegin();
while (std::isspace(*start_it))
while (start_it != input.end() && std::isspace(*start_it))
++start_it;
while (std::isspace(*end_it))
while (end_it != input.rend() && std::isspace(*end_it))
++end_it;
if (start_it > end_it.base())
return std::string();
return std::string(start_it, end_it.base());
}

Expand Down Expand Up @@ -70,8 +72,6 @@ unsigned int LevenshteinDistance(const std::string &s1, const std::string &s2) {

for (unsigned int i = 1; i <= len1; ++i)
for (unsigned int j = 1; j <= len2; ++j)
// note that std::min({arg1, arg2, arg3}) works only in C++11,
// for C++98 use std::min(std::min(arg1, arg2), arg3)
d[i][j] = min(min(d[i - 1][j] + 1, d[i][j - 1] + 1),
d[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 0 : 1));
return d[len1][len2];
Expand Down

0 comments on commit 861ca6c

Please sign in to comment.