Skip to content

Commit

Permalink
・[fix] windows7で起動しない問題をたぶん修正 (WindowsGraphicsCapture関連の処理を WindowsGr…
Browse files Browse the repository at this point in the history
…aphicsCapture.dllに移動させて、動的にロードするようにした)

・[change] ウマ娘ウィンドウが横画面の時は、TextRecognizerを実行しないようにした (CPU負荷対策)
・[fix] 選択肢の候補が複数ある時、より類似度の高い方を選ぶようにした #90
・[fix] イベント名末尾にある"①"や"(イベント進行①)"などの文字列を取り除いた (選択肢検出の精度を上げるため)
  • Loading branch information
amate committed Dec 29, 2021
1 parent 0dfc9dd commit 2c40f2d
Show file tree
Hide file tree
Showing 26 changed files with 730 additions and 211 deletions.
17 changes: 15 additions & 2 deletions UmaCruise.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31129.286
# Visual Studio Version 17
VisualStudioVersion = 17.0.31912.275
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UmaCruise", "UmaCruise\UmaCruise.vcxproj", "{EAA1B122-D050-4C8B-B9CB-1A6BFE424F65}"
ProjectSection(ProjectDependencies) = postProject
{8BB836E1-7B3E-417F-B25C-2AD758B73164} = {8BB836E1-7B3E-417F-B25C-2AD758B73164}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WindowsGraphicsCapture", "WindowsGraphicsCapture\WindowsGraphicsCapture.vcxproj", "{8BB836E1-7B3E-417F-B25C-2AD758B73164}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +26,14 @@ Global
{EAA1B122-D050-4C8B-B9CB-1A6BFE424F65}.Release|x64.Build.0 = Release|x64
{EAA1B122-D050-4C8B-B9CB-1A6BFE424F65}.Release|x86.ActiveCfg = Release|Win32
{EAA1B122-D050-4C8B-B9CB-1A6BFE424F65}.Release|x86.Build.0 = Release|Win32
{8BB836E1-7B3E-417F-B25C-2AD758B73164}.Debug|x64.ActiveCfg = Debug|x64
{8BB836E1-7B3E-417F-B25C-2AD758B73164}.Debug|x64.Build.0 = Debug|x64
{8BB836E1-7B3E-417F-B25C-2AD758B73164}.Debug|x86.ActiveCfg = Debug|Win32
{8BB836E1-7B3E-417F-B25C-2AD758B73164}.Debug|x86.Build.0 = Debug|Win32
{8BB836E1-7B3E-417F-B25C-2AD758B73164}.Release|x64.ActiveCfg = Release|x64
{8BB836E1-7B3E-417F-B25C-2AD758B73164}.Release|x64.Build.0 = Release|x64
{8BB836E1-7B3E-417F-B25C-2AD758B73164}.Release|x86.ActiveCfg = Release|Win32
{8BB836E1-7B3E-417F-B25C-2AD758B73164}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion UmaCruise/AboutDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PreviewWindow;
#define DEBUG_STRING
#endif

constexpr LPCWSTR kAppVersion = L"v1.17" DEBUG_STRING;
constexpr LPCWSTR kAppVersion = L"v1.18" DEBUG_STRING;

