Skip to content

Commit

Permalink
Empty scene server crash (#100)
Browse files Browse the repository at this point in the history
* Check for null or empty scene on client enter scene

* Set defaults for server player data
  • Loading branch information
Extremelyd1 authored Jan 13, 2024
1 parent 2b685e3 commit 21282ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 13 additions & 1 deletion HKMP/Game/Server/ServerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,12 @@ private void OnHelloServer(ushort id, HelloServer helloServer) {
return;
}

playerData.CurrentScene = helloServer.SceneName;
// Specifically set the position, scale and animation before current scene so that when we check if current
// scene exists, we have all other data set
playerData.Position = helloServer.Position;
playerData.Scale = helloServer.Scale;
playerData.AnimationId = helloServer.AnimationClipId;
playerData.CurrentScene = helloServer.SceneName;

var clientInfo = new List<(ushort, string)>();

Expand Down Expand Up @@ -321,6 +323,16 @@ private void OnClientEnterScene(ServerPlayerData playerData) {
var enterSceneList = new List<ClientPlayerEnterScene>();
var alreadyPlayersInScene = false;

// Edge case where the scene of the player is empty (uninitialized) and we don't want to match with other
// uninitialized players. Otherwise, it causes issues where other parts of the player data for other players
// could be null and result in NREs further down the line
if (string.IsNullOrEmpty(playerData.CurrentScene)) {
_netServer.GetUpdateManagerForClient(playerData.Id)?.AddPlayerAlreadyInSceneData(
enterSceneList,
true
);
}

foreach (var idPlayerDataPair in _playerData) {
// Skip source player
if (idPlayerDataPair.Key == playerData.Id) {
Expand Down
4 changes: 2 additions & 2 deletions HKMP/Game/Server/ServerPlayerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal class ServerPlayerData : IServerPlayer {
public string CurrentScene { get; set; }

/// <inheritdoc />
public Vector2 Position { get; set; }
public Vector2 Position { get; set; } = Vector2.Zero;

/// <inheritdoc />
public bool Scale { get; set; }
Expand All @@ -34,7 +34,7 @@ internal class ServerPlayerData : IServerPlayer {
public bool HasMapIcon { get; set; }

/// <inheritdoc />
public Vector2 MapPosition { get; set; }
public Vector2 MapPosition { get; set; } = Vector2.Zero;

/// <inheritdoc />
public ushort AnimationId { get; set; }
Expand Down

0 comments on commit 21282ba

Please sign in to comment.