Skip to content

Commit

Permalink
Merge pull request #410 from Vivelin/windows-settings-updates
Browse files Browse the repository at this point in the history
Windows launcher and auto track viewed events
  • Loading branch information
MattEqualsCoder committed Oct 14, 2023
2 parents a14a21b + e0e0308 commit 7936e18
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 71 deletions.
4 changes: 3 additions & 1 deletion src/Randomizer.App/Controls/MultiRomListPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Randomizer.Data.Services;
using Randomizer.Shared.Models;
using Randomizer.SMZ3.Generation;
using Randomizer.SMZ3.Infrastructure;

namespace Randomizer.App.Controls
{
Expand All @@ -23,7 +24,8 @@ public MultiRomListPanel(IServiceProvider serviceProvider,
OptionsFactory optionsFactory,
ILogger<MultiRomListPanel> logger,
RomGenerationService romGenerationService,
IGameDbService gameDbService) : base(serviceProvider, optionsFactory, logger, romGenerationService, gameDbService)
IGameDbService gameDbService,
RomLauncherService romLauncherService) : base(serviceProvider, optionsFactory, logger, romGenerationService, gameDbService, romLauncherService)
{
Model = new MultiplayerGamesViewModel();
DataContext = Model;
Expand Down
13 changes: 7 additions & 6 deletions src/Randomizer.App/Controls/RomListPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,30 @@
using Randomizer.Data.Services;
using Randomizer.Shared.Models;
using Randomizer.SMZ3.Generation;
using Randomizer.SMZ3.Infrastructure;
using Randomizer.SMZ3.Tracking;

namespace Randomizer.App.Controls
{
public abstract class RomListPanel : UserControl
{
private TrackerWindow? _trackerWindow;
private RomLauncherService _romLauncherService;


public RomListPanel(IServiceProvider serviceProvider,
OptionsFactory optionsFactory,
ILogger<RomListPanel> logger,
RomGenerationService romGenerationService,
IGameDbService gameDbService)
IGameDbService gameDbService,
RomLauncherService romLauncherService)
{
ServiceProvider = serviceProvider;
Logger = logger;
RomGenerationService = romGenerationService;
GameDbService = gameDbService;
Options = optionsFactory.Create();
_romLauncherService = romLauncherService;
CheckSpeechRecognition();
}

Expand Down Expand Up @@ -170,11 +174,8 @@ public void LaunchRom(GeneratedRom rom)
{
try
{
Process.Start(new ProcessStartInfo
{
FileName = path,
UseShellExecute = true
});
_romLauncherService.LaunchRom(path, Options.GeneralOptions.LaunchApplication,
Options.GeneralOptions.LaunchArguments);
}
catch (Win32Exception e)
{
Expand Down
4 changes: 3 additions & 1 deletion src/Randomizer.App/Controls/SoloRomListPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Randomizer.Shared.Models;
using Randomizer.SMZ3.Contracts;
using Randomizer.SMZ3.Generation;
using Randomizer.SMZ3.Infrastructure;
using YamlDotNet.Core;

namespace Randomizer.App.Controls
Expand All @@ -26,7 +27,8 @@ public SoloRomListPanel(IServiceProvider serviceProvider,
OptionsFactory optionsFactory,
ILogger<SoloRomListPanel> logger,
RomGenerationService romGenerationService,
IGameDbService gameDbService) : base(serviceProvider, optionsFactory, logger, romGenerationService, gameDbService)
IGameDbService gameDbService,
RomLauncherService romLauncherService) : base(serviceProvider, optionsFactory, logger, romGenerationService, gameDbService, romLauncherService)
{
Model = new GeneratedRomsViewModel();
DataContext = Model;
Expand Down
35 changes: 32 additions & 3 deletions src/Randomizer.App/Windows/OptionsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,28 @@
</Grid>
</controls:LabeledControl>

<controls:LabeledControl Text="Launch application:"
ToolTip="Determines the application to launch for the rom. If not supplied, it will use the system default for sfc files.">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<TextBox Name="LaunchApplicationTextBox" Text="{Binding LaunchApplication, Mode=TwoWay}" />
</Grid>
</controls:LabeledControl>

<controls:LabeledControl Text="Launch arguments:"
ToolTip="Arguments to be passed to the launch application, if needed. %rom% will be replaced with the rom path. If %rom% is not in the launch argument, the rom path will be passed at the end.">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<TextBox Name="LaunchArgumentsTextbox" Text="{Binding LaunchArguments, Mode=TwoWay}" />
</Grid>
</controls:LabeledControl>

<controls:LabeledControl Text="Tracker voice frequency:"
ToolTip="Determines how much tracker will say in response to events.">
<Grid>
Expand Down Expand Up @@ -246,16 +268,23 @@
</controls:LabeledControl>

<StackPanel Orientation="Horizontal">

</StackPanel>

<WrapPanel>
<CheckBox Content="Auto Tracker Updates Map Automatically"
IsChecked="{Binding AutoTrackerChangeMap}" />
</StackPanel>

<StackPanel Orientation="Horizontal">
<CheckBox Content="Auto Track Viewed Events"
ToolTip="Automatically track things that are viewed, such as dungeon rewards when looking the map and dungeon requirements when near their entrances."
IsChecked="{Binding AutoSaveLookAtEvents}" />

<CheckBox Content="Enable hints"
IsChecked="{Binding TrackerHintsEnabled}" />

<CheckBox Content="Enable spoilers"
IsChecked="{Binding TrackerSpoilersEnabled}" />
</StackPanel>
</WrapPanel>

</StackPanel>
</Expander>
Expand Down
55 changes: 6 additions & 49 deletions src/Randomizer.CrossPlatform/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Randomizer.Data.Options;
using Randomizer.Data.Services;
using Randomizer.SMZ3.Generation;
using Randomizer.SMZ3.Infrastructure;
using Serilog;

namespace Randomizer.CrossPlatform;
Expand Down Expand Up @@ -34,6 +35,7 @@ public static void Main(string[] args)
var optionsFile = new FileInfo("randomizer-options.yml");
var randomizerOptions = s_services.GetRequiredService<OptionsFactory>().LoadFromFile(optionsFile.FullName, optionsFile.FullName, true);
randomizerOptions.Save();
var launcher = s_services.GetRequiredService<RomLauncherService>();

if (!ValidateRandomizerOptions(randomizerOptions))
{
Expand Down Expand Up @@ -77,7 +79,8 @@ public static void Main(string[] args)
{
var romPath = Path.Combine(randomizerOptions.RomOutputPath, results.Rom!.RomPath);
Console.WriteLine($"Rom generated successfully: {romPath}");
Launch(romPath, randomizerOptions);
launcher.LaunchRom(romPath, randomizerOptions.GeneralOptions.LaunchApplication,
randomizerOptions.GeneralOptions.LaunchArguments);
_ = s_services.GetRequiredService<ConsoleTrackerDisplayService>().StartTracking(results.Rom, romPath);
}
else
Expand All @@ -99,7 +102,8 @@ public static void Main(string[] args)
{
var selectedRom = roms[result.Value.Item1];
var romPath = Path.Combine(randomizerOptions.RomOutputPath, selectedRom.RomPath);
Launch(romPath, randomizerOptions);
launcher.LaunchRom(romPath, randomizerOptions.GeneralOptions.LaunchApplication,
randomizerOptions.GeneralOptions.LaunchArguments);
_ = s_services.GetRequiredService<ConsoleTrackerDisplayService>().StartTracking(selectedRom, romPath);
}

Expand Down Expand Up @@ -182,53 +186,6 @@ private static (int, string)? DisplayMenu(string prompt, List<string> options)

}

private static void Launch(string romPath, RandomizerOptions options)
{
if (!File.Exists(romPath))
{
Log.Error("No test rom found at {Path}", romPath);
return;
}

var launchApplication = options.GeneralOptions.LaunchApplication;
var launchArguments = "";
if (string.IsNullOrEmpty(launchApplication))
{
launchApplication = romPath;
}
else
{
if (string.IsNullOrEmpty(options.GeneralOptions.LaunchArguments))
{
launchArguments = $"\"{romPath}\"";
}
else if (options.GeneralOptions.LaunchArguments.Contains("%rom%"))
{
launchArguments = options.GeneralOptions.LaunchArguments.Replace("%rom%", $"{romPath}");
}
else
{
launchArguments = $"{options.GeneralOptions.LaunchArguments} \"{romPath}\"";
}
}

try
{
Console.WriteLine($"Executing {launchApplication} {launchArguments}");
Log.Information("Executing {FileName} {Arguments}", launchApplication, launchArguments);
Process.Start(new ProcessStartInfo
{
FileName = launchApplication,
Arguments = launchArguments,
UseShellExecute = true,
});
}
catch (Exception e)
{
Log.Error(e, "Unable to launch rom");
}
}

private static void InitializeMsuRandomizer(IMsuRandomizerInitializationService msuRandomizerInitializationService)
{
var settingsStream = Assembly.GetExecutingAssembly()
Expand Down
2 changes: 1 addition & 1 deletion src/Randomizer.Data/Options/GeneralOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public string? TwitchId
/// <summary>
/// Automatically tracks the map and other "hey tracker, look at this" events when viewing
/// </summary>
public bool AutoSaveLookAtEvents { get; set; }
public bool AutoSaveLookAtEvents { get; set; } = true;

public event PropertyChangedEventHandler? PropertyChanged;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class ViewedMap : IZeldaStateCheck
{
private Tracker? _tracker;
private readonly IWorldAccessor _worldAccessor;
private bool _lightWorldUpdated;
private bool _darkWorldUpdated;

public ViewedMap(IWorldAccessor worldAccessor, IItemService itemService)
{
Expand All @@ -41,23 +43,23 @@ public ViewedMap(IWorldAccessor worldAccessor, IItemService itemService)
/// <returns>True if the check was identified, false otherwise</returns>
public bool ExecuteCheck(Tracker tracker, AutoTrackerZeldaState currentState, AutoTrackerZeldaState prevState)
{
if (tracker.AutoTracker == null) return false;
if (tracker.AutoTracker == null || (_lightWorldUpdated && _darkWorldUpdated)) return false;

if (currentState.State == 14 && currentState.Substate == 7 && currentState.ReadUInt8(0xE0) == 0x80)
{
_tracker = tracker;
var currentRegion = tracker.World.Regions
.OfType<Z3Region>()
.FirstOrDefault(x => x.StartingRooms != null && x.StartingRooms.Contains(currentState.OverworldScreen) && x.IsOverworld);
if (currentRegion is LightWorldNorthWest or LightWorldNorthEast or LightWorldSouth or LightWorldDeathMountainEast or LightWorldDeathMountainWest)
if (currentRegion is LightWorldNorthWest or LightWorldNorthEast or LightWorldSouth or LightWorldDeathMountainEast or LightWorldDeathMountainWest && !_lightWorldUpdated)
{
tracker.AutoTracker.LatestViewAction = new AutoTrackerViewedAction(UpdateLightWorldRewards);
if (tracker.Options.AutoSaveLookAtEvents)
{
tracker.AutoTracker.LatestViewAction.Invoke();
}
}
else if (currentRegion is DarkWorldNorthWest or DarkWorldNorthEast or DarkWorldSouth or DarkWorldMire or DarkWorldDeathMountainEast or DarkWorldDeathMountainWest)
else if (currentRegion is DarkWorldNorthWest or DarkWorldNorthEast or DarkWorldSouth or DarkWorldMire or DarkWorldDeathMountainEast or DarkWorldDeathMountainWest && !_darkWorldUpdated)
{
tracker.AutoTracker.LatestViewAction = new AutoTrackerViewedAction(UpdateDarkWorldRewards);
if (tracker.Options.AutoSaveLookAtEvents)
Expand All @@ -76,7 +78,7 @@ public bool ExecuteCheck(Tracker tracker, AutoTrackerZeldaState currentState, Au
/// </summary>
private void UpdateLightWorldRewards()
{
if (_tracker == null) return;
if (_tracker == null || _lightWorldUpdated) return;

var rewards = new List<RewardType>();
var dungeons = new (Region Region, ItemType Map)[] {
Expand Down Expand Up @@ -107,14 +109,22 @@ private void UpdateLightWorldRewards()
{
_tracker.Say(x => x.AutoTracker.LookedAtNothing);
}

// If all dungeons are marked, save the light world as updated
if (dungeons.Select(x => x.Region as IDungeon).Count(x => x?.DungeonState.MarkedReward != null) >=
dungeons.Length)
{
_lightWorldUpdated = true;
}

}

/// <summary>
/// Marks all of the rewards for the dark world dungeons
/// </summary>
protected void UpdateDarkWorldRewards()
{
if (_tracker == null) return;
if (_tracker == null || _darkWorldUpdated) return;

var rewards = new List<RewardType>();
var dungeons = new (Region Region, ItemType Map)[] {
Expand Down Expand Up @@ -153,6 +163,13 @@ protected void UpdateDarkWorldRewards()
_tracker.Say(x => x.AutoTracker.LookedAtNothing);
}

// If all dungeons are marked, save the light world as updated
if (dungeons.Select(x => x.Region as IDungeon).Count(x => x?.DungeonState.MarkedReward != null) >=
dungeons.Length)
{
_darkWorldUpdated = true;
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class ViewedMedallion : IZeldaStateCheck
{
private Tracker? _tracker;
private readonly IWorldAccessor _worldAccessor;
private bool _mireUpdated;
private bool _turtleRockUpdated;

public ViewedMedallion(IWorldAccessor worldAccessor, IItemService itemService)
{
Expand All @@ -31,14 +33,14 @@ public ViewedMedallion(IWorldAccessor worldAccessor, IItemService itemService)
/// <returns>True if the check was identified, false otherwise</returns>
public bool ExecuteCheck(Tracker tracker, AutoTrackerZeldaState currentState, AutoTrackerZeldaState prevState)
{
if (tracker.AutoTracker == null || tracker.AutoTracker.LatestViewAction?.IsValid == true) return false;
if (tracker.AutoTracker == null || tracker.AutoTracker.LatestViewAction?.IsValid == true || (_mireUpdated && _turtleRockUpdated)) return false;

_tracker = tracker;

var x = currentState.LinkX;
var y = currentState.LinkY;

if (currentState.OverworldScreen == 112 && x is >= 172 and <= 438 && y is >= 3200 and <= 3432)
if (!_mireUpdated && currentState.OverworldScreen == 112 && x is >= 172 and <= 438 && y is >= 3200 and <= 3432)
{
tracker.AutoTracker.LatestViewAction = new AutoTrackerViewedAction(MarkMiseryMireMedallion);
if (tracker.Options.AutoSaveLookAtEvents)
Expand All @@ -47,7 +49,7 @@ public bool ExecuteCheck(Tracker tracker, AutoTrackerZeldaState currentState, Au
}
return true;
}
else if (currentState.OverworldScreen == 71 && x is >= 3708 and <= 4016 && y is >= 128 and <= 368)
else if (!_turtleRockUpdated && currentState.OverworldScreen == 71 && x is >= 3708 and <= 4016 && y is >= 128 and <= 368)
{
tracker.AutoTracker.LatestViewAction = new AutoTrackerViewedAction(MarkTurtleRockMedallion);
if (tracker.Options.AutoSaveLookAtEvents)
Expand All @@ -62,16 +64,18 @@ public bool ExecuteCheck(Tracker tracker, AutoTrackerZeldaState currentState, Au

private void MarkMiseryMireMedallion()
{
if (_tracker == null) return;
if (_tracker == null || _mireUpdated) return;
var dungeon = _tracker.World.MiseryMire;
_tracker.SetDungeonRequirement(dungeon, dungeon.DungeonState.RequiredMedallion);
_mireUpdated = true;
}

private void MarkTurtleRockMedallion()
{
if (_tracker == null) return;
if (_tracker == null || _turtleRockUpdated) return;
var dungeon = _tracker.World.TurtleRock;
_tracker.SetDungeonRequirement(dungeon, dungeon.DungeonState.RequiredMedallion);
_turtleRockUpdated = true;
}

}
Loading

0 comments on commit 7936e18

Please sign in to comment.