gamemode stub

This commit is contained in:
2022-05-05 17:00:45 +03:00
parent 3f1230fdf9
commit 8762138fe3
6 changed files with 99 additions and 37 deletions

View File

@@ -0,0 +1,7 @@
namespace Game
{
public abstract class GameMode
{
}
}

View File

@@ -0,0 +1,19 @@
using Cabrito;
using FlaxEngine.Networking;
namespace Game
{
public static class GameModeManager
{
public static void Init()
{
NetworkManager.OnMessage += OnClientConnected;
}
public static bool OnClientConnected(NetworkMessage message)
{
Console.Print("client connected");
return true;
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using FlaxEditor;
@@ -30,6 +31,14 @@ namespace Game
// NetworkMulticastAttribute: calls methods marked with this in all clients
public enum NetworkMessageType : byte
{
Handshake = 1,
Message,
}
public static partial class NetworkManager
{
private static bool initialized = false;
@@ -42,7 +51,8 @@ namespace Game
private static ushort MTU = 1500;
private static ushort MaximumClients = 32;
public static uint LocalPlayerClientId { get; private set; } = 0;
public delegate bool OnMessageDecl(NetworkMessage message);
public static OnMessageDecl OnMessage;
public static void Init()
{
@@ -68,6 +78,7 @@ namespace Game
#endif
initialized = true;
GameModeManager.Init(); // FIXME
}
public static void Deinitialize()
@@ -90,5 +101,44 @@ namespace Game
client = null;
}
}
private static void OnNetworkMessage(NetworkEvent networkEvent)
{
byte messageTypeByte = networkEvent.Message.ReadByte();
if (!Enum.IsDefined(typeof(NetworkMessageType), messageTypeByte))
{
Console.PrintError($"Unsupported message type received from client: {messageTypeByte}");
return;
}
NetworkMessageType messageType = (NetworkMessageType)messageTypeByte;
switch (messageType)
{
case NetworkMessageType.Handshake:
{
var message = networkEvent.Message.ReadString();
Console.Print($"Received handshake from {networkEvent.Sender.ConnectionId}, msg: " + message);
break;
}
case NetworkMessageType.Message:
{
if (OnMessage != null)
{
foreach (OnMessageDecl func in OnMessage.GetInvocationList()
.Cast<OnMessageDecl>().ToArray())
{
bool ret = func.Invoke(networkEvent.Message);
if (ret)
break;
}
}
break;
}
default:
Console.PrintError($"Unsupported message type received from client: {messageTypeByte}");
break;
}
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using FlaxEngine;
@@ -11,6 +12,8 @@ namespace Game
{
public static partial class NetworkManager
{
public static uint LocalPlayerClientId { get; private set; } = 0;
public static bool ConnectServer()
{
client = NetworkPeer.CreatePeer(new NetworkConfig
@@ -38,14 +41,14 @@ namespace Game
{
using var _ = Utilities.ProfileScope("NetworkManager_OnClientUpdate");
while (client.PopEvent(out NetworkEvent eventData))
while (client.PopEvent(out NetworkEvent networkEvent))
{
switch (eventData.EventType)
switch (networkEvent.EventType)
{
case NetworkEventType.Connected:
{
LocalPlayerClientId = eventData.Sender.ConnectionId;
Console.Print("Connected to server, ConnectionId: " + eventData.Sender.ConnectionId);
LocalPlayerClientId = networkEvent.Sender.ConnectionId;
Console.Print("Connected to server, ConnectionId: " + networkEvent.Sender.ConnectionId);
break;
}
case NetworkEventType.Disconnected:
@@ -62,21 +65,8 @@ namespace Game
}
case NetworkEventType.Message:
{
// Read the message contents
var message = eventData.Message;
var messageData = message.ReadString();
Console.Print($"Received message from Client({eventData.Sender.ConnectionId}): {messageData}");
// Send hello message to the client back
{
var sendmessage = client.BeginSendMessage();
sendmessage.WriteString($"Hello, Server({eventData.Sender.ConnectionId})!");
client.EndSendMessage(NetworkChannelType.Reliable, sendmessage);
}
client.RecycleMessage(message);
OnNetworkMessage(networkEvent);
client.RecycleMessage(networkEvent.Message);
break;
}
default:

View File

@@ -81,43 +81,39 @@ namespace Game
{
using var _ = Utilities.ProfileScope("NetworkManager_OnServerUpdate");
while (server.PopEvent(out NetworkEvent eventData))
while (server.PopEvent(out NetworkEvent networkEvent))
{
switch (eventData.EventType)
switch (networkEvent.EventType)
{
case NetworkEventType.Connected:
{
Console.Print($"Client({eventData.Sender.ConnectionId}) connected!");
Console.Print($"Client({networkEvent.Sender.ConnectionId}) connected!");
ConnectedClients.Add(eventData.Sender);
ConnectedClients.Add(networkEvent.Sender);
Console.Print("Connected clients: " + ConnectedClients.Count);
// Send hello message to the client back
{
var sendmessage = server.BeginSendMessage();
sendmessage.WriteString($"Welcome, ({eventData.Sender.ConnectionId})");
server.EndSendMessage(NetworkChannelType.Reliable, sendmessage, eventData.Sender);
NetworkMessage sendmessage = server.BeginSendMessage();
sendmessage.WriteByte((byte)NetworkMessageType.Message);
//sendmessage.WriteString($"Welcome, ({networkEvent.Sender.ConnectionId})");
server.EndSendMessage(NetworkChannelType.Reliable, sendmessage, networkEvent.Sender);
}
break;
}
case NetworkEventType.Disconnected:
case NetworkEventType.Timeout:
{
Console.Print($"Client({eventData.Sender.ConnectionId}) disconnected!");
Console.Print($"Client({networkEvent.Sender.ConnectionId}) disconnected!");
ConnectedClients.Remove(eventData.Sender);
ConnectedClients.Remove(networkEvent.Sender);
Console.Print("Connected clients: " + ConnectedClients.Count);
break;
}
case NetworkEventType.Message:
{
// Read the message contents
var message = eventData.Message;
var messageData = message.ReadString();
Console.Print($"Received message from Client({eventData.Sender.ConnectionId}): {messageData}");
server.RecycleMessage(message);
OnNetworkMessage(networkEvent);
server.RecycleMessage(networkEvent.Message);
break;
}
default:

View File

@@ -29,7 +29,7 @@ public class GameTarget : GameProjectTarget
public override string GetOutputFilePath(BuildOptions options, TargetOutputType? outputType = null)
{
// For IDE builds only, these do not work during cooking
// For IDE builds only, these do not work during cooking (fails to update cooked files in multiple ways)
if (options.Configuration == TargetConfiguration.Development)
options.OutputFolder = @"C:\dev\GoakeFlax\Output\WindowsDevelopment";
else if (options.Configuration == TargetConfiguration.Release)