{"id":170931,"date":"2013-02-27T13:55:10","date_gmt":"2013-02-27T09:55:10","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=170931"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=170931","title":{"rendered":"<span class=\"post_title\">RedDwarf (\u0431\u044b\u0432\u0448\u0438\u0439 DarkStar) \u0438 Unity3D<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0414\u043b\u044f \u0442\u0435\u0445 \u043a\u0442\u043e \u0445\u043e\u0447\u0435\u0442 \u0441\u0432\u044f\u0437\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430 java RedDwarf (DarkStar):<br \/>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f Unity.<\/p>\n<p>  1. \u0421\u043a\u0430\u0447\u0430\u0442\u044c <a href=\"http:\/\/sourceforge.net\/apps\/trac\/reddwarf\/attachment\/wiki\/CommunityClientImplementations\/DarkstarSharp.unitypackage\">\u043f\u043b\u0430\u0433\u0438\u043d \u043d\u0430 Unity<\/a> (http:\/\/sourceforge.net\/apps\/trac\/reddwarf\/attachment\/wiki\/CommunityClientImplementations\/DarkstarSharp.unitypackage). \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 Unity. \u0412 \u0430\u0441\u0441\u0435\u0442\u0430\u0445 \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u044f\u0432\u0438\u0442\u044c\u0441\u044f \u00abDarkstarSharp\u00bb.<\/p>\n<p>  2. \u0412 \u0444\u0430\u0439\u043b\u0435 \u00abSimpleSgsProtocol.cs\u00bb \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 \u0441\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u0430\u043c\u0438 \u00abDarkstarSharp\u00bb, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u0435\u0440\u0441\u0438\u044e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430,<br \/>  \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 VERSION = 0x04; \u043c\u0435\u043d\u044f\u0435\u043c \u043d\u0430 0x05;<\/p>\n<p>  3. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u0430\u043f\u043a\u0443 \u0434\u043b\u044f \u043d\u0430\u0448\u0438\u0445 \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432 \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0432 \u043d\u0435\u043c csharp \u0441\u043a\u0440\u0438\u043f\u0442 \u00abMessages.cs\u00bb \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f:<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<pre><code class=\"cs\">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);         } } <\/code><\/pre>\n<p>  4. \u0421\u043e\u0437\u0434\u0430\u0435\u043c csharp \u0441\u043a\u0440\u0438\u043f\u0442 \u00abConnectToRedDwarf.cs\u00bb \u0441 \u0442\u0430\u043a\u0438\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u044b\u043c:  <\/p>\n<pre><code class=\"cs\"> 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(&quot;login failed &quot; + username+&quot;: &quot;+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, &quot;&quot;);         }             public ClientChannelListener JoinedChannel(ClientChannel channel){                          return null;;                   }                 public Boolean login() {                 Boolean result = true;         try {                         simpleClient.login(host, 62964);         } catch (Exception e) {             connectRD.setStatus(&quot;cannot login&quot; + username+&quot;: &quot;+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 = &quot;127.0.0.1&quot;;         String clientName = &quot;Unity&quot;;         string status = &quot;Not connection&quot;;          \/\/ Use this for initialization (\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435)         void Start () {                                                 }                 \/\/ Update is called once per frame (\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e, \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0440\u0438 \u043e\u0442\u0440\u0438\u0441\u043e\u0432\u043a\u0435 \u043a\u0430\u0434\u0440\u0430)         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), &quot;Connect to Server&quot;);                         serverIP = GUI.TextField(new Rect(100,90,100,20), serverIP);                         clientName = GUI.TextField(new Rect(100,120,100,20), clientName);                         \/\/\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443                                                      if (GUI.Button(new Rect(100, 150, 100, 20), &quot;Connect&quot;)){                                 client = new RedDwarfClient(serverIP, clientName, this);                                                                onConnect = client.login();                         }                                               GUI.Label(new Rect(100,180,200,20), status);                 \/\/\u0417\u0430\u043a\u0440\u044b\u0442\u044c \u043e\u043a\u043d\u043e                 if (GUI.Button(new Rect(100, 360, 100, 20), &quot;Close&quot;))                 {                                   }                 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), &quot;Connection Status&quot;);                                                         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), &quot;Drop connection&quot;)){                                 client.LogOff();                                 onConnect = false;                         }                                               GUI.Label(new Rect(100,180,200,20), status);                 \/\/\u0417\u0430\u043a\u0440\u044b\u0442\u044c \u043e\u043a\u043d\u043e                 if (GUI.Button(new Rect(100, 360, 100, 20), &quot;Close&quot;))                 {                                   }                 GUI.EndGroup();                 }         }                 public void loggedIn(){ \/\/ \u0434\u0435\u043b\u0430\u0435\u043c \u0447\u0442\u043e-\u0442\u043e \u043f\u0440\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443                 status = &quot;Logined&quot;;         }                 public void setStatus(String status){ \/\/ \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043d\u043e\u0432\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u043c \u0432 GUI)                 this.status = status;         }                 public void move(String pos){} \/\/ \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u043c \u043d\u0430\u0448 \u043e\u0431\u044a\u0435\u043a\u0442                 public void endGame(String game){ \/\/ \u043a\u043e\u043d\u0435\u0446 \u0438\u0433\u0440\u044b                 status = game;         }                 public void disconnected(String username, String message){ \/\/ \u043e\u0431\u0440\u044b\u0432 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c                 this.setStatus(&quot;Lost connection: &quot;+message);                 onConnect = false;         }                 public void receivedMessage(String msg){                 if (msg.StartsWith(&quot;MOVE&quot;)) {             move(msg);         } else if (msg.StartsWith(&quot;END&quot;)) {             endGame(msg);         }                       }                 public void play(){ \/\/ \u043b\u043e\u0433\u0438\u043a\u0430 \u0438\u0433\u0440\u044b                 client.SendMessage(&quot;PLAY&quot;);         }               } <\/code><\/pre>\n<p>  5. \u0412 \u043d\u0430\u0448\u0435\u0439 \u0441\u0446\u0435\u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u0443\u0441\u0442\u043e\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438 \u043a\u0438\u0434\u0430\u0435\u043c \u043d\u0430 \u043d\u0435\u0433\u043e \u0441\u043a\u0440\u0438\u043f\u0442 \u00abConnectToRedDwarf.cs\u00bb. \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043d\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0438 \u043d\u0430\u0431\u043b\u044e\u0434\u0435\u043c \u043c\u0435\u043d\u044e \u0433\u0434\u0435 \u043d\u0443\u0436\u043d\u043e \u0432\u0432\u0435\u0441\u0442\u0438 \u0430\u0434\u0440\u0435\u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043b\u043e\u0433\u0438\u043d. \u0412 \u043e\u0431\u0449\u0435\u043c \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u0442\u0435\u0441\u044c)<\/p>\n<p>  \u041f\u043e\u0441\u043b\u0435\u0441\u043b\u043e\u0432\u0438\u0435:<br \/>  \u0421\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0444\u0430\u0439\u043b\u0430 \u00abMessages.cs\u00bb \u0432\u0437\u044f\u0442\u043e \u0441 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u0430\u0439\u0442\u0430, \u0438 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0440\u0435\u0442\u0443\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u043f\u043e\u0434 \u0441\u0438\u0448\u0430\u0440\u043f.<br \/>  \u0421\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0444\u0430\u0439\u043b\u0430 \u00abConnectToRedDwarf.cs\u00bb \u0432\u044b\u0447\u043b\u0435\u043d\u0435\u043d\u043e \u0441 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432 \u043f\u043b\u044e\u0441 \u043f\u043b\u043e\u0434 \u043c\u043e\u0435\u0433\u043e \u0432\u043e\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f.<br \/>  \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e <a href=\"http:\/\/habrahabr.ru\/users\/dvb\/\">dvb <\/a> \u0437\u0430 \u0432\u0435\u043b\u0438\u043a\u043e\u043b\u0435\u043f\u043d\u044b\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/129174\/\">habrahabr.ru\/post\/129174\/<\/a>, <a href=\"http:\/\/habrahabr.ru\/post\/134812\/\">habrahabr.ru\/post\/134812\/<\/a><\/p>\n<p>  \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043b, \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. (c#)<br \/>  \u041e\u0431\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u0435 \u043d\u0430 <a href=\"http:\/\/unity3d.ru\/distribution\/viewtopic.php?f=14&#038;t=13799&#038;p=100827#p100827\">unity3d.ru\/distribution\/viewtopic.php?f=14&#038;t=13799&#038;p=100827#p100827<\/a>    \t \t\t   \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/170931\/\"> http:\/\/habrahabr.ru\/post\/170931\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0414\u043b\u044f \u0442\u0435\u0445 \u043a\u0442\u043e \u0445\u043e\u0447\u0435\u0442 \u0441\u0432\u044f\u0437\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430 java RedDwarf (DarkStar):<br \/>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f Unity.<\/p>\n<p>  1. \u0421\u043a\u0430\u0447\u0430\u0442\u044c <a href=\"http:\/\/sourceforge.net\/apps\/trac\/reddwarf\/attachment\/wiki\/CommunityClientImplementations\/DarkstarSharp.unitypackage\">\u043f\u043b\u0430\u0433\u0438\u043d \u043d\u0430 Unity<\/a> (http:\/\/sourceforge.net\/apps\/trac\/reddwarf\/attachment\/wiki\/CommunityClientImplementations\/DarkstarSharp.unitypackage). \u0418\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 Unity. \u0412 \u0430\u0441\u0441\u0435\u0442\u0430\u0445 \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u044f\u0432\u0438\u0442\u044c\u0441\u044f \u00abDarkstarSharp\u00bb.<\/p>\n<p>  2. \u0412 \u0444\u0430\u0439\u043b\u0435 \u00abSimpleSgsProtocol.cs\u00bb \u0438\u0437 \u043f\u0430\u043f\u043a\u0438 \u0441\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u0430\u043c\u0438 \u00abDarkstarSharp\u00bb, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u0435\u0440\u0441\u0438\u044e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430,<br \/>  \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 VERSION = 0x04; \u043c\u0435\u043d\u044f\u0435\u043c \u043d\u0430 0x05;<\/p>\n<p>  3. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u0430\u043f\u043a\u0443 \u0434\u043b\u044f \u043d\u0430\u0448\u0438\u0445 \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0432 \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0432 \u043d\u0435\u043c csharp \u0441\u043a\u0440\u0438\u043f\u0442 \u00abMessages.cs\u00bb \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f:  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-170931","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/170931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=170931"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/170931\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=170931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=170931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=170931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}