Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code style + small performance improvements #2517

Merged
merged 5 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 21 additions & 25 deletions src/UniGetUI.Core.Data/CoreData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static class CoreData
{
private static int? __code_page;
public static int CODE_PAGE { get => __code_page ??= GetCodePage(); }

private static int GetCodePage()
{
try
Expand All @@ -26,7 +26,7 @@ private static int GetCodePage()
p.Start();
string contents = p.StandardOutput.ReadToEnd();
return int.Parse(contents.Split(':')[^1].Trim());
}
}
catch (Exception e)
{
Logger.Error(e);
Expand Down Expand Up @@ -184,10 +184,8 @@ public static string UniGetUIExecutableDirectory
{
return dir;
}
else
{
Logger.Error("System.Reflection.Assembly.GetExecutingAssembly().Location returned an empty path");
}

Logger.Error("System.Reflection.Assembly.GetExecutingAssembly().Location returned an empty path");

return Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "UiGetUI");
}
Expand All @@ -205,10 +203,8 @@ public static string UniGetUIExecutableFile
{
return filename.Replace(".dll", ".exe");
}
else
{
Logger.Error("System.Reflection.Assembly.GetExecutingAssembly().Location returned an empty path");
}

Logger.Error("System.Reflection.Assembly.GetExecutingAssembly().Location returned an empty path");

return Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "UniGetUI", "UniGetUI.exe");
}
Expand All @@ -232,7 +228,8 @@ private static string GetNewDataDirectoryOrMoveOld(string old_path, string new_p
{
return new_path;
}
else if (Directory.Exists(new_path) && Directory.Exists(old_path))

if (Directory.Exists(new_path) && Directory.Exists(old_path))
{
try
{
Expand Down Expand Up @@ -287,7 +284,8 @@ private static string GetNewDataDirectoryOrMoveOld(string old_path, string new_p
return new_path;
}
}
else if (/*Directory.Exists(new_path)*/Directory.Exists(old_path))
marticliment marked this conversation as resolved.
Show resolved Hide resolved

if (/*Directory.Exists(new_path)*/Directory.Exists(old_path))
{
try
{
Expand All @@ -302,20 +300,18 @@ private static string GetNewDataDirectoryOrMoveOld(string old_path, string new_p
return old_path;
}
}
else

try
{
try
{
Logger.Debug("Creating non-existing data directory at: " + new_path);
Directory.CreateDirectory(new_path);
return new_path;
}
catch (Exception e)
{
Logger.Error("Could not create new directory. You may perhaps need to disable Controlled Folder Access from Windows Settings or make an exception for UniGetUI.");
Logger.Error(e);
return new_path;
}
Logger.Debug("Creating non-existing data directory at: " + new_path);
Directory.CreateDirectory(new_path);
return new_path;
}
catch (Exception e)
{
Logger.Error("Could not create new directory. You may perhaps need to disable Controlled Folder Access from Windows Settings or make an exception for UniGetUI.");
Logger.Error(e);
return new_path;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/UniGetUI.Core.IconStore/IconCacheEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public enum CachedIconVerificationMethod
public readonly struct CacheableIcon
{
public readonly Uri Url;
public readonly byte[] Sha256 = { };
public readonly byte[] Sha256 = [];
public readonly string Version = "";
public readonly long Size = -1;
public readonly CachedIconVerificationMethod VerificationMethod;
Expand Down
12 changes: 4 additions & 8 deletions src/UniGetUI.Core.LanguageEngine/LanguageData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ private static ReadOnlyDictionary<string, string> LoadTranslationPercentages()
{
return new(val.ToDictionary(x => x.Key, x => (x.Value ?? ("404%" + x.Key)).ToString()));
}
else
{
return new(new Dictionary<string, string>());
}

return new(new Dictionary<string, string>());
}

private static ReadOnlyDictionary<string, string> LoadLanguageReference()
Expand All @@ -74,10 +72,8 @@ private static ReadOnlyDictionary<string, string> LoadLanguageReference()
{
return new(val.ToDictionary(x => x.Key, x => (x.Value ?? ("NoNameLang_" + x.Key)).ToString()));
}
else
{
return new(new Dictionary<string, string>());
}

return new(new Dictionary<string, string>());
}

private static Person[] LoadLanguageTranslatorList()
Expand Down
12 changes: 6 additions & 6 deletions src/UniGetUI.Core.LanguageEngine/LanguageEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ public string Translate(string key)

return "UniGetUI (formerly WingetUI)";
}
else if (key == "Formerly known as WingetUI")

if (key == "Formerly known as WingetUI")
{
if (MainLangDict.ContainsKey(key))
{
Expand All @@ -166,14 +167,13 @@ public string Translate(string key)
{
return "";
}
else if (MainLangDict.ContainsKey(key) && MainLangDict[key] != "")
marticliment marked this conversation as resolved.
Show resolved Hide resolved

if (MainLangDict.ContainsKey(key) && MainLangDict[key] != "")
{
return MainLangDict[key].Replace("WingetUI", "UniGetUI");
}
else
{
return key.Replace("WingetUI", "UniGetUI");
}

return key.Replace("WingetUI", "UniGetUI");
}

public string Translate(string key, Dictionary<string, object?> dict)
Expand Down
8 changes: 3 additions & 5 deletions src/UniGetUI.Core.Tools/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,9 @@
Logger.ImportantInfo($"Command {command} was not found on the system");
return new Tuple<bool, string>(false, "");
}
else
{
Logger.Debug($"Command {command} was found on {output}");
return new Tuple<bool, string>(File.Exists(output), output);
}

Logger.Debug($"Command {command} was found on {output}");
return new Tuple<bool, string>(File.Exists(output), output);
}

/// <summary>
Expand Down Expand Up @@ -226,8 +224,8 @@
{
try
{
return new WindowsPrincipal(WindowsIdentity.GetCurrent())

Check warning on line 227 in src/UniGetUI.Core.Tools/Tools.cs

View workflow job for this annotation

GitHub Actions / test

This call site is reachable on all platforms. 'WindowsPrincipal.IsInRole(WindowsBuiltInRole)' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 227 in src/UniGetUI.Core.Tools/Tools.cs

View workflow job for this annotation

GitHub Actions / test

This call site is reachable on all platforms. 'WindowsPrincipal' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 227 in src/UniGetUI.Core.Tools/Tools.cs

View workflow job for this annotation

GitHub Actions / test

This call site is reachable on all platforms. 'WindowsIdentity.GetCurrent()' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
.IsInRole(WindowsBuiltInRole.Administrator);

Check warning on line 228 in src/UniGetUI.Core.Tools/Tools.cs

View workflow job for this annotation

GitHub Actions / test

This call site is reachable on all platforms. 'WindowsBuiltInRole.Administrator' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
}
catch (Exception e)
{
Expand Down
16 changes: 11 additions & 5 deletions src/UniGetUI.PackageEngine.Managers.Chocolatey/Chocolatey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Chocolatey() : base()
CanRunInteractively = true,
SupportsCustomVersions = true,
SupportsCustomArchitectures = true,
SupportedCustomArchitectures = new Architecture[] { Architecture.X86 },
SupportedCustomArchitectures = [Architecture.X86],
SupportsPreRelease = true,
SupportsCustomSources = true,
SupportsCustomPackageIcons = true,
Expand Down Expand Up @@ -174,15 +174,18 @@ public override OperationVeredict GetInstallOperationVeredict(Package package, I
{
return OperationVeredict.Succeeded;
}
else if (ReturnCode == 3010)

if (ReturnCode == 3010)
{
return OperationVeredict.Succeeded; // TODO: Restart required
}
else if ((output_string.Contains("Run as administrator") || output_string.Contains("The requested operation requires elevation") || output_string.Contains("ERROR: Exception calling \"CreateDirectory\" with \"1\" argument(s): \"Access to the path")) && !options.RunAsAdministrator)

if ((output_string.Contains("Run as administrator") || output_string.Contains("The requested operation requires elevation") || output_string.Contains("ERROR: Exception calling \"CreateDirectory\" with \"1\" argument(s): \"Access to the path")) && !options.RunAsAdministrator)
{
options.RunAsAdministrator = true;
return OperationVeredict.AutoRetry;
}

return OperationVeredict.Failed;
}

Expand All @@ -199,15 +202,18 @@ public override OperationVeredict GetUninstallOperationVeredict(Package package,
{
return OperationVeredict.Succeeded;
}
else if (ReturnCode == 3010)

if (ReturnCode == 3010)
{
return OperationVeredict.Succeeded; // TODO: Restart required
}
else if ((output_string.Contains("Run as administrator") || output_string.Contains("The requested operation requires elevation")) && !options.RunAsAdministrator)
marticliment marked this conversation as resolved.
Show resolved Hide resolved

if ((output_string.Contains("Run as administrator") || output_string.Contains("The requested operation requires elevation")) && !options.RunAsAdministrator)
{
options.RunAsAdministrator = true;
return OperationVeredict.AutoRetry;
}

return OperationVeredict.Failed;
}
public override string[] GetInstallParameters(Package package, InstallationOptions options)
Expand Down
2 changes: 1 addition & 1 deletion src/UniGetUI.PackageEngine.Managers.Dotnet/DotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public DotNet() : base()
CanRunAsAdmin = true,
SupportsCustomScopes = true,
SupportsCustomArchitectures = true,
SupportedCustomArchitectures = new Architecture[] { Architecture.X86, Architecture.X64, Architecture.Arm64, Architecture.Arm },
SupportedCustomArchitectures = [Architecture.X86, Architecture.X64, Architecture.Arm64, Architecture.Arm],
SupportsPreRelease = true,
SupportsCustomLocations = true,
SupportsCustomPackageIcons = true,
Expand Down
8 changes: 3 additions & 5 deletions src/UniGetUI.PackageEngine.Managers.Pip/Pip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,13 @@ public override OperationVeredict GetInstallOperationVeredict(Package package, I
{
return OperationVeredict.Succeeded;
}
else if (output_string.Contains("--user") && package.Scope == PackageScope.Global)
marticliment marked this conversation as resolved.
Show resolved Hide resolved

if (output_string.Contains("--user") && package.Scope == PackageScope.Global)
{
package.Scope = PackageScope.User;
return OperationVeredict.AutoRetry;
}
else
{
return OperationVeredict.Failed;
}
return OperationVeredict.Failed;
}

public override OperationVeredict GetUpdateOperationVeredict(Package package, InstallationOptions options, int ReturnCode, string[] Output)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal static partial class BundledWinGetLegacyMethods
{
public static async Task<Package[]> FindPackages_UnSafe(WinGet Manager, string query)
{
List<Package> Packages = new();
List<Package> Packages = [];
Process p = new()
{
StartInfo = new()
Expand All @@ -26,9 +26,9 @@ public static async Task<Package[]> FindPackages_UnSafe(WinGet Manager, string q
StandardOutputEncoding = System.Text.Encoding.UTF8
}
};

ProcessTaskLogger logger = Manager.TaskLogger.CreateNew(LoggableTaskType.FindPackages, p);

p.Start();

string OldLine = "";
Expand Down Expand Up @@ -79,7 +79,7 @@ public static async Task<Package[]> FindPackages_UnSafe(WinGet Manager, string q

public static async Task<Package[]> GetAvailableUpdates_UnSafe(WinGet Manager)
{
List<Package> Packages = new();
List<Package> Packages = [];
Process p = new()
{
StartInfo = new()
Expand All @@ -96,7 +96,7 @@ public static async Task<Package[]> GetAvailableUpdates_UnSafe(WinGet Manager)
};

ProcessTaskLogger logger = Manager.TaskLogger.CreateNew(LoggableTaskType.ListUpdates, p);

p.Start();

string OldLine = "";
Expand All @@ -112,7 +112,7 @@ public static async Task<Package[]> GetAvailableUpdates_UnSafe(WinGet Manager)

if (line.Contains("have pins"))
continue;

if (!DashesPassed && line.Contains("---"))
{
string HeaderPrefix = OldLine.Contains("SearchId") ? "Search" : "";
Expand Down Expand Up @@ -164,7 +164,7 @@ public static async Task<Package[]> GetAvailableUpdates_UnSafe(WinGet Manager)

public static async Task<Package[]> GetInstalledPackages_UnSafe(WinGet Manager)
{
List<Package> Packages = new();
List<Package> Packages = [];
Process p = new()
{
StartInfo = new()
Expand All @@ -178,9 +178,9 @@ public static async Task<Package[]> GetInstalledPackages_UnSafe(WinGet Manager)
StandardOutputEncoding = System.Text.Encoding.UTF8
}
};

ProcessTaskLogger logger = Manager.TaskLogger.CreateNew(LoggableTaskType.ListInstalledPackages, p);

p.Start();

string OldLine = "";
Expand Down Expand Up @@ -235,7 +235,7 @@ public static async Task<Package[]> GetInstalledPackages_UnSafe(WinGet Manager)
Logger.Error(e);
}
}

logger.AddToStdErr(await p.StandardError.ReadToEndAsync());
await p.WaitForExitAsync();
logger.Close(p.ExitCode);
Expand All @@ -259,11 +259,11 @@ private static ManagerSource GetLocalSource(WinGet Manager, string id)
return Manager.AndroidSubsystemSource;

// Check if source is Steama
if ((id == "Steam" || id.Contains("Steam App ")) && id.Split("Steam App").Count() >= 2 && id.Split("Steam App")[1].Trim().Count(x => !"1234567890".Contains(x)) == 0)
if ((id == "Steam" || id.Contains("Steam App ")) && id.Split("Steam App").Length >= 2 && id.Split("Steam App")[1].Trim().Count(x => !"1234567890".Contains(x)) == 0)
return Manager.SteamSource;

// Check if source is Ubisoft Connect
if (id == "Uplay" || id.Contains("Uplay Install ") && id.Split("Uplay Install").Count() >= 2 && id.Split("Uplay Install")[1].Trim().Count(x => !"1234567890".Contains(x)) == 0)
if (id == "Uplay" || id.Contains("Uplay Install ") && id.Split("Uplay Install").Length >= 2 && id.Split("Uplay Install")[1].Trim().Count(x => !"1234567890".Contains(x)) == 0)
return Manager.UbisoftConnectSource;

// Check if source is GOG
Expand Down
9 changes: 5 additions & 4 deletions src/UniGetUI.PackageEngine.Managers.WinGet/WinGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public WinGet() : base()
CanRunInteractively = true,
SupportsCustomVersions = true,
SupportsCustomArchitectures = true,
SupportedCustomArchitectures = new Architecture[] { Architecture.X86, Architecture.X64, Architecture.Arm64 },
SupportedCustomArchitectures = [Architecture.X86, Architecture.X64, Architecture.Arm64],
SupportsCustomScopes = true,
SupportsCustomLocations = true,
SupportsCustomSources = true,
Expand Down Expand Up @@ -153,13 +153,13 @@ public ManagerSource GetLocalSource(string id)
}

// Check if source is Steam
if ((id == "Steam" || id.Contains("Steam App ")) && id.Split("Steam App").Count() >= 2 && id.Split("Steam App")[1].Trim().Count(x => !"1234567890".Contains(x)) == 0)
if ((id == "Steam" || id.Contains("Steam App ")) && id.Split("Steam App").Length >= 2 && id.Split("Steam App")[1].Trim().Count(x => !"1234567890".Contains(x)) == 0)
{
return SteamSource;
}

// Check if source is Ubisoft Connect
if (id == "Uplay" || (id.Contains("Uplay Install ") && id.Split("Uplay Install").Count() >= 2 && id.Split("Uplay Install")[1].Trim().Count(x => !"1234567890".Contains(x)) == 0))
if (id == "Uplay" || (id.Contains("Uplay Install ") && id.Split("Uplay Install").Length >= 2 && id.Split("Uplay Install")[1].Trim().Count(x => !"1234567890".Contains(x)) == 0))
{
return UbisoftConnectSource;
}
Expand Down Expand Up @@ -288,7 +288,8 @@ public override OperationVeredict GetInstallOperationVeredict(Package package, I
{
return OperationVeredict.Succeeded; // TODO: Needs restart
}
else if (ReturnCode == -1978335215)
marticliment marked this conversation as resolved.
Show resolved Hide resolved

if (ReturnCode == -1978335215)
{
return OperationVeredict.Failed; // TODO: Needs skip checksum
}
Expand Down
Loading