Skip to content

Commit 0bc8d58

Browse files
committed
fixed autoload mods, updated to 1.4.3
1 parent 3b7b64b commit 0bc8d58

File tree

7 files changed

+79
-67
lines changed

7 files changed

+79
-67
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>1.4.2</Version>
3+
<Version>1.4.3</Version>
44
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<LangVersion>latest</LangVersion>

src/Addons/Providers/InstalledAddonsProvider.cs

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,17 @@ public async Task CreateCache(bool createNew, AddonTypeEnum addonType)
113113

114114
_isCacheUpdating = true;
115115

116+
var cache = addonType switch
117+
{
118+
AddonTypeEnum.TC => _campaignsCache,
119+
AddonTypeEnum.Map => _mapsCache,
120+
AddonTypeEnum.Mod => _modsCache,
121+
_ => throw new NotImplementedException(),
122+
};
123+
116124
if (createNew)
117125
{
118-
_campaignsCache.Clear();
119-
_campaignsCache.Clear();
120-
_campaignsCache.Clear();
126+
cache.Clear();
121127
}
122128

123129
try
@@ -241,26 +247,28 @@ public async Task AddAddonAsync(string pathToFile)
241247
return;
242248
}
243249

244-
//foreach (var addon in addons)
245-
//{
246-
// if (!_cache.TryGetValue(addon.Type, out _))
247-
// {
248-
// _cache.Add(addon.Type, []);
249-
// }
250-
251-
// var dict = _cache[addon.Type];
252-
253-
// if (dict.TryGetValue(new(addon.Id, addon.Version), out _))
254-
// {
255-
// dict[new(addon.Id, addon.Version)] = addon;
256-
// }
257-
// else
258-
// {
259-
// dict.Add(new(addon.Id, addon.Version), addon);
260-
// }
261-
262-
// AddonsChangedEvent?.Invoke(_game, addon.Type);
263-
//}
250+
foreach (var addon in addons)
251+
{
252+
var cache = addon.Type switch
253+
{
254+
AddonTypeEnum.TC => _campaignsCache,
255+
AddonTypeEnum.Map => _mapsCache,
256+
AddonTypeEnum.Mod => _modsCache,
257+
_ => throw new NotImplementedException(),
258+
};
259+
260+
261+
if (cache.TryGetValue(new(addon.Id, addon.Version), out _))
262+
{
263+
cache[new(addon.Id, addon.Version)] = addon;
264+
}
265+
else
266+
{
267+
cache.Add(new(addon.Id, addon.Version), addon);
268+
}
269+
270+
AddonsChangedEvent?.Invoke(_game, addon.Type);
271+
}
264272
}
265273

266274
/// <inheritdoc/>
@@ -309,8 +317,14 @@ public void DeleteAddon(IAddon addon)
309317
/// <inheritdoc/>
310318
public void EnableAddon(AddonVersion addon)
311319
{
312-
if (!_modsCache.TryGetValue(addon, out var mod) ||
313-
mod is not AutoloadMod autoloadMod)
320+
var existing = _modsCache.FirstOrDefault(x => x.Key.Equals(addon));
321+
322+
if (existing.Value is not AutoloadMod autoloadMod)
323+
{
324+
return;
325+
}
326+
327+
if (autoloadMod.IsEnabled)
314328
{
315329
return;
316330
}
@@ -333,13 +347,12 @@ public void EnableAddon(AddonVersion addon)
333347
}
334348
}
335349

336-
var otherVersions =
337-
from moda in _modsCache
338-
where moda.Key.Id.Equals(addon.Id)
339-
&& VersionComparer.Compare(moda.Key.Version, addon.Version, "==")
340-
&& moda.Value.FileName!.Equals(mod.FileName)
341-
select moda;
342-
350+
var otherVersions = _modsCache
351+
.Where(x =>
352+
x.Key.Id == addon.Id &&
353+
!VersionComparer.Compare(x.Key.Version, addon.Version, "==") &&
354+
!x.Value.FileName!.Equals(autoloadMod.FileName)
355+
);
343356

