-
Notifications
You must be signed in to change notification settings - Fork 1
/
WoWResizer.cs
347 lines (308 loc) · 12 KB
/
WoWResizer.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections.Generic;
using System.IO;
using System.Threading;
namespace WoWStarter
{
// Class to launch, configure, resize and layout the wow windows
// Should be able to do:
// * Launch all wow windows
// * Relaunch a wow window if it's missing or if number of boxes is increased
// * Change layout if different layout is selected
// * Global hotkey to maximize PIPs or smaller WoW windows or cycle through followers
// *
public class WoWResizer
{
protected WoWStarterConfig config;
protected int selectedWindow = 0;
protected List<Rectangle> layouts = new List<Rectangle>();
//protected List<Process> wowProcesses = new List<Process>(100);
protected List<WoWBoxState> wow = new List<WoWBoxState>(40);
protected int currentMaximized = 0;
protected int windowBorderSize = 0;
protected int windowCaptionSize = 0;
public class WoWBoxState
{
public Process process = null;
public bool isBorderless = false;
public bool isAlwaysOnTop = false;
public Rectangle position = new Rectangle(-1, -1, 0, 0);
}
public WoWResizer(WoWStarterConfig config)
{
this.config = config;
for (int i = 0; i < 40; i++)
wow.Add(new WoWBoxState());
}
public void LaunchWoW(int boxNumber)
{
// first check if the process isn't already running
Process wowProcess = wow[boxNumber].process;
if (wowProcess == null || wowProcess.HasExited)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
// get the appropriate install path of which directory to launch for this box
int installPathIndex = Math.Min(boxNumber, config.installPaths.Length - 1);
String installPath = config.installPaths[installPathIndex];
// check if wow.exe or wowClassic.exe exists
String wowExePath;
if (File.Exists(installPath + "\\Wow.exe"))
wowExePath = installPath + "\\Wow.exe";
else if (File.Exists(installPath + "\\WowClassic.exe"))
wowExePath = installPath + "\\WowClassic.exe";
else
throw new Exception("Wow.exe or WowClassic.exe not found in " + installPath);
startInfo.FileName = wowExePath;
startInfo.WorkingDirectory = installPath;
// check to see if special config<N>.wtf exists and use it. Otherwise don't pass argument
String configWTF = "config" + (boxNumber + 1) + ".wtf";
if (File.Exists(installPath + "\\WTF\\" + configWTF))
startInfo.Arguments = "-config " + configWTF;
wow[boxNumber].process = Process.Start(startInfo);
//wowProcess.WaitForInputIdle(5000);
}
}
public void GenerateWoWLayout()
{
layouts.Clear();
Rectangle screen;
if (config.subtractTaskbarHeight)
{
var vs = SystemInformation.VirtualScreen;
vs = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
screen = new Rectangle(vs.Left, vs.Top, vs.Width, vs.Height);
}
else
screen = new Rectangle(0, 0, SystemInformation.PrimaryMonitorSize.Width, SystemInformation.PrimaryMonitorSize.Height);
if (config.layout == MultiBoxLayouts.BottomRow)
{
// put all them pips on the bottom row, divide screen by boxCount-1
int gridN = Math.Max(2, config.boxCount - 1);
int pipWidth = screen.Width / gridN;
int pipHeight = (pipWidth / 16) * 9;
int mainHeight = screen.Height - pipHeight;
int mainWidth = mainHeight / 9 * 16;
layouts.Add(new Rectangle(screen.Left, screen.Top, mainWidth, mainHeight));
for (int i = 0; i < config.boxCount - 1; i++)
{ // we can fit gridN windows at the bottom
layouts.Add(new Rectangle(i * pipWidth, mainHeight, pipWidth, pipHeight));
}
}
else if (config.layout == MultiBoxLayouts.BottomDoubleRow)
{
// put all them pips on the bottom row, divide screen by boxCount-1
int gridN = Math.Max(4, (int)Math.Ceiling((config.boxCount - 1) / 2.0));
int pipWidth = screen.Width / gridN;
int pipHeight = (pipWidth / 16) * 9;
int mainHeight = screen.Height - pipHeight * 2;
int mainWidth = mainHeight / 9 * 16;
layouts.Add(new Rectangle(screen.Left, screen.Top, mainWidth, mainHeight));
// we can fit equal windows on both rows
int firstRow = ((config.boxCount - 1) / 2);
for (int i = 0; i < config.boxCount - 1; i++)
{
if (i < firstRow)
layouts.Add(new Rectangle(i * pipWidth, mainHeight, pipWidth, pipHeight));
else
layouts.Add(new Rectangle((i - firstRow) * pipWidth, mainHeight + pipHeight, pipWidth, pipHeight));
}
}
else if (config.layout == MultiBoxLayouts.BottomAndRight)
{ // Main window topleft with windows arranted in L shape around bottom right
// how many screens fit into an L shape in an N*N grid: 1 + N + N-1 = 2*N
// Calculate grid needed for LType layout: MB / 2
int gridN = Math.Max(2, (int)Math.Ceiling(config.boxCount / 2.0));
int pipWidth = screen.Width / gridN;
//pipWidth = Math.Floor(pipWidth / 16) * 16;
int pipHeight = (pipWidth / 16) * 9;
int mainHeight = screen.Height - pipHeight;
//mainHeight = Math.Floor(mainHeight / 9) * 9;
int mainWidth = pipWidth * (gridN - 1);
layouts.Add(new Rectangle(screen.Left, screen.Top, mainWidth, mainHeight));
for (int i = 0; i < gridN; i++)
{ // we can fit gridN windows at the bottom
layouts.Add(new Rectangle(i * pipWidth, mainHeight, pipWidth, pipHeight));
}
for (int i = 0; i < (config.boxCount - 1 - gridN); i++)
{ // we can fit the rest at the right
layouts.Add(new Rectangle(mainWidth, mainHeight - (i + 1) * pipHeight, pipWidth, pipHeight));
}
}
else if (config.layout == MultiBoxLayouts.PIPVertical)
{
layouts.Add(screen);
var pip = config.PIPPosition;
for (int i = 0; i < config.boxCount - 1; i++)
{ // put the pips according to config vertical
layouts.Add(new Rectangle(pip.Left, pip.Top + i * pip.Height, pip.Width, pip.Height));
}
}
else if (config.layout == MultiBoxLayouts.CustomConfig)
{
for (int i = 0; i < config.boxCount; i++)
{ // put custom configs in there, repeat last one if not enough
layouts.Add(config.customLayout[Math.Min(i, config.customLayout.Length - 1)]);
}
}
}
public void LayoutWoWWindows()
{
currentMaximized = 0;
for (int i = 0; i < config.boxCount; i++)
{
bool alwaysOnTop = config.alwaysOnTop && (i > 0);
LayoutWoWWindow(i, i, alwaysOnTop);
}
}
public void LayoutWoWWindow(int boxNumber, int layoutNumer, bool alwaysOnTop)
{
WoWBoxState wowState = wow[boxNumber];
wow[boxNumber].process.WaitForInputIdle(5000);
IntPtr wowHandle = wow[boxNumber].process.MainWindowHandle;
// update the window style to borderless and captionless
if (wowState.isBorderless != config.borderless) {
Win32Util.setBorderless(wowHandle, config.borderless);
wowState.isBorderless = config.borderless;
}
int x = layouts[layoutNumer].Left;
int y = layouts[layoutNumer].Top;
int w = layouts[layoutNumer].Width;
int h = layouts[layoutNumer].Height;
// if this is our first window, get the border size by comparing window and client size
if (windowBorderSize == 0)
{
RECT windowRect, clientRect;
Win32Util.GetWindowRect(wowHandle, out windowRect);
Win32Util.GetClientRect(wowHandle, out clientRect);
windowBorderSize = ((windowRect.Right - windowRect.Left) - clientRect.Right) / 2;
windowCaptionSize = ((windowRect.Bottom - windowRect.Top) - clientRect.Bottom - windowBorderSize);
}
// fix window size for borders with shadow
if (!config.borderless)
{
x -= windowBorderSize;
y -= windowCaptionSize;
w += windowBorderSize * 2;
h += windowCaptionSize + windowBorderSize;
}
// move and resize the window as needed. MoveWindow seems to be faster than PositionWindow
Win32Util.MoveWindow(wowHandle, x, y, w, h, false);
//Win32Util.PositionWindow(wowHandle, x, y, w, h, alwaysOnTop);
// resizing can invalidate always on top setting
bool isResize = (wowState.position.Width != w || wowState.position.Height != h);
wowState.position = new Rectangle(x, y, w, h);
// update the always on top flag if needed or on resize
if (wowState.isAlwaysOnTop != alwaysOnTop || isResize) {
Win32Util.setAlwaysOnTop(wowHandle, alwaysOnTop);
wowState.isAlwaysOnTop = alwaysOnTop;
}
}
public void cycleToWindow(int nextMaximized)
{
// reset currentlyMaximized (soon previously maximized) unless it's 0
if (currentMaximized != 0)
LayoutWoWWindow(currentMaximized, currentMaximized, config.alwaysOnTop);
// put the 0 to where the nextMaximized is
LayoutWoWWindow(0, nextMaximized, config.alwaysOnTop);
// maximize the nextMaximized
LayoutWoWWindow(nextMaximized, 0, false);
// this sets the maximized window as the foreground window but messes with switching back and fourth
//Win32Util.SetForegroundWindow(wow[nextMaximized].process.MainWindowHandle);
currentMaximized = nextMaximized;
}
public void maximizeHotkey(bool tabForward)
{
// get the current foreground window
IntPtr forergoundWin = Win32Util.GetForegroundWindow();
// check if it's one of our wow windows
for (int i = 0; i < config.boxCount; i++)
{
Process wp = wow[i].process;
if (wp != null && !wp.HasExited && wp.MainWindowHandle == forergoundWin)
{
// either we pressed Ctrl+Tab above the maximized or main window and want to cycle next
// or we Ctrl+Tab above above one of the PIP windows and want to maximize that one
if (i == currentMaximized)
{
int nextMaximized = (currentMaximized + 1) % config.boxCount;
cycleToWindow(nextMaximized);
}
else
{
cycleToWindow(i);
}
// break from this loop
return;
}
}
}
public bool isWoWRunning(int boxNumber)
{
return wow[boxNumber].process != null && !wow[boxNumber].process.HasExited && wow[boxNumber].process.MainWindowHandle != IntPtr.Zero;
}
public int recoverAlreadyRunningWoWWindows()
{
GenerateWoWLayout();
// see if we can / want to recover previous wow instances
int runningWoWs = 0;
int orphanCount = 0;
for (int i = 0; i < config.boxCount; i++)
if (isWoWRunning(i))
runningWoWs++;
if (runningWoWs < config.boxCount)
{
// find orphaned wow processes that are not in our list and sort them in dictionary
SortedDictionary<long, Process> orphans = new SortedDictionary<long, Process>();
Process[] list = Process.GetProcesses();
foreach (Process p in list)
{
if (p.MainWindowTitle.StartsWith("World of Warcraft") && p.MainModule.ModuleName.Equals("Wow.exe"))
{
bool isOrphaned = true;
for (int i = 0; i < config.boxCount; i++)
if (isWoWRunning(i) && wow[i].process.MainWindowHandle == p.MainWindowHandle)
isOrphaned = false;
if (isOrphaned)
orphans.Add(p.StartTime.Ticks, p);
}
}
orphanCount = orphans.Count;
// go through each wow and assign the oldest orphan
List<Process> orphansList = new List<Process>(orphans.Values);
//orphansList.Reverse(); // why no reverse? Weird lol
for (int i = 0; i < config.boxCount; i++)
if (!isWoWRunning(i) && orphansList.Count > 0)
{
wow[i].process = orphansList[0];
orphansList.RemoveAt(0);
}
}
return orphanCount;
}
public void LaunchWoWClients()
{
currentMaximized = 0;
GenerateWoWLayout();
// first make sure all are launched
for (int i = 0; i < config.boxCount; i++)
LaunchWoW(i);
// the rack 'em and stack em
for (int i = 0; i < config.boxCount; i++)
LayoutWoWWindow(i, i, config.alwaysOnTop && (i > 0));
}
public bool isLaunched()
{
return (wow[0].process != null && !wow[0].process.HasExited);
}
public void CloseWoWClients()
{
for (int i = 0; i < wow.Count; i++)
{
if (wow[i].process != null && !wow[i].process.HasExited)
wow[i].process.CloseMainWindow();
}
}
}
}