Fixed problem with icons after rename

There was an issue where changing a profile broke
the link between the shortcuts and the shortcut icon
file. This meant that the runshortcut would be pointing
to an icon file that didn't exist. This has been fixed.
This commit is contained in:
Terry MacDonald 2020-11-28 17:55:34 +13:00
parent c05a2a0f0b
commit 19a8ccfe5a
6 changed files with 154 additions and 151 deletions

View File

@ -82,7 +82,7 @@ namespace HeliosPlus
private ProfileItem _profileToUse;
private string _uuid = "";
private string _name = "";
private ShortcutCategory _category = ShortcutCategory.NoGame;
private ShortcutCategory _category = ShortcutCategory.Game;
private string _differentExecutableToMonitor;
private string _executableNameAndPath = "";
private string _executableArguments;
@ -94,8 +94,8 @@ namespace HeliosPlus
private uint _startTimeout;
private string _gameArguments;
private bool _gameArgumentsRequired;
private ShortcutPermanence _permanence;
private bool _autoName;
private ShortcutPermanence _permanence = ShortcutPermanence.Temporary;
private bool _autoName = true;
private bool _isPossible;
private List<StartProgram> _startPrograms;
[JsonIgnore]
@ -833,6 +833,7 @@ namespace HeliosPlus
if (_profileToUse is ProfileItem)
_shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
ReplaceShortcutIconInCache();
}
public void UpdateNoGameShortcut(string name, string profileUuid, ShortcutPermanence permanence, string originalIconPath,
@ -871,6 +872,8 @@ namespace HeliosPlus
// (We only do it if there is a valid profile)
if (_profileToUse is ProfileItem)
_shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
ReplaceShortcutIconInCache();
}
@ -919,6 +922,7 @@ namespace HeliosPlus
if (_profileToUse is ProfileItem)
_shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
ReplaceShortcutIconInCache();
}
public void UpdateGameShortcut(string name, ProfileItem profile, GameStruct game, ShortcutPermanence permanence, string originalIconPath,
@ -951,6 +955,8 @@ namespace HeliosPlus
// (We only do it if there is a valid profile)
if (_profileToUse is ProfileItem)
_shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
ReplaceShortcutIconInCache();
}
@ -998,6 +1004,8 @@ namespace HeliosPlus
// (We only do it if there is a valid profile)
if (_profileToUse is ProfileItem)
_shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
ReplaceShortcutIconInCache();
}
public void UpdateExecutableShortcut(
@ -1044,6 +1052,7 @@ namespace HeliosPlus
// _shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
_shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
ReplaceShortcutIconInCache();
}
public void UpdateExecutableShortcut(string name, ProfileItem profile, Executable executable, ShortcutPermanence permanence, string originalIconPath,
@ -1076,6 +1085,8 @@ namespace HeliosPlus
//if (_profileToUse is ProfileItem)
// _shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
_shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
ReplaceShortcutIconInCache();
}
public void UpdateExecutableShortcut(string name, string profileUuid, Executable executable, ShortcutPermanence permanence, string originalIconPath,
@ -1132,10 +1143,12 @@ namespace HeliosPlus
//if (_profileToUse is ProfileItem)
// _shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
_shortcutBitmap = ToBitmapOverlay(_originalLargeBitmap, _profileToUse.ProfileTightestBitmap, 256, 256);
ReplaceShortcutIconInCache();
}
public bool CopyTo (ShortcutItem shortcut, bool overwriteUUID = false)
public bool CopyTo (ShortcutItem shortcut, bool overwriteUUID = true)
{
if (!(shortcut is ShortcutItem))
return false;
@ -1167,9 +1180,40 @@ namespace HeliosPlus
shortcut.IsPossible = IsPossible;
shortcut.StartPrograms = StartPrograms;
// Save the shortcut incon to the icon cache
shortcut.ReplaceShortcutIconInCache();
return true;
}
public void ReplaceShortcutIconInCache()
{
string newShortcutIconFilename = "";
if (_category == ShortcutCategory.Application)
{
// Work out the name of the shortcut we'll save.
newShortcutIconFilename = Path.Combine(Program.AppShortcutPath, String.Concat(@"executable-", _profileToUse.UUID, "-", Path.GetFileNameWithoutExtension(_executableNameAndPath), @".ico"));
}
else
{
// Work out the name of the shortcut we'll save.
newShortcutIconFilename = Path.Combine(Program.AppShortcutPath, String.Concat(_gameLibrary.ToString().ToLower(CultureInfo.InvariantCulture), @"-", _profileToUse.UUID, "-", _gameAppId.ToString(), @".ico"));
}
// If the new shortcut icon should be named differently
// then change the name of it
if (!newShortcutIconFilename.Equals(_savedShortcutIconCacheFilename))
{
if (System.IO.File.Exists(_savedShortcutIconCacheFilename))
System.IO.File.Delete(_savedShortcutIconCacheFilename);
SaveShortcutIconToCache();
}
}
public void SaveShortcutIconToCache()
{
@ -1202,6 +1246,7 @@ namespace HeliosPlus
shortcutIcon = _profileToUse.ProfileIcon.ToIcon();
shortcutIcon.Save(_savedShortcutIconCacheFilename, MultiIconFormat.ICO);
}
}
public static Bitmap ToLargeBitmap(string fileNameAndPath)

