Exemplo n.º 1
0
        public void Inject(XDocument modifiedDocument)
        {
            XNamespace windows10Namespace = "http://schemas.microsoft.com/appx/manifest/foundation/windows10";
            XNamespace appxNamespace      = "http://schemas.microsoft.com/appx/2010/manifest";
            XNamespace uap4Namespace      = XNamespace.Get("http://schemas.microsoft.com/appx/manifest/uap/windows10/4");
            XNamespace buildNamespace     = XNamespace.Get("http://schemas.microsoft.com/developer/appx/2015/build");

            if (modifiedDocument.Root == null)
            {
                throw new ArgumentException("The XML file is corrupted. Root element is not present.");
            }

            var package = modifiedDocument.Element(windows10Namespace + "Package") ?? modifiedDocument.Element(appxNamespace + "Package");

            if (package == null)
            {
                return;
            }

            var namespaceAdded = false;

            var originalBuildNamespace = modifiedDocument.Root.Attribute(XNamespace.Xmlns + "build");

            if (originalBuildNamespace != null)
            {
                buildNamespace = originalBuildNamespace.Value;
            }
            else
            {
                modifiedDocument.Root.SetAttributeValue(XNamespace.Xmlns + "build", buildNamespace);
                namespaceAdded = true;
            }

            var originalUap4Namespace = modifiedDocument.Root.Attribute(XNamespace.Xmlns + "uap4");

            if (originalUap4Namespace != null)
            {
                uap4Namespace = originalUap4Namespace.Value;
            }
            else
            {
                modifiedDocument.Root.SetAttributeValue(XNamespace.Xmlns + "uap4", uap4Namespace);
                namespaceAdded = true;
            }

            if (namespaceAdded)
            {
                var ignorable = modifiedDocument.Root.Attribute("IgnorableNamespaces");
                if (ignorable == null)
                {
                    modifiedDocument.Root.Add(new XAttribute("IgnorableNamespaces", "build uap4"));
                }
                else
                {
                    modifiedDocument.Root.SetAttributeValue("IgnorableNamespaces", string.Join(" ", ignorable.Value.Split(' ').Union(new[] { "build", "uap4" })));
                }
            }

            var metaData = package.Element(buildNamespace + "Metadata");

            metaData?.Remove();

            metaData = new XElement(buildNamespace + "Metadata");
            package.Add(metaData);
            var version = NdDll.RtlGetVersion();

            var operatingSystem = new XElement(buildNamespace + "Item"); //(  "bu", "Item");

            metaData.Add(operatingSystem);
            operatingSystem.Add(new XAttribute("Name", "OperatingSystem"));
            operatingSystem.Add(new XAttribute("Version", version.ToString(4)));

            var msixHero = new XElement(buildNamespace + "Item");

            metaData.Add(msixHero);
            msixHero.Add(new XAttribute("Name", "MsixHero"));
            // ReSharper disable once PossibleNullReferenceException
            msixHero.Add(new XAttribute("Version", (Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly()).GetName().Version.ToString()));

            var signTool = new XElement(buildNamespace + "Item");

            metaData.Add(signTool);
            signTool.Add(new XAttribute("Name", "SignTool.exe"));
            signTool.Add(new XAttribute("Version", GetVersion("SignTool.exe")));

            // ReSharper disable once IdentifierTypo
            var makepri = new XElement(buildNamespace + "Item");

            metaData.Add(makepri);
            makepri.Add(new XAttribute("Name", "MakePri.exe"));
            makepri.Add(new XAttribute("Version", GetVersion("MakePri.exe")));

            // ReSharper disable once IdentifierTypo
            var makeappx = new XElement(buildNamespace + "Item");

            metaData.Add(makeappx);
            makeappx.Add(new XAttribute("Name", "MakeAppx.exe"));
            makeappx.Add(new XAttribute("Version", GetVersion("MakeAppx.exe")));
        }
