Skip to content

Commit

Permalink
Migrate to new API; Include Remote in builds
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Jun 21, 2024
1 parent 94cf12e commit 0d32617
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 185 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ jobs:
cd ItchIoIntegration
dotnet publish -o ../Release/plugins/ItchIoIntegration -p:Configuration=Release
- name: Build RemoteDownloaderPlugin
run: |
cd RemoteDownloaderPlugin
dotnet publish -o ../Release/plugins/RemoteDownloaderPlugin -p:Configuration=Release
- name: Build SteamGridDb Middleware
run: |
cd SteamGridDbMiddleware
Expand Down Expand Up @@ -114,6 +119,11 @@ jobs:
cd ItchIoIntegration
dotnet publish -o ../Release/plugins/ItchIoIntegration -p:Configuration=Release
- name: Build RemoteDownloaderPlugin
run: |
cd RemoteDownloaderPlugin
dotnet publish -o ../Release/plugins/RemoteDownloaderPlugin -p:Configuration=Release
- name: Build SteamGridDb Middleware
run: |
cd SteamGridDbMiddleware
Expand Down
3 changes: 2 additions & 1 deletion Plugins.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ ItchIoIntegration
LegendaryIntegration
LocalGames
SteamExporterPlugin
SteamGridDbMiddleware
SteamGridDbMiddleware
RemoteDownloaderPlugin
52 changes: 25 additions & 27 deletions RemoteDownloaderPlugin/Game/GameDownload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ namespace RemoteDownloaderPlugin.Game;

