Skip to content

Commit 990e331

Browse files
committed
Prevent Package Managers from doing certain operations when not enabled or not found (fix #2052)
1 parent a90ac97 commit 990e331

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/UniGetUI.PackageEngine.PackageManagerClasses/Manager/PackageManager.cs

+16-3
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,8 @@ public virtual async Task InitializeAsync()
6363
{
6464
Status = await LoadManager();
6565

66-
67-
if (SourceProvider != null && Status.Found)
66+
if (IsReady() && SourceProvider != null)
6867
{
69-
7068
Task<ManagerSource[]> SourcesTask = GetSources();
7169
Task winner = await Task.WhenAny(
7270
SourcesTask,
@@ -129,6 +127,15 @@ public bool IsEnabled()
129127
return !Settings.Get("Disable" + Name);
130128
}
131129

130+
/// <summary>
131+
/// Returns true if the manager is enabled and available (the required executable files were found). Returns false otherwise
132+
/// </summary>
133+
/// <returns></returns>
134+
public bool IsReady()
135+
{
136+
return IsEnabled() && Status.Found;
137+
}
138+
132139
/// <summary>
133140
/// Returns an array of Package objects that the manager lists for the given query. Depending on the manager, the list may
134141
/// also include similar results. This method is fail-safe and will return an empty array if an error occurs.
@@ -137,6 +144,7 @@ public bool IsEnabled()
137144
/// <returns></returns>
138145
public async Task<Package[]> FindPackages(string query)
139146
{
147+
if (!IsReady()) { Logger.Warn($"Manager {Name} is disabled but yet FindPackages was called"); return []; };
140148
try
141149
{
142150
Package[] packages = await FindPackages_UnSafe(query);
@@ -163,6 +171,7 @@ public async Task<Package[]> FindPackages(string query)
163171
/// <returns></returns>
164172
public async Task<Package[]> GetAvailableUpdates()
165173
{
174+
if (!IsReady()) { Logger.Warn($"Manager {Name} is disabled but yet GetAvailableUpdates was called"); return []; };
166175
try
167176
{
168177
await RefreshPackageIndexes();
@@ -187,6 +196,7 @@ public async Task<Package[]> GetAvailableUpdates()
187196
/// <returns></returns>
188197
public async Task<Package[]> GetInstalledPackages()
189198
{
199+
if (!IsReady()) { Logger.Warn($"Manager {Name} is disabled but yet GetInstalledPackages was called"); return []; };
190200
try
191201
{
192202
Package[] packages = await GetInstalledPackages_UnSafe();
@@ -348,6 +358,7 @@ public OperationVeredict GetRemoveSourceOperationVeredict(ManagerSource source,
348358
}
349359
public virtual async Task<ManagerSource[]> GetSources()
350360
{
361+
if (!IsReady()) { Logger.Warn($"Manager {Name} is disabled but yet GetSources was called"); return []; };
351362
try
352363
{
353364
AssertSourceCompatibility("GetSources");
@@ -381,6 +392,7 @@ private void AssertPackageDetailsCompatibility(string MethodName)
381392
#pragma warning disable CS8602
382393
public async Task<PackageDetails> GetPackageDetails(Package package)
383394
{
395+
if (!IsReady()) { Logger.Warn($"Manager {Name} is disabled but yet GetPackageDetails was called"); return new(package); };
384396
try
385397
{
386398
AssertPackageDetailsCompatibility("GetPackageDetails");
@@ -398,6 +410,7 @@ public async Task<PackageDetails> GetPackageDetails(Package package)
398410

399411
public async Task<string[]> GetPackageVersions(Package package)
400412
{
413+
if (!IsReady()) { Logger.Warn($"Manager {Name} is disabled but yet GetPackageVersions was called"); return []; };
401414
try
402415
{
403416
AssertPackageDetailsCompatibility("GetPackageVersions");

src/UniGetUI/Interface/Widgets/SourceManager.xaml.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public SourceManager(PackageManager Manager)
153153

154154
public async void LoadSources()
155155
{
156-
if (!Manager.Status.Found)
156+
if (!Manager.IsReady())
157157
return;
158158

159159
LoadingBar.Visibility = Visibility.Visible;

0 commit comments

Comments
 (0)