Ejemplo n.º 1
0
        private static void Update()
        {
            if (Main.gameMenu)
            {
                return;
            }

            if (installUI > 0) //Skip some frames before access layers
            {
                installUI--;
                if (installUI == 0)
                {
                    Logger.Log("Installing InterfaceLayer...");
                    var layers = Reflect.GetF <List <GameInterfaceLayer> >(Main.instance, "_gameInterfaceLayers");
                    layers?.Insert(36, new LegacyGameInterfaceLayer("Creative mod: Custom UI", () =>
                    {
                        if (Main.gameMenu)
                        {
                            return(false);
                        }
                        if (Copy.Visible)
                        {
                            ucopy.Draw(Main.spriteBatch, new GameTime());
                            copy.UIDraw();
                        }
                        if (CheatState.Visible)
                        {
                            inter.Draw(Main.spriteBatch, new GameTime());
                        }
                        if (tools.visible)
                        {
                            tools.UIDraw();
                        }

                        return(true);
                    }));
                    Logger.Log("Installing InterfaceLayer done!");
                }
            }

            if (controlsUI == null && Main.InGameUI.CurrentState != null)
            {
                if (Main.InGameUI.CurrentState is UIManageControls ui)
                {
                    Logger.Log("Hijack UIManageControls. Adding own input panel...");
                    controlsUI = ui;
                    try
                    {
                        Logger.Log("Getting \"CreateBindingGroup\" method...");
                        var pan = Reflect.Invoke <UISortableElement>(controlsUI, "CreateBindingGroup", 3,
                                                                     creativeControlsString, InputMode.Keyboard);
                        Logger.Log("Adding new panel to _bindKeyboard...");
                        Reflect.GetF <List <UIElement> >(controlsUI, "_bindsKeyboard").Add(pan);
                        Logger.Log("Adding new panel to _bindKeyboardUI...");
                        Reflect.GetF <List <UIElement> >(controlsUI, "_bindsKeyboardUI").Add(pan);

                        var KeyStatus = PlayerInput.CurrentProfile.InputModes[InputMode.Keyboard].KeyStatus;

                        Logger.Log("Registering new input bindings...");
                        if (!KeyStatus.ContainsKey("CycleMode"))
                        {
                            KeyStatus.Add("CycleMode", new List <string> {
                                CreativeInput[0].ToString()
                            });
                        }
                        if (!KeyStatus.ContainsKey("FlyMode"))
                        {
                            KeyStatus.Add("FlyMode", new List <string> {
                                CreativeInput[1].ToString()
                            });
                        }
                        if (!KeyStatus.ContainsKey("UnlockAllItems"))
                        {
                            KeyStatus.Add("UnlockAllItems", new List <string> {
                                CreativeInput[2].ToString()
                            });
                        }
                        if (!KeyStatus.ContainsKey("UnlockAllBestiary"))
                        {
                            KeyStatus.Add("UnlockAllBestiary", new List <string> {
                                CreativeInput[3].ToString()
                            });
                        }
                        if (!KeyStatus.ContainsKey("LockBestiary"))
                        {
                            KeyStatus.Add("LockBestiary", new List <string> {
                                CreativeInput[4].ToString()
                            });
                        }
                        if (!KeyStatus.ContainsKey("IncreaseFlySpeed"))
                        {
                            KeyStatus.Add("IncreaseFlySpeed", new List <string> {
                                CreativeInput[5].ToString()
                            });
                        }
                        if (!KeyStatus.ContainsKey("DecreaseFlySpeed"))
                        {
                            KeyStatus.Add("DecreaseFlySpeed", new List <string> {
                                CreativeInput[6].ToString()
                            });
                        }
                        if (!KeyStatus.ContainsKey("InstantRevive"))
                        {
                            KeyStatus.Add("InstantRevive", new List <string> {
                                CreativeInput[7].ToString()
                            });
                        }
                        if (!KeyStatus.ContainsKey("InstantRevive"))
                        {
                            KeyStatus.Add("InstantRevive", new List <string> {
                                CreativeInput[7].ToString()
                            });
                        }

                        PlayerInput.OnBindingChange += BindingChanged;

                        Logger.Log("Done! Getting \"FillList\" method...");
                        var method2 = controlsUI.GetType()
                                      .GetMethod("FillList", BindingFlags.NonPublic | BindingFlags.Instance);
                        //We use catch statement to notify if injection failed
                        // ReSharper disable once PossibleNullReferenceException
                        method2.Invoke(controlsUI, new object[] { });

                        Logger.Log("Successively added new panel!");
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "Error occured during adding new controls panel!");
                    }
                }
            }

            if (!firstUpdate)
            {
                firstUpdate = true;
                BindingChanged();
            }

            //All our ui get broken when UI Scale != 1
            //Main.UIScale = 1f;



            //NPC.NewNPC((int)Main.LocalPlayer.position.X, (int)Main.LocalPlayer.position.Y-300, 636);

            if (tools.visible)
            {
                tools.UIUpdate();
            }



            if (Main.LocalPlayer.creativeTracker.ItemSacrifices.SacrificesCountByItemIdCache.Count < 1000 &&
                Main.keyState.IsKeyDown(CreativeInput[2]) && Main.oldKeyState.IsKeyUp(CreativeInput[2]))
            {
                Logger.Log("Unlocking all items...");
                for (int i = 0; i < 5079; i++)
                {
                    try
                    {
                        Main.LocalPlayer.creativeTracker.ItemSacrifices.RegisterItemSacrifice(i, 999);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }

                Text("All items in journey mode unlocked");
            }

            if (Main.keyState.IsKeyDown(CreativeInput[0]) && Main.oldKeyState.IsKeyUp(CreativeInput[0]))
            {
                //CycleMode();
                CheatState.Visible = !CheatState.Visible;
            }

            if (Main.keyState.IsKeyDown(CreativeInput[3]) && Main.oldKeyState.IsKeyUp(CreativeInput[3]))
            {
                Logger.Log("Processing Bestiary...");
                try
                {
                    foreach (var it in ContentSamples.NpcBestiaryCreditIdsByNpcNetIds)
                    {
                        Console.WriteLine($"Registering NPCID: {it.Key} -> {it.Value}");
                        Main.BestiaryTracker.Kills.SetKillCountDirectly(it.Value, 9999);
                        Main.BestiaryTracker.Chats.SetWasChatWithDirectly(it.Value);
                        Main.BestiaryTracker.Sights.SetWasSeenDirectly(it.Value);
                    }
                }
                catch (Exception e)
                {
                    Logger.Error(e, "Error occured while processing bestiarry!");
                }
            }

            if (Main.keyState.IsKeyDown(CreativeInput[4]) && Main.oldKeyState.IsKeyUp(CreativeInput[4]))
            {
                Logger.Log("Clearing Bestiary...");
                try
                {
                    Main.BestiaryTracker.Kills.Reset();
                    Main.BestiaryTracker.Chats.Reset();
                    Main.BestiaryTracker.Sights.Reset();
                }
                catch (Exception e)
                {
                    Logger.Error(e, "Error occured while processing bestiarry!");
                }
            }

            if (Main.keyState.IsKeyDown(CreativeInput[1]) && Main.oldKeyState.IsKeyUp(CreativeInput[1]))
            {
                ChangeFly();
            }

            if (Main.keyState.IsKeyDown(CreativeInput[5]) && Main.oldKeyState.IsKeyUp(CreativeInput[5]))
            {
                if (flyIndex < 10)
                {
                    flyIndex++;
                    SetupFlySpeed();
                }
            }

            if (Main.keyState.IsKeyDown(CreativeInput[6]) && Main.oldKeyState.IsKeyUp(CreativeInput[6]))
            {
                if (flyIndex > 1)
                {
                    flyIndex--;
                    SetupFlySpeed();
                }
            }

            if (Main.keyState.IsKeyDown(CreativeInput[7]) && Main.oldKeyState.IsKeyUp(CreativeInput[7]))
            {
                instantRevive = !instantRevive;
            }


            //Logic
            delta = Main.LocalPlayer.velocity - Main.LocalPlayer.oldVelocity;

            if (fly)
            {
                if (delta.Length() < 1)
                {
                    Main.LocalPlayer.velocity  = -delta;
                    Main.LocalPlayer.position += -delta;
                }
                else
                {
                    Main.LocalPlayer.velocity    = Vector2.Zero;
                    Main.LocalPlayer.oldVelocity = Vector2.Zero;
                }

                Main.LocalPlayer.fallStart = (int)(Main.LocalPlayer.position.Y / 16f);

                if (Main.keyState.IsKeyDown(FlyInput[0]))
                {
                    Main.LocalPlayer.position.Y -= flyDelta;
                }
                if (Main.keyState.IsKeyDown(FlyInput[1]))
                {
                    Main.LocalPlayer.position.Y += flyDelta;
                }
                if (Main.keyState.IsKeyDown(FlyInput[2]))
                {
                    Main.LocalPlayer.position.X += flyDelta;
                }
                if (Main.keyState.IsKeyDown(FlyInput[3]))
                {
                    Main.LocalPlayer.position.X -= flyDelta;
                }
            }

            if (instantRevive && Main.LocalPlayer.respawnTimer > 0)
            {
                fly = false;
                Main.LocalPlayer.respawnTimer = 0;
            }

            if (Main.mapFullscreen)
            {
                if (Main.mouseRight && Main.keyState.IsKeyUp(Keys.LeftControl))
                {
                    int     mapWidth       = Main.maxTilesX * 16;
                    int     mapHeight      = Main.maxTilesY * 16;
                    Vector2 cursorPosition = new Vector2(Main.mouseX, Main.mouseY);

                    cursorPosition.X -= Main.screenWidth / 2;
                    cursorPosition.Y -= Main.screenHeight / 2;

                    Vector2 mapPosition         = Main.mapFullscreenPos;
                    Vector2 cursorWorldPosition = mapPosition;

                    cursorPosition      /= 16;
                    cursorPosition      *= 16 / Main.mapFullscreenScale;
                    cursorWorldPosition += cursorPosition;
                    cursorWorldPosition *= 16;

                    Player player = Main.player[Main.myPlayer];
                    cursorWorldPosition.Y -= player.height;
                    if (cursorWorldPosition.X < 0)
                    {
                        cursorWorldPosition.X = 0;
                    }
                    else if (cursorWorldPosition.X + player.width > mapWidth)
                    {
                        cursorWorldPosition.X = mapWidth - player.width;
                    }
                    if (cursorWorldPosition.Y < 0)
                    {
                        cursorWorldPosition.Y = 0;
                    }
                    else if (cursorWorldPosition.Y + player.height > mapHeight)
                    {
                        cursorWorldPosition.Y = mapHeight - player.height;
                    }

                    if (Main.netMode == 0) // single
                    {
                        player.Teleport(cursorWorldPosition, 1);
                        player.position  = cursorWorldPosition;
                        player.velocity  = Vector2.Zero;
                        player.fallStart = (int)(player.position.Y / 16f);
                    }
                    else // 1, client
                    {
                        NetMessage.SendData(65, -1, -1, null, 0, player.whoAmI, cursorWorldPosition.X,
                                            cursorWorldPosition.Y, 1);
                        player.Teleport(cursorWorldPosition, 1);
                        player.position  = cursorWorldPosition;
                        player.velocity  = Vector2.Zero;
                        player.fallStart = (int)(player.position.Y / 16f);
                    }
                }
            }
            ucopy.Update(new GameTime());
            inter.Update(new GameTime());
        }