Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Extremelyd1 committed Dec 22, 2024
2 parents 73cd856 + 8e02a1c commit 041b336
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 35 deletions.
14 changes: 13 additions & 1 deletion HKMP/Game/Server/ServerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,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 @@ -326,6 +328,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
2 changes: 1 addition & 1 deletion HKMP/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ internal static class Version {
/// <summary>
/// The version as a string.
/// </summary>
public const string String = "2.4.1";
public const string String = "2.4.2";
}
36 changes: 5 additions & 31 deletions HKMPServer/HkmpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public void Initialize(string[] args) {
return;
}

if (string.IsNullOrEmpty(args[0]) || !ParsePort(args[0], out var port)) {
var portArg = args[0];

if (string.IsNullOrEmpty(portArg) || !ushort.TryParse(portArg, out var port)) {
Logger.Info("Invalid port, should be an integer between 0 and 65535");
return;
}
Expand All @@ -47,7 +49,7 @@ public void Initialize(string[] args) {
/// <param name="consoleInputManager">The input manager for command-line input.</param>
/// <param name="consoleLogger">The logging class for logging to console.</param>
private void StartServer(
int port,
ushort port,
ServerSettings serverSettings,
ConsoleInputManager consoleInputManager,
ConsoleLogger consoleLogger
Expand All @@ -60,7 +62,7 @@ ConsoleLogger consoleLogger

var serverManager = new ConsoleServerManager(netServer, serverSettings, packetManager, consoleLogger);
serverManager.Initialize();
serverManager.Start(port);
serverManager.Start((int)port);

// TODO: make an event in ServerManager that we can register for so we know when the server shuts down
consoleInputManager.ConsoleInputEvent += input => {
Expand All @@ -71,33 +73,5 @@ ConsoleLogger consoleLogger
};
consoleInputManager.Start();
}

/// <summary>
/// Try to parse the given input as a networking port.
/// </summary>
/// <param name="input">The string to parse.</param>
/// <param name="port">Will be set to the parsed port if this method returns true, or 0 if the method
/// returns false.</param>
/// <returns>True if the given input was parsed as a valid port, false otherwise.</returns>
private static bool ParsePort(string input, out int port) {
if (!int.TryParse(input, out port)) {
return false;
}

if (!IsValidPort(port)) {
return false;
}

return true;
}

/// <summary>
/// Returns true if the given port is a valid networking port.
/// </summary>
/// <param name="port">The port to check.</param>
/// <returns>True if the port is valid, false otherwise.</returns>
private static bool IsValidPort(int port) {
return port >= 0 && port <= 65535;
}
}
}

0 comments on commit 041b336

Please sign in to comment.