Skip to content

Commit

Permalink
Merge pull request #6 from Drommedhar/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Drommedhar authored Sep 26, 2024
2 parents 552cd64 + 89651d1 commit 37f80b0
Show file tree
Hide file tree
Showing 17 changed files with 388 additions and 69 deletions.
29 changes: 29 additions & 0 deletions DlssUpdater/Controls/LauncherPanel.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<UserControl x:Class="DlssUpdater.Controls.LauncherPanel"
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"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
xmlns:local="clr-namespace:DlssUpdater.Controls"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" Margin="0,0,0,5" x:Name="LibPanel">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="#19000000">
<ui:ToggleSwitch Content="{Binding LibraryConfig.LibraryName, ElementName=LibPanel}" HorizontalAlignment="Left" Margin="5,5,0,5" IsChecked="{Binding LibraryConfig.IsChecked, ElementName=LibPanel}" Click="ToggleSwitch_Click"/>
</Grid>
<Grid x:Name="GridExpand" Grid.Row="1" Background="#0C000000">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ui:TextBox Grid.Column="0" IsReadOnly="True" Text="{Binding LibraryConfig.InstallPath, ElementName=LibPanel}" Margin="5,0,0,0"/>
<ui:Button Grid.Column="1" Content="..." Margin="10,0,5,0" Click="Button_Click_1"/>
<ui:Button Grid.Column="2" Content="Auto Scan" Margin="5,0,5,0" Click="Button_Click"/>
</Grid>
</Grid>
</UserControl>
80 changes: 80 additions & 0 deletions DlssUpdater/Controls/LauncherPanel.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using DlssUpdater.Defines;
using DlssUpdater.Singletons;
using DLSSUpdater.Defines;
using Microsoft.Win32;
using System;
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 System.Windows.Navigation;
using System.Windows.Shapes;

namespace DlssUpdater.Controls
{
/// <summary>
/// Interaction logic for LauncherPanel.xaml
/// </summary>
public partial class LauncherPanel : UserControl
{
public static readonly DependencyProperty LibraryConfigProperty =
DependencyProperty.Register("LibraryConfig", typeof(LibraryConfig), typeof(LauncherPanel));

public LibraryConfig LibraryConfig
{
get => (LibraryConfig)GetValue(LibraryConfigProperty);
set => SetValue(LibraryConfigProperty, value);
}

private readonly Settings _settings;
private readonly GameContainer _gameContainer;
private readonly NLog.Logger _logger;

public LauncherPanel()
{
InitializeComponent();

_settings = App.GetService<Settings>()!;
_gameContainer = App.GetService<GameContainer>()!;
_logger = App.GetService<NLog.Logger>()!;

GridExpand.Visibility = Visibility.Visible;
}

private async void ToggleSwitch_Click(object sender, RoutedEventArgs e)
{
_settings.Save();
_logger.Debug($"Switched library '{LibraryConfig.LibraryName}' to {LibraryConfig.IsChecked}");
_gameContainer.UpdateLibraries();
await _gameContainer.ReloadLibraryGames(LibraryConfig.LibraryType);
}

private void Button_Click(object sender, RoutedEventArgs e)
{
var libRef = _gameContainer.Libraries.FirstOrDefault(l => l.GetLibraryType() == LibraryConfig.LibraryType);
libRef?.GetInstallationDirectory();
}

private async void Button_Click_1(object sender, RoutedEventArgs e)
{
OpenFolderDialog dlg = new()
{
Multiselect = false
};
if (dlg.ShowDialog() == true)
{
LibraryConfig.InstallPath = dlg.FolderName;
_settings.Save();
_gameContainer.UpdateLibraries();
await _gameContainer.LoadGamesAsync();
}
}
}
}
11 changes: 9 additions & 2 deletions DlssUpdater/Defines/GameInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public partial class GameInfo : ObservableObject

[JsonIgnore] public LibraryType LibraryType;
[JsonIgnore] public readonly DllUpdater _updater;
[JsonIgnore] public readonly NLog.Logger _logger;

public GameInfo(string gameName, string gamePath, LibraryType type)
{
Expand All @@ -45,6 +46,7 @@ public GameInfo(string gameName, string gamePath, LibraryType type)
Self = this;
HasAntiCheat = App.GetService<AntiCheatChecker>()!.Check(gamePath);
_updater = App.GetService<DllUpdater>()!;
_logger = App.GetService<NLog.Logger>()!;
GatherInstalledVersions().ConfigureAwait(true);
}

