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

Fix warnings part 1 #3251

Merged
merged 9 commits into from
Feb 5, 2025
4 changes: 2 additions & 2 deletions src/UniGetUI.Core.Classes.Tests/ObservableQueueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public void TestObservableQueue()
{
var queue = new ObservableQueue<int>();

List<int> enqueuedElements = new();
List<int> dequeuedElements = new();
List<int> enqueuedElements = [];
List<int> dequeuedElements = [];

queue.ItemEnqueued += (_, e) => enqueuedElements.Add(e.Item);
queue.ItemDequeued += (_, e) => dequeuedElements.Add(e.Item);
Expand Down
58 changes: 29 additions & 29 deletions src/UniGetUI.Core.Classes.Tests/TaskRecyclerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,42 @@ private int MySlowMethod4(int argument)
}

[Fact]
public void TestTaskRecycler_Static_Int()
public async Task TestTaskRecycler_Static_Int()
{
// The same static method should be cached, and therefore the return value should be the same
var task1 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1);
var task2 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1);
int result1 = task1.GetAwaiter().GetResult();
int result2 = task2.GetAwaiter().GetResult();
int result1 = await task1;
int result2 = await task2;
Assert.Equal(result1, result2);

// The same static method should be cached, and therefore the return value should be the same, but different from previous runs
var task3 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1);
var task4 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1);
int result4 = task4.GetAwaiter().GetResult();
int result3 = task3.GetAwaiter().GetResult();
int result4 = await task4;
int result3 = await task3;
Assert.Equal(result3, result4);

// Ensure the last call was not permanently cached
Assert.NotEqual(result1, result3);
}

[Fact]
public void TestTaskRecycler_Static_Int_WithCache()
public async Task TestTaskRecycler_Static_Int_WithCache()
{
// The same static method should be cached, and therefore the return value should be the same
var task1 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1, 2);
var task2 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1, 2);
int result1 = task1.GetAwaiter().GetResult();
int result2 = task2.GetAwaiter().GetResult();
int result1 = await task1;
int result2 = await task2;
Assert.Equal(result1, result2);

// The same static method should be cached, and therefore the return value should be the same,
// and equal to previous runs due to 3 seconds cache
var task3 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1, 2);
var task4 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1, 2);
int result4 = task4.GetAwaiter().GetResult();
int result3 = task3.GetAwaiter().GetResult();
int result4 = await task4;
int result3 = await task3;
Assert.Equal(result3, result4);
Assert.Equal(result1, result3);

Expand All @@ -77,8 +77,8 @@ public void TestTaskRecycler_Static_Int_WithCache()
// The same static method should be cached, but cached runs should have been removed. This results should differ from previous ones
var task5 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1, 2);
var task6 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1, 2);
int result5 = task6.GetAwaiter().GetResult();
int result6 = task5.GetAwaiter().GetResult();
int result5 = await task6;
int result6 = await task5;
Assert.Equal(result5, result6);
Assert.NotEqual(result4, result5);

Expand All @@ -88,37 +88,37 @@ public void TestTaskRecycler_Static_Int_WithCache()
// The same static method should be cached, but cached runs should have been cleared manually. This results should differ from previous ones
var task7 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1, 2);
var task8 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod1, 2);
int result7 = task7.GetAwaiter().GetResult();
int result8 = task8.GetAwaiter().GetResult();
int result7 = await task7;
int result8 = await task8;
Assert.Equal(result7, result8);
Assert.NotEqual(result6, result7);
}

[Fact]
public void TestTaskRecycler_StaticWithArgument_Int()
public async Task TestTaskRecycler_StaticWithArgument_Int()
{
// The same static method should be cached, and therefore the return value should be the same
var task1 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod4, 2);
var task2 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod4, 2);
var task3 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod4, 3);
int result1 = task1.GetAwaiter().GetResult();
int result2 = task2.GetAwaiter().GetResult();
int result3 = task3.GetAwaiter().GetResult();
int result1 = await task1;
int result2 = await task2;
int result3 = await task3;
Assert.Equal(result1, result2);
Assert.NotEqual(result1, result3);

