Skip to content

Commit

Permalink
Try to download the download assistant by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Legi428 committed Oct 5, 2024
1 parent 4678cbf commit 64cc970
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
30 changes: 23 additions & 7 deletions UnityLauncherPro/GetUnityUpdates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class GetUnityUpdates
private const int DelayBetweenBatches = 1000; // 1 second in milliseconds
private const string CacheFileName = "UnityVersionCache.json";

private static readonly HttpClient httpClient = new HttpClient();
private static readonly HttpClient Client = new HttpClient();

public static async Task<List<UnityVersion>> FetchAll()
{
Expand All @@ -37,7 +37,7 @@ public static async Task<List<UnityVersion>> FetchAll()
return allVersions;
}

public static async Task<string> FetchDownloadUrl(string unityVersion, bool assistantUrl = false)
public static async Task<string> FetchDownloadUrl(string unityVersion)
{
if (string.IsNullOrEmpty(unityVersion))
{
Expand All @@ -48,7 +48,7 @@ public static async Task<string> FetchDownloadUrl(string unityVersion, bool assi

try
{
string responseString = await httpClient.GetStringAsync(apiUrl);
string responseString = await Client.GetStringAsync(apiUrl);
JsonDocument doc = JsonDocument.Parse(responseString);
try
{
Expand All @@ -75,21 +75,37 @@ public static async Task<string> FetchDownloadUrl(string unityVersion, bool assi

if (!string.IsNullOrEmpty(downloadUrl))
{
if (!assistantUrl) return downloadUrl;

if (!string.IsNullOrEmpty(shortRevision))
{
var startIndex = downloadUrl.LastIndexOf(shortRevision, StringComparison.Ordinal) + shortRevision.Length + 1;
var endIndex = downloadUrl.Length - startIndex;
return downloadUrl.Replace(downloadUrl.Substring(startIndex, endIndex),
var assistantUrl = downloadUrl.Replace(downloadUrl.Substring(startIndex, endIndex),
$"UnityDownloadAssistant-{unityVersion}.exe");
using (var assistantResponse = await Client.GetAsync(assistantUrl))
{
if (assistantResponse.IsSuccessStatusCode)
{
Console.WriteLine("Assistant download URL found.");
return assistantUrl;
}
else
{
Console.WriteLine("Assistant download URL not found, returning original download URL.");
return downloadUrl;
}
}
}
else
{
Console.WriteLine("ShortRevision not found, returning original download URL.");
return downloadUrl;
}
}
else
{
Console.WriteLine("No download URL found.");
return downloadUrl;
}
}

Console.WriteLine($"No download URL found for version {unityVersion}");
Expand Down Expand Up @@ -145,7 +161,7 @@ private static async Task<UnityVersionResponse> FetchBatch(int offset)

try
{
var response = await httpClient.GetStringAsync(url);
var response = await Client.GetStringAsync(url);
return JsonSerializer.Deserialize<UnityVersionResponse>(response);
}
catch (Exception e)
Expand Down
2 changes: 1 addition & 1 deletion UnityLauncherPro/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ private void BtnDownloadInBrowser_Click(object sender, RoutedEventArgs e)
private void BtnDownloadInBrowserFull_Click(object sender, RoutedEventArgs e)
{
var unity = GetSelectedUpdate();
Tools.DownloadInBrowser(unity?.Version, true);
Tools.DownloadInBrowser(unity?.Version);
}

private void btnDownloadInstallUpdate_Click(object sender, RoutedEventArgs e)
Expand Down
11 changes: 7 additions & 4 deletions UnityLauncherPro/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -659,14 +659,14 @@ public static void OpenURL(string url)
Process.Start(url);
}

public static async void DownloadInBrowser(string version, bool preferFullInstaller = false)
public static async void DownloadInBrowser(string version)
{
if (version == null) return;
string exeURL = await GetUnityUpdates.FetchDownloadUrl(version, preferFullInstaller);
string exeURL = await GetUnityUpdates.FetchDownloadUrl(version);

Console.WriteLine("DownloadInBrowser exeURL= '" + exeURL + "'");

if (string.IsNullOrEmpty(exeURL) == false && exeURL.StartsWith("https") == true)
if (string.IsNullOrEmpty(exeURL) == false && exeURL.StartsWith("https"))
{
//SetStatus("Download installer in browser: " + exeURL);
Process.Start(exeURL);
Expand Down Expand Up @@ -2255,7 +2255,10 @@ private static async Task<bool> DownloadFileAsync(string fileUrl, string destina
}
finally
{
DeleteTempFile(destinationPath);
if (!result)
{
DeleteTempFile(destinationPath);
}
progressWindow.Close();
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion UnityLauncherPro/UpgradeWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private void BtnOpenReleasePage_Click(object sender, RoutedEventArgs e)

private void BtnDownloadEditor_Click(object sender, RoutedEventArgs e)
{
Tools.DownloadInBrowser(txtCurrentVersion.Text, true);
Tools.DownloadInBrowser(txtCurrentVersion.Text);
}

private void BtnDownload_Click(object sender, RoutedEventArgs e)
Expand Down

0 comments on commit 64cc970

Please sign in to comment.