public class GameDownload : ProgressStatus
{
private IEntry _entry;
private OnlineGameDownload _entry;
private int _lastSecond = 0;
private readonly CancellationTokenSource _cts = new();
private bool _doneDownloading = false;
public long TotalSize { get; private set; }
public string Version { get; private set; }
public GameType Type { get; private set; }
public string BaseFileName { get; private set; }
public string Filename { get; private set; }
public string BasePath { get; private set; }
public ContentTypes InstalledEntries { get; private set; }

private DateTimeOffset _downloadStart = DateTimeOffset.Now;

public GameDownload(IEntry entry)
public GameDownload(OnlineGameDownload entry)

Check warning on line 24 in RemoteDownloaderPlugin/Game/GameDownload.cs

View workflow job for this annotation

GitHub Actions / build-linux

Non-nullable property 'Version' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 24 in RemoteDownloaderPlugin/Game/GameDownload.cs

View workflow job for this annotation

GitHub Actions / build-linux

Non-nullable property 'Filename' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 24 in RemoteDownloaderPlugin/Game/GameDownload.cs

View workflow job for this annotation

GitHub Actions / build-linux

Non-nullable property 'BasePath' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
{
_entry = entry;
InstalledEntries = new();
Expand Down Expand Up @@ -49,50 +49,47 @@ public async Task Download(IApp app)
{
_doneDownloading = false;

if (_entry is EmuEntry emuEntry)
await DownloadEmu(app, emuEntry);
else if (_entry is PcEntry pcEntry)
await DownloadPc(app, pcEntry);
if (_entry.Platform == "Pc")
await DownloadPc(app);
else
throw new Exception("Download failed: Unknown type");
await DownloadEmu(app);
}

private async Task DownloadEmu(IApp app, EmuEntry entry)
private async Task DownloadEmu(IApp app)
{
Type = GameType.Emu;
if (entry.Files.Count(x => x.Type == "base") != 1)
if (_entry.Files.Count(x => x.Type == DownloadType.Base) != 1)
{
throw new Exception("Multiple base images, impossible download");
}

Line2 = $"{entry.Files.Count(x => x.Type == "base")} base, {entry.Files.Count(x => x.Type == "update")} update, {entry.Files.Count(x => x.Type == "dlc")} dlc";
BasePath = Path.Join(app.GameDir, "Remote", entry.Emu);
Line2 = $"{_entry.Files.Count(x => x.Type == DownloadType.Base)} base, {_entry.Files.Count(x => x.Type == DownloadType.Update)} update, {_entry.Files.Count(x => x.Type == DownloadType.Dlc)} dlc";
BasePath = Path.Join(app.GameDir, "Remote", _entry.Platform);
string baseGamePath = null;
var extraFilesPath = Path.Join(app.GameDir, "Remote", entry.Emu, entry.GameId);
var extraFilesPath = Path.Join(BasePath, _entry.Id);
Directory.CreateDirectory(BasePath);
Directory.CreateDirectory(extraFilesPath);

using HttpClient client = new();

for (int i = 0; i < entry.Files.Count; i++)
for (int i = 0; i < _entry.Files.Count; i++)
{

Progress<float> localProcess = new();
localProcess.ProgressChanged += (sender, f) =>
{
var part = (float)1 / entry.Files.Count;
var add = (float)i / entry.Files.Count;
var part = (float)1 / _entry.Files.Count;
var add = (float)i / _entry.Files.Count;
OnProgressUpdate(null, part * f + add);
};

var fileEntry = entry.Files[i];
var destPath = Path.Join(fileEntry.Type == "base" ? BasePath : extraFilesPath, fileEntry.Name);
var fileEntry = _entry.Files[i];
var destPath = Path.Join(fileEntry.Type == DownloadType.Base ? BasePath : extraFilesPath, fileEntry.Name);
InstalledEntries.Add(fileEntry.Type);

if (fileEntry.Type == "base")
if (fileEntry.Type == DownloadType.Base)
{
baseGamePath = destPath;
BaseFileName = fileEntry.Name;
Filename = fileEntry.Name;
}

var fs = new FileStream(destPath, FileMode.Create);
Expand Down Expand Up @@ -121,13 +118,13 @@ private async Task DownloadEmu(IApp app, EmuEntry entry)
}

TotalSize = (await Task.Run(() => LauncherGamePlugin.Utils.DirSize(new(extraFilesPath)))) + (new FileInfo(baseGamePath!)).Length;
Version = entry.Files.Last(x => x.Type is "base" or "update").Version;
Version = _entry.Files.Last(x => x.Type is DownloadType.Base or DownloadType.Update).Version;
}

private async Task DownloadPc(IApp app, PcEntry entry)
private async Task DownloadPc(IApp app)
{
Type = GameType.Pc;
BasePath = Path.Join(app.GameDir, "Remote", "Pc", entry.GameId);
BasePath = Path.Join(app.GameDir, "Remote", "Pc", _entry.Id);
Directory.CreateDirectory(BasePath);

using HttpClient client = new();
Expand All @@ -137,7 +134,7 @@ private async Task DownloadPc(IApp app, PcEntry entry)
try
{
// TODO: Fix security vuln, zips can have backwards paths
using HttpResponseMessage response = await client.GetAsync(entry.Url, HttpCompletionOption.ResponseHeadersRead, _cts.Token);
using HttpResponseMessage response = await client.GetAsync(_entry.Files.First().Url, HttpCompletionOption.ResponseHeadersRead, _cts.Token);
response.EnsureSuccessStatusCode();
await using var responseStream = await response.Content.ReadAsStreamAsync();
var interceptor =
Expand Down Expand Up @@ -178,9 +175,10 @@ private async Task DownloadPc(IApp app, PcEntry entry)
Directory.Delete(BasePath, true);
throw;
}


InstalledEntries.Base++;
TotalSize = await Task.Run(() => LauncherGamePlugin.Utils.DirSize(new(BasePath)));
Version = entry.Version;
Version = _entry.Files.First().Version;
}

public void Stop()
Expand Down
65 changes: 21 additions & 44 deletions RemoteDownloaderPlugin/Game/InstalledGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace RemoteDownloaderPlugin.Game;

public class InstalledGame : IGame
{
public string InternalName => Game.Id;
public string InternalName { get; }
public string Name => Game.Name;
public bool IsRunning { get; set; } = false;
public IGameSource Source => _plugin;
Expand All @@ -18,12 +18,7 @@ public bool HasImage(ImageType type)

public bool IsEmu => _type == GameType.Emu;

public ContentTypes InstalledContentTypes => IsEmu
? _emuGame.Types
: new ContentTypes()
{
Base = 1
};
public ContentTypes InstalledContentTypes => Game.InstalledContent;

public Task<byte[]?> GetImage(ImageType type)
{
Expand All @@ -38,41 +33,33 @@ public bool HasImage(ImageType type)
public InstalledStatus InstalledStatus => InstalledStatus.Installed;

public Platform EstimatedGamePlatform => IsEmu
? LauncherGamePlugin.Utils.GuessPlatformBasedOnString(_plugin.Storage.Data.EmuProfiles.FirstOrDefault(x => x.Platform == _emuGame!.Emu)?.ExecPath)
? LauncherGamePlugin.Utils.GuessPlatformBasedOnString(_plugin.Storage.Data.EmuProfiles.FirstOrDefault(x => x.Platform == Game.Platform)?.ExecPath)
: LauncherGamePlugin.Utils.GuessPlatformBasedOnString(_pcLaunchDetails!.LaunchExec);

public string GamePlatform => IsEmu
? _emuGame!.Emu
: "Pc";


public ProgressStatus? ProgressStatus => null;
public event Action? OnUpdate;

public void InvokeOnUpdate()
=> OnUpdate?.Invoke();

public IInstalledGame Game { get; }
public InstalledGameContent Game { get; }
private Plugin _plugin;
private PcLaunchDetails? _pcLaunchDetails;
private InstalledEmuGame? _emuGame;
private GameType _type;

public InstalledGame(IInstalledGame game, Plugin plugin)
public InstalledGame(InstalledGameContent game, Plugin plugin)
{
Game = game;
_plugin = plugin;
_type = game is InstalledEmuGame ? GameType.Emu : GameType.Pc;
_type = game.Platform == "Pc" ? GameType.Pc : GameType.Emu;
_pcLaunchDetails = null;
InternalName = $"{Game.Id}_{LauncherGamePlugin.Utils.OnlyLetters(Game.Platform).ToLower()}";

if (_type == GameType.Pc)
{
var fullPath = Path.Join(game.BasePath, "game.json");
_pcLaunchDetails = PcLaunchDetails.GetFromPath(fullPath);
}
else
{
_emuGame = (game as InstalledEmuGame)!;
}
}

public void Play()
Expand All @@ -81,14 +68,14 @@ public void Play()
{
if (IsEmu)
{
var emuProfile = _plugin.Storage.Data.EmuProfiles.FirstOrDefault(x => x.Platform == _emuGame!.Emu);
var emuProfile = _plugin.Storage.Data.EmuProfiles.FirstOrDefault(x => x.Platform == Game.Platform);

if (emuProfile == null)
{
throw new Exception($"No '{_emuGame!.Emu}' emulation profile exists");
throw new Exception($"No '{Game.Platform}' emulation profile exists");
}

var baseGamePath = Path.Join(Game.BasePath, _emuGame!.BaseFilename);
var baseGamePath = Path.Join(Game.BasePath, Game.Filename);

LaunchParams args = new(emuProfile.ExecPath,
emuProfile.CliArgs.Replace("{EXEC}", $"\"{baseGamePath}\""), emuProfile.WorkingDirectory, this,
Expand All @@ -112,46 +99,36 @@ public void Play()

public void Delete()
{
bool success = false;
if (IsEmu)
{
var baseGamePath = Path.Join(Game.BasePath, _emuGame.BaseFilename);
var baseGamePath = Path.Join(Game.BasePath, Game.Filename);
var extraDir = Path.Join(Game.BasePath, Game.Id);
var success = false;


try
{
File.Delete(baseGamePath);
Directory.Delete(extraDir, true);
success = true;
}
catch {}

var game = _plugin.Storage.Data.EmuGames.Find(x => x.Id == Game.Id);
_plugin.Storage.Data.EmuGames.Remove(game!);

if (!success)
{
_plugin.App.ShowTextPrompt("Failed to delete files. Game has been unlinked");
}
}
else
{
var success = false;

try
{
Directory.Delete(Game.BasePath, true);
success = true;
}
catch {}
}

var game = _plugin.Storage.Data.Games.Find(x => x.Id == Game.Id && x.Platform == Game.Platform);
_plugin.Storage.Data.Games.Remove(game!);

var game = _plugin.Storage.Data.PcGames.Find(x => x.Id == Game.Id);
_plugin.Storage.Data.PcGames.Remove(game!);

if (!success)
{
_plugin.App.ShowTextPrompt("Failed to delete files. Game has been unlinked");
}
if (!success)
{
_plugin.App.ShowTextPrompt("Failed to delete files. Game has been unlinked");
}

_plugin.App.ReloadGames();
Expand Down
Loading

0 comments on commit 0d32617

Please sign in to comment.