Ejemplo n.º 1
0
    // NOT on main thread
    protected void AcceptCallback(System.IAsyncResult ar)
    {
        int threadId = System.Threading.Thread.CurrentThread.ManagedThreadId;

        UnityEngine.Debug.Log("Accept thread id: " + threadId);
        System.Net.Sockets.TcpListener listener = (System.Net.Sockets.TcpListener)ar.AsyncState;
        System.Net.Sockets.TcpClient   client   = listener.EndAcceptTcpClient(ar);

        UnityEngine.Debug.Log("thread id " + threadId + " accepted client " + client.Client.RemoteEndPoint);
        UnityEngine.Debug.Log("thread id " + threadId + " beginning read from client " + client.Client.RemoteEndPoint);

        AltClientSocketHandler clientHandler =
            new AltClientSocketHandler(client,
                                       ClientSocketHandlerDelegate,
                                       SeparatorString,
                                       Encoding);

        System.Threading.Thread clientThread = new System.Threading.Thread(clientHandler.Run);
        ClientHandlerThreads.Add(new AltSocketClientThreadHolder(clientThread, clientHandler));
        clientThread.Start();
        UnityEngine.Debug.Log("Client thread started");

        if (ClientCount < maxClients)
        {
            UnityEngine.Debug.Log("client handler threads less than max clients. Listening again");
            ListenForConnection();
        }
        else
        {
            UnityEngine.Debug.Log(System.String.Format("Max number of clients reached ({0}), stopping listening", maxClients));
            StopListeningForConnections();
        }
    }
Ejemplo n.º 2
0
    public System.Collections.IEnumerator TakeScreenshot(AltClientSocketHandler handler)
    {
        yield return(new UnityEngine.WaitForEndOfFrame());

        var screenshot  = UnityEngine.ScreenCapture.CaptureScreenshotAsTexture();
        var bytesPNG    = UnityEngine.ImageConversion.EncodeToPNG(screenshot);
        var pngAsString = Convert.ToBase64String(bytesPNG);

        handler.SendResponse(pngAsString);
    }
Ejemplo n.º 3
0
    public void SendResponse(AltClientSocketHandler handler)
    {
        AltUnityRunner._responseQueue.ScheduleResponse(delegate
        {
            string response = null;
            try
            {
                response = Execute();

            }
            catch (System.NullReferenceException exception)
            {
                UnityEngine.Debug.Log(exception);
                response = AltUnityRunner._altUnityRunner.errorNullRefferenceMessage;
            }
            catch (System.ArgumentException exception)
            {
                UnityEngine.Debug.Log(exception);
                response = AltUnityRunner._altUnityRunner.errorFailedToParseArguments;
            }
            catch (System.Reflection.TargetParameterCountException)
            {
                response = AltUnityRunner._altUnityRunner.errorIncorrectNumberOfParameters;
            }
            catch (Newtonsoft.Json.JsonException e)
            {
                UnityEngine.Debug.Log(e);
                response = AltUnityRunner._altUnityRunner.errorCouldNotParseJsonString;
            }
            catch (Assets.AltUnityTester.AltUnityDriver.ComponentNotFoundException e)
            {
                UnityEngine.Debug.Log(e);
                response = AltUnityRunner._altUnityRunner.errorComponentNotFoundMessage;
            }
            catch (Assets.AltUnityTester.AltUnityDriver.PropertyNotFoundException e)
            {
                UnityEngine.Debug.Log(e);
                response = AltUnityRunner._altUnityRunner.errorPropertyNotFoundMessage;
            }
            catch (System.Exception exception)
            {
                UnityEngine.Debug.Log(exception);
                response = AltUnityRunner._altUnityRunner.errorUnknownError + AltUnityRunner._altUnityRunner.requestSeparatorString + exception;
            }

            finally
            {
                handler.SendResponse(response);
            }
        });
    }
Ejemplo n.º 4
0
 public AltSocketClientThreadHolder(System.Threading.Thread thread, AltClientSocketHandler handler)
 {
     this.thread  = thread;
     this.handler = handler;
 }
Ejemplo n.º 5
0
    public System.Collections.IEnumerator TakeTexturedScreenshot(UnityEngine.Vector2 size, AltClientSocketHandler handler)
    {
        yield return(new UnityEngine.WaitForEndOfFrame());

        var screenshot = UnityEngine.ScreenCapture.CaptureScreenshotAsTexture();

        var response = new ScreenshotReadyCommand(screenshot, size).Execute();

        handler.SendResponse(response);
    }
