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

@ -125,8 +125,8 @@ namespace stardew_access.Features
public static CATEGORY DroppedItems => FromString("dropped item"); public static CATEGORY DroppedItems => FromString("dropped item");
public static CATEGORY Others => FromString("other"); public static CATEGORY Others => FromString("other");
} }
public enum MachineState public enum MachineState
{ {
Ready, Busy, Waiting Ready, Busy, Waiting
} }

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,27 +173,44 @@ namespace stardew_access
if (Config.ReadTile) if (Config.ReadTile)
ReadTileFeature.update(); ReadTileFeature.update();
if (!RadarFeature.isRunning && Config.Radar) RunRadarFeatureIfEnabled();
{
RadarFeature.isRunning = true;
RadarFeature.Run();
Task.Delay(RadarFeature.delay).ContinueWith(_ => { RadarFeature.isRunning = false; });
}
if (!isNarratingHudMessage) RunHudMessageNarration();
{
isNarratingHudMessage = true;
Other.narrateHudMessages();
Task.Delay(300).ContinueWith(_ => { isNarratingHudMessage = false; });
}
if (Game1.player != null) RefreshBuildListIfRequired();
async void RunRadarFeatureIfEnabled()
{ {
if (Game1.timeOfDay >= 600 && prevDate != CurrentPlayer.Date) if (!RadarFeature.isRunning && Config.Radar)
{ {
prevDate = CurrentPlayer.Date; RadarFeature.isRunning = true;
DebugLog("Refreshing buildlist..."); RadarFeature.Run();
CustomCommands.onBuildListCalled(); await Task.Delay(RadarFeature.delay);
RadarFeature.isRunning = false;
}
}
async void RunHudMessageNarration()
{
if (!isNarratingHudMessage)
{
isNarratingHudMessage = true;
Other.narrateHudMessages();
await Task.Delay(300);
isNarratingHudMessage = false;
}
}
void RefreshBuildListIfRequired()
{
if (Game1.player != null)
{
if (Game1.timeOfDay >= 600 && prevDate != CurrentPlayer.Date)
{
prevDate = CurrentPlayer.Date;
DebugLog("Refreshing buildlist...");
CustomCommands.onBuildListCalled();
}
} }
} }
} }
@ -204,49 +220,56 @@ namespace stardew_access
if (e == null) if (e == null)
return; return;
#region Simulate left and right clicks void SimulateMouseClicks(Action<int, int> leftClickHandler, Action<int, int> rightClickHandler)
if (Game1.activeClickableMenu != null && !TextBoxPatch.isAnyTextBoxActive)
{ {
bool isCustomizingCharacter = Game1.activeClickableMenu is CharacterCustomization || (TitleMenu.subMenu != null && TitleMenu.subMenu is CharacterCustomization); int mouseX = Game1.getMouseX(true);
int mouseY = Game1.getMouseY(true);
#region Mouse Click Simulation
if (Config.LeftClickMainKey.JustPressed() || Config.LeftClickAlternateKey.JustPressed()) if (Config.LeftClickMainKey.JustPressed() || Config.LeftClickAlternateKey.JustPressed())
{ {
Game1.activeClickableMenu.receiveLeftClick(Game1.getMouseX(true), Game1.getMouseY(true)); leftClickHandler(mouseX, mouseY);
} }
else if (Config.RightClickMainKey.JustPressed() || Config.RightClickAlternateKey.JustPressed())
if (Config.RightClickMainKey.JustPressed() || Config.RightClickAlternateKey.JustPressed())
{ {
Game1.activeClickableMenu.receiveRightClick(Game1.getMouseX(true), Game1.getMouseY(true)); rightClickHandler(mouseX, mouseY);
} }
#endregion
} }
if (Game1.currentMinigame != null && !TextBoxPatch.isAnyTextBoxActive) #region Simulate left and right clicks
if (!TextBoxPatch.isAnyTextBoxActive)
{ {
#region Mouse Click Simulation if (Game1.activeClickableMenu != null)
if (Config.LeftClickMainKey.JustPressed() || Config.LeftClickAlternateKey.JustPressed())
{ {
Game1.currentMinigame.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.currentMinigame.receiveRightClick(Game1.getMouseX(true), Game1.getMouseY(true)); SimulateMouseClicks(
(x, y) => Game1.currentMinigame.receiveLeftClick(x, y),
(x, y) => Game1.currentMinigame.receiveRightClick(x, y)
);
} }
#endregion
} }
#endregion #endregion
if (!Context.IsPlayerFree) if (!Context.IsPlayerFree)
return; return;
// Stops the auto walk controller if any movement key(WASD) is pressed void Narrate(string message) => MainClass.ScreenReader.Say(message, true);
if (TileViewerFeature.isAutoWalking &&
(e.Button.Equals(SButtonExtensions.ToSButton(Game1.options.moveUpButton[0])) bool IsMovementKey(SButton button)
|| e.Button.Equals(SButtonExtensions.ToSButton(Game1.options.moveDownButton[0])) {
|| e.Button.Equals(SButtonExtensions.ToSButton(Game1.options.moveLeftButton[0])) return button.Equals(SButtonExtensions.ToSButton(Game1.options.moveUpButton[0]))
|| e.Button.Equals(SButtonExtensions.ToSButton(Game1.options.moveRightButton[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
if (TileViewerFeature.isAutoWalking && IsMovementKey(e.Button))
{ {
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>