Exemplo n.º 2
0
        public void Inject(XDocument modifiedDocument)
        {
            var win10          = XNamespace.Get("http://schemas.microsoft.com/appx/manifest/foundation/windows10");
            var namespace4     = XNamespace.Get("http://schemas.microsoft.com/appx/manifest/uap/windows10/4");
            var namespaceBuild = XNamespace.Get("http://schemas.microsoft.com/developer/appx/2015/build");

            Debug.Assert(modifiedDocument.Root != null, "modifiedDocument.Root != null");

            modifiedDocument.Root.SetAttributeValue(XNamespace.Xmlns + "build", namespaceBuild);
            modifiedDocument.Root.SetAttributeValue(XNamespace.Xmlns + "uap4", namespace4);

            var ignorable = modifiedDocument.Root.Attribute("IgnorableNamespaces");

            if (ignorable == null)
            {
                modifiedDocument.Root.Add(new XAttribute("IgnorableNamespaces", "build uap4"));
            }
            else
            {
                modifiedDocument.Root.SetAttributeValue("IgnorableNamespaces", string.Join(" ", ignorable.Value.Split(' ').Union(new [] { "build", "uap4" })));
            }

            var package = modifiedDocument.Element(win10 + "Package");

            if (package == null)
            {
                return;
            }

            var metaData = package.Element(namespaceBuild + "Metadata");

            if (metaData != null)
            {
                metaData.Remove();
            }

            metaData = new XElement(namespaceBuild + "Metadata");
            package.Add(metaData);
            var version = NdDll.RtlGetVersion();

            var operatingSystem = new XElement(namespaceBuild + "Item"); //(  "bu", "Item");

            metaData.Add(operatingSystem);
            operatingSystem.Add(new XAttribute("Name", "OperatingSystem"));
            operatingSystem.Add(new XAttribute("Version", $"{version.ToString(4)}"));

            var msixHero = new XElement(namespaceBuild + "Item");

            metaData.Add(msixHero);
            msixHero.Add(new XAttribute("Name", "MsixHero"));
            // ReSharper disable once PossibleNullReferenceException
            msixHero.Add(new XAttribute("Version", (Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly()).GetName().Version.ToString()));

            var signTool = new XElement(namespaceBuild + "Item");

            metaData.Add(signTool);
            signTool.Add(new XAttribute("Name", "SignTool.exe"));
            signTool.Add(new XAttribute("Version", GetVersion("SignTool.exe")));

            var makepri = new XElement(namespaceBuild + "Item");

            metaData.Add(makepri);
            makepri.Add(new XAttribute("Name", "MakePri.exe"));
            makepri.Add(new XAttribute("Version", GetVersion("MakePri.exe")));

            var makeappx = new XElement(namespaceBuild + "Item");

            metaData.Add(makeappx);
            makeappx.Add(new XAttribute("Name", "MakeAppx.exe"));
            makeappx.Add(new XAttribute("Version", GetVersion("MakeAppx.exe")));
        }
        public async Task Inject(XDocument manifestContent, BrandingInjectorOverrideOption overwrite = BrandingInjectorOverrideOption.Default)
        {
            var toWrite = new Dictionary <string, string>();
            var toWriteOnlyIfMissing = new Dictionary <string, string>();

            var signToolVersion        = GetVersion("signtool.exe");
            var makePriVersion         = GetVersion("makepri.exe");
            var makeAppxVersion        = GetVersion("makeappx.exe");
            var operatingSystemVersion = NdDll.RtlGetVersion().ToString();
            // ReSharper disable once PossibleNullReferenceException
            var msixHeroVersion = (Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly()).GetName().Version.ToString();

            toWrite.Add("MsixHero", msixHeroVersion);

            switch (overwrite)
            {
            case BrandingInjectorOverrideOption.Default:
                // prefer existing, with exception of signtool and makeappx that are always re-created.
                toWriteOnlyIfMissing.Add("MakePri.exe", makePriVersion);
                toWrite.Add("SignTool.exe", signToolVersion);
                toWrite.Add("MakeAppx.exe", makeAppxVersion);
                toWriteOnlyIfMissing.Add("OperatingSystem", operatingSystemVersion);
                break;

            case BrandingInjectorOverrideOption.PreferExisting:
                // prefer all existing
                toWriteOnlyIfMissing.Add("MakePri.exe", makePriVersion);
                toWriteOnlyIfMissing.Add("SignTool.exe", signToolVersion);
                toWriteOnlyIfMissing.Add("MakeAppx.exe", makeAppxVersion);
                toWriteOnlyIfMissing.Add("OperatingSystem", operatingSystemVersion);
                break;

            case BrandingInjectorOverrideOption.PreferIncoming:
                // overwrite everything
                toWrite.Add("MakePri.exe", makePriVersion);
                toWrite.Add("SignTool.exe", signToolVersion);
                toWrite.Add("MakeAppx.exe", makeAppxVersion);
                toWrite.Add("OperatingSystem", operatingSystemVersion);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(overwrite), overwrite, null);
            }

            var executor = new SetBuildMetaDataExecutor(manifestContent);

            if (toWrite.Any())
            {
                var action = new SetBuildMetaData(toWrite)
                {
                    OnlyCreateNew = false
                };

                await executor.Execute(action).ConfigureAwait(false);
            }

            if (toWriteOnlyIfMissing.Any())
            {
                var action = new SetBuildMetaData(toWriteOnlyIfMissing)
                {
                    OnlyCreateNew = true
                };

                await executor.Execute(action).ConfigureAwait(false);
            }
        }