Skip to content

Commit

Permalink
Implement GWToolbox launcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandru Macocian committed Apr 9, 2021
1 parent eb5552b commit 4af4577
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 30 deletions.
2 changes: 2 additions & 0 deletions Daybreak/Configuration/ApplicationConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public sealed class ApplicationConfiguration
{
[JsonProperty("GamePath")]
public string GamePath { get; set; }
[JsonProperty("ToolboxPath")]
public string ToolboxPath { get; set; }
[JsonProperty("CharacterName")]
public string CharacterName { get; set; }
[JsonProperty("LeftBrowserDefault")]
Expand Down
2 changes: 1 addition & 1 deletion Daybreak/Configuration/ProjectConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void RegisterViews(IViewProducer viewProducer)
{
viewProducer.ThrowIfNull(nameof(viewProducer));

viewProducer.RegisterView<StartupView>();
viewProducer.RegisterView<MainView>();
viewProducer.RegisterView<SettingsView>();
}
}
Expand Down
2 changes: 1 addition & 1 deletion Daybreak/Daybreak.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies>
<LangVersion>preview</LangVersion>
<ApplicationIcon>Daybreak.ico</ApplicationIcon>
<Version>0.1.2</Version>
<Version>0.1.3</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Daybreak/Launch/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public MainWindow(

private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.viewManager.ShowView<StartupView>();
this.viewManager.ShowView<MainView>();
this.SetupImageCycle();
}

Expand Down
27 changes: 24 additions & 3 deletions Daybreak/Services/ApplicationDetection/ApplicationDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ namespace Daybreak.Services.ApplicationDetection
{
public class ApplicationDetector : IApplicationDetector
{
private const string ToolboxProcessName = "GWToolbox";
private const string ProcessName = "gw";

private readonly IConfigurationManager configurationManager;
private readonly ICredentialManager credentialManager;

public bool IsGuildwarsRunning => GuildwarsProcessDetected();
public bool IsToolboxRunning => GuildwarsToolboxProcessDetected();

public ApplicationDetector(
IConfigurationManager configurationManager,
Expand Down Expand Up @@ -46,7 +48,7 @@ public void LaunchGuildwars()
{
if (Process.Start(executable, new List<string> { "-email", credentials.Username, "-password", credentials.Password, "-character", configuration.CharacterName }) is null)
{
throw new InvalidOperationException($"Unable to launch executable");
throw new InvalidOperationException($"Unable to launch {executable}");
}
},
onNone: () =>
Expand All @@ -55,10 +57,29 @@ public void LaunchGuildwars()
});
}

public void LaunchGuildwarsToolbox()
{
var configuration = this.configurationManager.GetConfiguration();
var executable = configuration.ToolboxPath;
if (File.Exists(executable) is false)
{
throw new InvalidOperationException($"Guildwars executable doesn't exist at {executable}");
}

if (Process.Start(executable) is null)
{
throw new InvalidOperationException($"Unable to launch {executable}");
}
}

private static bool GuildwarsProcessDetected()
{
var process = Process.GetProcessesByName(ProcessName).FirstOrDefault();
return process is not null;
return Process.GetProcessesByName(ProcessName).FirstOrDefault() is not null;
}

private static bool GuildwarsToolboxProcessDetected()
{
return Process.GetProcessesByName(ToolboxProcessName).FirstOrDefault() is not null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
public interface IApplicationDetector
{
bool IsGuildwarsRunning { get; }
bool IsToolboxRunning { get; }
void LaunchGuildwars();
void LaunchGuildwarsToolbox();
}
}
10 changes: 7 additions & 3 deletions Daybreak/Views/StartupView.xaml → Daybreak/Views/MainView.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<UserControl x:Class="Daybreak.Views.StartupView"
<UserControl x:Class="Daybreak.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Expand All @@ -25,9 +25,13 @@
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<controls:OpaqueButton Text="Launch game" Highlight="White" TransparentBackground="Black" HighlightOpacity="0.3" BackgroundOpacity="0.2"
Foreground="White" FontSize="36" Width="250" Height="60" VerticalAlignment="Bottom" Margin="0, 0, 0, 30"
Foreground="White" FontSize="36" Width="250" Height="60" VerticalAlignment="Bottom"
IsEnabled="{Binding ElementName=_this, Path=LaunchButtonEnabled, Mode=OneWay}"
Clicked="OpaqueButton_Clicked" Grid.Row="1" Grid.ColumnSpan="3"></controls:OpaqueButton>
Clicked="LaunchButton_Clicked" Grid.Row="0" Grid.ColumnSpan="3"></controls:OpaqueButton>
<controls:OpaqueButton Text="Launch toolbox" Highlight="White" TransparentBackground="Black" HighlightOpacity="0.3" BackgroundOpacity="0.2"
Foreground="White" FontSize="24" Width="200" Height="40" Margin="0, 10, 0, 10"
IsEnabled="{Binding ElementName=_this, Path=LaunchToolboxButtonEnabled, Mode=OneWay}"
Clicked="LaunchToolboxButton_Clicked" Grid.Row="1" Grid.ColumnSpan="3"></controls:OpaqueButton>
<Grid x:Name="RightContainer" Grid.Column="2" Margin="10">
<controls:ChromiumBrowserWrapper Address="{Binding ElementName=_this, Path=RightBrowserAddress, Mode=OneWay}"
Foreground="White" FavoriteUriChanged="RightBrowser_FavoriteUriChanged"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ namespace Daybreak.Views
/// <summary>
/// Interaction logic for StartupView.xaml
/// </summary>
public partial class StartupView : UserControl
public partial class MainView : UserControl
{
public static readonly DependencyProperty LaunchButtonEnabledProperty =
DependencyPropertyExtensions.Register<StartupView, bool>(nameof(LaunchButtonEnabled));
DependencyPropertyExtensions.Register<MainView, bool>(nameof(LaunchButtonEnabled));
public static readonly DependencyProperty LaunchToolboxButtonEnabledProperty =
DependencyPropertyExtensions.Register<MainView, bool>(nameof(LaunchToolboxButtonEnabled));
public static readonly DependencyProperty RightBrowserAddressProperty =
DependencyPropertyExtensions.Register<StartupView, string>(nameof(RightBrowserAddress));
DependencyPropertyExtensions.Register<MainView, string>(nameof(RightBrowserAddress));
public static readonly DependencyProperty LeftBrowserAddressProperty =
DependencyPropertyExtensions.Register<StartupView, string>(nameof(LeftBrowserAddress));
DependencyPropertyExtensions.Register<MainView, string>(nameof(LeftBrowserAddress));
public static readonly DependencyProperty RightBrowserFavoriteAddressProperty =
DependencyPropertyExtensions.Register<StartupView, string>(nameof(RightBrowserFavoriteAddress));
DependencyPropertyExtensions.Register<MainView, string>(nameof(RightBrowserFavoriteAddress));
public static readonly DependencyProperty LeftBrowserFavoriteAddressProperty =
DependencyPropertyExtensions.Register<StartupView, string>(nameof(LeftBrowserFavoriteAddress));
DependencyPropertyExtensions.Register<MainView, string>(nameof(LeftBrowserFavoriteAddress));

private readonly IApplicationDetector applicationDetector;
private readonly IViewManager viewManager;
Expand Down Expand Up @@ -60,8 +62,13 @@ public bool LaunchButtonEnabled
get => this.GetTypedValue<bool>(LaunchButtonEnabledProperty);
set => this.SetTypedValue(LaunchButtonEnabledProperty, value);
}
public bool LaunchToolboxButtonEnabled
{
get => this.GetTypedValue<bool>(LaunchToolboxButtonEnabledProperty);
set => this.SetTypedValue(LaunchToolboxButtonEnabledProperty, value);
}

public StartupView(
public MainView(
IApplicationDetector applicationDetector,
IViewManager viewManager,
IConfigurationManager configurationManager)
Expand Down Expand Up @@ -90,14 +97,8 @@ private void PeriodicallyCheckGameState()

private void CheckGameState()
{
if (applicationDetector.IsGuildwarsRunning)
{
this.LaunchButtonEnabled = false;
}
else
{
this.LaunchButtonEnabled = true;
}
this.LaunchButtonEnabled = applicationDetector.IsGuildwarsRunning is false;
this.LaunchToolboxButtonEnabled = applicationDetector.IsToolboxRunning is false;
}

private void StartupView_Loaded(object sender, RoutedEventArgs e)
Expand All @@ -109,7 +110,7 @@ private void StartupView_Unloaded(object sender, RoutedEventArgs e)
this.cancellationTokenSource.Cancel();
}

private void OpaqueButton_Clicked(object sender, EventArgs e)
private void LaunchButton_Clicked(object sender, EventArgs e)
{
try
{
Expand All @@ -121,6 +122,18 @@ private void OpaqueButton_Clicked(object sender, EventArgs e)
}
}

