|
@@ -7,6 +7,7 @@ using System.Net.Sockets;
|
|
|
using System.Net;
|
|
|
using System.Text;
|
|
|
using System;
|
|
|
+using UnityEngine.UI;
|
|
|
|
|
|
public class Client : MonoBehaviour
|
|
|
{
|
|
@@ -20,11 +21,11 @@ public class Client : MonoBehaviour
|
|
|
bool login_sent;
|
|
|
bool entered = false;
|
|
|
bool dc = false;
|
|
|
- bool connected = false;
|
|
|
+ public bool connected = false;
|
|
|
int lag = 0;
|
|
|
double last_ping_time = 0;
|
|
|
static string[] Servers = { "dev.prmsys.net", "localhost", "corp.prmsys.net" };
|
|
|
- static bool ShowPacketsSent = true;
|
|
|
+ static bool ShowPacketsSent = false;
|
|
|
//private const bool ShowPacketsSent = false;
|
|
|
|
|
|
// private const bool ShowPackets = true; //ставится только в редакторе
|
|
@@ -32,11 +33,6 @@ public class Client : MonoBehaviour
|
|
|
double initial_timer;
|
|
|
int internal_timer = 0;
|
|
|
|
|
|
- internal void SendGetUsers(object p)
|
|
|
- {
|
|
|
- throw new NotImplementedException();
|
|
|
- }
|
|
|
-
|
|
|
public double timer
|
|
|
{
|
|
|
get
|
|
@@ -54,8 +50,6 @@ public class Client : MonoBehaviour
|
|
|
Dictionary<int, int> PacketLength = new Dictionary<int, int>();
|
|
|
public delegate void OnReceive(byte[] bytedata);
|
|
|
public static OnReceive[] packets = new OnReceive[255];
|
|
|
- private static Dictionary<string, UnityEngine.Object> loaded_models = new Dictionary<string, UnityEngine.Object>();
|
|
|
- private static Dictionary<string, UnityEngine.Object> loaded_icons = new Dictionary<string, UnityEngine.Object>();
|
|
|
public string _dataPath = "";
|
|
|
GameObject drone;
|
|
|
public static ClientType clientType;
|
|
@@ -79,19 +73,21 @@ public class Client : MonoBehaviour
|
|
|
|
|
|
Register(1, Disconnect, 5);
|
|
|
Register(2, Login, 9);
|
|
|
- Register(3, CoordinatesReceive);
|
|
|
+ Register(3, User.CoordinatesReceive);
|
|
|
Register(30, Myping, 3);
|
|
|
- Register(47, UsersReceive);
|
|
|
- Register(48, BeaconsReceive);
|
|
|
+ Register(47, User.Receive);
|
|
|
+ Register(48, Beacon.Receive);
|
|
|
|
|
|
Register(51, Wall.ReceiveFromServer);
|
|
|
Register(52, Zone.ReceiveFromServer);
|
|
|
Register(54, Location.ReceiveFromServer);
|
|
|
+ Register(55, ImgReceive);
|
|
|
|
|
|
- //set data path
|
|
|
- //if (Application.platform == RuntimePlatform.WindowsWebPlayer ||
|
|
|
- // Application.platform == RuntimePlatform.OSXWebPlayer)
|
|
|
- if (Application.platform == RuntimePlatform.WebGLPlayer)
|
|
|
+
|
|
|
+ //set data path
|
|
|
+ //if (Application.platform == RuntimePlatform.WindowsWebPlayer ||
|
|
|
+ // Application.platform == RuntimePlatform.OSXWebPlayer)
|
|
|
+ if (Application.platform == RuntimePlatform.WebGLPlayer)
|
|
|
{
|
|
|
_dataPath = Application.dataPath + "/StreamingAssets";
|
|
|
clientType = ClientType.Web;
|
|
@@ -142,41 +138,6 @@ public class Client : MonoBehaviour
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void CoordinatesReceive(byte[] bytedata)
|
|
|
- {
|
|
|
- //var coordleng = bytedata.Length - 11;
|
|
|
- var accid = BitConverter.ToUInt32(bytedata, 5);
|
|
|
- var locid = BitConverter.ToUInt32(bytedata, 9);
|
|
|
- var player = PlayerController.instance;
|
|
|
- //WorkerController.TestStructures.Clear();
|
|
|
- //print("len " + BitConverter.ToUInt32(bytedata, 1));
|
|
|
- var worker = new List<Structure>();
|
|
|
- for (var read = 13; read < bytedata.Length; read += 20)
|
|
|
- {
|
|
|
- var id = BitConverter.ToUInt32(bytedata, read);
|
|
|
- var x = BitConverter.ToInt32(bytedata, read + 4);
|
|
|
- var y = BitConverter.ToInt32(bytedata, read + 8);
|
|
|
- var ticks = BitConverter.ToInt64(bytedata, read + 12);
|
|
|
- //print($"coord accid {accid} locid {locid} {id} x {x} y{y} ticks {ticks}");
|
|
|
-
|
|
|
- //WorkerController.markers.ElementAt(1).marker.transform.position = new Vector3(x, 0.5f, y);
|
|
|
-
|
|
|
- //WorkerController.TestStructures
|
|
|
- worker.Add(new Structure
|
|
|
- {
|
|
|
- id = id,
|
|
|
- coord_x = x,
|
|
|
- coord_y = y,
|
|
|
- ts = new DateTime(ticks),
|
|
|
- acc_id = accid,
|
|
|
- location_id = locid,
|
|
|
- zone_id = 1
|
|
|
- });
|
|
|
- }
|
|
|
- player.Workers[accid] = worker;
|
|
|
- player.end_send[accid] = true;
|
|
|
- }
|
|
|
-
|
|
|
public void Myping(byte[] bytedata)
|
|
|
{
|
|
|
last_ping_time = timer;
|
|
@@ -190,7 +151,7 @@ public class Client : MonoBehaviour
|
|
|
|
|
|
List<byte> list = new List<byte>();
|
|
|
list.Add(30);
|
|
|
- list.Add((byte)1);
|
|
|
+ list.Add(1);
|
|
|
SendEnqueue(list.ToArray());
|
|
|
}
|
|
|
|
|
@@ -229,21 +190,21 @@ public class Client : MonoBehaviour
|
|
|
}
|
|
|
|
|
|
//пакет с 1 параметром = 1
|
|
|
- public static void SendOneBytePacket(int num)
|
|
|
+ public static void SendOneBytePacket(byte num)
|
|
|
{
|
|
|
SendOneByteParamPacket(num, 1);
|
|
|
}
|
|
|
|
|
|
- public static void SendOneByteTwoParamsPacket(int num, int param1, int param2)
|
|
|
+ public static void SendOneByteTwoParamsPacket(byte num, byte param1, byte param2)
|
|
|
{
|
|
|
- byte[] data = { (Byte)num, (Byte)param1, (byte)param2 };
|
|
|
+ byte[] data = { num, param1, param2 };
|
|
|
SendEnqueue(data);
|
|
|
}
|
|
|
|
|
|
- public static void SendThreeParamsIntPacket(int num, int param1, int param2, int param3)
|
|
|
+ public static void SendThreeParamsIntPacket(byte num, int param1, int param2, int param3)
|
|
|
{
|
|
|
List<byte> list = new List<byte>();
|
|
|
- list.Add((Byte)num);
|
|
|
+ list.Add(num);
|
|
|
list.AddRange(BitConverter.GetBytes(param1));
|
|
|
list.AddRange(BitConverter.GetBytes(param2));
|
|
|
list.AddRange(BitConverter.GetBytes(param3));
|
|
@@ -251,17 +212,17 @@ public class Client : MonoBehaviour
|
|
|
}
|
|
|
|
|
|
//пакет с 1 1-байтовым параметром
|
|
|
- public static void SendOneByteParamPacket(int num, int param)
|
|
|
+ public static void SendOneByteParamPacket(byte num, byte param)
|
|
|
{
|
|
|
- byte[] data = { (Byte)num, (Byte)param };
|
|
|
+ byte[] data = { num, param };
|
|
|
byteSend(data);
|
|
|
}
|
|
|
|
|
|
//пакет с 1 4-байтовым параметром
|
|
|
- public static void SendTwoByteParamPacket(int num, ushort param)
|
|
|
+ public static void SendTwoByteParamPacket(byte num, ushort param)
|
|
|
{
|
|
|
List<byte> list = new List<byte>();
|
|
|
- list.Add((Byte)num);
|
|
|
+ list.Add(num);
|
|
|
list.AddRange(BitConverter.GetBytes(param));
|
|
|
SendEnqueue(list.ToArray());
|
|
|
}
|
|
@@ -328,15 +289,35 @@ public class Client : MonoBehaviour
|
|
|
}
|
|
|
else
|
|
|
leng = PacketLength[num];
|
|
|
- //Debug.Log("GetLength " + leng);
|
|
|
return leng;
|
|
|
}
|
|
|
|
|
|
int maximumPacketsPerUpdate = 50;
|
|
|
+ public string tempflag;
|
|
|
|
|
|
- public void Start()
|
|
|
+ public static void printBytes(byte[] bytedata, bool sent = false)
|
|
|
{
|
|
|
- entered = true;
|
|
|
+ var prefix = "";
|
|
|
+ if (!sent)
|
|
|
+ {
|
|
|
+ if (!ShowPackets)
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (!ShowPacketsSent)
|
|
|
+ return;
|
|
|
+ prefix = "sent ";
|
|
|
+ }
|
|
|
+ byte[] newbuf = new byte[bytedata.Length];
|
|
|
+ Array.Copy(bytedata, newbuf, bytedata.Length);
|
|
|
+
|
|
|
+ string mygg = prefix + "printBytes: "; //Печатаем отправленные байты!
|
|
|
+ foreach (byte b in newbuf)
|
|
|
+ {
|
|
|
+ mygg += b + " ";
|
|
|
+ }
|
|
|
+ Debug.Log(newbuf.Length + " bytes: " + mygg);
|
|
|
}
|
|
|
|
|
|
public void Update()
|
|
@@ -356,8 +337,7 @@ public class Client : MonoBehaviour
|
|
|
StartConnectFlag = true;
|
|
|
}
|
|
|
}
|
|
|
- if (AuthorizationController.send)
|
|
|
- SendLogin();
|
|
|
+
|
|
|
int from_connect = DateTime.Now.Subtract(connect_started).Seconds;
|
|
|
|
|
|
if (from_connect > 7 && from_connect != DateTime.Now.Second && account_id == 0)
|
|
@@ -374,41 +354,32 @@ public class Client : MonoBehaviour
|
|
|
}
|
|
|
int offset = 0;
|
|
|
int totallen = 0;
|
|
|
- int num = (int)packetbytes[0];
|
|
|
+ int num = packetbytes[0];
|
|
|
int leng = GetLength(packetbytes, packetbytes[0], 0);
|
|
|
|
|
|
if (leng <= packetbytes.Length)
|
|
|
{
|
|
|
while (offset < packetbytes.Length)
|
|
|
{
|
|
|
- num = (int)packetbytes[offset];
|
|
|
+ num = packetbytes[offset];
|
|
|
leng = GetLength(packetbytes, num, offset);
|
|
|
totallen += leng;
|
|
|
print("num " + num + " leng " + leng);
|
|
|
byte[] newpack = new byte[leng];
|
|
|
Array.Copy(packetbytes, offset, newpack, 0, leng);
|
|
|
offset += leng;
|
|
|
- if (ShowPackets)
|
|
|
- {
|
|
|
- if (num != 30) //не пакет пинга
|
|
|
- {
|
|
|
- //string mygg = packets[num].Method.Name + " "; //Печатаем принятые байты!
|
|
|
- string mygg = "";
|
|
|
- foreach (byte b in newpack)
|
|
|
- {
|
|
|
- mygg += b + " ";
|
|
|
- }
|
|
|
- //Debug.Log("ticks " + DateTime.Now.Ticks / 10000 + " timesince " + Time.timeSinceLevelLoad +" "+ newpack.Length + " bytes packet: " + mygg);
|
|
|
- Debug.Log(newpack.Length + " bytes packet: " + mygg);
|
|
|
- }
|
|
|
- }
|
|
|
+ printBytes(newpack);
|
|
|
processedCount++;
|
|
|
- packets[num](newpack);//запустить OnReceive функцию
|
|
|
- //print("copy "+num);
|
|
|
+ packets[num](newpack); //запустить OnReceive функцию
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- //Debug.Log("Update End");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Start()
|
|
|
+ {
|
|
|
+ tempflag = "";
|
|
|
+ entered = true;
|
|
|
}
|
|
|
|
|
|
public void LateUpdate()
|
|
@@ -693,21 +664,7 @@ public class Client : MonoBehaviour
|
|
|
Debug.LogError("tosend null");
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- if (ShowPacketsSent)
|
|
|
- {
|
|
|
- byte[] newbuf = new Byte[tosend.Length];
|
|
|
- Array.Copy(tosend, newbuf, tosend.Length);
|
|
|
- if (newbuf[0] != 30) //не пакет пинга
|
|
|
- {
|
|
|
- string mygg = "sent: "; //Печатаем отправленные байты!
|
|
|
- foreach (byte b in newbuf)
|
|
|
- {
|
|
|
- mygg += b + " ";
|
|
|
- }
|
|
|
- print(newbuf.Length + " bytes: " + mygg);
|
|
|
- }
|
|
|
- }
|
|
|
+ printBytes(tosend, true);
|
|
|
|
|
|
#if UNITY_WEBGL
|
|
|
byte[] webglbuf = new Byte[tosend.Length];
|
|
@@ -801,24 +758,6 @@ public class Client : MonoBehaviour
|
|
|
SendEnqueue(list.ToArray());
|
|
|
}
|
|
|
|
|
|
- public void UsersReceive(byte[] bytedata)
|
|
|
- {
|
|
|
- print("UsersReceive");
|
|
|
- var player = PlayerController.instance;
|
|
|
- player.users = new List<User>();
|
|
|
- int read = 5;
|
|
|
- while (read < bytedata.Length)
|
|
|
- {
|
|
|
- var id = BitConverter.ToUInt32(bytedata, read);
|
|
|
- var lenname = bytedata[read + 4];
|
|
|
- var name = Encoding.UTF8.GetString(bytedata, read + 5, lenname);
|
|
|
- read += 5 + lenname;
|
|
|
- //print($"user received id {id} lenname {lenname} name {name}");
|
|
|
- player.users.Add(new User { id = id, name = name });
|
|
|
- }
|
|
|
- player.users_load = true;
|
|
|
- }
|
|
|
-
|
|
|
public static byte[] ConstructVariablePacket(byte num, byte[] vardatabytes)
|
|
|
{
|
|
|
List<byte> bytedata = new List<byte>();
|
|
@@ -829,90 +768,20 @@ public class Client : MonoBehaviour
|
|
|
return bytedata.ToArray();
|
|
|
}
|
|
|
|
|
|
- public static void BeaconsSave(List<Beacon> beacons)
|
|
|
+ public void MessageReceiver(string data)
|
|
|
{
|
|
|
- if (beacons.Count > 0)
|
|
|
- {
|
|
|
- List<byte> list = new List<byte>();
|
|
|
+ var expl = data.Split(' ');
|
|
|
|
|
|
- foreach (var b in beacons)
|
|
|
- {
|
|
|
- list.AddRange(BitConverter.GetBytes(b.id));
|
|
|
- list.AddRange(BitConverter.GetBytes(b.x));
|
|
|
- list.AddRange(BitConverter.GetBytes(b.y));
|
|
|
- list.AddRange(BitConverter.GetBytes(b.z));
|
|
|
- list.AddRange(BitConverter.GetBytes(b.minor));
|
|
|
- list.AddRange(BitConverter.GetBytes(b.major));
|
|
|
- if (b.uuid.Length != 36)
|
|
|
- b.uuid = "00000000-0000-0000-0000-000000000000";
|
|
|
- list.AddRange(Encoding.UTF8.GetBytes(b.uuid));
|
|
|
- Debug.Log($"id {b.id} enabled {b.enabled} byte: {b.byteEnabled}");
|
|
|
- list.Add(b.byteEnabled);
|
|
|
- }
|
|
|
-
|
|
|
- var tosend = ConstructVariablePacket(49, list.ToArray());
|
|
|
- SendEnqueue(tosend);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static void BeaconsSaveAll()
|
|
|
- {
|
|
|
- GameObject editor = GameObject.Find("Canvas").transform.GetChild(3).gameObject;
|
|
|
- if (editor != null)
|
|
|
- {
|
|
|
- var e2c = editor.GetComponent<EditorController>();
|
|
|
- var beacons = e2c.Beacons;
|
|
|
- if (beacons.Count > 0)
|
|
|
- {
|
|
|
- BeaconsSave(beacons);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ var accname = expl[0];
|
|
|
+ account_id = Convert.ToUInt32(expl[1]);
|
|
|
+ company_id = Convert.ToUInt32(expl[2]);
|
|
|
|
|
|
- public void BeaconsReceive(byte[] bytedata)
|
|
|
- {
|
|
|
- int read = 5;
|
|
|
- int num = 0;
|
|
|
- var player = PlayerController.instance;
|
|
|
- uint location_id = 0;
|
|
|
- while (read < bytedata.Length)
|
|
|
- {
|
|
|
- num++;
|
|
|
- var id = BitConverter.ToUInt32(bytedata, read);
|
|
|
- var uuid = Encoding.UTF8.GetString(bytedata, read + 4, 36);
|
|
|
- var x = BitConverter.ToSingle(bytedata, read + 40);
|
|
|
- var y = BitConverter.ToSingle(bytedata, read + 44);
|
|
|
- var z = BitConverter.ToSingle(bytedata, read + 48);
|
|
|
- var minor = BitConverter.ToUInt16(bytedata, read + 52);
|
|
|
- var major = BitConverter.ToUInt16(bytedata, read + 54);
|
|
|
- var enabled = bytedata[read + 56];
|
|
|
- var lid = BitConverter.ToUInt32(bytedata, read+57);
|
|
|
- var mac = Encoding.UTF8.GetString(bytedata, read + 61, 17);
|
|
|
- var label = BitConverter.ToUInt16(bytedata, read + 78);
|
|
|
- print($"beacon received id {id} uuid {uuid} x {x} y {y} z {z} minor {minor} major {major} enabled {enabled} location {lid} mac{mac} label {label}");
|
|
|
- read += 80;
|
|
|
-
|
|
|
- //PlayerController.Beacons.Add(new Beacon { id = id, location_id = lid, uuid = uuid, x = x, y = y, z = z, minor = minor, major = major, enabled = enabled == 1 ? true : false });
|
|
|
- player.locations[lid].beacons.Add(new Beacon { vagrant_label=label, id = id, location_id = lid, uuid = uuid, x = x, y = y, z = z, minor = minor, major = major, mac = mac, enabled = enabled == 1 ? true : false });
|
|
|
- location_id = lid;
|
|
|
- }
|
|
|
- print("Beacons loaded: " + num);
|
|
|
- //PlayerController.beacons_load = true;
|
|
|
- if (location_id != 0) player.load_location_elements[location_id].beacons = true;
|
|
|
- }
|
|
|
-
|
|
|
- public static void BeaconsRequest(uint locationId)
|
|
|
- {
|
|
|
- Debug.Log("BeaconsRequest connected " + instance.connected+ " locationId "+ locationId);
|
|
|
- //if (!connected)
|
|
|
- //{
|
|
|
- // return;
|
|
|
- //}
|
|
|
- PlayerController.instance.locations[locationId].beacons = new List<Beacon>();
|
|
|
- SendFourByteParamPacket(48, locationId);
|
|
|
+ AuthorizationController.success = true;
|
|
|
+
|
|
|
+ tempflag = data+" "+ accname;
|
|
|
}
|
|
|
|
|
|
- public void SendLogin()
|
|
|
+ public void SendLogin(string login, string passwordToEdit)
|
|
|
{
|
|
|
if (!connected)
|
|
|
{
|
|
@@ -923,11 +792,7 @@ public class Client : MonoBehaviour
|
|
|
if (!login_sent)
|
|
|
{
|
|
|
login_sent = true;
|
|
|
- Debug.Log("SendLogin");
|
|
|
- string passwordToEdit = AuthorizationController.password;// "8*9Efc2%";
|
|
|
- string login = AuthorizationController.login;//"vsheiko";
|
|
|
string pass = GetMD5Hash(passwordToEdit);
|
|
|
-
|
|
|
byte[] bpass = Encoding.UTF8.GetBytes(pass);
|
|
|
byte[] blogin = Encoding.UTF8.GetBytes(login);
|
|
|
List<byte> list = new List<byte>();
|
|
@@ -942,13 +807,58 @@ public class Client : MonoBehaviour
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //public void OnGUI()
|
|
|
- //{
|
|
|
- // if (GUI.Button(new Rect(250, 280, 160, 64), "test Save Walls"))
|
|
|
- // {
|
|
|
- // Zone.LoadByLocationId(22);
|
|
|
- // //Wall.LoadWallsByLocationId(1);
|
|
|
- // //Wall.SaveAll();
|
|
|
- // }
|
|
|
- //}
|
|
|
+ #region test functions
|
|
|
+ public void ImgReceive(byte[] bytedata)
|
|
|
+ {
|
|
|
+ var imglen = bytedata.Length - 5;
|
|
|
+ print("ImgReceive " + imglen);
|
|
|
+ byte[] rawdata = new byte[imglen];
|
|
|
+
|
|
|
+ Array.Copy(bytedata, 5, rawdata, 0, imglen);
|
|
|
+ //printBytes(rawdata);
|
|
|
+ Texture2D tex = new Texture2D(2, 2);
|
|
|
+ tex.LoadImage(rawdata);
|
|
|
+ //tex.LoadRawTextureData(rawdata);
|
|
|
+
|
|
|
+ var rawimg = GameObject.Find("RawImage");
|
|
|
+ if (rawimg != null)
|
|
|
+ {
|
|
|
+ print("rawimg2 not null");
|
|
|
+ //var testimg = (Texture2D)Resources.Load("Image/map1", typeof(Texture2D));
|
|
|
+ //TestImageSend(testimg);
|
|
|
+ rawimg.GetComponent<RawImage>().texture = tex;
|
|
|
+ //rawimg.SetActive(false);
|
|
|
+ //rawimg.SetActive(true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void TestImageSend(Texture2D tex)
|
|
|
+ {
|
|
|
+ byte[] bdata = new byte[0];
|
|
|
+ var data = ConstructVariablePacket(55, bdata);
|
|
|
+ SendEnqueue(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnGUI()
|
|
|
+ {
|
|
|
+ //if (tempflag != "")
|
|
|
+ //{
|
|
|
+ if (GUI.Button(new Rect(250, 280, 160, 64), "Hello from web page! "+ tempflag))
|
|
|
+ {
|
|
|
+ var rawimg = GameObject.Find("RawImage");
|
|
|
+ if (rawimg != null)
|
|
|
+ {
|
|
|
+ print("rawimg not null");
|
|
|
+ var testimg = (Texture2D)Resources.Load("Image/map1", typeof(Texture2D));
|
|
|
+ TestImageSend(testimg);
|
|
|
+ // rawimg.GetComponent<RawImage>().material.mainTexture = testimg;
|
|
|
+
|
|
|
+ }
|
|
|
+ // Zone.LoadByLocationId(22);
|
|
|
+ //Wall.LoadWallsByLocationId(1);
|
|
|
+ //Wall.SaveAll();
|
|
|
+ }
|
|
|
+ //}
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
}
|