From b1c3bfd2b698df3689f3ee897aefbb0c23471baa Mon Sep 17 00:00:00 2001 From: Tiger Date: Fri, 20 Oct 2023 21:02:11 +0200 Subject: [PATCH] Changed packet reading operations to never return null --- .../Incoming/Catalog/GetCatalogIndexEvent.cs | 4 ++-- .../Incoming/Catalog/GetCatalogPageEvent.cs | 8 +++---- .../Handshake/SsoTicketMessageEvent.cs | 10 +-------- .../Badges/SetActivatedBadgesEvent.cs | 4 ++-- .../Navigator/NavigatorSearchEvent.cs | 8 ++----- .../User/Data/UserCurrentBadgesEvent.cs | 4 +--- .../Incoming/User/Data/UserFigureEvent.cs | 4 ++-- .../Incoming/User/Data/UserProfileEvent.cs | 16 ++------------ Communication/Messages/MessageHandler.cs | 8 +++---- Communication/Messages/Types/ClientMessage.cs | 22 +++++++++---------- 10 files changed, 31 insertions(+), 57 deletions(-) diff --git a/Communication/Messages/Incoming/Catalog/GetCatalogIndexEvent.cs b/Communication/Messages/Incoming/Catalog/GetCatalogIndexEvent.cs index df6c436..ce4ce30 100644 --- a/Communication/Messages/Incoming/Catalog/GetCatalogIndexEvent.cs +++ b/Communication/Messages/Incoming/Catalog/GetCatalogIndexEvent.cs @@ -26,11 +26,11 @@ public class GetCatalogIndexEvent : IMessageEvent return; } - var mode = request.ReadString() ?? "normal"; + var mode = request.ReadString(); var categories = _catalogueManager.Pages.Values.Where( - p => p.Parent is null && p.Modes.Contains(mode) && p.MinRank <= gameSession.Habbo.Rank); + p => p.Parent is null && p.Modes.Contains(mode.ToLower()) && p.MinRank <= gameSession.Habbo.Rank); await gameSession.SendComposerAsync(new CatalogPagesListComposer(categories, mode)); } diff --git a/Communication/Messages/Incoming/Catalog/GetCatalogPageEvent.cs b/Communication/Messages/Incoming/Catalog/GetCatalogPageEvent.cs index 7e16944..6b794f2 100644 --- a/Communication/Messages/Incoming/Catalog/GetCatalogPageEvent.cs +++ b/Communication/Messages/Incoming/Catalog/GetCatalogPageEvent.cs @@ -26,12 +26,12 @@ public class GetCatalogPageEvent : IMessageEvent return; } - var pageId = request.ReadInt32() ?? -1; - var offerId = request.ReadInt32() ?? 0; - var mode = request.ReadString() ?? "normal"; + var pageId = request.ReadInt32(); + var offerId = request.ReadInt32(); + var mode = request.ReadString(); if (!_catalogueManager.Pages.TryGetValue(pageId, out var page) || !page.Enabled || - page.MinRank > gameSession.Habbo.Rank || !page.Modes.Contains(mode)) + page.MinRank > gameSession.Habbo.Rank || !page.Modes.Contains(mode.ToLower())) { return; } diff --git a/Communication/Messages/Incoming/Handshake/SsoTicketMessageEvent.cs b/Communication/Messages/Incoming/Handshake/SsoTicketMessageEvent.cs index 6c7e28f..634f4bd 100644 --- a/Communication/Messages/Incoming/Handshake/SsoTicketMessageEvent.cs +++ b/Communication/Messages/Incoming/Handshake/SsoTicketMessageEvent.cs @@ -32,15 +32,7 @@ public class SsoTicketMessageEvent : IMessageEvent public async Task HandleAsync(GameSession gameSession, ClientMessage request) { - var sso = request.ReadString(); - - if (sso == null) - { - await _gameSessionManager.CloseAsync("Malformed packet", gameSession); - return; - } - - var habbo = await _habboRepository.FindOneByAsync(h => h.SsoTicket == sso); + var habbo = await _habboRepository.FindOneByAsync(h => h.SsoTicket == request.ReadString()); if (habbo == null) { diff --git a/Communication/Messages/Incoming/Inventory/Badges/SetActivatedBadgesEvent.cs b/Communication/Messages/Incoming/Inventory/Badges/SetActivatedBadgesEvent.cs index ef063d2..07499da 100644 --- a/Communication/Messages/Incoming/Inventory/Badges/SetActivatedBadgesEvent.cs +++ b/Communication/Messages/Incoming/Inventory/Badges/SetActivatedBadgesEvent.cs @@ -42,13 +42,13 @@ public class SetActivatedBadgesEvent : IMessageEvent var slotId = request.ReadInt32(); var code = request.ReadString(); - if (slotId == null || slotId is < 1 or > 5 || string.IsNullOrEmpty(code)) continue; + if (slotId is < 1 or > 5 || string.IsNullOrEmpty(code)) continue; var badge = gameSession.Habbo.Badges.FirstOrDefault(b => b.Code == code); if (badge == null) continue; - badge.Slot = slotId.Value; + badge.Slot = slotId; wearingBadges.Add(badge); badgesToUpdate.TryAdd(badge.Id, badge); diff --git a/Communication/Messages/Incoming/Navigator/NavigatorSearchEvent.cs b/Communication/Messages/Incoming/Navigator/NavigatorSearchEvent.cs index c4b973d..63cc074 100644 --- a/Communication/Messages/Incoming/Navigator/NavigatorSearchEvent.cs +++ b/Communication/Messages/Incoming/Navigator/NavigatorSearchEvent.cs @@ -25,13 +25,9 @@ public class NavigatorSearchEvent : IMessageEvent await _gameSessionManager.CloseAsync("Not logged in", gameSession); return; } - - var viewName = request.ReadString(); - var query = request.ReadString(); - if (viewName is null || query is null || - !_navigatorManager.NavigatorViews.TryGetValue(viewName, out var navigatorView)) return; + if (!_navigatorManager.NavigatorViews.TryGetValue(request.ReadString(), out var navigatorView)) return; - await gameSession.SendComposerAsync(new NavigatorSearchComposer(navigatorView, query, gameSession.Habbo)); + await gameSession.SendComposerAsync(new NavigatorSearchComposer(navigatorView, request.ReadString(), gameSession.Habbo)); } } \ No newline at end of file diff --git a/Communication/Messages/Incoming/User/Data/UserCurrentBadgesEvent.cs b/Communication/Messages/Incoming/User/Data/UserCurrentBadgesEvent.cs index 7b2fdbf..e3c41e9 100644 --- a/Communication/Messages/Incoming/User/Data/UserCurrentBadgesEvent.cs +++ b/Communication/Messages/Incoming/User/Data/UserCurrentBadgesEvent.cs @@ -29,9 +29,7 @@ public class UserCurrentBadgesEvent : IMessageEvent var habboId = request.ReadInt32(); - if (habboId == null) return; - var badges = await _badgeRepository.FindByAsync(b => b.Habbo.Id == habboId && b.Slot > 0); - await gameSession.SendComposerAsync(new UserCurrentBadgesComposer(habboId.Value, badges.ToList())); + await gameSession.SendComposerAsync(new UserCurrentBadgesComposer(habboId, badges.ToList())); } } \ No newline at end of file diff --git a/Communication/Messages/Incoming/User/Data/UserFigureEvent.cs b/Communication/Messages/Incoming/User/Data/UserFigureEvent.cs index e71a64c..ddd7dc1 100644 --- a/Communication/Messages/Incoming/User/Data/UserFigureEvent.cs +++ b/Communication/Messages/Incoming/User/Data/UserFigureEvent.cs @@ -33,10 +33,10 @@ public class UserFigureEvent : IMessageEvent return; } - var gender = request.ReadString()?.ToUpper(); + var gender = request.ReadString().ToUpper(); var figure = request.ReadString(); - if (figure is null || (gender != "M" && gender != "F") || (figure == gameSession.Habbo.Figure && gender == gameSession.Habbo.Gender.ToUpper()) /*|| + if ((gender != "M" && gender != "F") || (figure == gameSession.Habbo.Figure && gender == gameSession.Habbo.Gender.ToUpper()) /*|| !_figureDataManager.ValidateFigure(figure, gender, gameSession.Habbo)*/) { return; diff --git a/Communication/Messages/Incoming/User/Data/UserProfileEvent.cs b/Communication/Messages/Incoming/User/Data/UserProfileEvent.cs index cc3b334..1c518c1 100644 --- a/Communication/Messages/Incoming/User/Data/UserProfileEvent.cs +++ b/Communication/Messages/Incoming/User/Data/UserProfileEvent.cs @@ -21,21 +21,9 @@ public class UserProfileEvent : IMessageEvent public IncomingHeaders Header => IncomingHeaders.UserProfile; public async Task HandleAsync(GameSession gameSession, ClientMessage request) { - var habboId = request.ReadInt32(); - - if (habboId == null) - { - await _gameSessionManager.CloseAsync("Malformed packet", gameSession); - return; - } + var habbo = await _habboRepository.FindAsync(request.ReadInt32()); - var habbo = await _habboRepository.FindAsync(habboId); - - if (habbo == null) - { - await _gameSessionManager.CloseAsync("Habbo not found", gameSession); - return; - } + if (habbo == null) return; await gameSession.SendComposerAsync(new UserProfileComposer(habbo)); } diff --git a/Communication/Messages/MessageHandler.cs b/Communication/Messages/MessageHandler.cs index c610a66..2f604ca 100644 --- a/Communication/Messages/MessageHandler.cs +++ b/Communication/Messages/MessageHandler.cs @@ -27,20 +27,20 @@ public class MessageHandler : IMessageHandler public async Task TryHandleAsync(GameSession session, ClientMessage request) { - if (request.Header == null) + if (request.Header == -1) { return; } - if (_messageEvents.TryGetValue(request.Header.Value, out var messageEvent)) + if (_messageEvents.TryGetValue(request.Header, out var messageEvent)) { - _logger.LogInformation("Handling header ID {Header} on class {Class}", request.Header.Value, messageEvent.GetType()); + _logger.LogInformation("Handling header ID {Header} on class {Class}", request.Header, messageEvent.GetType()); await messageEvent.HandleAsync(session, request); } else { - _logger.LogWarning("Unregistered header {Header} {Name}", request.Header.Value, _messageNames.TryGetValue(request.Header.Value, out var messageName) ? messageName : string.Empty); + _logger.LogWarning("Unregistered header {Header} {Name}", request.Header, _messageNames.TryGetValue(request.Header, out var messageName) ? messageName : string.Empty); } } } \ No newline at end of file diff --git a/Communication/Messages/Types/ClientMessage.cs b/Communication/Messages/Types/ClientMessage.cs index d37673e..0ec738e 100644 --- a/Communication/Messages/Types/ClientMessage.cs +++ b/Communication/Messages/Types/ClientMessage.cs @@ -8,7 +8,7 @@ public class ClientMessage private readonly byte[] _packet; private int _readerIndex; - public short? Header { get; } + public short Header { get; } public ClientMessage(byte[] packet) { @@ -17,42 +17,42 @@ public class ClientMessage Header = ReadInt16(); } - public short? ReadInt16() + public short ReadInt16() { var raw = ReadBytes(2); - if (raw.Length != 2) return null; + if (raw.Length != 2) return -1; return ByteUtils.GetInt16(raw); } - public int? ReadInt32() + public int ReadInt32() { var raw = ReadBytes(4); - if (raw.Length != 4) return null; + if (raw.Length != 4) return -1; return ByteUtils.GetInt32(raw); } - public bool? ReadBoolean() + public bool ReadBoolean() { - if (_packet.Length - _readerIndex == 0) return null; + if (_packet.Length - _readerIndex == 0) return false; var result = _packet[_readerIndex]; _readerIndex++; return result == 1; } - public string? ReadString() + public string ReadString() { var strlen = ReadInt16(); - if (strlen == null) return null; + if (strlen == -1) return string.Empty; - var raw = ReadBytes(strlen.Value); + var raw = ReadBytes(strlen); - return raw.Length != strlen ? null : Encoding.UTF8.GetString(raw); + return raw.Length != strlen ? string.Empty : Encoding.UTF8.GetString(raw); } private byte[] ReadBytes(int amount)