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 Microsoft.Xna.Framework;
using StardewValley.Menus;
using Microsoft.Xna.Framework.Input;
namespace stardew_access
{
@ -134,6 +135,12 @@ namespace stardew_access
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)
{
// This closes the connection with the screen reader, important for linux
@ -142,12 +149,6 @@ namespace stardew_access
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)
{
StaticTiles.LoadTilesFiles();
@ -161,10 +162,8 @@ namespace stardew_access
// Narrates currently selected inventory slot
Other.narrateCurrentSlot();
// Narrate current location's name
Other.narrateCurrentLocation();
//handle TileCursor update logic
TileViewerFeature.update();
@ -174,20 +173,36 @@ namespace stardew_access
if (Config.ReadTile)
ReadTileFeature.update();
RunRadarFeatureIfEnabled();
RunHudMessageNarration();
RefreshBuildListIfRequired();
async void RunRadarFeatureIfEnabled()
{
if (!RadarFeature.isRunning && Config.Radar)
{
RadarFeature.isRunning = true;
RadarFeature.Run();
Task.Delay(RadarFeature.delay).ContinueWith(_ => { RadarFeature.isRunning = false; });
await Task.Delay(RadarFeature.delay);
RadarFeature.isRunning = false;
}
}
async void RunHudMessageNarration()
{
if (!isNarratingHudMessage)
{
isNarratingHudMessage = true;
Other.narrateHudMessages();
Task.Delay(300).ContinueWith(_ => { isNarratingHudMessage = false; });
await Task.Delay(300);
isNarratingHudMessage = false;
}
}
void RefreshBuildListIfRequired()
{
if (Game1.player != null)
{
if (Game1.timeOfDay >= 600 && prevDate != CurrentPlayer.Date)
@ -198,55 +213,63 @@ namespace stardew_access
}
}
}
}
private void OnButtonPressed(object? sender, ButtonPressedEventArgs? e)
{
if (e == null)
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
if (Game1.activeClickableMenu != null && !TextBoxPatch.isAnyTextBoxActive)
if (!TextBoxPatch.isAnyTextBoxActive)
{
bool isCustomizingCharacter = Game1.activeClickableMenu is CharacterCustomization || (TitleMenu.subMenu != null && TitleMenu.subMenu is CharacterCustomization);
#region Mouse Click Simulation
if (Config.LeftClickMainKey.JustPressed() || Config.LeftClickAlternateKey.JustPressed())
if (Game1.activeClickableMenu != null)
{
Game1.activeClickableMenu.receiveLeftClick(Game1.getMouseX(true), Game1.getMouseY(true));
SimulateMouseClicks(
(x, y) => Game1.activeClickableMenu.receiveLeftClick(x, y),
(x, y) => Game1.activeClickableMenu.receiveRightClick(x, y)
);
}
if (Config.RightClickMainKey.JustPressed() || Config.RightClickAlternateKey.JustPressed())
else if (Game1.currentMinigame != null)
{
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
if (!Context.IsPlayerFree)
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
if (TileViewerFeature.isAutoWalking &&
(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]))))
if (TileViewerFeature.isAutoWalking && IsMovementKey(e.Button))
{
TileViewerFeature.stopAutoWalking(wasForced: true);
}
@ -254,25 +277,17 @@ namespace stardew_access
// Narrate Current Location
if (Config.LocationKey.JustPressed())
{
string toSpeak = $"{Game1.currentLocation.Name}";
MainClass.ScreenReader.Say(toSpeak, true);
Narrate(Game1.currentLocation.Name);
return;
}
// Narrate Position
if (Config.PositionKey.JustPressed())
{
string toSpeak;
if (Config.VerboseCoordinates)
{
toSpeak = $"X: {CurrentPlayer.PositionX}, Y: {CurrentPlayer.PositionY}";
}
else
{
toSpeak = $"{CurrentPlayer.PositionX}, {CurrentPlayer.PositionY}";
}
MainClass.ScreenReader.Say(toSpeak, true);
string toSpeak = Config.VerboseCoordinates
? $"X: {CurrentPlayer.PositionX}, Y: {CurrentPlayer.PositionY}"
: $"{CurrentPlayer.PositionX}, {CurrentPlayer.PositionY}";
Narrate(toSpeak);
return;
}
@ -282,29 +297,25 @@ namespace stardew_access
if (ModHelper == null)
return;
string toSpeak;
if (Config.HealthNStaminaInPercentage)
toSpeak = ModHelper.Translation.Get("manuallytriggered.healthnstamina.percent", new { health = CurrentPlayer.PercentHealth, stamina = CurrentPlayer.PercentStamina });
else
toSpeak = ModHelper.Translation.Get("manuallytriggered.healthnstamina.normal", new { health = CurrentPlayer.CurrentHealth, stamina = CurrentPlayer.CurrentStamina });
string toSpeak = Config.HealthNStaminaInPercentage
? 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 });
MainClass.ScreenReader.Say(toSpeak, true);
Narrate(toSpeak);
return;
}
// Narrate money at hand
if (Config.MoneyKey.JustPressed())
{
string toSpeak = $"You have {CurrentPlayer.Money}g";
MainClass.ScreenReader.Say(toSpeak, true);
Narrate($"You have {CurrentPlayer.Money}g");
return;
}
// Narrate time and season
if (Config.TimeNSeasonKey.JustPressed())
{
string toSpeak = $"Time is {CurrentPlayer.TimeOfDay} and it is {CurrentPlayer.Day} {CurrentPlayer.Date} of {CurrentPlayer.Season}";
MainClass.ScreenReader.Say(toSpeak, true);
Narrate($"Time is {CurrentPlayer.TimeOfDay} and it is {CurrentPlayer.Day} {CurrentPlayer.Date} of {CurrentPlayer.Season}");
return;
}
@ -326,28 +337,27 @@ namespace stardew_access
TileViewerFeature.HandleInput();
}
public static void ErrorLog(string message)
private static void LogMessage(string message, LogLevel logLevel)
{
if (monitor == null)
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)
{
if (monitor == null)
return;
monitor.Log(message, LogLevel.Info);
LogMessage(message, LogLevel.Info);
}
public static void DebugLog(string message)
{
if (monitor == null)
return;
monitor.Log(message, LogLevel.Debug);
LogMessage(message, LogLevel.Debug);
}
}
}

View File

@ -13,7 +13,6 @@
<ItemGroup>
<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" />
<Reference Include="./TolkDotNet.dll" />
</ItemGroup>