Skip to content

Commit 84c845e

Browse files
CopilotBornToBeRoot
andcommitted
Final polish: add constant for buffer size, use string interpolation, fix compatibility
Co-authored-by: BornToBeRoot <16019165+BornToBeRoot@users.noreply.github.com>
1 parent 56d5d27 commit 84c845e

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

Source/NETworkManager.Profiles/ProfileManager.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public static class ProfileManager
5757
Converters = { new JsonStringEnumConverter() }
5858
};
5959

60+
/// <summary>
61+
/// Maximum number of bytes to check for XML content detection.
62+
/// </summary>
63+
private const int XmlDetectionBufferSize = 200;
64+
6065
/// <summary>
6166
/// ObservableCollection of all profile files.
6267
/// </summary>
@@ -167,9 +172,9 @@ private static string GetJsonProfilePath(string originalPath, bool encrypted)
167172
var basePath = Path.ChangeExtension(originalPath, null);
168173

169174
if (encrypted)
170-
return basePath + ProfileFileExtension + ProfileFileExtensionEncrypted;
175+
return $"{basePath}{ProfileFileExtension}{ProfileFileExtensionEncrypted}";
171176

172-
return basePath + ProfileFileExtension;
177+
return $"{basePath}{ProfileFileExtension}";
173178
}
174179

175180
/// <summary>
@@ -521,7 +526,7 @@ private static void Load(ProfileFileInfo profileFileInfo)
521526
var originalPath = profileFileInfo.Path;
522527

523528
// Migrate to JSON format by saving the file
524-
var newPath = Path.ChangeExtension(profileFileInfo.Path, ProfileFileExtension + ProfileFileExtensionEncrypted);
529+
var newPath = GetJsonProfilePath(profileFileInfo.Path, true);
525530
var newProfileFileInfo = new ProfileFileInfo(profileFileInfo.Name, newPath, true)
526531
{
527532
Password = profileFileInfo.Password,
@@ -638,8 +643,8 @@ private static bool IsXmlContent(byte[] data)
638643

639644
try
640645
{
641-
// Only check the first 200 bytes for performance
642-
var bytesToCheck = Math.Min(200, data.Length);
646+
// Only check the first few bytes for performance
647+
var bytesToCheck = Math.Min(XmlDetectionBufferSize, data.Length);
643648
var text = Encoding.UTF8.GetString(data, 0, bytesToCheck).TrimStart();
644649
// Check for XML declaration or root element that matches profile structure
645650
return text.StartsWith("<?xml") || text.StartsWith("<ArrayOfGroupInfoSerializable");
@@ -896,7 +901,7 @@ private static List<GroupInfo> DeserializeGroup(List<GroupInfoSerializable> grou
896901
throw new ArgumentNullException(nameof(groupsSerializable));
897902

898903
return (from groupSerializable in groupsSerializable
899-
let profiles = (groupSerializable.Profiles ?? []).Select(profileSerializable => new ProfileInfo(profileSerializable)
904+
let profiles = (groupSerializable.Profiles ?? new List<ProfileInfoSerializable>()).Select(profileSerializable => new ProfileInfo(profileSerializable)
900905
{
901906
// Migrate old tags to new tags list
902907
// if TagsList is null or empty and Tags is not null or empty, split Tags by ';' and create a new ObservableSetCollection

0 commit comments

Comments
 (0)