Skip to content

Commit

Permalink
fixed addons sometimes not adding to the lists
Browse files Browse the repository at this point in the history
  • Loading branch information
fgsfds committed May 30, 2024
1 parent 83124f8 commit 6e6cd2b
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 36 deletions.
12 changes: 5 additions & 7 deletions src/Avalonia/Core/ViewModels/CampaignsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ ScoresProvider scoresProvider
_playtimeProvider = playtimeProvider;

_gamesProvider.GameChangedEvent += OnGameChanged;
Game.DownloadableAddonsProvider.AddonDownloadedEvent += OnAddonDownloaded;
Game.DownloadableAddonsProvider.AddonDownloadedEvent += OnAddonChanged;
Game.InstalledAddonsProvider.AddonsChangedEvent += OnAddonChanged;
}


Expand Down Expand Up @@ -215,15 +216,12 @@ private void OnGameChanged(GameEnum parameterName)
}
}

private void OnAddonDownloaded(IGame game, AddonTypeEnum addonType)
private void OnAddonChanged(IGame game, AddonTypeEnum? addonType)
{
if (game.GameEnum != Game.GameEnum ||
addonType is not AddonTypeEnum.TC)
if (game.GameEnum == Game.GameEnum && (addonType is AddonTypeEnum.TC || addonType is null))
{
return;
OnPropertyChanged(nameof(CampaignsList));
}

OnPropertyChanged(nameof(CampaignsList));
}
}
}
4 changes: 2 additions & 2 deletions src/Avalonia/Core/ViewModels/DownloadsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public DownloadsViewModel(IGame game)
Game = game;

Game.DownloadableAddonsProvider.AddonDownloadedEvent += OnAddonChanged;
Game.InstalledAddonsProvider.AddonDeletedEvent += OnAddonChanged;
Game.InstalledAddonsProvider.AddonsChangedEvent += OnAddonChanged;
}


Expand Down Expand Up @@ -195,7 +195,7 @@ private void OnProgressChanged(object? sender, float e)
OnPropertyChanged(nameof(ProgressBarValue));
}

private void OnAddonChanged(IGame game, AddonTypeEnum addonType)
private void OnAddonChanged(IGame game, AddonTypeEnum? addonType)
{
if (game.GameEnum != Game.GameEnum)
{
Expand Down
12 changes: 5 additions & 7 deletions src/Avalonia/Core/ViewModels/MapsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ ScoresProvider scoresProvider
_playtimeProvider = playtimeProvider;

_gamesProvider.GameChangedEvent += OnGameChanged;
Game.DownloadableAddonsProvider.AddonDownloadedEvent += OnAddonDownloaded;
Game.DownloadableAddonsProvider.AddonDownloadedEvent += OnAddonChanged;
Game.InstalledAddonsProvider.AddonsChangedEvent += OnAddonChanged;
}


Expand Down Expand Up @@ -218,15 +219,12 @@ private void OnGameChanged(GameEnum parameterName)
}
}

private void OnAddonDownloaded(IGame game, AddonTypeEnum addonType)
private void OnAddonChanged(IGame game, AddonTypeEnum? addonType)
{
if (game.GameEnum != Game.GameEnum ||
addonType is not AddonTypeEnum.Map)
if (game.GameEnum == Game.GameEnum && (addonType is AddonTypeEnum.Map || addonType is null))
{
return;
OnPropertyChanged(nameof(MapsList));
}

OnPropertyChanged(nameof(MapsList));
}
}
}
12 changes: 5 additions & 7 deletions src/Avalonia/Core/ViewModels/ModsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ ScoresProvider scoresProvider
_playtimeProvider = playtimeProvider;

_gamesProvider.GameChangedEvent += OnGameChanged;
Game.DownloadableAddonsProvider.AddonDownloadedEvent += OnModDownloaded;
Game.DownloadableAddonsProvider.AddonDownloadedEvent += OnAddonChanged;
Game.InstalledAddonsProvider.AddonsChangedEvent += OnAddonChanged;
}


Expand Down Expand Up @@ -157,15 +158,12 @@ private void OnGameChanged(GameEnum parameterName)
}
}

private void OnModDownloaded(IGame game, AddonTypeEnum addonType)
private void OnAddonChanged(IGame game, AddonTypeEnum? addonType)
{
if (game.GameEnum != Game.GameEnum ||
addonType is not AddonTypeEnum.Mod)
if (game.GameEnum == Game.GameEnum && (addonType is AddonTypeEnum.Mod || addonType is null))
{
return;
OnPropertyChanged(nameof(ModsList));
}

OnPropertyChanged(nameof(ModsList));
}
}
}
5 changes: 5 additions & 0 deletions src/ClientCommon/Config/ConfigProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ private void SetGamePathValue(string? value, [CallerMemberName] string caller =
{
var setting = _dbContext.GamePaths.Find([caller]);

if (string.IsNullOrWhiteSpace(value))
{
value = null;
}

if (setting is null)
{
_dbContext.GamePaths.Add(new() { Game = caller, Path = value });
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Helpers/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

namespace Common.Helpers
{
public delegate void AddonChanged(IGame game, AddonTypeEnum modType);
public delegate void AddonChanged(IGame game, AddonTypeEnum? modType);
}
2 changes: 1 addition & 1 deletion src/Common/Interfaces/IInstalledAddonsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Common.Interfaces
{
public interface IInstalledAddonsProvider
{
event AddonChanged AddonDeletedEvent;
event AddonChanged AddonsChangedEvent;

/// <summary>
/// Add addon to cache
Expand Down
7 changes: 7 additions & 0 deletions src/Mods/Providers/DownloadableAddonsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public sealed class DownloadableAddonsProvider : IDownloadableAddonsProvider
private readonly ApiInterface _apiInterface;

private Dictionary<AddonTypeEnum, Dictionary<string, IDownloadableAddon>>? _cache;
private readonly SemaphoreSlim _semaphore = new(1);

public event AddonChanged AddonDownloadedEvent;

Expand All @@ -42,15 +43,19 @@ ApiInterface apiInterface
/// <inheritdoc/>
public async Task CreateCacheAsync()
{
await _semaphore.WaitAsync();

if (_cache is not null)
{
_semaphore.Release();
return;
}

var addons = await _apiInterface.GetAddonsAsync(_game.GameEnum).ConfigureAwait(false);

if (addons is null || addons.Count == 0)
{
_semaphore.Release();
return;
}

Expand All @@ -61,6 +66,8 @@ public async Task CreateCacheAsync()
_cache.TryAdd(addon.AddonType, []);
_cache[addon.AddonType].TryAdd(addon.Id, addon);
}

_semaphore.Release();
}


Expand Down
27 changes: 16 additions & 11 deletions src/Mods/Providers/InstalledAddonsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ public sealed class InstalledAddonsProvider : IInstalledAddonsProvider
private readonly PlaytimeProvider _playtimeProvider;

private readonly Dictionary<AddonTypeEnum, Dictionary<string, IAddon>> _cache;
private readonly SemaphoreSlim _semaphore = new(1);
private static readonly SemaphoreSlim _semaphore = new(1);

public event AddonChanged AddonDeletedEvent;
private bool _isCacheUpdating = false;

public event AddonChanged AddonsChangedEvent;

[Obsolete($"Don't create directly. Use {nameof(InstalledAddonsProvider)}.")]
public InstalledAddonsProvider(
Expand All @@ -42,7 +44,8 @@ PlaytimeProvider playtimeProvider
/// <inheritdoc/>
public async Task CreateCache(bool createNew)
{
_semaphore.Wait();
await _semaphore.WaitAsync();
_isCacheUpdating = true;

if (createNew)
{
Expand All @@ -55,7 +58,7 @@ await Task.Run(() =>
{
IEnumerable<string> files;

files = Directory.GetFiles(_game.CampaignsFolderPath).Where(static x => x.EndsWith(".zip", StringComparison.OrdinalIgnoreCase) || x.EndsWith(".grp", StringComparison.OrdinalIgnoreCase));
files = Directory.GetFiles(_game.CampaignsFolderPath, "*.zip");
var camps = GetAddonsFromFiles(AddonTypeEnum.TC, files);
_cache.Add(AddonTypeEnum.TC, camps);

Expand All @@ -66,9 +69,11 @@ await Task.Run(() =>
files = Directory.GetFiles(_game.ModsFolderPath, "*.zip");
var mods = GetAddonsFromFiles(AddonTypeEnum.Mod, files);
_cache.Add(AddonTypeEnum.Mod, mods);
});
}).WaitAsync(CancellationToken.None);
}

_isCacheUpdating = false;
AddonsChangedEvent?.Invoke(_game, null);
_semaphore.Release();

_cache.ThrowIfNull();
Expand Down Expand Up @@ -110,7 +115,7 @@ public void DeleteAddon(IAddon addon)

_cache[addon.Type].Remove(addon.Id);

AddonDeletedEvent?.Invoke(_game, addon.Type);
AddonsChangedEvent?.Invoke(_game, addon.Type);
}

/// <inheritdoc/>
Expand All @@ -132,6 +137,11 @@ public void DisableAddon(string addonId)
/// <inheritdoc/>
public Dictionary<string, IAddon> GetInstalledAddons(AddonTypeEnum addonType)
{
if (_isCacheUpdating)
{
return [];
}

_cache.ThrowIfNull();

_cache.TryGetValue(addonType, out var result);
Expand Down Expand Up @@ -233,8 +243,6 @@ newAddon.Version is not null &&
Dictionary<string, string?>? incompatibles = null;
IStartMap? startMap = null;

DukeVersionEnum? dukeVersion = null;

if (ArchiveFactory.IsArchive(pathToFile, out var _))
{
using var archive = ArchiveFactory.Open(pathToFile);
Expand Down Expand Up @@ -302,15 +310,12 @@ newAddon.Version is not null &&
{
if (manifest.SupportedGame.Version == DukeVersionEnum.Duke3D_13D.ToString())
{
dukeVersion = DukeVersionEnum.Duke3D_13D;
}
else if (manifest.SupportedGame.Version == DukeVersionEnum.Duke3D_Atomic.ToString())
{
dukeVersion = DukeVersionEnum.Duke3D_Atomic;
}
else if (manifest.SupportedGame.Version == DukeVersionEnum.Duke3D_WT.ToString())
{
dukeVersion = DukeVersionEnum.Duke3D_WT;
}
}
}
Expand Down

0 comments on commit 6e6cd2b

Please sign in to comment.