예제 #1
0
        public async Task ReplaceUpdatedPresetsFilesAsync()
        {
            try
            {
                await Task.Run(async() =>
                {
                    foreach (ReleaseType curReleaseType in Enum.GetValues(typeof(ReleaseType)))
                    {
                        var pathToUpdatedPresetsFile = GetPathToUpdatedPresetsFile(curReleaseType);
                        if (string.IsNullOrWhiteSpace(pathToUpdatedPresetsFile) || !File.Exists(pathToUpdatedPresetsFile))
                        {
                            continue;
                        }

                        logger.Debug($"start replacing presets with update: {pathToUpdatedPresetsFile}");

                        if (curReleaseType == ReleaseType.PresetsAndIcons)
                        {
                            using (var archive = ZipFile.OpenRead(pathToUpdatedPresetsFile))
                            {
                                foreach (var curEntry in archive.Entries)
                                {
                                    var destinationPath      = Path.Combine(Path.GetDirectoryName(pathToUpdatedPresetsFile), curEntry.FullName);
                                    var destinationDirectory = Path.GetDirectoryName(destinationPath);

                                    if (!Directory.Exists(destinationDirectory))
                                    {
                                        Directory.CreateDirectory(destinationDirectory);
                                    }

                                    if (File.Exists(destinationPath))
                                    {
                                        FileHelper.ResetFileAttributes(destinationPath);
                                    }

                                    //Sometimes an entry has no name. (Temporary file? Directory?)
                                    if (!string.IsNullOrWhiteSpace(curEntry.Name))
                                    {
                                        curEntry.ExtractToFile(destinationPath, true);
                                    }
                                }
                            }

                            //wait extra time for extraction to finish (sometimes the disk needs extra time)
                            await Task.Delay(TimeSpan.FromMilliseconds(200));

                            File.Delete(pathToUpdatedPresetsFile);

                            logger.Debug("finished extracting updated presets file");

                            continue;
                        }

                        var originalPresetsFileName   = Path.GetFileName(pathToUpdatedPresetsFile).Replace(CoreConstants.PrefixUpdatedPresetsFile, string.Empty);
                        var pathToOriginalPresetsFile = Path.Combine(Path.GetDirectoryName(pathToUpdatedPresetsFile), originalPresetsFileName);
                        if (File.Exists(pathToOriginalPresetsFile))
                        {
                            FileHelper.ResetFileAttributes(pathToOriginalPresetsFile);
                            File.Delete(pathToOriginalPresetsFile);
                        }

                        File.Move(pathToUpdatedPresetsFile, pathToOriginalPresetsFile);

                        logger.Debug("finished replacing presets with update");
                    }
                });
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Error replacing updated presets file.");

                string language = Localization.Localization.GetLanguageCodeFromName(Settings.Default.SelectedLanguage);
                MessageBox.Show(Localization.Localization.Translations[language]["UpdateErrorPresetMessage"],
                                Localization.Localization.Translations[language]["Error"],
                                MessageBoxButton.OK,
                                MessageBoxImage.Error,
                                MessageBoxResult.OK);
            }
        }
