<commit_before>using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
using Windows.ApplicationModel;
using Windows.Management.Deployment;
using AppxPackaing;
using Shell;
using Wox.Infrastructure;
using Wox.Infrastructure.Logger;
using IStream = AppxPackaing.IStream;
using Rect = System.Windows.Rect;
using NLog;
using System.Collections.Concurrent;
using Windows.UI.Xaml.Controls;
using Microsoft.Win32;
using System.Xml;
using Windows.ApplicationModel.Activation;
using System.Security.Cryptography;
using Sentry.Protocol;

namespace Wox.Plugin.Program.Programs
{
    [Serializable]
    public class UWP
    {
        public string FullName { get; }
        public string FamilyName { get; }
        public string Name { get; set; }
        public string Location { get; set; }

        public Application[] Apps { get; set; }

        public PackageVersion Version { get; set; }

        private static readonly NLog.Logger Logger = LogManager.GetCurrentClassLogger();

        public UWP(string id, string location)
        {
            FullName = id;
            string[] parts = id.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
            FamilyName = $"{parts[0]}_{parts[parts.Length - 1]}";
            Location = location;
        }


        /// <exception cref="ArgumentException"
        private void InitializeAppInfo()
        {
            var path = Path.Combine(Location, "AppxManifest.xml");
            using (var reader = XmlReader.Create(path))
            {
                bool success = reader.ReadToFollowing("Package");
                if (!success) { throw new ArgumentException($"Cannot read Package key from {path}"); }

                Version = PackageVersion.Unknown;
                for (int i = 0; i < reader.AttributeCount; i++)
                {
                    string schema = reader.GetAttribute(i);
                    if (schema != null)
                    {
                        if (schema == "http://schemas.microsoft.com/appx/manifest/foundation/windows10")
                        {
                            Version = PackageVersion.Windows10;
                        }
                        else if (schema == "http://schemas.microsoft.com/appx/2013/manifest")
                        {
                            Version = PackageVersion.Windows81;
                        }
                        else if (schema == "http://schemas.microsoft.com/appx/2010/manifest")
                        {
                            Version = PackageVersion.Windows8;
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
                if (Version == PackageVersion.Unknown)
                {
                    throw new ArgumentException($"Unknowen schema version {path}");
                }

                success = reader.ReadToFollowing("Identity");
                if (!success) { throw new ArgumentException($"Cannot read Identity key from {path}"); }
                if (success)
                {
                    Name = reader.GetAttribute("Name");
                }

                success = reader.ReadToFollowing("Applications");
                if (!success) { throw new ArgumentException($"Cannot read Applications key from {path}"); }
                success = reader.ReadToDescendant("Application");
                if (!success) { throw new ArgumentException($"Cannot read Applications key from {path}"); }
                List<Application> apps = new List<Application>();
                do
                {
                    string id = reader.GetAttribute("Id");

                    reader.ReadToFollowing("uap:VisualElements");
                    string displayName = reader.GetAttribute("DisplayName");
                    string description = reader.GetAttribute("Description");
                    string backgroundColor = reader.GetAttribute("BackgroundColor");
                    string appListEntry = reader.GetAttribute("AppListEntry");

                    if (appListEntry == "none")
                    {
                        continue;
                    }

                    string logoUri = string.Empty;
                    if (Version == PackageVersion.Windows10)
                    {
                        logoUri = reader.GetAttribute("Square44x44Logo");
                    }
                    else if (Version == PackageVersion.Windows81)
                    {
                        logoUri = reader.GetAttribute("Square30x30Logo");
                    }
                    else if (Version == PackageVersion.Windows8)
                    {
                        logoUri = reader.GetAttribute("SmallLogo");
                    }
                    else
                    {
                        throw new ArgumentException($"Unknowen schema version {path}");
                    }

                    if (string.IsNullOrEmpty(displayName) || string.IsNullOrEmpty(id))
                    {
                        continue;
                    }

                    string userModelId = $"{FamilyName}!{id}";
                    Application app = new Application(this, userModelId, FullName, Name, displayName, description, logoUri, backgroundColor);

                    apps.Add(app);
                } while (reader.ReadToFollowing("Application"));
                Apps = apps.ToArray();
            }
        }

        private void InitPackageVersion(List<string> namespaces)
        {
            var versionFromNamespace = new Dictionary<string, PackageVersion>
            {
                {"http://schemas.microsoft.com/appx/manifest/foundation/windows10", PackageVersion.Windows10},
                {"http://schemas.microsoft.com/appx/2013/manifest", PackageVersion.Windows81},
                {"http://schemas.microsoft.com/appx/2010/manifest", PackageVersion.Windows8},
            };

            foreach (var n in versionFromNamespace.Keys)
            {
                if (namespaces.Contains(n))
                {
                    Version = versionFromNamespace[n];
                    return;
                }
            }

            throw new ArgumentException($"Unknown package version {string.Join(",", namespaces)}");
        }

        public static Application[] All()
        {
            ConcurrentBag<Application> bag = new ConcurrentBag<Application>();
            Parallel.ForEach(PackageFoldersFromRegistry(), (package, state) =>
            {
                try
                {
                    package.InitializeAppInfo();<commit_msg>remvoe useless code<commit_after>using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
using Windows.ApplicationModel;
using Windows.Management.Deployment;
using AppxPackaing;
using Shell;
using Wox.Infrastructure;
using Wox.Infrastructure.Logger;
using IStream = AppxPackaing.IStream;
using Rect = System.Windows.Rect;
using NLog;
using System.Collections.Concurrent;
using Windows.UI.Xaml.Controls;
using Microsoft.Win32;
using System.Xml;
using Windows.ApplicationModel.Activation;
using System.Security.Cryptography;
using Sentry.Protocol;

namespace Wox.Plugin.Program.Programs
{
    [Serializable]
    public class UWP
    {
        public string FullName { get; }
        public string FamilyName { get; }
        public string Name { get; set; }
        public string Location { get; set; }

        public Application[] Apps { get; set; }

        public PackageVersion Version { get; set; }

        private static readonly NLog.Logger Logger = LogManager.GetCurrentClassLogger();

        public UWP(string id, string location)
        {
            FullName = id;
            string[] parts = id.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
            FamilyName = $"{parts[0]}_{parts[parts.Length - 1]}";
            Location = location;
        }


        /// <exception cref="ArgumentException"
        private void InitializeAppInfo()
        {
            var path = Path.Combine(Location, "AppxManifest.xml");
            using (var reader = XmlReader.Create(path))
            {
                bool success = reader.ReadToFollowing("Package");
                if (!success) { throw new ArgumentException($"Cannot read Package key from {path}"); }

                Version = PackageVersion.Unknown;
                for (int i = 0; i < reader.AttributeCount; i++)
                {
                    string schema = reader.GetAttribute(i);
                    if (schema != null)
                    {
                        if (schema == "http://schemas.microsoft.com/appx/manifest/foundation/windows10")
                        {
                            Version = PackageVersion.Windows10;
                        }
                        else if (schema == "http://schemas.microsoft.com/appx/2013/manifest")
                        {
                            Version = PackageVersion.Windows81;
                        }
                        else if (schema == "http://schemas.microsoft.com/appx/2010/manifest")
                        {
                            Version = PackageVersion.Windows8;
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
                if (Version == PackageVersion.Unknown)
                {
                    throw new ArgumentException($"Unknowen schema version {path}");
                }

                success = reader.ReadToFollowing("Identity");
                if (!success) { throw new ArgumentException($"Cannot read Identity key from {path}"); }
                if (success)
                {
                    Name = reader.GetAttribute("Name");
                }

                success = reader.ReadToFollowing("Applications");
                if (!success) { throw new ArgumentException($"Cannot read Applications key from {path}"); }
                success = reader.ReadToDescendant("Application");
                if (!success) { throw new ArgumentException($"Cannot read Applications key from {path}"); }
                List<Application> apps = new List<Application>();
                do
                {
                    string id = reader.GetAttribute("Id");

                    reader.ReadToFollowing("uap:VisualElements");
                    string displayName = reader.GetAttribute("DisplayName");
                    string description = reader.GetAttribute("Description");
                    string backgroundColor = reader.GetAttribute("BackgroundColor");
                    string appListEntry = reader.GetAttribute("AppListEntry");

                    if (appListEntry == "none")
                    {
                        continue;
                    }

                    string logoUri = string.Empty;
                    if (Version == PackageVersion.Windows10)
                    {
                        logoUri = reader.GetAttribute("Square44x44Logo");
                    }
                    else if (Version == PackageVersion.Windows81)
                    {
                        logoUri = reader.GetAttribute("Square30x30Logo");
                    }
                    else if (Version == PackageVersion.Windows8)
                    {
                        logoUri = reader.GetAttribute("SmallLogo");
                    }
                    else
                    {
                        throw new ArgumentException($"Unknowen schema version {path}");
                    }

                    if (string.IsNullOrEmpty(displayName) || string.IsNullOrEmpty(id))
                    {
                        continue;
                    }

                    string userModelId = $"{FamilyName}!{id}";
                    Application app = new Application(this, userModelId, FullName, Name, displayName, description, logoUri, backgroundColor);

                    apps.Add(app);
                } while (reader.ReadToFollowing("Application"));
                Apps = apps.ToArray();
            }
        }

        public static Application[] All()
        {
            ConcurrentBag<Application> bag = new ConcurrentBag<Application>();
            Parallel.ForEach(PackageFoldersFromRegistry(), (package, state) =>
            {
                try
                {
                    package.InitializeAppInfo();
{'fraction_non_alphanumeric': 0.09381482904341144, 'fraction_numerical': 0.006223588167499039, 'mean_word_length': 1.4792380952380952, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6975123', 'n_tokens_mistral': 3131, 'n_tokens_neox': 2850, 'n_words': 657}