Ejemplo n.º 1
0
        public async Task <string> InstallCustom(string id, IProgress <double?> progress = null, CancellationToken cancellation = default(CancellationToken))
        {
            var destination = FilesStorage.Instance.GetDirectory("Locales", id);

            var data = await CmApiProvider.GetDataAsync(@"locales/get/base", progress, cancellation);

            if (cancellation.IsCancellationRequested || data == null)
            {
                return(null);
            }

            progress?.Report(null);
            using (var memory = new MemoryStream(data))
                using (var updateZip = new ZipArchive(memory)) {
                    foreach (var entry in updateZip.Entries)
                    {
                        using (var stream = entry.Open()) {
                            var filename = Path.Combine(destination, entry.Name);
                            if (File.Exists(filename))
                            {
                                continue;
                            }

                            await FileUtils.WriteAllBytesAsync(filename, stream, cancellation);

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

            return(destination);
        }
Ejemplo n.º 2
0
        public async Task <byte[]> GetModel(string key)
        {
            if (key != ExtraModels.KeyCrewExtra)
            {
                return(null);
            }

            var temporary = FilesStorage.Instance.GetTemporaryFilename("Crew.kn5");

            if (!File.Exists(temporary))
            {
                using (var dialog = new WaitingDialog()) {
                    dialog.Report(ControlsStrings.Common_Downloading);

                    var data = await CmApiProvider.GetDataAsync("static/get/cs_crew", dialog, dialog.CancellationToken);

                    if (data == null)
                    {
                        return(null);
                    }

                    await Task.Run(() => {
                        using (var stream = new MemoryStream(data, false))
                            using (var archive = new ZipArchive(stream)) {
                                archive.GetEntry("Crew.kn5").ExtractToFile(temporary);
                            }
                    });
                }
            }

            return(await FileUtils.ReadAllBytesAsync(temporary).ConfigureAwait(false));
        }
Ejemplo n.º 3
0
        private async Task <bool> LoadAndPrepare()
        {
#if !FORCE_UPDATE || !DEBUG
            if (!MainExecutingFile.IsPacked)
            {
                NonfatalError.Notify(ToolsStrings.AppUpdater_CannotUpdateApp, ToolsStrings.AppUpdater_UnpackedVersionMessage);
                LatestError = ToolsStrings.AppUpdater_UnpackedVersionMessage;
                return(false);
            }
#endif

            if (_isPreparing)
            {
                return(false);
            }
            _isPreparing  = true;
            UpdateIsReady = null;

            try {
                var data = await CmApiProvider.GetDataAsync($"app/get/{Branch}");

                if (data == null)
                {
                    throw new InformativeException(ToolsStrings.AppUpdater_CannotLoad, ToolsStrings.Common_MakeSureInternetWorks);
                }

                string preparedVersion = null;
                await Task.Run(() => {
                    if (File.Exists(UpdateLocation))
                    {
                        File.Delete(UpdateLocation);
                    }

                    using (var stream = new MemoryStream(data, false))
                        using (var archive = new ZipArchive(stream)) {
                            preparedVersion = VersionFromData(archive.GetEntry(@"Manifest.json").Open().ReadAsStringAndDispose());

                            archive.GetEntry(@"Content Manager.exe").ExtractToFile(UpdateLocation);
                            Logging.Write($"New version {preparedVersion} was extracted to “{UpdateLocation}”");
                        }
                });

                UpdateIsReady = preparedVersion;
                return(true);
            } catch (UnauthorizedAccessException) {
                NonfatalError.Notify(ToolsStrings.AppUpdater_AccessIsDenied,
                                     ToolsStrings.AppUpdater_AccessIsDenied_Commentary);
                LatestError = ToolsStrings.AppUpdater_AccessIsDenied_Short;
            } catch (Exception e) {
                NonfatalError.Notify(ToolsStrings.AppUpdater_CannotLoad,
                                     ToolsStrings.AppUpdater_CannotLoad_Commentary, e);
                LatestError = ToolsStrings.AppUpdater_CannotLoadShort;
            } finally {
                _isPreparing = false;
            }

            return(false);
        }
Ejemplo n.º 4
0
        public async Task InstallPlugin(PluginEntry plugin, IProgress <AsyncProgressEntry> progress = null,
                                        CancellationToken cancellation = default(CancellationToken))
        {
            var destination = GetPluginDirectory(plugin.Id);

            try {
                plugin.IsInstalling = true;

                var data = await CmApiProvider.GetDataAsync($"plugins/get/{plugin.Id}", progress, cancellation);

                if (data == null || cancellation.IsCancellationRequested)
                {
                    return;
                }

                await Task.Run(() => {
                    if (Directory.Exists(destination))
                    {
                        FileUtils.Recycle(destination);
                    }

                    using (var stream = new MemoryStream(data, false))
                        using (var archive = new ZipArchive(stream)) {
                            archive.ExtractToDirectory(destination);
                        }
                }, cancellation);

                if (cancellation.IsCancellationRequested)
                {
                    return;
                }

                plugin.InstalledVersion = plugin.Version;
                File.WriteAllText(Path.Combine(destination, ManifestFileName), JsonConvert.SerializeObject(plugin));

                if (plugin.IsEnabled)
                {
                    PluginEnabled?.Invoke(this, new PluginEventArgs {
                        PluginId = plugin.Id
                    });
                }
            } catch (Exception e) when(e.IsCancelled())
            {
            } catch (Exception e) {
                NonfatalError.Notify(ToolsStrings.Plugins_CannotInstall, e);
            } finally {
                plugin.IsInstalling = false;
            }
        }
Ejemplo n.º 5
0
        private static async Task <byte[]> GetFlamesTexturesAsync(IProgress <AsyncProgressEntry> progress = null,
                                                                  CancellationToken cancellation          = default(CancellationToken))
        {
            if (_flamesTextures == null)
            {
                progress?.Report(AsyncProgressEntry.FromStringIndetermitate("Loading flames textures…"));
                _flamesTextures = await CmApiProvider.GetDataAsync("static/get/flames", cancellation : cancellation);

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

            return(_flamesTextures);
        }
Ejemplo n.º 6
0
        protected override async Task <bool> CheckAndUpdateIfNeededInner()
        {
            if (InstalledVersion == null)
            {
                return(false);
            }

            var installedVersion = File.Exists(GetPackageFilename(SettingsHolder.Locale.LocaleName)) ? InstalledVersion : @"0";
            var data             = await CmApiProvider.GetDataAsync($"locales/update/{SettingsHolder.Locale.LocaleName}/{installedVersion}");

            if (data == null)
            {
                LatestError = ToolsStrings.BaseUpdater_CannotDownloadInformation;
                Logging.Warning("Cannot get locales/update");
                return(false);
            }

            if (data.Length == 0)
            {
                return(false);
            }

            try {
                LocalePackageManifest manifest;
                using (var memory = new MemoryStream(data))
                    using (var updateZip = new ZipArchive(memory)) {
                        manifest = LocalePackageManifest.FromArchive(updateZip);
                        if (manifest == null)
                        {
                            throw new Exception("Manifest is missing");
                        }
                    }

                var package = GetPackageFilename(manifest.Id);
                await FileUtils.WriteAllBytesAsync(package, data);

                Logging.Write("Locale updated");

                InstalledVersion = manifest.Version;
                return(true);
            } catch (Exception e) {
                Logging.Warning("Cannot update locale: " + e);
                return(false);
            }
        }
Ejemplo n.º 7
0
        private static async Task InstallShowroom(string showroomName, string showroomId, CarUpdatePreviewsDialog instance = null)
        {
            if (instance != null)
            {
                instance._mode = UpdatePreviewMode.Options;
                await Task.Delay(100);
            }

            using (var dialog = new WaitingDialog(showroomName)) {
                dialog.Report(ControlsStrings.Common_Downloading);

                var destination = FileUtils.GetShowroomsDirectory(AcRootDirectory.Instance.Value);
                var data        = await CmApiProvider.GetDataAsync($"static/get/{showroomId}", dialog, dialog.CancellationToken);

                if (data == null)
                {
                    dialog.Close();
                    NonfatalError.Notify(string.Format(AppStrings.CarPreviews_CannotDownloadShowroom, showroomName), ToolsStrings.Common_CannotDownloadFile_Commentary);
                    return;
                }

                dialog.Content = ControlsStrings.Common_Installing;
                try {
                    await Task.Run(() => {
                        using (var stream = new MemoryStream(data, false))
                            using (var archive = new ZipArchive(stream)) {
                                archive.ExtractToDirectory(destination);
                            }
                    });
                } catch (Exception e) {
                    dialog.Close();
                    NonfatalError.Notify(string.Format(AppStrings.CarPreviews_CannotInstallShowroom, showroomName), e);
                    return;
                }

                await Task.Delay(1000);

                if (instance != null)
                {
                    instance.SelectedShowroom = ShowroomsManager.Instance.GetById(showroomId) ?? instance.SelectedShowroom;
                }
            }
        }
Ejemplo n.º 8
0
        private static async Task <byte[]> LoadPackageTimeout(string langId, string version = "0")
        {
            if (!SettingsHolder.Locale.UpdateOnStart)
            {
                return(null);
            }
            using (var cancellation = new CancellationTokenSource()) {
                cancellation.CancelAfter(500);
                var data = await CmApiProvider.GetDataAsync($"locales/update/{langId}/{version}",
                                                            cancellation : cancellation.Token);

                if (cancellation.IsCancellationRequested)
                {
                    Logging.Write("Timeout exceeded");
                }

                return(data == null || data.Length == 0 ? null : data);
            }
        }
Ejemplo n.º 9
0
        private async Task <bool> LoadAndInstall()
        {
            if (_isInstalling)
            {
                return(false);
            }
            _isInstalling = true;

            try {
                var data = await CmApiProvider.GetDataAsync("data/latest");

                if (data == null)
                {
                    throw new InformativeException(ToolsStrings.AppUpdater_CannotLoad, ToolsStrings.Common_MakeSureInternetWorks);
                }

                string installedVersion = null;
                await Task.Run(() => {
                    var location = FilesStorage.Instance.Combine(FilesStorage.DataDirName);
                    Directory.Delete(location, true);

                    using (var stream = new MemoryStream(data, false))
                        using (var archive = new ZipArchive(stream)) {
                            installedVersion = VersionFromData(archive.GetEntry(@"Manifest.json").Open().ReadAsStringAndDispose());
                            archive.ExtractToDirectory(location);
                        }
                });

                InstalledVersion = installedVersion;
                Logging.Write("Data loaded: " + InstalledVersion);
                return(true);
            } catch (Exception e) {
                NonfatalError.Notify(ToolsStrings.ContentSyncronizer_CannotLoadContent, ToolsStrings.ContentSyncronizer_CannotLoadContent_Commentary, e);
            } finally {
                _isInstalling = false;
            }

            return(false);
        }