private void LaunchToolboxButton_Clicked(object sender, EventArgs e)
{
try
{
this.applicationDetector.LaunchGuildwarsToolbox();
}
catch
{
this.viewManager.ShowView<SettingsView>();
}
}

private void LeftBrowser_FavoriteUriChanged(object sender, string e)
{
var config = this.configurationManager.GetConfiguration();
Expand Down
11 changes: 9 additions & 2 deletions Daybreak/Views/SettingsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
Expand All @@ -39,9 +40,15 @@
x:Name="GamePathTextbox" FontSize="22" Margin="0, 0, 30, 0" Background="Transparent" Foreground="White"></TextBox>
<controls:FilePickerGlyph Grid.Row="4" Grid.Column="1" Height="30" Width="30" HorizontalAlignment="Right"
Foreground="White" BorderBrush="Gray" BorderThickness="1"
Clicked="FilePickerGlyph_Clicked"></controls:FilePickerGlyph>
<TextBlock Text="Address bar readonly: " FontSize="22" Foreground="White" Grid.Row="5"/>
Clicked="GameFilePickerGlyph_Clicked"></controls:FilePickerGlyph>
<TextBlock Text="Toolbox path: " FontSize="22" Foreground="White" Grid.Row="5"></TextBlock>
<TextBox Grid.Column="1" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Grid.Row="5"
x:Name="ToolboxPathTextbox" FontSize="22" Margin="0, 0, 30, 0" Background="Transparent" Foreground="White"></TextBox>
<controls:FilePickerGlyph Grid.Row="5" Grid.Column="1" Height="30" Width="30" HorizontalAlignment="Right"
Foreground="White" BorderBrush="Gray" BorderThickness="1"
Clicked="ToolboxFilePickerGlyph_Clicked"></controls:FilePickerGlyph>
<TextBlock Text="Address bar readonly: " FontSize="22" Foreground="White" Grid.Row="6"/>
<TextBox Grid.Column="1" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Grid.Row="6"
x:Name="AddressBarReadonlyTextbox" FontSize="22" Background="Transparent" Foreground="White"></TextBox>
</Grid>
</UserControl>
23 changes: 20 additions & 3 deletions Daybreak/Views/SettingsView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,26 @@ private void LoadSettings()
this.AddressBarReadonlyTextbox.Text = config.AddressBarReadonly.ToString();
this.CharacterTextbox.Text = config.CharacterName;
this.GamePathTextbox.Text = config.GamePath;
this.ToolboxPathTextbox.Text = config.ToolboxPath;
}

private void SaveButton_Clicked(object sender, System.EventArgs e)
{
var currentConfig = this.configurationManager.GetConfiguration();
currentConfig.CharacterName = this.CharacterTextbox.Text;
currentConfig.GamePath = this.GamePathTextbox.Text;
currentConfig.ToolboxPath = this.ToolboxPathTextbox.Text;
if (bool.TryParse(this.AddressBarReadonlyTextbox.Text, out var addressBarReadonly))
{
currentConfig.AddressBarReadonly = addressBarReadonly;
}

this.configurationManager.SaveConfiguration(currentConfig);
this.credentialManager.StoreCredentials(new LoginCredentials { Username = this.UsernameTextbox.Text, Password = this.PasswordBox.Password });
this.viewManager.ShowView<StartupView>();
this.viewManager.ShowView<MainView>();
}

private void FilePickerGlyph_Clicked(object sender, System.EventArgs e)
private void GameFilePickerGlyph_Clicked(object sender, System.EventArgs e)
{
var filePicker = new OpenFileDialog()
{
Expand All @@ -75,9 +77,24 @@ private void FilePickerGlyph_Clicked(object sender, System.EventArgs e)
}
}

private void ToolboxFilePickerGlyph_Clicked(object sender, System.EventArgs e)
{
var filePicker = new OpenFileDialog()
{
CheckFileExists = true,
CheckPathExists = true,
DefaultExt = "exe",
Multiselect = false
};
if (filePicker.ShowDialog() is true)
{
this.ToolboxPathTextbox.Text = filePicker.FileName;
}
}

private void BackButton_Clicked(object sender, System.EventArgs e)
{
this.viewManager.ShowView<StartupView>();
this.viewManager.ShowView<MainView>();
}
}
}

0 comments on commit 4af4577

Please sign in to comment.