| 1 | using AllocsFixes.JSON;
 | 
|---|
| 2 | using System;
 | 
|---|
| 3 | using System.Collections.Generic;
 | 
|---|
| 4 | using System.Net;
 | 
|---|
| 5 | 
 | 
|---|
| 6 | namespace AllocsFixes.NetConnections.Servers.Web.API
 | 
|---|
| 7 | {
 | 
|---|
| 8 |     class GetRawEntitiesList : WebAPI
 | 
|---|
| 9 |     {
 | 
|---|
| 10 |         public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel)
 | 
|---|
| 11 |         {
 | 
|---|
| 12 |             JSONArray entitiesJsResult = new JSONArray ();
 | 
|---|
| 13 | 
 | 
|---|
| 14 |             // Yes: hardcoded... even hacking the web interface won't show this data.  You -must- be an admin.
 | 
|---|
| 15 |             if (permissionLevel != 0) {
 | 
|---|
| 16 |                 try {
 | 
|---|
| 17 |                     foreach (object base_entity in GameManager.Instance.World.Entities.list) {
 | 
|---|
| 18 |                         try {
 | 
|---|
| 19 |                             Entity entity = (Entity)base_entity;
 | 
|---|
| 20 | 
 | 
|---|
| 21 |                             if ((entity.entityType == EntityType.Animal) || (base_entity.GetType () == typeof(EntityAnimal)) || (base_entity.GetType ().ToString ().ToLower ().Contains ("animal")))
 | 
|---|
| 22 |                             {
 | 
|---|
| 23 |                                 EntityAnimal ea = (EntityAnimal)entity;
 | 
|---|
| 24 | 
 | 
|---|
| 25 |                                 Vector3i position = new Vector3i (entity.GetPosition ());
 | 
|---|
| 26 | 
 | 
|---|
| 27 |                                 JSONObject jsonPOS = new JSONObject ();
 | 
|---|
| 28 |                                 jsonPOS.Add("x", new JSONNumber (position.x));
 | 
|---|
| 29 |                                 jsonPOS.Add("y", new JSONNumber (position.y));
 | 
|---|
| 30 |                                 jsonPOS.Add("z", new JSONNumber (position.z));
 | 
|---|
| 31 | 
 | 
|---|
| 32 |                                 JSONObject pJson = new JSONObject ();
 | 
|---|
| 33 |                                 pJson.Add ("id", new JSONNumber (entity.entityId));
 | 
|---|
| 34 |                                 pJson.Add ("type", new JSONString ("animal"));
 | 
|---|
| 35 |                                 pJson.Add ("tag", new JSONString (entity.tag ?? ""));
 | 
|---|
| 36 |                                 pJson.Add ("model", new JSONString ((entity.emodel == null) ? "?" : entity.emodel.name ?? "?"));
 | 
|---|
| 37 |                                 pJson.Add ("alive", new JSONBoolean (ea.IsAlive ()));
 | 
|---|
| 38 |                                 pJson.Add ("health", new JSONNumber (ea.Health));
 | 
|---|
| 39 | 
 | 
|---|
| 40 |                                 if (PetesUtils.ValidText (ea.EntityName))
 | 
|---|
| 41 |                                     pJson.Add ("name", new JSONString (ea.EntityName));
 | 
|---|
| 42 |                                 else
 | 
|---|
| 43 |                                     pJson.Add ("name", new JSONString ("animal class #" + entity.entityClass.ToString ()));
 | 
|---|
| 44 | 
 | 
|---|
| 45 |                                 pJson.Add ("position", jsonPOS);
 | 
|---|
| 46 | 
 | 
|---|
| 47 |                                 entitiesJsResult.Add (pJson);
 | 
|---|
| 48 |                             }
 | 
|---|
| 49 |                             else if ((entity.entityType == EntityType.Zombie) || (base_entity.GetType () == typeof(EntityEnemy))) {
 | 
|---|
| 50 |                                 EntityEnemy ee = (EntityEnemy)entity;
 | 
|---|
| 51 | 
 | 
|---|
| 52 |                                 Vector3i position = new Vector3i (entity.GetPosition ());
 | 
|---|
| 53 | 
 | 
|---|
| 54 |                                 JSONObject jsonPOS = new JSONObject ();
 | 
|---|
| 55 |                                 jsonPOS.Add ("x", new JSONNumber (position.x));
 | 
|---|
| 56 |                                 jsonPOS.Add ("y", new JSONNumber (position.y));
 | 
|---|
| 57 |                                 jsonPOS.Add ("z", new JSONNumber (position.z));
 | 
|---|
| 58 | 
 | 
|---|
| 59 |                                 JSONObject pJson = new JSONObject ();
 | 
|---|
| 60 |                                 pJson.Add ("id", new JSONNumber (entity.entityId));
 | 
|---|
| 61 |                                 pJson.Add ("type", new JSONString ("enemy"));
 | 
|---|
| 62 |                                 pJson.Add ("tag", new JSONString (entity.tag ?? ""));
 | 
|---|
| 63 |                                 pJson.Add ("model", new JSONString ((entity.emodel == null) ? "?" : entity.emodel.name ?? "?"));
 | 
|---|
| 64 |                                 pJson.Add ("alive", new JSONBoolean (ee.IsAlive ()));
 | 
|---|
| 65 |                                 pJson.Add ("health", new JSONNumber (ee.Health));
 | 
|---|
| 66 | 
 | 
|---|
| 67 |                                 if (PetesUtils.ValidText (ee.EntityName))
 | 
|---|
| 68 |                                     pJson.Add ("name", new JSONString (ee.EntityName));
 | 
|---|
| 69 |                                 else
 | 
|---|
| 70 |                                     pJson.Add ("name", new JSONString ("enemy class #" + entity.entityClass.ToString ()));
 | 
|---|
| 71 | 
 | 
|---|
| 72 |                                 pJson.Add ("position", jsonPOS);
 | 
|---|
| 73 | 
 | 
|---|
| 74 |                                 entitiesJsResult.Add (pJson);
 | 
|---|
| 75 |                             }
 | 
|---|
| 76 |                             else if ((entity.entityType == EntityType.Player) || (base_entity.GetType () == typeof(EntityPlayer))) {
 | 
|---|
| 77 |                                 EntityPlayer ep = (EntityPlayer)entity;
 | 
|---|
| 78 | 
 | 
|---|
| 79 |                                 Vector3i position = new Vector3i (entity.GetPosition ());
 | 
|---|
| 80 | 
 | 
|---|
| 81 |                                 JSONObject jsonPOS = new JSONObject ();
 | 
|---|
| 82 |                                 jsonPOS.Add ("x", new JSONNumber (position.x));
 | 
|---|
| 83 |                                 jsonPOS.Add ("y", new JSONNumber (position.y));
 | 
|---|
| 84 |                                 jsonPOS.Add ("z", new JSONNumber (position.z));
 | 
|---|
| 85 | 
 | 
|---|
| 86 |                                 JSONObject pJson = new JSONObject ();
 | 
|---|
| 87 |                                 pJson.Add ("id", new JSONNumber (entity.entityId));
 | 
|---|
| 88 |                                 pJson.Add ("type", new JSONString ("player"));
 | 
|---|
| 89 |                                 pJson.Add ("tag", new JSONString (entity.tag ?? ""));
 | 
|---|
| 90 |                                 pJson.Add ("model", new JSONString ((entity.emodel == null) ? "?" : entity.emodel.name ?? "?"));
 | 
|---|
| 91 |                                 pJson.Add ("alive", new JSONBoolean (ep.IsAlive ()));
 | 
|---|
| 92 |                                 pJson.Add ("score", new JSONNumber (ep.Score));
 | 
|---|
| 93 |                                 pJson.Add ("health", new JSONNumber (ep.Health));
 | 
|---|
| 94 |                                 pJson.Add ("zombiekills", new JSONNumber (ep.KilledZombies));
 | 
|---|
| 95 |                                 pJson.Add ("stamina", new JSONNumber (ep.Stamina));
 | 
|---|
| 96 | 
 | 
|---|
| 97 |                                 if (PetesUtils.ValidText (ep.EntityName))
 | 
|---|
| 98 |                                     pJson.Add ("name", new JSONString (ep.EntityName));
 | 
|---|
| 99 |                                 else
 | 
|---|
| 100 |                                     pJson.Add ("name", new JSONString ("player class #" + entity.entityClass.ToString ()));
 | 
|---|
| 101 | 
 | 
|---|
| 102 |                                 pJson.Add("position", jsonPOS);
 | 
|---|
| 103 | 
 | 
|---|
| 104 |                                 entitiesJsResult.Add(pJson);
 | 
|---|
| 105 |                             }
 | 
|---|
| 106 |                             else { // note: animals are an EntityAnimal but are being generated with the wrong entityType ID, so get caught here
 | 
|---|
| 107 |                                 Vector3i position = new Vector3i (entity.GetPosition ());
 | 
|---|
| 108 | 
 | 
|---|
| 109 |                                 JSONObject jsonPOS = new JSONObject ();
 | 
|---|
| 110 |                                 jsonPOS.Add ("x", new JSONNumber (position.x));
 | 
|---|
| 111 |                                 jsonPOS.Add ("y", new JSONNumber (position.y));
 | 
|---|
| 112 |                                 jsonPOS.Add ("z", new JSONNumber (position.z));
 | 
|---|
| 113 | 
 | 
|---|
| 114 |                                 JSONObject pJson = new JSONObject ();
 | 
|---|
| 115 |                                 pJson.Add ("id", new JSONNumber (entity.entityId));
 | 
|---|
| 116 |                                 pJson.Add ("type", new JSONString ("unknown #" + ((int)entity.entityType).ToString ()));
 | 
|---|
| 117 |                                 pJson.Add ("tag", new JSONString (entity.tag ?? ""));
 | 
|---|
| 118 | 
 | 
|---|
| 119 |                                 string true_name = entity.name;
 | 
|---|
| 120 |                                 
 | 
|---|
| 121 |                                 try {
 | 
|---|
| 122 |                                     int iTries = 0;
 | 
|---|
| 123 |                                     UnityEngine.GameObject go = entity.gameObject;
 | 
|---|
| 124 | 
 | 
|---|
| 125 |                                     while (true_name == "GameObject") {
 | 
|---|
| 126 |                                         true_name = go.name;
 | 
|---|
| 127 |                                         go = go.gameObject;
 | 
|---|
| 128 | 
 | 
|---|
| 129 |                                         if (++iTries > 10)
 | 
|---|
| 130 |                                             break;
 | 
|---|
| 131 |                                     }
 | 
|---|
| 132 |                                 }
 | 
|---|
| 133 |                                 catch { }
 | 
|---|
| 134 | 
 | 
|---|
| 135 |                                 string true_emodel = (entity.emodel == null) ? "?" : entity.emodel.name;
 | 
|---|
| 136 | 
 | 
|---|
| 137 |                                 try {
 | 
|---|
| 138 |                                     int iTries = 0;
 | 
|---|
| 139 |                                     UnityEngine.GameObject go = entity.emodel.gameObject;
 | 
|---|
| 140 | 
 | 
|---|
| 141 |                                     while (true_emodel == "GameObject") {
 | 
|---|
| 142 |                                         true_emodel = go.name;
 | 
|---|
| 143 |                                         go = go.gameObject;
 | 
|---|
| 144 | 
 | 
|---|
| 145 |                                         if (++iTries > 10)
 | 
|---|
| 146 |                                             break;
 | 
|---|
| 147 |                                     }
 | 
|---|
| 148 |                                 }
 | 
|---|
| 149 |                                 catch { }
 | 
|---|
| 150 | 
 | 
|---|
| 151 |                                 pJson.Add ("model", new JSONString (true_emodel));
 | 
|---|
| 152 | 
 | 
|---|
| 153 |                                 if (PetesUtils.ValidText (true_name))
 | 
|---|
| 154 |                                     pJson.Add("name", new JSONString (base_entity.GetType ().ToString () + ": " + true_name));
 | 
|---|
| 155 |                                 else
 | 
|---|
| 156 |                                     pJson.Add("name", new JSONString (base_entity.GetType ().ToString () + " class #" + entity.entityClass.ToString ()));
 | 
|---|
| 157 | 
 | 
|---|
| 158 |                                 pJson.Add ("position", jsonPOS);
 | 
|---|
| 159 | 
 | 
|---|
| 160 |                                 entitiesJsResult.Add (pJson);
 | 
|---|
| 161 |                             }
 | 
|---|
| 162 |                         }
 | 
|---|
| 163 |                         catch { }
 | 
|---|
| 164 |                     }
 | 
|---|
| 165 |                 }
 | 
|---|
| 166 |                 catch (Exception ex) {
 | 
|---|
| 167 |                     entitiesJsResult.Add (new JSONString ("Error: " + ex.GetType ().ToString () + " - " + ex.Message + "\r\n" + ex.StackTrace));
 | 
|---|
| 168 |                 }
 | 
|---|
| 169 |             }
 | 
|---|
| 170 | 
 | 
|---|
| 171 |             WriteJSON (resp, entitiesJsResult);
 | 
|---|
| 172 |         }
 | 
|---|
| 173 |     }
 | 
|---|
| 174 | }
 | 
|---|