private static IEnumerator DownloadSong_internal(string hash, bool refreshWhenDownloaded = true, Action <string, bool> songDownloaded = null, Action <string, float> downloadProgressChanged = null, string customHostUrl = null) { var songUrl = $"{beatSaverDownloadUrl}{hash}.zip"; if (!string.IsNullOrEmpty(customHostUrl)) { songUrl = $"{customHostUrl}{hash.ToUpper()}.zip"; } UnityWebRequest www = UnityWebRequest.Get(songUrl); bool timeout = false; float time = 0f; float lastProgress = 0f; www.SetRequestHeader("user-agent", SharedConstructs.Name); UnityWebRequestAsyncOperation asyncRequest = www.SendWebRequest(); while (!asyncRequest.isDone || asyncRequest.progress < 1f) { yield return(null); time += Time.deltaTime; if (time >= 15f && asyncRequest.progress == 0f) { www.Abort(); timeout = true; } if (lastProgress != asyncRequest.progress) { lastProgress = asyncRequest.progress; downloadProgressChanged?.Invoke($"custom_level_{hash.ToUpper()}", asyncRequest.progress); } } if (www.isNetworkError || www.isHttpError || timeout) { Logger.Error($"Error downloading song {hash}: {www.error}"); songDownloaded?.Invoke($"custom_level_{hash.ToUpper()}", false); } else { string zipPath = ""; string customSongsPath = CustomLevelPathHelper.customLevelsDirectoryPath; string customSongPath = ""; byte[] data = www.downloadHandler.data; try { customSongPath = customSongsPath + "/" + hash + "/"; zipPath = customSongPath + hash + ".zip"; if (!Directory.Exists(customSongPath)) { Directory.CreateDirectory(customSongPath); } File.WriteAllBytes(zipPath, data); } catch (Exception e) { Logger.Error($"Error writing zip: {e}"); songDownloaded?.Invoke($"custom_level_{hash.ToUpper()}", false); yield break; } try { ZipFile.ExtractToDirectory(zipPath, customSongPath); } catch (Exception e) { Logger.Error($"Unable to extract ZIP! Exception: {e}"); songDownloaded?.Invoke($"custom_level_{hash.ToUpper()}", false); yield break; } try { File.Delete(zipPath); } catch (IOException e) { Logger.Warning($"Unable to delete zip! Exception: {e}"); yield break; } Logger.Success($"Downloaded!"); if (refreshWhenDownloaded) { Action <Loader, ConcurrentDictionary <string, CustomPreviewBeatmapLevel> > songsLoaded = null; songsLoaded = (_, __) => { Loader.SongsLoadedEvent -= songsLoaded; songDownloaded?.Invoke($"custom_level_{hash.ToUpper()}", true); }; Loader.SongsLoadedEvent += songsLoaded; Loader.Instance.RefreshSongs(false); } else { songDownloaded?.Invoke($"custom_level_{hash.ToUpper()}", true); } } }
public static async Task <bool> AttemptAutoUpdate() { string CurrentFilename; if (osType.Contains("Unix")) { CurrentFilename = linuxFilename; } else if (osType.Contains("Windows")) { CurrentFilename = WindowsFilename; } else { Logger.Error($"AutoUpdater does not support your operating system. Detected Operating system is: {osType}. Supported are: Unix, Windows"); return(false); } Uri URI = await GetExecutableURI(CurrentFilename); if (URI == null) { Logger.Error($"AutoUpdate resource not found. Please update manually from: {repoURL}"); return(false); } //Delete any .old executables, if there are any. File.Delete($"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}{CurrentFilename}.old"); //Rename current executable to .old File.Move($"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}{CurrentFilename}", $"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}{CurrentFilename}.old"); //Download new executable Logger.Info("Downloading new version..."); await GetExecutableFromURI(URI, CurrentFilename); Logger.Success("New version downloaded sucessfully!"); //Restart as the new version Logger.Info("Attempting to start new version"); if (osType.Contains("Unix")) { Process.Start("chmod", $"+x {Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}{CurrentFilename}"); //This is pretty hacky, but oh well.... } try { using Process newVersion = new(); newVersion.StartInfo.UseShellExecute = true; newVersion.StartInfo.CreateNoWindow = osType.Contains("Unix"); //In linux shell there are no windows - causes an exception newVersion.StartInfo.WindowStyle = ProcessWindowStyle.Normal; newVersion.StartInfo.FileName = $"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}{CurrentFilename}"; newVersion.Start(); } catch (Exception e) { Logger.Error(e.ToString()); Logger.Error($"Failed to start, please start new version manually from shell - downloaded version is saved at {Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}{CurrentFilename}"); return(false); } Logger.Success("Application updated succesfully!!"); return(true); }