class CAboutDlg : public CDialogImpl<CAboutDlg>, DarkModeUI<CAboutDlg>
{
Expand Down
4 changes: 2 additions & 2 deletions UmaCruise/ConfigDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "Utility\Logger.h"
#include "Utility\WinHTTPWrapper.h"

#include "WindowsGraphicsCapture.h"
#include "WindowsGraphicsCaptureWrapper.h"

using json = nlohmann::json;
using namespace WinHTTPWrapper;
Expand Down Expand Up @@ -43,7 +43,7 @@ LRESULT ConfigDlg::OnInitDialog(UINT, WPARAM, LPARAM, BOOL&)
m_screenCaptureMethod = m_config.screenCaptureMethod;
DoDataExchange(DDX_LOAD);

if (!WindowsGraphicsCapture::IsSupported()) {
if (!WindowsGraphicsCaptureWrapper::IsDllLoaded()) {
GetDlgItem(IDC_RADIO_WINDOWSGRAPHICSCAPTURE).EnableWindow(FALSE);
}

Expand Down
1 change: 1 addition & 0 deletions UmaCruise/IScreenShotWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <memory>

#include <atlbase.h>
#include <atlwin.h>
#include <atltypes.h>

Expand Down
19 changes: 16 additions & 3 deletions UmaCruise/MainDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include "GDIWindowCapture.h"
#include "DesktopDuplication.h"
#include "WindowsGraphicsCapture.h"
#include "WindowsGraphicsCaptureWrapper.h"

#include "ConfigDlg.h"

Expand Down Expand Up @@ -613,13 +613,23 @@ void CMainDlg::OnStart(UINT uNotifyCode, int nID, CWindow wndCtl)
}
ssImage2.reset();

const auto milisecInterval = m_config.refreshInterval * 1000;
int count = 0;
while (!m_cancelAutoDetect.load()) {
Utility::timer timer;

const auto begin = std::chrono::steady_clock::now();

auto ssImage = _ScreenShotUmaWindow(); // m_umaTextRecoginzer.ScreenShot();
if (ssImage) {
const int imageWidth = ssImage->GetWidth();
const int imageHeight = ssImage->GetHeight();
if (imageHeight < imageWidth) { // 横画面なら何もしない
m_previewWindow.UpdateImage(ssImage.release());
::Sleep(milisecInterval);
continue;
}
}
bool success = m_umaTextRecoginzer.TextRecognizer(ssImage.get());
if (success) {
if (m_cancelAutoDetect.load()) {
Expand Down Expand Up @@ -665,7 +675,6 @@ void CMainDlg::OnStart(UINT uNotifyCode, int nID, CWindow wndCtl)
ChangeWindowTitle((LPCWSTR)title);

// wait
const auto milisecInterval = m_config.refreshInterval * 1000;
auto end = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
do {
Expand Down Expand Up @@ -1173,7 +1182,11 @@ std::unique_ptr<Gdiplus::Bitmap> CMainDlg::_ScreenShotUmaWindow()
break;

case Config::kWindowsGraphicsCapture:
m_screenshotWindow.reset(new WindowsGraphicsCapture());
if (WindowsGraphicsCaptureWrapper::IsDllLoaded()) {
m_screenshotWindow.reset(WindowsGraphicsCaptureWrapper::CreateWindowsGraphicsCapture());
} else {
m_screenshotWindow.reset(new GDIWindowCapture());
}
break;

default:
Expand Down
11 changes: 10 additions & 1 deletion UmaCruise/UmaCruise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "Utility\json.hpp"
#include "TesseractWrapper.h"
#include "win32-darkmode\DarkMode.h"
#include "WindowsGraphicsCaptureWrapper.h"

// グローバル変数
CAppModule _Module;
Expand Down Expand Up @@ -110,11 +111,19 @@ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lp
BOOL b = funcSetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED);
}


InitDarkMode();

bool bLoadWindowsGraphicsCaptureDll = false;
if (WindowsGraphicsCaptureWrapper::IsSupported()) {
bLoadWindowsGraphicsCaptureDll = WindowsGraphicsCaptureWrapper::LoadLibrary();
}

int nRet = Run(lpstrCmdLine, nCmdShow);

if (bLoadWindowsGraphicsCaptureDll) {
WindowsGraphicsCaptureWrapper::FreeLibrary();
}

g_funcSetThreadDpiAwarenessContext = nullptr;
::FreeLibrary(hModUser32);
hModUser32 = NULL;
Expand Down
8 changes: 6 additions & 2 deletions UmaCruise/UmaCruise.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<WholeProgramOptimization>true</WholeProgramOptimization>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down Expand Up @@ -241,7 +242,10 @@
<ClCompile Include="UmaTextRecognizer.cpp" />
<ClCompile Include="Utility\CodeConvert.cpp" />
<ClCompile Include="Utility\CommonUtility.cpp" />
<ClCompile Include="Utility\GdiplusUtil.cpp" />
<ClCompile Include="Utility\GdiplusUtil.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="Utility\Logger.cpp" />
<ClCompile Include="Utility\WinHTTPWrapper.cpp" />
<ClCompile Include="win32-darkmode\DarkMode.cpp">
Expand Down Expand Up @@ -290,7 +294,7 @@
<ClInclude Include="win32-darkmode\IatHook.h" />
<ClInclude Include="win32-darkmode\ListViewUtil.h" />
<ClInclude Include="win32-darkmode\win32-darkmode.h" />
<ClInclude Include="WindowsGraphicsCapture.h" />
<ClInclude Include="WindowsGraphicsCaptureWrapper.h" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="UmaCruise.rc" />
Expand Down
2 changes: 1 addition & 1 deletion UmaCruise/UmaCruise.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
<ClInclude Include="DesktopDuplication.h">
<Filter>Header Files\ScreenShot</Filter>
</ClInclude>
<ClInclude Include="WindowsGraphicsCapture.h">
<ClInclude Include="WindowsGraphicsCaptureWrapper.h">
<Filter>Header Files\ScreenShot</Filter>
</ClInclude>
<ClInclude Include="GDIWindowCapture.h">
Expand Down
44 changes: 37 additions & 7 deletions UmaCruise/UmaEventLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,54 @@ boost::optional<std::pair<std::wstring, double>> retrieve(
{
// Retrieve similar strings into a string vector.
std::vector<std::wstring> xstrs;
std::wstring query_org;
for (; threshold > minThreshold/*kMinThreshold*/; threshold -= 0.05) { // 少なくとも一つが見つかるような閾値を探す
for (const std::wstring& query : ambiguousEventNames) {
dbr.retrieve(query, measure, threshold, std::back_inserter(xstrs));
if (xstrs.size()) {
query_org = query;
break;
}
}
if (xstrs.size()) {
break;
}
}
if (xstrs.size() >= 2) {
INFO_LOG << L"retrieve - 候補が複数あります [" << query_org << L"]";
// 類似度が高い方を調べる
simstring::ngram_generator ngen(1, false);
std::vector<std::wstring> query_ngrams;
ngen(query_org, std::back_inserter(query_ngrams));
ATLASSERT(query_ngrams.size());
if (query_ngrams.empty()) {
return boost::none;
}
std::wstring topSimilrartyText;
double topSimilrartyRatio = 0.0;
for (const std::wstring& str : xstrs) {
std::vector<std::wstring> str_ngrams;
ngen(str, std::back_inserter(str_ngrams));
ATLASSERT(str_ngrams.size());
int matchCount = 0;
for (const std::wstring& gram : str_ngrams) {
bool found = std::find(query_ngrams.begin(), query_ngrams.end(), gram) != query_ngrams.end();
if (found) {
++matchCount;
}
}
const int maxSize = static_cast<int>(std::max(query_ngrams.size(), str_ngrams.size()));
const double simRatio = static_cast<double>(matchCount) / maxSize;
INFO_LOG << L"[" << str << L"] - " << simRatio;
if (topSimilrartyRatio < simRatio) {
topSimilrartyRatio = simRatio;
topSimilrartyText = str; // 前のより類似度が高い
}
}
INFO_LOG << L"topSimilrartyText -> [" << topSimilrartyText << L"]";
return std::make_pair(topSimilrartyText, topSimilrartyRatio);
}

if (xstrs.size()) {
INFO_LOG << L"result: " << xstrs.front() << L" threshold: " << threshold;
return std::make_pair(xstrs.front(), threshold);
Expand All @@ -61,12 +98,6 @@ boost::optional<std::wstring> retrieve(
}
}

void EventNameNormalize(std::wstring& eventName)
{
std::wregex rx(L"(進行度[^\\)]+)");
eventName = std::regex_replace(eventName, rx, L"");
}

// ==============================================================

bool UmaEventLibrary::LoadUmaMusumeLibrary()
Expand All @@ -78,7 +109,6 @@ bool UmaEventLibrary::LoadUmaMusumeLibrary()
for (const json& jsonEvent : jsonEventList) {
auto eventElm = *jsonEvent.items().begin();
std::wstring eventName = UTF16fromUTF8(eventElm.key());
EventNameNormalize(eventName);

charaEvent.umaEventList.emplace_back();
UmaEvent& umaEvent = charaEvent.umaEventList.back();
Expand Down
5 changes: 0 additions & 5 deletions UmaCruise/UmaTextRecognizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include <unordered_map>
#include <wtl\atlmisc.h>
#include "Utility\GdiplusUtil.h"
#include "DesktopDuplication.h"
#include "WindowsGraphicsCapture.h"

class UmaTextRecognizer
{
Expand Down Expand Up @@ -86,8 +84,5 @@ class UmaTextRecognizer
bool m_bIkuseiTop = false;
int m_entryRaceDistance = 0;

DesktopDuplication m_desktopDuplication;
WindowsGraphicsCapture m_graphicsCapture;

};

5 changes: 4 additions & 1 deletion UmaCruise/Utility/GdiplusUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
* @brief Gdi+を使うのを便利にする
*/

#include "stdafx.h"
//#include "stdafx.h"
#include "GdiplusUtil.h"

#include <shlwapi.h> // for PathMatchSpecW

#pragma comment(lib, "gdiplus.lib")
#pragma comment(lib, "Shlwapi.lib")

using namespace Gdiplus;

Expand Down
1 change: 1 addition & 0 deletions UmaCruise/Utility/GdiplusUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#pragma once

#include <Windows.h>
#include <GdiPlus.h>

// 初期化/後始末
Expand Down
78 changes: 78 additions & 0 deletions UmaCruise/WindowsGraphicsCaptureWrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#pragma once

#include "IScreenShotWindow.h"

#include "Utility\Logger.h"
#include "Utility\CommonUtility.h"

class WindowsGraphicsCaptureWrapper
{
public:

static bool IsSupported()
{
using fnRtlGetNtVersionNumbers = void (WINAPI*)(LPDWORD major, LPDWORD minor, LPDWORD build);
auto RtlGetNtVersionNumbers =
reinterpret_cast<fnRtlGetNtVersionNumbers>(
GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlGetNtVersionNumbers"));
ATLASSERT(RtlGetNtVersionNumbers);
DWORD major, minor, buildNumber;
RtlGetNtVersionNumbers(&major, &minor, &buildNumber);
buildNumber &= ~0xF0000000;
if (major >= 10 && buildNumber >= 19041) {
//bool b = winrt::GraphicsCaptureSession::IsSupported();
//return b;
return true;
}
return false;
}

static bool LoadLibrary()
{
if (!IsSupported()) {
return false;
}

ATLASSERT(!s_hDll);
s_hDll = ::LoadLibrary((GetExeDirectory() / L"WindowsGraphicsCapture.dll").wstring().c_str());
ATLASSERT(s_hDll);
if (!s_hDll) {
ERROR_LOG << L"WindowsGraphicsCapture.dll の読み込みに失敗しました";
return false;

} else {
s_func_CreateWindowsGraphicsCapture = (func_CreateWindowsGraphicsCapture)::GetProcAddress(s_hDll, "CreateWindowsGraphicsCapture");
ATLASSERT(s_func_CreateWindowsGraphicsCapture);
if (s_func_CreateWindowsGraphicsCapture) {
return true;
} else {
return false;
}
}
}

static void FreeLibrary()
{
ATLASSERT(IsDllLoaded());
s_func_CreateWindowsGraphicsCapture = nullptr;
::FreeLibrary(s_hDll);
s_hDll = NULL;
}

static bool IsDllLoaded()
{
bool b = s_hDll && s_func_CreateWindowsGraphicsCapture;
return b;
}

static IScreenShotWindow* CreateWindowsGraphicsCapture()
{
ATLASSERT(s_func_CreateWindowsGraphicsCapture);
return s_func_CreateWindowsGraphicsCapture();
}

private:
using func_CreateWindowsGraphicsCapture = IScreenShotWindow * (*)();
static inline func_CreateWindowsGraphicsCapture s_func_CreateWindowsGraphicsCapture = nullptr;
static inline HMODULE s_hDll = NULL;
};
Loading

0 comments on commit 2c40f2d

Please sign in to comment.