|
1 | 1 | using System.Collections.Concurrent; |
| 2 | +using UniGetUI.Core.Logging; |
2 | 3 | using UniGetUI.Core.Tools; |
3 | 4 | using UniGetUI.PackageEngine.Interfaces; |
4 | 5 |
|
@@ -119,71 +120,89 @@ protected void InvokeFinishedLoadingEvent() |
119 | 120 | /// </summary> |
120 | 121 | public virtual async Task ReloadPackages() |
121 | 122 | { |
122 | | - if (DISABLE_RELOAD) |
| 123 | + try |
123 | 124 | { |
124 | | - InvokePackagesChangedEvent(false, [], []); |
125 | | - return; |
126 | | - } |
127 | | - |
128 | | - ClearPackages(emitFinishSignal: false); |
129 | | - LoadOperationIdentifier = new Random().Next(); |
130 | | - int current_identifier = LoadOperationIdentifier; |
131 | | - IsLoading = true; |
132 | | - StartedLoading?.Invoke(this, EventArgs.Empty); |
| 125 | + if (DISABLE_RELOAD) |
| 126 | + { |
| 127 | + InvokePackagesChangedEvent(false, [], []); |
| 128 | + return; |
| 129 | + } |
133 | 130 |
|
134 | | - if (REQUIRES_INTERNET) |
135 | | - { |
136 | | - await CoreTools.WaitForInternetConnection(); |
137 | | - } |
| 131 | + if (IsLoading) |
| 132 | + { |
| 133 | + Logger.Debug($"[{this.GetType()}] Packages are already being loaded!!!"); |
| 134 | + return; |
| 135 | + } |
138 | 136 |
|
139 | | - List<Task<IReadOnlyList<IPackage>>> tasks = []; |
| 137 | + ClearPackages(emitFinishSignal: false); |
| 138 | + LoadOperationIdentifier = new Random().Next(); |
| 139 | + int current_identifier = LoadOperationIdentifier; |
| 140 | + IsLoading = true; |
| 141 | + StartedLoading?.Invoke(this, EventArgs.Empty); |
140 | 142 |
|
141 | | - foreach (IPackageManager manager in Managers) |
142 | | - { |
143 | | - if (manager.IsReady()) |
| 143 | + if (REQUIRES_INTERNET) |
144 | 144 | { |
145 | | - Task<IReadOnlyList<IPackage>> task = Task.Run(() => LoadPackagesFromManager(manager)); |
146 | | - tasks.Add(task); |
| 145 | + await CoreTools.WaitForInternetConnection(); |
147 | 146 | } |
148 | | - } |
149 | 147 |
|
150 | | - while (tasks.Count > 0) |
151 | | - { |
152 | | - foreach (Task<IReadOnlyList<IPackage>> task in tasks.ToArray()) |
| 148 | + List<Task<IReadOnlyList<IPackage>>> tasks = []; |
| 149 | + |
| 150 | + foreach (IPackageManager manager in Managers) |
153 | 151 | { |
154 | | - if (!task.IsCompleted) |
| 152 | + if (manager.IsReady()) |
155 | 153 | { |
156 | | - await Task.Delay(100).ConfigureAwait(false); |
| 154 | + Task<IReadOnlyList<IPackage>> task = Task.Run(() => LoadPackagesFromManager(manager)); |
| 155 | + tasks.Add(task); |
157 | 156 | } |
| 157 | + } |
158 | 158 |
|
159 | | - if (task.IsCompleted) |
| 159 | + while (tasks.Count > 0) |
| 160 | + { |
| 161 | + foreach (Task<IReadOnlyList<IPackage>> task in tasks.ToArray()) |
160 | 162 | { |
161 | | - if (LoadOperationIdentifier == current_identifier && task.IsCompletedSuccessfully) |
| 163 | + if (!task.IsCompleted) |
162 | 164 | { |
163 | | - var toAdd = new List<IPackage>(); |
164 | | - foreach (IPackage package in task.Result) |
| 165 | + await Task.Delay(100).ConfigureAwait(false); |
| 166 | + } |
| 167 | + |
| 168 | + if (task.IsCompleted) |
| 169 | + { |
| 170 | + if (LoadOperationIdentifier == current_identifier && task.IsCompletedSuccessfully) |
165 | 171 | { |
166 | | - if (Contains(package) || !await IsPackageValid(package)) |
| 172 | + var toAdd = new List<IPackage>(); |
| 173 | + foreach (IPackage package in task.Result) |
167 | 174 | { |
168 | | - continue; |
| 175 | + if (Contains(package) || !await IsPackageValid(package)) |
| 176 | + { |
| 177 | + continue; |
| 178 | + } |
| 179 | + |
| 180 | + toAdd.Add(package); |
| 181 | + await AddPackage(package); |
169 | 182 | } |
170 | 183 |
|
171 | | - toAdd.Add(package); |
172 | | - await AddPackage(package); |
| 184 | + InvokePackagesChangedEvent(true, toAdd, []); |
173 | 185 | } |
174 | | - InvokePackagesChangedEvent(true, toAdd, []); |
| 186 | + |
| 187 | + tasks.Remove(task); |
175 | 188 | } |
176 | | - tasks.Remove(task); |
177 | 189 | } |
178 | 190 | } |
179 | | - } |
180 | 191 |
|
181 | | - if (LoadOperationIdentifier == current_identifier) |
| 192 | + if (LoadOperationIdentifier == current_identifier) |
| 193 | + { |
| 194 | + InvokeFinishedLoadingEvent(); |
| 195 | + IsLoaded = true; |
| 196 | + } |
| 197 | + |
| 198 | + IsLoading = false; |
| 199 | + } |
| 200 | + catch (Exception ex) |
182 | 201 | { |
183 | | - InvokeFinishedLoadingEvent(); |
184 | | - IsLoaded = true; |
| 202 | + Logger.Error(ex); |
| 203 | + IsLoading = false; |
| 204 | + throw; |
185 | 205 | } |
186 | | - IsLoading = false; |
187 | 206 | } |
188 | 207 |
|
189 | 208 | /// <summary> |
@@ -310,16 +329,7 @@ public IReadOnlyList<IPackage> GetEquivalentPackages(IPackage? package) |
310 | 329 | return []; |
311 | 330 | } |
312 | 331 |
|
313 | | - List<IPackage> result = []; |
314 | | - long hash_to_match = package.GetHash(); |
315 | | - foreach (IPackage local_package in Packages) |
316 | | - { |
317 | | - if (local_package.GetHash() == hash_to_match) |
318 | | - { |
319 | | - result.Add(local_package); |
320 | | - } |
321 | | - } |
322 | | - return result; |
| 332 | + return Packages.Where(p => p.IsEquivalentTo(package)).ToArray(); |
323 | 333 | } |
324 | 334 |
|
325 | 335 | public IPackage? GetPackageForId(string id, string? sourceName = null) |
|
0 commit comments