Beispiel #1
0
    void StartInteraction()
    {
        int hr = 0;

        try
        {
            // initialize Kinect sensor as needed
            hr = InteractionWrapper.InitKinectSensor();
            if (hr != 0)
            {
                throw new Exception("Initialization of Kinect sensor failed");
            }

            // initialize Kinect interaction
            hr = InteractionWrapper.InitKinectInteraction();
            if (hr != 0)
            {
                throw new Exception("Initialization of KinectInteraction failed");
            }

            // kinect interaction was successfully initialized
            interactionInited = true;
        }
        catch (DllNotFoundException ex)
        {
            Debug.LogError(ex.ToString());
            if (handGuiText != null)
            {
                handGuiText.GetComponent <GUIText>().text = "Please check the Kinect SDK installation.";
            }
        }
        catch (Exception ex)
        {
            string message = ex.Message + " - " + InteractionWrapper.GetNuiErrorString(hr);
            Debug.LogError(ex.ToString());

            if (handGuiText != null)
            {
                handGuiText.GetComponent <GUIText>().text = message;
            }

            return;
        }

//		// transform matrix - kinect to world
//		Quaternion quatTiltAngle = new Quaternion();
//		int sensorAngle = InteractionWrapper.GetKinectElevationAngle();
//		quatTiltAngle.eulerAngles = new Vector3(-sensorAngle, 0.0f, 0.0f);
//
//		float heightAboveHips = SensorHeight - 1.0f;
//		kinectToWorld.SetTRS(new Vector3(0.0f, heightAboveHips, 0.0f), quatTiltAngle, Vector3.one);

        // load cursor textures once
        gripHandTexture    = (Texture)Resources.Load("GripCursor");
        releaseHandTexture = (Texture)Resources.Load("ReleaseCursor");
        normalHandTexture  = (Texture)Resources.Load("HandCursor");

        // don't destroy the object on loading levels
        DontDestroyOnLoad(gameObject);
    }
Beispiel #2
0
 void OnApplicationQuit()
 {
     // uninitialize Kinect interaction
     if (interactionInited)
     {
         InteractionWrapper.FinishKinectInteraction();
         InteractionWrapper.ShutdownKinectSensor();
         interactionInited = false;
     }
 }
Beispiel #3
0
//	// resets the last valid right hand event
//	public void ResetRightHandEvent()
//	{
//		lastRightHandEvent = InteractionWrapper.InteractionHandEventType.None;
//	}

    //----------------------------------- end of public functions --------------------------------------//

    void Awake()
    {
        // get reference to gui texts for left/right hand
        handGuiText = GameObject.Find("HandGuiText");
        handCursor  = GameObject.Find("HandCursor");

        // ensure the needed dlls are in place
        if (InteractionWrapper.CheckKinectInteractionPresence())
        {
            // reload the same level
            Application.LoadLevel(Application.loadedLevel);
        }
    }
