public async Task <AppxBundle> ReadBundle(IAppxFileReader fileReader, CancellationToken cancellationToken)
        {
            var bundle = new AppxBundle();

            using (var file = fileReader.GetFile(FileConstants.AppxBundleManifestFilePath))
            {
                var document = await XDocument.LoadAsync(file, LoadOptions.None, cancellationToken).ConfigureAwait(false);

                if (document.Root == null)
                {
                    throw new FormatException("The manifest is malformed. There is no root element.");
                }

                // var b4 = XNamespace.Get("http://schemas.microsoft.com/appx/2018/bundle");
                // var b5 = XNamespace.Get("http://schemas.microsoft.com/appx/2019/bundle");
                var ns = XNamespace.Get("http://schemas.microsoft.com/appx/2013/bundle");

                var identity = document.Root.Element(ns + "Identity");
                if (identity == null)
                {
                    throw new FormatException("The manifest is malformed, missing <Identity />.");
                }

                bundle.Version   = identity.Attribute("Version")?.Value;
                bundle.Name      = identity.Attribute("Name")?.Value;
                bundle.Publisher = identity.Attribute("Publisher")?.Value;
            }

            return(bundle);
        }
Пример #2
0
        // ReSharper disable once MemberCanBeMadeStatic.Local
        private async Task <PsfContentViewModel> LoadPsf(IAppxFileReader source, AppxPackage manifest)
        {
            var paths = manifest.Applications.Where(a => PackageTypeConverter.GetPackageTypeFrom(a.EntryPoint, a.Executable, a.StartPage, manifest.IsFramework) == MsixPackageType.BridgePsf).Select(a => a.Executable).Where(a => a != null).Select(Path.GetDirectoryName).Where(a => !string.IsNullOrEmpty(a)).Distinct().ToList();

            paths.Add(string.Empty);

            foreach (var items in paths)
            {
                var configJsonPath = string.IsNullOrWhiteSpace(items)
                    ? "config.json"
                    : Path.Combine(items, "config.json");
                if (!source.FileExists(configJsonPath))
                {
                    continue;
                }

                using (var s = source.GetFile(configJsonPath))
                {
                    using var stringReader = new StreamReader(s);
                    var all = await stringReader.ReadToEndAsync().ConfigureAwait(false);

                    var psfSerializer = new PsfConfigSerializer();

                    var configJson = psfSerializer.Deserialize(all);
                    return(new PsfContentViewModel(configJson));
                }
            }

            return(null);
        }
Пример #3
0
        public async Task <string> GetDiskPath(IAppxFileReader fileReader, string filePath)
        {
            if (!fileReader.FileExists(filePath))
            {
                throw new FileNotFoundException();
            }

            await using var fileStream = fileReader.GetFile(filePath);
            if (fileStream is FileStream fs)
            {
                // we can assume the file is directly accessible
                return(fs.Name);
            }

            var    index = 0;
            string fullPath;

            this.tempDirectory ??= new DirectoryInfo(Path.Combine(Path.GetTempPath(), "MSIX-Hero", Guid.NewGuid().ToString("N").Substring(10)));

            while (true)
            {
                fullPath = Path.Combine(this.tempDirectory.FullName, index.ToString("0"), Path.GetFileName(filePath));

                if (!File.Exists(fullPath))
                {
                    break;
                }

                index++;
            }

            var fileInfo = new FileInfo(fullPath);

            if (fileInfo.Directory?.Exists == false)
            {
                fileInfo.Directory.Create();
            }

            await using var targetStream = File.OpenWrite(fileInfo.FullName);
            await fileStream.CopyToAsync(targetStream).ConfigureAwait(false);

            return(fileInfo.FullName);
        }
