Skip to content

Commit

Permalink
Finishing 'cleanup Wellbia's XignCode' feature.
Browse files Browse the repository at this point in the history
- Properly show console log
- Optimize memory allocation for downloading the "xuninstaller".
  • Loading branch information
Leayal committed Nov 7, 2023
1 parent 255cad8 commit 2e8647d
Show file tree
Hide file tree
Showing 27 changed files with 194 additions and 38 deletions.
33 changes: 33 additions & 0 deletions LauncherCore/Classes/MemoryStreamPooledBackBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Leayal.PSO2Launcher.Toolbox;
using System.Buffers;
using System.IO;
using System.Threading;

namespace Leayal.PSO2Launcher.Core.Classes
{
sealed class MemoryStreamPooledBackBuffer : MemoryStream
{
private readonly bool isfromArrayPool;
private byte[]? buffer;

public MemoryStreamPooledBackBuffer(byte[] buffer, int index, int count, bool isfromArrayPool) : base(buffer, index, count, false, true)
{
this.isfromArrayPool = isfromArrayPool;
this.Position = 0;
this.buffer = buffer;
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (isfromArrayPool)
{
var borrowed = Interlocked.Exchange(ref this.buffer, null);
if (borrowed != null)
{
ArrayPool<byte>.Shared.Return(borrowed);
}
}
}
}
}
14 changes: 8 additions & 6 deletions LauncherCore/Classes/PSO2/PSO2HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Security.Cryptography;
using Leayal.Shared;
using Leayal.SharedInterfaces;
using System.Buffers;

namespace Leayal.PSO2Launcher.Core.Classes.PSO2
{
Expand Down Expand Up @@ -48,12 +49,13 @@ static PSO2HttpClient()
}

private readonly HttpClient client;
private const string UA_AQUA_HTTP = "AQUA_HTTP",
internal const string UA_AQUA_HTTP = "AQUA_HTTP",
// PSO2Launcher = "PSO2Launcher",
UA_PSO2_Launcher = "PSO2 Launcher",
UA_pso2launcher = "pso2launcher",
UA_WellbiaSite = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/119.0",
Uri_WellbiaUninstaller = "https://wellbia.com/xuninstaller.zip";
Url_WellbiaUninstaller = "https://wellbia.com/xuninstaller.zip";
internal static readonly Uri Uri_WellbiaUninstaller = new Uri(Url_WellbiaUninstaller);
private readonly PersistentCacheManager? dataCache;

