Exemplo n.º 1
0
        //----------------------------------------------------------------------------------------------------

        static void UploadFile(RequestAdapter req, string filepath)
        {
            try
            {
                File.WriteAllBytes(filepath, req.Body);
                req.Reject(ResponseCode.OK);
            }
            catch (Exception)
            {
                req.Reject(ResponseCode.InternalServerError);
            }
        }
        public static void HandleBind(RequestAdapter req, string path)
        {
            if (sMatchQuery.IsMatch(path) == false)
            {
                req.Reject(ResponseCode.BadRequest);
                return;
            }

            // select events

            var query = new Query(path + "=event", Unium.Root).Select();


            // bind

            var bound   = 0;
            var target  = query.SearchPath.Target;
            var adapter = req as RequestAdapterSocket;

            foreach (var obj in query.Selected)
            {
                try
                {
                    var eventInfo = obj.GetType().GetEvent(target);

                    if (eventInfo != null)
                    {
                        adapter.Socket.Bind(adapter.Message, obj, eventInfo);
                        bound++;
                    }
                }
                catch (Exception e)
                {
                    UniumComponent.Warn(string.Format("Failed to get bind to event '{0}' - {1}", target, e.Message));
                }
            }


            // not found if none bound

            if (bound == 0)
            {
                req.Reject(ResponseCode.NotFound);
                return;
            }
        }
Exemplo n.º 3
0
        //----------------------------------------------------------------------------------------------------

        public static void Serve(RequestAdapter req, string path)
        {
            try
            {
                // remove initial

                if (path[0] == '/')
                {
                    path = path.Substring(1);
                }


                // find "drive"

                var useWWW = Application.platform == RuntimePlatform.Android;
                var drive  = sPaths["root"];

                foreach (var key in sPaths.Keys)
                {
                    if (path.StartsWith(key + "/"))
                    {
                        drive  = sPaths[key];
                        path   = path.Substring(key.Length + 1);
                        useWWW = Application.platform == RuntimePlatform.Android && key != "persistent";

                        break;
                    }
                }


                // combine drive path and file path to get local path

                var filepath = Path.Combine(drive, path);

                // do the thing

                if (req.Body == null)
                {
                    if (useWWW)
                    {
                        DownloadFileWWW(req, filepath);
                    }
                    else
                    {
                        DownloadFileNative(req, filepath);
                    }
                }
                else
                {
                    UploadFile(req, filepath);
                }
            }
            catch (Exception)
            {
                req.Reject(ResponseCode.NotFound);
            }
        }
Exemplo n.º 4
0
        //----------------------------------------------------------------------------------------------------

        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
        }
Exemplo n.º 5
0
        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.Reject(ResponseCode.OK);
            }
        }
Exemplo n.º 6
0
 public void Dispatch(RequestAdapter request)
 {
     try
     {
         Handler(request, RelativePath(request));
     }
     catch (Exception e)
     {
         request.Reject(ResponseCode.BadRequest);
         UniumComponent.Error(e.Message);
     }
 }
        public static void Execute(RequestAdapter adapter, string path)
        {
            var socket = (adapter as RequestAdapterSocket).Socket;
            var root   = new Dictionary <string, object>()
            {
                { "socket", new UniumSocket.SocketCommands(socket) }
            };

            var q = new Query(adapter.Path, root).Select();
            var r = q.Execute();

            if (r.Count == 0)
            {
                adapter.Reject(ResponseCode.NotFound);
            }
        }
Exemplo n.º 8
0
        //----------------------------------------------------------------------------------------------------

        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);
            }
        }
Exemplo n.º 9
0
        //----------------------------------------------------------------------------------------------------

        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
        }
Exemplo n.º 10
0
        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);
            }
        }
Exemplo n.º 11
0
        //----------------------------------------------------------------------------------------------------
        //

        public static void NotFound(RequestAdapter req, string path)
        {
            req.Reject(ResponseCode.NotFound);
        }
Exemplo n.º 12
0
        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);
        }