Files
GoakeFlax/Source/Game/Network/NetworkManager.cs
2022-05-02 20:34:42 +03:00

94 lines
2.4 KiB
C#

using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
using FlaxEditor;
using FlaxEngine;
using FlaxEngine.Networking;
using Console = Cabrito.Console;
using Debug = FlaxEngine.Debug;
namespace Game
{
[AttributeUsage(AttributeTargets.Class)]
public class NetworkPredictedAttribute : Attribute
{
public NetworkPredictedAttribute()
{
}
}
// TODO: insert code to update variables with this attribute?
// rename to NetworkReplicatedAttribute?
[AttributeUsage(AttributeTargets.Class)]
public class NetworkedAttribute : Attribute
{
public NetworkedAttribute()
{
}
}
// NetworkMulticastAttribute: calls methods marked with this in all clients
public static partial class NetworkManager
{
private static bool initialized = false;
private static NetworkPeer server;
private static NetworkPeer client;
private static ushort ServerPort = 59183;
private static string ServerAddress = null;
private static ushort MTU = 1500;
private static ushort MaximumClients = 32;
public static uint LocalPlayerClientId { get; private set; } = 0;
public static void Init()
{
if (initialized)
return;
if (Engine.CommandLine.Contains("-server"))
{
StartServer();
ServerAddress = "localhost";
ConnectServer();
}
else if (Engine.CommandLine.Contains("-client"))
{
ServerAddress = "localhost";
ConnectServer();
}
#if FLAX_EDITOR
else
{
StartServer();
}
#endif
initialized = true;
}
public static void Deinitialize()
{
if (server != null)
{
Scripting.FixedUpdate -= OnServerUpdate;
Scripting.Exit -= Deinitialize;
Level.ActorSpawned -= OnServerActorSpawned;
NetworkPeer.ShutdownPeer(server);
server = null;
}
if (client != null)
{
Scripting.FixedUpdate -= OnClientUpdate;
Scripting.Exit -= Deinitialize;
Level.ActorSpawned -= OnClientActorSpawned;
NetworkPeer.ShutdownPeer(client);
client = null;
}
}
}
}