// Need to add snail mode (for when internet is extremely unreliable).
Expand Down Expand Up @@ -220,7 +222,7 @@ public async Task<bool> AuthOTPAsync(PSO2LoginToken loginToken, SecureString otp

public async Task<Stream> DownloadWellbiaUninstaller(CancellationToken cancellationToken)
{
var url = new Uri(Uri_WellbiaUninstaller);
var url = Uri_WellbiaUninstaller;
using (var request = new HttpRequestMessage(HttpMethod.Get, url))
{
request.Headers.Host = url.Host;
Expand All @@ -234,7 +236,7 @@ public async Task<Stream> DownloadWellbiaUninstaller(CancellationToken cancellat
var contentLen = response.Content.Headers.ContentLength;
if (contentLen.HasValue && contentLen.Value < (1024 * 1024 * 5))
{
var buffer = new byte[contentLen.Value];
var buffer = ArrayPool<byte>.Shared.Rent((int)contentLen.Value);
int totalread = 0;
using (var repContent = await response.Content.ReadAsStreamAsync(cancellationToken))
{
Expand All @@ -246,11 +248,11 @@ public async Task<Stream> DownloadWellbiaUninstaller(CancellationToken cancellat
}
while (read != 0 && totalread < buffer.Length);
}
return new MemoryStream(buffer, 0, totalread, false, true) { Position = 0 };
return new MemoryStreamPooledBackBuffer(buffer, 0, totalread, true);
}
else
{
var filename = Path.GetFullPath(string.Concat("xuninstaller.".AsSpan(), DateTimeOffset.UtcNow.ToFileTime().ToString(System.Globalization.NumberFormatInfo.InvariantInfo).AsSpan(), Path.GetExtension(Uri_WellbiaUninstaller.AsSpan())), RuntimeValues.RootDirectory);
var filename = Path.GetFullPath(string.Concat("xuninstaller.".AsSpan(), DateTimeOffset.UtcNow.ToFileTime().ToString(System.Globalization.NumberFormatInfo.InvariantInfo).AsSpan(), Path.GetExtension(url.AbsolutePath.AsSpan())), RuntimeValues.RootDirectory);
var local_fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.Read, 0, FileOptions.DeleteOnClose | FileOptions.Asynchronous);
using (var repContent = await response.Content.ReadAsStreamAsync(cancellationToken))
{
Expand Down
39 changes: 20 additions & 19 deletions LauncherCore/StaticResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ namespace Leayal.PSO2Launcher.Core
{
static class StaticResources
{
public static readonly Uri Url_ConfirmSelfUpdate = new Uri("pso2lealauncher://selfupdatechecker/confirm");
public static readonly Uri Url_IgnoreSelfUpdate = new Uri("pso2lealauncher://selfupdatechecker/ignore");

public static readonly Uri Url_ShowAuthor = new Uri("pso2lealauncher://myself/show-author");
public static readonly Uri Url_ShowSourceCodeGithub = new Uri("pso2lealauncher://myself/show-source-code");
public static readonly Uri Url_ShowLatestGithubRelease = new Uri("pso2lealauncher://myself/show-latest-github-release");
public static readonly Uri Url_ShowIssuesGithub = new Uri("pso2lealauncher://myself/show-github-issues");

public static readonly Uri Url_OpenWebView2InstallerDownloadPage = new Uri("pso2lealauncher://myself/show-webview2-downloadpage");
public static readonly Uri Url_DownloadWebView2BootstrapInstaller = new Uri("pso2lealauncher://myself/download-webview2-bootstrapinstaller");

public static readonly Uri Url_ShowLogDialogFromGuid = new Uri("pso2lealauncher://showdialog.fromguid/");

public static readonly Uri Url_ShowPathInExplorer_SpecialFolder_JP_PSO2Config = new Uri("pso2lealauncher://showpathinexplorer/specialfolder/jp/pso2config");

public static readonly Uri Url_Toolbox_VendorItemPickupCounter = new Uri("pso2lealauncher://toolbox/vendoritempickupcounter");
public static readonly Uri Url_Toolbox_PSO2DataOrganizer = new Uri("pso2lealauncher://toolbox/pso2dataorganizer");

public static readonly Uri SEGALauncherNewsUrl = new Uri("https://launcher.pso2.jp/ngs/01/");
public static readonly Uri Url_ConfirmSelfUpdate = new Uri("pso2lealauncher://selfupdatechecker/confirm"),
Url_IgnoreSelfUpdate = new Uri("pso2lealauncher://selfupdatechecker/ignore"),

Url_ShowAuthor = new Uri("pso2lealauncher://myself/show-author"),
Url_ShowSourceCodeGithub = new Uri("pso2lealauncher://myself/show-source-code"),
Url_ShowLatestGithubRelease = new Uri("pso2lealauncher://myself/show-latest-github-release"),
Url_ShowIssuesGithub = new Uri("pso2lealauncher://myself/show-github-issues"),

Url_OpenWebView2InstallerDownloadPage = new Uri("pso2lealauncher://myself/show-webview2-downloadpage"),
Url_DownloadWebView2BootstrapInstaller = new Uri("pso2lealauncher://myself/download-webview2-bootstrapinstaller"),

Url_ShowLogDialogFromGuid = new Uri("pso2lealauncher://showdialog.fromguid/"),

Url_ShowPathInExplorer_SpecialFolder_JP_PSO2Config = new Uri("pso2lealauncher://showpathinexplorer/specialfolder/jp/pso2config"),

Url_Toolbox_VendorItemPickupCounter = new Uri("pso2lealauncher://toolbox/vendoritempickupcounter"),
Url_Toolbox_PSO2DataOrganizer = new Uri("pso2lealauncher://toolbox/pso2dataorganizer"),

SEGALauncherNewsUrl = new Uri("https://launcher.pso2.jp/ngs/01/"),
WellbiaFAQWebsite = new Uri("https://wellbia.com/?module=Board&action=SiteBoard&sMode=SELECT_FORM&iBrdNo=3");

/*
public static readonly bool IsCurrentProcessAdmin = Leayal.Shared.UacHelper.IsCurrentProcessElevated;
Expand Down
29 changes: 29 additions & 0 deletions LauncherCore/Windows/MainMenuWindow.ConsoleLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ICSharpCode.AvalonEdit.Rendering;
using ICSharpCode.AvalonEdit.Utils;
using Leayal.PSO2Launcher.Core.Classes.AvalonEdit;
using Leayal.Shared.Windows;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -227,6 +228,34 @@ private static void VisualLineLinkText_LinkClicked(HyperlinkVisualLineElementDat
}
}

[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static void ConsoleLog_OpenLinkWithDefaultBrowser(HyperlinkVisualLineElementData element)
{
var url = element.NavigateUri;
if (url != null)
{
try
{
WindowsExplorerHelper.OpenUrlWithDefaultBrowser(url);
}
catch { }
}
}

[MethodImpl(MethodImplOptions.AggressiveOptimization)]
private static void ConsoleLog_SelectLocalPathLinkInExplorer(HyperlinkVisualLineElementData element)
{
var url = element.NavigateUri;
if (url != null && url.IsFile)
{
try
{
WindowsExplorerHelper.SelectPathInExplorer(url.LocalPath);
}
catch { }
}
}

private void ConsoleLogMenuItemCopySelected_Click(object sender, RoutedEventArgs e)
{
if (this.ConsoleLog.SelectionLength == 0) return;
Expand Down
Loading

0 comments on commit 2e8647d

Please sign in to comment.