Skip to content

Commit

Permalink
Merge pull request #140 from Corona-Studio/hotfix
Browse files Browse the repository at this point in the history
fix concurrent issue
  • Loading branch information
laolarou726 committed Apr 8, 2024
2 parents 4af878b + 1465140 commit 4e84ee2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 45 deletions.
2 changes: 0 additions & 2 deletions ProjBobcat/ProjBobcat/Class/Helper/DownloadHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -25,7 +24,6 @@ public static class DownloadHelper
/// </summary>
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace ProjBobcat.DefaultComponent.Launch;

public class DefaultLauncherAccountParser : LauncherParserBase, ILauncherAccountParser
{
readonly object _lock = new();
readonly string _fullLauncherAccountPath;

/// <summary>
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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<string, AccountModel>? 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)
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace ProjBobcat.DefaultComponent.Launch;
/// </summary>
public sealed class DefaultLauncherProfileParser : LauncherParserBase, ILauncherProfileParser
{
readonly object _lock = new();
readonly string _fullLauncherProfilePath;

/// <summary>
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 4e84ee2

Please sign in to comment.