//---------------------------------------------------------------------------------------------------- public static void Query(RequestAdapter req, string path) { // if we have already cached the query then use that if (req.CachedContext != null) { var cache = req.CachedContext as CachedQuery; var query = cache.Q; if (cache.Reselect) { query.Select(); req.Respond(JsonReflector.Reflect(query.Execute())); query.Clear(); } else { req.Respond(JsonReflector.Reflect(query.Execute())); } } // otherwise execute it else { var query = new Query(path, Unium.Root).Select(); req.Respond(JsonReflector.Reflect(query.Execute())); } }
//---------------------------------------------------------------------------------------------------- public static void HandlerStatus(RequestAdapter req, string path) { req.Respond(JsonReflector.Reflect(new { FPS = 1.0f / Time.smoothDeltaTime, RunningTime = Time.realtimeSinceStartup, Scene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name, })); }
//---------------------------------------------------------------------------------------------------- static void DownloadFileWWW(RequestAdapter req, string filepath) { #if UNITY_2017_3_OR_NEWER var www = UnityWebRequest.Get(filepath); var asyncOp = www.SendWebRequest(); while (!asyncOp.isDone) { Thread.Sleep(10); } #if UNITY_2020_1_OR_NEWER if (www.result != UnityWebRequest.Result.Success) #else if (www.isNetworkError || www.isHttpError) #endif { req.Reject(ResponseCode.InternalServerError); } else { req.SetContentType(GetMimeType(filepath)); req.Respond(www.downloadHandler.data); } #else var data = new WWW(filepath); while (!data.isDone) { Thread.Sleep(10); } if (string.IsNullOrEmpty(data.error)) { req.SetContentType(GetMimeType(filepath)); req.Respond(data.bytes); } else { req.Reject(ResponseCode.InternalServerError); } #endif }
//---------------------------------------------------------------------------------------------------- static void DownloadFile(RequestAdapter req, string filepath) { #if UNITY_ANDROID /* * var data = new WWW( filepath ); * * while( !data.isDone ) * { * Thread.Sleep( 10 ); * } * * if( string.IsNullOrEmpty( data.error ) ) * { * req.SetContentType( GetMimeType( filepath ) ); * req.Respond( data.bytes ); * } * else * { * req.Reject( ResponseCode.InternalServerError ); * } */ req.Reject(ResponseCode.InternalServerError); #else // System.IO if ((File.GetAttributes(filepath) & FileAttributes.Directory) == FileAttributes.Directory) { // list contents of directory var files = from c in Directory.GetFileSystemEntries(filepath) select Path.GetFileName(c); req.Respond(JsonReflector.Reflect(files.ToArray())); } else { // dump bytes req.SetContentType(GetMimeType(filepath)); req.Respond(File.ReadAllBytes(filepath)); } #endif }
//---------------------------------------------------------------------------------------------------- static void DownloadFileNative(RequestAdapter req, string filepath) { // System.IO if ((File.GetAttributes(filepath) & FileAttributes.Directory) == FileAttributes.Directory) { // list contents of directory var files = from c in Directory.GetFileSystemEntries(filepath) select Path.GetFileName(c); req.Respond(JsonReflector.Reflect(files.ToArray())); } else { // dump bytes req.SetContentType(GetMimeType(filepath)); req.Respond(File.ReadAllBytes(filepath)); } }
//---------------------------------------------------------------------------------------------------- public static void HandlerAbout(RequestAdapter req, string path) { req.Respond(JsonReflector.Reflect(new { Unium = Unium.Version.ToString(2), Unity = Application.unityVersion, Mono = Environment.Version.ToString(), IsEditor = Application.isEditor, Product = Application.productName, Company = Application.companyName, Version = Application.version, IPAddress = Util.DetectPublicIPAddress(), FPS = 1.0f / Time.smoothDeltaTime, RunningTime = Time.realtimeSinceStartup, Scene = SceneManager.GetActiveScene().name, })); }
//---------------------------------------------------------------------------------------------------- static void DownloadFileWWW(RequestAdapter req, string filepath) { var data = new WWW(filepath); while (!data.isDone) { Thread.Sleep(10); } if (string.IsNullOrEmpty(data.error)) { req.SetContentType(GetMimeType(filepath)); req.Respond(data.bytes); } else { req.Reject(ResponseCode.InternalServerError); } }
private static IEnumerator LoadScene(RequestAdapter req, string name) { var asyncOp = SceneManager.LoadSceneAsync(name, LoadSceneMode.Single); if (asyncOp == null) { req.Reject(ResponseCode.NotFound); } else { asyncOp.allowSceneActivation = true; while (asyncOp.isDone == false) { yield return(asyncOp); } req.Respond(JsonReflector.Reflect(new { scene = name })); } }
//---------------------------------------------------------------------------------------------------- public static void HandlerScene(RequestAdapter req, string path) { // if path empty then list all loaded scenes if (string.IsNullOrEmpty(path) || path.Length <= 1) { var scenes = new List <object>(); #if UNITY_EDITOR foreach (var scene in EditorBuildSettings.scenes) { scenes.Add(new { name = Path.GetFileNameWithoutExtension(scene.path), path = scene.path, enabled = scene.enabled }); } #else for (int i = 0; i < SceneManager.sceneCount; i++) { var scene = SceneManager.GetSceneAt(i); scenes.Add(new { name = scene.name, path = scene.path, enabled = true }); } #endif req.Respond(JsonReflector.Reflect(scenes.ToArray())); } // otherwise load scene else { UniumComponent.Singleton.StartCoroutine(LoadScene(req, path.Substring(1))); } }
static IEnumerator TakeScreenshot(RequestAdapter req, string path) { // render cameras to render texture var screenshot = new RenderTexture(Screen.width, Screen.height, 24); if (screenshot == null) { req.Reject(ResponseCode.InternalServerError); yield break; } screenshot.filterMode = FilterMode.Point; // no filtering as we are using the same resolution as the screen yield return(new WaitForEndOfFrame()); for (int i = 0; i < Camera.allCamerasCount; ++i) { var camera = Camera.allCameras[i]; var previousRTex = camera.targetTexture; camera.targetTexture = screenshot; camera.Render(); camera.targetTexture = previousRTex; } yield return(null); // read from render texture to memory (ReadPixels will read from current active render texture) var pixels = new Texture2D(screenshot.width, screenshot.height, TextureFormat.RGB24, false); if (pixels == null) { req.Reject(ResponseCode.InternalServerError); yield break; } var previousActiveRTex = RenderTexture.active; RenderTexture.active = screenshot; pixels.ReadPixels(new Rect(0, 0, screenshot.width, screenshot.height), 0, 0); pixels.Apply(); RenderTexture.active = previousActiveRTex; // return png var bytes = pixels.EncodeToPNG(); if (bytes != null) { req.SetContentType("image/png"); req.Respond(bytes); } else { req.Reject(ResponseCode.InternalServerError); } }
public static void DebugOutput(RequestAdapter req, string path) { req.SetContentType("text/plain"); req.Respond(string.Join(Environment.NewLine, sLog.ToArray())); }
static IEnumerator TakeScreenshot(RequestAdapter req) { var query = Util.ParseQueryString(req.Query); var width = query["width"] != null?Int32.Parse(query["width"]) : -1; var height = query["height"] != null?Int32.Parse(query["height"]) : -1; var scale = query["scale"] != null?float.Parse(query["scale"], System.Globalization.CultureInfo.InvariantCulture) : -1.0f; // render cameras to render texture int texWidth = width > 0 ? width : Screen.width; int texHeight = height > 0 ? height : Screen.height; if (scale > 0.0f) { texWidth = (int)(texWidth * scale); texHeight = (int)(texHeight * scale); } var screenshot = new RenderTexture(texWidth, texHeight, 24); if (screenshot == null) { req.Reject(ResponseCode.InternalServerError); yield break; } screenshot.filterMode = FilterMode.Point; // no filtering as we are using the same resolution as the screen yield return(new WaitForEndOfFrame()); for (int i = 0; i < Camera.allCamerasCount; ++i) { var camera = Camera.allCameras[i]; var previousRTex = camera.targetTexture; camera.targetTexture = screenshot; camera.Render(); camera.targetTexture = previousRTex; } yield return(null); // read from render texture to memory (ReadPixels will read from current active render texture) var pixels = new Texture2D(screenshot.width, screenshot.height, TextureFormat.RGB24, false); if (pixels == null) { req.Reject(ResponseCode.InternalServerError); yield break; } var previousActiveRTex = RenderTexture.active; RenderTexture.active = screenshot; pixels.ReadPixels(new Rect(0, 0, screenshot.width, screenshot.height), 0, 0); pixels.Apply(); RenderTexture.active = previousActiveRTex; screenshot.Release(); UnityEngine.Object.Destroy(screenshot); // return png var bytes = pixels.EncodeToPNG(); if (bytes != null) { req.SetContentType("image/png"); req.Respond(bytes); } else { req.Reject(ResponseCode.InternalServerError); } UnityEngine.Object.Destroy(pixels); }