private async Task <PoEBuild> ImportBuildAsync(Func <Task <XmlBuild> > supplier)
        {
            try
            {
                var xmlBuild = await supplier();

                var build = ConvertFromXmlBuild(xmlBuild);
                if (!CheckVersion(xmlBuild.Version))
                {
                    Log.Warn($"Build is of an old version and can't be imported (version {xmlBuild.Version})");
                    await DialogCoordinator.ShowWarningAsync(PersistentData,
                                                             L10n.Message("Build is of an old version and can't be imported"),
                                                             title : L10n.Message("Import failed"));

                    return(null);
                }
                return(build);
            }
            catch (Exception e)
            {
                Log.Error("Error while importing build", e);
                await DialogCoordinator.ShowErrorAsync(PersistentData, L10n.Message("Could not import build"),
                                                       e.Message, L10n.Message("Import failed"));

                return(null);
            }
        }
        /// <summary>
        /// Imports the build located at <paramref name="buildPath"/>. <see cref="IPersistentData.SaveBuild"/> may be
        /// called by this method.
        /// </summary>
        public async Task ImportBuildFromFileAsync(string buildPath)
        {
            const string extension = SerializationConstants.BuildFileExtension;

            if (!File.Exists(buildPath) || Path.GetExtension(buildPath) != extension)
            {
                Log.Error($"Could not import build file from {buildPath}");
                var message = string.Format(
                    L10n.Message(
                        "Could not import build file, only existing files with the extension {0} can be imported."),
                    extension);
                await DialogCoordinator.ShowErrorAsync(PersistentData, message, title : L10n.Message("Import failed"));

                return;
            }

            var unifiedBuildsSavePath = PersistentData.Options.BuildsSavePath.Replace(Path.AltDirectorySeparatorChar,
                                                                                      Path.DirectorySeparatorChar);
            var unifiedPath = buildPath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);

            if (unifiedPath.StartsWith(unifiedBuildsSavePath))
            {
                // If BuildsSavePath is part of buildPath, just set it as current and selected build
                // Remove BuildsSavePath
                var relativePath = unifiedPath.Remove(0, unifiedBuildsSavePath.Length + 1);
                // Remove extension
                var path  = relativePath.Remove(relativePath.Length - extension.Length);
                var build = BuildForPath(path) as PoEBuild;
                if (build == null)
                {
                    Log.Warn($"Import failed, build with path {path} not found");
                    return;
                }

                PersistentData.CurrentBuild  = build;
                PersistentData.SelectedBuild = build;
            }
            else
            {
                // Else, proper import
                var build = await ImportBuildAsync(
                    async() => await SerializationUtils.XmlDeserializeFileAsync <XmlBuild>(buildPath));

                if (build != null)
                {
                    build.Name = Util.FindDistinctName(build.Name, PersistentData.RootBuild.Builds.Select(b => b.Name));
                    PersistentData.RootBuild.Builds.Add(build);
                    PersistentData.CurrentBuild  = build;
                    PersistentData.SelectedBuild = build;
                    PersistentData.SaveBuild(PersistentData.RootBuild);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Imports the build located at <paramref name="buildPath"/>. <see cref="IPersistentData.SaveBuild"/> may be
        /// called by this method.
        /// </summary>
        public async Task ImportBuildAsync(string buildPath)
        {
            const string extension = SerializationConstants.BuildFileExtension;

            if (!File.Exists(buildPath) || Path.GetExtension(buildPath) != extension)
            {
                Log.Error($"Could not import build file from {buildPath}");
                var message = string.Format(
                    L10n.Message(
                        "Could not import build file, only existing files with the extension {0} can be imported."),
                    extension);
                await DialogCoordinator.ShowErrorAsync(PersistentData, message, title : L10n.Message("Import failed"));

                return;
            }

            var unifiedBuildsSavePath = PersistentData.Options.BuildsSavePath.Replace(Path.AltDirectorySeparatorChar,
                                                                                      Path.DirectorySeparatorChar);
            var unifiedPath = buildPath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);

            if (unifiedPath.StartsWith(unifiedBuildsSavePath))
            {
                // If BuildsSavePath is part of buildPath, just set it as current and selected build
                // Remove BuildsSavePath
                var relativePath = unifiedPath.Remove(0, unifiedBuildsSavePath.Length + 1);
                // Remove extension
                var path  = relativePath.Remove(relativePath.Length - extension.Length);
                var build = BuildForPath(path) as PoEBuild;
                if (build == null)
                {
                    Log.Warn($"Import failed, build with path {path} not found");
                    return;
                }

                PersistentData.CurrentBuild  = build;
                PersistentData.SelectedBuild = build;
            }
            else
            {
                // Else, proper import
                PoEBuild build;
                try
                {
                    var xmlBuild = await SerializationUtils.XmlDeserializeFileAsync <XmlBuild>(buildPath);

                    build = ConvertFromXmlBuild(xmlBuild);
                    if (!CheckVersion(xmlBuild.Version))
                    {
                        Log.Warn($"Build is of an old version and can't be imported (version {xmlBuild.Version})");
                        await DialogCoordinator.ShowWarningAsync(PersistentData,
                                                                 L10n.Message("Build is of an old version and can't be imported"),
                                                                 title : L10n.Message("Import failed"));

                        return;
                    }
                }
                catch (Exception e)
                {
                    Log.Error("Error while importing build", e);
                    await DialogCoordinator.ShowErrorAsync(PersistentData, L10n.Message("Could not import build"),
                                                           e.Message, L10n.Message("Import failed"));

                    return;
                }
                var message = L10n.Message("Enter the name for the imported build.\n\n")
                              + L10n.Message("This build can be saved to your build directory after this dialog.\n")
                              + L10n.Message("The originally imported file will not be modified.");
                var newName = await DialogCoordinator.ShowValidatingInputDialogAsync(PersistentData,
                                                                                     L10n.Message("Import build"), message, build.Name, ValidateImportedBuildName);

                if (string.IsNullOrEmpty(newName))
                {
                    return;
                }
                build.Name = newName;
                PersistentData.RootBuild.Builds.Add(build);

                PersistentData.CurrentBuild  = build;
                PersistentData.SelectedBuild = build;
                PersistentData.SaveBuild(PersistentData.RootBuild);
            }
        }