Skip to content

Commit

Permalink
Steam Launcher Utility v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
darklinkpower committed Apr 23, 2021
1 parent db0ae24 commit 96bcf5c
Show file tree
Hide file tree
Showing 11 changed files with 613 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Extensions/SteamLauncherUtility/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SteamLauncherUtility")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SteamLauncherUtility")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("31a65402-5b0c-44f0-9fc2-44b22ca4263c")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
304 changes: 304 additions & 0 deletions Extensions/SteamLauncherUtility/SteamLauncherUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
using Playnite.SDK;
using Playnite.SDK.Models;
using Playnite.SDK.Plugins;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using Microsoft.Win32;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace SteamLauncherUtility
{
public class SteamLauncherUtility : Plugin
{
private static readonly ILogger logger = LogManager.GetLogger();

private SteamLauncherUtilitySettings settings { get; set; }

public override Guid Id { get; } = Guid.Parse("31a65402-5b0c-44f0-9fc2-44b22ca4263c");

public SteamLauncherUtility(IPlayniteAPI api) : base(api)
{
settings = new SteamLauncherUtilitySettings(this);
}


public override void OnGameStarting(Game game)
{

if (game.IsInstalled == false)
{
return;
}

if (BuiltinExtensions.GetExtensionFromId(game.PluginId) != BuiltinExtension.SteamLibrary)
{
return;
}

string modeFeatureName = GetModeFeatureName();
if (game.Features != null)
{
var matchingFeature = game.Features.Where(f => f.Name == modeFeatureName);
if (settings.LaunchMode == 0 && matchingFeature.Count() > 0)
{
logger.Info(String.Format("Stopped execution in game \"{0}\". Global mode and game has \"{1}\" feature", game.Name, modeFeatureName));
return;
}
else if (settings.LaunchMode == 1 && matchingFeature.Count() > 0)
{
logger.Info(String.Format("Stopped execution in game \"{0}\". Selective mode and game has \"{1}\" feature", game.Name, modeFeatureName));
return;
}
}

LaunchSteam();
}

public override void OnGameStopped(Game game, long elapsedSeconds)
{
// Add code to be executed when game is preparing to be started.
}

public override ISettings GetSettings(bool firstRunSettings)
{
return settings;
}

public override UserControl GetSettingsView(bool firstRunSettings)
{
return new SteamLauncherUtilitySettingsView();
}

public override List<MainMenuItem> GetMainMenuItems(GetMainMenuItemsArgs menuArgs)
{
return new List<MainMenuItem>
{
new MainMenuItem
{
Description = "Launch Steam with configured actions",
MenuSection = "@Steam Launcher Utility",
Action = args => {
LaunchSteam();
}
},
new MainMenuItem
{
Description = "Add mode filter feature to selected games",
MenuSection = "@Steam Launcher Utility",
Action = args => {
AddModeFeature();
}
},
new MainMenuItem
{
Description = "Remove mode filter feature from selected games",
MenuSection = "@Steam Launcher Utility",
Action = args => {
RemoveModeFeature();
}
},
};
}

public bool GetIsSteamRunning()
{
Process[] processes = Process.GetProcessesByName("Steam");
if (processes.Length > 0)
{
return true;
}
else
{
return false;
}
}

public string GetModeFeatureName()
{
if (settings.LaunchMode == 0)
{
return "[SLU] Global Mode block";
}
else
{
return "[SLU] Selective Mode allow";
}
}

public string GetSteamInstallationPath()
{
using (var key = Registry.CurrentUser.OpenSubKey(@"Software\Valve\Steam"))
{

if (key?.GetValueNames().Contains("SteamExe") == true)
{
return key.GetValue("SteamExe")?.ToString().Replace('/', '\\') ?? "C:\\Program Files (x86)\\Steam\\steam.exe";
}

}
return "C:\\Program Files (x86)\\Steam\\steam.exe";
}

public string GetSteamLaunchArguments()
{
string arguments = "";
if (PlayniteApi.ApplicationInfo.Mode == ApplicationMode.Desktop)
{
if (settings.DisableSteamWebBrowserOnDesktopMode == true)
{
arguments = arguments + " -no-browser";
}
if (settings.LaunchSteamBpmOnDesktopMode == true)
{
arguments = arguments + " -bigpicture";
}
else
{
arguments = arguments + " -silent";
}
}
else if (PlayniteApi.ApplicationInfo.Mode == ApplicationMode.Fullscreen)
{
if (settings.DisableSteamWebBrowserOnFullscreenMode == true)
{
arguments = arguments + " -no-browser";
}
if (settings.LaunchSteamBpmOnFullscreenMode == true)
{
arguments = arguments + " -bigpicture";
}
else
{
arguments = arguments + " -silent";
}
}

return arguments;
}

public void LaunchSteam()
{
string steamInstallationPath = GetSteamInstallationPath();
if (!File.Exists(steamInstallationPath))
{
logger.Error(String.Format("Steam executable not detected in path \"{0}\"", steamInstallationPath));
return;
}

bool isSteamRunning = GetIsSteamRunning();
if (isSteamRunning == true && settings.CloseSteamIfRunning == true)
{
Process.Start(steamInstallationPath, "-shutdown");
logger.Info("Steam detected running. Closing via command line.");
for (int i = 0; i < 8; i++)
{
isSteamRunning = GetIsSteamRunning();
if (isSteamRunning == true)
{
logger.Info("Steam detected running.");
Thread.Sleep(2000);
}
else
{
logger.Info("Steam has closed.");
break;
}
}
}

if (isSteamRunning == false)
{
string launchArguments = GetSteamLaunchArguments();
Process.Start(steamInstallationPath, launchArguments);
logger.Info(String.Format("Steam launched with arguments: \"{0}\"", launchArguments));
}
else
{
logger.Warn("Steam was detected as running and was not launched via the extension.");
}
}

public bool AddFeature(Game game, GameFeature feature)
{
if (game.FeatureIds == null)
{
game.FeatureIds = new List<Guid> { feature.Id };
PlayniteApi.Database.Games.Update(game);
return true;
}
else if (game.FeatureIds.Contains(feature.Id) == false)
{
game.FeatureIds.AddMissing(feature.Id);
PlayniteApi.Database.Games.Update(game);
return true;
}
else
{
return false;
}
}

public void AddModeFeature()
{
string featureName = GetModeFeatureName();
GameFeature feature = PlayniteApi.Database.Features.Add(featureName);
var gameDatabase = PlayniteApi.MainView.SelectedGames.Where(g => g.PluginId == BuiltinExtensions.GetIdFromExtension(BuiltinExtension.SteamLibrary));
int featureAddedCount = 0;
foreach (var game in gameDatabase)
{
bool featureAdded = AddFeature(game, feature);
if (featureAdded == true)
{
featureAddedCount++;
logger.Info(String.Format("Added \"{0}\" feature to \"{1}\"", featureName, game.Name));
}
}
PlayniteApi.Dialogs.ShowMessage(String.Format("Added \"{0}\" feature to {1} game(s).", featureName, featureAddedCount), "Steam Launcher Utility");
}

public bool RemoveFeature(Game game, GameFeature feature)
{
if (game.FeatureIds != null)
{
if (game.FeatureIds.Contains(feature.Id))
{
game.FeatureIds.Remove(feature.Id);
PlayniteApi.Database.Games.Update(game);
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}

public void RemoveModeFeature()
{
string featureName = GetModeFeatureName();
GameFeature feature = PlayniteApi.Database.Features.Add(featureName);
var gameDatabase = PlayniteApi.MainView.SelectedGames.Where(g => g.PluginId == BuiltinExtensions.GetIdFromExtension(BuiltinExtension.SteamLibrary));
int featureRemovedCount = 0;
foreach (var game in gameDatabase)
{
bool featureRemoved = RemoveFeature(game, feature);
if (featureRemoved == true)
{
featureRemovedCount++;
logger.Info(String.Format("Removed \"{0}\" feature from \"{1}\"", featureName, game.Name));
}
}
PlayniteApi.Dialogs.ShowMessage(String.Format("Removed \"{0}\" feature from {1} game(s).", featureName, featureRemovedCount), "Steam Launcher Utility");
}
}
}
Loading

0 comments on commit 96bcf5c

Please sign in to comment.