Exemplo n.º 1
0
 public void UpdateStatus(ResourceFileStatus value)
 {
     Status = value;
     DatabaseClient.UpdateResourceFileStatus(Id, value);
 }
Exemplo n.º 2
0
        public async Task DownloadAsync(CancellationToken cancellationToken)
        {
            EnsureDirectoryExists();
            switch (status)
            {
            case ResourceFileStatus.BeforeDownload:
            case ResourceFileStatus.ChecksumFailed:
                File.WriteAllText(FullPathName, string.Empty);
                var numChunks = (Size + ConfigModel.CHUNKSIZE - 1) / ConfigModel.CHUNKSIZE;
                for (var i = 0; i < numChunks; i++)
                {
                    Chunks.Add(new FileChunkModel {
                        FileId = Id, Id = i
                    });
                }
                DatabaseClient.SaveChunks(Chunks);
                UpdateStatus(ResourceFileStatus.Downloading);
                break;

            case ResourceFileStatus.Paused:
                Status = ResourceFileStatus.Downloading;
                break;

            case ResourceFileStatus.Downloading:
                break;

            case ResourceFileStatus.Deleted:
            case ResourceFileStatus.Finished:
                return;
            }

            var tasks   = Chunks.Where(c => !c.Finished).Select(c => c.DownloadAsync(cancellationToken));
            var working = new List <Task <(int, byte[])> >();

            using (var fs = new FileStream(FullPathName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read, 4096, true))
            {
                foreach (var task in tasks)
                {
                    if (working.Count < 5)
                    {
                        working.Add(task);
                    }
                    else
                    {
                        var finished = await Task.WhenAny(working);

                        working.Remove(finished);
                        working.Add(task);
                        await ContinueWith(await finished);
                    }
                }
                while (working.Count > 0)
                {
                    var finished = await Task.WhenAny(working);

                    working.Remove(finished);
                    await ContinueWith(await finished);
                }

                async Task ContinueWith((int Id, byte[] Data) finished)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        UpdateStatus(ResourceFileStatus.Paused);
                        cancellationToken.ThrowIfCancellationRequested();
                    }
                    fs.Seek(finished.Id * ConfigModel.CHUNKSIZE, SeekOrigin.Begin);
                    await fs.WriteAsync(finished.Data, 0, finished.Data.Length);

                    OnPropertyChanged(nameof(FinishedSize));
                    DatabaseClient.UpdateFileChunkFinished(Id, finished.Id, true);
                }
            }

            DatabaseClient.DeleteFileChunks(Id);
            if (Util.GetFileSha1(FullPathName).Equals(Sha1, StringComparison.InvariantCultureIgnoreCase))
            {
                UpdateStatus(ResourceFileStatus.Finished);
            }
            else
            {
                UpdateStatus(ResourceFileStatus.ChecksumFailed);
            }
        }
Exemplo n.º 3
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;
        }
Exemplo n.º 4
0
 private void UpdateIsActivated(bool isActivated)
 {
     IsActivated = isActivated;
     DatabaseClient.SaveDrs(this);
 }
Exemplo n.º 5
0
 private static void ActiveDrses_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
 {
     DatabaseClient.SaveDrses(ActiveDrses);
 }