Changed packet reading operations to never return null

main
Tiger 2023-10-20 21:02:11 +02:00
parent 77f3b6a983
commit b1c3bfd2b6
10 changed files with 31 additions and 57 deletions

View File

@ -26,11 +26,11 @@ public class GetCatalogIndexEvent : IMessageEvent
return; return;
} }
var mode = request.ReadString() ?? "normal"; var mode = request.ReadString();
var categories = var categories =
_catalogueManager.Pages.Values.Where( _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)); await gameSession.SendComposerAsync(new CatalogPagesListComposer(categories, mode));
} }

View File

@ -26,12 +26,12 @@ public class GetCatalogPageEvent : IMessageEvent
return; return;
} }
var pageId = request.ReadInt32() ?? -1; var pageId = request.ReadInt32();
var offerId = request.ReadInt32() ?? 0; var offerId = request.ReadInt32();
var mode = request.ReadString() ?? "normal"; var mode = request.ReadString();
if (!_catalogueManager.Pages.TryGetValue(pageId, out var page) || !page.Enabled || 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; return;
} }

View File

@ -32,15 +32,7 @@ public class SsoTicketMessageEvent : IMessageEvent
public async Task HandleAsync(GameSession gameSession, ClientMessage request) public async Task HandleAsync(GameSession gameSession, ClientMessage request)
{ {
var sso = request.ReadString(); var habbo = await _habboRepository.FindOneByAsync(h => h.SsoTicket == request.ReadString());
if (sso == null)
{
await _gameSessionManager.CloseAsync("Malformed packet", gameSession);
return;
}
var habbo = await _habboRepository.FindOneByAsync(h => h.SsoTicket == sso);
if (habbo == null) if (habbo == null)
{ {

View File

@ -42,13 +42,13 @@ public class SetActivatedBadgesEvent : IMessageEvent
var slotId = request.ReadInt32(); var slotId = request.ReadInt32();
var code = request.ReadString(); 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); var badge = gameSession.Habbo.Badges.FirstOrDefault(b => b.Code == code);
if (badge == null) continue; if (badge == null) continue;
badge.Slot = slotId.Value; badge.Slot = slotId;
wearingBadges.Add(badge); wearingBadges.Add(badge);
badgesToUpdate.TryAdd(badge.Id, badge); badgesToUpdate.TryAdd(badge.Id, badge);

View File

@ -26,12 +26,8 @@ public class NavigatorSearchEvent : IMessageEvent
return; return;
} }
var viewName = request.ReadString(); if (!_navigatorManager.NavigatorViews.TryGetValue(request.ReadString(), out var navigatorView)) return;
var query = request.ReadString();
if (viewName is null || query is null || await gameSession.SendComposerAsync(new NavigatorSearchComposer(navigatorView, request.ReadString(), gameSession.Habbo));
!_navigatorManager.NavigatorViews.TryGetValue(viewName, out var navigatorView)) return;
await gameSession.SendComposerAsync(new NavigatorSearchComposer(navigatorView, query, gameSession.Habbo));
} }
} }

View File

@ -29,9 +29,7 @@ public class UserCurrentBadgesEvent : IMessageEvent
var habboId = request.ReadInt32(); var habboId = request.ReadInt32();
if (habboId == null) return;
var badges = await _badgeRepository.FindByAsync(b => b.Habbo.Id == habboId && b.Slot > 0); 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()));
} }
} }

View File

@ -33,10 +33,10 @@ public class UserFigureEvent : IMessageEvent
return; return;
} }
var gender = request.ReadString()?.ToUpper(); var gender = request.ReadString().ToUpper();
var figure = request.ReadString(); 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)*/) !_figureDataManager.ValidateFigure(figure, gender, gameSession.Habbo)*/)
{ {
return; return;

View File

@ -21,21 +21,9 @@ public class UserProfileEvent : IMessageEvent
public IncomingHeaders Header => IncomingHeaders.UserProfile; public IncomingHeaders Header => IncomingHeaders.UserProfile;
public async Task HandleAsync(GameSession gameSession, ClientMessage request) public async Task HandleAsync(GameSession gameSession, ClientMessage request)
{ {
var habboId = request.ReadInt32(); var habbo = await _habboRepository.FindAsync(request.ReadInt32());
if (habboId == null) if (habbo == null) return;
{
await _gameSessionManager.CloseAsync("Malformed packet", gameSession);
return;
}
var habbo = await _habboRepository.FindAsync(habboId);
if (habbo == null)
{
await _gameSessionManager.CloseAsync("Habbo not found", gameSession);
return;
}
await gameSession.SendComposerAsync(new UserProfileComposer(habbo)); await gameSession.SendComposerAsync(new UserProfileComposer(habbo));
} }

View File

@ -27,20 +27,20 @@ public class MessageHandler : IMessageHandler
public async Task TryHandleAsync(GameSession session, ClientMessage request) public async Task TryHandleAsync(GameSession session, ClientMessage request)
{ {
if (request.Header == null) if (request.Header == -1)
{ {
return; 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); await messageEvent.HandleAsync(session, request);
} }
else 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);
} }
} }
} }

View File

@ -8,7 +8,7 @@ public class ClientMessage
private readonly byte[] _packet; private readonly byte[] _packet;
private int _readerIndex; private int _readerIndex;
public short? Header { get; } public short Header { get; }
public ClientMessage(byte[] packet) public ClientMessage(byte[] packet)
{ {
@ -17,42 +17,42 @@ public class ClientMessage
Header = ReadInt16(); Header = ReadInt16();
} }
public short? ReadInt16() public short ReadInt16()
{ {
var raw = ReadBytes(2); var raw = ReadBytes(2);
if (raw.Length != 2) return null; if (raw.Length != 2) return -1;
return ByteUtils.GetInt16(raw); return ByteUtils.GetInt16(raw);
} }
public int? ReadInt32() public int ReadInt32()
{ {
var raw = ReadBytes(4); var raw = ReadBytes(4);
if (raw.Length != 4) return null; if (raw.Length != 4) return -1;
return ByteUtils.GetInt32(raw); 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]; var result = _packet[_readerIndex];
_readerIndex++; _readerIndex++;
return result == 1; return result == 1;
} }
public string? ReadString() public string ReadString()
{ {
var strlen = ReadInt16(); 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) private byte[] ReadBytes(int amount)