Ejemplo n.º 6
0
    public System.Collections.IEnumerator HighLightSelectedObjectCorutine(UnityEngine.GameObject gameObject, UnityEngine.Color color, float width, UnityEngine.Vector2 size, AltClientSocketHandler handler)
    {
        destroyHightlight = false;
        UnityEngine.Renderer renderer = gameObject.GetComponent <UnityEngine.Renderer>();
        System.Collections.Generic.List <UnityEngine.Shader> originalShaders = new System.Collections.Generic.List <UnityEngine.Shader>();
        if (renderer != null)
        {
            foreach (var material in renderer.materials)
            {
                originalShaders.Add(material.shader);
                material.shader = outlineShader;
                material.SetColor("_OutlineColor", color);
                material.SetFloat("_OutlineWidth", width);
            }
            yield return(null);

            new GetScreenshotCommand(size, handler).Execute();
            yield return(null);

            for (var i = 0; i < renderer.materials.Length; i++)
            {
                renderer.materials[i].shader = originalShaders[0];
            }
        }
        else
        {
            var rectTransform = gameObject.GetComponent <UnityEngine.RectTransform>();
            if (rectTransform != null)
            {
                var panelHighlight = Instantiate(panelHightlightPrefab, rectTransform);
                panelHighlight.GetComponent <UnityEngine.UI.Image>().color = color;
                yield return(null);

                new GetScreenshotCommand(size, handler).Execute();
                while (!destroyHightlight)
                {
                    yield return(null);
                }
                Destroy(panelHighlight);
                destroyHightlight = false;
            }
            else
            {
                var response = new GetScreenshotCommand(size, handler).Execute();
                handler.SendResponse(response);
            }
        }
    }