344357
foreach (var version in otherVersions)
345358
{
@@ -352,8 +365,14 @@ where moda.Key.Id.Equals(addon.Id)
352365
/// <inheritdoc/>
353366
public void DisableAddon(AddonVersion addon)
354367
{
355-
if (!_modsCache.TryGetValue(addon, out var mod) ||
356-
mod is not AutoloadMod autoloadMod)
368+
var existing = _modsCache.FirstOrDefault(x => x.Key.Equals(addon));
369+
370+
if (existing.Value is not AutoloadMod autoloadMod)
371+
{
372+
return;
373+
}
374+
375+
if (!autoloadMod.IsEnabled)
357376
{
358377
return;
359378
}

src/Avalonia.Desktop/Controls/ModsControl.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
<StackPanel Orientation="Horizontal">
4141

42-
<CheckBox IsChecked="{Binding IsEnabled}"
42+
<CheckBox IsChecked="{Binding IsEnabled, Mode=OneWay}"
4343
Margin="5,0,0,0"
4444
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=((vm:ModsViewModel)DataContext).ModCheckboxPressedCommand}"
4545
CommandParameter="{Binding $parent[ListBoxItem].Content}"/>

src/Avalonia.Desktop/ViewModels/ModsViewModel.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,7 @@ private void OpenFolder()
119119
/// Refresh campaigns list
120120
/// </summary>
121121
[RelayCommand]
122-
private async Task RefreshListAsync()
123-
{
124-
await UpdateAsync(true).ConfigureAwait(true);
125-
}
122+
private Task RefreshListAsync() => UpdateAsync(true);
126123

127124

128125
/// <summary>
@@ -146,7 +143,7 @@ private void ModCheckboxPressed(object? obj)
146143
obj.ThrowIfNotType<AutoloadMod>(out var mod);
147144

148145
//disabling
149-
if (!mod.IsEnabled)
146+
if (mod.IsEnabled)
150147
{
151148
_installedAddonsProvider.DisableAddon(new(mod.Id, mod.Version));
152149
}

src/Common.Common/AddonVersion.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Diagnostics.CodeAnalysis;
1+
using Common.Helpers;
2+
using System.Diagnostics.CodeAnalysis;
23

34
namespace Common;
45

@@ -33,13 +34,12 @@ public override bool Equals([NotNullWhen(true)] object? obj)
3334
throw new ArgumentNullException(nameof(obj));
3435
}
3536

36-
if ((Version is null || addon.Version is null) &&
37-
Id.Equals(addon.Id))
37+
if (!Id.Equals(addon.Id, StringComparison.OrdinalIgnoreCase))
3838
{
39-
return true;
39+
return false;
4040
}
4141

42-
return base.Equals(obj);
42+
return VersionComparer.Compare(Version, addon.Version, "==");
4343
}
4444

4545
public static bool operator ==(AddonVersion left, AddonVersion right)

src/Common.Common/Helpers/VersionComparer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace Common.Helpers;
44

55
public static class VersionComparer
66
{
7-
public static bool Compare(string? v1, string v2)
7+
public static bool Compare(string? v1, string? v2)
88
{
9-
if (v1 is null)
9+
if (v1 is null || v2 is null)
1010
{
1111
return true;
1212
}
@@ -50,7 +50,7 @@ public static bool Compare(string? v1, string v2)
5050

5151
public static bool Compare(string? v1, string? v2, string op)
5252
{
53-
if (v1 is null)
53+
if (v1 is null || v2 is null)
5454
{
5555
return true;
5656
}

src/Tests/VersionCompareTests.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ public sealed class VersionCompareTests
1313
[InlineData("1.1", ">1.0")]
1414
[InlineData("1.1", "<1.3")]
1515
[InlineData(null, "==1")]
16-
public void CompareTestTrue(string? v1, string v2)
16+
[InlineData("1", null)]
17+
[InlineData(null, null)]
18+
public void CompareTestTrue(string? v1, string? v2)
1719
{
1820
var result = VersionComparer.Compare(v1, v2);
1921

@@ -34,25 +36,19 @@ public void CompareTestFalse(string v1, string v2)
3436
}
3537

3638
[Theory]
37-
[InlineData("1.1-a1", "==1.1-a1")]
38-
[InlineData("1.1-a1", ">=1.1-a1")]
39-
[InlineData("1.1-a1", ">=1.1-a0")]
40-
[InlineData("1.1-a1", "<=1.1-a1")]
41-
[InlineData("1.1-a1", "<=1.1-a2")]
42-
[InlineData("1.1-a1", ">1.1-a0")]
43-
[InlineData("1.1-a1", "<1.1-a3")]
44-
public void CompareTest2(string v1, string v2)
45-
{
46-
var result = VersionComparer.Compare(v1, v2);
47-
48-
Assert.True(result);
49-
}
50-
51-
[Theory]
39+
[InlineData("1.1-a1", "1.1-a1", "==")]
40+
[InlineData("1.1-a1", "1.1-a1", ">=")]
41+
[InlineData("1.1-a1", "1.1-a0", ">=")]
42+
[InlineData("1.1-a1", "1.1-a1", "<=")]
43+
[InlineData("1.1-a1", "1.1-a2", "<=")]
44+
[InlineData("1.1-a1", "1.1-a0", ">")]
45+
[InlineData("1.1-a1", "1.1-a3", "<")]
46+
[InlineData(null, "1", "==")]
47+
[InlineData("1", null, "==")]
5248
[InlineData(null, null, "==")]
53-
public void CompareTest3(string? v1, string? v2, string c)
49+
public void CompareTest2(string? v1, string? v2, string op)
5450
{
55-
var result = VersionComparer.Compare(v1, v2, c);
51+
var result = VersionComparer.Compare(v1, v2, op);
5652

5753
Assert.True(result);
5854
}

0 commit comments

Comments
 (0)