Skip to content

Commit

Permalink
Added support for steam shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
slxdy committed Apr 3, 2024
1 parent 2b8ec61 commit 5fe910d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 23 deletions.
22 changes: 5 additions & 17 deletions TweaksLauncher.API/Launcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,16 @@ public static int Main(string[] args)
if (gamePath.Equals("createmod", StringComparison.OrdinalIgnoreCase))
return DevTools.CreateMod() ? 0 : -1;

if (File.Exists(gamePath))
if (InitContext(gamePath))
{
gamePath = Path.GetDirectoryName(gamePath);
}
var gameArgs = new string[args.Length - 1];
Array.Copy(args, 1, gameArgs, 0, gameArgs.Length);

if (Directory.Exists(gamePath))
{
if (InitContext(gamePath))
{
var gameArgs = new string[args.Length - 1];
Array.Copy(args, 1, gameArgs, 0, gameArgs.Length);

return StartGame(gameArgs);
}
else
{
logger.Log($"No valid Unity game found at: '{gamePath}'", Color.Red);
}
return StartGame(gameArgs);
}
else
{
logger.Log($"Could not find the game directory at: '{gamePath}'", Color.Red);
logger.Log($"No valid Unity game found at: '{gamePath}'", Color.Red);
}
}

Expand Down
19 changes: 14 additions & 5 deletions TweaksLauncher.API/LauncherContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using TweaksLauncher.Utility;

namespace TweaksLauncher;

Expand All @@ -19,11 +20,19 @@ internal class LauncherContext
{
if (File.Exists(gameDirectory))
{
var dir = Path.GetDirectoryName(gameDirectory);
if (dir == null)
return null;

gameDirectory = dir;
var steamPath = SteamTools.GetPathFromShortcut(gameDirectory);
if (steamPath != null)
{
gameDirectory = steamPath;
}
else
{
var dir = Path.GetDirectoryName(gameDirectory);
if (dir == null)
return null;

gameDirectory = dir;
}
}
else if (!Directory.Exists(gameDirectory))
return null;
Expand Down
1 change: 1 addition & 0 deletions TweaksLauncher.API/TweaksLauncher.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="GameFinder.StoreHandlers.Steam" Version="4.2.0" PrivateAssets="all" />
<PackageReference Include="Samboy063.Cpp2IL.Core" Version="2022.1.0-pre-release.14" ExcludeAssets="native" PrivateAssets="all" />
<PackageReference Include="Il2CppInterop.Generator" Version="1.4.6-ci.394" PrivateAssets="all" />
<PackageReference Include="Il2CppInterop.HarmonySupport" Version="1.4.6-ci.394" PrivateAssets="all" />
Expand Down
46 changes: 46 additions & 0 deletions TweaksLauncher.API/Utility/SteamTools.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using GameFinder.RegistryUtils;
using GameFinder.StoreHandlers.Steam;
using GameFinder.StoreHandlers.Steam.Models.ValueTypes;
using NexusMods.Paths;

namespace TweaksLauncher.Utility;

internal static class SteamTools
{
private static readonly SteamHandler handler = new(FileSystem.Shared, WindowsRegistry.Shared);

public static string? GetPathFromShortcut(string shortcutPath)
{
if (!shortcutPath.EndsWith(".url", StringComparison.OrdinalIgnoreCase))
return null;

string contents;
try
{
contents = File.ReadAllText(shortcutPath);
}
catch
{
return null;
}

var pattern = "URL=steam://rungameid/";
var startIdx = contents.IndexOf(pattern);
if (startIdx == -1)
return null;

startIdx += pattern.Length;

var endIdx = contents.IndexOf('\r', startIdx);
if (endIdx == -1)
return null;

var length = endIdx - startIdx;

if (!uint.TryParse(contents.AsSpan(startIdx, length), out var appId))
return null;

var game = handler.FindOneGameById(AppId.From(appId), out _);
return game?.Path.GetFullPath();
}
}
1 change: 0 additions & 1 deletion TweaksLauncher.API/nuget.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
<configuration>
<packageSources>
<add key="BepInEx" value="https://nuget.bepinex.dev/v3/index.json" />
<add key="Samboy" value="https://nuget.samboy.dev/v3/index.json" />
</packageSources>
</configuration>

0 comments on commit 5fe910d

Please sign in to comment.