Skip to content

Commit 8008da7

Browse files
committed
SpecialKHelper: Fix plugin failing to load if custom install path has not been set
1 parent d6525be commit 8008da7

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

source/Generic/SpecialKHelper/Core/Application/SpecialKHelper.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class SpecialKHelper : GenericPlugin
4848
public SpecialKHelper(IPlayniteAPI api) : base(api)
4949
{
5050
_specialKServiceManager = new SpecialKServiceManager(_logger);
51-
settings = new SpecialKHelperSettingsViewModel(this, _specialKServiceManager);
51+
settings = new SpecialKHelperSettingsViewModel(this, _specialKServiceManager, _logger);
5252
Properties = new GenericPluginProperties
5353
{
5454
HasSettings = true
@@ -59,7 +59,6 @@ public SpecialKHelper(IPlayniteAPI api) : base(api)
5959
_sidebarItemSwitcherViewModel = new SidebarItemSwitcherViewModel(true, _pluginInstallPath, _specialKServiceManager, _specialKProfilesEditor);
6060
_easyAnticheatHelper = new EasyAnticheatService(new EasyAnticheatCache(GetPluginUserDataPath()));
6161
_steamHelper = new SteamHelper(GetPluginUserDataPath(), PlayniteApi);
62-
_specialKServiceManager.SetSpecialKInstallDirectory(Path.GetDirectoryName(settings.Settings.CustomSpecialKPath));
6362
}
6463

6564
public override IEnumerable<SidebarItem> GetSidebarItems()

source/Generic/SpecialKHelper/Core/Application/SpecialKHelperSettings.cs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,27 @@ public class SpecialKHelperSettings : ObservableObject
5353
public class SpecialKHelperSettingsViewModel : ObservableObject, ISettings
5454
{
5555
private readonly SpecialKHelper _plugin;
56+
private readonly ILogger _logger;
5657
private readonly SpecialKServiceManager _specialKServiceManager;
5758

58-
private SpecialKHelperSettings editingClone { get; set; }
59+
private SpecialKHelperSettings _editingClone;
5960

60-
private SpecialKHelperSettings settings;
61+
private SpecialKHelperSettings _settings;
6162
public SpecialKHelperSettings Settings
6263
{
63-
get => settings;
64+
get => _settings;
6465
set
6566
{
66-
settings = value;
67+
_settings = value;
6768
OnPropertyChanged();
6869
}
6970
}
7071

71-
public SpecialKHelperSettingsViewModel(SpecialKHelper plugin, SpecialKServiceManager specialKServiceManager)
72+
public SpecialKHelperSettingsViewModel(SpecialKHelper plugin, SpecialKServiceManager specialKServiceManager, ILogger logger)
7273
{
7374
// Injecting your plugin instance is required for Save/Load method because Playnite saves data to a location based on what plugin requested the operation.
7475
_plugin = plugin;
76+
_logger = logger;
7577
_specialKServiceManager = specialKServiceManager;
7678
// Load saved settings.
7779
var savedSettings = plugin.LoadPluginSettings<SpecialKHelperSettings>();
@@ -85,27 +87,48 @@ public SpecialKHelperSettingsViewModel(SpecialKHelper plugin, SpecialKServiceMan
8587
{
8688
Settings = new SpecialKHelperSettings();
8789
}
90+
UpdateSpecialKServiceSettings();
8891
}
8992

9093
public void BeginEdit()
9194
{
9295
// Code executed when settings view is opened and user starts editing values.
93-
editingClone = Serialization.GetClone(Settings);
96+
_editingClone = Serialization.GetClone(Settings);
9497
}
9598

9699
public void CancelEdit()
97100
{
98101
// Code executed when user decides to cancel any changes made since BeginEdit was called.
99102
// This method should revert any changes made to Option1 and Option2.
100-
Settings = editingClone;
103+
Settings = _editingClone;
101104
}
102105

103106
public void EndEdit()
104107
{
105108
// Code executed when user decides to confirm changes made since BeginEdit was called.
106109
// This method should save settings made to Option1 and Option2.
107110
_plugin.SavePluginSettings(Settings);
108-
_specialKServiceManager.SetSpecialKInstallDirectory(Path.GetDirectoryName(settings.CustomSpecialKPath));
111+
UpdateSpecialKServiceSettings();
112+
}
113+
114+
private void UpdateSpecialKServiceSettings()
115+
{
116+
try
117+
{
118+
if (!_settings.CustomSpecialKPath.IsNullOrEmpty() && FileSystem.FileExists(_settings.CustomSpecialKPath))
119+
{
120+
var directory = Path.GetDirectoryName(string.Empty);
121+
_specialKServiceManager.SetSpecialKInstallDirectory(directory);
122+
}
123+
else
124+
{
125+
_specialKServiceManager.ResetSpecialKInstallDirectory();
126+
}
127+
}
128+
catch (Exception ex)
129+
{
130+
_logger.Error(ex, $"Failed to set Special K installation from executable: {_settings.CustomSpecialKPath ?? "null"}");
131+
}
109132
}
110133

111134
public bool VerifySettings(out List<string> errors)
@@ -132,7 +155,7 @@ public RelayCommand BrowseSelectSpecialKExecutableCommand
132155
var filePath = _plugin.PlayniteApi.Dialogs.SelectFile("SKIF|SKIF.exe");
133156
if (!filePath.IsNullOrEmpty())
134157
{
135-
settings.CustomSpecialKPath = filePath;
158+
_settings.CustomSpecialKPath = filePath;
136159
}
137160
});
138161
}
@@ -141,7 +164,7 @@ public RelayCommand RemoveSpecialKExecutableCommand
141164
{
142165
get => new RelayCommand(() =>
143166
{
144-
settings.CustomSpecialKPath = string.Empty;
167+
_settings.CustomSpecialKPath = string.Empty;
145168
});
146169
}
147170
}

source/Generic/SpecialKHelper/SpecialKHandler/Application/SpecialKServiceManager.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,30 @@ public bool Is64BitsServiceRunning()
127127
return IsProcessRunning(_64BitsServiceProcessName);
128128
}
129129

130-
internal void SetSpecialKInstallDirectory(string customSpecialKPath)
130+
public void ResetSpecialKInstallDirectory()
131+
{
132+
if (!_customSpecialKInstallationPath.IsNullOrEmpty())
133+
{
134+
_customSpecialKInstallationPath = string.Empty;
135+
_logger.Info($"Special K installation path has been reset");
136+
}
137+
}
138+
139+
internal bool SetSpecialKInstallDirectory(string customSpecialKPath)
131140
{
132141
if (customSpecialKPath.IsNullOrWhiteSpace() || !FileSystem.DirectoryExists(customSpecialKPath))
133142
{
134143
_logger.Info($"Failed to set Special K installation path: {customSpecialKPath ?? "null"}. Directory does not exist.");
135-
return;
144+
return false;
145+
}
146+
147+
if (_customSpecialKInstallationPath != customSpecialKPath)
148+
{
149+
_customSpecialKInstallationPath = customSpecialKPath;
150+
_logger.Info($"Special K installation path successfully set to: {_customSpecialKInstallationPath}");
136151
}
137152

138-
_customSpecialKInstallationPath = customSpecialKPath;
139-
_logger.Info($"Special K installation path successfully set to: {_customSpecialKInstallationPath}");
153+
return true;
140154
}
141155

142156
public string GetInstallDirectory()

0 commit comments

Comments
 (0)