Skip to content

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreisser committed Jan 24, 2022
1 parent 9312071 commit db8435c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 81 deletions.
29 changes: 14 additions & 15 deletions TTMouseclickSimulator/Core/Environment/InteractionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,11 @@ protected virtual void TransformVirtualKey(ref WindowsEnvironment.VirtualKey key
// Do nothing.
}

protected virtual void ValidateWindowPositionAndSize(WindowPosition windowPosition)
{
// Do nothing.
}

private void ThrowIfCapabilityNotSet(SimulatorCapabilities capabilities)
{
if (!this.simulator.RequiredCapabilities.IsSet(capabilities))
Expand All @@ -571,7 +576,7 @@ private void WaitCore(int milliseconds, bool checkWindow = true)
}
else
{
// Wait max. 100 ms, and check if the TT window is still active.
// Wait at most 100 ms, and check if the window is still active.
var sw = new Stopwatch();
sw.Start();
while (true)
Expand All @@ -593,30 +598,24 @@ private void WaitCore(int milliseconds, bool checkWindow = true)

private WindowPosition GetWindowPositionCore(bool failIfMinimized = true)
{
// Fail if the window is no longer in foreground (active) and we are not
// using background mode.
// Fail if the window is no longer in foreground (active) and we are not using
// background mode.
var windowPosition = this.environmentInterface.GetWindowPosition(
this.windowHandle,
out _,
failIfNotInForeground: !this.backgroundMode,
failIfMinimized: failIfMinimized);

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

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

return windowPosition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,90 +4,102 @@

using TTMouseClickSimulator.Core.Environment;

namespace TTMouseClickSimulator.Core.Toontown.Environment
namespace TTMouseClickSimulator.Core.Toontown.Environment;

public class ToontownInteractionProvider : InteractionProvider
{
public class ToontownInteractionProvider : InteractionProvider
{
private const string TTRProcessNameX64 = "TTREngine64";
private const string TTRProcessNameX64 = "TTREngine64";

private const string TTRProcessNameX86 = "TTREngine";
private const string TTRProcessNameX86 = "TTREngine";

private const string CCProcessName = "CorporateClash";
private const string CCProcessName = "CorporateClash";

public ToontownInteractionProvider(
Simulator simulator,
ToontownFlavor toontownFlavor,
WindowsEnvironment environmentInterface,
bool backgroundMode)
: base(simulator, environmentInterface, backgroundMode)
{
this.ToontownFlavor = toontownFlavor;
}
public ToontownInteractionProvider(
Simulator simulator,
ToontownFlavor toontownFlavor,
WindowsEnvironment environmentInterface,
bool backgroundMode)
: base(simulator, environmentInterface, backgroundMode)
{
this.ToontownFlavor = toontownFlavor;
}

public ToontownFlavor ToontownFlavor
{
get;
}
public ToontownFlavor ToontownFlavor
{
get;
}

public bool UseWasdMovement
{
get;
init;
}
public bool UseWasdMovement
{
get;
init;
}

protected override sealed List<Process> FindProcesses()
protected override sealed List<Process> FindProcesses()
{
if (this.ToontownFlavor is ToontownFlavor.ToontownRewritten)
{
if (this.ToontownFlavor is ToontownFlavor.ToontownRewritten)
{
var processes = this.environmentInterface.FindProcessesByName(TTRProcessNameX64);
processes.AddRange(this.environmentInterface.FindProcessesByName(TTRProcessNameX86));

if (processes.Count is 0)
{
throw new InvalidOperationException(
"Could not find Toontown Rewritten. Please make sure " +
"TT Rewritten is running before starting the simulator.\n\n" +
"If you're running Toontown Rewritten as administrator, you may also " +
"need to the simulator as administrator.");
}
var processes = this.environmentInterface.FindProcessesByName(TTRProcessNameX64);
processes.AddRange(this.environmentInterface.FindProcessesByName(TTRProcessNameX86));

return processes;
}
else if (this.ToontownFlavor is ToontownFlavor.CorporateClash)
if (processes.Count is 0)
{
var processes = this.environmentInterface.FindProcessesByName(CCProcessName);
throw new InvalidOperationException(
"Could not find Toontown Rewritten. Please make sure " +
"TT Rewritten is running before starting the simulator.\n\n" +
"If you're running Toontown Rewritten as administrator, you may also " +
"need to the simulator as administrator.");
}

if (processes.Count is 0)
{
throw new InvalidOperationException(
"Could not find Corporate Clash. Please make sure " +
"Corporate Clash is running before starting the simulator.\n\n" +
"If you're running Corporate Clash as administrator, you may also " +
"need to the simulator as administrator.");
}
return processes;
}
else if (this.ToontownFlavor is ToontownFlavor.CorporateClash)
{
var processes = this.environmentInterface.FindProcessesByName(CCProcessName);

return processes;
}
else
if (processes.Count is 0)
{
throw new NotSupportedException("Unsupported Toontown flavor: " + this.ToontownFlavor);
throw new InvalidOperationException(
"Could not find Corporate Clash. Please make sure " +
"Corporate Clash is running before starting the simulator.\n\n" +
"If you're running Corporate Clash as administrator, you may also " +
"need to the simulator as administrator.");
}

return processes;
}
else
{
throw new NotSupportedException("Unsupported Toontown flavor: " + this.ToontownFlavor);
}
}

protected override void ValidateWindowPositionAndSize(WindowPosition windowPosition)
{
// Check that the aspect ratio of the window is 4:3 or higher if the window
// currently isn't minimized.
if (!windowPosition.IsMinimized &&
((double)windowPosition.Size.Width / windowPosition.Size.Height) < 4d / 3d)
{
throw new ArgumentException(
"The Toontown window must have an aspect ratio " +
"of 4:3 or higher (e.g. 16:9).");
}
}

protected override void TransformVirtualKey(ref WindowsEnvironment.VirtualKey key)
protected override void TransformVirtualKey(ref WindowsEnvironment.VirtualKey key)
{
if (this.UseWasdMovement)
{
if (this.UseWasdMovement)
// Replace arrow keys with WASD keys.
key = key switch
{
// Replace arrow keys with WASD keys.
key = key switch
{
WindowsEnvironment.VirtualKey.Up => WindowsEnvironment.VirtualKey.W,
WindowsEnvironment.VirtualKey.Down => WindowsEnvironment.VirtualKey.S,
WindowsEnvironment.VirtualKey.Left => WindowsEnvironment.VirtualKey.A,
WindowsEnvironment.VirtualKey.Right => WindowsEnvironment.VirtualKey.D,
var other => other
};
}
WindowsEnvironment.VirtualKey.Up => WindowsEnvironment.VirtualKey.W,
WindowsEnvironment.VirtualKey.Down => WindowsEnvironment.VirtualKey.S,
WindowsEnvironment.VirtualKey.Left => WindowsEnvironment.VirtualKey.A,
WindowsEnvironment.VirtualKey.Right => WindowsEnvironment.VirtualKey.D,
var other => other
};
}
}
}

0 comments on commit db8435c

Please sign in to comment.