Ejemplo n.º 7
0
    public void ClientSocketHandlerDidReadMessage(AltClientSocketHandler handler, string message)
    {
        string[]          separator = new string[] { requestSeparatorString };
        string[]          pieces    = message.Split(separator, System.StringSplitOptions.None);
        AltUnityComponent altComponent;
        AltUnityObject    altUnityObject;
        string            methodParameters;

        UnityEngine.Vector2 size;
        PLayerPrefKeyType   option;
        Command             command = null;

        try
        {
            switch (pieces[0])
            {
            case "findAllObjects":
                methodParameters = pieces[1] + requestSeparatorString + pieces[2];
                command          = new FindAllObjectsCommand(methodParameters);
                break;

            case "findObjectByName":
                methodParameters = pieces[1] + requestSeparatorString + pieces[2] + requestSeparatorString + pieces[3];
                command          = new FindObjectByNameCommand(methodParameters);
                break;

            case "findObjectWhereNameContains":
                methodParameters = pieces[1] + requestSeparatorString + pieces[2] + requestSeparatorString + pieces[3];
                command          = new FindObjectWhereNameContainsCommand(methodParameters);
                break;

            case "tapObject":
                altUnityObject = Newtonsoft.Json.JsonConvert.DeserializeObject <AltUnityObject>(pieces[1]);
                command        = new TapCommand(altUnityObject);
                break;

            case "findObjectsByName":
                methodParameters = pieces[1] + requestSeparatorString + pieces[2] + requestSeparatorString + pieces[3];
                command          = new FindObjectsByNameCommand(methodParameters);
                break;

            case "findObjectsWhereNameContains":
                methodParameters = pieces[1] + requestSeparatorString + pieces[2] + requestSeparatorString + pieces[3];
                command          = new FindObjectsWhereNameContainsCommand(methodParameters);
                break;

            case "getCurrentScene":
                command = new GetCurrentSceneCommand();
                break;

            case "findObjectByComponent":
                methodParameters = pieces[1] + requestSeparatorString + pieces[2] + requestSeparatorString + pieces[3] + requestSeparatorString + pieces[4];
                command          = new FindObjectByComponentCommand(methodParameters);
                break;

            case "findObjectsByComponent":
                methodParameters = pieces[1] + requestSeparatorString + pieces[2] + requestSeparatorString + pieces[3] + requestSeparatorString + pieces[4];
                command          = new FindObjectsByComponentCommand(methodParameters);
                break;

            case "getObjectComponentProperty":
                command = new GetComponentPropertyCommand(pieces[1], pieces[2]);
                break;

            case "setObjectComponentProperty":
                command = new SetObjectComponentPropertyCommand(pieces[1], pieces[2], pieces[3]);
                break;

            case "callComponentMethodForObject":
                command = new CallComponentMethodForObjectCommand(pieces[1], pieces[2]);
                break;

            case "closeConnection":
                UnityEngine.Debug.Log("Socket connection closed!");
                _socketServer.StartListeningForConnections();
                break;

            case "clickEvent":
                altUnityObject = Newtonsoft.Json.JsonConvert.DeserializeObject <AltUnityObject>(pieces[1]);
                command        = new ClickEventCommand(altUnityObject);
                break;

            case "tapScreen":
                command = new ClickOnScreenAtXyCommand(pieces[1], pieces[2]);
                break;

            case "dragObject":
                UnityEngine.Vector2 positionVector2 = Newtonsoft.Json.JsonConvert.DeserializeObject <UnityEngine.Vector2>(pieces[1]);
                altUnityObject = Newtonsoft.Json.JsonConvert.DeserializeObject <AltUnityObject>(pieces[2]);
                command        = new DragObjectCommand(positionVector2, altUnityObject);
                break;

            case "dropObject":
                UnityEngine.Vector2 positionDropVector2 = Newtonsoft.Json.JsonConvert.DeserializeObject <UnityEngine.Vector2>(pieces[1]);
                altUnityObject = Newtonsoft.Json.JsonConvert.DeserializeObject <AltUnityObject>(pieces[2]);
                command        = new DropObjectCommand(positionDropVector2, altUnityObject);
                break;

            case "pointerUpFromObject":
                altUnityObject = Newtonsoft.Json.JsonConvert.DeserializeObject <AltUnityObject>(pieces[1]);
                command        = new PointerUpFromObjectCommand(altUnityObject);
                break;

            case "pointerDownFromObject":
                altUnityObject = Newtonsoft.Json.JsonConvert.DeserializeObject <AltUnityObject>(pieces[1]);
                command        = new PointerDownFromObjectCommand(altUnityObject);
                break;

            case "pointerEnterObject":
                altUnityObject = Newtonsoft.Json.JsonConvert.DeserializeObject <AltUnityObject>(pieces[1]);
                command        = new PointerEnterObjectCommand(altUnityObject);
                break;

            case "pointerExitObject":
                altUnityObject = Newtonsoft.Json.JsonConvert.DeserializeObject <AltUnityObject>(pieces[1]);
                command        = new PointerExitObjectCommand(altUnityObject);
                break;

            case "tilt":
                UnityEngine.Vector3 vector3 = Newtonsoft.Json.JsonConvert.DeserializeObject <UnityEngine.Vector3>(pieces[1]);
                command = new TiltCommand(vector3);
                break;

            case "movingTouch":
                UnityEngine.Vector2 start2 = Newtonsoft.Json.JsonConvert.DeserializeObject <UnityEngine.Vector2>(pieces[1]);
                UnityEngine.Vector2 end2   = Newtonsoft.Json.JsonConvert.DeserializeObject <UnityEngine.Vector2>(pieces[2]);
                command = new SetMovingTouchCommand(start2, end2, pieces[3]);
                break;

            case "loadScene":
                command = new Assets.AltUnityTester.AltUnityServer.Commands.LoadSceneCommand(pieces[1]);
                break;

            case "setTimeScale":
                float timeScale = Newtonsoft.Json.JsonConvert.DeserializeObject <float>(pieces[1]);
                command = new SetTimeScaleCommand(timeScale);
                break;

            case "getTimeScale":
                command = new GetTimeScaleCommand();
                break;

            case "deletePlayerPref":
                command = new DeletePlayerPrefCommand();
                break;

            case "deleteKeyPlayerPref":
                command = new DeleteKeyPlayerPrefCommand(pieces[1]);
                break;

            case "setKeyPlayerPref":
                option  = (PLayerPrefKeyType)System.Enum.Parse(typeof(PLayerPrefKeyType), pieces[3]);
                command = new SetKeyPlayerPrefCommand(option, pieces[1], pieces[2]);
                break;

            case "getKeyPlayerPref":
                option  = (PLayerPrefKeyType)System.Enum.Parse(typeof(PLayerPrefKeyType), pieces[2]);
                command = new GetKeyPlayerPrefCommand(option, pieces[1]);
                break;

            case "actionFinished":
                command = new ActionFinishedCommand();
                break;

            case "getAllComponents":
                command = new GetAllComponentsCommand(pieces[1]);
                break;

            case "getAllFields":
                altComponent = Newtonsoft.Json.JsonConvert.DeserializeObject <AltUnityComponent>(pieces[2]);
                command      = new GetAllFieldsCommand(pieces[1], altComponent);
                break;

            case "getAllMethods":
                altComponent = Newtonsoft.Json.JsonConvert.DeserializeObject <AltUnityComponent>(pieces[1]);
                command      = new GetAllMethodsCommand(altComponent);
                break;

            case "getAllScenes":
                command = new GetAllScenesCommand();
                break;

            case "getAllCameras":
                command = new GetAllCamerasCommand();
                break;

            case "getScreenshot":
                size    = Newtonsoft.Json.JsonConvert.DeserializeObject <UnityEngine.Vector2>(pieces[1]);
                command = new GetScreenshotCommand(size, handler);
                break;

            case "hightlightObjectScreenshot":
                var id = System.Convert.ToInt32(pieces[1]);
                size    = Newtonsoft.Json.JsonConvert.DeserializeObject <UnityEngine.Vector2>(pieces[3]);
                command = new HighlightSelectedObjectCommand(id, pieces[2], size, handler);
                break;

            case "hightlightObjectFromCoordinatesScreenshot":
                var coordinates = Newtonsoft.Json.JsonConvert.DeserializeObject <UnityEngine.Vector2>(pieces[1]);
                size    = Newtonsoft.Json.JsonConvert.DeserializeObject <UnityEngine.Vector2>(pieces[3]);
                command = new HightlightObjectFromCoordinatesCommand(coordinates, pieces[2], size, handler);
                break;

            case "pressKeyboardKey":
                var piece = pieces[1];
                UnityEngine.KeyCode keycode = (UnityEngine.KeyCode)System.Enum.Parse(typeof(UnityEngine.KeyCode), piece);
                float power    = Newtonsoft.Json.JsonConvert.DeserializeObject <float>(pieces[2]);
                float duration = Newtonsoft.Json.JsonConvert.DeserializeObject <float>(pieces[3]);
                command = new HoldButtonCommand(keycode, power, duration);
                break;

            case "moveMouse":
                UnityEngine.Vector2 location = Newtonsoft.Json.JsonConvert.DeserializeObject <UnityEngine.Vector2>(pieces[1]);
                duration = Newtonsoft.Json.JsonConvert.DeserializeObject <float>(pieces[2]);
                command  = new MoveMouseCommand(location, duration);
                break;

            case "scrollMouse":
                var scrollValue = Newtonsoft.Json.JsonConvert.DeserializeObject <float>(pieces[1]);
                duration = Newtonsoft.Json.JsonConvert.DeserializeObject <float>(pieces[2]);
                command  = new ScrollMouseCommand(scrollValue, duration);
                break;

            case "findObject":
                methodParameters = pieces[1] + requestSeparatorString + pieces[2] + requestSeparatorString + pieces[3];
                command          = new FindObjectCommand(methodParameters);
                break;

            case "findObjects":
                methodParameters = pieces[1] + requestSeparatorString + pieces[2] + requestSeparatorString + pieces[3];
                command          = new FindObjectsCommand(methodParameters);
                break;

            case "findActiveObjectByName":
                methodParameters = pieces[1] + requestSeparatorString + pieces[2] + requestSeparatorString + pieces[3];
                command          = new FindActiveObjectsByNameCommand(methodParameters);
                break;

            case "enableLogging":
                var enableLogging = bool.Parse(pieces[1]);
                command = new EnableLoggingCommand(enableLogging);
                break;

            case "getText":
                altUnityObject = Newtonsoft.Json.JsonConvert.DeserializeObject <AltUnityObject>(pieces[1]);
                command        = new GetTextCommand(altUnityObject);
                break;

            case "setText":
                altUnityObject = Newtonsoft.Json.JsonConvert.DeserializeObject <AltUnityObject>(pieces[1]);
                command        = new SetTextCommand(altUnityObject, pieces[2]);
                break;

            case "getPNGScreenshot":
                command = new GetScreenshotPNGCommand(handler);
                break;


            default:
                command = new UnknowStringCommand();
                break;
            }
        }
        catch (Newtonsoft.Json.JsonException exception)
        {
            UnityEngine.Debug.Log(exception);
            handler.SendResponse(errorCouldNotParseJsonString);
        }
        if (command != null)
        {
            command.SendResponse(handler);
        }
    }