Exemplo n.º 1
0
 private static async Task <T?> DeserializeAsync <T>(string path)
     where T : class
 {
     try
     {
         return(await XmlSerializationUtils.DeserializeFileAsync <T>(path, true).ConfigureAwait(false));
     }
     catch (Exception e)
     {
         Log.Error(e, $"Could not deserialize file from {path} as type {typeof(T)}");
         return(default);
Exemplo n.º 2
0
        /// <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 XmlSerializationUtils.DeserializeFileAsync <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);
                }
            }
        }