// The same static method should be cached, and therefore the return value should be the same, but different from previous runs
var task4 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod4, 2);
var task5 = TaskRecycler<int>.RunOrAttachAsync(MySlowMethod4, 3);
int result4 = task4.GetAwaiter().GetResult();
int result5 = task5.GetAwaiter().GetResult();
int result4 = await task4;
int result5 = await task5;
Assert.NotEqual(result4, result5);
Assert.NotEqual(result1, result4);
Assert.NotEqual(result3, result5);
}

[Fact]
public void TestTaskRecycler_Class_String()
public async Task TestTaskRecycler_Class_String()
{
var class1 = new TestClass();
var class2 = new TestClass();
Expand All @@ -127,8 +127,8 @@ public void TestTaskRecycler_Class_String()
// and therefore the return value should be the same
var task1 = TaskRecycler<string>.RunOrAttachAsync(class1.SlowMethod2);
var task2 = TaskRecycler<string>.RunOrAttachAsync(class1.SlowMethod2);
string result1 = task1.GetAwaiter().GetResult();
string result2 = task2.GetAwaiter().GetResult();
string result1 = await task1;
string result2 = await task2;
Assert.Equal(result1, result2);

var class1_copy = class1;
Expand All @@ -137,16 +137,16 @@ public void TestTaskRecycler_Class_String()
// from different variable names should be cached, and therefore the return value should be the same
var task5 = TaskRecycler<string>.RunOrAttachAsync(class1_copy.SlowMethod2);
var task6 = TaskRecycler<string>.RunOrAttachAsync(class1.SlowMethod2);
string result5 = task5.GetAwaiter().GetResult();
string result6 = task6.GetAwaiter().GetResult();
string result5 = await task5;
string result6 = await task6;
Assert.Equal(result5, result6);

// The SAME method from two DIFFERENT instances should NOT be
// cached, so the results should differ
var task3 = TaskRecycler<string>.RunOrAttachAsync(class1.SlowMethod2);
var task4 = TaskRecycler<string>.RunOrAttachAsync(class2.SlowMethod2);
string result4 = task4.GetAwaiter().GetResult();
string result3 = task3.GetAwaiter().GetResult();
string result4 = await task4;
string result3 = await task3;
Assert.NotEqual(result3, result4);

// Ensure the last call was not permanently cached
Expand All @@ -156,8 +156,8 @@ public void TestTaskRecycler_Class_String()
// cached, so the results should differ
var task7 = TaskRecycler<string>.RunOrAttachAsync(class1.SlowMethod3);
var task8 = TaskRecycler<string>.RunOrAttachAsync(class2.SlowMethod2);
string result7 = task7.GetAwaiter().GetResult();
string result8 = task8.GetAwaiter().GetResult();
string result7 = await task7;
string result8 = await task8;
Assert.NotEqual(result7, result8);
}
}
10 changes: 4 additions & 6 deletions src/UniGetUI.Core.Data/CoreData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ public static string UniGetUIDataDirectory
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
return path;
}
else
{
string old_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".wingetui");
string new_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "UniGetUI");
return GetNewDataDirectoryOrMoveOld(old_path, new_path);
}

