📜 ⬆️ ⬇️

RedDwarf (formerly DarkStar) and Unity3D

For those who want to link the server on java RedDwarf (DarkStar):
Uses the free version of Unity.

1. Download the plugin on Unity (http://sourceforge.net/apps/trac/reddwarf/attachment/wiki/CommunityClientImplementations/DarkstarSharp.unitypackage). Import to Unity. In assesta should appear «DarkstarSharp».

2. In the file “SimpleSgsProtocol.cs” from the “DarkstarSharp” scripts folder, you need to change the protocol version,
VERSION value = 0x04; change to 0x05;
')
3. Create a folder for our scripts and create in it a csharp “Messages.cs” script with the following contents:

using UnityEngine; using System; using System.Text; using System.Collections; public class Messages { public static byte[] encodeString(String s) { return Encoding.ASCII.GetBytes(s); } public static String decodeString(byte[] message) { return ASCIIEncoding.ASCII.GetString(message); } } 


4. Create a csharp ConnectToRedDwarf.cs script with the following contents:
 using UnityEngine; using System; using System.Text; using System.Collections; using DarkstarSharp; public class RedDwarfClient : SimpleClientListener { private SimpleClient simpleClient; private ConnectToRedDwarf connectRD; private String host; private String username; public RedDwarfClient(String host, String username, ConnectToRedDwarf connectRD) { this.host = host; this.username = username; this.connectRD = connectRD; simpleClient = new SimpleClient(this); } public void LoggedIn(byte[] reconnectKey){ connectRD.loggedIn(); } public void LoginFailed(String reason){ connectRD.setStatus("login failed " + username+": "+reason); } public void SendMessage(String s){ try { simpleClient.WriteMessage(Messages.encodeString(s)); } catch (Exception e) { connectRD.setStatus(e.Message.ToString()); } } public void ReceivedMessage(byte[] message){ connectRD.receivedMessage(Messages.decodeString(message)); } public void Disconnected(bool forced, String message){ connectRD.disconnected(username, message); } public PasswordAuthentication GetPasswordAuthentication(){ return new PasswordAuthentication(username, ""); } public ClientChannelListener JoinedChannel(ClientChannel channel){ return null;; } public Boolean login() { Boolean result = true; try { simpleClient.login(host, 62964); } catch (Exception e) { connectRD.setStatus("cannot login" + username+": "+e.Message.ToString()); result = false; } return result; } public void LogOff(){ simpleClient.Logout(false); } } public class ConnectToRedDwarf : MonoBehaviour { private RedDwarfClient client; Boolean onConnect = false; String serverIP = "127.0.0.1"; String clientName = "Unity"; string status = "Not connection"; // Use this for initialization (  ,  ) void Start () { } // Update is called once per frame ( ,     ) void Update () { } void OnGUI() { if (!onConnect) { //renderer.material.color = Color.blue; GUI.BeginGroup(new Rect(Screen.width/2-150, Screen.height/2-200, 300, 400)); GUI.Box(new Rect(0, 0, 300, 400), "Connect to Server"); serverIP = GUI.TextField(new Rect(100,90,100,20), serverIP); clientName = GUI.TextField(new Rect(100,120,100,20), clientName); //   if (GUI.Button(new Rect(100, 150, 100, 20), "Connect")){ client = new RedDwarfClient(serverIP, clientName, this); onConnect = client.login(); } GUI.Label(new Rect(100,180,200,20), status); //  if (GUI.Button(new Rect(100, 360, 100, 20), "Close")) { } GUI.EndGroup(); } else { GUI.BeginGroup(new Rect(Screen.width/2-150, Screen.height/2-200, 300, 400)); GUI.Box(new Rect(0, 0, 300, 400), "Connection Status"); serverIP = GUI.TextField(new Rect(100,90,100,20), serverIP); clientName = GUI.TextField(new Rect(100,120,100,20), clientName); if (GUI.Button(new Rect(100, 150, 100, 20), "Drop connection")){ client.LogOff(); onConnect = false; } GUI.Label(new Rect(100,180,200,20), status); //  if (GUI.Button(new Rect(100, 360, 100, 20), "Close")) { } GUI.EndGroup(); } } public void loggedIn(){ //  -     status = "Logined"; } public void setStatus(String status){ //    (  GUI) this.status = status; } public void move(String pos){} //    public void endGame(String game){ //   status = game; } public void disconnected(String username, String message){ //     this.setStatus("Lost connection: "+message); onConnect = false; } public void receivedMessage(String msg){ if (msg.StartsWith("MOVE")) { move(msg); } else if (msg.StartsWith("END")) { endGame(msg); } } public void play(){ //   client.SendMessage("PLAY"); } } 


5. In our scene, create an empty object and throw the “ConnectToRedDwarf.cs” script on it. We launch and observe the menu where you need to enter the server's login address. In general, you will understand)

Afterword:
The content of the "Messages.cs" file is taken from the official site, and a bit retouched under sisharp.
The contents of the file “ConnectToRedDwarf.cs” are extracted from various sources plus the fruit of my imagination.
Thanks to dvb user for great articles habrahabr.ru/post/129174 , habrahabr.ru/post/134812

Checked, works. (c #)
Talk on unity3d.ru/distribution/viewtopic.php?f=14&t=13799&p=100827#p100827

Source: https://habr.com/ru/post/170931/


All Articles