<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using IWshRuntimeLibrary;
using Microsoft.Win32;
using Wox.Infrastructure.Logger;

namespace Wox.Plugin.Program.Programs
{
    [Serializable]
    public class Win32
    {
        public string FullName { get; set; }
        public string IcoPath { get; set; }
        public string FullPath { get; set; }
        public string ParentDirectory { get; set; }
        public string ExecutableName { get; set; }
        public int Score { get; set; }

        private const string ShortcutExtension = "lnk";
        private const string ApplicationReferenceExtension = "appref-ms";
        private const string ExeExtension = "exe";

        public override string ToString()
        {
            return ExecutableName;
        }

        private static Win32 Win32Program(string path)
        {
            var p = new Win32
            {
                FullName = Path.GetFileNameWithoutExtension(path),
                IcoPath = path,
                FullPath = path,
                ParentDirectory = Directory.GetParent(path).FullName,
            };
            return p;
        }

        private static Win32 LnkProgram(string path)
        {
            var shell = new WshShell();
            var shortcut = (IWshShortcut)shell.CreateShortcut(path);
            var program = Win32Program(path);

            try
            {
                var description = shortcut.Description;
                if (!string.IsNullOrEmpty(description))
                {
                    program.FullName += $": {description}";
                }
                else
                {
                    if (!string.IsNullOrEmpty(shortcut.TargetPath))
                    {
                        var info = FileVersionInfo.GetVersionInfo(shortcut.TargetPath);
                        if (!string.IsNullOrEmpty(info.FileDescription))
                        {
                            program.FullName += $": {info.FileDescription}";
                        }
                    }
                }
                return program;
            }
            catch (Exception)
            {
                Log.Error($"Error when parsing shortcut: {path}");
                return program;
            }
        }

        private static Win32 ExeProgram(string path)
        {
            var program = Win32Program(path);
            var versionInfo = FileVersionInfo.GetVersionInfo(path);
            if (!string.IsNullOrEmpty(versionInfo.FileDescription))
            {
                program.FullName = versionInfo.FileDescription;
            }
            return program;
        }

        private static IEnumerable<string> ProgramPaths(string directory, string[] suffixes)
        {<commit_msg>Fix unicode in lnk dexcrption<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using IWshRuntimeLibrary;
using Microsoft.Win32;
using Shell;
using Wox.Infrastructure.Logger;

namespace Wox.Plugin.Program.Programs
{
    [Serializable]
    public class Win32
    {
        public string FullName { get; set; }
        public string IcoPath { get; set; }
        public string FullPath { get; set; }
        public string ParentDirectory { get; set; }
        public string ExecutableName { get; set; }
        public int Score { get; set; }

        private const string ShortcutExtension = "lnk";
        private const string ApplicationReferenceExtension = "appref-ms";
        private const string ExeExtension = "exe";

        public override string ToString()
        {
            return ExecutableName;
        }

        private static Win32 Win32Program(string path)
        {
            var p = new Win32
            {
                FullName = Path.GetFileNameWithoutExtension(path),
                IcoPath = path,
                FullPath = path,
                ParentDirectory = Directory.GetParent(path).FullName,
            };
            return p;
        }

        private static Win32 LnkProgram(string path)
        {
            var program = Win32Program(path);

            try
            {
                var link = new ShellLink();
                const uint STGM_READ = 0;
                ((IPersistFile)link).Load(path, STGM_READ);
                var hwnd = new _RemotableHandle();
                link.Resolve(ref hwnd, 0);

                const int MAX_PATH = 260;
                StringBuilder buffer = new StringBuilder(MAX_PATH);
                link.GetDescription(buffer, MAX_PATH);
                var description = buffer.ToString();
                if (!string.IsNullOrEmpty(description))
                {
                    program.FullName += $": {description}";
                }
                else
                {
                    buffer = new StringBuilder(MAX_PATH);
                    var data = new _WIN32_FIND_DATAW();
                    const uint SLGP_SHORTPATH = 1;
                    link.GetPath(buffer, buffer.Capacity, ref data, SLGP_SHORTPATH);
                    var target = buffer.ToString();
                    if (!string.IsNullOrEmpty(target))
                    {
                        var info = FileVersionInfo.GetVersionInfo(target);
                        if (!string.IsNullOrEmpty(info.FileDescription))
                        {
                            program.FullName += $": {info.FileDescription}";
                        }
                    }
                }
                return program;
            }
            catch (Exception)
            {
                Log.Error($"Error when parsing shortcut: {path}");
                return program;
            }
        }

        private static Win32 ExeProgram(string path)
        {
            var program = Win32Program(path);
            var info = FileVersionInfo.GetVersionInfo(path);
            if (!string.IsNullOrEmpty(info.FileDescription))
            {
                program.FullName += $": {info.FileDescription}";
            }
            return program;
        }

        private static IEnumerable<string> ProgramPaths(string directory, string[] suffixes)
        {
{'fraction_non_alphanumeric': 0.08028503562945369, 'fraction_numerical': 0.006967537608867775, 'mean_word_length': 1.4778344448803453, 'pattern_counts': {'":': 5, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4929165', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1481, 'n_words': 371}