|
1 | 1 | using System;
|
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.Diagnostics;
|
3 | 4 | using System.IO;
|
| 5 | +using System.Linq; |
| 6 | +using System.Net; |
4 | 7 | using System.Text;
|
| 8 | +using System.Text.RegularExpressions; |
| 9 | +using System.Windows.Controls; |
| 10 | +using System.Windows.Input; |
5 | 11 |
|
6 | 12 | namespace UnityLauncherPro
|
7 | 13 | {
|
@@ -149,6 +155,244 @@ public static bool LaunchExe(string path)
|
149 | 155 | }
|
150 | 156 |
|
151 | 157 |
|
| 158 | + public static string GetUnityReleaseURL(string version) |
| 159 | + { |
| 160 | + string url = ""; |
| 161 | + if (VersionIsArchived(version)) |
| 162 | + { |
| 163 | + // remove f# |
| 164 | + version = Regex.Replace(version, @"f.", "", RegexOptions.IgnoreCase); |
| 165 | + |
| 166 | + string padding = "unity-"; |
| 167 | + string whatsnew = "whats-new"; |
| 168 | + |
| 169 | + if (version.Contains("5.6")) padding = ""; |
| 170 | + if (version.Contains("2017.1")) whatsnew = "whatsnew"; |
| 171 | + if (version.Contains("2018.2")) whatsnew = "whatsnew"; |
| 172 | + if (version.Contains("2018.3")) padding = ""; |
| 173 | + if (version.Contains("2018.1")) whatsnew = "whatsnew"; // doesnt work |
| 174 | + if (version.Contains("2017.4.")) padding = ""; // doesnt work for all versions |
| 175 | + if (version.Contains("2018.4.")) padding = ""; |
| 176 | + if (version.Contains("2019")) padding = ""; |
| 177 | + url = "https://unity3d.com/unity/" + whatsnew + "/" + padding + version; |
| 178 | + } |
| 179 | + else |
| 180 | + if (VersionIsPatch(version)) |
| 181 | + { |
| 182 | + url = "https://unity3d.com/unity/qa/patch-releases/" + version; |
| 183 | + } |
| 184 | + else |
| 185 | + if (VersionIsBeta(version)) |
| 186 | + { |
| 187 | + url = "https://unity3d.com/unity/beta/" + version; |
| 188 | + } |
| 189 | + else |
| 190 | + if (VersionIsAlpha(version)) |
| 191 | + { |
| 192 | + url = "https://unity3d.com/unity/alpha/" + version; |
| 193 | + } |
| 194 | + |
| 195 | + Console.WriteLine(url); |
| 196 | + |
| 197 | + return url; |
| 198 | + } |
| 199 | + |
| 200 | + // if version contains *f* its archived version |
| 201 | + public static bool VersionIsArchived(string version) |
| 202 | + { |
| 203 | + return version.Contains("f"); |
| 204 | + } |
| 205 | + |
| 206 | + public static bool VersionIsPatch(string version) |
| 207 | + { |
| 208 | + return version.Contains("p"); |
| 209 | + } |
| 210 | + |
| 211 | + public static bool VersionIsBeta(string version) |
| 212 | + { |
| 213 | + return version.Contains("b"); |
| 214 | + } |
| 215 | + |
| 216 | + public static bool VersionIsAlpha(string version) |
| 217 | + { |
| 218 | + return version.Contains("a"); |
| 219 | + } |
| 220 | + |
| 221 | + // open release notes page in browser |
| 222 | + public static bool OpenReleaseNotes(string version) |
| 223 | + { |
| 224 | + bool result = false; |
| 225 | + var url = Tools.GetUnityReleaseURL(version); |
| 226 | + if (string.IsNullOrEmpty(url) == false) |
| 227 | + { |
| 228 | + Process.Start(url); |
| 229 | + result = true; |
| 230 | + } |
| 231 | + else |
| 232 | + { |
| 233 | + } |
| 234 | + return result; |
| 235 | + } |
| 236 | + |
| 237 | + public static void DownloadInBrowser(string url, string version) |
| 238 | + { |
| 239 | + string exeURL = ParseDownloadURLFromWebpage(version); |
| 240 | + |
| 241 | + if (string.IsNullOrEmpty(exeURL) == false) |
| 242 | + { |
| 243 | + //SetStatus("Download installer in browser: " + exeURL); |
| 244 | + Process.Start(exeURL); |
| 245 | + } |
| 246 | + else // not found |
| 247 | + { |
| 248 | + //SetStatus("Error> Cannot find installer executable ... opening website instead"); |
| 249 | + url = "https://unity3d.com/get-unity/download/archive"; |
| 250 | + Process.Start(url + "#installer-not-found---version-" + version); |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + // parse Unity installer exe from release page |
| 255 | + // thanks to https://github.com/softfruit |
| 256 | + static string ParseDownloadURLFromWebpage(string version) |
| 257 | + { |
| 258 | + string url = ""; |
| 259 | + |
| 260 | + using (WebClient client = new WebClient()) |
| 261 | + { |
| 262 | + // get correct page url |
| 263 | + string website = "https://unity3d.com/get-unity/download/archive"; |
| 264 | + if (Tools.VersionIsPatch(version)) website = "https://unity3d.com/unity/qa/patch-releases"; |
| 265 | + if (Tools.VersionIsBeta(version)) website = "https://unity3d.com/unity/beta/" + version; |
| 266 | + if (Tools.VersionIsAlpha(version)) website = "https://unity3d.com/unity/alpha/" + version; |
| 267 | + |
| 268 | + // download html |
| 269 | + string sourceHTML = client.DownloadString(website); |
| 270 | + string[] lines = sourceHTML.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); |
| 271 | + |
| 272 | + // patch version download assistant finder |
| 273 | + if (Tools.VersionIsPatch(version)) |
| 274 | + { |
| 275 | + for (int i = 0; i < lines.Length; i++) |
| 276 | + { |
| 277 | + if (lines[i].Contains("UnityDownloadAssistant-" + version + ".exe")) |
| 278 | + { |
| 279 | + int start = lines[i].IndexOf('"') + 1; |
| 280 | + int end = lines[i].IndexOf('"', start); |
| 281 | + url = lines[i].Substring(start, end - start); |
| 282 | + break; |
| 283 | + } |
| 284 | + } |
| 285 | + } |
| 286 | + else if (Tools.VersionIsArchived(version)) |
| 287 | + { |
| 288 | + // archived version download assistant finder |
| 289 | + for (int i = 0; i < lines.Length; i++) |
| 290 | + { |
| 291 | + // find line where full installer is (from archive page) |
| 292 | + if (lines[i].Contains("UnitySetup64-" + version)) |
| 293 | + { |
| 294 | + // take previous line, which contains download assistant url |
| 295 | + string line = lines[i - 1]; |
| 296 | + int start = line.IndexOf('"') + 1; |
| 297 | + int end = line.IndexOf('"', start); |
| 298 | + url = @"https://unity3d.com" + line.Substring(start, end - start); |
| 299 | + break; |
| 300 | + } |
| 301 | + } |
| 302 | + } |
| 303 | + else // alpha or beta version download assistant finder |
| 304 | + { |
| 305 | + for (int i = 0; i < lines.Length; i++) |
| 306 | + { |
| 307 | + if (lines[i].Contains("UnityDownloadAssistant.exe")) |
| 308 | + { |
| 309 | + int start = lines[i].IndexOf('"') + 1; |
| 310 | + int end = lines[i].IndexOf('"', start); |
| 311 | + url = lines[i].Substring(start, end - start) + "#version=" + version; |
| 312 | + break; |
| 313 | + } |
| 314 | + } |
| 315 | + } |
| 316 | + } |
| 317 | + |
| 318 | + // didnt find installer |
| 319 | + if (string.IsNullOrEmpty(url)) |
| 320 | + { |
| 321 | + //SetStatus("Cannot find UnityDownloadAssistant.exe for this version."); |
| 322 | + } |
| 323 | + return url; |
| 324 | + } |
| 325 | + |
| 326 | + |
| 327 | + public static string FindNearestVersion(string currentVersion, List<string> allAvailable) |
| 328 | + { |
| 329 | + if (currentVersion.Contains("2019")) |
| 330 | + { |
| 331 | + return FindNearestVersionFromSimilarVersions(currentVersion, allAvailable.Where(x => x.Contains("2019"))); |
| 332 | + } |
| 333 | + if (currentVersion.Contains("2018")) |
| 334 | + { |
| 335 | + return FindNearestVersionFromSimilarVersions(currentVersion, allAvailable.Where(x => x.Contains("2018"))); |
| 336 | + } |
| 337 | + if (currentVersion.Contains("2017")) |
| 338 | + { |
| 339 | + return FindNearestVersionFromSimilarVersions(currentVersion, allAvailable.Where(x => x.Contains("2017"))); |
| 340 | + } |
| 341 | + return FindNearestVersionFromSimilarVersions(currentVersion, allAvailable.Where(x => !x.Contains("2017"))); |
| 342 | + } |
| 343 | + |
| 344 | + private static string FindNearestVersionFromSimilarVersions(string version, IEnumerable<string> allAvailable) |
| 345 | + { |
| 346 | + Dictionary<string, string> stripped = new Dictionary<string, string>(); |
| 347 | + var enumerable = allAvailable as string[] ?? allAvailable.ToArray(); |
| 348 | + |
| 349 | + foreach (var t in enumerable) |
| 350 | + { |
| 351 | + stripped.Add(new Regex("[a-zA-z]").Replace(t, "."), t); |
| 352 | + } |
| 353 | + |
| 354 | + var comparableVersion = new Regex("[a-zA-z]").Replace(version, "."); |
| 355 | + if (!stripped.ContainsKey(comparableVersion)) |
| 356 | + { |
| 357 | + stripped.Add(comparableVersion, version); |
| 358 | + } |
| 359 | + |
| 360 | + var comparables = stripped.Keys.OrderBy(x => x).ToList(); |
| 361 | + var actualIndex = comparables.IndexOf(comparableVersion); |
| 362 | + |
| 363 | + if (actualIndex < stripped.Count - 1) return stripped[comparables[actualIndex + 1]]; |
| 364 | + return null; |
| 365 | + } |
| 366 | + |
| 367 | + // https://stackoverflow.com/a/1619103/5452781 |
| 368 | + public static KeyValuePair<TKey, TValue> GetEntry<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) |
| 369 | + { |
| 370 | + return new KeyValuePair<TKey, TValue>(key, dictionary[key]); |
| 371 | + } |
| 372 | + |
| 373 | + public static void HandleDataGridScrollKeys(object sender, KeyEventArgs e) |
| 374 | + { |
| 375 | + DataGrid grid = sender as DataGrid; |
| 376 | + switch (e.Key) |
| 377 | + { |
| 378 | + case Key.Up: |
| 379 | + if (grid.SelectedIndex > 0) |
| 380 | + { |
| 381 | + grid.SelectedIndex--; |
| 382 | + } |
| 383 | + else |
| 384 | + { |
| 385 | + grid.SelectedIndex = grid.Items.Count - 1; |
| 386 | + } |
| 387 | + e.Handled = true; |
| 388 | + break; |
| 389 | + case Key.Down: |
| 390 | + grid.SelectedIndex = ++grid.SelectedIndex % grid.Items.Count; |
| 391 | + e.Handled = true; |
| 392 | + break; |
| 393 | + } |
| 394 | + grid.ScrollIntoView(grid.Items[grid.SelectedIndex]); |
| 395 | + } |
152 | 396 |
|
153 | 397 | } // class
|
154 | 398 | } // namespace
|
0 commit comments