Skip to content

Commit

Permalink
Merge pull request #15 from rGunti/feature/working-downloader
Browse files Browse the repository at this point in the history
Feature/working downloader
  • Loading branch information
Raphael Guntersweiler authored Oct 31, 2020
2 parents 09cda7d + bb58f01 commit 157f180
Show file tree
Hide file tree
Showing 15 changed files with 402 additions and 164 deletions.
3 changes: 2 additions & 1 deletion BeatKeeper.Kernel/BeatKeeper.Kernel.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Octokit" Version="0.48.0" />
<PackageReference Include="Serilog" Version="2.9.0" />
</ItemGroup>

Expand Down
13 changes: 11 additions & 2 deletions BeatKeeper.Kernel/Services/BeatSaberVersionDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using BeatKeeper.Kernel.Entities;
using BeatKeeper.Kernel.Services.DepotDownloader;

namespace BeatKeeper.Kernel.Services
{
Expand All @@ -13,22 +14,28 @@ public class BeatSaberVersionDownloader
private readonly string _username;
private readonly string _versionFilePath;
private readonly SteamCmdService _steamCmdService;
private readonly DepotDownloaderService _depotDownloaderService;

public BeatSaberVersionDownloader(
ISteamCmdServiceFactory steamCmdServiceFactory,
IDepotDownloaderServiceFactory depotDownloaderServiceFactory,
string username,
string versionFilePath = "versions.txt")
: this(steamCmdServiceFactory.Build(), username, versionFilePath)
: this(steamCmdServiceFactory.Build(),
depotDownloaderServiceFactory.Build(),
username, versionFilePath)
{
}

public BeatSaberVersionDownloader(
SteamCmdService steamCmdService,
DepotDownloaderService depotDownloaderService,
string username,
string versionFilePath = "versions.txt")
{
_username = username;
_steamCmdService = steamCmdService;
_depotDownloaderService = depotDownloaderService;
_versionFilePath = versionFilePath;
ReadVersionFile();
}
Expand Down Expand Up @@ -113,7 +120,9 @@ private void ReadVersionFile()

public void DownloadArtifact(Artifact version)
{
_steamCmdService.DownloadArtifact(_username, version.ManifestId)
// _steamCmdService.DownloadArtifact(_username, version.ManifestId)
// .WaitForExit();
_depotDownloaderService.DownloadArtifact("620980", "620981", version.ManifestId, _username)
.WaitForExit();
}
}
Expand Down
116 changes: 116 additions & 0 deletions BeatKeeper.Kernel/Services/DepotDownloader/DepotDownloaderService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using Octokit;
using Serilog;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;

