-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
173 lines (138 loc) · 3.09 KB
/
Copy pathmain.cpp
File metadata and controls
173 lines (138 loc) · 3.09 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
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
#include "main.hpp"
#include "windows_lean.h"
#include <string>
#include <sstream>
#include "obse/PluginAPI.h"
#include "obse_common/obse_version.h"
#include "utils.hpp"
#define MOD_VERSION 1
static const int TryFindWindowIntervalMs = 1000;
static const char* ModName = "obl_clipcursor";
static const char* GameClassName = "Oblivion";
static HWND GameWindow = NULL;
static WNDPROC GameWndProc = NULL;
BOOL WINAPI DllMain(HANDLE, DWORD, LPVOID)
{
return true;
}
extern "C" {
EXPORT bool OBSEPlugin_Query(
const OBSEInterface* obse,
PluginInfo* info
) {
info->infoVersion = PluginInfo::kInfoVersion;
info->name = ModName;
info->version = MOD_VERSION;
std::string log_filename(ModName);
log_filename.append(".log");
gLog.Open(log_filename.c_str());
if (!version_check(obse))
return false;
return true;
}
EXPORT bool OBSEPlugin_Load(
const OBSEInterface* obse
) {
_MESSAGE("load");
if (
SetTimer(
NULL,
0,
TryFindWindowIntervalMs,
find_game_window_timer) == 0
) {
fatal_error("Failed to set timer");
return false;
}
return true;
}
}
bool version_check(const OBSEInterface* obse) {
if (obse->isEditor)
return false;
if (obse->obseVersion < OBSE_VERSION_INTEGER) {
std::stringstream ss;
ss
<< "OBSE version is too old ("
<< obse->obseVersion
<< "), expected at least "
<< OBSE_VERSION_INTEGER;
std::string str = ss.str();
fatal_error(str.c_str(), false);
return false;
}
return true;
}
void CALLBACK find_game_window_timer(
HWND window,
UINT wm_timer,
UINT_PTR timer_id,
DWORD tick_count
) {
GameWindow = FindWindowA(GameClassName, NULL);
if (GameWindow == NULL)
return;
KillTimer(window, timer_id);
inject_wndproc();
}
void inject_wndproc() {
GameWndProc = reinterpret_cast<WNDPROC>(
GetWindowLongPtrA(GameWindow, GWLP_WNDPROC));
if (GameWndProc == NULL) {
fatal_error("Failed to get game wndproc");
return;
}
if (
SetWindowLongPtrA(
GameWindow,
GWLP_WNDPROC,
reinterpret_cast<LONG>(&injected_wndproc)) == 0
) {
fatal_error("Failed to set injected wndproc");
return;
}
setup_clipcursor(GameWindow);
_MESSAGE("obl_clipcursor setup correctly");
}
LRESULT CALLBACK injected_wndproc(
HWND window,
UINT message,
WPARAM w_param,
LPARAM l_param
) {
switch (message) {
case WM_ACTIVATE: {
if (w_param == WA_INACTIVE)
unset_clipcursor();
else
setup_clipcursor(window);
break;
}
case WM_MOUSEMOVE: {
setup_clipcursor(window);
break;
}
case WM_SETFOCUS: {
setup_clipcursor(window);
break;
}
case WM_KILLFOCUS: {
unset_clipcursor();
break;
}
}
return GameWndProc(window, message, w_param, l_param);
}
void setup_clipcursor(HWND window) {
RECT rect;
if (!GetWindowRect(window, &rect)) {
fatal_error("Failed to get window rect");
return;
}
if (!ClipCursor(&rect))
fatal_error("Failed to setup clip cursor");
}
void unset_clipcursor() {
if (!ClipCursor(NULL))
fatal_error("Failed to unset clip cursor");
}