private void ConfigureNetworkManager() { networkAddress = QSBCore.DefaultServerIP; maxConnections = MaxConnections; if (QSBCore.DebugSettings.UseKcpTransport) { kcp2k.Log.Info = s => { DebugLog.DebugWrite("[KCP] " + s); if (_kcpErrorLogs.Any(p => Regex.IsMatch(s, p))) { _lastTransportError = s; } }; kcp2k.Log.Warning = s => { DebugLog.DebugWrite("[KCP] " + s, MessageType.Warning); _lastTransportError = s; }; kcp2k.Log.Error = s => { DebugLog.DebugWrite("[KCP] " + s, MessageType.Error); _lastTransportError = s; }; } DebugLog.DebugWrite("Network Manager ready.", MessageType.Success); }
public override void OnClientConnect() // Called on the client when connecting to a server { DebugLog.DebugWrite("OnClientConnect", MessageType.Info); base.OnClientConnect(); OnClientConnected?.SafeInvoke(); QSBMessageManager.Init(); gameObject.AddComponent <RespawnOnDeath>(); gameObject.AddComponent <ServerStateManager>(); gameObject.AddComponent <ClientStateManager>(); if (QSBSceneManager.IsInUniverse) { QSBWorldSync.BuildWorldObjects(QSBSceneManager.CurrentScene).Forget(); } var specificType = QSBCore.IsHost ? QSBPatchTypes.OnServerClientConnect : QSBPatchTypes.OnNonServerClientConnect; QSBPatchManager.DoPatchType(specificType); QSBPatchManager.DoPatchType(QSBPatchTypes.OnClientConnect); Delay.RunWhen(() => PlayerTransformSync.LocalInstance, () => new PlayerJoinMessage(PlayerName).Send()); if (!QSBCore.IsHost) { Delay.RunWhen(() => PlayerTransformSync.LocalInstance, () => new RequestStateResyncMessage().Send()); } _everConnected = true; }
public override void OnServerAddPlayer(NetworkConnectionToClient connection) // Called on the server when a client joins { DebugLog.DebugWrite($"OnServerAddPlayer", MessageType.Info); base.OnServerAddPlayer(connection); NetworkServer.Spawn(Instantiate(_probePrefab), connection); }
public override void OnClientDisconnect() { DebugLog.DebugWrite("OnClientDisconnect"); base.OnClientDisconnect(); OnClientDisconnected?.SafeInvoke(_lastTransportError); _lastTransportError = null; }
public override void OnStopClient() // Called on the client when closing connection { DebugLog.DebugWrite("OnStopClient", MessageType.Info); DebugLog.ToConsole("Disconnecting from server...", MessageType.Info); Destroy(GetComponent <RespawnOnDeath>()); Destroy(GetComponent <ServerStateManager>()); Destroy(GetComponent <ClientStateManager>()); QSBPlayerManager.PlayerList.ForEach(player => player.HudMarker?.Remove()); QSBWorldSync.RemoveWorldObjects(); if (WakeUpSync.LocalInstance != null) { WakeUpSync.LocalInstance.OnDisconnect(); } if (_everConnected) { var specificType = QSBCore.IsHost ? QSBPatchTypes.OnServerClientConnect : QSBPatchTypes.OnNonServerClientConnect; QSBPatchManager.DoUnpatchType(specificType); QSBPatchManager.DoUnpatchType(QSBPatchTypes.OnClientConnect); } _everConnected = false; }
public override void OnStopClient() // Called on the client when closing connection { DebugLog.DebugWrite("OnStopClient", MessageType.Info); DebugLog.ToConsole("Disconnecting from server...", MessageType.Info); Destroy(GetComponent <RespawnOnDeath>()); QSBEventManager.Reset(); QSBPlayerManager.PlayerList.ForEach(player => player.HudMarker?.Remove()); RemoveWorldObjects(); QSBWorldSync.OrbSyncList.Clear(); QSBWorldSync.OldDialogueTrees.Clear(); if (_everConnected) { var specificType = QNetworkServer.active ? QSBPatchTypes.OnServerClientConnect : QSBPatchTypes.OnNonServerClientConnect; QSBPatchManager.DoUnpatchType(specificType); QSBPatchManager.DoUnpatchType(QSBPatchTypes.OnClientConnect); } _lobby.CanEditName = true; QSBCore.HasWokenUp = false; IsReady = false; _everConnected = false; }
public override void OnClientConnect(QNetworkConnection connection) // Called on the client when connecting to a server { DebugLog.DebugWrite("OnClientConnect", MessageType.Info); base.OnClientConnect(connection); QSBEventManager.Init(); gameObject.AddComponent <RespawnOnDeath>(); if (QSBSceneManager.IsInUniverse) { WorldObjectManager.Rebuild(QSBSceneManager.CurrentScene); } var specificType = QNetworkServer.active ? QSBPatchTypes.OnServerClientConnect : QSBPatchTypes.OnNonServerClientConnect; QSBPatchManager.DoPatchType(specificType); QSBPatchManager.DoPatchType(QSBPatchTypes.OnClientConnect); _lobby.CanEditName = false; OnNetworkManagerReady?.SafeInvoke(); IsReady = true; QSBCore.UnityEvents.RunWhen(() => QSBEventManager.Ready && PlayerTransformSync.LocalInstance != null, () => QSBEventManager.FireEvent(EventNames.QSBPlayerJoin, _lobby.PlayerName)); if (!QSBCore.IsServer) { QSBCore.UnityEvents.RunWhen(() => QSBEventManager.Ready && PlayerTransformSync.LocalInstance != null, () => QSBEventManager.FireEvent(EventNames.QSBPlayerStatesRequest)); } _everConnected = true; }
public override void OnStartClient(QNetworkClient _) { DebugLog.DebugWrite($"Setting defaultServerIP to {networkAddress}"); var config = QSBCore.Helper.Config; config.SetSettingsValue("defaultServerIP", networkAddress); QSBCore.Helper.Storage.Save(config, Constants.ModConfigFileName); }
public override void OnStopServer() { DebugLog.DebugWrite("OnStopServer", MessageType.Info); Destroy(GetComponent <RespawnOnDeath>()); DebugLog.ToConsole("Server stopped!", MessageType.Info); QSBPlayerManager.PlayerList.ForEach(player => player.HudMarker?.Remove()); base.OnStopServer(); }
public override void OnServerAddPlayer(QNetworkConnection connection, short playerControllerId) // Called on the server when a client joins { DebugLog.DebugWrite($"OnServerAddPlayer {playerControllerId}", MessageType.Info); base.OnServerAddPlayer(connection, playerControllerId); QNetworkServer.SpawnWithClientAuthority(Instantiate(_shipPrefab), connection); QNetworkServer.SpawnWithClientAuthority(Instantiate(_cameraPrefab), connection); QNetworkServer.SpawnWithClientAuthority(Instantiate(_probePrefab), connection); QNetworkServer.SpawnWithClientAuthority(Instantiate(_stickPrefab), connection); }
public override void OnStartServer() { DebugLog.DebugWrite("OnStartServer", MessageType.Info); if (QSBWorldSync.OldDialogueTrees.Count == 0 && QSBSceneManager.IsInUniverse) { QSBWorldSync.OldDialogueTrees = Resources.FindObjectsOfTypeAll <CharacterDialogueTree>().ToList(); } if (QSBSceneManager.IsInUniverse) { QSBCore.HasWokenUp = true; } }
public static PlayerInfo GetPlayer(uint id) { var player = PlayerList.FirstOrDefault(x => x.NetId == id); if (player != null) { return(player); } DebugLog.DebugWrite($"Creating player with id {id}", MessageType.Info); player = new PlayerInfo(id); PlayerList.Add(player); return(player); }
public override void OnServerDisconnect(QNetworkConnection connection) // Called on the server when any client disconnects { base.OnServerDisconnect(connection); DebugLog.DebugWrite("OnServerDisconnect", MessageType.Info); foreach (var item in QSBWorldSync.OrbSyncList) { var identity = item.GetComponent <QNetworkIdentity>(); if (identity.ClientAuthorityOwner == connection) { identity.RemoveClientAuthority(connection); } } }
/// create a new network prefab from the network object prefab template. /// this works by calling Unload(false) and then reloading the AssetBundle, /// which makes LoadAsset give you a new resource. /// see https://docs.unity3d.com/Manual/AssetBundles-Native.html. private static GameObject MakeNewNetworkObject(int assetId, string name, Type transformSyncType) { var bundle = QSBCore.Helper.Assets.LoadBundle("AssetBundles/empty"); var template = bundle.LoadAsset <GameObject>("Assets/Prefabs/Empty.prefab"); bundle.Unload(false); DebugLog.DebugWrite($"MakeNewNetworkObject - prefab id {template.GetInstanceID()} " + $"for {assetId} {name} {transformSyncType.Name}"); template.name = name; template.AddComponent <NetworkIdentity>().SetValue("m_AssetId", assetId.ToGuid().ToString("N")); template.AddComponent(transformSyncType); return(template); }
private void ConfigureNetworkManager() { networkAddress = QSBCore.DefaultServerIP; networkPort = QSBCore.Port; maxConnections = MaxConnections; customConfig = true; connectionConfig.AddChannel(QosType.Reliable); connectionConfig.AddChannel(QosType.Unreliable); this.SetValue("m_MaxBufferedPackets", MaxBufferedPackets); channels.Add(QosType.Reliable); channels.Add(QosType.Unreliable); DebugLog.DebugWrite("Network Manager ready.", MessageType.Success); }
private static void OnCompleteSceneLoad(OWScene oldScene, OWScene newScene) { DebugLog.DebugWrite($"COMPLETE SCENE LOAD ({oldScene} -> {newScene})", MessageType.Info); if (QSBCore.IsInMultiplayer) { WorldObjectManager.Rebuild(newScene); } var universe = InUniverse(newScene); OnSceneLoaded?.SafeInvoke(newScene, universe); if (universe) { OnUniverseSceneLoaded?.SafeInvoke(newScene); } }
public static void HandleFullStateMessage(PlayerStateMessage message) { DebugLog.DebugWrite($"Handle full state message"); var player = GetPlayer(message.AboutId); player.Name = message.PlayerName; player.IsReady = message.PlayerReady; player.State = message.PlayerState; //DebugLog.DebugWrite($"Updating state of player {player.NetId} to : {Environment.NewLine}" + // $"{DebugLog.GenerateTable(Enum.GetNames(typeof(State)).ToList(), FlagsHelper.FlagsToListSet(player.State))}"); if (LocalPlayer.IsReady) { player.UpdateStateObjects(); } }
public override void OnServerDisconnect(NetworkConnectionToClient conn) // Called on the server when any client disconnects { DebugLog.DebugWrite("OnServerDisconnect", MessageType.Info); // local conn = we are host, so skip if (conn is not LocalConnectionToClient) { // revert authority from ship if (ShipTransformSync.LocalInstance != null) { var identity = ShipTransformSync.LocalInstance.netIdentity; if (identity != null && identity.connectionToClient == conn) { identity.SetAuthority(QSBPlayerManager.LocalPlayerId); } } // stop dragging for the orbs this player was dragging foreach (var qsbOrb in QSBWorldSync.GetWorldObjects <QSBOrb>()) { if (qsbOrb.TransformSync == null) { DebugLog.ToConsole($"{qsbOrb} TransformSync == null??????????", MessageType.Warning); continue; } var identity = qsbOrb.TransformSync.netIdentity; if (identity.connectionToClient == conn) { qsbOrb.SetDragging(false); qsbOrb.SendMessage(new OrbDragMessage(false)); } } AuthorityManager.OnDisconnect(conn); } base.OnServerDisconnect(conn); }
public void CleanupNetworkBehaviour(uint netId) { DebugLog.DebugWrite($"Cleaning up object {netId}"); // Multiple networkbehaviours can use the same networkidentity (same netId), so get all of them var networkBehaviours = FindObjectsOfType <NetworkBehaviour>() .Where(x => x != null && x.netId.Value == netId); foreach (var networkBehaviour in networkBehaviours) { var transformSync = networkBehaviour.GetComponent <TransformSync.TransformSync>(); if (transformSync != null) { PlayerRegistry.PlayerSyncObjects.Remove(transformSync); if (transformSync.SyncedTransform != null) { Destroy(transformSync.SyncedTransform.gameObject); } } Destroy(networkBehaviour.gameObject); } }
private static void OnCompleteSceneLoad(OWScene oldScene, OWScene newScene) { DebugLog.DebugWrite($"COMPLETE SCENE LOAD ({oldScene} -> {newScene})", MessageType.Info); QSBWorldSync.RemoveWorldObjects(); var universe = InUniverse(newScene); if (QSBCore.IsInMultiplayer && universe) { // So objects have time to be deleted, made, whatever Delay.RunNextFrame(() => QSBWorldSync.BuildWorldObjects(newScene).Forget()); } OnSceneLoaded?.SafeInvoke(oldScene, newScene, universe); if (universe) { OnUniverseSceneLoaded?.SafeInvoke(oldScene, newScene); } if (newScene == OWScene.TitleScreen && QSBCore.IsInMultiplayer) { QSBNetworkManager.singleton.StopHost(); } }
public static void RemovePlayer(uint id) { DebugLog.DebugWrite($"Removing player with id {id}", MessageType.Info); PlayerList.Remove(GetPlayer(id)); }
static QSBSceneManager() { LoadManager.OnCompleteSceneLoad += OnCompleteSceneLoad; DebugLog.DebugWrite("Scene Manager ready.", MessageType.Success); }