Respect MVVM

This commit is contained in:
Isaac Marovitz 2024-01-15 20:48:42 -05:00
parent 793f830ffd
commit 09214d3faf
No known key found for this signature in database
GPG key ID: 97250B2B09A132E1
5 changed files with 101 additions and 103 deletions

View file

@ -120,7 +120,7 @@ namespace Ryujinx.Ava.UI.Controls
{
await CheatWindow.Show(
viewModel.VirtualFileSystem,
viewModel.SelectedApplication.TitleId,
ulong.Parse(viewModel.SelectedApplication.TitleId, NumberStyles.HexNumber),
viewModel.SelectedApplication.TitleName,
viewModel.SelectedApplication.Path);
}

View file

@ -0,0 +1,82 @@
using Avalonia.Collections;
using Ryujinx.Ava.UI.Models;
using Ryujinx.HLE.FileSystem;
using Ryujinx.HLE.HOS;
using Ryujinx.Ui.App.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Ryujinx.Ava.UI.ViewModels
{
public class CheatWindowViewModel : BaseModel
{
private readonly string _enabledCheatsPath;
public AvaloniaList<CheatNode> LoadedCheats { get; } = new();
public string BuildId { get; }
public CheatWindowViewModel(VirtualFileSystem virtualFileSystem, ulong titleId, string titlePath)
{
BuildId = ApplicationData.GetApplicationBuildId(virtualFileSystem, titlePath);
string modsBasePath = ModLoader.GetModsBasePath();
string titleModsPath = ModLoader.GetTitleDir(modsBasePath, titleId.ToString("x16"));
_enabledCheatsPath = Path.Combine(titleModsPath, "cheats", "enabled.txt");
string[] enabled = Array.Empty<string>();
if (File.Exists(_enabledCheatsPath))
{
enabled = File.ReadAllLines(_enabledCheatsPath);
}
var mods = new ModLoader.ModCache();
ModLoader.QueryContentsDir(mods, new DirectoryInfo(Path.Combine(modsBasePath, "contents")), titleId);
string currentCheatFile = string.Empty;
string buildId = string.Empty;
CheatNode currentGroup = null;
foreach (var cheat in mods.Cheats)
{
if (cheat.Path.FullName != currentCheatFile)
{
currentCheatFile = cheat.Path.FullName;
string parentPath = currentCheatFile.Replace(titleModsPath, "");
buildId = Path.GetFileNameWithoutExtension(currentCheatFile).ToUpper();
currentGroup = new CheatNode("", buildId, parentPath, true);
LoadedCheats.Add(currentGroup);
}
var model = new CheatNode(cheat.Name, buildId, "", false, enabled.Contains($"{buildId}-{cheat.Name}"));
currentGroup?.SubNodes.Add(model);
}
}
public void Save()
{
List<string> enabledCheats = new();
foreach (var cheats in LoadedCheats)
{
foreach (var cheat in cheats.SubNodes)
{
if (cheat.IsEnabled)
{
enabledCheats.Add(cheat.BuildIdKey);
}
}
}
Directory.CreateDirectory(Path.GetDirectoryName(_enabledCheatsPath));
File.WriteAllLines(_enabledCheatsPath, enabledCheats);
}
}
}

View file

@ -15,6 +15,7 @@ using Ryujinx.Ui.Common.Configuration;
using Ryujinx.Ui.Common.Helper;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
@ -172,7 +173,7 @@ namespace Ryujinx.Ava.UI.Views.Main
await CheatWindow.Show(
Window.VirtualFileSystem,
ViewModel.AppHost.Device.Processes.ActiveApplication.ProgramIdText,
ulong.Parse(ViewModel.AppHost.Device.Processes.ActiveApplication.ProgramIdText, NumberStyles.HexNumber),
name,
Window.ViewModel.SelectedApplication.Path);

View file

@ -5,10 +5,10 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:locale="clr-namespace:Ryujinx.Ava.Common.Locale"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:window="clr-namespace:Ryujinx.Ava.UI.Windows"
xmlns:viewModels="clr-namespace:Ryujinx.Ava.UI.ViewModels"
Width="500"
Height="500"
x:DataType="window:CheatWindow"
Height="380"
x:DataType="viewModels:CheatWindowViewModel"
mc:Ignorable="d"
Focusable="True">
<Grid Name="CheatGrid" Margin="15">
@ -88,15 +88,14 @@
Name="SaveButton"
MinWidth="90"
Margin="5"
Command="{Binding Save}"
IsVisible="{Binding !NoCheatsFound}">
Click="SaveAndClose">
<TextBlock Text="{locale:Locale SettingsButtonSave}" />
</Button>
<Button
Name="CancelButton"
MinWidth="90"
Margin="5"
Command="{Binding Close}">
Click="Close">
<TextBlock Text="{locale:Locale InputDialogCancel}" />
</Button>
</DockPanel>

