Skip to content

Commit 925ab87

Browse files
committed
attempt to fix unofficial download url (when it has been cached to local json) fixes #173, catch UAC declined error,
1 parent e2e9b49 commit 925ab87

File tree

2 files changed

+89
-23
lines changed

2 files changed

+89
-23
lines changed

UnityLauncherPro/GetUnityUpdates.cs

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public static class GetUnityUpdates
1616
private const int DelayBetweenBatches = 1000; // 1 second in milliseconds
1717
private const string CacheFileName = "UnityVersionCache.json";
1818
private static readonly HttpClient Client = new HttpClient();
19+
public static string unofficalReleasesURL = "https://raw.githubusercontent.com/unitycoder/UnofficialUnityReleasesWatcher/refs/heads/main/unity-releases.md";
1920

2021
static Dictionary<string, string> unofficialReleaseURLs = new Dictionary<string, string>();
2122

@@ -30,15 +31,19 @@ public static async Task<List<UnityVersion>> FetchAll(bool useUnofficialList = f
3031
{
3132
var unofficialVersions = await FetchUnofficialVersions(cachedVersions);
3233
unofficialReleaseURLs.Clear();
33-
// TODO modify FetchUnofficialVersions to put items in this dictionary directlys
34+
35+
// TODO modify FetchUnofficialVersions to put items in this dictionary directly
3436
foreach (var version in unofficialVersions)
3537
{
36-
//Console.WriteLine("unofficial: " + version.Version + " , " + version.directURL);
38+
Console.WriteLine("version in unofficials: " + version);
39+
// add only if not there yet
3740
if (unofficialReleaseURLs.ContainsKey(version.Version) == false)
3841
{
42+
Console.WriteLine("added unofficial: " + version.Version + " , " + version.directURL);
3943
unofficialReleaseURLs.Add(version.Version, version.directURL);
4044
}
4145
}
46+
// combine
4247
allVersions = unofficialVersions.Concat(allVersions).ToList();
4348
}
4449

@@ -58,23 +63,28 @@ public static async Task<List<UnityVersion>> FetchUnofficialVersions(List<UnityV
5863

5964
try
6065
{
61-
string url = "https://raw.githubusercontent.com/unitycoder/UnofficialUnityReleasesWatcher/refs/heads/main/unity-releases.md";
62-
63-
var content = await Client.GetStringAsync(url);
66+
var content = await Client.GetStringAsync(unofficalReleasesURL);
6467

6568
// Parse the Markdown content
6669
var lines = content.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
6770
foreach (var line in lines)
6871
{
72+
//Console.WriteLine(line);
73+
6974
if (line.StartsWith("- ")) // Identify Markdown list items
7075
{
71-
var urlPart = line.Substring(2).Trim();
76+
var urlPart = line.Substring(4).Trim(); // bug in server, has double -
7277
var version = ExtractVersionFromUrl(urlPart);
7378

74-
if (!string.IsNullOrEmpty(version) && !existingVersions.Contains(version))
79+
//Console.WriteLine("VERSION: " + version);
80+
//Console.WriteLine("IsNullOrEmpty: " + (string.IsNullOrEmpty(version)) + ", existingVersions:" + (existingVersions.Contains(version)));
81+
82+
if (string.IsNullOrEmpty(version) == false && existingVersions.Contains(version) == false)
7583
{
7684
var stream = InferStreamFromVersion(version);
7785

86+
Console.WriteLine(version + " , " + urlPart);
87+
7888
unofficialVersions.Add(new UnityVersion
7989
{
8090
Version = version,
@@ -148,28 +158,25 @@ public static async Task<string> FetchDownloadUrl(string unityVersion)
148158
}
149159
}
150160

151-
static string ParseHashCodeFromURL(string url)
152-
{
153-
// https://beta.unity3d.com/download/330fbefc18b7/download.html#6000.1.0a8 > 330fbefc18b7
154-
155-
int hashStart = url.IndexOf("download/") + 9;
156-
int hashEnd = url.IndexOf("/download.html", hashStart);
157-
return url.Substring(hashStart, hashEnd - hashStart);
158-
}
159-
160161
private static async Task<string> ExtractDownloadUrlAsync(string json, string unityVersion)
161162
{
162163
//Console.WriteLine("json: " + json + " vers: " + unityVersion);
163164

164165
if (json.Contains("\"results\":[]"))
165166
{
166-
Console.WriteLine("No results found from releases API, checking unofficial list (if enabled)");
167+
Console.WriteLine("No results found from releases API, checking unofficial list (if enabled), v=" + unityVersion);
168+
169+
// print unofficialReleaseURLs
170+
foreach (var kvp in unofficialReleaseURLs)
171+
{
172+
Console.WriteLine($"Unofficial: {kvp.Key} => {kvp.Value}");
173+
}
167174

168175
if (unofficialReleaseURLs.ContainsKey(unityVersion))
169176
{
170-
Console.WriteLine("Unofficial release found in the list.");
177+
Console.WriteLine("Unofficial release found in the list: ");
171178

172-
string unityHash = ParseHashCodeFromURL(unofficialReleaseURLs[unityVersion]);
179+
string unityHash = Tools.ParseHashCodeFromURL(unofficialReleaseURLs[unityVersion]);
173180
// Console.WriteLine(unityHash);
174181
string downloadURL = Tools.ParseDownloadURLFromWebpage(unityVersion, unityHash, false, true);
175182
// Console.WriteLine("direct download url: "+downloadURL);
@@ -385,6 +392,8 @@ private static List<UnityVersion> LoadCachedVersions()
385392
string cacheFilePath = Path.Combine(configDirectory, CacheFileName);
386393
if (!File.Exists(cacheFilePath)) return new List<UnityVersion>();
387394

395+
Console.WriteLine("cache file: " + cacheFilePath);
396+
388397
string json = File.ReadAllText(cacheFilePath);
389398
return ParseCachedUnityVersions(json);
390399
}
@@ -406,5 +415,27 @@ private static void SaveCachedVersions(List<UnityVersion> versions)
406415
//Console.WriteLine("Saving cachedrelease: " + cacheFilePath);
407416
File.WriteAllText(cacheFilePath, json);
408417
}
418+
419+
internal static async Task<string> CheckUnofficialVersionList(string version)
420+
{
421+
// fetch md page from unofficalReleasesURL
422+
var content = await Client.GetStringAsync(unofficalReleasesURL);
423+
// parse lines
424+
var lines = content.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
425+
foreach (var line in lines)
426+
{
427+
if (line.StartsWith("- ")) // Identify Markdown list items
428+
{
429+
var urlPart = line.Substring(4).Trim(); // bug in server, has double -
430+
var foundVersion = ExtractVersionFromUrl(urlPart);
431+
if (foundVersion == version)
432+
{
433+
Console.WriteLine("Found unofficial version in the list: " + foundVersion + " , " + urlPart);
434+
return urlPart;
435+
}
436+
}
437+
}
438+
return null;
439+
}
409440
}
410441
}

UnityLauncherPro/Tools.cs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,14 @@ public static async void DownloadInBrowser(string version, bool preferFullInstal
696696
// https://beta.unity3d.com/download/330fbefc18b7/UnityDownloadAssistant-6000.1.0a8.exe
697697
if (exeURL == null)
698698
{
699-
Console.WriteLine("TODO DownloadInBrowser ,v=" + version);
699+
// exe url not found, try unofficial list (TODO this is hack to avoid null url, because unofficial list items have been cached to local json, without download url..)
700+
Console.WriteLine("Fixing null in DownloadInBrowser ,v=" + version);
701+
var downloadURL = await GetUnityUpdates.CheckUnofficialVersionList(version);
702+
if (string.IsNullOrEmpty(downloadURL) == false)
703+
{
704+
string unityHash = ParseHashCodeFromURL(downloadURL);
705+
exeURL = ParseDownloadURLFromWebpage(version, unityHash, false, true);
706+
}
700707
return;
701708
}
702709

@@ -722,12 +729,26 @@ public static async void DownloadInBrowser(string version, bool preferFullInstal
722729

723730
public static async void DownloadAndInstall(string version)
724731
{
732+
string exeURL = await GetUnityUpdates.FetchDownloadUrl(version);
733+
725734
if (version == null)
726735
{
727736
Console.WriteLine("Error> Cannot download and install null version");
728737
return;
729738
}
730-
string exeURL = await GetUnityUpdates.FetchDownloadUrl(version);
739+
740+
if (string.IsNullOrEmpty(exeURL) == true)
741+
{
742+
// exe url not found, try unofficial list (TODO this is hack to avoid null url, because unofficial list items have been cached to local json, without download url..)
743+
Console.WriteLine("Fixing null in DownloadInBrowser ,v=" + version);
744+
var downloadURL = await GetUnityUpdates.CheckUnofficialVersionList(version);
745+
if (string.IsNullOrEmpty(downloadURL) == false)
746+
{
747+
string unityHash = ParseHashCodeFromURL(downloadURL);
748+
exeURL = ParseDownloadURLFromWebpage(version, unityHash, false, true);
749+
}
750+
751+
}
731752

732753
Console.WriteLine("download exeURL= (" + exeURL + ")");
733754

@@ -762,11 +783,18 @@ public static async void DownloadAndInstall(string version)
762783
process.Exited += (sender, e) => DeleteTempFile(tempFile);
763784
process.Start();
764785
}
765-
catch (Exception)
786+
catch (Win32Exception ex) when (ex.NativeErrorCode == 1223) // ERROR_CANCELLED
766787
{
767-
Console.WriteLine("Failed to run exe: " + tempFile);
788+
// User declined the UAC prompt
789+
Console.WriteLine("User cancelled elevation (UAC).");
768790
DeleteTempFile(tempFile);
769791
}
792+
catch (Exception ex)
793+
{
794+
Console.WriteLine("Failed to run exe: " + tempFile + " - " + ex.Message);
795+
DeleteTempFile(tempFile);
796+
}
797+
770798
// TODO refresh upgrade dialog after installer finished
771799
}
772800
}
@@ -3094,7 +3122,14 @@ private static UnityEditorInfoLabel ExtractLabelText(string json)
30943122
return label;
30953123
}
30963124

3125+
public static string ParseHashCodeFromURL(string url)
3126+
{
3127+
// https://beta.unity3d.com/download/330fbefc18b7/download.html#6000.1.0a8 > 330fbefc18b7
30973128

3129+
int hashStart = url.IndexOf("download/") + 9;
3130+
int hashEnd = url.IndexOf("/download.html", hashStart);
3131+
return url.Substring(hashStart, hashEnd - hashStart);
3132+
}
30983133

30993134

31003135
} // class

0 commit comments

Comments
 (0)