Skip to content

Commit

Permalink
fix #2083
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Oct 23, 2024
1 parent 2e84147 commit 4d681ef
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
14 changes: 13 additions & 1 deletion src/Snap.Hutao/Snap.Hutao/Core/IO/TempFileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ internal sealed partial class TempFileStream : Stream
private readonly FileStream stream;

public TempFileStream(FileMode mode, FileAccess access)
: this(Path.GetTempFileName(), mode, access)
{
path = Path.GetTempFileName();
}

private TempFileStream(string path, FileMode mode, FileAccess access)
{
this.path = path;
stream = File.Open(path, mode, access);
}

Expand All @@ -26,6 +31,13 @@ public TempFileStream(FileMode mode, FileAccess access)

public override long Position { get => stream.Position; set => stream.Position = value; }

public static TempFileStream CopyFrom(string file, FileMode mode, FileAccess access)
{
string path = Path.GetTempFileName();
File.Copy(file, path, true);
return new TempFileStream(path, mode, access);
}

public override void Flush()
{
stream.Flush();
Expand Down
11 changes: 2 additions & 9 deletions src/Snap.Hutao/Snap.Hutao/Service/Feature/FeatureService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,8 @@ internal sealed partial class FeatureService : IFeatureService
IHttpClientFactory httpClientFactory = scope.ServiceProvider.GetRequiredService<IHttpClientFactory>();
using (HttpClient httpClient = httpClientFactory.CreateClient(nameof(FeatureService)))
{
try
{
string url = hutaoEndpointsFactory.Create().Feature($"UnlockerIsland_{tag}");
return await httpClient.GetFromJsonAsync<IslandFeature>(url).ConfigureAwait(false);
}
catch
{
return default;
}
string url = hutaoEndpointsFactory.Create().Feature($"UnlockerIsland_{tag}");
return await httpClient.GetFromJsonAsync<IslandFeature>(url).ConfigureAwait(false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public async ValueTask<ValueResult<bool, GachaLogQuery>> GetQueryAsync()
return new(false, GachaLogQuery.Invalid(SH.FormatServiceGachaLogUrlProviderCachePathNotFound(cacheFile)));
}

using (FileStream fileStream = File.OpenRead(GetCacheFile(path)))
// Must copy the file to avoid the following exception:
// System.IO.IOException: The process cannot access the file
using (TempFileStream fileStream = TempFileStream.CopyFrom(path, FileMode.Open, FileAccess.Read))
{
using (MemoryStream memoryStream = await memoryStreamFactory.GetStreamAsync(fileStream).ConfigureAwait(false))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

using Snap.Hutao.Core;
using Snap.Hutao.Core.ExceptionService;
using Snap.Hutao.Service.Game.Unlocker;

namespace Snap.Hutao.Service.Game.Launching.Handler;
Expand Down Expand Up @@ -35,7 +36,7 @@ public async ValueTask OnExecutionAsync(LaunchExecutionContext context, LaunchEx
}
else
{
context.Logger.LogError("Unlocking FPS failed");
HutaoException.Throw("下载解锁帧率配置文件失败");
}
}
catch (Exception ex)
Expand Down

0 comments on commit 4d681ef

Please sign in to comment.