Exemplo n.º 1
0
        /// <summary>
        /// 安全的转换成字典集
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TElement"></typeparam>
        /// <param name="source"></param>
        /// <param name="keySelector">键选择器</param>
        /// <param name="elementSelector">值选择器</param>
        /// <returns></returns>
        public static async Task <DisposableDictionary <TKey, TElement> > ToDisposableDictionarySafetyAsync <TSource, TKey, TElement>(this IEnumerable <TSource> source, Func <TSource, TKey> keySelector, Func <TSource, Task <TElement> > elementSelector) where TElement : IDisposable
        {
            var dic = new DisposableDictionary <TKey, TElement>(source.Count());
            await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));

            return(dic);
        }
Exemplo n.º 2
0
 public MaterialTextureCache(dynamic options)
 {
     m_defaultTextures = CreateDefaultTextures();
     m_enableTintColor = options.EnableTintColor;
     m_tintColorName   = options.TintColorName;
     m_textureInfoList = options.TextureInfoList;
     m_textureCache    = new DisposableDictionary <string, TexturePacker.MaterialTexture>();
 }
Exemplo n.º 3
0
            private static DisposableDictionary <PackingType, WorkingTexture> CreateDefaultTextures()
            {
                DisposableDictionary <PackingType, WorkingTexture> textures = new DisposableDictionary <PackingType, WorkingTexture>();

                textures.Add(PackingType.White, CreateEmptyTexture(4, 4, Color.white, false));
                textures.Add(PackingType.Black, CreateEmptyTexture(4, 4, Color.black, false));
                textures.Add(PackingType.Normal, CreateEmptyTexture(4, 4, new Color(0.5f, 0.5f, 1.0f), true));

                return(textures);
            }
