Remove dependency on Newtonsoft.Json since we're now relying on System.Text.Json.

Some optimization and refactoring in ModEntry.cs
Other minor fixes
master
Katie Durden 2023-04-04 19:21:04 -07:00
parent ad0cb687b7
commit ed405a139e
3 changed files with 93 additions and 84 deletions

View File

@ -7,6 +7,7 @@ using stardew_access.Patches;
using stardew_access.ScreenReader; using stardew_access.ScreenReader;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using StardewValley.Menus; using StardewValley.Menus;
using Microsoft.Xna.Framework.Input;
namespace stardew_access namespace stardew_access
{ {
@ -134,6 +135,12 @@ namespace stardew_access
AppDomain.CurrentDomain.ProcessExit += OnExit; AppDomain.CurrentDomain.ProcessExit += OnExit;
} }
/// <summary>Returns the Screen Reader class for other mods to use.</summary>
public override object GetApi()
{
return new API();
}
public void OnExit(object? sender, EventArgs? e) public void OnExit(object? sender, EventArgs? e)
{ {
// This closes the connection with the screen reader, important for linux // This closes the connection with the screen reader, important for linux
@ -142,12 +149,6 @@ namespace stardew_access
ScreenReader.CloseScreenReader(); ScreenReader.CloseScreenReader();
} }
/// <summary>Returns the Screen Reader class for other mods to use.</summary>
public override object GetApi()
{
return new API();
}
private void onDayStarted(object? sender, DayStartedEventArgs? e) private void onDayStarted(object? sender, DayStartedEventArgs? e)
{ {
StaticTiles.LoadTilesFiles(); StaticTiles.LoadTilesFiles();
@ -161,10 +162,8 @@ namespace stardew_access
// Narrates currently selected inventory slot // Narrates currently selected inventory slot
Other.narrateCurrentSlot(); Other.narrateCurrentSlot();
// Narrate current location's name // Narrate current location's name
Other.narrateCurrentLocation(); Other.narrateCurrentLocation();
//handle TileCursor update logic //handle TileCursor update logic
TileViewerFeature.update(); TileViewerFeature.update();
@ -174,20 +173,36 @@ namespace stardew_access
if (Config.ReadTile) if (Config.ReadTile)
ReadTileFeature.update(); ReadTileFeature.update();
RunRadarFeatureIfEnabled();
RunHudMessageNarration();
RefreshBuildListIfRequired();
async void RunRadarFeatureIfEnabled()
{
if (!RadarFeature.isRunning && Config.Radar) if (!RadarFeature.isRunning && Config.Radar)
{ {
RadarFeature.isRunning = true; RadarFeature.isRunning = true;
RadarFeature.Run(); RadarFeature.Run();
Task.Delay(RadarFeature.delay).ContinueWith(_ => { RadarFeature.isRunning = false; }); await Task.Delay(RadarFeature.delay);
RadarFeature.isRunning = false;
}
} }
async void RunHudMessageNarration()
{
if (!isNarratingHudMessage) if (!isNarratingHudMessage)
{ {
isNarratingHudMessage = true; isNarratingHudMessage = true;
Other.narrateHudMessages(); Other.narrateHudMessages();
Task.Delay(300).ContinueWith(_ => { isNarratingHudMessage = false; }); await Task.Delay(300);
isNarratingHudMessage = false;
}
} }
void RefreshBuildListIfRequired()
{
if (Game1.player != null) if (Game1.player != null)
{ {
if (Game1.timeOfDay >= 600 && prevDate != CurrentPlayer.Date) if (Game1.timeOfDay >= 600 && prevDate != CurrentPlayer.Date)
@ -198,55 +213,63 @@ namespace stardew_access
} }
} }
} }
}
private void OnButtonPressed(object? sender, ButtonPressedEventArgs? e) private void OnButtonPressed(object? sender, ButtonPressedEventArgs? e)
{ {
if (e == null) if (e == null)
return; return;
void SimulateMouseClicks(Action<int, int> leftClickHandler, Action<int, int> rightClickHandler)
{
int mouseX = Game1.getMouseX(true);
int mouseY = Game1.getMouseY(true);
if (Config.LeftClickMainKey.JustPressed() || Config.LeftClickAlternateKey.JustPressed())
{
leftClickHandler(mouseX, mouseY);
}
else if (Config.RightClickMainKey.JustPressed() || Config.RightClickAlternateKey.JustPressed())
{
rightClickHandler(mouseX, mouseY);
}
}
#region Simulate left and right clicks #region Simulate left and right clicks
if (Game1.activeClickableMenu != null && !TextBoxPatch.isAnyTextBoxActive) if (!TextBoxPatch.isAnyTextBoxActive)
{ {
bool isCustomizingCharacter = Game1.activeClickableMenu is CharacterCustomization || (TitleMenu.subMenu != null && TitleMenu.subMenu is CharacterCustomization); if (Game1.activeClickableMenu != null)
#region Mouse Click Simulation
if (Config.LeftClickMainKey.JustPressed() || Config.LeftClickAlternateKey.JustPressed())
{ {
Game1.activeClickableMenu.receiveLeftClick(Game1.getMouseX(true), Game1.getMouseY(true)); SimulateMouseClicks(
(x, y) => Game1.activeClickableMenu.receiveLeftClick(x, y),
(x, y) => Game1.activeClickableMenu.receiveRightClick(x, y)
);
} }
else if (Game1.currentMinigame != null)
if (Config.RightClickMainKey.JustPressed() || Config.RightClickAlternateKey.JustPressed())
{ {
Game1.activeClickableMenu.receiveRightClick(Game1.getMouseX(true), Game1.getMouseY(true)); SimulateMouseClicks(
(x, y) => Game1.currentMinigame.receiveLeftClick(x, y),
(x, y) => Game1.currentMinigame.receiveRightClick(x, y)
);
} }
#endregion
}
if (Game1.currentMinigame != null && !TextBoxPatch.isAnyTextBoxActive)
{
#region Mouse Click Simulation
if (Config.LeftClickMainKey.JustPressed() || Config.LeftClickAlternateKey.JustPressed())
{
Game1.currentMinigame.receiveLeftClick(Game1.getMouseX(true), Game1.getMouseY(true));
}
if (Config.RightClickMainKey.JustPressed() || Config.RightClickAlternateKey.JustPressed())
{
Game1.currentMinigame.receiveRightClick(Game1.getMouseX(true), Game1.getMouseY(true));
}
#endregion
} }
#endregion #endregion
if (!Context.IsPlayerFree) if (!Context.IsPlayerFree)
return; return;
void Narrate(string message) => MainClass.ScreenReader.Say(message, true);
bool IsMovementKey(SButton button)
{
return button.Equals(SButtonExtensions.ToSButton(Game1.options.moveUpButton[0]))
|| button.Equals(SButtonExtensions.ToSButton(Game1.options.moveDownButton[0]))
|| button.Equals(SButtonExtensions.ToSButton(Game1.options.moveLeftButton[0]))
|| button.Equals(SButtonExtensions.ToSButton(Game1.options.moveRightButton[0]));
}
// Stops the auto walk controller if any movement key(WASD) is pressed // Stops the auto walk controller if any movement key(WASD) is pressed
if (TileViewerFeature.isAutoWalking && if (TileViewerFeature.isAutoWalking && IsMovementKey(e.Button))
(e.Button.Equals(SButtonExtensions.ToSButton(Game1.options.moveUpButton[0]))
|| e.Button.Equals(SButtonExtensions.ToSButton(Game1.options.moveDownButton[0]))
|| e.Button.Equals(SButtonExtensions.ToSButton(Game1.options.moveLeftButton[0]))
|| e.Button.Equals(SButtonExtensions.ToSButton(Game1.options.moveRightButton[0]))))
{ {
TileViewerFeature.stopAutoWalking(wasForced: true); TileViewerFeature.stopAutoWalking(wasForced: true);
} }
@ -254,25 +277,17 @@ namespace stardew_access
// Narrate Current Location // Narrate Current Location
if (Config.LocationKey.JustPressed()) if (Config.LocationKey.JustPressed())
{ {
string toSpeak = $"{Game1.currentLocation.Name}"; Narrate(Game1.currentLocation.Name);
MainClass.ScreenReader.Say(toSpeak, true);
return; return;
} }
// Narrate Position // Narrate Position
if (Config.PositionKey.JustPressed()) if (Config.PositionKey.JustPressed())
{ {
string toSpeak; string toSpeak = Config.VerboseCoordinates
if (Config.VerboseCoordinates) ? $"X: {CurrentPlayer.PositionX}, Y: {CurrentPlayer.PositionY}"
{ : $"{CurrentPlayer.PositionX}, {CurrentPlayer.PositionY}";
toSpeak = $"X: {CurrentPlayer.PositionX}, Y: {CurrentPlayer.PositionY}"; Narrate(toSpeak);
}
else
{
toSpeak = $"{CurrentPlayer.PositionX}, {CurrentPlayer.PositionY}";
}
MainClass.ScreenReader.Say(toSpeak, true);
return; return;
} }
@ -282,29 +297,25 @@ namespace stardew_access
if (ModHelper == null) if (ModHelper == null)
return; return;
string toSpeak; string toSpeak = Config.HealthNStaminaInPercentage
if (Config.HealthNStaminaInPercentage) ? ModHelper.Translation.Get("manuallytriggered.healthnstamina.percent", new { health = CurrentPlayer.PercentHealth, stamina = CurrentPlayer.PercentStamina })
toSpeak = ModHelper.Translation.Get("manuallytriggered.healthnstamina.percent", new { health = CurrentPlayer.PercentHealth, stamina = CurrentPlayer.PercentStamina }); : ModHelper.Translation.Get("manuallytriggered.healthnstamina.normal", new { health = CurrentPlayer.CurrentHealth, stamina = CurrentPlayer.CurrentStamina });
else
toSpeak = ModHelper.Translation.Get("manuallytriggered.healthnstamina.normal", new { health = CurrentPlayer.CurrentHealth, stamina = CurrentPlayer.CurrentStamina });
MainClass.ScreenReader.Say(toSpeak, true); Narrate(toSpeak);
return; return;
} }
// Narrate money at hand // Narrate money at hand
if (Config.MoneyKey.JustPressed()) if (Config.MoneyKey.JustPressed())
{ {
string toSpeak = $"You have {CurrentPlayer.Money}g"; Narrate($"You have {CurrentPlayer.Money}g");
MainClass.ScreenReader.Say(toSpeak, true);
return; return;
} }
// Narrate time and season // Narrate time and season
if (Config.TimeNSeasonKey.JustPressed()) if (Config.TimeNSeasonKey.JustPressed())
{ {
string toSpeak = $"Time is {CurrentPlayer.TimeOfDay} and it is {CurrentPlayer.Day} {CurrentPlayer.Date} of {CurrentPlayer.Season}"; Narrate($"Time is {CurrentPlayer.TimeOfDay} and it is {CurrentPlayer.Day} {CurrentPlayer.Date} of {CurrentPlayer.Season}");
MainClass.ScreenReader.Say(toSpeak, true);
return; return;
} }
@ -326,28 +337,27 @@ namespace stardew_access
TileViewerFeature.HandleInput(); TileViewerFeature.HandleInput();
} }
public static void ErrorLog(string message) private static void LogMessage(string message, LogLevel logLevel)
{ {
if (monitor == null) if (monitor == null)
return; return;
monitor.Log(message, LogLevel.Error); monitor.Log(message, logLevel);
}
public static void ErrorLog(string message)
{
LogMessage(message, LogLevel.Error);
} }
public static void InfoLog(string message) public static void InfoLog(string message)
{ {
if (monitor == null) LogMessage(message, LogLevel.Info);
return;
monitor.Log(message, LogLevel.Info);
} }
public static void DebugLog(string message) public static void DebugLog(string message)
{ {
if (monitor == null) LogMessage(message, LogLevel.Debug);
return;
monitor.Log(message, LogLevel.Debug);
} }
} }
} }

View File

@ -13,7 +13,6 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Lib.Harmony" Version="2.2.2" /> <PackageReference Include="Lib.Harmony" Version="2.2.2" />
<PackageReference Include="newtonsoft.json" Version="13.0.2" />
<PackageReference Include="Pathoschild.Stardew.ModBuildConfig" Version="4.1.0" /> <PackageReference Include="Pathoschild.Stardew.ModBuildConfig" Version="4.1.0" />
<Reference Include="./TolkDotNet.dll" /> <Reference Include="./TolkDotNet.dll" />
</ItemGroup> </ItemGroup>