Skip to content

Commit

Permalink
Added game selector for GB downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
thesupersonic16 committed Oct 23, 2024
1 parent 450a0c7 commit 84b7434
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 23 deletions.
6 changes: 3 additions & 3 deletions HedgeModManager/HedgeApp.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -574,10 +574,10 @@ public static void SelectGameInstall(GameInstall gameinstall)

public static void InstallGBHandlers()
{
foreach (var game in Games.GetSupportedGames())
{
foreach (var game in Games.GetSupportedGames()
.GroupBy(t => t.GBProtocol)
.Select(t => t.First()))
GBAPI.InstallGBHandler(game);
}
}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions HedgeModManager/Languages/en-AU.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@
<system:String x:Key="ModUpdatesTitle" >Mod Updates</system:String>
<system:String x:Key="ModDownloadsTitle" >Mod Downloads</system:String>

<!-- Game Selector Window Strings -->
<system:String x:Key="GameSelectorTitle" >Select Game...</system:String>
<system:String x:Key="GameSelectorHeader" >Multiple Games Detected</system:String>
<system:String x:Key="GameSelectorDescription">Please select a game below to install the mod into.</system:String>

<!-- Exception Window Strings -->
<system:String x:Key="ExceptionWindowTitle" >Hedge Mod Manager Crash</system:String>
<system:String x:Key="ExceptionWindowCrash" >Hedge Mod Manager Has Thrown an Exception!</system:String>
Expand Down
5 changes: 5 additions & 0 deletions HedgeModManager/Languages/en-US.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@
<system:String x:Key="ModUpdatesTitle" >Mod Updates</system:String>
<system:String x:Key="ModDownloadsTitle" >Mod Downloads</system:String>

<!-- Game Selector Window Strings -->
<system:String x:Key="GameSelectorTitle" >Select Game...</system:String>
<system:String x:Key="GameSelectorHeader" >Multiple Games Detected</system:String>
<system:String x:Key="GameSelectorDescription">Please select a game below to install the mod into.</system:String>

<!-- Exception Window Strings -->
<system:String x:Key="ExceptionWindowTitle" >Hedge Mod Manager Crash</system:String>
<system:String x:Key="ExceptionWindowCrash" >Hedge Mod Manager Has Thrown an Exception!</system:String>
Expand Down
6 changes: 4 additions & 2 deletions HedgeModManager/Linux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ public static bool GenerateDesktop()
if (baseExec != null)
{
GenerateDesktopAndRegister("hedgemm.desktop", "Hedge Mod Manager", $"{baseExec} \"%u\"", icon, true, "x-scheme-handler/hedgemm");

// GameBanana
foreach (Game game in Games.GetSupportedGames())
foreach (var game in Games.GetSupportedGames()
.GroupBy(t => t.GBProtocol)
.Select(t => t.First()))
GenerateDesktopAndRegister($"{game.GBProtocol}.desktop", $"Hedge Mod Manager ({game.GameName})", $"{baseExec} \"-gb %u\"", icon, false, $"x-scheme-handler/{game.GBProtocol}");
}

Expand Down
44 changes: 44 additions & 0 deletions HedgeModManager/UI/GBModGameSelectorWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<Window x:Class="HedgeModManager.UI.GBModGameSelectorWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:HedgeModManager.UI"
xmlns:controls="clr-namespace:HedgeModManager.Controls"
mc:Ignorable="d"
Title="{StaticResource GameSelectorTitle}" Height="240" Width="400"
MinHeight="240" MinWidth="400" ResizeMode="NoResize"
Style="{DynamicResource HedgeWindow}" WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<TextBlock x:Name="Header" Text="{StaticResource GameSelectorHeader}" TextAlignment="Center" FontSize="30" Foreground="{DynamicResource HMM.Window.HeaderAccentBrush}" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="20,0" TextWrapping="Wrap"/>
<Grid Width="Auto" Margin="5,0" Grid.Row="1">
<TextBlock HorizontalAlignment="Center" Margin="0,10" Text="{StaticResource GameSelectorDescription}" />
<ComboBox x:Name="GameComboBox" ItemsSource="{Binding Games}" SelectedItem="{Binding SelectedGame}" Width="300" Height="40" HorizontalAlignment="Center">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border Width="32" Height="32" CornerRadius="3" RenderOptions.BitmapScalingMode="Fant" x:Name="GameImageBorder">
<Border.Background>
<ImageBrush ImageSource="{Binding GameImage}"/>
</Border.Background>
</Border>
<Label Content="{Binding GameName}" VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>

<Grid HorizontalAlignment="Stretch" Grid.Row="2" Margin="-4,0,-4,-4" Background="{DynamicResource HMM.Window.DialogBottom}">
<StackPanel x:Name="Stack" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Stretch" Margin="0,0,5,0">
<Button Width="60" Height="23" Margin="5" Content="{StaticResource CommonUIOK}" Click="Ok_Click" IsDefault="True"/>
<Button Width="60" Height="23" Margin="5" Content="{StaticResource CommonUICancel}" IsCancel="True"/>
</StackPanel>
</Grid>
</Grid>
</Window>
57 changes: 57 additions & 0 deletions HedgeModManager/UI/GBModGameSelectorWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using GameBananaAPI;
using System.Net;
using System.Windows.Controls.Primitives;
using HedgeModManager.Controls;
using System.Net.Cache;
using System.Net.Http;
using PropertyTools.Wpf;
using static HedgeModManager.Lang;
using PopupBox = HedgeModManager.Controls.PopupBox;
using System.Diagnostics;
using System.Windows.Shell;
using HedgeModManager.Misc;
using HedgeModManager.Exceptions;
using System.Collections.ObjectModel;

