EzPlugins

KadalaShopPlugin

Feb 24th, 2025 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.09 KB | None | 0 0
  1. using Turbo.Plugins.Default;
  2. using System;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using SharpDX.DirectInput;
  6. using System.Runtime.InteropServices;
  7.  
  8. namespace Turbo.Plugins.Ez
  9. {
  10.     public class KadalaShopPlugin : BasePlugin, IKeyEventHandler, IInGameTopPainter
  11.     {
  12.         public bool Debug { get; set; } = false;
  13.         private static IntPtr D3Hwnd = IntPtr.Zero;
  14.         [DllImport("USER32.DLL")]
  15.         private static extern IntPtr FindWindow(string ClassName, string WindowText);
  16.         [DllImport("USER32.DLL")]
  17.         private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  18.  
  19.         private static IntPtr MakeLParam(int x, int y)
  20.         {
  21.             return (IntPtr)(y << 16 | (x & 65535));
  22.         }
  23.  
  24.         public void mouseClickUiE(Keys key, IUiElement uie) // D3Hwnd
  25.         {
  26.             if (uie.Visible)
  27.             {
  28.                 IntPtr lParam = MakeLParam((int)(uie.Rectangle.X + uie.Rectangle.Width * 0.5f), (int)(uie.Rectangle.Y + uie.Rectangle.Height * 0.5f));
  29.                 if (key == Keys.LButton)
  30.                 {
  31.                     SendMessage(D3Hwnd, 513U, (IntPtr)1, lParam);
  32.                     SendMessage(D3Hwnd, 514U, (IntPtr)1, lParam);
  33.                 }
  34.                 else if (key == Keys.RButton)
  35.                 {
  36.                     SendMessage(D3Hwnd, 516U, (IntPtr)2, lParam);
  37.                     SendMessage(D3Hwnd, 517U, (IntPtr)2, lParam);
  38.                 }
  39.             }
  40.         }
  41.  
  42.         protected IUiElement ShopMainPage;
  43.         protected IUiElement ShopGoldText;
  44.  
  45.         private bool doShop { get; set; }
  46.         private int itemActive { get; set; } = 0;
  47.  
  48.         private long msLapseAction { get; set; } = 0;
  49.         private long msLapseMin { get; set; } = 50;
  50.  
  51.         private IUiElement[] UiEItemsRegion { get; set; }
  52.         private IUiElement[] UiETabs { get; set; }
  53.         private int[,] itemsData { get; set; }
  54.  
  55.         public IFont InfoFont { get; set; }
  56.  
  57.         public IKeyEvent ToggleKeyEvent { get; set; }
  58.  
  59.         public string[] names { get; set; } = new string[] { "<None>", "1-H Weapon", "2-H Weapon", "Quiver", "Orb", "Mojo", "Phylactery", "Helm", "Gloves", "Boots", "Chest Armor", "Belt", "Shoulders", "Pants", "Bracers", "Shield", "Ring", "Amulet" };
  60.  
  61.         public KadalaShopPlugin()
  62.         {
  63.             Enabled = true;
  64.         }
  65.  
  66.         public override void Load(IController hud)
  67.         {
  68.             base.Load(hud);
  69.  
  70.             ToggleKeyEvent = Hud.Input.CreateKeyEvent(true, Key.F3, controlPressed: false, altPressed: false, shiftPressed: false); // Hotkey
  71.             InfoFont = Hud.Render.CreateFont("tahoma", 8, 255, 200, 200, 0, true, false, 255, 0, 0, 0, true);
  72.  
  73.             ShopMainPage = Hud.Render.RegisterUiElement("Root.NormalLayer.shop_dialog_mainPage", null, null);
  74.             ShopGoldText = Hud.Render.RegisterUiElement("Root.NormalLayer.shop_dialog_mainPage.gold_text", ShopMainPage, null);
  75.  
  76.             itemsData = new int[18,5]   //{numUieRegion, numUieTab, animTabActive, shards, space}
  77.             {
  78.                 {0, 0,  0,   0, 0}, //  0) None
  79.                 {0, 0, 49,  75, 2}, //  1) 1-H Weapon
  80.                 {1, 0, 49,  75, 2}, //  2) 2-H Weapon
  81.                 {2, 0, 49,  25, 2}, //  3) Quiver
  82.                 {3, 0, 49,  25, 2}, //  4) Orb
  83.                 {4, 0, 49,  25, 2}, //  5) Mojo
  84.                 {5, 0, 49,  25, 2}, //  6) Phylactery
  85.                 {0, 1, 40,  25, 2}, //  7) Helm
  86.                 {1, 1, 40,  25, 2}, //  8) Gloves
  87.                 {2, 1, 40,  25, 2}, //  9) Boots
  88.                 {3, 1, 40,  25, 2}, // 10) Chest Armor
  89.                 {4, 1, 40,  25, 1}, // 11) Belt
  90.                 {5, 1, 40,  25, 2}, // 12) Shoulders
  91.                 {6, 1, 40,  25, 2}, // 13) Pants
  92.                 {7, 1, 40,  25, 2}, // 14) Bracers
  93.                 {8, 1, 40,  25, 2}, // 15) Shield
  94.                 {0, 2, 46,  50, 1}, // 16) Ring
  95.                 {1, 2, 46, 100, 1}, // 17) Amulet
  96.             };
  97.  
  98.             UiEItemsRegion = new IUiElement[]
  99.             {
  100.                 Hud.Render.RegisterUiElement("Root.NormalLayer.shop_dialog_mainPage.shop_item_region.item 0 0", ShopMainPage, null),
  101.                 Hud.Render.RegisterUiElement("Root.NormalLayer.shop_dialog_mainPage.shop_item_region.item 1 0", ShopMainPage, null),
  102.                 Hud.Render.RegisterUiElement("Root.NormalLayer.shop_dialog_mainPage.shop_item_region.item 0 1", ShopMainPage, null),
  103.                 Hud.Render.RegisterUiElement("Root.NormalLayer.shop_dialog_mainPage.shop_item_region.item 1 1", ShopMainPage, null),
  104.                 Hud.Render.RegisterUiElement("Root.NormalLayer.shop_dialog_mainPage.shop_item_region.item 0 2", ShopMainPage, null),
  105.                 Hud.Render.RegisterUiElement("Root.NormalLayer.shop_dialog_mainPage.shop_item_region.item 1 2", ShopMainPage, null),
  106.                 Hud.Render.RegisterUiElement("Root.NormalLayer.shop_dialog_mainPage.shop_item_region.item 0 3", ShopMainPage, null),
  107.                 Hud.Render.RegisterUiElement("Root.NormalLayer.shop_dialog_mainPage.shop_item_region.item 1 3", ShopMainPage, null),
  108.                 Hud.Render.RegisterUiElement("Root.NormalLayer.shop_dialog_mainPage.shop_item_region.item 0 4", ShopMainPage, null)
  109.             };
  110.  
  111.             UiETabs = new IUiElement[]
  112.             {
  113.                 Hud.Render.RegisterUiElement("Root.NormalLayer.shop_dialog_mainPage.tab_0", ShopMainPage, null), // AnimState 48-49-50 (over/active/out)
  114.                 Hud.Render.RegisterUiElement("Root.NormalLayer.shop_dialog_mainPage.tab_1", ShopMainPage, null), // AnimState 39-40-41 (over/active/out)
  115.                 Hud.Render.RegisterUiElement("Root.NormalLayer.shop_dialog_mainPage.tab_2", ShopMainPage, null)  // AnimState 45-46-47 (over/active/out)
  116.             };
  117.  
  118.             D3Hwnd = FindWindow("D3 Main Window Class", null); // D3Hwnd = FindWindow(null, "Diablo III");
  119.         }
  120.  
  121.         public void OnKeyEvent(IKeyEvent keyEvent)
  122.         {
  123.             if (keyEvent.IsPressed && ToggleKeyEvent.Matches(keyEvent))
  124.             {
  125.                 if (Hud.Game?.IsInTown == true && Hud.Inventory.InventoryMainUiElement.Visible && ShopMainPage.Visible && ShopGoldText.Visible && ShopGoldText.ReadText(Encoding.UTF8, true).EndsWith("{icon:x1_shard}"))
  126.                 {
  127.                     if (doShop)
  128.                     {
  129.                         doShop = false;
  130.                     }
  131.                     else
  132.                     {
  133.                         for (int i = 0; i < UiEItemsRegion.Length ; i++)
  134.                         {
  135.                             if (UiEItemsRegion[i].Visible && UiEItemsRegion[i].Rectangle.Contains(Hud.Window.CursorX, Hud.Window.CursorY))
  136.                             {
  137.                                 for (int j = 1; j < itemsData.Length ; j++) // itemsData {numUieRegion, numUieTab, animTabActive, shards, space}
  138.                                 {
  139.                                     if (itemsData[j,0] == i && UiETabs[itemsData[j,1]].AnimState == itemsData[j,2])
  140.                                     {
  141.                                         itemActive = j;
  142.                                         break;
  143.                                     }
  144.                                 }
  145.                                 break;
  146.                             }
  147.                         }
  148.                         if (itemActive > 0 && itemActive < 18 && Hud.Game.Me.Materials.BloodShard >= itemsData[itemActive,3])
  149.                         {
  150.                             doShop = true;
  151.                         }
  152.                     }
  153.                 }
  154.             }
  155.         }
  156.  
  157.         public void PaintTopInGame(ClipState clipState)
  158.         {
  159.             if (!Hud.Game.IsInGame || !Hud.Game.IsInTown || !Hud.Inventory.InventoryMainUiElement.Visible) return;
  160.             if (ShopMainPage.Visible && ShopGoldText.Visible && ShopGoldText.ReadText(Encoding.UTF8, true).EndsWith("{icon:x1_shard}"))
  161.             {
  162.                 if (clipState == ClipState.AfterClip)
  163.                 {
  164.                     var layout = InfoFont.GetTextLayout(String.Format("[KadalaShopPlugin] Hotkey: {0} , Default Item: {1} ", ToggleKeyEvent.ToString(), names[itemActive]));
  165.                     InfoFont.DrawText(layout, ShopMainPage.Rectangle.X + (ShopMainPage.Rectangle.Width * 0.055f), ShopMainPage.Rectangle.Y + (ShopMainPage.Rectangle.Height * 0.089f));
  166.                 }
  167.                 else if (clipState == ClipState.Inventory)
  168.                 {
  169.                     if (doShop)
  170.                     {
  171.                         if (Hud.Game.CurrentRealTimeMilliseconds > msLapseAction)
  172.                         {
  173.                             if (UiETabs[itemsData[itemActive,1]].Visible)
  174.                             {
  175.                                 if (UiETabs[itemsData[itemActive,1]].AnimState == itemsData[itemActive,2])  // itemsData {numUieRegion, numUieTab, animTabActive, shards, space}
  176.                                 {
  177.                                     doShop = false;
  178.                                     if (UiEItemsRegion[itemsData[itemActive,0]].Visible)
  179.                                     {
  180.                                         int numShopShards = (int) Hud.Game.Me.Materials.BloodShard / itemsData[itemActive,3];
  181.                                         int numShopInv = (Hud.Game.Me.InventorySpaceTotal - Hud.Game.InventorySpaceUsed) / itemsData[itemActive,4];
  182.                                         var numShop = Math.Min(numShopShards,numShopInv);
  183.                                         if (numShop++ > 0) // 1 more if (numShop != 0);
  184.                                         {
  185.                                             while (numShop-- > 0)
  186.                                             {
  187.                                                 mouseClickUiE(Keys.RButton, UiEItemsRegion[itemsData[itemActive,0]]);
  188.                                             }
  189.                                         }
  190.                                     }
  191.                                 }
  192.                                 else
  193.                                 {
  194.                                     mouseClickUiE(Keys.LButton, UiETabs[itemsData[itemActive,1]]);
  195.                                     msLapseAction = Hud.Game.CurrentRealTimeMilliseconds + msLapseMin;
  196.                                 }
  197.                             }
  198.                             else doShop = false;
  199.                         }
  200.                     }
  201.                 }
  202.             }
  203.             else if (doShop)
  204.             {
  205.                 doShop = false;
  206.             }
  207.         }
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment