From c3dc13e1b8de863cb266419f20c522e9e58f5e0d Mon Sep 17 00:00:00 2001 From: laolarou Date: Wed, 3 Apr 2024 16:51:17 -0700 Subject: [PATCH 1/2] fix concurrent issue --- .../Launch/DefaultLauncherAccountParser.cs | 72 ++++++++++--------- .../Launch/DefaultLauncherProfileParser.cs | 39 +++++++--- 2 files changed, 68 insertions(+), 43 deletions(-) diff --git a/ProjBobcat/ProjBobcat/DefaultComponent/Launch/DefaultLauncherAccountParser.cs b/ProjBobcat/ProjBobcat/DefaultComponent/Launch/DefaultLauncherAccountParser.cs index 7d9e8bb..0fc7b81 100644 --- a/ProjBobcat/ProjBobcat/DefaultComponent/Launch/DefaultLauncherAccountParser.cs +++ b/ProjBobcat/ProjBobcat/DefaultComponent/Launch/DefaultLauncherAccountParser.cs @@ -13,6 +13,7 @@ namespace ProjBobcat.DefaultComponent.Launch; public class DefaultLauncherAccountParser : LauncherParserBase, ILauncherAccountParser { + readonly object _lock = new(); readonly string _fullLauncherAccountPath; /// @@ -69,12 +70,15 @@ LauncherAccountModel GenerateLauncherAccountModel(Guid clientToken) public bool ActivateAccount(string uuid) { - if (!(LauncherAccount?.Accounts?.ContainsKey(uuid) ?? false)) - return false; - - LauncherAccount.ActiveAccountLocalId = uuid; + lock (_lock) + { + if (!(LauncherAccount?.Accounts?.ContainsKey(uuid) ?? false)) + return false; - Save(); + LauncherAccount.ActiveAccountLocalId = uuid; + Save(); + } + return true; } @@ -87,35 +91,33 @@ public bool AddNewAccount(string uuid, AccountModel account, out Guid? id) } LauncherAccount.Accounts ??= []; - - if (LauncherAccount.Accounts.ContainsKey(uuid)) + + lock (_lock) { - id = null; - return false; + if (LauncherAccount.Accounts.ContainsKey(uuid)) + { + id = null; + return false; + } } - var oldRecord = LauncherAccount.Accounts - .FirstOrDefault(a => a.Value.MinecraftProfile?.Id == account.MinecraftProfile?.Id).Value; - if (oldRecord != null) + lock (_lock) { - id = oldRecord.Id; - return true; + var oldRecord = LauncherAccount.Accounts + .FirstOrDefault(a => a.Value.MinecraftProfile?.Id == account.MinecraftProfile?.Id).Value; + if (oldRecord != null) + { + id = oldRecord.Id; + return true; + } } - var newId = Guid.NewGuid(); - /* - var existsAccount = LauncherAccount.Accounts - .FirstOrDefault(p => p.Value?.RemoteId?.Equals(account.RemoteId, StringComparison.OrdinalIgnoreCase) ?? false); - var (key, value) = existsAccount; - if(!string.IsNullOrEmpty(key) && value != null) - { - LauncherAccount.Accounts[key] = value; - } - else - */ + lock (_lock) { + var newId = Guid.NewGuid(); var findResult = Find(account.Id); + if (findResult is { Key: not null, Value: not null }) { newId = account.Id; @@ -127,17 +129,20 @@ public bool AddNewAccount(string uuid, AccountModel account, out Guid? id) LauncherAccount.Accounts.Add(uuid, account); } - } - - Save(); - id = newId; + Save(); + id = newId; + } + return true; } public KeyValuePair? Find(Guid id) { - return LauncherAccount?.Accounts?.FirstOrDefault(a => a.Value.Id == id); + lock (_lock) + { + return LauncherAccount?.Accounts?.FirstOrDefault(a => a.Value.Id == id); + } } public bool RemoveAccount(Guid id) @@ -150,8 +155,11 @@ public bool RemoveAccount(Guid id) if (string.IsNullOrEmpty(key)) return false; - LauncherAccount?.Accounts?.Remove(key); - Save(); + lock (_lock) + { + LauncherAccount?.Accounts?.Remove(key); + Save(); + } return true; } diff --git a/ProjBobcat/ProjBobcat/DefaultComponent/Launch/DefaultLauncherProfileParser.cs b/ProjBobcat/ProjBobcat/DefaultComponent/Launch/DefaultLauncherProfileParser.cs index 2ccdf4f..366aa93 100644 --- a/ProjBobcat/ProjBobcat/DefaultComponent/Launch/DefaultLauncherProfileParser.cs +++ b/ProjBobcat/ProjBobcat/DefaultComponent/Launch/DefaultLauncherProfileParser.cs @@ -18,6 +18,7 @@ namespace ProjBobcat.DefaultComponent.Launch; /// public sealed class DefaultLauncherProfileParser : LauncherParserBase, ILauncherProfileParser { + readonly object _lock = new(); readonly string _fullLauncherProfilePath; /// @@ -87,35 +88,51 @@ public void AddNewGameProfile(GameProfileModel gameProfile) if (string.IsNullOrEmpty(gameProfile.Name)) return; if (IsGameProfileExist(gameProfile.Name)) return; - LauncherProfile.Profiles!.Add(gameProfile.Name, gameProfile); - SaveProfile(); + lock (_lock) + { + LauncherProfile.Profiles!.Add(gameProfile.Name, gameProfile); + SaveProfile(); + } } public void EmptyGameProfiles() { - LauncherProfile.Profiles?.Clear(); - SaveProfile(); + lock (_lock) + { + LauncherProfile.Profiles?.Clear(); + SaveProfile(); + } } public GameProfileModel GetGameProfile(string name) { - var profile = LauncherProfile.Profiles!.FirstOrDefault( - p => p.Value.Name?.Equals(name, StringComparison.Ordinal) ?? false).Value ?? - throw new UnknownGameNameException(name); + lock (_lock) + { + var profile = LauncherProfile.Profiles!.FirstOrDefault( + p => p.Value.Name?.Equals(name, StringComparison.Ordinal) ?? false).Value ?? + throw new UnknownGameNameException(name); - profile.Resolution ??= new ResolutionModel(); + profile.Resolution ??= new ResolutionModel(); - return profile; + return profile; + } } public bool IsGameProfileExist(string name) { - return LauncherProfile.Profiles!.Any(p => p.Value.Name?.Equals(name, StringComparison.Ordinal) ?? false); + lock (_lock) + { + return LauncherProfile.Profiles! + .Any(p => p.Value.Name?.Equals(name, StringComparison.Ordinal) ?? false); + } } public void RemoveGameProfile(string name) { - LauncherProfile.Profiles!.Remove(name); + lock (_lock) + { + LauncherProfile.Profiles!.Remove(name); + } } public void SaveProfile() From 1465140151ffdd10dee4cbc5a7d56c4690a63b39 Mon Sep 17 00:00:00 2001 From: laolarou Date: Sat, 6 Apr 2024 11:06:10 -0700 Subject: [PATCH 2/2] Update DownloadHelper.cs --- ProjBobcat/ProjBobcat/Class/Helper/DownloadHelper.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/ProjBobcat/ProjBobcat/Class/Helper/DownloadHelper.cs b/ProjBobcat/ProjBobcat/Class/Helper/DownloadHelper.cs index ff12f5f..b919d8f 100644 --- a/ProjBobcat/ProjBobcat/Class/Helper/DownloadHelper.cs +++ b/ProjBobcat/ProjBobcat/Class/Helper/DownloadHelper.cs @@ -1,5 +1,4 @@ using System; -using System.Buffers; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; @@ -25,7 +24,6 @@ public static class DownloadHelper /// public static int DownloadThread { get; set; } = 8; - const int DefaultBufferSize = 1024 * 1024 * 4; static HttpClient Head => HttpClientHelper.HeadClient; static HttpClient Data => HttpClientHelper.DataClient; static HttpClient MultiPart => HttpClientHelper.MultiPartClient;