optimizing network stuff
This commit is contained in:
@@ -9,8 +9,9 @@ using RebootKit.Engine.Foundation;
|
||||
using RebootKit.Engine.Input;
|
||||
using RebootKit.Engine.Network;
|
||||
using RebootKit.Engine.Simulation;
|
||||
#if RR_STEAM
|
||||
using RebootKit.Engine.Steam;
|
||||
using Unity.Netcode;
|
||||
#endif
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
@@ -28,7 +29,7 @@ namespace RebootKit.Engine.Main {
|
||||
|
||||
[ConfigVar("con.write_log", 1, "Enables writing game log to console output")]
|
||||
static ConfigVar s_WriteLogToConsole;
|
||||
|
||||
|
||||
internal static EngineConfigAsset EngineConfig;
|
||||
|
||||
static DisposableBag s_DisposableBag;
|
||||
@@ -36,8 +37,13 @@ namespace RebootKit.Engine.Main {
|
||||
|
||||
static AsyncOperationHandle<SceneInstance> s_MainMenuSceneHandle;
|
||||
|
||||
static NetworkSystem s_NetworkSystemPrefab;
|
||||
internal static NetworkSystem NetworkSystemInstance;
|
||||
internal static NetworkSystem Network;
|
||||
internal static byte NetworkProtocolVersion {
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get {
|
||||
return EngineConfig.protocolVersion;
|
||||
}
|
||||
}
|
||||
|
||||
internal static ConsoleService Console { get; private set; }
|
||||
public static InputService Input { get; private set; }
|
||||
@@ -65,30 +71,18 @@ namespace RebootKit.Engine.Main {
|
||||
s_ServicesBag.Add(Input);
|
||||
World = CreateService<WorldService>();
|
||||
|
||||
await InitializeAssetsAsync(cancellationToken);
|
||||
|
||||
#if RR_STEAM
|
||||
await SteamManager.InitializeAsync(cancellationToken);
|
||||
Network = new NetworkSystem(new SteamNetworkManager());
|
||||
#else
|
||||
Network = new NetworkSystem(new UnityNetworkManager());
|
||||
#endif
|
||||
|
||||
await InitializeAssetsAsync(cancellationToken);
|
||||
}
|
||||
|
||||
// @NOTE: This method is called after the main scene is loaded.
|
||||
internal static async UniTask RunAsync(CancellationToken cancellationToken) {
|
||||
s_NetworkSystemPrefab =
|
||||
Resources.Load<NetworkSystem>(RConsts.k_CoreNetworkGameSystemsResourcesPath);
|
||||
|
||||
NetworkManager.Singleton.OnConnectionEvent += OnConnectionEvent;
|
||||
NetworkManager.Singleton.OnServerStarted += OnServerStarted;
|
||||
NetworkManager.Singleton.OnServerStopped += OnServerStopped;
|
||||
|
||||
#if RR_STEAM
|
||||
if (NetworkManager.Singleton.TryGetComponent(out FacepunchTransport facepunchTransport)) {
|
||||
NetworkManager.Singleton.NetworkConfig.NetworkTransport = facepunchTransport;
|
||||
} else {
|
||||
s_Logger.Error("Steam integration is enabled but FacepunchTransport is not found in NetworkManager.");
|
||||
}
|
||||
#endif
|
||||
|
||||
Observable.EveryUpdate()
|
||||
.Subscribe(_ => Tick())
|
||||
.AddTo(ref s_DisposableBag);
|
||||
@@ -112,16 +106,12 @@ namespace RebootKit.Engine.Main {
|
||||
s_Logger.Info("Shutting down");
|
||||
|
||||
if (GameInstance is not null) {
|
||||
GameInstance.NetworkObject.Despawn();
|
||||
Object.Destroy(GameInstance);
|
||||
GameInstance = null;
|
||||
}
|
||||
|
||||
if (NetworkManager.Singleton is not null) {
|
||||
NetworkManager.Singleton.OnConnectionEvent -= OnConnectionEvent;
|
||||
NetworkManager.Singleton.OnServerStarted -= OnServerStarted;
|
||||
NetworkManager.Singleton.OnServerStopped -= OnServerStopped;
|
||||
}
|
||||
Network.Dispose();
|
||||
Network = null;
|
||||
|
||||
#if RR_STEAM
|
||||
SteamManager.Shutdown();
|
||||
@@ -159,8 +149,10 @@ namespace RebootKit.Engine.Main {
|
||||
return worldConfig;
|
||||
}
|
||||
|
||||
// Game API
|
||||
public static async UniTask OpenMainMenuAsync(CancellationToken cancellationToken) {
|
||||
//
|
||||
// @MARK: Game
|
||||
//
|
||||
internal static async UniTask OpenMainMenuAsync(CancellationToken cancellationToken) {
|
||||
s_Logger.Info("Opening main menu");
|
||||
|
||||
World.Unload();
|
||||
@@ -201,7 +193,7 @@ namespace RebootKit.Engine.Main {
|
||||
return;
|
||||
}
|
||||
|
||||
NetworkSystemInstance.SetCurrentWorld(worldID);
|
||||
Network.SetCurrentWorld(worldID);
|
||||
}
|
||||
|
||||
public static Actor SpawnActor(AssetReferenceGameObject assetReference,
|
||||
@@ -212,7 +204,7 @@ namespace RebootKit.Engine.Main {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (NetworkSystemInstance is null) {
|
||||
if (Network is null) {
|
||||
s_Logger.Error("NetworkSystemInstance is not initialized. Cannot spawn actor.");
|
||||
return null;
|
||||
}
|
||||
@@ -223,16 +215,16 @@ namespace RebootKit.Engine.Main {
|
||||
}
|
||||
|
||||
s_Logger.Info($"Spawning actor from asset reference: {assetReference.RuntimeKey}");
|
||||
return NetworkSystemInstance.Actors.SpawnActor(assetReference, position, rotation);
|
||||
return Network.Actors.SpawnActor(assetReference, position, rotation);
|
||||
}
|
||||
|
||||
public static Actor FindSpawnedActor(ulong actorID) {
|
||||
if (NetworkSystemInstance is null) {
|
||||
public static Actor FindSpawnedActor(ushort actorID) {
|
||||
if (Network is null) {
|
||||
s_Logger.Error("NetworkSystemInstance is not initialized. Cannot find actor.");
|
||||
return null;
|
||||
}
|
||||
|
||||
Actor actor = NetworkSystemInstance.Actors.FindActorByID(actorID);
|
||||
Actor actor = Network.Actors.FindActorByID(actorID);
|
||||
if (actor is null) {
|
||||
s_Logger.Error($"Actor with ID {actorID} not found");
|
||||
}
|
||||
@@ -240,7 +232,24 @@ namespace RebootKit.Engine.Main {
|
||||
return actor;
|
||||
}
|
||||
|
||||
// Service API
|
||||
public static void PossessActor(ulong clientID, ushort actorID) {
|
||||
if (!IsServer()) {
|
||||
s_Logger.Error("Only server can possess actors for clients.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (Network == null) {
|
||||
s_Logger.Error("Network is not initialized. Cannot possess actor.");
|
||||
return;
|
||||
}
|
||||
|
||||
Network.SendPossessedActor(clientID, actorID);
|
||||
}
|
||||
|
||||
//
|
||||
// @MARK: Service API
|
||||
// Services seems to be useless in the current architecture. Consider removing this API in the future.
|
||||
//
|
||||
public static TService CreateService<TService>(ServiceAsset<TService> asset) where TService : class, IService {
|
||||
if (asset is null) {
|
||||
throw new ArgumentNullException($"Null asset of type {typeof(TService)}");
|
||||
@@ -257,7 +266,9 @@ namespace RebootKit.Engine.Main {
|
||||
return service;
|
||||
}
|
||||
|
||||
// Logging API
|
||||
//
|
||||
// @MARK: Logging API
|
||||
//
|
||||
public static void Log(string message) {
|
||||
Debug.Log(message);
|
||||
Console?.WriteToOutput(message);
|
||||
@@ -277,7 +288,9 @@ namespace RebootKit.Engine.Main {
|
||||
Console?.WriteToOutput(message);
|
||||
}
|
||||
|
||||
// CVar API
|
||||
//
|
||||
// @MARK: Config Variables
|
||||
//
|
||||
public static ConfigVar CVarIndex(string name, int defaultValue = -1) {
|
||||
ConfigVar cvar = ConfigVarsContainer.Get(name);
|
||||
if (cvar != null) {
|
||||
@@ -311,49 +324,57 @@ namespace RebootKit.Engine.Main {
|
||||
return cvar;
|
||||
}
|
||||
|
||||
// Network API
|
||||
//
|
||||
// @MARK: Network API
|
||||
//
|
||||
public static bool IsServer() {
|
||||
return NetworkManager.Singleton.IsServer;
|
||||
return Network != null && Network.Manager.IsServer();
|
||||
}
|
||||
|
||||
public static bool IsClient() {
|
||||
return NetworkManager.Singleton.IsClient;
|
||||
return Network != null && Network.Manager.IsClient();
|
||||
}
|
||||
|
||||
public static void StartHost() {
|
||||
if (NetworkManager.Singleton.IsHost) {
|
||||
s_Logger.Error("Already hosting a server");
|
||||
if (IsServer() || IsClient()) {
|
||||
s_Logger.Error("Already hosting a server or connected as a client");
|
||||
return;
|
||||
}
|
||||
|
||||
// @TODO: Handle failures
|
||||
|
||||
s_Logger.Info("Starting host");
|
||||
NetworkManager.Singleton.StartHost();
|
||||
if (!Network.Manager.StartHost()) {
|
||||
s_Logger.Error("Failed to start host.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public static void StopServer() { }
|
||||
public static void StopHost() {
|
||||
Network.Manager.StopHost();
|
||||
}
|
||||
|
||||
public static void Connect() {
|
||||
if (NetworkManager.Singleton.IsClient) {
|
||||
if (IsClient()) {
|
||||
s_Logger.Error("Already connected to a server");
|
||||
return;
|
||||
}
|
||||
|
||||
s_Logger.Info($"Connecting to server.");
|
||||
NetworkManager.Singleton.StartClient();
|
||||
s_Logger.Info("Connecting to server.");
|
||||
Network.Manager.StartClient();
|
||||
}
|
||||
|
||||
public static void ConnectWithSteamID(ulong steamId) {
|
||||
#if RR_STEAM
|
||||
|
||||
if (NetworkManager.Singleton.IsClient) {
|
||||
if (IsClient()) {
|
||||
s_Logger.Error("Already connected to a server");
|
||||
return;
|
||||
}
|
||||
|
||||
s_Logger.Info($"Connecting to server with Steam ID: {steamId}");
|
||||
if (NetworkManager.Singleton.NetworkConfig.NetworkTransport is FacepunchTransport facepunchTransport) {
|
||||
facepunchTransport.targetSteamId = steamId;
|
||||
NetworkManager.Singleton.StartClient();
|
||||
if (Network.Manager is SteamNetworkManager steamNetworkManager) {
|
||||
steamNetworkManager.TargetSteamID = steamId;
|
||||
Network.Manager.StartClient();
|
||||
} else {
|
||||
s_Logger.Error("Network transport is not FacepunchTransport. Cannot connect with Steam ID.");
|
||||
}
|
||||
@@ -362,7 +383,9 @@ namespace RebootKit.Engine.Main {
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void Disconnect() { }
|
||||
public static void Disconnect() {
|
||||
Network.Manager.Disconnect();
|
||||
}
|
||||
|
||||
public static void SendChatMessage(string message) {
|
||||
if (!IsClient()) {
|
||||
@@ -374,40 +397,48 @@ namespace RebootKit.Engine.Main {
|
||||
return;
|
||||
}
|
||||
|
||||
GameInstance.SendChatMessageRpc(message);
|
||||
throw new NotSupportedException("Cannot send chat message. Not connected to a server.");
|
||||
}
|
||||
|
||||
|
||||
static void Tick() {
|
||||
float deltaTime = Time.deltaTime;
|
||||
Network.Tick(deltaTime);
|
||||
}
|
||||
|
||||
static void OnConnectionEvent(NetworkManager network, ConnectionEventData data) {
|
||||
s_Logger.Info("Connection event: " + data.EventType);
|
||||
}
|
||||
|
||||
static void OnServerStarted() {
|
||||
internal static void OnServerStarted() {
|
||||
s_Logger.Info("Server started");
|
||||
|
||||
GameInstance = Object.Instantiate(EngineConfig.gamePrefab);
|
||||
GameInstance.NetworkObject.Spawn();
|
||||
|
||||
NetworkSystemInstance = Object.Instantiate(s_NetworkSystemPrefab);
|
||||
NetworkSystemInstance.NetworkObject.Spawn();
|
||||
}
|
||||
|
||||
static void OnServerStopped(bool obj) {
|
||||
internal static void OnServerStopped() {
|
||||
s_Logger.Info("Server stopped");
|
||||
|
||||
if (GameInstance is not null) {
|
||||
GameInstance.NetworkObject.Despawn();
|
||||
Object.Destroy(GameInstance.gameObject);
|
||||
GameInstance = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (NetworkSystemInstance is not null) {
|
||||
if (NetworkSystemInstance.NetworkObject is not null && NetworkSystemInstance.NetworkObject.IsSpawned) {
|
||||
NetworkSystemInstance.NetworkObject.Despawn();
|
||||
}
|
||||
internal static void OnClientStarted() {
|
||||
if (IsServer()) {
|
||||
return;
|
||||
}
|
||||
|
||||
NetworkSystemInstance = null;
|
||||
GameInstance = Object.Instantiate(EngineConfig.gamePrefab);
|
||||
}
|
||||
|
||||
internal static void OnClientStopped() {
|
||||
if (IsServer()) {
|
||||
return;
|
||||
}
|
||||
|
||||
World.Unload();
|
||||
OpenMainMenuAsync(CancellationToken.None).Forget();
|
||||
|
||||
if (GameInstance is not null) {
|
||||
Object.Destroy(GameInstance.gameObject);
|
||||
GameInstance = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user