Loading...   


using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using EasyJSON;


public class EqemuConnect : MonoBehaviour
{
    public GameObject EqemuConnectObject;
    public GameObject[] RaceHandler;
	public WebSocket sock;
    public GameObject NullGameObject;
    //Use this for initialization
    IEnumerator Start()
    {
		int i = 0;
//        string urlx ="http://server.eqbrowser.com";
//        string urlx = "38.103.160.13";
//        Security.PrefetchSocketPolicy(urlx, 843);
//        Security.PrefetchSocketPolicy(38.103.160.13, 843);

        sock = new WebSocket(new Uri("ws://127.0.0.1:9080"));
        yield return StartCoroutine(sock.Connect());

        Auth auth1 = new Auth();
        auth1.id = "token_auth_id";
        auth1.method = "WebInterface.Authorize";
        auth1.@params = new string[1] { "aksdjlka23ij3l1j23lk1j23j123jkjql" };
        //EASYJSON Serialize the entity into a JSON string
        string authoutput = EasyJSON.Serializer.Serialize(auth1, false);
        //send auth to server
		sock.SendString(authoutput);
		bool Connected = false;
		while (!Connected) {
			//Establish a world connection first.
			string reply = sock.RecvString();
			if (reply != null)
			{
				i = 0;
				{
					Debug.Log("Received: " + reply + i++);
					//				Debug.Log("i =" + i++);
					
					//deserialize
					//eqemu json start
					if (i > 0)
					{
						string IdChecker = ("[" + reply + "]");
						CheckIdClass[] IdChecker1 = Serializer.Deserialize<CheckIdClass[]>(IdChecker);
						//					Debug.Log ("OBJ2: "+CheckIdClass.id+i++);
						
						
						if (CheckIdClass.id == ("token_auth_id"))
						{
							Debug.Log("Auth successful. Proceeding");
							Connected = true;
						}
					}
					if (sock.Error != null)
					{
						Debug.LogError("Error: " + sock.Error);
						break;
					}
				}
			}
			yield return 0;
		}

		OpcodeToServerClass SendLoginInfoRequest = new OpcodeToServerClass();
		SendLoginInfoRequest.id = "0";
		SendLoginInfoRequest.method = "World.OpcodeToClient";

		LoginInfo_Struct SendLoginInfoStruct = new LoginInfo_Struct();
		SendLoginInfoStruct.login_user = "Secrets";
		SendLoginInfoStruct.login_pass = "test";
		SendLoginInfoStruct.zoning = "0";

		byte[] SendLoginInfoPacket = new byte[488];
		Buffer.BlockCopy(System.Text.Encoding.UTF8.GetBytes(SendLoginInfoStruct.login_user), 0, SendLoginInfoPacket, 0, SendLoginInfoStruct.login_user.Length);
		Buffer.BlockCopy(System.Text.Encoding.UTF8.GetBytes(SendLoginInfoStruct.login_pass), 0, SendLoginInfoPacket, SendLoginInfoStruct.login_user.Length + 1, SendLoginInfoStruct.login_pass.Length);
		string base64SendLoginInfo = System.Convert.ToBase64String(SendLoginInfoPacket);
		OpcodeToServerClass.@params = new string[5] { "-1", "-1", "427", "464", base64SendLoginInfo };
        //EASYJSON Serialize the entity into a JSON string
		string SendLoginInfoPacketString = Serializer.Serialize(SendLoginInfoRequest, false);
        //Request Entity Positions from server
		sock.SendString(SendLoginInfoPacketString);

        i = 0;
        while (Connected)
        {
			string reply = sock.RecvString();

            if (reply != null)
            {
                Debug.Log("Received: " + reply + i++);
                //				Debug.Log("i =" + i++);

                //deserialize
                //eqemu json start
                if (i > 1)
                {
                    string IdChecker = ("[" + reply + "]");
                    CheckIdClass[] IdChecker1 = Serializer.Deserialize<CheckIdClass[]>(IdChecker);
                    //					Debug.Log ("OBJ2: "+CheckIdClass.id+i++);


                    if (CheckIdClass.id == ("get_initial_entity_positions"))
                    {

                        Debug.Log("GetInitialEntitiyPositions");

                        string GIEParray = ("[" + reply + "]");
                        GetInitialEntityPositionClass[] GIEPObject = Serializer.Deserialize<GetInitialEntityPositionClass[]>(GIEParray);

                        //convert strings to float/int
                        float fx = Single.Parse(GIEPResult.x);
                        float fy = Single.Parse(GIEPResult.y);
                        float fz = Single.Parse(GIEPResult.z);
                        int race_id = int.Parse(GIEPResult.race_id);

                        if (GIEPResult.type == "NPC")
                        {
                            string curIndex = GIEPResult.ent_id;
                            GameObject newNPC = (GameObject)Instantiate(RaceHandler[race_id], new Vector3(-fx, fz, fy), Quaternion.identity);
                            newNPC.name = GIEPResult.ent_id;

                        }
                    }
                    //					if (CheckIdClass.method == ("On.NPC.Position"))
                    if (CheckIdClass.method != null)
                    {
                        switch (CheckIdClass.method)
                        {
                            case "On.NPC.Position":
                                {
                                    Debug.Log("OnNPCPosition");
                                    string OnNPCPositionarray = ("[" + reply + "]");
                                    OnNPCPositionClass[] OnNPCPositionObject = Serializer.Deserialize<OnNPCPositionClass[]>(OnNPCPositionarray);
                                    Debug.Log(OnNPCPositionClass.@params[1]);
                                    float fx = Single.Parse(OnNPCPositionClass.@params[2]);
                                    float fy = Single.Parse(OnNPCPositionClass.@params[3]);
                                    float fz = Single.Parse(OnNPCPositionClass.@params[4]);
                                    //									Debug.Log ("Received: "+reply+i++);

                                    NullGameObject = GameObject.Find(OnNPCPositionClass.@params[0]);
                                    Debug.Log(NullGameObject);
                                    NullGameObject.transform.position = new Vector3(-fx, fz, fy);
                                    break;
                                }
                            default:
                                {
                                    Debug.Log("unrecognized method: " + CheckIdClass.method);
                                    break;
                                }
                        }
                    }
                }
            }

			if (sock.Error != null)
            {
                Debug.LogError("Error: " + sock.Error);
                break;
            }
            yield return 0;
        }
		sock.Close();
    }