View File

@ -99,19 +99,8 @@ namespace HeliosPlus
if (!(shortcut is ShortcutItem))
return false;
// Doublecheck if it already exists
// Because then we just update the one that already exists
if (ContainsShortcut(shortcut))
{
// We update the existing Shortcut with the data over
ShortcutItem shortcutToUpdate = GetShortcut(shortcut.UUID);
shortcut.CopyTo(shortcutToUpdate);
}
else
{
// Add the shortcut to the list of shortcuts
_allShortcuts.Add(shortcut);
}
// Add the shortcut to the list of shortcuts
_allShortcuts.Add(shortcut);
//Doublecheck it's been added
if (ContainsShortcut(shortcut))
@ -129,6 +118,29 @@ namespace HeliosPlus
}
/* public static bool ReplaceShortcut(ShortcutItem shortcut)
{
if (!(shortcut is ShortcutItem))
return false;
// Doublecheck if it already exists
// Because then we just update the one that already exists
if (ContainsShortcut(shortcut))
{
// We update the existing Shortcut with the data over
ShortcutItem shortcutToUpdate = GetShortcut(shortcut.UUID);
shortcutToUpdate = shortcut;
// Save the shortcuts JSON as it's different
SaveShortcuts();
return true;
}
else
return false;
}*/
public static bool RemoveShortcut(ShortcutItem shortcut)
{
if (!(shortcut is ShortcutItem))
@ -308,56 +320,6 @@ namespace HeliosPlus
return true;
}
/* public static bool ReplaceShortcut(ShortcutItem oldShortcut, ShortcutItem newShortcut)
{
if (!(oldShortcut is ShortcutItem) || !(newShortcut is ShortcutItem))
return false;
// Remove the old Shortcut Icons from the Cache
List<ShortcutItem> shortcutsToRemove = _allShortcuts.FindAll(item => item.UUID.Equals(oldShortcut.UUID, StringComparison.InvariantCultureIgnoreCase));
foreach (ShortcutItem shortcutToRemove in shortcutsToRemove)
{
try
{
File.Delete(shortcutToRemove.SavedShortcutIconCacheFilename);
}
catch (Exception ex)
{
Console.WriteLine($"ShortcutRepository/ReplaceShortcut exception: {ex.Message}: {ex.StackTrace} - {ex.InnerException}");
// TODO check and report
}
}
// Remove the shortcut from the list.
int numRemoved = _allShortcuts.RemoveAll(item => item.UUID.Equals(shortcut.UUID, StringComparison.InvariantCultureIgnoreCase));
if (numRemoved == 1)
{
SaveShortcuts();
return true;
}
else if (numRemoved == 0)
return false;
else
throw new ShortcutRepositoryException();
foreach (ShortcutItem testShortcut in ShortcutRepository.AllShortcuts)
{
if (testShortcut.ProfileUUID.Equals(newProfile.UUID, StringComparison.InvariantCultureIgnoreCase) && testShortcut.AutoName)
{
testShortcut.ProfileToUse = newProfile;
testShortcut.AutoSuggestShortcutName();
}
}
SaveShortcuts();
return true;
}*/
private static bool LoadShortcuts()
{

View File

@ -94,8 +94,8 @@ namespace HeliosPlus.UIForms
private void pb_game_shortcut_Click(object sender, EventArgs e)
{
var shortcutForm = new ShortcutForm();
shortcutForm.ShowDialog(this);
var shortcutLibraryForm = new ShortcutLibraryForm();
shortcutLibraryForm.ShowDialog(this);
}
private void btn_setup_game_shortcuts_Click(object sender, EventArgs e)

View File

@ -794,11 +794,6 @@ namespace HeliosPlus.UIForms
0,
0,
0});
this.nud_timeout_executable.Minimum = new decimal(new int[] {
5,
0,
0,
0});
this.nud_timeout_executable.Name = "nud_timeout_executable";
this.nud_timeout_executable.Size = new System.Drawing.Size(70, 26);
this.nud_timeout_executable.TabIndex = 6;
@ -955,11 +950,6 @@ namespace HeliosPlus.UIForms
0,
0,
0});
this.nud_timeout_game.Minimum = new decimal(new int[] {
5,
0,
0,
0});
this.nud_timeout_game.Name = "nud_timeout_game";
this.nud_timeout_game.Size = new System.Drawing.Size(70, 26);
this.nud_timeout_game.TabIndex = 5;

