Ejemplo n.º 1
0
        public static void ItemSlotDraw(ILContext il)
        {
            ILCursor cursor = new ILCursor(il);

            if (!cursor.TryGotoNext(i => i.MatchLdcI4(-1) && i.Next.Next.MatchLdarg(2) && i.Next.Next.Next.MatchLdcI4(13)))
            {
                throw new Exception("Could not locate int [x] = -1; if (context == 13) in IL.Terraria.UI.ItemSlot.Draw");
            }

            byte indx = (byte)((VariableDefinition)cursor.Next.Next.Operand).Index;

            System.Reflection.MethodInfo callTo = typeof(Int32).GetMethod(nameof(Int32.ToString), new Type[] { });

            if (!cursor.TryGotoNext(i => i.MatchLdloca(indx) && i.Next.MatchCall(callTo)))
            {
                throw new Exception("Could not locate call to ChatManager.DrawColorCodedStringWithShadow");
            }

            cursor.Index += 2;
            cursor.Emit(Ldloc_1);
            cursor.EmitDelegate <Func <string, Item, string> >((ammoCount, weapon) => {
                DaesPlayer modPlayer = Main.LocalPlayer.GetModPlayer <DaesPlayer>();
                Item ammo            = modPlayer.GetItemForEndlessAmmoType(weapon.useAmmo);
                if (modPlayer.useEndlessAmmoFirst)
                {
                    return(ammo.type > ItemID.None ? "Inf" : ammoCount);
                }

                if (ammoCount != "0")
                {
                    return(ammoCount);
                }

                return(ammo.type > ItemID.None ? "Inf" : "0");
            });

            /* Resize text?
             * cursor.Emit(Stloc_S, indx);
             * cursor.Emit(Ldloc_S, indx);
             *
             * if (cursor.TryGotoNext(i => i.MatchLdcR4(0.8f))) {
             *      cursor.Remove();
             *      cursor.Emit(Ldloc_S, indx);
             *      cursor.EmitDelegate<Func<string, float>>((count) => {
             *
             *              if (count != "∞")
             *                      return 0.8f;
             *
             *              return 1.6f;
             *      });
             * }
             */
        }
Ejemplo n.º 2
0
        public static void HasAmmo(ILContext il)
        {
            ILCursor cursor = new ILCursor(il);

            cursor.Emit(Ldarg_0);
            cursor.Emit(Ldarg_1);
            cursor.EmitDelegate <Func <Player, Item, bool> >((player, item) => {
                DaesPlayer modPlayer = player.GetModPlayer <DaesPlayer>();
                return(modPlayer.GetItemForEndlessAmmoType(item.useAmmo).type > ItemID.None);
            });

            ILLabel label = il.DefineLabel();

            cursor.Emit(Brfalse, label);
            cursor.Emit(Ldc_I4_1);
            cursor.Emit(Ret);
            cursor.MarkLabel(label);
        }