string old_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".wingetui");
string new_path = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "UniGetUI");
return GetNewDataDirectoryOrMoveOld(old_path, new_path);
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/UniGetUI.Core.IconStore/IconCacheEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ private static void DownsizeImage(string cachedIconFile, string extension)
if (width > MAX_SIDE || height > MAX_SIDE)
{
File.Move(cachedIconFile, $"{cachedIconFile}.copy");
var image = MagicImageProcessor.BuildPipeline($"{cachedIconFile}.copy", new ProcessImageSettings()
var image = MagicImageProcessor.BuildPipeline($"{cachedIconFile}.copy", new ProcessImageSettings
{
Width = MAX_SIDE,
Height = MAX_SIDE,
Expand Down Expand Up @@ -350,15 +350,17 @@ private static void DeteteCachedFiles(string iconLocation)
try
{
foreach (string file in Directory.GetFiles(iconLocation))
{
File.Delete(file);
}
}
catch (Exception e)
{
Logger.Warn($"An error occurred while deleting old icon cache: {e.Message}");
}
}

public static readonly ReadOnlyDictionary<string, string> MimeToExtension = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()
public static readonly ReadOnlyDictionary<string, string> MimeToExtension = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>
{
{"image/avif", "avif"},
{"image/gif", "gif"},
Expand All @@ -373,7 +375,7 @@ private static void DeteteCachedFiles(string iconLocation)
{"image/tiff", "tif"},
});

public static readonly ReadOnlyDictionary<string, string> ExtensionToMime = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()
public static readonly ReadOnlyDictionary<string, string> ExtensionToMime = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>
{
{"avif", "image/avif"},
{"gif", "image/gif"},
Expand Down
4 changes: 3 additions & 1 deletion src/UniGetUI.Core.LanguageEngine/LanguageEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Dictionary<string, string> LoadLanguageFile(string LangKey)
{
Logger.Warn("User has updated translations disabled");
}
else if(!File.Exists(CachedLangFileToLoad))
else if (!File.Exists(CachedLangFileToLoad))
{
Logger.Warn($"Tried to access a non-existing cached language file! file={CachedLangFileToLoad}");
}
Expand All @@ -99,7 +99,9 @@ public Dictionary<string, string> LoadLanguageFile(string LangKey)
{
if (JsonNode.Parse(File.ReadAllText(CachedLangFileToLoad)) is JsonObject parsedObject)
foreach (var keyval in parsedObject.ToDictionary(x => x.Key, x => x.Value))
{
LangDict[keyval.Key] = keyval.Value?.ToString() ?? "";
}
else
throw new ArgumentException($"parsedObject was null for lang file {CachedLangFileToLoad}");
}
Expand Down
42 changes: 20 additions & 22 deletions src/UniGetUI.Core.Settings/SettingsEngine_Dictionaries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ namespace UniGetUI.Core.SettingsEngine
{
public static partial class Settings
{
private static readonly ConcurrentDictionary<string, Dictionary<object, object?>> dictionarySettings = new();
private static readonly ConcurrentDictionary<string, Dictionary<object, object?>> _dictionarySettings = new();

// Returns an empty dictionary if the setting doesn't exist and null if the types are invalid
private static Dictionary<K, V>? _getDictionary<K, V>(string setting)
private static Dictionary<K, V?> _getDictionary<K, V>(string setting)
where K : notnull
{
try
{
try
{
if (dictionarySettings.TryGetValue(setting, out Dictionary<object, object>? result))
if (_dictionarySettings.TryGetValue(setting, out Dictionary<object, object?>? result))
{
// If the setting was cached
return result.ToDictionary(
kvp => (K)kvp.Key,
kvp => (V)kvp.Value
kvp => (V?)kvp.Value
);
}
}
Expand All @@ -33,16 +33,16 @@ public static partial class Settings
return null;
}

// Otherwhise, load the setting from disk and cache that setting
Dictionary<K, V> value = new();
// Otherwise, load the setting from disk and cache that setting
Dictionary<K, V?> value = new();
if (File.Exists(Path.Join(CoreData.UniGetUIDataDirectory, $"{setting}.json")))
{
string result = File.ReadAllText(Path.Join(CoreData.UniGetUIDataDirectory, $"{setting}.json"));
try
{
if (result != "")
{
Dictionary<K, V>? item = JsonSerializer.Deserialize<Dictionary<K, V>>(result);
Dictionary<K, V?>? item = JsonSerializer.Deserialize<Dictionary<K, V?>>(result);
if (item is not null)
{
value = item;
Expand All @@ -56,7 +56,7 @@ public static partial class Settings
}
}

dictionarySettings[setting] = value.ToDictionary(
_dictionarySettings[setting] = value.ToDictionary(
kvp => (object)kvp.Key,
kvp => (object?)kvp.Value
);
Expand All @@ -71,16 +71,16 @@ public static partial class Settings
}

// Returns an empty dictionary if the setting doesn't exist and null if the types are invalid
public static IReadOnlyDictionary<K, V>? GetDictionary<K, V>(string setting)
public static IReadOnlyDictionary<K, V?> GetDictionary<K, V>(string setting)
where K : notnull
{
return _getDictionary<K, V>(setting);
return _getDictionary<K, V?>(setting);
}

public static void SetDictionary<K, V>(string setting, Dictionary<K, V> value)
where K : notnull
{
dictionarySettings[setting] = value.ToDictionary(
_dictionarySettings[setting] = value.ToDictionary(
kvp => (object)kvp.Key,
kvp => (object?)kvp.Value
);
Expand All @@ -101,7 +101,7 @@ public static void SetDictionary<K, V>(string setting, Dictionary<K, V> value)
public static V? GetDictionaryItem<K, V>(string setting, K key)
where K : notnull
{
Dictionary<K, V>? dictionary = _getDictionary<K, V>(setting);
Dictionary<K, V?>? dictionary = _getDictionary<K, V>(setting);
if (dictionary == null || !dictionary.TryGetValue(key, out V? value)) return default;

return value;
Expand All @@ -111,7 +111,7 @@ public static void SetDictionary<K, V>(string setting, Dictionary<K, V> value)
public static V? SetDictionaryItem<K, V>(string setting, K key, V value)
where K : notnull
{
Dictionary<K, V>? dictionary = _getDictionary<K, V>(setting);
Dictionary<K, V?>? dictionary = _getDictionary<K, V>(setting);
if (dictionary == null)
{
dictionary = new()
Expand All @@ -128,18 +128,16 @@ public static void SetDictionary<K, V>(string setting, Dictionary<K, V> value)
SetDictionary(setting, dictionary);
return oldValue;
}
else
{
dictionary.Add(key, value);
SetDictionary(setting, dictionary);
return default;
}

dictionary.Add(key, value);
SetDictionary(setting, dictionary);
return default;
}

public static V? RemoveDictionaryKey<K, V>(string setting, K key)
where K : notnull
{
Dictionary<K, V>? dictionary = _getDictionary<K, V>(setting);
Dictionary<K, V?>? dictionary = _getDictionary<K, V>(setting);
if (dictionary == null) return default;

bool success = false;
Expand All @@ -156,7 +154,7 @@ public static void SetDictionary<K, V>(string setting, Dictionary<K, V> value)
public static bool DictionaryContainsKey<K, V>(string setting, K key)
where K : notnull
{
Dictionary<K, V>? dictionary = _getDictionary<K, V>(setting);
Dictionary<K, V?>? dictionary = _getDictionary<K, V>(setting);
if (dictionary == null) return false;

return dictionary.ContainsKey(key);
Expand All @@ -165,7 +163,7 @@ public static bool DictionaryContainsKey<K, V>(string setting, K key)
public static bool DictionaryContainsValue<K, V>(string setting, V value)
where K : notnull
{
Dictionary<K, V>? dictionary = _getDictionary<K, V>(setting);
Dictionary<K, V?>? dictionary = _getDictionary<K, V>(setting);
if (dictionary == null) return false;

return dictionary.ContainsValue(value);
Expand Down
2 changes: 1 addition & 1 deletion src/UniGetUI.Core.Settings/SettingsEngine_Lists.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static partial class Settings
{
Logger.Error($"Could not load list {setting} from settings");
Logger.Error(ex);
return new();
return [];
}
}

Expand Down
Loading