Made Icons as reliable as possible

Icon logic works well now except for a single issue.
It will not change the shortcut image if the profile
or the game has been changed, and it needs to.
This commit is contained in:
Terry MacDonald 2020-10-19 22:58:23 +13:00
parent e34d59c1f7
commit 464545fbc7
5 changed files with 81 additions and 138 deletions

View File

@ -498,37 +498,6 @@ namespace HeliosPlus.GameLibraries
// Construct the full path to the game dir from the appInfo and libraryAppManifest data
string steamGameInstallDir = Path.Combine(steamLibraryPath, @"steamapps", @"common", steamAppInfo[steamGameId].GameInstallDir);
// Next, we need to get the Icons we want to use, and make sure it's the latest one.
string steamGameIconPath = "";
// First of all, we attempt to use the Icon that Steam has cached, if it's available, as that will be updated to the latest
if (File.Exists(steamAppInfo[steamGameId].GameSteamIconPath) && steamAppInfo[steamGameId].GameSteamIconPath.EndsWith(".ico"))
{
steamGameIconPath = steamAppInfo[steamGameId].GameSteamIconPath;
}
// If there isn't an icon for us to use, then we need to extract one from the Game Executables
else if (steamAppInfo[steamGameId].GameExes.Count > 0)
{
foreach (string gameExe in steamAppInfo[steamGameId].GameExes)
{
steamGameExe = Path.Combine(steamGameInstallDir, gameExe);
// If the game executable exists, then we can proceed
if (File.Exists(steamGameExe))
{
// Now we need to get the Icon from the app if possible if it's not in the games folder
steamGameIconPath = steamGameExe;
break;
}
}
}
// The absolute worst case means we don't have an icon to use. SO we use the Steam one.
else
{
// And we have to make do with a Steam Icon
steamGameIconPath = _steamPath;
}
// And finally we try to populate the 'where', to see what gets run
// And so we can extract the process name
if (steamAppInfo[steamGameId].GameExes.Count > 0)
@ -545,6 +514,25 @@ namespace HeliosPlus.GameLibraries
}
// Next, we need to get the Icons we want to use, and make sure it's the latest one.
string steamGameIconPath = "";
// First of all, we attempt to use the Icon that Steam has cached, if it's available, as that will be updated to the latest
if (File.Exists(steamAppInfo[steamGameId].GameSteamIconPath) && steamAppInfo[steamGameId].GameSteamIconPath.EndsWith(".ico"))
{
steamGameIconPath = steamAppInfo[steamGameId].GameSteamIconPath;
}
// If there isn't an icon for us to use, then we need to extract one from the Game Executables
else if (!String.IsNullOrEmpty(steamGameExe))
{
steamGameIconPath = steamGameExe;
}
// The absolute worst case means we don't have an icon to use. SO we use the Steam one.
else
{
// And we have to make do with a Steam Icon
steamGameIconPath = _steamPath;
}
// And we add the Game to the list of games we have!
_allSteamGames.Add(new SteamGame(steamGameId, steamGameName, steamGameExe, steamGameIconPath));

View File