예제 #2
0
        public async Task ReplaceUpdatedPresetFileAsync()
        {
            try
            {
                await Task.Run(async() =>
                {
                    if (!string.IsNullOrWhiteSpace(PathToUpdatedPresetsFile) && File.Exists(PathToUpdatedPresetsFile))
                    {
                        var originalPathToPresetsFile = Path.Combine(App.ApplicationPath, CoreConstants.BuildingPresetsFile);

                        FileHelper.ResetFileAttributes(originalPathToPresetsFile);
                        File.Delete(originalPathToPresetsFile);
                        File.Move(PathToUpdatedPresetsFile, originalPathToPresetsFile);
                    }
                    else if (!string.IsNullOrWhiteSpace(PathToUpdatedPresetsAndIconsFile) && File.Exists(PathToUpdatedPresetsAndIconsFile))
                    {
                        using (var archive = ZipFile.OpenRead(PathToUpdatedPresetsAndIconsFile))
                        {
                            foreach (var curEntry in archive.Entries)
                            {
                                var destinationPath      = Path.Combine(App.ApplicationPath, curEntry.FullName);
                                var destinationDirectory = Path.GetDirectoryName(destinationPath);

                                if (!Directory.Exists(destinationDirectory))
                                {
                                    Directory.CreateDirectory(destinationDirectory);
                                }

                                if (File.Exists(destinationPath))
                                {
                                    FileHelper.ResetFileAttributes(destinationPath);
                                }

                                //Sometimes an entry has no name. (Temporary file? Directory?)
                                if (!string.IsNullOrWhiteSpace(curEntry.Name))
                                {
                                    curEntry.ExtractToFile(destinationPath, true);
                                }
                            }
                        }

                        //wait extra time for extraction to finish (sometimes the disk needs extra time)
                        await Task.Delay(TimeSpan.FromMilliseconds(200));

                        File.Delete(PathToUpdatedPresetsAndIconsFile);
                    }
                    else if (!string.IsNullOrWhiteSpace(PathToUpdatedIconMappingFile) && File.Exists(PathToUpdatedIconMappingFile))
                    {
                        var originalPathToIconMappingFile = Path.Combine(App.ApplicationPath, CoreConstants.IconNameFile);

                        FileHelper.ResetFileAttributes(originalPathToIconMappingFile);
                        File.Delete(originalPathToIconMappingFile);
                        File.Move(PathToUpdatedIconMappingFile, originalPathToIconMappingFile);
                    }
                    else if (!string.IsNullOrWhiteSpace(PathToUpdatedPredefinedColorsFile) && File.Exists(PathToUpdatedPredefinedColorsFile))
                    {
                        var originalPathToPredefinedColorsFile = Path.Combine(App.ApplicationPath, CoreConstants.ColorPresetsFile);

                        FileHelper.ResetFileAttributes(originalPathToPredefinedColorsFile);
                        File.Delete(originalPathToPredefinedColorsFile);
                        File.Move(PathToUpdatedPredefinedColorsFile, originalPathToPredefinedColorsFile);
                    }
                });
            }
            catch (Exception ex)
            {
                App.WriteToErrorLog("error replacing updated presets file", ex.Message, ex.StackTrace);

                string language = Localization.Localization.GetLanguageCodeFromName(Settings.Default.SelectedLanguage);
                MessageBox.Show(Localization.Localization.Translations[language]["UpdateErrorPresetMessage"],
                                Localization.Localization.Translations[language]["Error"],
                                MessageBoxButton.OK,
                                MessageBoxImage.Error,
                                MessageBoxResult.OK);
            }
        }
예제 #3
0
        public async Task <string> DownloadReleaseAsync(AvailableRelease releaseToDownload)
        {
            try
            {
                if (AllReleases == null)
                {
                    return(null);
                }

                logger.Debug($"Download asset for release {releaseToDownload.Version}.");

                var release = AllReleases.FirstOrDefault(x => x.Id == releaseToDownload.Id);
                if (release == null)
                {
                    logger.Warn($"No release found for {nameof(releaseToDownload.Id)}: {nameof(releaseToDownload.Id)}.");
                    return(null);
                }

                var assetName = GetAssetNameForReleaseType(releaseToDownload.Type);
                if (string.IsNullOrWhiteSpace(assetName))
                {
                    logger.Warn($"No asset name found for type: {releaseToDownload.Type}.");
                    return(null);
                }

                var foundAsset = release.Assets.FirstOrDefault(x => x.Name.StartsWith(assetName, StringComparison.OrdinalIgnoreCase));
                if (foundAsset == null && releaseToDownload.Type == ReleaseType.Presets)
                {
                    //check for release of presets with icons
                    releaseToDownload.Type = ReleaseType.PresetsAndIcons;
                    assetName  = GetAssetNameForReleaseType(releaseToDownload.Type);
                    foundAsset = release.Assets.FirstOrDefault(x => x.Name.StartsWith(assetName, StringComparison.OrdinalIgnoreCase));
                }

                if (foundAsset == null)
                {
                    logger.Warn($"No asset found with name: {assetName}.");
                    return(null);
                }

                //download file to temp directory
                var pathToDownloadedFile = await DownloadFileAsync(foundAsset.BrowserDownloadUrl, Path.GetTempFileName()).ConfigureAwait(false);

                if (string.IsNullOrWhiteSpace(pathToDownloadedFile))
                {
                    logger.Warn("Could not get path to downloaded file.");
                    return(null);
                }

                //move file to app directory
                var tempFileInfo = new FileInfo(pathToDownloadedFile);

                var pathToUpdatedPresetsFile = GetPathToUpdatedPresetsFile(releaseToDownload.Type);
                if (string.IsNullOrWhiteSpace(pathToUpdatedPresetsFile))
                {
                    logger.Warn($"Could not get path to updated presets file for type: {releaseToDownload.Type}.");
                    return(null);
                }

                if (File.Exists(pathToUpdatedPresetsFile))
                {
                    FileHelper.ResetFileAttributes(pathToUpdatedPresetsFile);
                    File.Delete(pathToUpdatedPresetsFile);
                }

                tempFileInfo.MoveTo(pathToUpdatedPresetsFile);

                return(pathToUpdatedPresetsFile);
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Error downloading release.");
                App.ShowMessageWithUnexpectedErrorAndExit();
                return(null);
            }
        }
