From 0edb0e36416c9663f1226b225011edeb20df857c Mon Sep 17 00:00:00 2001 From: in0finite Date: Sun, 3 Apr 2022 02:19:39 +0200 Subject: [PATCH] fix network discovery ; use git submodule --- .gitmodules | 3 + Assets/NetworkDiscoveryUnity | 1 + Assets/NetworkDiscoveryUnity.meta | 8 +++ Assets/Prefabs/GameManager.prefab | 50 +++++++++++++ Assets/Prefabs/NetworkManager.prefab | 70 +++---------------- .../Networking/NetworkDiscoveryManager.cs | 61 ++++++++++++++++ .../NetworkDiscoveryManager.cs.meta | 11 +++ Assets/Scripts/UI/JoinGameWindow.cs | 40 ++++++----- 8 files changed, 165 insertions(+), 79 deletions(-) create mode 160000 Assets/NetworkDiscoveryUnity create mode 100644 Assets/NetworkDiscoveryUnity.meta create mode 100644 Assets/Scripts/Networking/NetworkDiscoveryManager.cs create mode 100644 Assets/Scripts/Networking/NetworkDiscoveryManager.cs.meta diff --git a/.gitmodules b/.gitmodules index 150f5eae..d690d793 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "Assets/NavMeshes"] path = Assets/NavMeshes url = https://github.com/GTA-ASM/NavMeshes +[submodule "Assets/NetworkDiscoveryUnity"] + path = Assets/NetworkDiscoveryUnity + url = https://github.com/in0finite/NetworkDiscoveryUnity diff --git a/Assets/NetworkDiscoveryUnity b/Assets/NetworkDiscoveryUnity new file mode 160000 index 00000000..723c3ead --- /dev/null +++ b/Assets/NetworkDiscoveryUnity @@ -0,0 +1 @@ +Subproject commit 723c3eadcd07f79083e8f2434ec6432b59deb9c0 diff --git a/Assets/NetworkDiscoveryUnity.meta b/Assets/NetworkDiscoveryUnity.meta new file mode 100644 index 00000000..ea292bad --- /dev/null +++ b/Assets/NetworkDiscoveryUnity.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: c87fbb2ff57b3b74b88c20989a0bac78 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Prefabs/GameManager.prefab b/Assets/Prefabs/GameManager.prefab index fd34afec..7e2da8b0 100644 --- a/Assets/Prefabs/GameManager.prefab +++ b/Assets/Prefabs/GameManager.prefab @@ -39,6 +39,9 @@ GameObject: - component: {fileID: 2483599330734961439} - component: {fileID: 6746758691818800092} - component: {fileID: 6423383668577662412} + - component: {fileID: 492799016881118158} + - component: {fileID: 5842669163583086755} + - component: {fileID: 450012255851182817} m_Layer: 0 m_Name: GameManager m_TagString: Untagged @@ -550,3 +553,50 @@ MonoBehaviour: m_EditorClassIdentifier: m_maxTimePerFrameMs: 0 m_navMeshPathfindingIterationsPerFrame: 1000 +--- !u!114 &492799016881118158 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1297494511425690} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 65d5e9cc49a255842837a7d853db68fe, type: 3} + m_Name: + m_EditorClassIdentifier: + m_serverPort: 18418 + gameServerPortNumber: 7777 +--- !u!114 &5842669163583086755 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1297494511425690} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 97d06c4b188731a47baf1a65a4b8a512, type: 3} + m_Name: + m_EditorClassIdentifier: + drawGUI: 0 + offsetX: 5 + offsetY: 150 + width: 500 + height: 400 + refreshInterval: 3 + onConnectEvent: + m_PersistentCalls: + m_Calls: [] +--- !u!114 &450012255851182817 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1297494511425690} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 98aa3365f77ceca4b89c239f0d96a766, type: 3} + m_Name: + m_EditorClassIdentifier: diff --git a/Assets/Prefabs/NetworkManager.prefab b/Assets/Prefabs/NetworkManager.prefab index efaf05a5..83230188 100644 --- a/Assets/Prefabs/NetworkManager.prefab +++ b/Assets/Prefabs/NetworkManager.prefab @@ -11,8 +11,6 @@ GameObject: - component: {fileID: 434938348018962339} - component: {fileID: 2570981519678807036} - component: {fileID: 4862460140142645991} - - component: {fileID: 8720984785530044672} - - component: {fileID: 2615086005589439192} m_Layer: 0 m_Name: NetworkManager m_TagString: Untagged @@ -48,14 +46,14 @@ MonoBehaviour: m_EditorClassIdentifier: dontDestroyOnLoad: 1 runInBackground: 1 - startOnHeadless: 0 + autoStartServerBuild: 0 serverTickRate: 30 - showDebugMessages: 0 offlineScene: onlineScene: transport: {fileID: 4862460140142645991} networkAddress: localhost maxConnections: 20 + authenticator: {fileID: 0} playerPrefab: {fileID: 3871880573184668455, guid: 750f4b120647dadf39075cba7361800a, type: 3} autoCreatePlayer: 1 @@ -82,63 +80,15 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: c7424c1070fad4ba2a7a96b02fbeb4bb, type: 3} m_Name: m_EditorClassIdentifier: - OnClientConnected: - m_PersistentCalls: - m_Calls: [] - OnClientDataReceived: - m_PersistentCalls: - m_Calls: [] - OnClientError: - m_PersistentCalls: - m_Calls: [] - OnClientDisconnected: - m_PersistentCalls: - m_Calls: [] - OnServerConnected: - m_PersistentCalls: - m_Calls: [] - OnServerDataReceived: - m_PersistentCalls: - m_Calls: [] - OnServerError: - m_PersistentCalls: - m_Calls: [] - OnServerDisconnected: - m_PersistentCalls: - m_Calls: [] port: 7777 NoDelay: 1 + SendTimeout: 5000 + ReceiveTimeout: 30000 serverMaxMessageSize: 16384 + serverMaxReceivesPerTick: 10000 + serverSendQueueLimitPerConnection: 10000 + serverReceiveQueueLimitPerConnection: 10000 clientMaxMessageSize: 16384 ---- !u!114 &8720984785530044672 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 4549513672316258300} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 65d5e9cc49a255842837a7d853db68fe, type: 3} - m_Name: - m_EditorClassIdentifier: - m_serverPort: 18418 - gameServerPortNumber: 7777 ---- !u!114 &2615086005589439192 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 4549513672316258300} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 97d06c4b188731a47baf1a65a4b8a512, type: 3} - m_Name: - m_EditorClassIdentifier: - drawGUI: 1 - offsetX: 5 - offsetY: 150 - width: 500 - height: 400 - refreshInterval: 3 + clientMaxReceivesPerTick: 1000 + clientSendQueueLimit: 10000 + clientReceiveQueueLimit: 10000 diff --git a/Assets/Scripts/Networking/NetworkDiscoveryManager.cs b/Assets/Scripts/Networking/NetworkDiscoveryManager.cs new file mode 100644 index 00000000..554d35e3 --- /dev/null +++ b/Assets/Scripts/Networking/NetworkDiscoveryManager.cs @@ -0,0 +1,61 @@ +using UnityEngine; +using SanAndreasUnity.Utilities; +using NetworkDiscoveryUnity; + +namespace SanAndreasUnity.Net +{ + public class NetworkDiscoveryManager : StartupSingleton + { + public const string NumPlayersKey = "NumPlayers"; + public const string MaxNumPlayersKey = "MaxNumPlayers"; + + public NetworkDiscovery NetworkDiscovery { get; private set; } + + + protected override void OnSingletonAwake() + { + this.NetworkDiscovery = this.GetComponentOrThrow(); + } + + protected override void OnSingletonStart() + { + NetManager.Instance.onServerStatusChanged -= OnServerStatusChanged; + NetManager.Instance.onServerStatusChanged += OnServerStatusChanged; + + Player.onStart -= OnPlayerStart; + Player.onStart += OnPlayerStart; + + Player.onDisable -= OnPlayerDisable; + Player.onDisable += OnPlayerDisable; + + this.AssignPlayerCounts(); + } + + private void OnServerStatusChanged() + { + if (NetStatus.IsServer) + { + this.AssignPlayerCounts(); + this.NetworkDiscovery.EnsureServerIsInitialized(); + } + else + this.NetworkDiscovery.CloseServerUdpClient(); + } + + private void OnPlayerStart(Player player) + { + this.AssignPlayerCounts(); + } + + private void OnPlayerDisable(Player player) + { + this.AssignPlayerCounts(); + } + + private void AssignPlayerCounts() + { + this.NetworkDiscovery.RegisterResponseData(NumPlayersKey, Player.AllPlayersList.Count.ToString()); + this.NetworkDiscovery.RegisterResponseData(MaxNumPlayersKey, NetManager.maxNumPlayers.ToString()); + } + } +} diff --git a/Assets/Scripts/Networking/NetworkDiscoveryManager.cs.meta b/Assets/Scripts/Networking/NetworkDiscoveryManager.cs.meta new file mode 100644 index 00000000..1e70f672 --- /dev/null +++ b/Assets/Scripts/Networking/NetworkDiscoveryManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 98aa3365f77ceca4b89c239f0d96a766 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/UI/JoinGameWindow.cs b/Assets/Scripts/UI/JoinGameWindow.cs index f8d44366..8ebc4a42 100644 --- a/Assets/Scripts/UI/JoinGameWindow.cs +++ b/Assets/Scripts/UI/JoinGameWindow.cs @@ -4,7 +4,7 @@ using SanAndreasUnity.Utilities; using SanAndreasUnity.Net; using System.Linq; using System.Threading.Tasks; -using Mirror.Discovery; +using NetworkDiscoveryUnity; namespace SanAndreasUnity.UI { @@ -45,12 +45,16 @@ namespace SanAndreasUnity.UI float height = width * 9f / 16f; this.windowRect = GUIUtils.GetCenteredRect(new Vector2(width, height)); - m_netDiscoveryHUD = Mirror.NetworkManager.singleton.GetComponentOrThrow(); - - // TODO: NetworkDiscoveryHud connection actions & draw gui - // m_netDiscoveryHUD.connectAction = this.ConnectFromDiscovery; - // m_netDiscoveryHUD.drawGUI = false; - } + m_netDiscoveryHUD = NetworkDiscoveryManager.Singleton.NetworkDiscovery.GetComponentOrThrow(); + m_netDiscoveryHUD.drawGUI = false; + m_netDiscoveryHUD.onConnectEvent.RemoveAllListeners(); + m_netDiscoveryHUD.onConnectEvent.AddListener(this.ConnectFromDiscovery); + m_netDiscoveryHUD.additionalDataToDisplay.AddRange(new string[] + { + NetworkDiscoveryManager.NumPlayersKey, + NetworkDiscoveryManager.MaxNumPlayersKey, + }); + } void Update() { @@ -88,9 +92,8 @@ namespace SanAndreasUnity.UI } else if (LanTabIndex == m_currentTabIndex) { - //TODO: NetDiscovery Hud window resizing - //m_netDiscoveryHUD.width = (int) this.WindowSize.x - 30; - //m_netDiscoveryHUD.DisplayServers(); + m_netDiscoveryHUD.width = (int) this.WindowSize.x - 30; + m_netDiscoveryHUD.DisplayServers(); } else if (InternetTabIndex == m_currentTabIndex) { @@ -165,10 +168,9 @@ namespace SanAndreasUnity.UI { if (LanTabIndex == m_currentTabIndex) { - //TODO: NetDiscovery Hud visibility & button stuff - // GUI.enabled = ! m_netDiscoveryHUD.IsRefreshing; - // buttonText = m_netDiscoveryHUD.IsRefreshing ? ( "Refreshing." + new string('.', (int) ((Time.time * 2) % 3)) ) : "Refresh LAN"; - // buttonAction = () => m_netDiscoveryHUD.Refresh(); + GUI.enabled = ! m_netDiscoveryHUD.IsRefreshing; + buttonText = m_netDiscoveryHUD.IsRefreshing ? ( "Refreshing." + new string('.', (int) ((Time.time * 2) % 3)) ) : "Refresh LAN"; + buttonAction = () => m_netDiscoveryHUD.Refresh(); } else if (InternetTabIndex == m_currentTabIndex) { @@ -208,20 +210,20 @@ namespace SanAndreasUnity.UI void ConnectDirectly() { - this.Connect(m_ipStr, int.Parse(m_portStr)); + this.Connect(m_ipStr, ushort.Parse(m_portStr)); } - void ConnectFromDiscovery(ServerResponse info) + void ConnectFromDiscovery(NetworkDiscovery.DiscoveryInfo info) { - this.Connect(info.EndPoint.Address.ToString(), info.EndPoint.Port); + this.Connect(info.EndPoint.Address.ToString(), info.GetGameServerPort()); } void ConnectToServerFromMasterServer(ServerInfo serverInfo) { - Connect(serverInfo.IP, serverInfo.Port); + Connect(serverInfo.IP, (ushort) serverInfo.Port); } - void Connect(string ip, int port) + void Connect(string ip, ushort port) { try {