Fix creating and deleting character slots crashing the client and server (#2172)
* Fix creating a character slot crashing the client * a * Fix deleting character profiles
This commit is contained in:
@@ -42,6 +42,8 @@ namespace Content.Server.Preferences
|
||||
HandleSelectCharacterMessage);
|
||||
_netManager.RegisterNetMessage<MsgUpdateCharacter>(nameof(MsgUpdateCharacter),
|
||||
HandleUpdateCharacterMessage);
|
||||
_netManager.RegisterNetMessage<MsgDeleteCharacter>(nameof(MsgDeleteCharacter),
|
||||
HandleDeleteCharacterMessage);
|
||||
}
|
||||
|
||||
|
||||
@@ -77,6 +79,13 @@ namespace Content.Server.Preferences
|
||||
var profile = message.Profile;
|
||||
var userId = message.MsgChannel.UserId;
|
||||
|
||||
if (profile == null)
|
||||
{
|
||||
Logger.WarningS("prefs",
|
||||
$"User {userId} sent a {nameof(MsgUpdateCharacter)} with a null profile in slot {slot}.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_cachedPlayerPrefs.TryGetValue(userId, out var prefsData) || !prefsData.PrefsLoaded.IsCompleted)
|
||||
{
|
||||
Logger.WarningS("prefs", $"User {userId} tried to modify preferences before they loaded.");
|
||||
@@ -103,6 +112,35 @@ namespace Content.Server.Preferences
|
||||
}
|
||||
}
|
||||
|
||||
private async void HandleDeleteCharacterMessage(MsgDeleteCharacter message)
|
||||
{
|
||||
var slot = message.Slot;
|
||||
var userId = message.MsgChannel.UserId;
|
||||
|
||||
if (!_cachedPlayerPrefs.TryGetValue(userId, out var prefsData) || !prefsData.PrefsLoaded.IsCompleted)
|
||||
{
|
||||
Logger.WarningS("prefs", $"User {userId} tried to modify preferences before they loaded.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (slot < 0 || slot >= MaxCharacterSlots)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var curPrefs = prefsData.Prefs!;
|
||||
|
||||
var arr = new ICharacterProfile[MaxCharacterSlots];
|
||||
curPrefs.Characters.Where((profile, index) => index != slot).ToArray().CopyTo(arr, 0);
|
||||
|
||||
prefsData.Prefs = new PlayerPreferences(arr, slot);
|
||||
|
||||
if (ShouldStorePrefs(message.MsgChannel.AuthType))
|
||||
{
|
||||
await _db.SaveCharacterSlotAsync(message.MsgChannel.UserId, null, message.Slot);
|
||||
}
|
||||
}
|
||||
|
||||
public async void OnClientConnected(IPlayerSession session)
|
||||
{
|
||||
if (!ShouldStorePrefs(session.ConnectedClient.AuthType))
|
||||
|
||||
Reference in New Issue
Block a user