Skip to content

Commit db8435c

Browse files
committed
Refactor.
1 parent 9312071 commit db8435c

File tree

2 files changed

+92
-81
lines changed

2 files changed

+92
-81
lines changed

TTMouseclickSimulator/Core/Environment/InteractionProvider.cs

+14-15
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,11 @@ protected virtual void TransformVirtualKey(ref WindowsEnvironment.VirtualKey key
550550
// Do nothing.
551551
}
552552

553+
protected virtual void ValidateWindowPositionAndSize(WindowPosition windowPosition)
554+
{
555+
// Do nothing.
556+
}
557+
553558
private void ThrowIfCapabilityNotSet(SimulatorCapabilities capabilities)
554559
{
555560
if (!this.simulator.RequiredCapabilities.IsSet(capabilities))
@@ -571,7 +576,7 @@ private void WaitCore(int milliseconds, bool checkWindow = true)
571576
}
572577
else
573578
{
574-
// Wait max. 100 ms, and check if the TT window is still active.
579+
// Wait at most 100 ms, and check if the window is still active.
575580
var sw = new Stopwatch();
576581
sw.Start();
577582
while (true)
@@ -593,30 +598,24 @@ private void WaitCore(int milliseconds, bool checkWindow = true)
593598

594599
private WindowPosition GetWindowPositionCore(bool failIfMinimized = true)
595600
{
596-
// Fail if the window is no longer in foreground (active) and we are not
597-
// using background mode.
601+
// Fail if the window is no longer in foreground (active) and we are not using
602+
// background mode.
598603
var windowPosition = this.environmentInterface.GetWindowPosition(
599604
this.windowHandle,
600605
out _,
601606
failIfNotInForeground: !this.backgroundMode,
602607
failIfMinimized: failIfMinimized);
603608

604-
// When we use mouse input or capture screenshots, check that the aspect
605-
// ratio of the window is 4:3 or higher if the window currently isn't minimized.
609+
// When we use mouse input or capture screenshots, allow the subclass to validate the
610+
// window location and size.
606611
if ((this.simulator.RequiredCapabilities.IsSet(SimulatorCapabilities.MouseInput) ||
607-
this.simulator.RequiredCapabilities.IsSet(SimulatorCapabilities.CaptureScreenshot)) &&
608-
!windowPosition.IsMinimized)
612+
this.simulator.RequiredCapabilities.IsSet(SimulatorCapabilities.CaptureScreenshot)))
609613
{
610-
if (((double)windowPosition.Size.Width / windowPosition.Size.Height) < 4d / 3d)
611-
{
612-
throw new ArgumentException(
613-
"The Toontown window must have an aspect ratio " +
614-
"of 4:3 or higher (e.g. 16:9).");
615-
}
614+
this.ValidateWindowPositionAndSize(windowPosition);
616615

617616
// TODO: Check if the window is beyond the virtual screen size (if in non-background
618-
// mode and we require mouse or screenshot capabilities, or if in background mode and
619-
// we require screenshot capabilities).
617+
// mode and we require mouse or screenshot capabilities, or if in background mode
618+
// and we require screenshot capabilities).
620619
}
621620

622621
return windowPosition;

TTMouseclickSimulator/Core/Toontown/Environment/ToontownInteractionProvider.cs

+78-66
Original file line numberDiff line numberDiff line change
@@ -4,90 +4,102 @@
44

55
using TTMouseClickSimulator.Core.Environment;
66

