-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathProgram.cs
258 lines (219 loc) · 9.07 KB
/
Program.cs
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using Fix_LCU_Window.Util;
using Fix_LCU_Window.ErrorDict;
using static Fix_LCU_Window.Util.ProcessControl;
using static Fix_LCU_Window.Util.LeagueClientAPI;
namespace Fix_LCU_Window
{
class Program
{
static LeagueClientAPI GetLCU()
{
var LeagueClientId = GetProcessId("LeagueClientUx");
var LeagueClientUxCommandLine = GetCommandLineByProcessId(LeagueClientId);
var LeagueClientUxArgs = CommandLineParser(LeagueClientUxCommandLine);
if (!LeagueClientUxArgs.Available)
{
return null;
}
// Console.WriteLine(LeagueClientUxArgs.ToString());
// Return a new instance of LeagueClientAPI
return new LeagueClientAPI(LeagueClientUxArgs.Port, LeagueClientUxArgs.Token);
}
static (IntPtr LeagueClientWindowHWnd, IntPtr LeagueClientWindowCefHWnd) GetLeagueClientWindowHandle()
{
IntPtr LeagueClientWindowHWnd = FindWindow("RCLIENT", "League of Legends");
IntPtr LeagueClientWindowCefHWnd = FindWindowEx(LeagueClientWindowHWnd, IntPtr.Zero, "CefBrowserWindow", null);
// Return a tuple of the two window handles
return (LeagueClientWindowHWnd, LeagueClientWindowCefHWnd);
}
static bool needResize(RECT Rect)
{
return (Rect.Bottom - Rect.Top) /
(double)(Rect.Right - Rect.Left) != 0.5625;
}
static async Task<int> FixLeagueClientWindow(bool forced = false)
{
var LeagueClientWindow = GetLeagueClientWindowHandle();
var LeagueClientWindowRect = new RECT();
var LeagueClientWindowCefRect = new RECT();
if (
LeagueClientWindow.LeagueClientWindowHWnd == IntPtr.Zero ||
LeagueClientWindow.LeagueClientWindowCefHWnd == IntPtr.Zero
)
{
return -1; // Failed to get leagueclient window handle
}
if (IsMinimalized(LeagueClientWindow.LeagueClientWindowHWnd))
{
return 0;
}
GetWindowRect(LeagueClientWindow.LeagueClientWindowHWnd, ref LeagueClientWindowRect);
GetWindowRect(LeagueClientWindow.LeagueClientWindowCefHWnd, ref LeagueClientWindowCefRect);
if (!needResize(LeagueClientWindowRect) && !needResize(LeagueClientWindowCefRect) && !forced)
{
return 0;
}
var LeagueClientAPIClient = GetLCU();
if (LeagueClientAPIClient == null)
{
return -2; // Failed to get leagueclient api instance
}
var LeagueClientZoom = await LeagueClientAPIClient.GetClientZoom();
var PrimaryScreenWidth = Screen.PrimaryScreen.Bounds.Width;
var PrimaryScreenHeight = Screen.PrimaryScreen.Bounds.Height;
var PrimaryScreenDpi = GetDpiForWindow(LeagueClientWindow.LeagueClientWindowHWnd) / (double)GetDpiForSystem();
if (LeagueClientZoom == -1)
{
return -3; // Failed to get leagueclient zoom
}
var TargetLeagueClientWindowWidth = (int)(1280 * LeagueClientZoom);
var TargetLeagueClientWindowHeight = (int)(720 * LeagueClientZoom);
PatchDpiChangedMessage(LeagueClientWindow.LeagueClientWindowHWnd);
PatchDpiChangedMessage(LeagueClientWindow.LeagueClientWindowCefHWnd);
SetWindowPos(
LeagueClientWindow.LeagueClientWindowHWnd,
0,
(PrimaryScreenWidth - TargetLeagueClientWindowWidth) / 2,
(PrimaryScreenHeight - TargetLeagueClientWindowHeight) / 2,
TargetLeagueClientWindowWidth, TargetLeagueClientWindowHeight,
0x0040
);
SetWindowPos(
LeagueClientWindow.LeagueClientWindowCefHWnd,
0,
0,
0,
TargetLeagueClientWindowWidth,
TargetLeagueClientWindowHeight,
0x0040
);
return 1;
}
static void Main(string[] args)
{
if (args.Length >= 2 && args[0] == "--mode" && int.TryParse(args[1], out int mode))
{
Run(true, mode).Wait();
}
else
{
Run(false).Wait();
Console.WriteLine("> 按任意键退出...");
Console.ReadKey();
}
}
static void PrintMenu()
{
Console.WriteLine("------");
Console.WriteLine("> 功能菜单: ");
Console.WriteLine("| [ 0 ]: 退出");
Console.WriteLine("| [ 1 ]: 立即恢复客户端窗口到正常大小 ");
Console.WriteLine("| [ 2 ]: 自动恢复客户端窗口到正常大小 (常驻)");
Console.WriteLine("| [ 3 ]: 直接跳过结算页面");
Console.WriteLine("| [ 4 ]: 热重载客户端");
Console.WriteLine("> ");
Console.Write("> 请输入功能序号并回车以执行 (1): ");
}
static void PrintCopyright()
{
Console.WriteLine("------");
Console.WriteLine("> [ 英雄联盟客户端疑难杂症解决方案 ]");
Console.WriteLine("> Fix LCU Window -- " + GetVersion());
Console.WriteLine("------");
Console.WriteLine("> Github: https://github.com/LeagueTavern/fix-lcu-window");
Console.WriteLine("> Bilibili: Butter_Cookies");
Console.WriteLine("> Code by LeagueTavern");
}
static string GetVersion()
{
return Application.ProductVersion.Substring(0, Application.ProductVersion.LastIndexOf('.'));
}
static int GetUserChoice()
{
var UserInput = 0;
var UserOriginalInput = Console.ReadLine();
if (String.IsNullOrWhiteSpace(UserOriginalInput) || !int.TryParse(UserOriginalInput, out UserInput))
{
UserInput = 1; // Default to 1
}
return UserInput;
}
static async Task<string> Plan1()
{
var Result = await FixLeagueClientWindow(true);
var ErrorMessage = FixLeagueClientWindowError.ErrorDict[Result];
return ErrorMessage;
}
static async Task Plan2()
{
int CurrentTriggerCount = 0;
Console.WriteLine("> 已进入自动检测模式,您现在可以放心的去玩游戏了");
Console.WriteLine("> 当客户端尺寸出现异常时,程序将会自动修复");
Console.WriteLine("> 按下 [Ctrl] + [C] 或 直接关闭本窗口 即可关闭检测并结束本程序");
Console.WriteLine("> ------");
while (true)
{
var CurrentResult = await FixLeagueClientWindow();
if (CurrentResult == 1)
{
CurrentTriggerCount++;
Console.WriteLine("> 检测到窗口尺寸异常,已自动处理 (" + CurrentTriggerCount + ")");
}
await Task.Delay(1500);
};
}
static async Task<string> Plan3()
{
var LeagueClientAPIClient = GetLCU();
var Result = await LeagueClientAPIClient.LobbyPlayAgain();
return Result ? "成功向客户端发送指令" : "向客户端发送指令时出现问题";
}
static async Task<string> Plan4()
{
var LeagueClientAPIClient = GetLCU();
var Result = await LeagueClientAPIClient.RestartClientUx();
return Result ? "成功向客户端发送指令" : "向客户端发送指令时出现问题";
}
static async Task Run(bool withArgs, int mode = 0)
{
if (!withArgs)
{
PrintCopyright();
PrintMenu();
}
var UserChoice = withArgs ? mode : GetUserChoice();
if (withArgs)
{
Console.WriteLine("> 正在执行功能: " + mode);
}
else
{
Console.WriteLine("> ------");
}
switch (UserChoice)
{
case 0: // Exit
return;
case 1: // Fix League Client Window
Console.WriteLine("> " + await Plan1());
break;
case 2: // Fix League Client Window (Auto)
await Plan2();
break;
case 3: // Skip Post Game
Console.WriteLine("> " + await Plan3());
break;
case 4: // Restart Client
Console.WriteLine("> " + await Plan4());
break;
default: // Unknown Choice
Console.WriteLine("> 未知的功能序号");
break;
}
return;
}
}
}