Expand All @@ -62,10 +64,15 @@ await Task.Run(() =>
foreach (var (dll, info) in InstalledDlls)
{
var allFiles = Directory.GetFiles(GamePath, GetDllName(dll), SearchOption.AllDirectories);
if (allFiles is null || allFiles.Length != 1) continue;
_logger.Debug($"Found '{allFiles?.Length.ToString() ?? "0"} files' for {GetDllName(dll)} in {GameName}");
if (allFiles is null || allFiles.Length == 0)
{
continue;
}
// TODO: Support for multiple instances of the same dll?

// We only should have one entry
info.Path = allFiles[0];
info.Path = allFiles[^1];
var fileInfo = FileVersionInfo.GetVersionInfo(info.Path);
var newVersion = fileInfo.FileVersion?.Replace(',', '.');
if (newVersion is not null && newVersion != info.Version)
Expand Down
92 changes: 92 additions & 0 deletions DlssUpdater/Defines/LibraryConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using DlssUpdater.Defines;
using DlssUpdater.GameLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Threading.Tasks;
using DLSSUpdater.Defines;

namespace DLSSUpdater.Defines
{
public partial class LibraryConfig : ObservableObject
{
public LibraryType LibraryType { get; set; }

[ObservableProperty]
private bool _isChecked;

[ObservableProperty]
private string _libraryName;

[ObservableProperty]
private string _installPath;

[ObservableProperty][JsonIgnore] public LibraryConfig _self;

public LibraryConfig(LibraryType type, string name)
{
LibraryType = type;
_libraryName = name;
_installPath = string.Empty;
_isChecked = true;
Self = this;
}
}
}

public class LibraryConvert : JsonConverter<LibraryConfig>
{
public override LibraryConfig? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
bool isChecked = false;
string libraryName = "";
string installPath = "";
LibraryType libraryType = LibraryType.Manual;
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
{
var config = new LibraryConfig(libraryType, libraryName)
{
IsChecked = isChecked,
InstallPath = installPath
};
return config;
}
// TODO: More
var propName = reader.GetString();
reader.Read();
if (propName == "IsChecked")
{
isChecked = reader.GetBoolean();
}
if(propName == "LibraryName")
{
libraryName = reader.GetString()!;
}
if(propName == "LibraryType")
{
libraryType = (LibraryType)Enum.Parse(typeof(LibraryType), reader.GetString()!);
}
if(propName == "InstallPath")
{
installPath = reader.GetString()!;
}
}

return null;
}

public override void Write(Utf8JsonWriter writer, LibraryConfig value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteBoolean(nameof(LibraryConfig.IsChecked), value.IsChecked);
writer.WriteString(nameof(LibraryConfig.LibraryName), value.LibraryName);
writer.WriteString(nameof(LibraryConfig.LibraryType), value.LibraryType.ToString());
writer.WriteString(nameof(LibraryConfig.InstallPath), value.InstallPath);
writer.WriteEndObject();
}
}
22 changes: 18 additions & 4 deletions DlssUpdater/GameLibrary/ILibrary.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel;
using DlssUpdater.GameLibrary.Steam;
using DLSSUpdater.Defines;
using GameInfo = DlssUpdater.Defines.GameInfo;

namespace DlssUpdater.GameLibrary;
Expand All @@ -15,15 +16,28 @@ public enum LibraryType

