Fix match deserialization not reading 7-digit hex values

This commit is contained in:
jvyden 2022-01-16 19:59:24 -05:00
commit be7200aaf0
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
2 changed files with 11 additions and 4 deletions

View file

@ -6,6 +6,7 @@ using System.Text.Json;
using System.Threading.Tasks;
using Kettu;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Helpers.Extensions;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types;
using LBPUnion.ProjectLighthouse.Types.Match;
@ -53,8 +54,10 @@ namespace LBPUnion.ProjectLighthouse.Controllers
}
catch(Exception e)
{
Logger.Log("Exception while parsing MatchData: " + e, LoggerLevelMatch.Instance);
Logger.Log("Data: " + bodyString, LoggerLevelMatch.Instance);
Logger.Log("Exception while parsing matchData: ", LoggerLevelMatch.Instance);
string[] lines = e.ToDetailedException().Split("\n");
foreach (string line in lines) Logger.Log(line, LoggerLevelMatch.Instance);
Logger.Log("Associated matchData: " + bodyString, LoggerLevelMatch.Instance);
return this.BadRequest();
}
@ -62,7 +65,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers
if (matchData == null)
{
Logger.Log("Could not parse match data: matchData is null", LoggerLevelMatch.Instance);
Logger.Log("Data: " + bodyString, LoggerLevelMatch.Instance);
Logger.Log("Associated matchData: " + bodyString, LoggerLevelMatch.Instance);
return this.BadRequest();
}

View file

@ -36,6 +36,7 @@ namespace LBPUnion.ProjectLighthouse.Helpers
return recentlyDivedIn.Contains(otherUserId);
}
// This is the function used to show people how laughably awful LBP's protocol is. Beware.
public static IMatchData? Deserialize(string data)
{
string matchType = "";
@ -49,10 +50,13 @@ namespace LBPUnion.ProjectLighthouse.Helpers
i++;
}
string matchData = $"{{{string.Concat(data.Skip(matchType.Length + 3).SkipLast(2))}}}";
string matchData = $"{{{string.Concat(data.Skip(matchType.Length + 3).SkipLast(2))}}}"; // unfuck formatting so we can parse it as json
// JSON does not like the hex value that location comes in (0x7f000001) so, convert it to int
matchData = Regex.Replace(matchData, @"0x[a-fA-F0-9]{8}", m => Convert.ToInt32(m.Value, 16).ToString());
// oh, but it gets better than that! LBP also likes to send hex values with an uneven amount of digits (0xa000064, 7 digits). in any case, we handle it here:
matchData = Regex.Replace(matchData, @"0x[a-fA-F0-9]{7}", m => Convert.ToInt32(m.Value, 16).ToString());
// i'm actually crying about it.
return Deserialize(matchType, matchData);
}