Пример #4
0
        public PsfApplicationDescriptor Read(string applicationId, string originalEntryPoint, IAppxFileReader fileReader)
        {
            if (
                string.Equals(originalEntryPoint, @"AI_STUBS\AiStub.exe", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(originalEntryPoint, @"AI_STUBS\AiStubElevated.exe", StringComparison.OrdinalIgnoreCase))
            {
                if (fileReader.FileExists(@"AI_STUBS\AiStub.exe") || fileReader.FileExists(@"AI_STUBS\AiStubElevated.exe"))
                {
                    // This is an old Advanced Installer stuff
                    if (fileReader.FileExists("Registry.dat"))
                    {
                        RegistryHiveOnDemand reg;
                        using (var stream = fileReader.GetFile("Registry.dat"))
                        {
                            if (stream is FileStream fileStream)
                            {
                                reg = new RegistryHiveOnDemand(fileStream.Name);
                            }
                            else
                            {
                                using var memoryStream = new MemoryStream();
                                stream.CopyTo(memoryStream);
                                memoryStream.Flush();
                                reg = new RegistryHiveOnDemand(memoryStream.ToArray(), "Registry.dat");
                            }
                        }

                        var key = reg.GetKey(@"root\registry\machine\software\caphyon\advanced installer\" + applicationId);
                        if (key?.Values != null)
                        {
                            var psfDef = new PsfApplicationDescriptor();

                            foreach (var item in key.Values.Where(item => item.ValueName != null))
                            {
                                switch (item.ValueName.ToLowerInvariant())
                                {
                                case "path":
                                    psfDef.Executable = (item.ValueData ?? string.Empty).Replace("[{AppVPackageRoot}]\\", string.Empty);
                                    break;

                                case "pathai":
                                    psfDef.Executable = (item.ValueData ?? string.Empty).Replace("[{AppVPackageRoot}]\\", string.Empty);
                                    break;

                                case "workingdirectory":
                                    psfDef.WorkingDirectory = (item.ValueData ?? string.Empty).Replace("[{AppVPackageRoot}]\\", string.Empty);
                                    break;

                                case "workingdirectoryai":
                                    psfDef.WorkingDirectory = (item.ValueData ?? string.Empty).Replace("[{AppVPackageRoot}]\\", string.Empty);
                                    break;

                                case "args":
                                    psfDef.Arguments = item.ValueData;
                                    break;
                                }
                            }

                            if (string.IsNullOrWhiteSpace(psfDef.Executable))
                            {
                                psfDef.Executable = null;
                            }
                            else if (psfDef.Executable.StartsWith("[{", StringComparison.OrdinalIgnoreCase))
                            {
                                var indexOfClosing = psfDef.Executable.IndexOf("}]", StringComparison.OrdinalIgnoreCase);
                                if (indexOfClosing != -1)
                                {
                                    var middlePart = psfDef.Executable.Substring(2, indexOfClosing - 2);
                                    var testedPath = "VFS\\" + middlePart + psfDef.Executable.Substring(indexOfClosing + 2);

                                    if (fileReader.FileExists(testedPath))
                                    {
                                        // this is to make sure that a path like [{ProgramFilesX86}]\test is replaced to VFS\ProgramFilesX86\test if present
                                        psfDef.Executable = testedPath;
                                    }
                                }
                            }

                            if (string.IsNullOrWhiteSpace(psfDef.WorkingDirectory))
                            {
                                psfDef.WorkingDirectory = null;
                            }

                            if (string.IsNullOrWhiteSpace(psfDef.Arguments))
                            {
                                psfDef.Arguments = null;
                            }

                            return(psfDef);
                        }
                    }
                }
            }

            var    dir = Path.GetDirectoryName(originalEntryPoint);
            string configJson;

            if (string.IsNullOrEmpty(dir))
            {
                configJson = "config.json";
            }
            else
            {
                configJson = Path.Combine(dir, "config.json");
            }

            if (fileReader.FileExists(configJson))
            {
                using (var stream = fileReader.GetFile(configJson))
                {
                    using (var streamReader = new StreamReader(stream))
                    {
                        return(this.Read(applicationId, originalEntryPoint, streamReader));
                    }
                }
            }

            return(null);
        }
        private static async Task <AppxPackage> Translate(IAppxFileReader fileReader, AppxPackage package, CancellationToken cancellationToken)
        {
            var translationRequired = package.DisplayName?.StartsWith("ms-resource:") == true ||
                                      package.PublisherDisplayName?.StartsWith("ms-resource:") == true ||
                                      package.Description?.StartsWith("ms-resource:") == true ||
                                      package.Applications.Any(a => a.DisplayName?.StartsWith("ms-resource:") == true) ||
                                      package.Applications.Any(a => a.Description?.StartsWith("ms-resource:") == true);

            if (!translationRequired)
            {
                return(package);
            }

            string priFullPath = null;
            var    cleanup     = false;

            try
            {
                if (fileReader.FileExists("resources.pri"))
                {
                    using (var stream = fileReader.GetFile("resources.pri"))
                    {
                        if (stream is FileStream fileStream)
                        {
                            priFullPath = fileStream.Name;
                        }
                        else
                        {
                            priFullPath = Path.GetTempFileName();
                            cleanup     = true;

                            using (var fs = File.OpenWrite(priFullPath))
                            {
                                await stream.CopyToAsync(fs, cancellationToken).ConfigureAwait(false);

                                await fs.FlushAsync(cancellationToken).ConfigureAwait(false);
                            }
                        }
                    }

                    package.DisplayName          = StringLocalizer.Localize(priFullPath, package.Name, package.FullName, package.DisplayName);
                    package.Description          = StringLocalizer.Localize(priFullPath, package.Name, package.FullName, package.Description);
                    package.PublisherDisplayName = StringLocalizer.Localize(priFullPath, package.Name, package.FullName, package.PublisherDisplayName);

                    if (string.IsNullOrEmpty(package.DisplayName))
                    {
                        package.DisplayName = package.Name;
                    }

                    if (string.IsNullOrEmpty(package.PublisherDisplayName))
                    {
                        package.PublisherDisplayName = package.Publisher;
                    }

                    foreach (var app in package.Applications ?? Enumerable.Empty <AppxApplication>())
                    {
                        app.DisplayName = StringLocalizer.Localize(priFullPath, app.Id, package.FullName, app.DisplayName);

                        if (string.IsNullOrEmpty(app.DisplayName))
                        {
                            app.DisplayName = app.Id;
                        }

                        app.Description = StringLocalizer.Localize(priFullPath, app.Id, package.FullName, app.Description);
                    }
                }

                return(package);
            }
            finally
            {
                if (cleanup && File.Exists(priFullPath))
                {
                    File.Delete(priFullPath);
                }
            }
        }
        private async Task <AppxPackage> ReadMsix(IAppxFileReader fileReader, string manifestFileName, CancellationToken cancellationToken = default)
        {
            using (var file = fileReader.GetFile(manifestFileName))
            {
                var document = await XDocument.LoadAsync(file, LoadOptions.None, cancellationToken).ConfigureAwait(false);

                if (document.Root == null)
                {
                    throw new FormatException("The manifest is malformed. There is no root element.");
                }

                var ns        = XNamespace.Get("http://schemas.microsoft.com/appx/manifest/foundation/windows10");
                var ns2       = XNamespace.Get("http://schemas.microsoft.com/appx/2010/manifest");
                var uap       = XNamespace.Get("http://schemas.microsoft.com/appx/manifest/uap/windows10");
                var uap10     = XNamespace.Get("http://schemas.microsoft.com/appx/manifest/uap/windows10/10");
                var uap5      = XNamespace.Get("http://schemas.microsoft.com/appx/manifest/uap/windows10/5");
                var uap3      = XNamespace.Get("http://schemas.microsoft.com/appx/manifest/uap/windows10/3");
                var desktop10 = XNamespace.Get("http://schemas.microsoft.com/appx/manifest/desktop/windows10");
                var desktop6  = XNamespace.Get("http://schemas.microsoft.com/appx/manifest/desktop/windows10/6");
                var desktop2  = XNamespace.Get("http://schemas.microsoft.com/appx/manifest/desktop/windows10/2");
                var build     = XNamespace.Get("http://schemas.microsoft.com/developer/appx/2015/build");

                if (document.Root == null)
                {
                    throw new FormatException("The manifest is malformed. The document root must be <Package /> element.");
                }

                if (document.Root.Name.LocalName != "Package")
                {
                    throw new FormatException("The manifest is malformed. The document root must be <Package /> element.");
                }

                if (document.Root.Name.Namespace != ns && document.Root.Name.Namespace != ns2)
                {
                    throw new FormatException("The manifest is malformed. The document root must be <Package /> element, belonging to a supported namespace.");
                }

                var nodePackage = document.Root;

                cancellationToken.ThrowIfCancellationRequested();
                var nodeIdentity = nodePackage.Element(ns + "Identity") ?? nodePackage.Element(ns2 + "Identity");

                cancellationToken.ThrowIfCancellationRequested();
                var nodeProperties = nodePackage.Element(ns + "Properties") ?? nodePackage.Element(ns2 + "Properties");

                cancellationToken.ThrowIfCancellationRequested();
                var nodeApplicationsRoot = nodePackage.Element(ns + "Applications") ?? nodePackage.Element(ns2 + "Applications");

                cancellationToken.ThrowIfCancellationRequested();
                var nodeCapabilitiesRoot = nodePackage.Element(ns + "Capabilities") ?? nodePackage.Element(ns2 + "Capabilities");

                cancellationToken.ThrowIfCancellationRequested();
                var nodeDependenciesRoot = nodePackage.Element(ns + "Dependencies") ?? nodePackage.Element(ns2 + "Dependencies");

                cancellationToken.ThrowIfCancellationRequested();
                var nodePrerequisitesRoot = nodePackage.Element(ns + "Prerequisites") ?? nodePackage.Element(ns2 + "Prerequisites");

                cancellationToken.ThrowIfCancellationRequested();
                var nodeBuild = nodePackage.Element(build + "Metadata");

                var appxPackage = new AppxPackage();

                if (fileReader is IAppxDiskFileReader diskReader)
                {
                    appxPackage.RootFolder = diskReader.RootDirectory;
                }
                else if (fileReader is ZipArchiveFileReaderAdapter zipArchiveReader)
                {
                    appxPackage.RootFolder = Path.GetDirectoryName(zipArchiveReader.PackagePath);
                }

                cancellationToken.ThrowIfCancellationRequested();
                if (nodeIdentity != null)
                {
                    appxPackage.Name = nodeIdentity.Attribute("Name")?.Value;
                    var procArch = nodeIdentity.Attribute("ProcessorArchitecture")?.Value;
                    if (Enum.TryParse(typeof(AppxPackageArchitecture), procArch ?? string.Empty, true, out object parsedArchitecture) && parsedArchitecture != null)
                    {
                        appxPackage.ProcessorArchitecture = (AppxPackageArchitecture)parsedArchitecture;
                    }
                    else
                    {
                        appxPackage.ProcessorArchitecture = AppxPackageArchitecture.Neutral;
                    }

                    appxPackage.Publisher = nodeIdentity.Attribute("Publisher")?.Value;
                    appxPackage.Version   = nodeIdentity.Attribute("Version")?.Value;
                }

                cancellationToken.ThrowIfCancellationRequested();
                if (nodeProperties != null)
                {
                    foreach (var node in nodeProperties.Elements())
                    {
                        switch (node.Name.LocalName)
                        {
                        case "Logo":
                            var logo = node.Value;
                            if (!string.IsNullOrEmpty(logo))
                            {
                                using (var resourceStream = fileReader.GetResource(logo))
                                {
                                    if (resourceStream != null)
                                    {
                                        using (var memoryStream = new MemoryStream())
                                        {
                                            await resourceStream.CopyToAsync(memoryStream, cancellationToken).ConfigureAwait(false);

                                            await memoryStream.FlushAsync(cancellationToken).ConfigureAwait(false);

                                            appxPackage.Logo = memoryStream.ToArray();
                                        }
                                    }
                                }
                            }

                            break;

                        case "DisplayName":
                            appxPackage.DisplayName = node.Value;
                            break;

                        case "PublisherDisplayName":
                            appxPackage.PublisherDisplayName = node.Value;
                            break;

                        case "PackageIntegrity":
                            var packageIntegrityContent = nodeProperties.Element(uap10 + "Content");
                            if (packageIntegrityContent != null)
                            {
                                appxPackage.PackageIntegrity = packageIntegrityContent.Attribute("Enforcement")?.Value == "on";
                            }

                            break;

                        case "Framework":
                            appxPackage.IsFramework = string.Equals(node.Value ?? "false", "true", StringComparison.OrdinalIgnoreCase);
                            break;

                        case "Description":
                            appxPackage.Description = node.Value;
                            break;
                        }
                    }
                }

                cancellationToken.ThrowIfCancellationRequested();
                appxPackage.PackageDependencies         = new List <AppxPackageDependency>();
                appxPackage.OperatingSystemDependencies = new List <AppxOperatingSystemDependency>();
                appxPackage.MainPackages = new List <AppxMainPackageDependency>();
                appxPackage.Applications = new List <AppxApplication>();

                if (nodeApplicationsRoot != null)
                {
                    foreach (var node in nodeApplicationsRoot.Elements().Where(x => x.Name.LocalName == "Application" && (x.Name.Namespace == ns || x.Name.Namespace == ns2)))
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        /*
                         * <Application EntryPoint="SparklerApp.App" Executable="XD.exe" Id="App">
                         *  <uap:VisualElements BackgroundColor="#2D001E" Description="Adobe XD" DisplayName="Adobe XD" Square150x150Logo="Assets\xd_med_tile.png" Square44x44Logo="Assets\xd_app_list_icon.png">
                         *    <uap:DefaultTile Square310x310Logo="Assets\xd_large_tile.png" Square71x71Logo="Assets\xd_small_tile.png" Wide310x150Logo="Assets\xd_wide_tile.png">
                         *      <uap:ShowNameOnTiles>
                         *        <uap:ShowOn Tile="square150x150Logo" />
                         *        <uap:ShowOn Tile="wide310x150Logo" />
                         *        <uap:ShowOn Tile="square310x310Logo" />
                         *      </uap:ShowNameOnTiles>
                         *    </uap:DefaultTile>
                         *    <uap:SplashScreen BackgroundColor="#FFFFFF" Image="Assets\xd_splash.png" />
                         *  </uap:VisualElements>
                         *
                         * <Application Id="RayEval" Executable="PsfLauncher32.exe" EntryPoint="Windows.FullTrustApplication">
                         *  <uap:VisualElements DisplayName="RayEval" Description="RayEval Raynet GmbH" BackgroundColor="transparent"
                         *      Square150x150Logo="Assets\Square150x150Logo.scale-100.png" Square44x44Logo="Assets\Square44x44Logo.scale-100.png">
                         *      <uap:DefaultTile Wide310x150Logo="Assets\Square310x150Logo.scale-100.png" Square310x310Logo="Assets\Square310x310Logo.scale-100.png" Square71x71Logo="Assets\Square71x71Logo.scale-100.png" />
                         *  </uap:VisualElements>
                         *  <Extensions />
                         * </Application>
                         */

                        var appxApplication = new AppxApplication
                        {
                            EntryPoint = node.Attribute("EntryPoint")?.Value,
                            StartPage  = node.Attribute("StartPage")?.Value,
                            Executable = node.Attribute("Executable")?.Value,
                            Id         = node.Attribute("Id")?.Value,
                            Extensions = new List <AppxExtension>()
                        };


                        /*
                         * <Extensions><desktop6:Extension Category="windows.service" Executable="VFS\ProgramFilesX86\RayPackStudio\FloatingLicenseServer\FloatingLicenseServer.exe" EntryPoint="Windows.FullTrustApplication"><desktop6:Service Name="PkgSuiteFloatingLicenseServer" StartupType="auto" StartAccount="networkService" /></desktop6:Extension></Extensions>
                         */

                        var nodeExtensions = node.Elements().FirstOrDefault(e => e.Name.LocalName == "Extensions");

                        if (nodeExtensions != null)
                        {
                            foreach (var extension in nodeExtensions.Elements().Where(e => e.Name.LocalName == "Extension" && (e.Name.Namespace == ns || e.Name.Namespace == ns2 || e.Name.Namespace == desktop6 || e.Name.Namespace == desktop2 || e.Name.Namespace == uap5 || e.Name.Namespace == uap3)))
                            {
                                cancellationToken.ThrowIfCancellationRequested();
                                var category = extension.Attribute("Category")?.Value;

                                switch (category)
                                {
                                case "windows.appExecutionAlias":
                                    var aliasNode = extension.Element(uap3 + "AppExecutionAlias") ?? extension.Element(uap5 + "AppExecutionAlias");
                                    if (aliasNode != null)
                                    {
                                        var desktopExecAliases = aliasNode.Elements(desktop10 + "ExecutionAlias").Concat(aliasNode.Elements(uap5 + "ExecutionAlias"));
                                        foreach (var desktopExecAlias in desktopExecAliases)
                                        {
                                            if (appxApplication.ExecutionAlias == null)
                                            {
                                                appxApplication.ExecutionAlias = new List <string>();
                                            }

                                            appxApplication.ExecutionAlias.Add(desktopExecAlias.Attribute("Alias")?.Value);
                                        }
                                    }

                                    break;

                                case "windows.service":
                                    var serviceNode = extension.Element(desktop6 + "Service");
                                    if (serviceNode == null)
                                    {
                                        continue;
                                    }

                                    var service = new AppxService
                                    {
                                        Category = "windows.service"
                                    };

                                    service.EntryPoint = extension.Attribute("EntryPoint")?.Value;
                                    service.Executable = extension.Attribute("Executable")?.Value;

                                    service.Name         = extension.Attribute("Name")?.Value;
                                    service.StartAccount = extension.Attribute("StartAccount")?.Value;
                                    service.StartupType  = extension.Attribute("StartupType")?.Value;

                                    appxApplication.Extensions.Add(service);
                                    break;
                                }
                            }
                        }

                        var visualElements = node.Elements().FirstOrDefault(e => e.Name.LocalName == "VisualElements");
                        if (visualElements != null)
                        {
                            cancellationToken.ThrowIfCancellationRequested();
                            appxApplication.Description       = visualElements.Attribute("Description")?.Value;
                            appxApplication.DisplayName       = visualElements.Attribute("DisplayName")?.Value;
                            appxApplication.BackgroundColor   = visualElements.Attribute("BackgroundColor")?.Value;
                            appxApplication.Square150x150Logo = visualElements.Attribute("Square150x150Logo")?.Value;
                            appxApplication.Square44x44Logo   = visualElements.Attribute("Square44x44Logo")?.Value;
                            appxApplication.Visible           = visualElements.Attribute("AppListEntry")?.Value != "none";

                            var defaultTile = visualElements.Element(uap + "DefaultTile");
                            if (defaultTile != null)
                            {
                                appxApplication.Wide310x150Logo   = defaultTile.Attribute("Wide310x150Logo")?.Value;
                                appxApplication.Square310x310Logo = defaultTile.Attribute("Square310x310Logo")?.Value;
                                appxApplication.Square71x71Logo   = defaultTile.Attribute("Square71x71Logo")?.Value;
                                appxApplication.ShortName         = defaultTile.Attribute("ShortName")?.Value;
                            }

                            var logo = appxApplication.Square44x44Logo ?? appxApplication.Square30x30Logo ?? appxApplication.Square71x71Logo ?? appxApplication.Square150x150Logo;
                            if (logo == null)
                            {
                                appxApplication.Logo = appxPackage.Logo;
                            }
                            else
                            {
                                using (var stream =
                                           fileReader.GetResource(appxApplication.Square44x44Logo) ??
                                           fileReader.GetResource(appxApplication.Square30x30Logo) ??
                                           fileReader.GetResource(appxApplication.Square71x71Logo) ??
                                           fileReader.GetResource(appxApplication.Square150x150Logo))
                                {
                                    if (stream != null)
                                    {
                                        var bytes = new byte[stream.Length];
                                        await stream.ReadAsync(bytes, 0, bytes.Length, cancellationToken).ConfigureAwait(false);

                                        appxApplication.Logo = bytes;
                                    }
                                    else
                                    {
                                        appxApplication.Logo = appxPackage.Logo;
                                    }
                                }
                            }
                        }

                        appxPackage.Applications.Add(appxApplication);
                    }

                    var psfApps = appxPackage.Applications.Where(a =>
                                                                 PackageTypeConverter.GetPackageTypeFrom(a.EntryPoint, a.Executable, a.StartPage, appxPackage.IsFramework) ==
                                                                 MsixPackageType.BridgePsf).ToArray();

                    if (psfApps.Any())
                    {
                        foreach (var psfApp in psfApps)
                        {
                            cancellationToken.ThrowIfCancellationRequested();
                            psfApp.Psf = this.PsfReader.Read(psfApp.Id, psfApp.Executable, fileReader);
                        }
                    }
                }

                if (nodeDependenciesRoot != null)
                {
                    var dependencies = nodeDependenciesRoot.Elements();

                    if (dependencies != null)
                    {
                        foreach (var node in dependencies)
                        {
                            switch (node.Name.LocalName)
                            {
                            case "MainPackageDependency":
                                var modName = node.Attribute("Name")?.Value;

                                var appxModPackDependency = new AppxMainPackageDependency
                                {
                                    Name = modName,
                                };

                                appxPackage.MainPackages.Add(appxModPackDependency);

                                break;

                            case "TargetDeviceFamily":
                            {
                                /*
                                 * <TargetDeviceFamily MaxVersionTested="10.0.15063.0" MinVersion="10.0.15063.0" Name="Windows.Universal" />
                                 */

                                var minVersion = node.Attribute("MinVersion")?.Value;
                                var maxVersion = node.Attribute("MaxVersionTested")?.Value;
                                var name       = node.Attribute("Name")?.Value;

                                appxPackage.OperatingSystemDependencies.Add(new AppxOperatingSystemDependency
                                    {
                                        Minimum = WindowsNames.GetOperatingSystemFromNameAndVersion(name, minVersion),
                                        Tested  = WindowsNames.GetOperatingSystemFromNameAndVersion(name, maxVersion),
                                    });

                                break;
                            }

                            case "PackageDependency":
                            {
                                /*
                                 * <PackageDependency MinVersion="1.4.24201.0" Name="Microsoft.NET.Native.Runtime.1.4" Publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" />
                                 */

                                var minVersion = node.Attribute("MinVersion")?.Value;
                                var name       = node.Attribute("Name")?.Value;
                                var publisher  = node.Attribute("Publisher")?.Value;

                                var appxDependency = new AppxPackageDependency
                                {
                                    Publisher = publisher,
                                    Name      = name,
                                    Version   = minVersion
                                };

                                appxPackage.PackageDependencies.Add(appxDependency);
                                break;
                            }
                            }
                        }
                    }
                }

                if (nodePrerequisitesRoot != null)
                {
                    var min = nodePrerequisitesRoot.Element(ns2 + "OSMinVersion")?.Value;
                    var max = nodePrerequisitesRoot.Element(ns2 + "OSMaxVersionTested")?.Value;

                    appxPackage.OperatingSystemDependencies.Add(new AppxOperatingSystemDependency
                    {
                        Minimum = min == null ? null : WindowsNames.GetOperatingSystemFromNameAndVersion("Windows.Desktop", min),
                        Tested  = max == null ? null : WindowsNames.GetOperatingSystemFromNameAndVersion("Windows.Desktop", max),
                    });
                }

                if (nodeBuild != null)
                {
                    var buildKeyValues = new Dictionary <string, string>();

                    foreach (var buildNode in nodeBuild.Elements(build + "Item"))
                    {
                        var attrName = buildNode.Attribute("Name")?.Value;
                        if (attrName == null)
                        {
                            continue;
                        }

                        var attrVersion = buildNode.Attribute("Version")?.Value;
                        if (attrVersion == null)
                        {
                            attrVersion = buildNode.Attribute("Value")?.Value;
                            if (attrVersion == null)
                            {
                                continue;
                            }
                        }

                        buildKeyValues[attrName] = attrVersion;
                    }

                    var appDetector = new AuthoringAppDetector(fileReader);
                    if (appDetector.TryDetectAny(buildKeyValues, out var buildInfo))
                    {
                        appxPackage.BuildInfo = buildInfo;
                    }
                }

                appxPackage.FamilyName = AppxPackaging.GetPackageFamilyName(appxPackage.Name, appxPackage.Publisher);
                appxPackage.FullName   = AppxPackaging.GetPackageFullName(appxPackage.Name, appxPackage.Publisher, appxPackage.ProcessorArchitecture, appxPackage.Version, appxPackage.ResourceId);

                appxPackage.Capabilities = this.GetCapabilities(nodeCapabilitiesRoot);

                var pkgManager = new PackageManager();
                var pkg        = pkgManager.FindPackageForUser(string.Empty, appxPackage.FullName);
                if (pkg == null && appxPackage.ResourceId == null)
                {
                    appxPackage.FullName = AppxPackaging.GetPackageFullName(appxPackage.Name, appxPackage.Publisher, appxPackage.ProcessorArchitecture, appxPackage.Version, "neutral");
                    pkg = pkgManager.FindPackageForUser(string.Empty, appxPackage.FullName);
                }

                string manifestFilePath;
                if (pkg?.InstalledLocation != null)
                {
                    manifestFilePath = Path.Combine(pkg.InstalledLocation.Path, FileConstants.AppxManifestFile);
                }
                else if (fileReader is IAppxDiskFileReader appxDiskReader)
                {
                    manifestFilePath = Path.Combine(appxDiskReader.RootDirectory, manifestFileName);
                }
                else
                {
                    manifestFilePath = manifestFileName;
                }

                if (pkg == null)
                {
                    appxPackage.Source = new NotInstalledSource();
                }
                else if (pkg.SignatureKind == PackageSignatureKind.System)
                {
                    appxPackage.Source = new SystemSource(manifestFilePath);
                }
                else if (pkg.SignatureKind == PackageSignatureKind.None || pkg.IsDevelopmentMode)
                {
                    appxPackage.Source = new DeveloperSource(Path.Combine(appxPackage.RootFolder, manifestFileName));
                }
                else if (pkg.SignatureKind == PackageSignatureKind.Store)
                {
                    appxPackage.Source = new StorePackageSource(appxPackage.FamilyName, Path.GetDirectoryName(manifestFilePath));
                }
                else
                {
                    var appInstaller = pkg.GetAppInstallerInfo();
                    if (appInstaller != null)
                    {
                        appxPackage.Source = new AppInstallerPackageSource(appInstaller.Uri, Path.GetDirectoryName(manifestFilePath));
                    }
                }

                if (appxPackage.Source == null)
                {
                    appxPackage.Source = new StandardSource(manifestFilePath);
                }

                return(await Translate(fileReader, appxPackage, cancellationToken).ConfigureAwait(false));
            }
        }