Skip to content

Commit c729888

Browse files
authored
Merge pull request #2753 from marticliment/better-async
Asynchronicity improvements
2 parents 71659b4 + 8861b3f commit c729888

File tree

69 files changed

+760
-735
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+760
-735
lines changed

scripts/apply_versions.py

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def fileReplaceLinesWith(filename: str, list: dict[str, str], encoding="utf-8"):
3939
"[assembly: AssemblyVersion(\"": f"{versionISS}\")]\n",
4040
"[assembly: AssemblyFileVersion(\"": f"{versionISS}\")]\n",
4141
"[assembly: AssemblyInformationalVersion(\"": f"{versionName}\")]\n",
42+
"[assembly: AssemblyInformationalVersionAttribute(\"": f"{versionName}\")]\n",
43+
"[assembly: AssemblyVersionAttribute(\"": f"{versionISS}\")]\n",
4244
# Your replacement dictionary here
4345
}, encoding="utf-8-sig")
4446

src/UniGetUI.Core.LanguageEngine/LanguageData.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static ReadOnlyDictionary<string, string> TranslationPercentages
5555

5656
private static ReadOnlyDictionary<string, string> LoadTranslationPercentages()
5757
{
58-
if (JsonObject.Parse(File.ReadAllText(Path.Join(CoreData.UniGetUIExecutableDirectory, "Assets", "Data", "TranslatedPercentages.json"))) is JsonObject val)
58+
if (JsonNode.Parse(File.ReadAllText(Path.Join(CoreData.UniGetUIExecutableDirectory, "Assets", "Data", "TranslatedPercentages.json"))) is JsonObject val)
5959
{
6060
return new(val.ToDictionary(x => x.Key, x => (x.Value ?? ("404%" + x.Key)).ToString()));
6161
}
@@ -65,7 +65,7 @@ private static ReadOnlyDictionary<string, string> LoadTranslationPercentages()
6565

6666
private static ReadOnlyDictionary<string, string> LoadLanguageReference()
6767
{
68-
if (JsonObject.Parse(File.ReadAllText(Path.Join(CoreData.UniGetUIExecutableDirectory, "Assets", "Data", "LanguagesReference.json"))) is JsonObject val)
68+
if (JsonNode.Parse(File.ReadAllText(Path.Join(CoreData.UniGetUIExecutableDirectory, "Assets", "Data", "LanguagesReference.json"))) is JsonObject val)
6969
{
7070
return new(val.ToDictionary(x => x.Key, x => (x.Value ?? ("NoNameLang_" + x.Key)).ToString()));
7171
}

src/UniGetUI.Core.Tools.Tests/ToolsTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ public void TranslateFunctionTester(string textEntry, bool TranslationExists)
3434
[Fact]
3535
public async Task TestWhichFunctionForExistingFile()
3636
{
37-
Tuple<bool, string> result = await CoreTools.Which("cmd.exe");
37+
Tuple<bool, string> result = await CoreTools.WhichAsync("cmd.exe");
3838
Assert.True(result.Item1);
3939
Assert.True(File.Exists(result.Item2));
4040
}
4141

4242
[Fact]
4343
public async Task TestWhichFunctionForNonExistingFile()
4444
{
45-
Tuple<bool, string> result = await CoreTools.Which("nonexistentfile.exe");
45+
Tuple<bool, string> result = await CoreTools.WhichAsync("nonexistentfile.exe");
4646
Assert.False(result.Item1);
4747
Assert.Equal("", result.Item2);
4848
}

src/UniGetUI.Core.Tools/Tools.cs

+20-13
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public static string Translate(string text, params object[] values)
4343
{
4444
dict.Add(index.ToString(), item);
4545
}
46+
4647
return Translate(text, dict);
4748
}
4849

@@ -75,7 +76,12 @@ public static void RelaunchProcess()
7576
/// </summary>
7677
/// <param name="command">The executable alias to find</param>
7778
/// <returns>A tuple containing: a boolean hat represents whether the path was found or not; the path to the file if found.</returns>
78-
public static async Task<Tuple<bool, string>> Which(string command)
79+
public static async Task<Tuple<bool, string>> WhichAsync(string command)
80+
{
81+
return await Task.Run(() => Which(command));
82+
}
83+
84+
public static Tuple<bool, string> Which(string command)
7985
{
8086
command = command.Replace(";", "").Replace("&", "").Trim();
8187
Logger.Debug($"Begin \"which\" search for command {command}");
@@ -95,18 +101,14 @@ public static async Task<Tuple<bool, string>> Which(string command)
95101
};
96102
process.StartInfo = UpdateEnvironmentVariables(process.StartInfo);
97103
process.Start();
98-
string? line = await process.StandardOutput.ReadLineAsync();
104+
string? line = process.StandardOutput.ReadLine();
99105
string output;
100-
if (line == null)
101-
{
102-
output = "";
103-
}
104-
else
105-
{
106-
output = line.Trim();
107-
}
108106

109-
await process.WaitForExitAsync();
107+
if (line is null) output = "";
108+
else output = line.Trim();
109+
110+
process.WaitForExit();
111+
110112
if (process.ExitCode != 0 || output == "")
111113
{
112114
Logger.ImportantInfo($"Command {command} was not found on the system");
@@ -243,6 +245,11 @@ public static async Task<double> GetFileSizeAsync(Uri? url)
243245
return await GetFileSizeAsyncAsLong(url) / 1048576d;
244246
}
245247

248+
public static double GetFileSize(Uri? url)
249+
{
250+
return GetFileSizeAsyncAsLong(url).GetAwaiter().GetResult() / 1048576d;
251+
}
252+
246253
public static async Task<long> GetFileSizeAsyncAsLong(Uri? url)
247254
{
248255
if (url == null)
@@ -434,7 +441,7 @@ public static long HashStringAsLong(string inputString)
434441
/// </summary>
435442
/// <param name="linkPath">The location of the link to be created</param>
436443
/// <param name="targetPath">The location of the real folder where to point</param>
437-
public static async Task CreateSymbolicLinkDir(string linkPath, string targetPath)
444+
public static void CreateSymbolicLinkDir(string linkPath, string targetPath)
438445
{
439446
var startInfo = new ProcessStartInfo
440447
{
@@ -449,7 +456,7 @@ public static async Task CreateSymbolicLinkDir(string linkPath, string targetPat
449456
Process? p = Process.Start(startInfo);
450457
if (p is not null)
451458
{
452-
await p.WaitForExitAsync();
459+
p.WaitForExit();
453460
}
454461

455462
if (p is null || p.ExitCode != 0)

src/UniGetUI.Interface.BackgroundApi/BackgroundApi.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public void BuildV1WidgetsApi()
303303
IPackage? package = PEInterface.UpgradablePackagesLoader.GetPackageForId(Request.Query.@packageId, Request.Query.@packageSource);
304304
if (package != null)
305305
{
306-
Uri iconUrl = await package.GetIconUrl();
306+
Uri iconUrl = await Task.Run(package.GetIconUrl);
307307
if (iconUrl.ToString() != "ms-appx:///Assets/Images/package_color.png")
308308
{
309309
iconPath = Path.Join(CoreData.UniGetUICacheDirectory_Icons, package.Manager.Name, $"{package.Id}.{iconUrl.ToString().Split('.')[^1]}");
@@ -321,7 +321,15 @@ public void BuildV1WidgetsApi()
321321
ContentType = $"image/{iconPath.Split('.')[^1]}",
322322
Contents = (stream) =>
323323
{
324-
stream.Write(fileContents, 0, fileContents.Length);
324+
try
325+
{
326+
stream.Write(fileContents, 0, fileContents.Length);
327+
}
328+
catch (Exception ex)
329+
{
330+
Logger.Warn($"Unable to load icon to path {iconPath}");
331+
Logger.Warn(ex);
332+
}
325333
}
326334
};
327335

src/UniGetUI.PAckageEngine.Interfaces/IPackage.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ public interface IPackage : INotifyPropertyChanged, IEquatable<IPackage>
7373
/// After calling this method, the returned URL points to a location on the local machine
7474
/// </summary>
7575
/// <returns>An always-valid URI object, pointing to a file:// or to a ms-appx:// URL</returns>
76-
public Task<Uri> GetIconUrl();
76+
public Uri GetIconUrl();
7777

7878
/// <summary>
7979
/// Retrieves a list og URIs representing the available screenshots for this package.
8080
/// </summary>
81-
public Task<Uri[]> GetPackageScreenshots();
81+
public IEnumerable<Uri> GetScreenshots();
8282

8383

8484
/// <summary>
@@ -136,7 +136,7 @@ public interface IPackage : INotifyPropertyChanged, IEquatable<IPackage>
136136
/// </summary>
137137
public bool NewerVersionIsInstalled();
138138

139-
public Task<SerializablePackage_v1> AsSerializable();
139+
public SerializablePackage_v1 AsSerializable();
140140

141141
public SerializableIncompatiblePackage_v1 AsSerializable_Incompatible();
142142
}

src/UniGetUI.PAckageEngine.Interfaces/IPackageManager.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public interface IPackageManager : ISourceProvider, IPackageDetailsProvider, IOp
2323
/// <summary>
2424
/// Initializes the Package Manager (asynchronously). Must be run before using any other method of the manager.
2525
/// </summary>
26-
public Task InitializeAsync();
26+
public void Initialize();
2727

2828
/// <summary>
2929
/// Returns true if the manager is enabled, false otherwise
@@ -39,25 +39,25 @@ public interface IPackageManager : ISourceProvider, IPackageDetailsProvider, IOp
3939
/// Returns an array of Package objects that the manager lists for the given query. Depending on the manager, the list may
4040
/// also include similar results. This method is fail-safe and will return an empty array if an error occurs.
4141
/// </summary>
42-
public Task<IPackage[]> FindPackages(string query);
42+
public IEnumerable<IPackage> FindPackages(string query);
4343

4444
/// <summary>
4545
/// Returns an array of UpgradablePackage objects that represent the available updates reported by the manager.
4646
/// This method is fail-safe and will return an empty array if an error occurs.
4747
/// </summary>
48-
public Task<IPackage[]> GetAvailableUpdates();
48+
public IEnumerable<IPackage> GetAvailableUpdates();
4949

5050
/// <summary>
5151
/// Returns an array of Package objects that represent the installed reported by the manager.
5252
/// This method is fail-safe and will return an empty array if an error occurs.
5353
/// </summary>
54-
public Task<IPackage[]> GetInstalledPackages();
54+
public IEnumerable<IPackage> GetInstalledPackages();
5555

5656
/// <summary>
5757
/// Refreshes the Package Manager sources/indexes
5858
/// Each manager MUST implement this method.
5959
/// </summary>
60-
public Task RefreshPackageIndexes();
60+
public void RefreshPackageIndexes();
6161

6262
public IManagerSource GetSourceOrDefault(string SourceName);
6363
public IManagerSource? GetSourceIfExists(string SourceName);

src/UniGetUI.PAckageEngine.Interfaces/ManagerProviders/IOperationProvider.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public interface IOperationProvider
88
/// that the requested operation is performed over the given package, with its corresponding
99
/// installation options.
1010
/// </summary>
11-
public abstract IEnumerable<string> GetOperationParameters(
11+
public IEnumerable<string> GetOperationParameters(
1212
IPackage package,
1313
IInstallationOptions options,
1414
OperationType operation
@@ -18,7 +18,7 @@ OperationType operation
1818
/// Returns the veredict of the given package operation, given the package, the operation type,
1919
/// the corresponding output and the return code.
2020
/// </summary>
21-
public abstract OperationVeredict GetOperationResult(
21+
public OperationVeredict GetOperationResult(
2222
IPackage package,
2323
OperationType operation,
2424
IEnumerable<string> processOutput,

src/UniGetUI.PAckageEngine.Interfaces/ManagerProviders/IPackageDetailsProvider.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface IPackageDetailsProvider
1111
/// </summary>
1212
/// <param name="details">The PackageDetails instance to load</param>
1313
/// <returns>A PackageDetails object</returns>
14-
public abstract Task GetPackageDetails(IPackageDetails details);
14+
public void GetPackageDetails(IPackageDetails details);
1515

1616
/// <summary>
1717
/// Returns the available versions to install for the given package.
@@ -20,28 +20,28 @@ public interface IPackageDetailsProvider
2020
/// </summary>
2121
/// <param name="package">The package from which to load its versions</param>
2222
/// <returns>An array of stings containing the found versions, an empty array if none.</returns>
23-
public abstract Task<string[]> GetPackageVersions(IPackage package);
23+
public IEnumerable<string> GetPackageVersions(IPackage package);
2424

2525
/// <summary>
2626
/// Returns an Uri pointing to the icon of this package.
2727
/// The uri may be either a ms-appx:/// url or a http(s):// protocol url
2828
/// </summary>
2929
/// <param name="package">The package from which to load the icon</param>
3030
/// <returns>A full path to a valid icon file</returns>
31-
public abstract Task<CacheableIcon?> GetPackageIconUrl(IPackage package);
31+
public CacheableIcon? GetPackageIconUrl(IPackage package);
3232

3333
/// <summary>
3434
/// Returns the URLs to the screenshots (if any) of this package.
3535
/// </summary>
3636
/// <param name="package">The package from which to load the screenshots</param>
3737
/// <returns>An array with valid URIs to the screenshots</returns>
38-
public abstract Task<Uri[]> GetPackageScreenshotsUrl(IPackage package);
38+
public IEnumerable<Uri> GetPackageScreenshotsUrl(IPackage package);
3939

4040
/// <summary>
4141
/// Returns the location where the package is installed, or null if the location cannot be loaded.
4242
/// </summary>
4343
/// <param name="package">The package for which to get the location</param>
4444
/// <returns>A valid path in the form of a string or a null object</returns>
45-
public abstract string? GetPackageInstallLocation(IPackage package);
45+
public string? GetPackageInstallLocation(IPackage package);
4646
}
4747
}

src/UniGetUI.PAckageEngine.Interfaces/ManagerProviders/ISourceProvider.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ public interface ISourceProvider
1111
/// </summary>
1212
/// <param name="source">The source to add</param>
1313
/// <returns>An array containing the parameters to pass to the manager executable</returns>
14-
public abstract string[] GetAddSourceParameters(IManagerSource source);
14+
public string[] GetAddSourceParameters(IManagerSource source);
1515

1616
/// <summary>
1717
/// Returns the command-line parameters required to remove the given source from the manager.
1818
/// </summary>
1919
/// <param name="source">The source to remove</param>
2020
/// <returns>An array containing the parameters to pass to the manager executable</returns>
21-
public abstract string[] GetRemoveSourceParameters(IManagerSource source);
21+
public string[] GetRemoveSourceParameters(IManagerSource source);
2222

2323
/// <summary>
2424
/// Checks the result of attempting to add a source
@@ -27,7 +27,7 @@ public interface ISourceProvider
2727
/// <param name="ReturnCode">The return code of the operation</param>
2828
/// <param name="Output">the command-line output of the operation</param>
2929
/// <returns>An OperationVeredict value</returns>
30-
public abstract OperationVeredict GetAddSourceOperationVeredict(IManagerSource source, int ReturnCode, string[] Output);
30+
public OperationVeredict GetAddSourceOperationVeredict(IManagerSource source, int ReturnCode, string[] Output);
3131

3232
/// <summary>
3333
/// Checks the result of attempting to remove a source
@@ -36,12 +36,12 @@ public interface ISourceProvider
3636
/// <param name="ReturnCode">The return code of the operation</param>
3737
/// <param name="Output">the command-line output of the operation</param>
3838
/// <returns>An OperationVeredict value</returns>
39-
public abstract OperationVeredict GetRemoveSourceOperationVeredict(IManagerSource source, int ReturnCode, string[] Output);
39+
public OperationVeredict GetRemoveSourceOperationVeredict(IManagerSource source, int ReturnCode, string[] Output);
4040

4141
/// <summary>
4242
/// Returns the available sources
4343
/// </summary>
4444
/// <returns>An array of ManagerSource objects</returns>
45-
public abstract Task<IManagerSource[]> GetSources();
45+
public IEnumerable<IManagerSource> GetSources();
4646
}
4747
}

0 commit comments

Comments
 (0)