Exemplo n.º 4
0
        /// <summary>
        /// 安全的转换成字典集
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TElement"></typeparam>
        /// <param name="source"></param>
        /// <param name="keySelector">键选择器</param>
        /// <param name="elementSelector">值选择器</param>
        /// <returns></returns>
        public static DisposableDictionary <TKey, TElement> ToDisposableDictionarySafety <TSource, TKey, TElement>(this IEnumerable <TSource> source, Func <TSource, TKey> keySelector, Func <TSource, TElement> elementSelector) where TElement : IDisposable
        {
            var dic = new DisposableDictionary <TKey, TElement>(source.Count());

            foreach (var item in source)
            {
                dic[keySelector(item)] = elementSelector(item);
            }

            return(dic);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 安全的转换成字典集
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="source"></param>
        /// <param name="keySelector">键选择器</param>
        /// <param name="defaultValue">键未找到时的默认值</param>
        /// <returns></returns>
        public static DisposableDictionary <TKey, TSource> ToDisposableDictionarySafety <TSource, TKey>(this IEnumerable <TSource> source, Func <TSource, TKey> keySelector, TSource defaultValue) where TSource : IDisposable
        {
            var dic = new DisposableDictionary <TKey, TSource>(source.Count())
            {
                FallbackValue = defaultValue
            };

            foreach (var item in source)
            {
                dic[keySelector(item)] = item;
            }

            return(dic);
        }
Exemplo n.º 6
0
 public ThreadWithMessageQueue(Func <T, DisposableDictionary, object> handleMessage)
 {
     messageQueue = new BlockingCollection <Internal>(new ConcurrentQueue <Internal>());
     state        = new DisposableDictionary();
     thread       = new Thread(new ThreadStart(() =>
     {
         foreach (var message in messageQueue.GetConsumingEnumerable())
         {
             var res = handleMessage(message.payload, state);
             message.tcs.SetResult(res);
         }
     }));
     thread.SetApartmentState(ApartmentState.STA);
     thread.Start();
 }
Exemplo n.º 7
0
        private object HandleMenuMessage(Dictionary <string, object> message, DisposableDictionary table)
        {
            switch (message.Get("Arguments", ""))
            {
            case "LoadContextMenu":
                var contextMenuResponse = new ValueSet();
                var filePath            = (string)message["FilePath"];
                var extendedMenu        = (bool)message["ExtendedMenu"];
                var showOpenMenu        = (bool)message["ShowOpenMenu"];
                var split     = filePath.Split('|').Where(x => !string.IsNullOrWhiteSpace(x));
                var cMenuLoad = ContextMenu.GetContextMenuForFiles(split.ToArray(),
                                                                   (extendedMenu ? Shell32.CMF.CMF_EXTENDEDVERBS : Shell32.CMF.CMF_NORMAL) | Shell32.CMF.CMF_SYNCCASCADEMENU, FilterMenuItems(showOpenMenu));
                table.SetValue("MENU", cMenuLoad);
                return(cMenuLoad);

            case "ExecAndCloseContextMenu":
                var cMenuExec = table.GetValue <ContextMenu>("MENU");
                if (message.TryGetValue("ItemID", out var menuId))
                {
                    switch (message.Get("CommandString", (string)null))
                    {
                    case "mount":
                        var vhdPath = cMenuExec.ItemsPath.First();
                        Win32API.MountVhdDisk(vhdPath);
                        break;

                    case "format":
                        var drivePath = cMenuExec.ItemsPath.First();
                        Win32API.OpenFormatDriveDialog(drivePath);
                        break;

                    default:
                        cMenuExec?.InvokeItem((int)(long)menuId);
                        break;
                    }
                }
                // The following line is needed to cleanup resources when menu is closed.
                // Unfortunately if you uncomment it some menu items will randomly stop working.
                // Resource cleanup is currently done on app closing,
                // if we find a solution for the issue above, we should cleanup as soon as a menu is closed.
                //table.RemoveValue("MENU");
                return(null);

            default:
                return(null);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// 压缩多个文件
        /// </summary>
        /// <param name="streams">多个文件流</param>
        /// <param name="zipFile">压缩到...</param>
        /// <param name="archiveType"></param>
        /// <param name="disposeAllStreams">是否需要释放所有流</param>
        public void Zip(DisposableDictionary <string, Stream> streams, string zipFile, ArchiveType archiveType = ArchiveType.Zip, bool disposeAllStreams = false)
        {
            using var archive = ArchiveFactory.Create(archiveType);
            foreach (var pair in streams)
            {
                archive.AddEntry(pair.Key, pair.Value, true);
            }

            archive.SaveTo(zipFile, new WriterOptions(CompressionType.LZMA)
            {
                LeaveStreamOpen = true,
                ArchiveEncoding = new ArchiveEncoding()
                {
                    Default = Encoding.UTF8
                }
            });
            if (disposeAllStreams)
            {
                streams.Dispose();
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 将多个文件压缩到一个文件流中,可保存为zip文件,方便于web方式下载
        /// </summary>
        /// <param name="streams">多个文件流</param>
        /// <param name="archiveType"></param>
        /// <param name="disposeAllStreams">是否需要释放所有流</param>
        /// <returns>文件流</returns>
        public static MemoryStream ZipStream(DisposableDictionary <string, Stream> streams, ArchiveType archiveType = ArchiveType.Zip, bool disposeAllStreams = false)
        {
            using var archive = ArchiveFactory.Create(archiveType);
            foreach (var pair in streams)
            {
                archive.AddEntry(pair.Key, pair.Value, true);
            }

            var ms = new MemoryStream();

            archive.SaveTo(ms, new WriterOptions(CompressionType.LZMA)
            {
                LeaveStreamOpen = true,
                ArchiveEncoding = new ArchiveEncoding()
                {
                    Default = Encoding.UTF8
                }
            });
            if (disposeAllStreams)
            {
                streams.Dispose();
            }
            return(ms);
        }
Exemplo n.º 10
0
 public FileOperationsHandler()
 {
     // Create handle table to store context menu references
     handleTable = new DisposableDictionary();
 }
Exemplo n.º 11
0
 public ContextMenuHandler()
 {
     // Create handle table to store context menu references
     handleTable = new DisposableDictionary();
 }
Exemplo n.º 12
0
        private object HandleMenuMessage(Dictionary <string, object> message, DisposableDictionary table)
        {
            switch (message.Get("Arguments", ""))
            {
            case "LoadContextMenu":
                var contextMenuResponse = new ValueSet();
                var filePath            = (string)message["FilePath"];
                var extendedMenu        = (bool)message["ExtendedMenu"];
                var showOpenMenu        = (bool)message["ShowOpenMenu"];
                var split     = filePath.Split('|').Where(x => !string.IsNullOrWhiteSpace(x));
                var cMenuLoad = ContextMenu.GetContextMenuForFiles(split.ToArray(),
                                                                   (extendedMenu ? Shell32.CMF.CMF_EXTENDEDVERBS : Shell32.CMF.CMF_NORMAL) | Shell32.CMF.CMF_SYNCCASCADEMENU, FilterMenuItems(showOpenMenu));
                table.SetValue("MENU", cMenuLoad);
                return(cMenuLoad);

            case "ExecAndCloseContextMenu":
                var cMenuExec = table.GetValue <ContextMenu>("MENU");
                if (message.TryGetValue("ItemID", out var menuId))
                {
                    var isFont = new[] { ".fon", ".otf", ".ttc", ".ttf" }.Contains(Path.GetExtension(cMenuExec.ItemsPath[0]), StringComparer.OrdinalIgnoreCase);
                    var verb = message.Get("CommandString", (string)null);
                    switch (verb)
                    {
                    case string _ when verb == "install" && isFont:
                    {
                        var userFontDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "Fonts");
                        var destName    = Path.Combine(userFontDir, Path.GetFileName(cMenuExec.ItemsPath[0]));
                        Win32API.RunPowershellCommand($"-command \"Copy-Item '{cMenuExec.ItemsPath[0]}' '{userFontDir}'; New-ItemProperty -Name '{Path.GetFileNameWithoutExtension(cMenuExec.ItemsPath[0])}' -Path 'HKCU:\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts' -PropertyType string -Value '{destName}'\"", false);
                    }
                    break;

                    case string _ when verb == "installAllUsers" && isFont:
                    {
                        var winFontDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Fonts");
                        Win32API.RunPowershellCommand($"-command \"Copy-Item '{cMenuExec.ItemsPath[0]}' '{winFontDir}'; New-ItemProperty -Name '{Path.GetFileNameWithoutExtension(cMenuExec.ItemsPath[0])}' -Path 'HKLM:\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts' -PropertyType string -Value '{Path.GetFileName(cMenuExec.ItemsPath[0])}'\"", true);
                    }
                    break;

                    case string _ when verb == "mount":
                        var vhdPath = cMenuExec.ItemsPath[0];
                        Win32API.MountVhdDisk(vhdPath);
                        break;

                    case string _ when verb == "format":
                        var drivePath = cMenuExec.ItemsPath[0];
                        Win32API.OpenFormatDriveDialog(drivePath);
                        break;

                    default:
                        cMenuExec?.InvokeItem((int)(long)menuId);
                        break;
                    }
                }
                // The following line is needed to cleanup resources when menu is closed.
                // Unfortunately if you uncomment it some menu items will randomly stop working.
                // Resource cleanup is currently done on app closing,
                // if we find a solution for the issue above, we should cleanup as soon as a menu is closed.
                //table.RemoveValue("MENU");
                return(null);

            default:
                return(null);
            }
        }
Exemplo n.º 13
0
 public static DisposableDictionary GetDisposable()
 {
     return(DisposableDictionary.Make());
 }