Beispiel #4
0
    void Update()
    {
        // start Kinect interaction as needed
        if (!interactionInited)
        {
            StartInteraction();

            if (!interactionInited)
            {
                Application.Quit();
                return;
            }
        }

        // update Kinect interaction
        if (InteractionWrapper.UpdateKinectInteraction() == 0)
        {
            //int lastSkeletonTrackingID = skeletonTrackingID;
            skeletonTrackingID = InteractionWrapper.GetSkeletonTrackingID();

            if (skeletonTrackingID != 0)
            {
                InteractionWrapper.InteractionHandEventType handEvent = InteractionWrapper.InteractionHandEventType.None;
                Vector4 handPos = Vector4.zero;
                //Vector4 shoulderPos = Vector4.zero;
                //Vector3 screenPos = Vector3.zero;

                // process left hand
                leftHandState   = InteractionWrapper.GetLeftHandState();
                handEvent       = (InteractionWrapper.InteractionHandEventType)InteractionWrapper.GetLeftHandEvent();
                isLeftHandPress = InteractionWrapper.GetLeftHandPressed();

                InteractionWrapper.GetLeftCursorPos(ref handPos);
                leftHandScreenPos.x = Mathf.Clamp01(handPos.x);
                leftHandScreenPos.y = 1.0f - Mathf.Clamp01(handPos.y);
                //leftHandScreenPos.z = Mathf.Clamp01(handPos.z);
                leftHandScreenPos.z = handPos.z;

                bool bLeftHandPrimaryNow  = (leftHandState & (uint)InteractionWrapper.NuiHandpointerState.PrimaryForUser) != 0;
                bool bRightHandPrimaryNow = (rightHandState & (uint)InteractionWrapper.NuiHandpointerState.PrimaryForUser) != 0;

                if ((bLeftHandPrimaryNow != isLeftHandPrimary) || (bRightHandPrimaryNow != isRightHandPrimary))
                {
                    if (controlMouseCursor && dragInProgress)
                    {
                        MouseControl.MouseRelease();
                        dragInProgress = false;
                    }

                    lastLeftHandEvent  = InteractionWrapper.InteractionHandEventType.Release;
                    lastRightHandEvent = InteractionWrapper.InteractionHandEventType.Release;
                }

                if (controlMouseCursor && (handEvent != lastLeftHandEvent))
                {
                    if (controlMouseDrag && !dragInProgress && (handEvent == InteractionWrapper.InteractionHandEventType.Grip))
                    {
                        dragInProgress = true;
                        MouseControl.MouseDrag();
                    }
                    else if (dragInProgress && (handEvent == InteractionWrapper.InteractionHandEventType.Release))
                    {
                        MouseControl.MouseRelease();
                        dragInProgress = false;
                    }
                }

                leftHandEvent = handEvent;
                if (handEvent != InteractionWrapper.InteractionHandEventType.None)
                {
                    lastLeftHandEvent = handEvent;
                }

                // if the hand is primary, set the cursor position
                if (bLeftHandPrimaryNow)
                {
                    isLeftHandPrimary = true;

                    // check for left hand click
                    float fClickDist = (leftHandScreenPos - lastLeftHandPos).magnitude;
                    if (!dragInProgress && fClickDist < InteractionWrapper.Constants.ClickMaxDistance)
                    {
                        if ((Time.realtimeSinceStartup - lastLeftHandTime) >= InteractionWrapper.Constants.ClickStayDuration)
                        {
                            if (!isLeftHandClick)
                            {
                                isLeftHandClick       = true;
                                leftHandClickProgress = 1f;

                                if (controlMouseCursor)
                                {
                                    MouseControl.MouseClick();

                                    isLeftHandClick       = false;
                                    leftHandClickProgress = 0f;
                                    lastLeftHandPos       = Vector3.zero;
                                    lastLeftHandTime      = Time.realtimeSinceStartup;
                                }
                            }
                        }
                        else
                        {
                            leftHandClickProgress = (Time.realtimeSinceStartup - lastLeftHandTime) / InteractionWrapper.Constants.ClickStayDuration;
                        }
                    }
                    else
                    {
                        isLeftHandClick       = false;
                        leftHandClickProgress = 0f;
                        lastLeftHandPos       = leftHandScreenPos;
                        lastLeftHandTime      = Time.realtimeSinceStartup;
                    }

                    if ((leftHandClickProgress < 0.7f) /**&& !isLeftHandPress*/)
                    {
                        cursorScreenPos = Vector3.Lerp(cursorScreenPos, leftHandScreenPos, smoothFactor * Time.deltaTime);
                    }
                    else
                    {
                        leftHandScreenPos = cursorScreenPos;
                    }

                    if (controlMouseCursor)
                    {
                        MouseControl.MouseMove(cursorScreenPos, debugText);
                    }
                }
                else
                {
                    isLeftHandPrimary = false;
                }

//				// check for left hand Pull-gesture
//				if(lastLeftHandEvent == InteractionWrapper.InteractionHandEventType.Grip)
//				{
//					if(!bPullLeftHandStarted)
//					{
//						if(leftHandScreenPos.z > pushWhenPressMoreThan)
//						{
//							bPullLeftHandStarted = true;
//							bPullLeftHandFinished = false;
//							pullLeftHandStartedAt = Time.realtimeSinceStartup;
//						}
//					}
//				}

                // check for left hand Push-gesture
                if (lastLeftHandEvent == InteractionWrapper.InteractionHandEventType.Grip)
                {
                    if (!bPushLeftHandStarted)
                    {
                        if (!isLeftHandPress)
                        {
                            bPushLeftHandStarted  = true;
                            bPushLeftHandFinished = false;
                            pushLeftHandStartedAt = Time.realtimeSinceStartup;
                        }
                    }
                }

//				if(lastLeftHandEvent == InteractionWrapper.InteractionHandEventType.Grip)
//				{
//					if(bPullLeftHandStarted)
//					{
//						if((Time.realtimeSinceStartup - pullLeftHandStartedAt) <= 1.5f)
//						{
//							if(leftHandScreenPos.z <= pullWhenPressLessThan)
//							{
//								bPullLeftHandFinished = true;
//								bPullLeftHandStarted = false;
//							}
//						}
//						else
//						{
//							bPullLeftHandStarted = false;
//						}
//					}
//				}
//				else
//				{
//					// no more hand grip
//					bPullLeftHandFinished = false;
//					bPullLeftHandStarted = false;
//				}

                if (lastLeftHandEvent == InteractionWrapper.InteractionHandEventType.Grip)
                {
                    if (bPushLeftHandStarted)
                    {
                        if ((Time.realtimeSinceStartup - pushLeftHandStartedAt) <= 1.5f)
                        {
                            if (isLeftHandPress)
                            {
                                bPushLeftHandFinished = true;
                                bPushLeftHandStarted  = false;
                            }
                        }
                        else
                        {
                            bPushLeftHandStarted = false;
                        }
                    }
                }
                else
                {
                    // no more hand release
                    bPushLeftHandFinished = false;
                    bPushLeftHandStarted  = false;
                }

                // process right hand
                rightHandState   = InteractionWrapper.GetRightHandState();
                handEvent        = (InteractionWrapper.InteractionHandEventType)InteractionWrapper.GetRightHandEvent();
                isRightHandPress = InteractionWrapper.GetRightHandPressed();

                InteractionWrapper.GetRightCursorPos(ref handPos);
                rightHandScreenPos.x = Mathf.Clamp01(handPos.x);
                rightHandScreenPos.y = 1.0f - Mathf.Clamp01(handPos.y);
                //rightHandScreenPos.z = Mathf.Clamp01(handPos.z);
                rightHandScreenPos.z = handPos.z;

                if (controlMouseCursor && (handEvent != lastRightHandEvent))
                {
                    if (controlMouseDrag && !dragInProgress && (handEvent == InteractionWrapper.InteractionHandEventType.Grip))
                    {
                        dragInProgress = true;
                        MouseControl.MouseDrag();
                    }
                    else if (dragInProgress && (handEvent == InteractionWrapper.InteractionHandEventType.Release))
                    {
                        MouseControl.MouseRelease();
                        dragInProgress = false;
                    }
                }

                rightHandEvent = handEvent;
                if (handEvent != InteractionWrapper.InteractionHandEventType.None)
                {
                    lastRightHandEvent = handEvent;
                }

                // if the hand is primary, set the cursor position
                if (bRightHandPrimaryNow)
                {
                    isRightHandPrimary = true;

                    // check for right hand click
                    float fClickDist = (rightHandScreenPos - lastRightHandPos).magnitude;
                    if (!dragInProgress && fClickDist < InteractionWrapper.Constants.ClickMaxDistance)
                    {
                        if ((Time.realtimeSinceStartup - lastRightHandTime) >= InteractionWrapper.Constants.ClickStayDuration)
                        {
                            if (!isRightHandClick)
                            {
                                isRightHandClick       = true;
                                rightHandClickProgress = 1f;

                                if (controlMouseCursor)
                                {
                                    MouseControl.MouseClick();

                                    isRightHandClick       = false;
                                    rightHandClickProgress = 0f;
                                    lastRightHandPos       = Vector3.zero;
                                    lastRightHandTime      = Time.realtimeSinceStartup;
                                }
                            }
                        }
                        else
                        {
                            rightHandClickProgress = (Time.realtimeSinceStartup - lastRightHandTime) / InteractionWrapper.Constants.ClickStayDuration;
                        }
                    }
                    else
                    {
                        isRightHandClick       = false;
                        rightHandClickProgress = 0f;
                        lastRightHandPos       = rightHandScreenPos;
                        lastRightHandTime      = Time.realtimeSinceStartup;
                    }

                    if ((rightHandClickProgress < 0.7f) /**&& !isRightHandPress*/)
                    {
                        cursorScreenPos = Vector3.Lerp(cursorScreenPos, rightHandScreenPos, smoothFactor * Time.deltaTime);
                    }
                    else
                    {
                        rightHandScreenPos = cursorScreenPos;
                    }

                    if (controlMouseCursor)
                    {
                        MouseControl.MouseMove(cursorScreenPos, debugText);
                    }
                }
                else
                {
                    isRightHandPrimary = false;
                }

//				// check for right hand Pull-gesture
//				if(lastRightHandEvent == InteractionWrapper.InteractionHandEventType.Grip)
//				{
//					if(!bPullRightHandStarted)
//					{
//						if(rightHandScreenPos.z > pushWhenPressMoreThan)
//						{
//							bPullRightHandStarted = true;
//							bPullRightHandFinished = false;
//							pullRightHandStartedAt = Time.realtimeSinceStartup;
//						}
//					}
//				}

                // check for right hand Push-gesture
                if (lastRightHandEvent == InteractionWrapper.InteractionHandEventType.Grip)
                {
                    if (!bPushRightHandStarted)
                    {
                        if (!isRightHandPress)
                        {
                            bPushRightHandStarted  = true;
                            bPushRightHandFinished = false;
                            pushRightHandStartedAt = Time.realtimeSinceStartup;
                        }
                    }
                }

//				if(lastRightHandEvent == InteractionWrapper.InteractionHandEventType.Grip)
//				{
//					if(bPullRightHandStarted)
//					{
//						if((Time.realtimeSinceStartup - pullRightHandStartedAt) <= 1.5f)
//						{
//							if(rightHandScreenPos.z <= pullWhenPressLessThan)
//							{
//								bPullRightHandFinished = true;
//								bPullRightHandStarted = false;
//							}
//						}
//						else
//						{
//							bPullRightHandStarted = false;
//						}
//					}
//				}
//				else
//				{
//					// no more hand grip
//					bPullRightHandFinished = false;
//					bPullRightHandStarted = false;
//				}

                if (lastRightHandEvent == InteractionWrapper.InteractionHandEventType.Grip)
                {
                    if (bPushRightHandStarted)
                    {
                        if ((Time.realtimeSinceStartup - pushRightHandStartedAt) <= 1.5f)
                        {
                            if (isRightHandPress)
                            {
                                bPushRightHandFinished = true;
                                bPushRightHandStarted  = false;
                            }
                        }
                        else
                        {
                            bPushRightHandStarted = false;
                        }
                    }
                }
                else
                {
                    // no more hand grip
                    bPushRightHandFinished = false;
                    bPushRightHandStarted  = false;
                }
            }
            else
            {
                leftHandState  = 0;
                rightHandState = 0;

                isLeftHandPrimary  = false;
                isRightHandPrimary = false;

                isLeftHandPress  = false;
                isRightHandPress = false;

                leftHandEvent  = InteractionWrapper.InteractionHandEventType.None;
                rightHandEvent = InteractionWrapper.InteractionHandEventType.None;

                lastLeftHandEvent  = InteractionWrapper.InteractionHandEventType.Release;
                lastRightHandEvent = InteractionWrapper.InteractionHandEventType.Release;

                if (controlMouseCursor && dragInProgress)
                {
                    MouseControl.MouseRelease();
                    dragInProgress = false;
                }
            }
        }
    }
