From 117f2ddfff3ff3729674ead7e6a7235da468c378 Mon Sep 17 00:00:00 2001 From: teascade Date: Mon, 8 May 2017 04:31:32 +0300 Subject: [PATCH] Added terminal commands for networking and fixed ui stuff --- Assets/Scenes/TestMap.unity | 6 ++--- Assets/Scripts/Networking/Client.cs | 4 +++ .../Scripts/Networking/NetworkEstablisher.cs | 25 +++++++++++++++++-- Assets/Scripts/Networking/Server.cs | 8 +++++- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/Assets/Scenes/TestMap.unity b/Assets/Scenes/TestMap.unity index 4e92438..a278fe2 100644 --- a/Assets/Scenes/TestMap.unity +++ b/Assets/Scenes/TestMap.unity @@ -607,9 +607,9 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: IPField: {fileID: 1974014777} - ClientPortField: {fileID: 0} - ServerPortField: {fileID: 0} - WorldRoot: {fileID: 0} + ClientPortField: {fileID: 2124238381} + ServerPortField: {fileID: 1813464171} + WorldRoot: {fileID: 605817613} --- !u!114 &564042476 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Networking/Client.cs b/Assets/Scripts/Networking/Client.cs index ddb2575..8e4b9e6 100644 --- a/Assets/Scripts/Networking/Client.cs +++ b/Assets/Scripts/Networking/Client.cs @@ -40,19 +40,23 @@ public class Client : MonoBehaviour { NetClient.Connect(ip, port); Debug.Log("Client launched!"); + Term.Println("Client launched!"); } public void OnConnected(NetworkMessage msg) { Debug.Log("Connected!"); + Term.Println("Connected!"); NetClient.Send(PktType.TestMessage, new TextMessage("Hai, I connected!")); } public void OnDisconnected(NetworkMessage msg) { Debug.Log("Disconnected!"); + Term.Println("Disconnected!"); } public void OnError(NetworkMessage msg) { Debug.LogError("Encountered a network error. Shutting down."); + Term.Println("Encountered a network error. Shutting down."); NetClient.Disconnect(); Running = false; } diff --git a/Assets/Scripts/Networking/NetworkEstablisher.cs b/Assets/Scripts/Networking/NetworkEstablisher.cs index 126f9af..de5ab59 100644 --- a/Assets/Scripts/Networking/NetworkEstablisher.cs +++ b/Assets/Scripts/Networking/NetworkEstablisher.cs @@ -22,8 +22,29 @@ public class NetworkEstablisher : MonoBehaviour { // Use this for initialization void Start () { - - } + Term.AddCommand("join", "joins a server at localhost:3935", (args) => { + StartClient("localhost", 3935); + }); + Term.AddCommand("join (ip)", "joins a server at given ip and port 3935", (args) => { + string ip = args[0]; + StartClient(ip, 3935); + }); + Term.AddCommand("join (ip) (port)", "joins a server at given ip and port", (args) => { + string ip = args[0]; + int port = 3935; + int.TryParse(args[1], out port); + StartClient(ip, port); + }); + + Term.AddCommand("host", "host a server at port 3935", (args) => { + StartServer(3935); + }); + Term.AddCommand("host (port)", "host a server at given port", (args) => { + int port = 3935; + int.TryParse(args[0], out port); + StartServer(port); + }); + } // Update is called once per frame void Update () { diff --git a/Assets/Scripts/Networking/Server.cs b/Assets/Scripts/Networking/Server.cs index 96e7c1e..690f75c 100644 --- a/Assets/Scripts/Networking/Server.cs +++ b/Assets/Scripts/Networking/Server.cs @@ -30,6 +30,7 @@ public class Server : MonoBehaviour { NetworkServer.RegisterHandler(MsgType.Error, OnError); Debug.Log("Server started on port " + port); + Term.Println("Server started on port " + port); } public void HandlePacket(NetworkMessage msg) { @@ -39,9 +40,11 @@ public class Server : MonoBehaviour { TextMessage TextMsg = new TextMessage(); TextMsg.Deserialize(msg.reader); Debug.Log("Received message: " + TextMsg.Message); + Term.Println("Received message: " + TextMsg.Message); break; default: Debug.LogError("Received an unknown packet, id: " + msg.msgType); + Term.Println("Received an unknown packet, id: " + msg.msgType); break; } @@ -49,13 +52,16 @@ public class Server : MonoBehaviour { public void OnConnected(NetworkMessage msg) { Debug.Log("Someone connected!"); + Term.Println("Someone connected!"); } public void OnDisconnected(NetworkMessage msg) { - Debug.Log("Someone disconnected?"); + Debug.Log("Someone disconnected."); + Term.Println("Someone disconnected."); } public void OnError(NetworkMessage msg) { Debug.LogError("Encountered a network error on server"); + Term.Println("Encountered a network error on server"); } }