Repository navigation
Expand file tree
/
Copy pathconfig.cpp
More file actions
114 lines (94 loc) · 3.21 KB
/
Copy pathconfig.cpp
File metadata and controls
114 lines (94 loc) · 3.21 KB
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
#include "config.h"
#include "log.h"
#include <string>
namespace
{
constexpr int kFullScreenWindow = 1;
constexpr char kConfigFileName[] = "ultrawide.ini";
constexpr char kUnityWindowClass[] = "UnityWndClass";
constexpr char kWindowTitle[] = "UNBEATABLE";
TargetResolution FromMonitorRect(const RECT& r)
{
return { r.right - r.left, r.bottom - r.top, kFullScreenWindow };
}
TargetResolution PrimaryMonitorResolution()
{
return { GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
kFullScreenWindow };
}
bool TryMonitorResolutionForWindow(HWND hwnd, TargetResolution& out)
{
HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
if (!monitor)
{
return false;
}
MONITORINFO info{};
info.cbSize = sizeof(info);
if (!GetMonitorInfoA(monitor, &info))
{
return false;
}
out = FromMonitorRect(info.rcMonitor);
return true;
}
TargetResolution DetectDisplayResolution()
{
HWND hwnd = FindWindowA(kUnityWindowClass, kWindowTitle);
TargetResolution result{};
if (hwnd && TryMonitorResolutionForWindow(hwnd, result))
{
Logf("Using game window's monitor: %dx%d",
result.width, result.height);
return result;
}
result = PrimaryMonitorResolution();
Logf("Game window not found; using primary monitor: %dx%d",
result.width, result.height);
return result;
}
std::string PathBesideModule(HMODULE selfModule, const char* fileName)
{
char modulePath[MAX_PATH]{};
const DWORD len =
GetModuleFileNameA(selfModule, modulePath, MAX_PATH);
if (len == 0 || len == MAX_PATH)
{
return {};
}
std::string path(modulePath, len);
const size_t slash = path.find_last_of("\\/");
if (slash == std::string::npos)
{
return {};
}
return path.substr(0, slash + 1) + fileName;
}
int ReadIniInt(const std::string& iniPath, const char* key, int fallback)
{
return static_cast<int>(
GetPrivateProfileIntA("Resolution", key, fallback, iniPath.c_str()));
}
bool FileExists(const std::string& path)
{
const DWORD attrs = GetFileAttributesA(path.c_str());
return attrs != INVALID_FILE_ATTRIBUTES &&
!(attrs & FILE_ATTRIBUTE_DIRECTORY);
}
}
TargetResolution ResolveTargetResolution(HMODULE selfModule)
{
const TargetResolution display = DetectDisplayResolution();
const std::string iniPath = PathBesideModule(selfModule, kConfigFileName);
if (iniPath.empty() || !FileExists(iniPath))
{
Logf("No config file; using detected resolution %dx%d",
display.width, display.height);
return display;
}
const int width = ReadIniInt(iniPath, "Width", display.width);
const int height = ReadIniInt(iniPath, "Height", display.height);
Logf("Loaded config %s: %dx%d", iniPath.c_str(), width, height);
return { width, height, kFullScreenWindow };
}