Example #1
0
        public async Task <(int Id, byte[] Data)> DownloadAsync(CancellationToken cancellationToken)
        {
            var data = await WebServiceClient.GetChunk(FileId, Id);

            if (!cancellationToken.IsCancellationRequested)
            {
                Finished = true;
            }
            return(Id, data);
        }
Example #2
0
 public async Task GetImages()
 {
     if (begunGettingImages)
     {
         return;
     }
     begunGettingImages = true;
     await WebServiceClient.GetResourceImageBytes(Id,
                                                  new Progress <byte[]>(imageBytes =>
     {
         var bitmapImage = new BitmapImage();
         bitmapImage.BeginInit();
         bitmapImage.StreamSource = new MemoryStream(imageBytes);
         bitmapImage.EndInit();
         Images.Add(bitmapImage);
     }));
 }
Example #3
0
        public async Task InstallAsync()
        {
            UpdateStatus(WorkshopResourceStatus.Installing);
            var(_, dtos) = await WebServiceClient.GetResourceUpdatedFilesAsync(Id);

            foreach (var dto in dtos)
            {
                if ((FileServerStatus)dto.Status == FileServerStatus.Alive)
                {
                    var file = ResourceFileModel.FromDto(dto);
                    file.ResourceId = Id;
                    file.Status     = ResourceFileStatus.BeforeDownload;
                    Files.Add(file);
                }
            }
            DatabaseClient.SaveResourceFiles(Files);
            cts = new CancellationTokenSource();
            await DownloadAsync(cts.Token);
        }
Example #4
0
        public async Task UpdateAsync()
        {
            var(lastFileChange, dtos) = await WebServiceClient.GetResourceUpdatedFilesAsync(Id, LastFileChangeDate);

            var toSave = new List <ResourceFileModel>(dtos.Count);

            foreach (var dto in dtos)
            {
                var file      = Files.FirstOrDefault(l => l.Id == dto.Id);
                var isNew     = file != null;
                var isDeleted = (FileServerStatus)dto.Status == FileServerStatus.Deleted;
                if (isNew && !isDeleted)
                {
                    file            = ResourceFileModel.FromDto(dto);
                    file.ResourceId = Id;
                    file.Status     = ResourceFileStatus.BeforeDownload;
                    Files.Add(file);
                }
                else if (isNew && isDeleted)
                {
                    // do nothing
                }
                else if (!isNew && !isDeleted)
                {
                    file.Sha1       = dto.Sha1;
                    file.Size       = dto.Size;
                    file.UpdateDate = dto.UpdateDate;
                    file.Status     = ResourceFileStatus.BeforeDownload;
                }
                else if (!isNew && isDeleted)
                {
                    file.DeleteFromDisk();
                    DatabaseClient.DeleteFile(file.Id);
                    Files.Remove(file);
                }
                toSave.Add(file);
            }
            DatabaseClient.UpdateResourceLastFileChange(Id, lastFileChange);
            DatabaseClient.SaveResourceFiles(toSave);
            cts = new CancellationTokenSource();
            await DownloadAsync(cts.Token);
        }
Example #5
0
        public static async Task UpdateResources()
        {
            var(timestamp, dtos) = await WebServiceClient.GetUpdatedServerResourcesAsync();

            var toSave = new List <WorkshopResourceModel>();

            foreach (var dto in dtos)
            {
                var resource = Resources.FirstOrDefault(l => l.Id == dto.Id);
                if (resource == null)
                // resource does not exist locally
                {
                    switch (dto.Type)
                    {
                    case WorkshopResourceType.Drs:
                        resource = Mapper.Map <WorkshopResourceDto, DrsResourceModel>(dto);
                        break;

                    case WorkshopResourceType.Mod:
                        resource = Mapper.Map <WorkshopResourceDto, ModResourceModel>(dto);
                        break;

                    case WorkshopResourceType.Taunt:
                        resource = Mapper.Map <WorkshopResourceDto, TauntResourceModel>(dto);
                        break;

                    case WorkshopResourceType.Language:
                        resource = Mapper.Map <WorkshopResourceDto, LanguageResourceModel>(dto);
                        break;

                    default:
                        resource = Mapper.Map <WorkshopResourceDto, WorkshopResourceModel>(dto);
                        break;
                    }
                    resource.Status = WorkshopResourceStatus.NotInstalled;
                    Resources.Add(resource);
                    toSave.Add(resource);
                }
                else if ((ResourceServerStatus)dto.Status == ResourceServerStatus.Deleted)
                // resource has been deleted from server
                {
                    if (resource.Status == WorkshopResourceStatus.NotInstalled)
                    {
                        DatabaseClient.DeleteResource(resource.Id);
                        Resources.Remove(resource);
                    }
                    else
                    {
                        resource.DeletePending = true;
                        toSave.Add(resource);
                    }
                }
                else
                // resource exists locally
                {
                    if (dto.LastFileChangeDate > resource.LastFileChangeDate)
                    // resource file list updated
                    {
                        resource.LastFileChangeDate = dto.LastFileChangeDate;
                        resource.Status             = WorkshopResourceStatus.NeedUpdate;
                    }
                    if (dto.LastChangeDate > resource.LastChangeDate)
                    // resource metadata updated
                    {
                        resource.Rating         = dto.Rating;
                        resource.DownloadCount  = dto.DownloadCount;
                        resource.Name           = dto.Name;
                        resource.Description    = dto.Description;
                        resource.GameVersion    = dto.GameVersion;
                        resource.SourceUrl      = dto.SourceUrl;
                        resource.LastChangeDate = dto.LastChangeDate;
                    }
                    toSave.Add(resource);
                }
            }
            DatabaseClient.SaveResources(toSave);
            Config.WorkshopTimestamp = timestamp;
        }