Skip to content

Commit

Permalink
added icons caching
Browse files Browse the repository at this point in the history
  • Loading branch information
MuxaJIbI4 committed Sep 10, 2014
1 parent 1d84fa8 commit 6a7494a
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions VisualGGPK/IconReader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
Expand All @@ -24,6 +25,9 @@ internal sealed class IconReader : IconNativeMethods
// return icon;
// }

private static Icon _directoryIcon;
private static Dictionary<string, Icon> _fileIcons = new Dictionary<string, Icon>();

#region OfExtension

///<summary>
Expand All @@ -34,23 +38,21 @@ internal sealed class IconReader : IconNativeMethods
///<returns>Icon</returns>
public static Icon OfExtension(string filename, bool overlay = false)
{
string filepath;
var extension = filename.Split('.');
var dirpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "cache");
var extension = Path.GetExtension(filename) ?? String.Empty;
if (_fileIcons.ContainsKey(extension))
return _fileIcons[extension];

var testName = (String.IsNullOrEmpty(filename) || String.IsNullOrEmpty(extension))
? "dummy_file"
: "dummy" + extension;
var dirpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "_icon_cache");
Directory.CreateDirectory(dirpath);
if (String.IsNullOrEmpty(filename) || extension.Length == 1)
{
filepath = Path.Combine(dirpath, "dummy_file");
}
else
{
filepath = Path.Combine(dirpath, String.Join(".", "dummy", extension[extension.Length - 1]));
}
string filepath = Path.Combine(dirpath, testName);

if (File.Exists(filepath) == false)
{
File.Create(filepath);
}
var icon = OfPath(filepath, true, true, overlay);
_fileIcons[extension] = icon;
return icon;
}
#endregion
Expand All @@ -64,9 +66,14 @@ public static Icon OfExtension(string filename, bool overlay = false)
///<param name="overlay">bool symlink overlay</param>
public static Icon OfFolder(bool overlay = false)
{
var dirpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "cache", "dummy");
if (_directoryIcon != null)
{
return _directoryIcon;
}
var dirpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "_icon_cache", "dummy");
Directory.CreateDirectory(dirpath);
var icon = OfPath(dirpath, true, true, overlay);
_directoryIcon = icon;
return icon;
}
#endregion
Expand All @@ -81,7 +88,7 @@ public static Icon OfFolder(bool overlay = false)
///<param name="checkdisk">bool fileicon</param>
///<param name="overlay">bool symlink overlay</param>
///<returns>Icon</returns>
public static Icon OfPath(string filepath, bool small = true, bool checkdisk = true, bool overlay = false)
private static Icon OfPath(string filepath, bool small = true, bool checkdisk = true, bool overlay = false)
{
SHGFI_Flag flags;
var shinfo = new SHFILEINFO();
Expand Down

0 comments on commit 6a7494a

Please sign in to comment.