Exemplo n.º 1
0
        private static string CreateXMLGameObject(GameObjectDetails obj)
        {
            string xml = "";

            xml += "<parent name=\"" + obj.parent + "\" />\r\n";
            xml += "<enabled value=\"" + obj.enabled.ToString() + "\" />\r\n";
            xml += "<layer value=\"" + obj.layer.ToString() + "\" />\r\n";

            xml += "<components>\r\n";
            foreach (var comp in obj.components)
            {
                xml += "<component name=\"" + comp + "\" />\r\n";
            }
            xml += "</components>\r\n";

            if (obj.children.Count > 0)
            {
                xml += "<children>\r\n";
                foreach (GameObjectDetails child in obj.children)
                {
                    xml += "<child name=\"" + child.name + "\">\r\n";
                    xml += CreateXMLGameObject(child);
                    xml += "</child>\r\n";
                }
                xml += "</children>\r\n";
            }

            return(xml);
        }
        public void Update()
        {
            if (!updateFired)
            {
                BepInExLoader.log.LogMessage("TrainerComponent Update() Fired!"); updateFired = true;
            }

            if (!initialized)
            {
                Initialize();
            }

            // Note: You can use the regular Input also for Key events, see the usings
            // Just an Option Toggle to give us more Key combinations
            if (Input.GetKeyInt(BepInEx.IL2CPP.UnityEngine.KeyCode.LeftShift) && Input.GetKeyInt(BepInEx.IL2CPP.UnityEngine.KeyCode.Delete) && Event.current.type == EventType.KeyDown)
            {
                optionToggle = !optionToggle;
                log.LogMessage("Option Toggle Enabled: " + optionToggle.ToString());
                Event.current.Use();
            }

            // Toggle TooltipGUI
            if (UnityEngine.Input.GetKeyDown(UnityEngine.KeyCode.T) && optionToggle && EventSystem.current != null && Event.current.type == EventType.KeyDown)
            {
                Event.current.Use();
                toolTipComp.enabled = !toolTipComp.enabled;
            }

            // Object Spy - Requires an EventSystem! (WIP, currently only detects UI elements)
            if (optionToggle && EventSystem.current != null && Event.current.type == EventType.MouseDrag)
            {
                //log.LogMessage("ObjectSpy Fired!");

                if (canvas != null)
                {
                    var saInput = EventSystem.current.GetComponent <StandaloneInputModule>();
                    var evData  = saInput.GetMousePointerEventData();

                    string name = "", pname = "", tmpSpyText = "";

                    try
                    {
                        name  = evData.GetButtonState(PointerEventData.InputButton.Left).eventData.buttonData.pointerEnter.name.ToString();
                        pname = evData.GetButtonState(PointerEventData.InputButton.Left).eventData.buttonData.pointerCurrentRaycast.module.name;

                        tmpSpyText = "GameObject: " + name + " - Parent: " + pname;

                        TooltipGUI.Tooltip = name;
                    }
                    catch { /* Not Implemented yet for World objects. TODO: Add Physics and Graphics Raycaster */ }

                    if (tmpSpyText != spyText)
                    {
                        log.LogMessage(""); // Just a Spacer
                        log.LogMessage("[GameObject]: " + name + " - Parent: " + pname);
                        spyText = tmpSpyText;

                        log.LogMessage("  [Parent Components]:");
                        var pcomps = evData.GetButtonState(PointerEventData.InputButton.Left).eventData.buttonData.pointerCurrentRaycast.module.gameObject.GetGameObjectComponents();
                        foreach (var comp in pcomps)
                        {
                            log.LogMessage("    " + comp.Name);
                        }

                        log.LogMessage("  [GameObject Components]:");
                        var gcomps = evData.GetButtonState(PointerEventData.InputButton.Left).eventData.buttonData.pointerEnter.GetGameObjectComponents();
                        foreach (var comp in gcomps)
                        {
                            log.LogMessage("    " + comp.Name);
                        }
                    }
                }

                Event.current.Use();
            }
            if (optionToggle && EventSystem.current != null && Event.current.type == EventType.MouseUp)
            {
                // Clears the tooltip on MouseUp
                TooltipGUI.Tooltip = "";
                Event.current.Use();
            }

            // Dump All Scenes GameObjects (w/ optionToggle True prints components also)
            if (UnityEngine.Input.GetKeyDown(UnityEngine.KeyCode.A) && Event.current.type == EventType.KeyDown)
            {
                DumpAll(GetAllScenesGameObjects());
                Event.current.Use();
            }

            // Dumping Root Scene Objects w/ Values (w/ optionToggle True prints components also)
            if (UnityEngine.Input.GetKeyDown(UnityEngine.KeyCode.R) && Event.current.type == EventType.KeyDown)
            {
                log.LogMessage("Dumping Root Scene Objects w/ values...");
                Trainer.Tools.SceneDumper.DumpObjects(GetRootSceneGameObjects().ToArray());
                Event.current.Use();
            }

            // Create UI if needed otherwise Show/Hide
            if (UnityEngine.Input.GetKeyDown(UnityEngine.KeyCode.U) && Event.current.type == EventType.KeyDown)
            {
                if (canvas == null)
                {
                    instance.CreateUI();
                }
                else
                {
                    if (isVisible)
                    {
                        canvas.SetActive(false);
                        isVisible = false;
                    }
                    else
                    {
                        canvas.SetActive(true);
                        isVisible = true;
                    }
                }

                Event.current.Use();
            }

            // Display Scene Details
            if (UnityEngine.Input.GetKeyDown(KeyCode.S) && Event.current.type == EventType.KeyDown)
            {
                log.LogMessage(" ");
                log.LogMessage("Scene Details:");
                Scene activeScene = SceneManager.GetActiveScene();
                log.LogMessage("   Scene Name: " + activeScene.name);
                log.LogMessage("   Scene Build Index: " + activeScene.buildIndex.ToString());
                log.LogMessage("   Root Object Count: " + activeScene.rootCount.ToString());

                log.LogMessage(" ");
                log.LogMessage("   Camera's:");
                foreach (var cam in Camera.allCameras)
                {
                    log.LogMessage("      Cam: " + cam.name + " GameObject: " + cam.gameObject.name);
                }

                log.LogMessage(" ");
                log.LogMessage("   Predefined Layers... (-1 = Not Found)");
                string[] predefinedLayers = { "Default", "TransparentFX", "Ignore Raycast", "Water", "UI", "Player", String.Empty };
                foreach (string layer in predefinedLayers)
                {
                    log.LogMessage("      Layer: [" + LayerMask.NameToLayer(layer) + "] " + layer);
                }

                log.LogMessage(" ");
                log.LogMessage("   Root Objects:");
                foreach (var obj in GetRootSceneGameObjects())
                {
                    log.LogMessage("      Name: " + obj.name);
                    log.LogMessage("         Enabled: " + obj.activeSelf.ToString());
                    log.LogMessage("         Layer: " + obj.layer.ToString());
                    log.LogMessage("         Position: " + obj.transform.position.ToString());
                    log.LogMessage("         Local Position: " + obj.transform.localPosition.ToString());
                    log.LogMessage("         Components: ");
                    foreach (var comp in obj.GetComponents <Component>())
                    {
                        log.LogMessage("            " + comp.name + "(" + comp.GetIl2CppType().Name + ")");
                    }
                    log.LogMessage(" ");
                }
            }

            // Dump All Scene's Objects to XML (w/ optionToggle True uses FindObjectsOfType<GameOBject>() (Finds more, but at loss of Hierarchy))
            if (UnityEngine.Input.GetKeyDown(KeyCode.X) && Event.current.type == EventType.KeyDown)
            {
                log.LogMessage("");

                List <GameObjectDetails> objectTree = new List <GameObjectDetails>();

                // Doesn't show DontDestroyOnLoad objects. GameObject.FindObjectsOfType<GameObject>() does, but loses heirarchy
                if (!optionToggle)
                {
                    log.LogMessage("Dumping All Objects to XML using GetAllScenesGameObject()...");
                    foreach (var obj in GetAllScenesGameObjects())
                    {
                        objectTree.Add(new GameObjectDetails(obj));
                    }
                }
                else
                {
                    log.LogMessage("Dumping All Objects to XML using GameObject.FindObjectsOfType<GameObject>()...");
                    foreach (var obj in GameObject.FindObjectsOfType <GameObject>())
                    {
                        objectTree.Add(new GameObjectDetails(obj));
                    }
                }

                File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "\\AllObjectsDump.xml", GameObjectDetails.XMLSerialize(objectTree));

                log.LogMessage("Complete!");
                log.LogMessage("XML written to " + (AppDomain.CurrentDomain.BaseDirectory + "\\AllObjectsDump.xml").Replace("\\\\", "\\"));

                string path = (AppDomain.CurrentDomain.BaseDirectory + "\\AllObjectsDump.xml").Replace("\\\\", "\\");
                Application.OpenURL("file:///" + path.Replace("\\", "/")); // Opens in default associated app

                Event.current.Use();
            }

            // Display Mouse/World/View Positions for Debugging
            if (positionDebug == null)
            {
                positionDebug = GameObject.Find("TitleImage");
            }                                                                             // Just a refence object to compare values
            if (optionToggle && EventSystem.current != null && Event.current.type == EventType.KeyDown)
            {
                log.LogMessage(" ");
                log.LogMessage("Mouse Debug Info:");
                log.LogMessage("   TitleImage POS: " + positionDebug.transform.position.ToString());
                log.LogMessage("   Mouse POS:      " + UnityEngine.Input.mousePosition.ToString());
                log.LogMessage("   Mouse2World:    " + Camera.current.ScreenToWorldPoint(UnityEngine.Input.mousePosition).ToString());
                log.LogMessage("   Mouse2Viewport: " + Camera.current.ScreenToViewportPoint(UnityEngine.Input.mousePosition).ToString());
                log.LogMessage(" ");

                Event.current.Use();
            }

            // Unity MonoBehavior Event Testing
            if (UnityEngine.Input.GetKeyDown(KeyCode.Tab) && EventSystem.current != null && Event.current.type == EventType.KeyDown)
            {
                Event.current.Use();
                log.LogMessage("Testing Unity MonoBehavior Events...");
                eventsTester = Trainer.Tools.EventTestComponent.Create("EventTestComponentGO");
            }
            if (Trainer.Tools.EventTestComponent.eventsFired)
            {
                Destroy(eventsTester);
            }
        }