Skip to content

Commit

Permalink
Update Remora.Results.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nihlus committed Mar 8, 2024
1 parent da8eadd commit 4d65279
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 219 deletions.
8 changes: 4 additions & 4 deletions Launchpad.Common/Handlers/Manifest/ManifestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,27 @@ public ManifestHandler(string localBaseDirectory, Uri remoteURL, ESystemTarget s
/// <param name="getOldManifest">Whether or not the old manifest or the new manifest should be retrieved.</param>
/// <returns>A list of <see cref="ManifestEntry"/> objects.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown if the <paramref name="manifestType"/> is not a known value.</exception>
public RetrieveEntityResult<IReadOnlyList<ManifestEntry>> GetManifest(EManifestType manifestType, bool getOldManifest)
public Result<IReadOnlyList<ManifestEntry>> GetManifest(EManifestType manifestType, bool getOldManifest)
{
lock (_manifestsLock)
{
if (getOldManifest)
{
if (_oldManifests.TryGetValue(manifestType, out var oldManifest))
{
return RetrieveEntityResult<IReadOnlyList<ManifestEntry>>.FromSuccess(oldManifest);
return Result<IReadOnlyList<ManifestEntry>>.FromSuccess(oldManifest);
}
}
else
{
if (_manifests.TryGetValue(manifestType, out var manifest))
{
return RetrieveEntityResult<IReadOnlyList<ManifestEntry>>.FromSuccess(manifest);
return Result<IReadOnlyList<ManifestEntry>>.FromSuccess(manifest);
}
}
}

return RetrieveEntityResult<IReadOnlyList<ManifestEntry>>.FromError("No manifest found.");
return new NotFoundError("No manifest found.");
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Launchpad.Common/Launchpad.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Remora.Results" Version="1.1.0" />
<PackageReference Include="Remora.Results" Version="7.4.1" />
</ItemGroup>
</Project>
8 changes: 4 additions & 4 deletions Launchpad.Launcher/Handlers/ChecksHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public ChecksHandler(ILogger<ChecksHandler> log, PatchProtocolHandler patch, Dir
/// Determines whether this instance can connect to a patching service.
/// </summary>
/// <returns><c>true</c> if this instance can connect to a patching service; otherwise, <c>false</c>.</returns>
public Task<RetrieveEntityResult<bool>> CanPatchAsync() => _patch.CanPatchAsync();
public Task<Result<bool>> CanPatchAsync() => _patch.CanPatchAsync();

/// <summary>
/// Determines whether this is the first time the launcher starts.
Expand Down Expand Up @@ -110,14 +110,14 @@ public bool IsGameInstalled()
/// Determines whether the game is outdated.
/// </summary>
/// <returns><c>true</c> if the game is outdated; otherwise, <c>false</c>.</returns>
public Task<RetrieveEntityResult<bool>> IsGameOutdatedAsync()
public Task<Result<bool>> IsGameOutdatedAsync()
=> _patch.IsModuleOutdatedAsync(EModule.Game);

/// <summary>
/// Determines whether the launcher is outdated.
/// </summary>
/// <returns><c>true</c> if the launcher is outdated; otherwise, <c>false</c>.</returns>
public Task<RetrieveEntityResult<bool>> IsLauncherOutdatedAsync()
public Task<Result<bool>> IsLauncherOutdatedAsync()
=> _patch.IsModuleOutdatedAsync(EModule.Launcher);

/// <summary>
Expand All @@ -143,7 +143,7 @@ private bool IsInstallCookieEmpty()
/// </summary>
/// <returns><c>true</c>, if the server does provide files for the platform, <c>false</c> otherwise.</returns>
/// <param name="platform">platform.</param>
public Task<RetrieveEntityResult<bool>> IsPlatformAvailableAsync(ESystemTarget platform)
public Task<Result<bool>> IsPlatformAvailableAsync(ESystemTarget platform)
=> _patch.IsPlatformAvailableAsync(platform);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ DirectoryHelpers directoryHelpers
}

/// <inheritdoc />
public override async Task<RetrieveEntityResult<bool>> CanPatchAsync()
public override async Task<Result<bool>> CanPatchAsync()
{
_log.LogInformation("Pinging remote patching server to determine if we can connect to it.");

Expand All @@ -101,7 +101,7 @@ public override async Task<RetrieveEntityResult<bool>> CanPatchAsync()
var getPlainRequest = CreateFtpWebRequest(url, username, password);
if (!getPlainRequest.IsSuccess)
{
return RetrieveEntityResult<bool>.FromError(getPlainRequest);
return Result<bool>.FromError(getPlainRequest);
}

var request = getPlainRequest.Entity;
Expand Down Expand Up @@ -146,30 +146,30 @@ public override async Task<RetrieveEntityResult<bool>> CanPatchAsync()
}

/// <inheritdoc />
public override Task<RetrieveEntityResult<bool>> IsPlatformAvailableAsync(ESystemTarget platform)
public override Task<Result<bool>> IsPlatformAvailableAsync(ESystemTarget platform)
{
var remote = $"{this.Configuration.RemoteAddress}/game/{platform}/.provides";

return DoesRemoteFileExistAsync(remote);
}

/// <inheritdoc />
public override Task<RetrieveEntityResult<string>> GetChangelogMarkupAsync()
public override Task<Result<string>> GetChangelogMarkupAsync()
{
var changelogURL = $"{this.Configuration.RemoteAddress}/launcher/changelog.pango";
return ReadRemoteFileAsync(changelogURL);
}

/// <inheritdoc />
public override Task<RetrieveEntityResult<bool>> CanProvideBannerAsync()
public override Task<Result<bool>> CanProvideBannerAsync()
{
var bannerURL = $"{this.Configuration.RemoteAddress}/launcher/banner.png";

return DoesRemoteFileExistAsync(bannerURL);
}

/// <inheritdoc />
public override async Task<RetrieveEntityResult<Image<Rgba32>>> GetBannerAsync()
public override async Task<Result<Image<Rgba32>>> GetBannerAsync()
{
var bannerURL = $"{this.Configuration.RemoteAddress}/launcher/banner.png";

Expand All @@ -181,7 +181,7 @@ public override async Task<RetrieveEntityResult<Image<Rgba32>>> GetBannerAsync()
}

/// <inheritdoc />
protected override async Task<RetrieveEntityResult<string>> ReadRemoteFileAsync(string url, bool useAnonymousLogin = false)
protected override async Task<Result<string>> ReadRemoteFileAsync(string url, bool useAnonymousLogin = false)
{
// Clean the input url first
var remoteURL = url.Replace(Path.DirectorySeparatorChar, '/');
Expand All @@ -206,12 +206,12 @@ protected override async Task<RetrieveEntityResult<string>> ReadRemoteFileAsync(

if (!getRequest.IsSuccess)
{
return RetrieveEntityResult<string>.FromError(getRequest);
return Result<string>.FromError(getRequest);
}

if (!getSizeRequest.IsSuccess)
{
return RetrieveEntityResult<string>.FromError(getSizeRequest);
return Result<string>.FromError(getSizeRequest);
}

var request = getRequest.Entity;
Expand Down Expand Up @@ -268,7 +268,7 @@ protected override async Task<RetrieveEntityResult<string>> ReadRemoteFileAsync(
}

/// <inheritdoc />
protected override async Task<DetermineConditionResult> DownloadRemoteFileAsync
protected override async Task<Result> DownloadRemoteFileAsync
(
string url,
string localPath,
Expand Down Expand Up @@ -300,12 +300,12 @@ protected override async Task<DetermineConditionResult> DownloadRemoteFileAsync

if (!getRequest.IsSuccess)
{
return DetermineConditionResult.FromError(getRequest);
return Result.FromError(getRequest);
}

if (!getSizeRequest.IsSuccess)
{
return DetermineConditionResult.FromError(getSizeRequest);
return Result.FromError(getSizeRequest);
}

var request = getRequest.Entity;
Expand All @@ -317,19 +317,11 @@ protected override async Task<DetermineConditionResult> DownloadRemoteFileAsync
sizeRequest.Method = WebRequestMethods.Ftp.GetFileSize;

await using var contentStream = (await request.GetResponseAsync()).GetResponseStream();
if (contentStream == null)
{
return DetermineConditionResult.FromError
(
$"Failed to download the remote file at \"{remoteURL}\" (content stream was null). " +
$"Check your internet connection."
);
}

var fileSize = contentOffset;
using (var sizereader = (FtpWebResponse)sizeRequest.GetResponse())
using (var sizeReader = (FtpWebResponse)sizeRequest.GetResponse())
{
fileSize += sizereader.ContentLength;
fileSize += sizeReader.ContentLength;
}

await using
Expand Down Expand Up @@ -395,22 +387,16 @@ protected override async Task<DetermineConditionResult> DownloadRemoteFileAsync
}
catch (WebException wex)
{
return DetermineConditionResult.FromError
(
$"Failed to download the remote file at \"{remoteURL}\".",
wex
);
_log.LogError(wex, "Failed to read the contents of remote file \"{RemoteUrl}\"", remoteURL);
return wex;
}
catch (IOException ioex)
{
return DetermineConditionResult.FromError
(
$"Failed to download the remote file at \"{remoteURL}\".",
ioex
);
_log.LogError(ioex, "Failed to read the contents of remote file \"{RemoteUrl}\"", remoteURL);
return ioex;
}

return DetermineConditionResult.FromSuccess();
return Result.FromSuccess();
}

/// <summary>
Expand All @@ -420,7 +406,7 @@ protected override async Task<DetermineConditionResult> DownloadRemoteFileAsync
/// <param name="remotePath">Ftp directory path.</param>
/// <param name="username">Remote FTP username.</param>
/// <param name="password">Remote FTP password.</param>
private CreateEntityResult<FtpWebRequest> CreateFtpWebRequest(string remotePath, string username, string password)
private Result<FtpWebRequest> CreateFtpWebRequest(string remotePath, string username, string password)
{
if (!remotePath.StartsWith(this.Configuration.RemoteAddress.AbsoluteUri))
{
Expand All @@ -444,28 +430,23 @@ private CreateEntityResult<FtpWebRequest> CreateFtpWebRequest(string remotePath,
}
catch (WebException wex)
{
return CreateEntityResult<FtpWebRequest>.FromError
(
"Unable to create a WebRequest for the specified file.",
wex
);
_log.LogError(wex, "Unable to create a WebRequest for the specified file \"{RemotePath}\"", remotePath);
return wex;
}
catch (ArgumentException aex)
{
return CreateEntityResult<FtpWebRequest>.FromError
(
"Unable to create a WebRequest for the specified file.",
aex
);
_log.LogError(aex, "Unable to create a WebRequest for the specified file \"{RemotePath}\"", remotePath);
return aex;
}
catch (UriFormatException uex)
{
return CreateEntityResult<FtpWebRequest>.FromError
_log.LogError
(
"Unable to create a WebRequest for the specified file. You may need to add \"ftp://\" before the " +
"url in the config",
uex
uex,
"Unable to create a WebRequest for the specified file. You may need to add \"ftp://\" before the url in the config"
);

return uex;
}
}

Expand All @@ -474,7 +455,7 @@ private CreateEntityResult<FtpWebRequest> CreateFtpWebRequest(string remotePath,
/// </summary>
/// <returns><c>true</c>, if the file exists, <c>false</c> otherwise.</returns>
/// <param name="remotePath">Remote path.</param>
private async Task<RetrieveEntityResult<bool>> DoesRemoteFileExistAsync(string remotePath)
private async Task<Result<bool>> DoesRemoteFileExistAsync(string remotePath)
{
try
{
Expand All @@ -487,7 +468,7 @@ private async Task<RetrieveEntityResult<bool>> DoesRemoteFileExistAsync(string r

if (!createRequest.IsSuccess)
{
return RetrieveEntityResult<bool>.FromError(createRequest);
return Result<bool>.FromError(createRequest);
}

var request = createRequest.Entity;
Expand All @@ -501,7 +482,7 @@ private async Task<RetrieveEntityResult<bool>> DoesRemoteFileExistAsync(string r
return false;
}

return RetrieveEntityResult<bool>.FromError(ex);
return Result<bool>.FromError(ex);
}

return true;
Expand Down
Loading

0 comments on commit 4d65279

Please sign in to comment.