Ejemplo n.º 3
0
        public static void PickAmmo(ILContext il)
        {
            ILLabel UseNormalAmmoLabel  = il.DefineLabel();
            ILLabel UseEndlessAmmoLabel = il.DefineLabel();
            ILLabel CanShootLabel       = il.DefineLabel();

            // Item item = new Item();
            // bool flag = false;
            ILCursor cursor = new ILCursor(il);

            if (!cursor.TryGotoNext(i => i.MatchLdcI4(0) && i.Next.MatchStloc(1)))
            {
                throw new Exception("Could not locate flag = false");
            }

            cursor.Index += 2;

            // bool useEndlessAmmoFirst = Delegate(this);
            cursor.Emit(Ldarg_0);             // Player player
            cursor.EmitDelegate <Func <Player, bool> >((player) => {
                DaesPlayer modPlayer = player.GetModPlayer <DaesPlayer>();
                return(modPlayer.useEndlessAmmoFirst);
            });

            cursor.Emit(Stloc_2);

            // if (useEndlessAmmoFirst == false) goto USE_NORMAL_AMMO;
            cursor.Emit(Ldloc_2);
            cursor.Emit(Brfalse, UseNormalAmmoLabel);

            // USE_ENDLESS_AMMO:
            cursor.MarkLabel(UseEndlessAmmoLabel);

            // item = Delegate(this, sItem);
            cursor.Emit(Ldarg_0);             // Player player
            cursor.Emit(Ldarg_1);             // Item weapon
            cursor.EmitDelegate <Func <Player, Item, Item> >((player, weapon) => {
                DaesPlayer modPlayer = player.GetModPlayer <DaesPlayer>();
                Item playerAmmo      = modPlayer.GetItemForEndlessAmmoType(weapon.useAmmo);
                return(playerAmmo);
            });
            cursor.Emit(Stloc_0);

            ILLabel IfItemIsAirLabel = il.DefineLabel();

            // if (item.type == 0) goto ITEM_IS_AIR;
            cursor.Emit(Ldloc_0);
            cursor.Emit(Ldfld, typeof(Item).GetField(nameof(Item.type)));
            cursor.Emit(Brfalse, IfItemIsAirLabel);
            // canShoot = true;
            cursor.Emit(Ldarg, 4);
            cursor.Emit(Ldc_I4_1);
            cursor.Emit(Stind_I1);
            // dontConsume = true;
            cursor.Emit(Ldc_I4_1);
            cursor.Emit(Starg, 7);

            // goto CAN_SHOOT;
            cursor.Emit(Br, CanShootLabel);

            // ITEM_IS_AIR:
            cursor.MarkLabel(IfItemIsAirLabel);

            // if (useEndlessAmmoFirst == false) goto CAN_SHOOT;
            cursor.Emit(Ldloc_2);
            cursor.Emit(Brfalse, CanShootLabel);

            // USE_NORMAL_AMMO:
            cursor.MarkLabel(UseNormalAmmoLabel);

            // ..
            if (!cursor.TryGotoNext(i => i.MatchLdarg(4) && i.Next.MatchLdindU1()))
            {
                throw new Exception("Could not locate canShoot == false conditional.");
            }

            // if (useEndlessAmmoFirst) goto CAN_SHOOT;
            ILLabel SkipAheadLabel = il.DefineLabel();

            cursor.Emit(Ldloc_2);
            cursor.Emit(Brtrue, CanShootLabel);

            // if (item.type == 0) goto USE_ENDLESS_AMMO;
            cursor.Emit(Ldloc_0);
            cursor.Emit(Ldfld, typeof(Item).GetField(nameof(Item.type)));
            cursor.Emit(Brfalse, UseEndlessAmmoLabel);

            // CAN_SHOOT:
            cursor.MarkLabel(CanShootLabel);

            // if (!canShoot) return;
            // ...
        }