View file

@ -1,29 +1,18 @@
using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Styling;
using FluentAvalonia.UI.Controls;
using Ryujinx.Ava.Common.Locale;
using Ryujinx.Ava.UI.Helpers;
using Ryujinx.Ava.UI.Models;
using Ryujinx.Ava.UI.ViewModels;
using Ryujinx.HLE.FileSystem;
using Ryujinx.HLE.HOS;
using Ryujinx.Ui.App.Common;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace Ryujinx.Ava.UI.Windows
{
public partial class CheatWindow : UserControl
{
private readonly string _enabledCheatsPath;
public bool NoCheatsFound { get; }
public AvaloniaList<CheatNode> LoadedCheats { get; }
public string BuildId { get; }
public CheatWindowViewModel ViewModel;
public CheatWindow()
{
@ -32,66 +21,14 @@ namespace Ryujinx.Ava.UI.Windows
InitializeComponent();
}
public CheatWindow(VirtualFileSystem virtualFileSystem, string titleId, string titleName, string titlePath)
public CheatWindow(VirtualFileSystem virtualFileSystem, ulong titleId, string titleName, string titlePath)
{
LoadedCheats = new AvaloniaList<CheatNode>();
BuildId = ApplicationData.GetApplicationBuildId(virtualFileSystem, titlePath);
DataContext = ViewModel = new CheatWindowViewModel(virtualFileSystem, titleId, titlePath);
InitializeComponent();
string modsBasePath = ModLoader.GetModsBasePath();
string titleModsPath = ModLoader.GetTitleDir(modsBasePath, titleId);
ulong titleIdValue = ulong.Parse(titleId, NumberStyles.HexNumber);
_enabledCheatsPath = Path.Combine(titleModsPath, "cheats", "enabled.txt");
string[] enabled = Array.Empty<string>();
if (File.Exists(_enabledCheatsPath))
{
enabled = File.ReadAllLines(_enabledCheatsPath);
}
int cheatAdded = 0;
var mods = new ModLoader.ModCache();
ModLoader.QueryContentsDir(mods, new DirectoryInfo(Path.Combine(modsBasePath, "contents")), titleIdValue);
string currentCheatFile = string.Empty;
string buildId = string.Empty;
CheatNode currentGroup = null;
foreach (var cheat in mods.Cheats)
{
if (cheat.Path.FullName != currentCheatFile)
{
currentCheatFile = cheat.Path.FullName;
string parentPath = currentCheatFile.Replace(titleModsPath, "");
buildId = Path.GetFileNameWithoutExtension(currentCheatFile).ToUpper();
currentGroup = new CheatNode("", buildId, parentPath, true);
LoadedCheats.Add(currentGroup);
}
var model = new CheatNode(cheat.Name, buildId, "", false, enabled.Contains($"{buildId}-{cheat.Name}"));
currentGroup?.SubNodes.Add(model);
cheatAdded++;
}
if (cheatAdded == 0)
{
NoCheatsFound = true;
}
DataContext = this;
}
public static async Task Show(VirtualFileSystem virtualFileSystem, string titleId, string titleName, string titlePath)
public static async Task Show(VirtualFileSystem virtualFileSystem, ulong titleId, string titleName, string titlePath)
{
ContentDialog contentDialog = new()
{
@ -99,7 +36,7 @@ namespace Ryujinx.Ava.UI.Windows
SecondaryButtonText = "",
CloseButtonText = "",
Content = new CheatWindow(virtualFileSystem, titleId, titleName, titlePath),
Title = string.Format(LocaleManager.Instance[LocaleKeys.CheatWindowHeading], titleName, titleId.ToUpper()),
Title = string.Format(LocaleManager.Instance[LocaleKeys.CheatWindowHeading], titleName, titleId.ToString("X16")),
};
Style bottomBorder = new(x => x.OfType<Grid>().Name("DialogSpace").Child().OfType<Border>());
@ -110,34 +47,13 @@ namespace Ryujinx.Ava.UI.Windows
await ContentDialogHelper.ShowAsync(contentDialog);
}
public void Save()
private void SaveAndClose(object sender, RoutedEventArgs e)
{
if (NoCheatsFound)
{
return;
}
List<string> enabledCheats = new();
foreach (var cheats in LoadedCheats)
{
foreach (var cheat in cheats.SubNodes)
{
if (cheat.IsEnabled)
{
enabledCheats.Add(cheat.BuildIdKey);
}
}
}
Directory.CreateDirectory(Path.GetDirectoryName(_enabledCheatsPath));
File.WriteAllLines(_enabledCheatsPath, enabledCheats);
ViewModel.Save();
((ContentDialog)Parent).Hide();
}
public void Close()
private void Close(object sender, RoutedEventArgs e)
{
((ContentDialog)Parent).Hide();
}