-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UI: Added Firmwares tab in emulation settings to view/manage firmware…
… files
- Loading branch information
Showing
15 changed files
with
516 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<UserControl | ||
xmlns="https://github.com/avaloniaui" | ||
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:c="using:Mesen.Controls" | ||
xmlns:l="using:Mesen.Localization" | ||
mc:Ignorable="d" d:DesignWidth="100" d:DesignHeight="50" | ||
x:Name="root" | ||
x:Class="Mesen.Controls.FirmwareSelect" | ||
> | ||
<Grid DataContext="{Binding ElementName=root}" ColumnDefinitions="*,Auto,Auto,Auto" RowDefinitions="Auto"> | ||
<TextBox Text="{Binding Filename}" Grid.Column="0" IsReadOnly="True" /> | ||
<Image | ||
Grid.Column="1" | ||
Stretch="None" | ||
Source="/Assets/Warning.png" | ||
ToolTip.Tip="{Binding WarningMessage}" | ||
ToolTip.ShowDelay="0" | ||
IsVisible="{Binding WarningMessage.Length}" | ||
Margin="3 0" | ||
/> | ||
<Button Content="{l:Translate btnSelect}" Click="btnBrowse_OnClick" Grid.Column="2" /> | ||
<c:IconButton | ||
Grid.Column="3" | ||
ToolTip.Tip="{l:Translate btnDelete}" | ||
Icon="Assets/Close.png" | ||
Command="{Binding DeleteFirmware}" | ||
Height="21" | ||
Width="21" | ||
/> | ||
</Grid> | ||
</UserControl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
using Avalonia; | ||
using Avalonia.Controls; | ||
using Avalonia.Interactivity; | ||
using Avalonia.Markup.Xaml; | ||
using Avalonia.Metadata; | ||
using Mesen.Config; | ||
using Mesen.Interop; | ||
using Mesen.Localization; | ||
using Mesen.Utilities; | ||
using Mesen.Windows; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace Mesen.Controls; | ||
|
||
public class FirmwareSelect : UserControl | ||
{ | ||
public static readonly StyledProperty<FirmwareType> FirmwareTypeProperty = AvaloniaProperty.Register<FirmwareSelect, FirmwareType>(nameof(FirmwareType)); | ||
public static readonly StyledProperty<string> FilenameProperty = AvaloniaProperty.Register<FirmwareSelect, string>(nameof(Filename), ""); | ||
public static readonly StyledProperty<string> WarningMessageProperty = AvaloniaProperty.Register<FirmwareSelect, string>(nameof(WarningMessage), ""); | ||
|
||
public FirmwareType FirmwareType | ||
{ | ||
get { return GetValue(FirmwareTypeProperty); } | ||
set { SetValue(FirmwareTypeProperty, value); } | ||
} | ||
|
||
public string Filename | ||
{ | ||
get { return GetValue(FilenameProperty); } | ||
set { SetValue(FilenameProperty, value); } | ||
} | ||
|
||
public string WarningMessage | ||
{ | ||
get { return GetValue(WarningMessageProperty); } | ||
set { SetValue(WarningMessageProperty, value); } | ||
} | ||
|
||
public FirmwareSelect() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
private void InitializeComponent() | ||
{ | ||
AvaloniaXamlLoader.Load(this); | ||
} | ||
|
||
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) | ||
{ | ||
ValidateFirmware(); | ||
base.OnAttachedToVisualTree(e); | ||
} | ||
|
||
private void ValidateFirmware() | ||
{ | ||
bool incorrectSize = false; | ||
long fileSize = 0; | ||
|
||
Filename = ""; | ||
WarningMessage = ""; | ||
|
||
FirmwareFiles firmwareFiles = FirmwareType.GetFirmwareInfo(); | ||
string? path = firmwareFiles.Names.Select(filename => Path.Combine(ConfigManager.FirmwareFolder, filename)).Where(File.Exists).FirstOrDefault(); | ||
if(path != null) { | ||
fileSize = new FileInfo(path).Length; | ||
Filename = Path.GetFileName(path); | ||
|
||
foreach(FirmwareFileInfo firmwareInfo in firmwareFiles) { | ||
if(fileSize == firmwareInfo.Size) { | ||
string hash = FirmwareHelper.GetFileHash(path); | ||
if(Array.IndexOf(firmwareInfo.Hashes, hash) < 0) { | ||
WarningMessage = ResourceHelper.GetMessage("UnknownFirmwareHash", firmwareInfo.Hashes[0], hash); | ||
} | ||
incorrectSize = false; | ||
break; | ||
} else { | ||
incorrectSize = true; | ||
} | ||
} | ||
} | ||
|
||
if(incorrectSize) { | ||
WarningMessage = ResourceHelper.GetMessage("InvalidFirmwareSize", FirmwareType.GetFirmwareInfo()[0].Size, fileSize); | ||
} | ||
} | ||
|
||
private async void btnBrowse_OnClick(object sender, RoutedEventArgs e) | ||
{ | ||
while(true) { | ||
string? selectedFile = await FileDialogHelper.OpenFile(null, VisualRoot, FileDialogHelper.FirmwareExt); | ||
if(selectedFile?.Length > 0 && File.Exists(selectedFile)) { | ||
if(await FirmwareHelper.SelectFirmwareFile(FirmwareType, selectedFile, VisualRoot)) { | ||
break; | ||
} | ||
} else { | ||
break; | ||
} | ||
} | ||
ValidateFirmware(); | ||
} | ||
|
||
public async void DeleteFirmware(object parameter) | ||
{ | ||
string path = Path.Combine(ConfigManager.FirmwareFolder, Filename); | ||
if(File.Exists(path)) { | ||
DialogResult result = await MesenMsgBox.Show(VisualRoot, "PromptDeleteFirmware", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, path); | ||
if(result == DialogResult.OK) { | ||
File.Delete(path); | ||
} | ||
} | ||
ValidateFirmware(); | ||
} | ||
|
||
[DependsOn(nameof(Filename))] | ||
public bool CanDeleteFirmware(object parameter) | ||
{ | ||
return Filename != null && Filename.Length > 0 && File.Exists(Path.Combine(ConfigManager.FirmwareFolder, Filename)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.