public interface ILibrary
{
public LibraryType GetLibraryType();
public void GetInstallationDirectory();
public Task<List<GameInfo>> GatherGamesAsync();

static ILibrary Create(LibraryType type, NLog.Logger logger)
static ILibrary Create(LibraryConfig config, NLog.Logger logger)
{
return config.LibraryType switch
{
LibraryType.Manual => new ManualLibrary(config, logger),
LibraryType.Steam => new SteamLibrary(config, logger),
LibraryType.Ubisoft => new UbisoftConnectLibrary(config, logger),
_ => throw new InvalidEnumArgumentException(nameof(config.LibraryType), (int)config.LibraryType, typeof(LibraryType))
};
}

static string GetName(LibraryType type)
{
return type switch
{
LibraryType.Manual => new ManualLibrary(logger),
LibraryType.Steam => new SteamLibrary(logger),
LibraryType.Ubisoft => new UbisoftConnectLibrary(logger),
LibraryType.Manual => "Manual",
LibraryType.Steam => "Steam",
LibraryType.Ubisoft => "Ubisoft Connect",
_ => throw new InvalidEnumArgumentException(nameof(type), (int)type, typeof(LibraryType))
};
}
Expand Down
15 changes: 13 additions & 2 deletions DlssUpdater/GameLibrary/ManualLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
using GameInfo = DlssUpdater.Defines.GameInfo;
using DLSSUpdater.Defines;
using GameInfo = DlssUpdater.Defines.GameInfo;

namespace DlssUpdater.GameLibrary;

public class ManualLibrary : ILibrary
{
public ManualLibrary(NLog.Logger logger)
public ManualLibrary(LibraryConfig config, NLog.Logger logger)
{

}

public LibraryType GetLibraryType()
{
return LibraryType.Manual;
}

public void GetInstallationDirectory()
{

}

public async Task<List<GameInfo>> GatherGamesAsync()
{
List<GameInfo> ret = [];
Expand Down
28 changes: 19 additions & 9 deletions DlssUpdater/GameLibrary/Steam/SteamLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
using System.IO;
using DlssUpdater.Helpers;
using DLSSUpdater.Defines;
using Microsoft.Win32;
using GameInfo = DlssUpdater.Defines.GameInfo;

namespace DlssUpdater.GameLibrary.Steam;

public class SteamLibrary : ILibrary
{
private string? _installPath;
private readonly NLog.Logger _logger;
private readonly LibraryConfig _config;

internal SteamLibrary(NLog.Logger logger)
internal SteamLibrary(LibraryConfig config, NLog.Logger logger)
{
_config = config;
_logger = logger;

getInstallationDirectory();
if(string.IsNullOrEmpty(config.InstallPath))
{
GetInstallationDirectory();
}
}

public LibraryType GetLibraryType()
{
return _config.LibraryType;
}

public async Task<List<GameInfo>> GatherGamesAsync()
{
if (_installPath is null) return [];
if (string.IsNullOrEmpty(_config.InstallPath)) return [];

// We now know steam is installed, so we can carry on by parsing the 'libraryfolder.vdf'
var folders = await getLibraryFolders();
Expand All @@ -29,7 +39,7 @@ public async Task<List<GameInfo>> GatherGamesAsync()
private async Task<List<LibraryFolder>> getLibraryFolders()
{
List<LibraryFolder> ret = [];
var vdfPath = Path.Combine(_installPath!, "steamapps", "libraryfolders.vdf");
var vdfPath = Path.Combine(_config.InstallPath!, "steamapps", "libraryfolders.vdf");
if (!File.Exists(vdfPath)) return ret;

VdfParser parser = new(vdfPath);
Expand All @@ -56,7 +66,7 @@ private async Task<List<LibraryFolder>> getLibraryFolders()
return ret;
}

private void getInstallationDirectory()
public void GetInstallationDirectory()
{
// We are getting the steam installation path from the user registry (if it exists)
using var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
Expand All @@ -65,7 +75,7 @@ private void getInstallationDirectory()

_logger.Debug($"Steam install directory: {installPath ?? "N/A"}");

if (!string.IsNullOrEmpty(installPath)) _installPath = installPath;
if (!string.IsNullOrEmpty(installPath)) _config.InstallPath = installPath;
}

private async Task<List<GameInfo>> getGames(List<LibraryFolder> folder)
Expand Down Expand Up @@ -124,14 +134,14 @@ private async Task<List<GameInfo>> getGames(List<LibraryFolder> folder)
await info.GatherInstalledVersions();
if (info.HasInstalledDlls()) return info;

_logger.Debug($"Steam: '{info.GameName}' does not have anny DLSS dll and is ignored.");
_logger.Debug($"Steam: '{info.GameName}' does not have any DLSS dll and is being ignored.");

return null;
}

private string? getGameImage(string appId)
{
var cachedImage = Path.Combine(_installPath!, "appcache", "librarycache", $"{appId}_library_600x900.jpg");
var cachedImage = Path.Combine(_config.InstallPath!, "appcache", "librarycache", $"{appId}_library_600x900.jpg");
if (File.Exists(cachedImage)) return new string(cachedImage);

var url = $"https://steamcdn-a.akamaihd.net/steam/apps/{appId}/library_600x900_2x.jpg";
Expand Down
Loading

0 comments on commit 37f80b0

Please sign in to comment.