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 concurrent issue #140

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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