コード例 #1
0
        private static async Task <bool> ApplyCarSkin(CarObject car, string skinChecksum)
        {
            return(await Task.Run(() => {
                var skinFile = FilesStorage.Instance.GetTemporaryFilename("RaceU", "Skins", car.Id, skinChecksum);
                if (!File.Exists(skinFile))
                {
                    return false;
                }

                var destination = Path.Combine(car.SkinsDirectory, skinChecksum);
                if (Directory.Exists(destination))
                {
                    return true;
                }

                FileUtils.EnsureDirectoryExists(destination);
                RaceUTemporarySkinsHelper.MarkForFutherRemoval(destination);

                using (var stream = File.OpenRead(skinFile))
                    using (var archive = new ZipArchive(stream)) {
                        archive.ExtractToDirectory(destination);
                    }
                return true;
            }));
        }
コード例 #2
0
            public void applySkinsAsync(string carId, string[] skinChecksums, IJavascriptCallback callback)
            {
                ActionExtension.InvokeInMainThreadAsync(async() => {
                    var car = await CarsManager.Instance.GetByIdAsync(carId);
                    if (car == null)
                    {
                        callback?.ExecuteAsync($"Car with ID={carId} is missing");
                        return;
                    }

                    using (CarsManager.Instance.IgnoreChanges()) {
                        var applied = (await skinChecksums.Select(x => ApplyCarSkin(car, x)).WhenAll(4)).Count(x => x);
                        if (applied < skinChecksums.Length)
                        {
                            var existingSkins = (await car.EnabledOnlySkins.Select(async x => new {
                                checksum = await TryGetCarSkinChecksumAsync(x),
                                skin = x
                            }).WhenAll(4)).NonNull().ToList();
                            foreach (var entry in existingSkins.Where(x => skinChecksums.Contains(x.checksum)))
                            {
                                var destination = Path.Combine(car.SkinsDirectory, entry.checksum);
                                if (!Directory.Exists(destination))
                                {
                                    RaceUTemporarySkinsHelper.MarkForFutherRemoval(destination);
                                    Directory.CreateDirectory(destination);
                                    foreach (var file in GetSkinFiles(entry.skin))
                                    {
                                        FileUtils.HardLinkOrCopyRecursive(file.FullName, Path.Combine(destination, file.FullName));
                                    }
                                }
                                ++applied;
                            }
                        }
                        callback?.ExecuteAsync(null, applied);
                    }
                });
            }