7-
namespace TTMouseClickSimulator.Core.Toontown.Environment
7+
namespace TTMouseClickSimulator.Core.Toontown.Environment;
8+
9+
public class ToontownInteractionProvider : InteractionProvider
810
{
9-
public class ToontownInteractionProvider : InteractionProvider
10-
{
11-
private const string TTRProcessNameX64 = "TTREngine64";
11+
private const string TTRProcessNameX64 = "TTREngine64";
1212

13-
private const string TTRProcessNameX86 = "TTREngine";
13+
private const string TTRProcessNameX86 = "TTREngine";
1414

15-
private const string CCProcessName = "CorporateClash";
15+
private const string CCProcessName = "CorporateClash";
1616

17-
public ToontownInteractionProvider(
18-
Simulator simulator,
19-
ToontownFlavor toontownFlavor,
20-
WindowsEnvironment environmentInterface,
21-
bool backgroundMode)
22-
: base(simulator, environmentInterface, backgroundMode)
23-
{
24-
this.ToontownFlavor = toontownFlavor;
25-
}
17+
public ToontownInteractionProvider(
18+
Simulator simulator,
19+
ToontownFlavor toontownFlavor,
20+
WindowsEnvironment environmentInterface,
21+
bool backgroundMode)
22+
: base(simulator, environmentInterface, backgroundMode)
23+
{
24+
this.ToontownFlavor = toontownFlavor;
25+
}
2626

27-
public ToontownFlavor ToontownFlavor
28-
{
29-
get;
30-
}
27+
public ToontownFlavor ToontownFlavor
28+
{
29+
get;
30+
}
3131

32-
public bool UseWasdMovement
33-
{
34-
get;
35-
init;
36-
}
32+
public bool UseWasdMovement
33+
{
34+
get;
35+
init;
36+
}
3737

38-
protected override sealed List<Process> FindProcesses()
38+
protected override sealed List<Process> FindProcesses()
39+
{
40+
if (this.ToontownFlavor is ToontownFlavor.ToontownRewritten)
3941
{
40-
if (this.ToontownFlavor is ToontownFlavor.ToontownRewritten)
41-
{
42-
var processes = this.environmentInterface.FindProcessesByName(TTRProcessNameX64);
43-
processes.AddRange(this.environmentInterface.FindProcessesByName(TTRProcessNameX86));
44-
45-
if (processes.Count is 0)
46-
{
47-
throw new InvalidOperationException(
48-
"Could not find Toontown Rewritten. Please make sure " +
49-
"TT Rewritten is running before starting the simulator.\n\n" +
50-
"If you're running Toontown Rewritten as administrator, you may also " +
51-
"need to the simulator as administrator.");
52-
}
42+
var processes = this.environmentInterface.FindProcessesByName(TTRProcessNameX64);
43+
processes.AddRange(this.environmentInterface.FindProcessesByName(TTRProcessNameX86));
5344

54-
return processes;
55-
}
56-
else if (this.ToontownFlavor is ToontownFlavor.CorporateClash)
45+
if (processes.Count is 0)
5746
{
58-
var processes = this.environmentInterface.FindProcessesByName(CCProcessName);
47+
throw new InvalidOperationException(
48+
"Could not find Toontown Rewritten. Please make sure " +
49+
"TT Rewritten is running before starting the simulator.\n\n" +
50+
"If you're running Toontown Rewritten as administrator, you may also " +
51+
"need to the simulator as administrator.");
52+
}
5953

60-
if (processes.Count is 0)
61-
{
62-
throw new InvalidOperationException(
63-
"Could not find Corporate Clash. Please make sure " +
64-
"Corporate Clash is running before starting the simulator.\n\n" +
65-
"If you're running Corporate Clash as administrator, you may also " +
66-
"need to the simulator as administrator.");
67-
}
54+
return processes;
55+
}
56+
else if (this.ToontownFlavor is ToontownFlavor.CorporateClash)
57+
{
58+
var processes = this.environmentInterface.FindProcessesByName(CCProcessName);
6859

69-
return processes;
70-
}
71-
else
60+
if (processes.Count is 0)
7261
{
73-
throw new NotSupportedException("Unsupported Toontown flavor: " + this.ToontownFlavor);
62+
throw new InvalidOperationException(
63+
"Could not find Corporate Clash. Please make sure " +
64+
"Corporate Clash is running before starting the simulator.\n\n" +
65+
"If you're running Corporate Clash as administrator, you may also " +
66+
"need to the simulator as administrator.");
7467
}
68+
69+
return processes;
70+
}
71+
else
72+
{
73+
throw new NotSupportedException("Unsupported Toontown flavor: " + this.ToontownFlavor);
7574
}
75+
}
76+
77+
protected override void ValidateWindowPositionAndSize(WindowPosition windowPosition)
78+
{
79+
// Check that the aspect ratio of the window is 4:3 or higher if the window
80+
// currently isn't minimized.
81+
if (!windowPosition.IsMinimized &&
82+
((double)windowPosition.Size.Width / windowPosition.Size.Height) < 4d / 3d)
83+
{
84+
throw new ArgumentException(
85+
"The Toontown window must have an aspect ratio " +
86+
"of 4:3 or higher (e.g. 16:9).");
87+
}
88+
}
7689

77-
protected override void TransformVirtualKey(ref WindowsEnvironment.VirtualKey key)
90+
protected override void TransformVirtualKey(ref WindowsEnvironment.VirtualKey key)
91+
{
92+
if (this.UseWasdMovement)
7893
{
79-
if (this.UseWasdMovement)
94+
// Replace arrow keys with WASD keys.
95+
key = key switch
8096
{
81-
// Replace arrow keys with WASD keys.
82-
key = key switch
83-
{
84-
WindowsEnvironment.VirtualKey.Up => WindowsEnvironment.VirtualKey.W,
85-
WindowsEnvironment.VirtualKey.Down => WindowsEnvironment.VirtualKey.S,
86-
WindowsEnvironment.VirtualKey.Left => WindowsEnvironment.VirtualKey.A,
87-
WindowsEnvironment.VirtualKey.Right => WindowsEnvironment.VirtualKey.D,
88-
var other => other
89-
};
90-
}
97+
WindowsEnvironment.VirtualKey.Up => WindowsEnvironment.VirtualKey.W,
98+
WindowsEnvironment.VirtualKey.Down => WindowsEnvironment.VirtualKey.S,
99+
WindowsEnvironment.VirtualKey.Left => WindowsEnvironment.VirtualKey.A,
100+
WindowsEnvironment.VirtualKey.Right => WindowsEnvironment.VirtualKey.D,
101+
var other => other
102+
};
91103
}
92104
}
93105
}

0 commit comments

Comments
 (0)