    public class Auth
    {
        public string id;
        public string method;
        public string[] @params;
    }

    public class GetInitialEntitiyPositions
    {
        public string id;
        public string method;
        public string[] @params;
    }

    public class GetNPCPositionUpdates
    {
        public string id;
        public string method;
        public string[] @params;
    }

    public class GIEPResult
    {
        public static string aggro_range { get; set; }
        public static string class_id { get; set; }
        public static string ent_id { get; set; }
        public static string h { get; set; }
        public static string name { get; set; }
        public static string race_id { get; set; }
        public static string type { get; set; }
        public static string x { get; set; }
        public static string y { get; set; }
        public static string z { get; set; }
    }

    public class GetInitialEntityPositionClass
    {
        public string id;
        public string error;
        public GIEPResult result;
    }

    public class OnNPCPositionClass
    {
        public string id;
        public string method;
        public static string[] @params { get; set; }

    }

	public class OpcodeFromServerClass
	{
		public string id;
		public string method;
		public static string zoneid { get; set; }
		public static string instanceid { get; set; }
		public static string opcode { get; set; }
		public static string datasize { get; set; }
		public static string data { get; set; }
		public static string[] @params { get; set; }
		
	}

	public class OpcodeToServerClass
	{
		public string id;
		public string method;
		public static string[] @params { get; set; }
		
	}

    public class CheckIdClass
    {
        public static string id { get; set; }
        public static string method { get; set; }
        public string[] @params;
    }
	
	public class LoginInfo_Struct {
		
		public static int PacketSize = 488;
		
		/*000*/	public string login_user { get; set; }  // 
		/*000*/	public string login_pass { get; set; }  //
		/*188*/	public string zoning	 { get; set; }		// 01 if zoning, 00 if not
		/*488*/
	};

}

Raw Paste Data