View File

@ -28,14 +28,13 @@ namespace HeliosPlus.UIForms
List<StartProgram> _startPrograms = new List<StartProgram>();
private ShortcutItem _shortcutToEdit = null;
List<Game> allGames;
private bool _isNewShortcut = true;
private bool _isUnsaved = true;
private bool _loadedShortcut = false;
private bool _autoName = true;
private uint _gameId = 0;
private string _uuid = "";
public ShortcutForm()
public ShortcutForm(ShortcutItem shortcutToEdit)
{
InitializeComponent();
@ -43,6 +42,7 @@ namespace HeliosPlus.UIForms
// into the Profiles ImageListView
_profileAdaptor = new ProfileAdaptor();
_shortcutToEdit = shortcutToEdit;
// Create a new SHortcut if we are creating a new one
// And set up the page (otherwise this is all set when we load an
// existing Shortcut)
@ -53,12 +53,6 @@ namespace HeliosPlus.UIForms
}*/
}
public ShortcutForm(ShortcutItem shortcutToEdit) : this()
{
_shortcutToEdit = shortcutToEdit;
_isNewShortcut = false;
}
public string ProcessNameToMonitor
{
get
@ -260,8 +254,8 @@ namespace HeliosPlus.UIForms
return;
}
// Please use a plain name that can be
if (_isNewShortcut && ShortcutRepository.ContainsShortcut(txt_shortcut_save_name.Text))
/*// Please use a plain name that can be
if (ShortcutRepository.ContainsShortcut(txt_shortcut_save_name.Text))
{
MessageBox.Show(
@"A shortcut has already been created with this name. Please enter a different name for this shortcut.",
@ -269,7 +263,7 @@ namespace HeliosPlus.UIForms
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
return;
}
}*/
// Check the profile is set and that it's still valid
if (!(_profileToUse is ProfileItem))
@ -429,7 +423,17 @@ namespace HeliosPlus.UIForms
_gameToUse.GameArguments = txt_args_game.Text;
_gameToUse.GameArgumentsRequired = cb_args_game.Checked;
_shortcutToEdit = new ShortcutItem(
/* _shortcutToEdit = new ShortcutItem(
txt_shortcut_save_name.Text,
_profileToUse,
_gameToUse,
_permanence,
_gameToUse.GameToPlay.IconPath,
_startPrograms,
_autoName,
_uuid
);*/
_shortcutToEdit.UpdateGameShortcut(
txt_shortcut_save_name.Text,
_profileToUse,
_gameToUse,
@ -439,17 +443,7 @@ namespace HeliosPlus.UIForms
_autoName,
_uuid
);
/*_shortcutToEdit.UpdateGameShortcut(
Name,
_profileToUse,
_gameToUse,
_permanence,
_gameToUse.GameToPlay.IconPath,
_startPrograms,
_autoName,
_uuid
);*/
}
// If the game is a SteamGame
else if (txt_game_launcher.Text == SupportedGameLibrary.Uplay.ToString())
@ -461,7 +455,17 @@ namespace HeliosPlus.UIForms
_gameToUse.GameArguments = txt_args_game.Text;
_gameToUse.GameArgumentsRequired = cb_args_game.Checked;
_shortcutToEdit = new ShortcutItem(
/*_shortcutToEdit = new ShortcutItem(
txt_shortcut_save_name.Text,
_profileToUse,
_gameToUse,
_permanence,
_gameToUse.GameToPlay.IconPath,
_startPrograms,
_autoName,
_uuid
);*/
_shortcutToEdit.UpdateGameShortcut(
txt_shortcut_save_name.Text,
_profileToUse,
_gameToUse,
@ -492,7 +496,16 @@ namespace HeliosPlus.UIForms
_executableToUse.ProcessNameToMonitorUsesExecutable = true;
}
_shortcutToEdit = new ShortcutItem(
/*_shortcutToEdit = new ShortcutItem(
txt_shortcut_save_name.Text,
_profileToUse,
_executableToUse,
_permanence,
_executableToUse.ExecutableNameAndPath,
_startPrograms,
_autoName
);*/
_shortcutToEdit.UpdateExecutableShortcut(
txt_shortcut_save_name.Text,
_profileToUse,
_executableToUse,
@ -501,21 +514,12 @@ namespace HeliosPlus.UIForms
_startPrograms,
_autoName
);
/* _shortcutToEdit.UpdateExecutableShortcut(
Name,
_profileToUse,
_executableToUse,
_permanence,
_executableToUse.ExecutableNameAndPath,
_startPrograms,
_autoName
);
*/
}
else
{
_shortcutToEdit = new ShortcutItem(
/* _shortcutToEdit = new ShortcutItem(
txt_shortcut_save_name.Text,
_profileToUse,
_permanence,
@ -524,24 +528,17 @@ namespace HeliosPlus.UIForms
_autoName
);
/* _shortcutToEdit.UpdateNoGameShortcut(
Name,
*/ _shortcutToEdit.UpdateNoGameShortcut(
txt_shortcut_save_name.Text,
_profileToUse,
_permanence,
_executableToUse.ExecutableNameAndPath,
_startPrograms,
_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.
// We've saved, so mark it as so
_isUnsaved = false;
@ -747,16 +744,6 @@ namespace HeliosPlus.UIForms
});
}
// If it is a new Shortcut then we don't have to load anything!
if (_isNewShortcut)
{
RefreshShortcutUI();
ChangeSelectedProfile(ProfileRepository.CurrentProfile);
_isUnsaved = true;
return;
}
// We only get down here if the form has loaded a shortcut to edit
if (_shortcutToEdit is ShortcutItem && _shortcutToEdit.ProfileToUse is ProfileItem)
{
@ -819,11 +806,19 @@ namespace HeliosPlus.UIForms
}
// Now start populating the other fields
// If it is a new Shortcut then we don't have to load anything!
//if (_isNewShortcut)
//{
// RefreshShortcutUI();
// ChangeSelectedProfile(ProfileRepository.CurrentProfile);
// _isUnsaved = true;
// return;
//}
// Now start populating the other fields if they need it
_uuid = _shortcutToEdit.UUID;
// Set if we launch App/Game/NoGame
switch (_shortcutToEdit.Category)
{
@ -838,6 +833,16 @@ namespace HeliosPlus.UIForms
break;
}
switch (_shortcutToEdit.Permanence)
{
case ShortcutPermanence.Permanent:
rb_switch_permanent.Checked = true;
break;
case ShortcutPermanence.Temporary:
rb_switch_temp.Checked = true;
break;
}
// Set the launcher items if we have them
txt_game_launcher.Text = _shortcutToEdit.GameLibrary.ToString();
txt_game_name.Text = _shortcutToEdit.GameName;
@ -1197,11 +1202,16 @@ namespace HeliosPlus.UIForms
{
// If the user doesn't want to close this window without saving, then don't close the window.
DialogResult result = MessageBox.Show(
@"You have unsaved changes! Do you want to close this window without saving your changes?",
@"You have unsaved changes! Do you want to save your changes?",
@"You have unsaved changes.",
MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation);
e.Cancel = (result == DialogResult.No);
if (result == DialogResult.Yes)
{
// Press the save button for the user
btn_save.PerformClick();
}
}
}

