public RemoteInspectorServer(ushort port = 8080) : base(port) { var viewEngine = new NustacheViewEngine("Views"); Use("/ping", (request, response, path) => { if (path != "") { return(false); } response.Send("pong"); return(true); }); Use("/api/go", new GameObjectExplorerService()); Use((request, response, relativePath) => { Dictionary <string, object> content = null; var found = false; Exception error = null; UnityMainThreadDispatcher.Instance().EnqueueAndWait(() => { try { // remove leading '/' relativePath = relativePath.Substring(1); if (string.IsNullOrEmpty(relativePath)) { content = SceneExplorer.InspectRoot(); found = true; } else { var gameObject = GameObject.Find(relativePath); if (gameObject == null) { found = false; } else { found = true; content = SceneExplorer.InspectGameObject(gameObject); var idString = request.QueryString.Get("id"); var componentId = 0; var findTargetId = !string.IsNullOrEmpty(idString) && int.TryParse(idString, out componentId); if (findTargetId) { content["componentDetails"] = SceneExplorer.CreateRemoteView(gameObject .GetComponents <Component>() .First(comp => comp.GetInstanceID() == componentId)); var properties = (List <SceneExplorer.RemoteProperty>)((IDictionary <string, object>)content[ "componentDetails"])["properties"]; for (int i = 0; i < properties.Count; i++) { var property = properties[i]; property.value = JsonConvert.SerializeObject(property.value, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, ReferenceLoopHandling = ReferenceLoopHandling.Ignore, ContractResolver = new IgnorePropertiesContractResolver() }); properties[i] = property; } } } } } catch (Exception exception) { error = exception; } }); if (error != null) { LogException(error); response.SendException(error); return(true); } if (!found) { return(false); } try { const string viewPath = "index.html"; // var contentJson = JsonConvert.SerializeObject( content, Formatting.Indented, new JsonSerializerSettings // { // NullValueHandling = NullValueHandling.Ignore, // ReferenceLoopHandling = ReferenceLoopHandling.Ignore, // ContractResolver = new IgnorePropertiesContractResolver() // } ); // // var renderContext = JsonHelper.Deserialize( contentJson ) as Dictionary<string, object>; var responseContent = viewEngine.Render(viewPath, content); response.Send(responseContent, MimeTypes.Text.Html); return(true); } catch (ViewNotFoundException) { LogError("Main interface view not found"); return(false); } }); Use(new StaticFileServer("Assets")); }
public bool HandleRequest(HttpListenerRequest request, HttpListenerResponse response, string relativePath) { var objectPath = relativePath.Substring(1); Dictionary <string, object> content = null; Exception error = null; var failContent = new Dictionary <string, object>(); UnityMainThreadDispatcher.Instance().EnqueueAndWait(() => { try { if (objectPath == "") { content = SceneExplorer.InspectRoot(); } else { var gameObject = GameObject.Find(objectPath); if (gameObject == null) { error = new ObjectNotFoundException(objectPath); return; } content = SceneExplorer.InspectGameObject(gameObject); var idString = request.QueryString.Get("id"); var componentId = 0; var findTargetId = !string.IsNullOrEmpty(idString) && int.TryParse(idString, out componentId); if (findTargetId) { content["component"] = SceneExplorer.CreateRemoteView(gameObject .GetComponents <Component>() .First(comp => comp.GetInstanceID() == componentId)); } } } catch (Exception exception) { error = exception; } }); if (error != null) { response.SendJson(JsonReponse.Error(error.Message)); } else if (failContent.Count > 0) { response.SendJson(JsonReponse.Fail(failContent)); } else { response.SendJson(JsonReponse.Success(content)); } return(true); }