ValveResourceFormat / ValveKeyValue

📃 Next-generation Valve's key value framework for .NET
https://www.nuget.org/packages/ValveKeyValue/
MIT License
146 stars 37 forks source link

KV1BinaryNodeType.WideString #95

Open Ellome opened 6 months ago

Ellome commented 6 months ago

Was looking through steamapi msgs and found kv wstring type in lobby chat

In case one will need it, here is a short add for ValveKeyValue/Deserialization/KeyValues1/KV1BinaryReader.cs

replace

case KV1BinaryNodeType.WideString:
                    throw new NotSupportedException("Wide String is not supported, please create an issue saying where you found it: https://github.com/ValveResourceFormat/ValveKeyValue/issues");

with

case KV1BinaryNodeType.WideString:
    byte[] bytes = new byte[2];

    bytes = reader.ReadBytes(2);

    if (BitConverter.IsLittleEndian)
    {
        Array.Reverse(bytes, 0, 2);
    }

    int length = BitConverter.ToInt16(bytes, 0);

    List<byte> byteList = new List<byte>();

    for(int i = 0; i < length; i++)
    {
        bytes = reader.ReadBytes(2);

        byteList.AddRange(bytes);
    }

    string wstring_utf16 = Encoding.BigEndianUnicode.GetString(byteList.ToArray());
    //string wstring_utf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(wstring_utf16));

    value = new KVObjectValue<string>(wstring_utf16, KVValueType.WString);
    //value = new KVObjectValue<string>(wstring_utf8, KVValueType.WString);
    break;
xPaw commented 6 months ago

Can you create a pull request (with some tests)?

Ellome commented 6 months ago

Yes, but since I only found out what .NET is yesterday, this will not be quick xD. By the way, with these same messages from the lobby, there was another moment akin to a magic header, specific to the game, so I'll get both sorted out at once.

yaakov-h commented 6 months ago

We have the existing C++ code quoted here: #34

A magic header for game-specific data probably doesn't belong in this lib? It's hard for me to judge without seeing it though.

Ellome commented 6 months ago

We have the existing C++ code quoted here: #34

A magic header for game-specific data probably doesn't belong in this lib? It's hard for me to judge without seeing it though.

Though there was none in lib. If you can make pull request or rewrite it, you will surely do it faster. Please tell then whether will you implement it, so i wont.

L4D series prepend int32be version to every lobbychat message, not sure about other games, and thats why NetHookAnalyzer2 didnt show them as KeyValues correctly and also its KV lib didnt support AlternateEnd \x0b, so i updated its code and used this lib for these messages to be shown convinient way as a tree.

Since i am not that familiar neither with git, nor c# it will be better if you will make commit for this