namespace HedgeModManager.UI
{
/// <summary>
/// Interaction logic for GBModGameSelectorWindow.xaml
/// </summary>
public partial class GBModGameSelectorWindow : Window
{

public GBModGameSelectorWindowViewModel ViewModel = null;
public GameInstall SelectedGame = null;

public GBModGameSelectorWindow(List<GameInstall> compatibleGameInstalls)
{
DataContext = ViewModel = new GBModGameSelectorWindowViewModel
{
Games = new ObservableCollection<GameInstall>(compatibleGameInstalls),
SelectedGame = compatibleGameInstalls.FirstOrDefault()
};

InitializeComponent();
}

private void Ok_Click(object sender, RoutedEventArgs e)
{
SelectedGame = ViewModel.SelectedGame;
DialogResult = true;
}
}
}
24 changes: 24 additions & 0 deletions HedgeModManager/UI/GBModGameSelectorWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using GongSolutions.Wpf.DragDrop;
using HedgeModManager.CodeCompiler;
using HedgeModManager.Exceptions;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace HedgeModManager.UI
{
public class GBModGameSelectorWindowViewModel
{
public ObservableCollection<GameInstall> Games { get; set; } = new ObservableCollection<GameInstall>();
public GameInstall SelectedGame { get; set; }

public event PropertyChangedEventHandler PropertyChanged;

}
}
62 changes: 44 additions & 18 deletions HedgeModManager/UI/GBModWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ public GBModWindow(string itemType, int itemId, string dl, string protocol)
InitializeComponent();
}

public Tuple<GameInstall, bool?> SelectGameInstall()
{
// Get compaitble games
var compatibleGames = Games.GetSupportedGames().Where(t => t.GBProtocol == Protocol).ToList();
List<GameInstall> compatibleGameInstalls = HedgeApp.GameInstalls.Where(t => compatibleGames.Contains(t.Game)).ToList();

if (compatibleGameInstalls.Count <= 1)
return new(compatibleGameInstalls.FirstOrDefault(), true);

var selector = new GBModGameSelectorWindow(compatibleGameInstalls);
selector.ShowDialog();
return new (selector.SelectedGame, selector.DialogResult);
}

private void Screenshot_Click(object sender, RoutedEventArgs e)
{
var shot = (GBAPIScreenshotData)((Button)sender).Tag;
Expand Down Expand Up @@ -93,6 +107,34 @@ private async void Download_Click(object sender, RoutedEventArgs e)
DownloadButton.Visibility = Visibility.Collapsed;
Progress.Visibility = Visibility.Visible;

var (gameInstall, result) = SelectGameInstall();

if (gameInstall == null)
{
DownloadButton.Visibility = Visibility.Visible;
Progress.Visibility = Visibility.Collapsed;

// No game
if (result == true)
{
await Dispatcher.InvokeAsync(() =>
{
var dialog = new HedgeMessageBox(Localise("CommonUIError"), LocaliseFormat("ModDownloaderNoGameMes", Localise($"Game{Game.GameName}")));
dialog.AddButton(Localise("CommonUIClose"), () =>
{
dialog.Close();
DialogResult = false;
Close();
});
dialog.ShowDialog();
});
return;
}

// Cancelled
return;
}

try
{
var progress = new Progress<double?>((v) =>
Expand All @@ -111,23 +153,7 @@ private async void Download_Click(object sender, RoutedEventArgs e)
}
});

var game = HedgeApp.GetGameInstall(Game);
if (game == null)
{
await Dispatcher.InvokeAsync(() =>
{
var dialog = new HedgeMessageBox(Localise("CommonUIError"), LocaliseFormat("ModDownloaderNoGameMes", Localise($"Game{Game.GameName}")));
dialog.AddButton(Localise("CommonUIClose"), () =>
{
dialog.Close();
DialogResult = false;
Close();
});
dialog.ShowDialog();
});
return;
}
HedgeApp.Config = new CPKREDIRConfig(game);
HedgeApp.Config = new CPKREDIRConfig(gameInstall);
var mod = (GBAPIItemDataBasic)DataContext;

using (var resp = await Singleton.GetInstance<HttpClient>().GetAsync(DownloadURL, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false))
Expand All @@ -139,7 +165,7 @@ await Dispatcher.InvokeAsync(() =>
using (var destinationFile = File.Create(destinationPath, 8192, FileOptions.Asynchronous))
await resp.Content.CopyToAsync(destinationFile, progress);

ModsDB.InstallMod(destinationPath, Path.Combine(game.GameDirectory, Path.GetDirectoryName(HedgeApp.Config.ModsDbIni)));
ModsDB.InstallMod(destinationPath, Path.Combine(gameInstall.GameDirectory, Path.GetDirectoryName(HedgeApp.Config.ModsDbIni)));
File.Delete(destinationPath);

// a dialog would be nice here but i ain't adding strings
Expand Down

0 comments on commit 84b7434

Please sign in to comment.