Beispiel #5
0
    void StartInteraction()
    {
        int hr = 0;

        try {
            // initialize Kinect sensor as needed
            hr = InteractionWrapper.InitKinectSensor((int)InteractionWrapper.Constants.ColorImageResolution, (int)InteractionWrapper.Constants.DepthImageResolution, InteractionWrapper.Constants.IsNearMode);
            if (hr != 0)
            {
                throw new Exception("Initialization of Kinect sensor failed");
            }

            // initialize Kinect interaction
            hr = InteractionWrapper.InitKinectInteraction();
            if (hr != 0)
            {
                throw new Exception("Initialization of KinectInteraction failed");
            }

            // kinect interaction was successfully initialized
            instance          = this;
            interactionInited = true;
        } catch (DllNotFoundException ex) {
            Debug.LogError(ex.ToString());
            if (debugText != null)
            {
                debugText.GetComponent <GUIText> ().text = "Please check the Kinect SDK installation.";
            }
        } catch (Exception ex) {
            string message = ex.Message + " - " + InteractionWrapper.GetNuiErrorString(hr);
            Debug.LogError(ex.ToString());
            Debug.Log("Kinect não conectado! Desabilitando o component InteractonManager...");
            GetComponent <InteractionManager> ().enabled = false;
            handCursor.SetActive(false);

            if (debugText != null)
            {
                debugText.GetComponent <GUIText> ().text = message;
            }

            return;
        }

//		// transform matrix - kinect to world
//		Quaternion quatTiltAngle = new Quaternion();
//		int sensorAngle = InteractionWrapper.GetKinectElevationAngle();
//		quatTiltAngle.eulerAngles = new Vector3(-sensorAngle, 0.0f, 0.0f);
//
//		float heightAboveHips = SensorHeight - 1.0f;
//		kinectToWorld.SetTRS(new Vector3(0.0f, heightAboveHips, 0.0f), quatTiltAngle, Vector3.one);

//		// load cursor textures once
//		if(!gripHandTexture)
//		{
//			gripHandTexture = (Texture)Resources.Load("GripCursor");
//		}
//		if(!releaseHandTexture)
//		{
//			releaseHandTexture = (Texture)Resources.Load("ReleaseCursor");
//		}
//		if(!normalHandTexture)
//		{
//			normalHandTexture = (Texture)Resources.Load("HandCursor");
//		}

        // don't destroy the object on loading levels
        DontDestroyOnLoad(gameObject);
    }
