Esempio n. 1
0
        private static async Task <BeatSaverApiResponse> GetResponse(string url, bool showNotification = true, int retries = 3)
        {
            if (retries == 0)
            {
                ModAssistant.Utils.Log($"Max tries reached: Skipping {url}", "ERROR");
                Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:RatelimitSkip"), url)}");
                App.CloseWindowOnFinish = false;
                throw new Exception("Max retries allowed");
            }

            BeatSaverApiResponse response = new BeatSaverApiResponse();

            try
            {
                var resp = await HttpClient.GetAsync(url);

                response.statusCode = resp.StatusCode;
                response.ratelimit  = GetRatelimit(resp.Headers);
                string body = await resp.Content.ReadAsStringAsync();

                if ((int)resp.StatusCode == 429)
                {
                    Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:RatelimitHit"), response.ratelimit.ResetTime.ToLocalTime())}");
                    await response.ratelimit.Wait();

                    return(await GetResponse(url, showNotification, retries - 1));
                }

                if (response.statusCode == HttpStatusCode.OK)
                {
                    response.map = JsonSerializer.Deserialize <BeatSaverApiResponseMap>(body);
                    return(response);
                }
                else
                {
                    Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:Failed"), url)}");
                    App.CloseWindowOnFinish = false;
                    return(response);
                }
            }
            catch (Exception e)
            {
                if (showNotification)
                {
                    MessageBox.Show($"{Application.Current.FindResource("OneClick:MapDownloadFailed")}\n\n" + e);
                }
                return(null);
            }
        }
Esempio n. 2
0
        private static async Task <BeatSaverMap> GetMap(string id, string type, bool showNotification)
        {
            string urlSegment;

            switch (type)
            {
            case "hash":
                urlSegment = "/api/maps/by-hash/";
                break;

            case "key":
                urlSegment = "/api/maps/detail/";
                break;

            default:
                return(null);
            }

            BeatSaverMap map = new BeatSaverMap
            {
                Success = false
            };

            if (showNotification)
            {
                Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:Installing"), id)}");
            }
            try
            {
                BeatSaverApiResponse beatsaver = await GetResponse(BeatSaverURLPrefix + urlSegment + id);

                if (beatsaver != null && beatsaver.map != null)
                {
                    map.response = beatsaver;
                    map.Name     = await InstallMap(beatsaver.map, showNotification);

                    map.Success = true;
                }
            }
            catch (Exception e)
            {
                ModAssistant.Utils.Log($"Failed downloading BeatSaver map: {id} | Error: {e.Message}", "ERROR");
                Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:Failed"), (map.Name ?? id))}");
                App.CloseWindowOnFinish = false;
            }
            return(map);
        }
Esempio n. 3
0
        public static async Task InstallMap(BeatSaverApiResponse Map)
        {
            string zip     = Path.Combine(Utils.BeatSaberPath, CustomSongsFolder, Map.hash) + ".zip";
            string mapName = string.Concat(($"{Map.key} ({Map.metadata.songName} - {Map.metadata.levelAuthorName})")
                                           .Split(ModAssistant.Utils.Constants.IllegalCharacters));
            string directory = Path.Combine(Utils.BeatSaberPath, CustomSongsFolder, mapName);

            if (BypassDownloadCounter)
            {
                await Utils.DownloadAsset(BeatSaverURLPrefix + Map.directDownload, CustomSongsFolder, Map.hash + ".zip", mapName);
            }
            else
            {
                await Utils.DownloadAsset(BeatSaverURLPrefix + Map.downloadURL, CustomSongsFolder, Map.hash + ".zip", mapName);
            }

            if (File.Exists(zip))
            {
                using (FileStream stream = new FileStream(zip, FileMode.Open))
                    using (ZipArchive archive = new ZipArchive(stream))
                    {
                        foreach (ZipArchiveEntry file in archive.Entries)
                        {
                            string fileDirectory = Path.GetDirectoryName(Path.Combine(directory, file.FullName));
                            if (!Directory.Exists(fileDirectory))
                            {
                                Directory.CreateDirectory(fileDirectory);
                            }

                            if (!string.IsNullOrEmpty(file.Name))
                            {
                                file.ExtractToFile(Path.Combine(directory, file.FullName), true);
                            }
                        }
                    }

                File.Delete(zip);
            }
            else
            {
                string line1 = (string)Application.Current.FindResource("OneClick:SongDownload:Failed");
                string line2 = (string)Application.Current.FindResource("OneClick:SongDownload:NetworkIssues");
                string title = (string)Application.Current.FindResource("OneClick:SongDownload:FailedTitle");
                MessageBox.Show($"{line1}\n{line2}", title);
            }
        }
Esempio n. 4
0
        private static async Task <BeatSaverMap> GetMap(string id, string type, bool showNotification)
        {
            string urlSegment;

            switch (type)
            {
            case "hash":
                urlSegment = "/maps/hash/";
                break;

            case "key":
                urlSegment = "/maps/id/";
                break;

            default:
                return(null);
            }

            BeatSaverMap map = new BeatSaverMap
            {
                Success = false
            };

            if (showNotification)
            {
                Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:Installing"), id)}");
            }
            try
            {
                BeatSaverApiResponse beatsaver = await GetResponse(BeatSaverURLPrefix + urlSegment + id);

                if (beatsaver != null && beatsaver.map != null)
                {
                    map.response = beatsaver;
                    if (type == "hash")
                    {
                        map.HashToDownload = id.ToLower();
                    }
                    else
                    {
                        BeatSaverApiResponseMap.BeatsaverMapVersion mapVersion = null;
                        foreach (var version in map.response.map.versions)
                        {
                            if (mapVersion == null || version.createdAt > mapVersion.createdAt)
                            {
                                mapVersion = version;
                            }
                        }
                        map.HashToDownload = mapVersion.hash;
                    }
                    map.Name = await InstallMap(map, showNotification);

                    map.Success = true;
                }
            }
            catch (Exception e)
            {
                ModAssistant.Utils.Log($"Failed downloading BeatSaver map: {id} | Error: {e.Message}", "ERROR");
                Utils.SetMessage($"{string.Format((string)Application.Current.FindResource("OneClick:Failed"), (map.Name ?? id))}");
                App.CloseWindowOnFinish = false;
            }
            return(map);
        }
Esempio n. 5
0
        public static async Task GetFromHash(string Hash)
        {
            BeatSaverApiResponse Map = await GetResponse(BeatSaverURLPrefix + "/api/maps/by-hash/" + Hash);

            await InstallMap(Map);
        }
Esempio n. 6
0
        public static async Task GetFromKey(string Key)
        {
            BeatSaverApiResponse Map = await GetResponse(BeatSaverURLPrefix + "/api/maps/detail/" + Key);

            await InstallMap(Map);
        }