View File

@ -159,24 +159,22 @@ namespace HeliosPlus.UIForms
if (_selectedShortcut == null)
return;
ShortcutItem oldShortcut = _selectedShortcut;
var shortcutForm = new ShortcutForm(_selectedShortcut);
shortcutForm.ShowDialog(this);
if (shortcutForm.DialogResult == DialogResult.OK)
{
ShortcutRepository.RemoveShortcut(oldShortcut);
_selectedShortcut = shortcutForm.Shortcut;
ShortcutRepository.AddShortcut(_selectedShortcut);
//ShortcutRepository.ReplaceShortcut(shortcutForm.Shortcut);
//_selectedShortcut = shortcutForm.Shortcut;
RefreshShortcutLibraryUI();
// As this is an edit, we need to manually force saving the shortcut library
//ShortcutRepository.SaveShortcuts();
ShortcutRepository.SaveShortcuts();
}
}
private void btn_new_Click(object sender, EventArgs e)
{
var shortcutForm = new ShortcutForm();
var shortcutForm = new ShortcutForm(new ShortcutItem());
shortcutForm.ShowDialog(this);
if (shortcutForm.DialogResult == DialogResult.OK)
{
@ -195,17 +193,15 @@ namespace HeliosPlus.UIForms
if (_selectedShortcut == null)
return;
ShortcutItem oldShortcut = _selectedShortcut;
var shortcutForm = new ShortcutForm(_selectedShortcut);
shortcutForm.ShowDialog(this);
if (shortcutForm.DialogResult == DialogResult.OK)
{
ShortcutRepository.RemoveShortcut(oldShortcut);
_selectedShortcut = shortcutForm.Shortcut;
ShortcutRepository.AddShortcut(_selectedShortcut);
//ShortcutRepository.ReplaceShortcut(shortcutForm.Shortcut);
//_selectedShortcut = shortcutForm.Shortcut;
RefreshShortcutLibraryUI();
// As this is an edit, we need to manually force saving the shortcut library
//ShortcutRepository.SaveShortcuts();
ShortcutRepository.SaveShortcuts();
}
}