LogicAndTrick / sledge-formats

C# parsers and formats for Half-Life 1 and related engines.
MIT License
71 stars 10 forks source link

SerialisedObjectFormatter fails to parse property with serialized JSON inside #3

Closed 13xforever closed 3 years ago

13xforever commented 3 years ago

The easiest way to reproduce is to try and parse steam\userdata\<user_id>\7\remote\sharedconfig.vdf, which has such property at UserLocalConfigStore>Software>Valve>Steam>FriendsUI>FriendsUIJSON.

Here's an excerpt:

"FriendsUI"
{
    "FriendsUIJSON"     "{\"bNotifications_ShowIngame\":false,\"bNotifications_ShowOnline\":false,\"bNotifications_ShowMessage\":true,\"bNotifications_EventsAndAnnouncements\":true,\"bSounds_PlayIngame\":false,\"bSounds_PlayOnline\":false,\"bSounds_PlayMessage\":true,\"bSounds_EventsAndAnnouncements\":false,\"bAlwaysNewChatWindow\":false,\"bForceAlphabeticFriendSorting\":false,\"nChatFlashMode\":1,\"bRememberOpenChats\":false,\"bCompactQuickAccess\":true,\"bCompactFriendsList\":true,\"bNotifications_ShowChatRoomNotification\":false,\"bSounds_PlayChatRoomNotification\":false,\"bHideOfflineFriendsInTagGroups\":false,\"bHideCategorizedFriends\":true,\"bCategorizeInGameFriendsByGame\":false,\"nChatFontSize\":2,\"b24HourClock\":true,\"bDoNotDisturbMode\":true,\"bDisableEmbedInlining\":false,\"bSignIntoFriends\":true,\"bDisableSpellcheck\":false,\"bDisableRoomEffects\":false,\"bAnimatedAvatars\":true,\"featuresEnabled\":{\"DoNotDisturb\":1,\"LoaderWindowSynchronization\":1,\"NonFriendMessageHandling\":1,\"NewVoiceHotKeyState\":1,\"PersonaNotifications\":1,\"ServerVirtualizedMemberLists\":1,\"SteamworksChatAPI\":1},\"bAutoSignIntoFriends\":true,\"bShowTimeInChatLogCheck\":true,\"bSingleWindowMode\":false}"
}
LogicAndTrick commented 3 years ago

Thanks for the bug report! This is probably because of the escaped quotes in the string - the SplitWithQuotes method doesn't have any support for escaped quotes. The method already loops through each character in the line, so adding support for an escape character shouldn't be too hard. It's a good chance for me to write some unit tests for it as well.

https://github.com/LogicAndTrick/sledge-formats/blob/0a75239b6c49af8c7ecea04d12eef0313ba50a26/Sledge.Formats/StringExtensions.cs#L17

Some basic test cases for my own reference

var testCases = new Dictionary<string, string[]>
{
    ["Simple"] = new[] { "Simple" },
    ["No Quotes"] = new[] { "No", "Quotes" },
    [@"""With"" ""Quotes"""] = new[] { "With", "Quotes" },
    [@"""Empty"" """""] = new[] { "Empty", "" },
    [@"""With"" ""\""Escaped\"" Quotes"""] = new[] { "With", @"""Escaped"" Quotes" },
    [@"""Json"" ""{ \""Key\"": \""Value\"" }"""] = new [] {"Json", @"{ ""Key"": ""Value"" }"}
};

foreach (var (input, expected) in testCases)
{
    $"TEST: {input}".Dump();
    try
    {
        var actual = SplitWithQuotes(input);
        if (!actual.SequenceEqual(expected)) $"Test failed. Expected <{String.Join(", ", expected)}>, got  <{String.Join(", ", actual)}>".Dump();
        else "Test passed.".Dump();
    }
    catch (Exception ex)
    {
        $"Test failed. Exception: {ex.Message}".Dump();
    }
    "".Dump();
}

Current output:

TEST: Simple
Test passed.

TEST: No Quotes
Test passed.

TEST: "With" "Quotes"
Test passed.

TEST: "Empty" ""
Test passed.

TEST: "With" "\"Escaped\" Quotes"
Test failed. Exception: Index and length must refer to a location within the string. (Parameter 'length')

TEST: "Json" "{ \"Key\": \"Value\" }"
Test failed. Exception: Index and length must refer to a location within the string. (Parameter 'length')