Ejemplo n.º 4
0
        public override void Draw(SpriteBatch spriteBatch)
        {
            DaesPlayer modPlayer = Main.LocalPlayer.GetModPlayer <DaesPlayer>();

            if (!modPlayer.hasEndlessAmmo)
            {
                base.Draw(spriteBatch);
                return;
            }

            Main.inventoryScale = 0.6f;

            if (AmmoPicker != AmmoID.None)
            {
                DrawAmmoPicker(spriteBatch, modPlayer);
                base.Draw(spriteBatch);
                return;
            }
            else
            {
                AmmoPickerScroll = 0;
            }

            int   dx         = LeftX;
            Color titleColor = new Color(Main.mouseTextColor, Main.mouseTextColor, Main.mouseTextColor, Main.mouseTextColor);
            Point mousePoint = new Point(Main.mouseX, Main.mouseY);

            Rect checkboxRect = new Rect(dx, 84f, 16, 16);

            LargeItemSlot.DrawPanel(spriteBatch, checkboxRect, Main.inventoryScale);

            string EndlessAmmoLabel = "∞ Ammo";

            ChatManager.DrawColorCodedString(spriteBatch, Main.fontMouseText, EndlessAmmoLabel, new Vector2(dx + 20, 84f), titleColor, 0, Vector2.Zero, Vector2.One * 0.75f);

            Rect slotRect = new Rect(52, 52);

            slotRect.Scale(Main.inventoryScale);

            int  hoverSlot     = -1;
            int  hoverAmmoType = 0;
            Item hoverAmmo     = new Item();

            for (int i = 0; i < DataContainer.EndlessAmmoTypes.Length; i++)
            {
                int x = i / 4;
                int y = i % 4;

                slotRect.X = dx + x * DeltaX;
                slotRect.Y = (int)(105f + y * 56 * Main.inventoryScale);

                EndlessAmmoType AmmoType = DataContainer.EndlessAmmoTypes[i];
                Item            ammo     = modPlayer.GetItemForEndlessAmmoType(AmmoType.Type);

                LargeItemSlot.DrawPanel(spriteBatch, slotRect, Main.inventoryScale);
                if (ammo.type != ItemID.None)
                {
                    LargeItemSlot.DrawItem(spriteBatch, slotRect, ammo, Main.inventoryScale);
                }
                else
                {
                    Texture2D texture  = AmmoType.Texture;
                    Vector2   position = slotRect.Center() - texture.Size() * Main.inventoryScale / 2f;
                    spriteBatch.Draw(texture, position, texture.Frame(), PreviewColor, 0f, Vector2.Zero, Main.inventoryScale, SpriteEffects.None, 0f);
                }

                if (modPlayer.CanUnlockAmmoForType(AmmoType.Type))
                {
                    Vector2 hPosition = slotRect.Position();
                    hPosition.X += 24;
                    hPosition.Y += 4;
                    ChatManager.DrawColorCodedStringWithShadow(
                        spriteBatch,
                        Main.fontMouseText,
                        "!",
                        hPosition,
                        UnlockColor,
                        0f,
                        Vector2.Zero,
                        Vector2.One * 0.6f
                        );
                }

                if (AmmoPicker == AmmoID.None && slotRect.Contains(mousePoint))
                {
                    hoverSlot     = i;
                    hoverAmmo     = ammo;
                    hoverAmmoType = AmmoType.Type;
                }
            }

            if (hoverSlot > -1)
            {
                Main.LocalPlayer.mouseInterface = true;
                if (hoverAmmo.type != ItemID.None)
                {
                    Main.HoverItem            = hoverAmmo.Clone();
                    Main.HoverItem.ammo       = 0;
                    Main.HoverItem.material   = false;
                    Main.HoverItem.consumable = false;
                    Main.instance.MouseText(hoverAmmo.Name, hoverAmmo.rare, 0);
                }
                else
                {
                    Main.instance.MouseText(DataContainer.EndlessAmmoTypes[hoverSlot].NoSelected);
                }

                if (Main.mouseLeft && Main.mouseLeftRelease)
                {
                    Main.PlaySound(SoundID.MenuOpen);
                    AmmoPicker = hoverAmmoType;
                }
            }

            if (checkboxRect.Contains(mousePoint))
            {
                Main.LocalPlayer.mouseInterface = true;
                Main.instance.MouseText("Use endless ammo first.");

                if (Main.mouseLeft && Main.mouseLeftRelease)
                {
                    Main.PlaySound(SoundID.MenuTick);
                    modPlayer.useEndlessAmmoFirst = !modPlayer.useEndlessAmmoFirst;
                }
            }

            if (modPlayer.useEndlessAmmoFirst)
            {
                Vector2 position = checkboxRect.Center();
                position.X += 2;
                position.Y -= 2;
                ChatManager.DrawColorCodedStringWithShadow(spriteBatch, Main.fontMouseText, "✓", position, Color.White, 0f, checkboxRect.Dimensions() / 2, Vector2.One * Main.inventoryScale);
            }

            base.Draw(spriteBatch);
        }