Example #1
0
        public static async Task <Tuple <string, bool> > GetPaintShopDataAsync([NotNull] string carId, IProgress <AsyncProgressEntry> progress = null,
                                                                               CancellationToken cancellation = default(CancellationToken))
        {
            var file = new FileInfo(FilesStorage.Instance.GetTemporaryFilename("Paint Shop", $"{carId}.zip"));

            if (JustLoadedPaintShopData.Contains(carId) && file.Exists)
            {
                return(Tuple.Create(file.FullName, false));
            }

            var result = await InternalUtils.CmGetPaintShopDataAsync(carId, UserAgent,
                                                                     file.Exists?file.LastWriteTime : (DateTime?)null, progress, cancellation).ConfigureAwait(false);

            if (cancellation.IsCancellationRequested)
            {
                return(null);
            }

            if (result != null && result.Item1.Length != 0)
            {
                Logging.Debug($"Fresh version of {carId} loaded, from {result.Item2?.ToString() ?? "UNKNOWN"}");
                var lastWriteTime = result.Item2 ?? DateTime.Now;
                await FileUtils.WriteAllBytesAsync(file.FullName, result.Item1, cancellation).ConfigureAwait(false);

                file.Refresh();
                file.LastWriteTime = lastWriteTime;
                JustLoadedPaintShopData.Add(carId);
                return(Tuple.Create(file.FullName, true));
            }

            if (!file.Exists)
            {
                return(null);
            }

            Logging.Debug($"Cached {carId} used");
            JustLoadedPaintShopData.Add(carId);
            return(Tuple.Create(file.FullName, false));
        }
Example #2
0
        public static async Task <string[]> GetPaintShopIdsAsync(IProgress <AsyncProgressEntry> progress = null,
                                                                 CancellationToken cancellation          = default(CancellationToken))
        {
            if (_paintShopIds != null)
            {
                return(_paintShopIds);
            }

            var file   = new FileInfo(FilesStorage.Instance.GetTemporaryFilename("Paint Shop", "IDs.json"));
            var result = await InternalUtils.CmGetPaintShopDataAsync(null, UserAgent,
                                                                     file.Exists?file.LastWriteTime : (DateTime?)null, progress, cancellation).ConfigureAwait(false);

            if (cancellation.IsCancellationRequested)
            {
                return(null);
            }

            if (result != null && result.Item1.Length != 0)
            {
                Logging.Debug($"Fresh version of IDs loaded, from {result.Item2?.ToString() ?? "UNKNOWN"}");
                var lastWriteTime = result.Item2 ?? DateTime.Now;
                await FileUtils.WriteAllBytesAsync(file.FullName, result.Item1, cancellation).ConfigureAwait(false);

                file.Refresh();
                file.LastWriteTime = lastWriteTime;
                _paintShopIds      = JsonConvert.DeserializeObject <string[]>(result.Item1.ToUtf8String());
                return(_paintShopIds);
            }

            if (!file.Exists)
            {
                return(null);
            }

            Logging.Debug("Cached IDs used");
            _paintShopIds = JsonConvert.DeserializeObject <string[]>(File.ReadAllText(file.FullName));
            return(_paintShopIds);
        }