@ -287,8 +287,9 @@ namespace HeliosPlus
// We create the ShortcutBitmap from the OriginalBitmap
// (We only do it if there is a valid profile)
if (_profileToUse is ProfileItem)
_shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
//if (_profileToUse is ProfileItem)
// _shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
_shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
}
@ -319,9 +320,9 @@ namespace HeliosPlus
// We create the ShortcutBitmap from the OriginalBitmap
// (We only do it if there is a valid profile)
if (_profileToUse is ProfileItem)
_shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
//if (_profileToUse is ProfileItem)
// _shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
_shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
}
public ShortcutItem(string name, string profileUuid, Executable executable, ShortcutPermanence permanence, string originalIconPath,
@ -360,13 +361,24 @@ namespace HeliosPlus
}
// We create the OriginalBitmap from the IconPath
if (_originalIconPath.EndsWith(".ico"))
{
Icon icoIcon = new Icon(_originalIconPath, 256, 256);
//_originalBitmap = ExtractVistaIcon(biggestIcon);
_originalLargeBitmap = icoIcon.ToBitmap();
icoIcon.Dispose();
}
else
{
_originalLargeBitmap = ToLargeBitmap(_originalIconPath);
}
_originalLargeBitmap = ToLargeBitmap(_originalIconPath);
// We create the ShortcutBitmap from the OriginalBitmap
// (We only do it if there is a valid profile)
if (_profileToUse is ProfileItem)
_shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
//if (_profileToUse is ProfileItem)
// _shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
_shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
}
@ -652,24 +664,10 @@ namespace HeliosPlus
_originalIconPath = value;
// And we do the same for the OriginalBitmap
_originalLargeBitmap = ToLargeBitmap(_originalIconPath);
//_originalLargeBitmap = ToLargeBitmap(_originalIconPath);
}
}
/*[JsonConverter(typeof(CustomBitmapConverter))]
public Bitmap OriginalSmallBitmap
{
get
{
return _originalSmallBitmap;
}
set
{
_originalSmallBitmap = value;
}
}*/
[JsonConverter(typeof(CustomBitmapConverter))]
public Bitmap OriginalLargeBitmap
{
@ -683,8 +681,8 @@ namespace HeliosPlus
_originalLargeBitmap = value;
// And we do the same for the Bitmap overlay, but only if the ProfileToUse is set
if (_profileToUse is ProfileItem)
_shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
//if (_profileToUse is ProfileItem)
// _shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
}
}
@ -942,8 +940,8 @@ namespace HeliosPlus
}
return bitmap;
}*/
/*private Bitmap ToBitmapFromIcon(string fileNameAndPath)
/*
public Bitmap ToBitmapFromIcon(string fileNameAndPath)
{
if (String.IsNullOrWhiteSpace(fileNameAndPath))
return null;
@ -954,21 +952,41 @@ namespace HeliosPlus
return bitmapToReturn;
}*/
private Bitmap ToLargeBitmap(string fileNameAndPath)
public static Bitmap ToLargeBitmap(string fileNameAndPath)
{
if (String.IsNullOrWhiteSpace(fileNameAndPath))
return null;
Bitmap bm = IconFromFile.GetLargeBitmapFromFile(fileNameAndPath, true, true);
Bitmap bm = null
; if (fileNameAndPath.EndsWith(".ico"))
{
Icon icoIcon = new Icon(fileNameAndPath, 256, 256);
bm = icoIcon.ToBitmap();
icoIcon.Dispose();
}
else
{
bm = IconFromFile.GetLargeBitmapFromFile(fileNameAndPath, true, true);
}
return bm;
}
private Bitmap ToSmallBitmap(string fileNameAndPath)
public static Bitmap ToSmallBitmap(string fileNameAndPath)
{
if (String.IsNullOrWhiteSpace(fileNameAndPath))
return null;
Bitmap bm = IconFromFile.GetSmallBitmapFromFile(fileNameAndPath, false, true, false);
Bitmap bm = null;
if (fileNameAndPath.EndsWith(".ico"))
{
Icon icoIcon = new Icon(fileNameAndPath, 128, 128);
bm = icoIcon.ToBitmap();
icoIcon.Dispose();
}
else
{
bm = IconFromFile.GetSmallBitmapFromFile(fileNameAndPath, false, true, false);
}
return bm;
}

View File

