Exemple #1
0
        public static void GetDownloadLinks(string domain, int modId, int fileId, Action <ModFileDownloadLink[]> doAfter)
        {
            var file = Path.Combine(_cache, $"links_{domain}_{modId}_{fileId}.json");

            if (File.Exists(file))
            {
                var dt    = File.GetCreationTimeUtc(file);
                var diff  = DateTime.UtcNow - dt;
                var useIt = false;
                if (diff.Hours < 6)
                {
                    useIt = true;
                }
                else
                {
                    if (RateLimits.IsBlocked())
                    {
                        var tillReset = RateLimits.GetTimeUntilRenewal();
                        if (tillReset.TotalSeconds > 10)
                        {
                            useIt = true;
                        }
                    }
                }

                if (useIt)
                {
                    doAfter?.Invoke(JsonConvert.DeserializeObject <ModFileDownloadLink[]>(File.ReadAllText(file)));
                    return;
                }
                File.Delete(file);
            }

            DoGetDownloadLinks(domain, modId, fileId, file).ContinueWith(x => doAfter?.Invoke(x.Result));
        }
Exemple #2
0
        public static Mod GetModSync(string domain, int modId)
        {
            var file = Path.Combine(_cache, $"mod_{domain}_{modId}.json");

            if (File.Exists(file))
            {
                var dt    = File.GetCreationTimeUtc(file);
                var diff  = DateTime.UtcNow - dt;
                var useIt = false;
                if (diff.Hours < 24)
                {
                    useIt = true;
                }
                else
                {
                    if (RateLimits.IsBlocked())
                    {
                        var tillReset = RateLimits.GetTimeUntilRenewal();
                        if (tillReset.TotalSeconds > 10)
                        {
                            useIt = true;
                        }
                    }
                }

                if (useIt)
                {
                    return(JsonConvert.DeserializeObject <Mod>(File.ReadAllText(file)));
                }

                File.Delete(file);
            }

            if (RateLimits.IsBlocked())
            {
                var renewDelay = RateLimits.GetTimeUntilRenewal();
                if (renewDelay.TotalMilliseconds > 0)
                {
                    Thread.Sleep(renewDelay);
                }
            }

            Mod mod;

            try
            {
                mod = _throttle.Queue(() => _client.GetMod(domain, modId).Result).Result;
            }
            catch
            {
                return(null);
            }

            File.WriteAllText(file, JsonConvert.SerializeObject(mod, Formatting.None));
            return(mod);
        }
Exemple #3
0
        /*public static Task<ModFileDownloadLink[]> GetDownloadLinks(string domain, int modId, int fileId)
         * {
         *  var file = Path.Combine(_cache, $"links_{domain}_{modId}_{fileId}.json");
         *  if (File.Exists(file))
         *  {
         *      var dt = File.GetCreationTimeUtc(file);
         *      var diff = DateTime.UtcNow - dt;
         *      var useIt = false;
         *      if (diff.Hours < 6)
         *      {
         *          useIt = true;
         *      }
         *      else
         *      {
         *          if (RateLimits.IsBlocked())
         *          {
         *              var tillReset = RateLimits.GetTimeUntilRenewal();
         *              if (tillReset.TotalSeconds > 10)
         *              {
         *                  useIt = true;
         *              }
         *          }
         *      }
         *
         *      if (useIt)
         *      {
         *          return Task.FromResult(JsonConvert.DeserializeObject<ModFileDownloadLink[]>(File.ReadAllText(file)));
         *      }
         *      File.Delete(file);
         *  }
         *
         *  return DoGetDownloadLinks(domain, modId, fileId, file);
         * }*/

        private static async Task <ModFileDownloadLink[]> DoGetDownloadLinks(string domain, int modId, int fileId, string file)
        {
            if (RateLimits.IsBlocked())
            {
                var renewDelay = RateLimits.GetTimeUntilRenewal();
                if (renewDelay.TotalMilliseconds > 0)
                {
                    await Task.Delay(renewDelay);

                    return(await DoGetDownloadLinks(domain, modId, fileId, file));
                }
            }

            var links = await(await _throttle.Queue(async() => await _client.GetDownloadLinks(domain, modId, fileId)));

            File.WriteAllText(file, JsonConvert.SerializeObject(links, Formatting.None));
            return(links);
        }
Exemple #4
0
        private static Task <ModFile[]> GetModFiles(string domain, int modId)
        {
            var file = Path.Combine(_cache, $"modfiles_{domain}_{modId}.json");

            if (!File.Exists(file))
            {
                return(DoGetModFiles(domain, modId, file));
            }
            var dt    = File.GetCreationTimeUtc(file);
            var diff  = DateTime.UtcNow - dt;
            var useIt = false;

            if (diff.Hours < 12)
            {
                useIt = true;
            }
            else
            {
                if (RateLimits.IsBlocked())
                {
                    var tillReset = RateLimits.GetTimeUntilRenewal();
                    if (tillReset.TotalSeconds > 10)
                    {
                        useIt = true;
                    }
                }
            }

            if (useIt)
            {
                return(Task.FromResult(JsonConvert.DeserializeObject <ModFile[]>(File.ReadAllText(file))));
            }

            File.Delete(file);

            return(DoGetModFiles(domain, modId, file));
        }