public void Start() { api1 = new Api1(); api1.http_server = this; requests_queue = new List <ApiRequest>(); t = new Thread(ThreadProc); t.Start(this); }
public void HandleRequest(ref ApiRequest request) { List <string> request_chunks = new List <string>(request.context.Request.Url.AbsolutePath.Trim('/').Split('/')); List <string> chunks = request_chunks.GetRange(2, request_chunks.Count - 2); request.context.Response.StatusCode = 501; switch (chunks[0]) { /*case "network": * switch (chunks.Count) * { * case 1: * request.string_response = JsonConvert.SerializeObject(Api1.SerializableNetwork.Current(), Formatting.Indented); * request.context.Response.StatusCode = 200; * break; * } * break;*/ case "object": int instance_id; GameObject gobj = null; if (chunks.Count > 1 && int.TryParse(chunks[1], out instance_id)) { gobj = (GameObject)FindObjectFromInstanceID(instance_id); } switch (chunks.Count) { case 1: request.string_response = Api1.GetTree(); request.context.Response.StatusCode = 200; break; case 2: if (gobj != null) { request.string_response = Api1.GameObjectToJSON(gobj); request.context.Response.StatusCode = 200; } else if (chunks[1] == "load") { string bundle_path = request.context.Request.QueryString["bundle_path"]; Settings.Log("Loading: " + bundle_path); Mod.DynamicBundle imported_obj; imported_obj = JsonConvert.DeserializeObject <Mod.DynamicBundle>(File.ReadAllText(bundle_path)); Settings.Log("deserialized"); imported_obj.Load(); Settings.Log("creating runtime objects"); request.context.Response.StatusCode = 200; /*ObjImporter oi = new ObjImporter(); * gobj = oi.LoadObject(request.context.Request.QueryString["obj_path"], request.context.Request.QueryString["texture_path"]); * request.string_response = Api1.GameObjectToJSON(gobj); * request.context.Response.StatusCode = 200;*/ } break; case 3: if (chunks[1] == "find") { string find_string = ""; try { find_string = Encoding.Unicode.GetString(Convert.FromBase64String(chunks[2])); } catch { request.context.Response.StatusCode = 422; } if (request.context.Response.StatusCode != 422) { gobj = GameObject.Find(find_string); if (gobj == null) { request.context.Response.StatusCode = 404; } else { request.string_response = Api1.GameObjectToJSON(gobj); request.context.Response.StatusCode = 200; } } } else if (chunks[2] == "position") { Vector3 position = gobj.transform.position; var qs = request.context.Request.QueryString; foreach (string key in qs.AllKeys) { switch (key) { case "x": position.x = float.Parse(qs["x"]); break; case "y": position.y = float.Parse(qs["y"]); break; case "z": position.z = float.Parse(qs["z"]); break; } } gobj.transform.position = position; request.context.Response.StatusCode = 200; } else if (chunks[2] == "localEulerAngles") { Vector3 localEulerAngles = gobj.transform.localEulerAngles; var qs = request.context.Request.QueryString; foreach (string key in qs.AllKeys) { switch (key) { case "x": localEulerAngles.x = float.Parse(qs["x"]); break; case "y": localEulerAngles.y = float.Parse(qs["y"]); break; case "z": localEulerAngles.z = float.Parse(qs["z"]); break; } } gobj.transform.localEulerAngles = localEulerAngles; request.context.Response.StatusCode = 200; } if (chunks[2] == "destroy") { Settings.Log("Destroying GameObject " + gobj.name); GameObject.DestroyImmediate(gobj); request.context.Response.StatusCode = 200; } break; } break; } request.done = true; }