From db8435c5d0c7309c3e09b214b0a587b3e12d32c2 Mon Sep 17 00:00:00 2001 From: kpreisser Date: Mon, 24 Jan 2022 21:55:04 +0100 Subject: [PATCH] Refactor. --- .../Core/Environment/InteractionProvider.cs | 29 ++-- .../ToontownInteractionProvider.cs | 144 ++++++++++-------- 2 files changed, 92 insertions(+), 81 deletions(-) diff --git a/TTMouseclickSimulator/Core/Environment/InteractionProvider.cs b/TTMouseclickSimulator/Core/Environment/InteractionProvider.cs index 0c6ff43..b449227 100644 --- a/TTMouseclickSimulator/Core/Environment/InteractionProvider.cs +++ b/TTMouseclickSimulator/Core/Environment/InteractionProvider.cs @@ -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)) @@ -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) @@ -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; diff --git a/TTMouseclickSimulator/Core/Toontown/Environment/ToontownInteractionProvider.cs b/TTMouseclickSimulator/Core/Toontown/Environment/ToontownInteractionProvider.cs index 340409b..d9659d9 100644 --- a/TTMouseclickSimulator/Core/Toontown/Environment/ToontownInteractionProvider.cs +++ b/TTMouseclickSimulator/Core/Toontown/Environment/ToontownInteractionProvider.cs @@ -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 FindProcesses() + protected override sealed List 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 + }; } } }