@ -402,67 +402,6 @@ namespace HeliosPlus
return false;
}
/* private static void SaveShortcutIconToCache(ShortcutItem shortcut)
{
// Force the creation of the ShortcutBitmap
// Only add the rest of the options if the permanence is temporary
if (shortcut.Permanence == ShortcutPermanence.Temporary)
{
// Only add this set of options if the shortcut is to an standalone application
if (shortcut.Category == ShortcutCategory.Application)
{
// Work out the name of the shortcut we'll save.
shortcut.SavedShortcutIconCacheFilename = Path.Combine(AppShortcutStoragePath, String.Concat(@"executable-", shortcut.ProfileToUse.UUID, "-", Path.GetFileNameWithoutExtension(shortcut.ExecutableNameAndPath), @".ico"));
}
// Only add the rest of the options if the temporary switch radio button is set
// and if the game launching radio button is set
else if (shortcut.Permanence == ShortcutPermanence.Temporary)
{
// TODO need to make this work so at least one game library is installed
// i.e. if (!SteamGame.SteamInstalled && !UplayGame.UplayInstalled )
if (shortcut.GameLibrary == SupportedGameLibrary.Steam)
{
// Work out the name of the shortcut we'll save.
shortcut.SavedShortcutIconCacheFilename = Path.Combine(AppShortcutStoragePath, String.Concat(@"steam-", shortcut.ProfileToUse.UUID, "-", shortcut.GameAppId.ToString(), @".ico"));
}
else if (shortcut.GameLibrary == SupportedGameLibrary.Uplay)
{
// Work out the name of the shortcut we'll save.
shortcut.SavedShortcutIconCacheFilename = Path.Combine(AppShortcutStoragePath, String.Concat(@"uplay-", shortcut.ProfileToUse.UUID, "-", shortcut.GameAppId.ToString(), @".ico"));
}
}
}
// Only add the rest of the options if the shortcut is permanent
else
{
// Work out the name of the shortcut we'll save.
shortcut.SavedShortcutIconCacheFilename = Path.Combine(AppShortcutStoragePath, String.Concat(@"permanent-", shortcut.ProfileToUse.UUID, @".ico"));
}
MultiIcon shortcutIcon;
try
{
//shortcutIcon = new ProfileIcon(shortcut.ProfileToUse).ToIconOverlay(shortcut.OriginalIconPath);
shortcutIcon = shortcut.ToIconOverlay();
shortcutIcon.Save(shortcut.SavedShortcutIconCacheFilename, MultiIconFormat.ICO);
}
catch (Exception ex)
{
Console.WriteLine($"ShortcutRepository/SaveShortcutIconToCache exception: {ex.Message}: {ex.StackTrace} - {ex.InnerException}");
// If we fail to create an icon based on the original executable or game
// Then we use the standard HeliosPlus profile one.
shortcutIcon = shortcut.ProfileToUse.ProfileIcon.ToIcon();
shortcutIcon.Save(shortcut.SavedShortcutIconCacheFilename, MultiIconFormat.ICO);
}
}*/
// ReSharper disable once CyclomaticComplexity
public static void RunShortcut(ShortcutItem shortcutToUse)

View File

@ -147,6 +147,7 @@ namespace HeliosPlus.UIForms
catch (Exception ex)
{
// catch the System.Drawing.Image exception created while accessing the Size
mySize = new Size(256, 256);
}
}
@ -164,6 +165,7 @@ namespace HeliosPlus.UIForms
catch (Exception ex)
{
// catch the System.Drawing.Image exception created while accessing the Size
mySizeF = new SizeF(256, 256);
}
}

View File

@ -488,13 +488,15 @@ namespace HeliosPlus.UIForms
_autoName
);
}
// Generate the Shortcut Icon ready to be used
_shortcutToEdit.SaveShortcutIconToCache();
// Add the Shortcut to the list of saved Shortcuts so it gets saved for later
// but only if it's new... if it is an edit then it will already be in the list.
if (_isNewShortcut)
{
// Generate the Shortcut Icon ready to be used
_shortcutToEdit.SaveShortcutIconToCache();
//SAve the shortcut to the Shortcut Repository
ShortcutRepository.AddShortcut(_shortcutToEdit);
}
@ -668,25 +670,19 @@ namespace HeliosPlus.UIForms
// Load the Games ListView
foreach (var game in SteamLibrary.AllInstalledGames.OrderBy(game => game.Name))
{
// Get the bitmap out of the IconPath
// IconPath can be an ICO, or an EXE
Bitmap bm = null;
try
{
bm = IconFromFile.GetSmallBitmapFromFile(game.IconPath, false, true, false);
bm = ShortcutItem.ToSmallBitmap(game.IconPath);
}
catch (Exception ex)
catch (Exception innerEx)
{
Console.WriteLine($"ShortcutForm exception: {ex.Message}: {ex.StackTrace} - {ex.InnerException}");
}
try
{
bm = IconFromFile.GetSmallBitmapFromFile(game.ExePath, false, true, false);
}
catch (Exception ex)
{
Console.WriteLine($"ShortcutForm exception: {ex.Message}: {ex.StackTrace} - {ex.InnerException}");
Console.WriteLine($"ShortcutForm exception: {innerEx.Message}: {innerEx.StackTrace} - {innerEx.InnerException}");
bm = Properties.Resources.Steam.ToBitmap();
}
// Add the images to the images array
il_games.Images.Add(bm);