namespace BeatKeeper.Kernel.Services.DepotDownloader
{
public class DepotDownloaderService
{
private const string DEPOTDOWNLOADER_ARTIFACT = "depotdownloader.zip";

private readonly string _basePath;

public DepotDownloaderService(string basePath)
{
Log.Verbose($"Constructing {GetType().Name} for {basePath}");
_basePath = basePath;
}

private string GetPath(string file)
=> Path.Combine(_basePath, file);

private string Executable
=> Path.Combine(_basePath, "depotdownloader.bat");
private string DotNetExecutable
=> Path.Combine(_basePath, "depotdownloader.dll");

public bool IsInitialized
=> File.Exists(DotNetExecutable) && File.Exists(Executable);

private void ClearBaseFolder()
{
try
{
Log.Debug("Clearing DepotDownloader folder ...");
Directory.Delete(_basePath, true);

Log.Debug("Recreating folder ...");
Directory.CreateDirectory(_basePath);
} catch (Exception ex)
{
Log.Error(ex, "Failed to clear folder");
throw ex;
}
}

public void DownloadLatestRelease()
{
var client = new GitHubClient(new ProductHeaderValue("BeatSaberKeeper"));
var releases = client.Repository.Release.GetAll("SteamRE", "DepotDownloader")
.GetAwaiter().GetResult();
var latestRelease = releases.First();

var downloadArtifact = GetPath(DEPOTDOWNLOADER_ARTIFACT);
using (var webClient = new WebClient())
{
ClearBaseFolder();

Log.Debug($"Downloading latest release of DepotDownloader ...");
webClient.DownloadFile(latestRelease.Assets.First().BrowserDownloadUrl, downloadArtifact);

Log.Debug("Extracting DepotDownloader ...");
ZipFile.ExtractToDirectory(downloadArtifact, _basePath);
File.Delete(downloadArtifact);
}
}

private Process ConstructProcess(
bool redirect,
bool useDotNet,
params string[] args)
{
var executable = useDotNet ? "dotnet" : Executable;
var processArgs = (useDotNet ? $"\"{DotNetExecutable}\" " : "") + string.Join(" ", args);

var p = new Process()
{
StartInfo = new ProcessStartInfo(executable, processArgs)
{
UseShellExecute = !redirect,
RedirectStandardInput = redirect,
RedirectStandardOutput = redirect,
WindowStyle = redirect ? ProcessWindowStyle.Hidden : ProcessWindowStyle.Normal,
WorkingDirectory = _basePath
}
};
return p;
}

private Process RunDDProcess(bool redirect, bool useDotNet, params string[] args)
{
Log.Debug($"Running DepotDownloader with parameters \"{string.Join(" ", args)}\"");

var p = ConstructProcess(redirect, useDotNet, args);
p.Start();
return p;
}

public Process DownloadArtifact(string appId, string depotId, string manifestId,
string username)
{
return RunDDProcess(false, true,
$"-app {appId}",
$"-depot {depotId}",
$"-manifest {manifestId}",
$"-username \"{username}\"",
$"-remember-password");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace BeatKeeper.Kernel.Services.DepotDownloader
{
public interface IDepotDownloaderServiceFactory
{
DepotDownloaderService Build();
}
public class PrebuiltDepotDownloaderServiceFactory : IDepotDownloaderServiceFactory
{
private readonly string _path;

public PrebuiltDepotDownloaderServiceFactory(string path)
{
_path = path;
}

public DepotDownloaderService Build()
{
return new DepotDownloaderService(_path);
}
}
}
7 changes: 4 additions & 3 deletions BeatKeeper.Windows/BeatKeeper.Windows.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
<ApplicationIcon>saber.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="GitHub.ReleaseDownloader, Version=0.1.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\GitHub.ReleaseDownloader.0.1.2\lib\net472\GitHub.ReleaseDownloader.dll</HintPath>
</Reference>
<Reference Include="ObjectListView, Version=2.9.1.1072, Culture=neutral, PublicKeyToken=b1c5bf581481bcd4, processorArchitecture=MSIL">
<HintPath>..\packages\ObjectListView.Official.2.9.1\lib\net20\ObjectListView.dll</HintPath>
</Reference>
<Reference Include="Octokit, Version=0.48.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Octokit.0.48.0\lib\net46\Octokit.dll</HintPath>
</Reference>
<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
<HintPath>..\packages\Serilog.2.5.0\lib\net46\Serilog.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -128,6 +128,7 @@
</Compile>
<Compile Include="Utils\AppInfo.cs" />
<Compile Include="Utils\ClientPathUtils.cs" />
<Compile Include="Utils\DepotDownloaderServiceFactory.cs" />
<Compile Include="Utils\MessageBoxUtils.cs" />
<Compile Include="Utils\ReleaseChecker.cs" />
<Compile Include="Utils\SettingsUtils.cs" />
Expand Down
11 changes: 6 additions & 5 deletions BeatKeeper.Windows/DownloadForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public DownloadForm()
InitializeComponent();
_beatSaberVersionDownloader = new BeatSaberVersionDownloader(
SteamCmdServiceFactory.Instance.Build(),
DepotDownloaderServiceFactory.Instance.Build(),
SettingsUtils.LastSteamUsername);
}

Expand Down Expand Up @@ -70,7 +71,7 @@ private void DownloadStartButton_Click(object sender, EventArgs e)

SetStatus($"Packing BeatSaber v{downloadArtifact.GameVersion} ...");
BeatKeeperPackageProcessor.PackVanillaArtifactV1(
ClientPathUtils.SteamCmdDownloadFolder,
ClientPathUtils.DepotDownloaderDownloadFolder,
ClientPathUtils.VanillaArchiveFolder,
downloadArtifact.GameVersion
);
Expand All @@ -82,7 +83,7 @@ private void DownloadStartButton_Click(object sender, EventArgs e)
Close();
});
}

private void DownloadVersionComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
DownloadStartButton.Enabled = DownloadVersionComboBox.SelectedItem != null;
Expand Down Expand Up @@ -121,16 +122,16 @@ private void DownloadAllVersionsButton_Click(object sender, EventArgs e)
i++;
SetStatus($"({i}/{total}) Downloading BeatSaber {artifact} ...");
SetProgress(i - 1, total * 2);
if (Directory.Exists(ClientPathUtils.SteamCmdDownloadFolder))
if (Directory.Exists(ClientPathUtils.DepotDownloaderDownloadFolder))
{
Directory.Delete(ClientPathUtils.SteamCmdDownloadFolder, true);
Directory.Delete(ClientPathUtils.DepotDownloaderDownloadFolder, true);
}
_beatSaberVersionDownloader.DownloadArtifact(artifact);

SetStatus($"({i}/{total}) Packing BeatSaber {artifact} ...");
SetProgress(i, total * 2);
BeatKeeperPackageProcessor.PackVanillaArtifactV1(
ClientPathUtils.SteamCmdDownloadFolder,
ClientPathUtils.DepotDownloaderDownloadFolder,
ClientPathUtils.VanillaArchiveFolder,
artifact.GameVersion);
}
Expand Down
Loading

0 comments on commit 157f180

Please sign in to comment.