예제 #4
0
        public async Task <string> DownloadLatestPresetFileAsync()
        {
            try
            {
                var result = string.Empty;

                if (LatestPresetRelease == null)
                {
                    return(result);
                }

                //check presets.json
                var latestPresetAsset = LatestPresetRelease.Assets.FirstOrDefault(x => x.Name.Equals(CoreConstants.BuildingPresetsFile, StringComparison.OrdinalIgnoreCase));
                if (latestPresetAsset == null)
                {
                    Trace.WriteLine($"No asset found for latest preset update. ({CoreConstants.BuildingPresetsFile})");
                }
                else
                {
                    LatestPresetReleaseType = AssetType.Presets;
                }

                //check presets with icons
                if (latestPresetAsset == null)
                {
                    latestPresetAsset = LatestPresetRelease.Assets.FirstOrDefault(x => x.Name.StartsWith(ASSET_NAME_PRESETS_AND_ICONS, StringComparison.OrdinalIgnoreCase));
                    if (latestPresetAsset == null)
                    {
                        Trace.WriteLine($"No asset found for latest preset update. ({ASSET_NAME_PRESETS_AND_ICONS})");
                    }
                    else
                    {
                        LatestPresetReleaseType = AssetType.PresetsAndIcons;
                    }
                }

                //check icons.json
                if (latestPresetAsset == null)
                {
                    latestPresetAsset = LatestPresetRelease.Assets.FirstOrDefault(x => x.Name.Equals(CoreConstants.IconNameFile, StringComparison.OrdinalIgnoreCase));
                    if (latestPresetAsset == null)
                    {
                        Trace.WriteLine($"No asset found for latest preset update. ({CoreConstants.IconNameFile})");
                    }
                    else
                    {
                        LatestPresetReleaseType = AssetType.IconMapping;
                    }
                }

                //check colors.json
                if (latestPresetAsset == null)
                {
                    latestPresetAsset = LatestPresetRelease.Assets.FirstOrDefault(x => x.Name.Equals(CoreConstants.ColorPresetsFile, StringComparison.OrdinalIgnoreCase));
                    if (latestPresetAsset == null)
                    {
                        Trace.WriteLine($"No asset found for latest preset update. ({CoreConstants.ColorPresetsFile})");
                    }
                    else
                    {
                        LatestPresetReleaseType = AssetType.PredefinedColors;
                    }
                }

                //still no supported asset found
                if (latestPresetAsset == null)
                {
                    Trace.WriteLine("No supported asset found for latest preset update.");
                    return(result);
                }

                //download file to temp directory
                var tempFileName         = Path.GetTempFileName();
                var pathToDownloadedFile = await DownloadFileAsync(latestPresetAsset.BrowserDownloadUrl, tempFileName).ConfigureAwait(false);

                if (string.IsNullOrWhiteSpace(pathToDownloadedFile))
                {
                    return(result);
                }

                tempFileName = pathToDownloadedFile;

                //move file to app directory
                var tempFileInfo = new FileInfo(tempFileName);

                if (LatestPresetReleaseType == AssetType.Presets)
                {
                    if (File.Exists(PathToUpdatedPresetsFile))
                    {
                        FileHelper.ResetFileAttributes(PathToUpdatedPresetsFile);
                        File.Delete(PathToUpdatedPresetsFile);
                    }

                    tempFileInfo.MoveTo(PathToUpdatedPresetsFile);
                    result = PathToUpdatedPresetsFile;
                }
                else if (LatestPresetReleaseType == AssetType.PresetsAndIcons)
                {
                    if (File.Exists(PathToUpdatedPresetsAndIconsFile))
                    {
                        FileHelper.ResetFileAttributes(PathToUpdatedPresetsAndIconsFile);
                        File.Delete(PathToUpdatedPresetsAndIconsFile);
                    }

                    tempFileInfo.MoveTo(PathToUpdatedPresetsAndIconsFile);
                    result = PathToUpdatedPresetsAndIconsFile;
                }
                else if (LatestPresetReleaseType == AssetType.IconMapping)
                {
                    if (File.Exists(PathToUpdatedIconMappingFile))
                    {
                        FileHelper.ResetFileAttributes(PathToUpdatedIconMappingFile);
                        File.Delete(PathToUpdatedIconMappingFile);
                    }

                    tempFileInfo.MoveTo(PathToUpdatedIconMappingFile);
                    result = PathToUpdatedIconMappingFile;
                }
                else if (LatestPresetReleaseType == AssetType.PredefinedColors)
                {
                    if (File.Exists(PathToUpdatedPredefinedColorsFile))
                    {
                        FileHelper.ResetFileAttributes(PathToUpdatedPredefinedColorsFile);
                        File.Delete(PathToUpdatedPredefinedColorsFile);
                    }

                    tempFileInfo.MoveTo(PathToUpdatedPredefinedColorsFile);
                    result = PathToUpdatedPredefinedColorsFile;
                }

                return(result);
            }
            catch (Exception ex)
            {
                //Trace.WriteLine($"Error downloading latest preset file.{Environment.NewLine}{ex}");
                App.LogErrorMessage(ex);
                return(string.Empty);
            }
        }