Skip to content

Commit

Permalink
Minor updates
Browse files Browse the repository at this point in the history
OnGameStopped() now operates asynchronously to prevent slow return to Playnite

Game icons are now passed as byte[] data instead of filenames to prevent accidental deletion from file system
  • Loading branch information
ASchoe311 committed Oct 10, 2024
1 parent 2c17fd4 commit bc79864
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
46 changes: 43 additions & 3 deletions RiotGamesLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
using System.Windows;
using System.Windows.Controls;
using System.Management;
using System.Drawing;
using System.Drawing.Imaging;

namespace RiotGamesLibrary
{
Expand Down Expand Up @@ -70,6 +72,14 @@ public override IEnumerable<GameMetadata> GetGames(LibraryGetGamesArgs args)
{
foreach (var game in rgGames.Keys)
{
// pass icon as bytes so it doesn't get deleted on game removal
byte[] iconData = null;
using (var ms = new MemoryStream())
{
Bitmap bmp = new Icon(RiotGame.Icons[game]).ToBitmap();
bmp.Save(ms, ImageFormat.Png);
iconData = ms.ToArray();
}
yield return new GameMetadata()
{
Name = rgGames[game].Item1,
Expand All @@ -78,7 +88,7 @@ public override IEnumerable<GameMetadata> GetGames(LibraryGetGamesArgs args)
Platforms = new HashSet<MetadataProperty> { new MetadataNameProperty("PC (Windows)") },
IsInstalled = RiotGame.IsInstalled(game),
InstallDirectory = RiotGame.InstallPath(game),
Icon = new MetadataFile(RiotGame.Icons[game])
Icon = new MetadataFile($"{game}_icon.png", iconData)
};
}
}
Expand Down Expand Up @@ -157,6 +167,11 @@ public override void OnApplicationStarted(OnApplicationStartedEventArgs args)
}
}

/// <summary>
/// Creates a game action for the given companion app
/// </summary>
/// <param name="comp">A <c>CompanionApp</c> <see cref="CompanionApp"/></param>
/// <returns>A new game action to launch the companion app</returns>
private GameAction GenAction (CompanionApp comp)
{
return new GameAction()
Expand All @@ -171,6 +186,9 @@ private GameAction GenAction (CompanionApp comp)
};
}

/// <summary>
/// Creates or removes game actions for each companion app based on settings
/// </summary>
public void UpdateCompanionActions()
{
PlayniteApi.Database.Games.BeginBufferUpdate();
Expand Down Expand Up @@ -238,7 +256,11 @@ public void UpdateCompanionActions()
}
PlayniteApi.Database.Games.EndBufferUpdate();
}


/// <summary>
/// Launch relevant companion apps on game start
/// </summary>
/// <param name="args"><see cref="OnGameStartedEventArgs"/></param>
public override void OnGameStarted(OnGameStartedEventArgs args)
{
//logger.Debug($"launching game with id {args.Game.Id}");
Expand Down Expand Up @@ -266,6 +288,24 @@ public override void OnGameStopped(OnGameStoppedEventArgs args)
{
return;
}
OnGameStoppedAsync(args);
}

/// <summary>
/// Asynchronously close companion apps and client
/// </summary>
/// <param name="args"><c>OnGameStoppedEventArgs</c></param>
private async void OnGameStoppedAsync(OnGameStoppedEventArgs args)
{
await Task.Run(() => OGS(args));
}

/// <summary>
/// Close companion apps and client
/// </summary>
/// <param name="args"><c>OnGameStoppedEventArgs</c></param>
private void OGS(OnGameStoppedEventArgs args)
{
if (args.Game.GameId != "rg-legendsofruneterra")
{
ObservableCollection<CompanionApp> companionsList = (args.Game.GameId == "rg-leagueoflegends") ? settings.Settings.LeagueCompanions : settings.Settings.ValorantCompanions;
Expand All @@ -275,7 +315,7 @@ public override void OnGameStopped(OnGameStoppedEventArgs args)
if (comp.CloseWithGame)
{
logger.Info($"Trying to stop {gameName} companion app: {comp.ExeName}");
var wmiQueryString = "SELECT ProcessId, ExecutablePath, CommandLine FROM Win32_Process";
var wmiQueryString = "SELECT ProcessId, ExecutablePath, CommandLine FROM Win32_Process";
using (var searcher = new ManagementObjectSearcher(wmiQueryString))
using (var results = searcher.Get())
{
Expand Down
1 change: 1 addition & 0 deletions RiotGamesLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml.Linq" />
Expand Down

0 comments on commit bc79864

Please sign in to comment.