Beispiel #6
0
 // returns the user ID at the given index
 // the index must be between 0 and (usersCount - 1)
 public uint GetUserIdAt(int index)
 {
     return(InteractionWrapper.GetSkeletonTrackingID((uint)index));
 }
Beispiel #7
0
 // returns the number of Kinect users
 public int GetUsersCount()
 {
     return(InteractionWrapper.GetInteractorsCount());
 }
Beispiel #8
0
 // sets new user ID (primary skeleton ID) to be used by the native wrapper
 public void SetUserId(uint userId)
 {
     InteractionWrapper.SetSkeletonTrackingID(userId);
 }
Beispiel #9
0
        static void Main(string[] args)
        {
            try
            {
                try
                {
                    try
                    {
                        x1 = short.Parse(args[0]);
                        y1 = short.Parse(args[1]);
                        x2 = short.Parse(args[2]);
                        y2 = short.Parse(args[3]);
                        if (x1 > x2 || y1 > y2)
                        {
                            throw new Exception();
                        }
                        try
                        {
                            File.Open(args[5], FileMode.Append, FileAccess.Write).Dispose();
                            logFilePath = args[5];
                        }
                        catch
                        { }
                    }
                    catch (OverflowException)
                    {
                        throw new Exception("Entire watched zone should be inside the map");
                    }
                    catch
                    {
                        throw new Exception("Parameters: <leftX> <topY> <rightX> <bottomY> [logFilePath] ; all in range -32768..32767");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    finishCTS.Cancel();
                    return;
                }
                logger = new Logger(finishCTS.Token, logFilePath);
                string fingerprint = Guid.NewGuid().ToString("N");
                cache = new ChunkCache(x1, y1, x2, y2, logger.LogLine);
                bool initialMapStateSaved = false;
                saveThread.Start();
                filename = string.Format("pixels_({0};{1})-({2};{3})_{4:yyyy.MM.dd_HH-mm}.bin", x1, y1, x2, y2, DateTime.Now);
                do
                {
                    try
                    {
                        using (InteractionWrapper wrapper = new InteractionWrapper(fingerprint, logger.LogLine, true))
                        {
                            cache.Wrapper = wrapper;
                            if (!initialMapStateSaved)
                            {
                                Task.Run(() =>
                                {
                                    initialMapStateSaved = true;
                                    cache.DownloadChunks();
                                    using (FileStream fileStream = File.Open(filename, FileMode.Create, FileAccess.Write))
                                    {
                                        using (BinaryWriter writer = new BinaryWriter(fileStream))
                                        {
                                            writer.Write(x1);
                                            writer.Write(y1);
                                            writer.Write(x2);
                                            writer.Write(y2);
                                            writer.Write(DateTime.Now.ToBinary());
                                            for (short y = y1; y <= y2; y++)
                                            {
                                                for (short x = x1; x <= x2; x++)
                                                {
                                                    writer.Write((byte)cache.GetPixelColor(x, y));
                                                }
                                            }
                                        }
                                    }
                                    logger.LogLine("Chunk data is saved to file", MessageGroup.TechInfo);
                                    lockingStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.None);
                                });
                            }

                            wrapper.OnPixelChanged += (o, e) =>
                            {
                                short x = PixelMap.ConvertToAbsolute(e.Chunk.Item1, e.Pixel.Item1);
                                short y = PixelMap.ConvertToAbsolute(e.Chunk.Item2, e.Pixel.Item2);
                                if (x <= x2 && x >= x1 && y <= y2 && y >= y1)
                                {
                                    logger.LogPixel("Received pixel update:", MessageGroup.PixelInfo, x, y, e.Color);
                                    lock (listLockObj)
                                    {
                                        updates.Add((x, y, e.Color));
                                    }
                                }
                            };
                            wrapper.StartListening();
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.LogLine($"Unhandled exception: {ex.Message}", MessageGroup.Error);
                    }
                } while (true);
            }
            finally
            {
                finishCTS.Cancel();
                Thread.Sleep(1000);
                finishCTS.Dispose();
                logger?.Dispose();
                if (saveThread.IsAlive)
                {
                    saveThread.Interrupt();
                }
            }
        }
Beispiel #10
0
        private static void Main(string[] args)
        {
            try
            {
                if (args.Length == 1)
                {
                    try
                    {
                        SaveFingerprint(Guid.Parse(args[0]));
                        Console.WriteLine("Fingerprint is saved, now you can relauch bot with needed parameters");
                    }
                    catch
                    {
                        Console.WriteLine("You should pass correct 128-bit fingerprint (GUID)");
                    }
                    return;
                }
                ushort           width, height;
                PlacingOrderMode order = PlacingOrderMode.Random;
                notificationMode = CaptchaNotificationMode.Browser;
                try
                {
                    try
                    {
                        leftX       = short.Parse(args[0]);
                        topY        = short.Parse(args[1]);
                        fingerprint = GetFingerprint();
                        string logFilePath = null;
                        if (args.Length > 3)
                        {
                            notificationMode = CaptchaNotificationMode.None;
                            string upper = args[3].ToUpper();
                            if (upper.Contains('B'))
                            {
                                notificationMode |= CaptchaNotificationMode.Browser;
                            }
                            if (upper.Contains('S'))
                            {
                                notificationMode |= CaptchaNotificationMode.Sound;
                            }
                            if (args.Length > 4)
                            {
                                defendMode = args[4].ToUpper() == "Y";

                                if (args.Length > 5)
                                {
                                    switch (args[5].ToUpper())
                                    {
                                    case "R":
                                        order = PlacingOrderMode.FromRight;
                                        break;

                                    case "L":
                                        order = PlacingOrderMode.FromLeft;
                                        break;

                                    case "T":
                                        order = PlacingOrderMode.FromTop;
                                        break;

                                    case "B":
                                        order = PlacingOrderMode.FromBottom;
                                        break;
                                    }

                                    if (args.Length > 6)
                                    {
                                        try
                                        {
                                            File.OpenWrite(args[6]).Dispose();
                                            logFilePath = args[6];
                                        }
                                        catch
                                        { }
                                    }
                                }
                            }
                        }
                        logger      = new Logger(finishCTS.Token, logFilePath);
                        imagePixels = ImageProcessing.PixelColorsByUri(args[2], logger.LogLine);
                        checked
                        {
                            width  = (ushort)imagePixels.GetLength(0);
                            height = (ushort)imagePixels.GetLength(1);
                            short check;
                            check = (short)(leftX + width);
                            check = (short)(topY + height);
                        }
                    }
                    catch (OverflowException)
                    {
                        throw new Exception("Entire image should be inside the map");
                    }
                    catch (WebException)
                    {
                        throw new Exception("Cannot download image");
                    }
                    catch (ArgumentException)
                    {
                        throw new Exception("Cannot convert image");
                    }
                    catch (IOException)
                    {
                        throw new Exception("Fingerprint is not saved, pass it from browser as only parameter to app to save before usage");
                    }
                    catch
                    {
                        throw new Exception("Parameters: <leftX> <topY> <imageURI> [notificationMode: N/B/S/BS = B] [defendMode: Y/N = N] [buildFrom L/R/T/B/RND = RND] [logFileName = none]");
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    finishCTS.Cancel();
                    return;
                }
                IEnumerable <int> allY           = Enumerable.Range(0, height);
                IEnumerable <int> allX           = Enumerable.Range(0, width);
                Pixel[]           nonEmptyPixels = allX.
                                                   SelectMany(X => allY.Select(Y =>
                                                                               (X: (short)(X + leftX), Y: (short)(Y + topY), C: imagePixels[X, Y]))).
                                                   Where(xy => xy.C != PixelColor.None).ToArray();
                switch (order)
                {
                case PlacingOrderMode.FromLeft:
                    pixelsToBuild = nonEmptyPixels.OrderBy(xy => xy.Item1).ToList();
                    break;

                case PlacingOrderMode.FromRight:
                    pixelsToBuild = nonEmptyPixels.OrderByDescending(xy => xy.Item1).ToList();
                    break;

                case PlacingOrderMode.FromTop:
                    pixelsToBuild = nonEmptyPixels.OrderBy(xy => xy.Item2).ToList();
                    break;

                case PlacingOrderMode.FromBottom:
                    pixelsToBuild = nonEmptyPixels.OrderByDescending(xy => xy.Item2).ToList();
                    break;

                default:
                    Random rnd = new Random();
                    for (int i = 0; i < nonEmptyPixels.Length; i++)
                    {
                        int   r   = rnd.Next(i, nonEmptyPixels.Length);
                        Pixel tmp = nonEmptyPixels[r];
                        nonEmptyPixels[r] = nonEmptyPixels[i];
                        nonEmptyPixels[i] = tmp;
                    }
                    pixelsToBuild = nonEmptyPixels;
                    break;
                }
                cache = new ChunkCache(pixelsToBuild, logger.LogLine);
                mapDownloadedResetEvent = new ManualResetEvent(false);
                cache.OnMapDownloaded  += (o, e) => mapDownloadedResetEvent.Set();
                if (defendMode)
                {
                    gotGriefed             = new AutoResetEvent(false);
                    cache.OnMapDownloaded += (o, e) => gotGriefed.Set();
                    waitingGriefLock       = new object();
                }
                statsThread = new Thread(StatsCollectionThreadBody);
                statsThread.Start();
                do
                {
                    try
                    {
                        using (InteractionWrapper wrapper = new InteractionWrapper(fingerprint, logger.LogLine))
                        {
                            wrapper.OnPixelChanged   += LogPixelChanged;
                            wrapper.OnConnectionLost += (o, e) => mapDownloadedResetEvent.Reset();
                            cache.Wrapper             = wrapper;
                            cache.DownloadChunks();
                            placed.Clear();
                            bool wasChanged;
                            do
                            {
                                wasChanged     = false;
                                repeatingFails = false;
                                foreach (Pixel pixel in pixelsToBuild)
                                {
                                    (short x, short y, PixelColor color) = pixel;
                                    PixelColor actualColor = cache.GetPixelColor(x, y);
                                    if (!IsCorrectPixelColor(actualColor, color))
                                    {
                                        wasChanged = true;
                                        bool success;
                                        placed.Add(pixel);
                                        do
                                        {
                                            byte placingPixelFails = 0;
                                            mapDownloadedResetEvent.WaitOne();
                                            success = wrapper.PlacePixel(x, y, color, out double cd, out double totalCd, out string error);
                                            if (success)
                                            {
                                                string prefix = cd == 4 ? "P" : "Rep";
                                                logger.LogPixel($"{prefix}laced pixel:", MessageGroup.Pixel, x, y, color);
                                                Thread.Sleep(TimeSpan.FromSeconds(totalCd < 53 ? 1 : cd));
                                            }
                                            else
                                            {
                                                if (cd == 0.0)
                                                {
                                                    logger.LogLine("Please go to browser and place pixel, then return and press any key", MessageGroup.Captcha);
                                                    if (notificationMode.HasFlag(CaptchaNotificationMode.Sound))
                                                    {
                                                        Task.Run(() =>
                                                        {
                                                            for (int j = 0; j < 7; j++)
                                                            {
                                                                Console.Beep(1000, 100);
                                                            }
                                                        });
                                                    }
                                                    if (notificationMode.HasFlag(CaptchaNotificationMode.Browser))
                                                    {
                                                        Process.Start($"{InteractionWrapper.BaseHttpAdress}/#{x},{y},30");
                                                    }
                                                    Thread.Sleep(100);
                                                    logger.ConsoleLoggingResetEvent.Reset();
                                                    while (Console.KeyAvailable)
                                                    {
                                                        Console.ReadKey(true);
                                                    }
                                                    Console.ReadKey(true);
                                                    logger.ConsoleLoggingResetEvent.Set();
                                                }
                                                else
                                                {
                                                    logger.LogLine($"Failed to place pixel: {error}", MessageGroup.PixelFail);
                                                    if (++placingPixelFails == 3)
                                                    {
                                                        throw new Exception("Cannot place pixel 3 times");
                                                    }
                                                }
                                                Thread.Sleep(TimeSpan.FromSeconds(cd));
                                            }
                                        } while (!success);
                                    }
                                }
                                if (defendMode)
                                {
                                    if (!wasChanged)
                                    {
                                        logger.LogLine("Image is intact, waiting...", MessageGroup.ImageDone);
                                        lock (waitingGriefLock)
                                        {
                                            gotGriefed.Reset();
                                            gotGriefed.WaitOne();
                                        }
                                        Thread.Sleep(new Random().Next(500, 3000));
                                    }
                                }
                            }while (defendMode || wasChanged);
                            logger.LogLine("Building is finished, exiting...", MessageGroup.ImageDone);
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.LogLine($"Unhandled exception: {ex.Message}", MessageGroup.Error);
                        int delay = repeatingFails ? 30 : 10;
                        repeatingFails = true;
                        logger.LogLine($"Reconnecting in {delay} seconds...", MessageGroup.TechState);
                        Thread.Sleep(TimeSpan.FromSeconds(delay));
                        continue;
                    }
                } while (true);
            }
            finally
            {
                finishCTS.Cancel();
                gotGriefed?.Dispose();
                mapDownloadedResetEvent?.Dispose();
                logger?.Dispose();
                Thread.Sleep(1000);
                finishCTS.Dispose();
                if (statsThread?.IsAlive ?? false)
                {
                    statsThread.Interrupt(); //fallback, should never work
                }
            }
        }
Beispiel #11
0
    void Update()
    {
        // start Kinect interaction as needed
        if (!interactionInited)
        {
            StartInteraction();

            if (!interactionInited)
            {
                Application.Quit();
                return;
            }
        }

        // update Kinect interaction
        if (InteractionWrapper.UpdateKinectInteraction() == 0)
        {
            //int lastSkeletonTrackingID = skeletonTrackingID;
            skeletonTrackingID = (int)InteractionWrapper.GetSkeletonTrackingID();

            if (skeletonTrackingID != 0)
            {
                InteractionWrapper.InteractionHandEventType handEvent = InteractionWrapper.InteractionHandEventType.None;
                Vector4 handPos     = Vector4.zero;
                Vector4 shoulderPos = Vector4.zero;
                Vector3 screenPos   = Vector3.zero;

                // process left hand
                leftHandState = InteractionWrapper.GetLeftHandState();
                handEvent     = (InteractionWrapper.InteractionHandEventType)InteractionWrapper.GetLeftHandEvent();

//				InteractionWrapper.GetLeftHandPos(ref handPos);
//				Vector3 handWorldPos = kinectToWorld.MultiplyPoint3x4(handPos);
//
//				InteractionWrapper.GetLeftShoulderPos(ref shoulderPos);
//				Vector3 shoulderWorldPos = kinectToWorld.MultiplyPoint3x4(shoulderPos);
//
//				Vector3 shoulderToHand =  handWorldPos - shoulderWorldPos;
//				if(leftHandScreenMag == 0f || skeletonTrackingID != lastSkeletonTrackingID)
//				{
//					leftHandScreenMag = shoulderToHand.magnitude;
//				}
//
//				if(leftHandScreenMag > 0f)
//				{
//					screenPos.x = Mathf.Clamp01((leftHandScreenMag / 2 + shoulderToHand.x) / leftHandScreenMag);
//					screenPos.y = Mathf.Clamp01((leftHandScreenMag / 2 + shoulderToHand.y) / leftHandScreenMag);
//					leftHandScreenPos = screenPos;
//				}

                InteractionWrapper.GetLeftCursorPos(ref handPos);
                leftHandScreenPos.x = Mathf.Clamp01(handPos.x);
                leftHandScreenPos.y = 1.0f - Mathf.Clamp01(handPos.y);

                leftHandEvent = handEvent;
                if (handEvent != InteractionWrapper.InteractionHandEventType.None)
                {
                    lastLeftHandEvent = handEvent;
                }

                if ((leftHandState & (uint)InteractionWrapper.NuiHandpointerState.PrimaryForUser) != 0)
                {
                    cursorScreenPos = leftHandScreenPos;
                }

                // process right hand
                rightHandState = InteractionWrapper.GetRightHandState();
                handEvent      = (InteractionWrapper.InteractionHandEventType)InteractionWrapper.GetRightHandEvent();

//				InteractionWrapper.GetRightHandPos(ref handPos);
//				handWorldPos = kinectToWorld.MultiplyPoint3x4(handPos);
//
//				InteractionWrapper.GetRightShoulderPos(ref shoulderPos);
//				shoulderWorldPos = kinectToWorld.MultiplyPoint3x4(shoulderPos);
//
//				shoulderToHand =  handWorldPos - shoulderWorldPos;
//				if(rightHandScreenMag == 0f || skeletonTrackingID != lastSkeletonTrackingID)
//				{
//					rightHandScreenMag = shoulderToHand.magnitude;
//				}
//
//				if(rightHandScreenMag > 0f)
//				{
//					screenPos.x = Mathf.Clamp01((rightHandScreenMag / 2 + shoulderToHand.x) / rightHandScreenMag);
//					screenPos.y = Mathf.Clamp01((rightHandScreenMag / 2 + shoulderToHand.y) / rightHandScreenMag);
//					rightHandScreenPos = screenPos;
//				}

                InteractionWrapper.GetRightCursorPos(ref handPos);
                rightHandScreenPos.x = Mathf.Clamp01(handPos.x);
                rightHandScreenPos.y = 1.0f - Mathf.Clamp01(handPos.y);

                rightHandEvent = handEvent;
                if (handEvent != InteractionWrapper.InteractionHandEventType.None)
                {
                    lastRightHandEvent = handEvent;
                }

                if ((rightHandState & (uint)InteractionWrapper.NuiHandpointerState.PrimaryForUser) != 0)
                {
                    cursorScreenPos = rightHandScreenPos;
                }
            }
            else
            {
                leftHandState  = 0;
                rightHandState = 0;

                leftHandEvent  = lastLeftHandEvent = InteractionWrapper.InteractionHandEventType.None;
                rightHandEvent = lastRightHandEvent = InteractionWrapper.InteractionHandEventType.None;
            }
        }
    }