Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Turbo.Plugins.Default;
- using System;
- using System.Text;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows.Forms;
- using SharpDX.DirectInput;
- using System.Runtime.InteropServices;
- using System.Text.RegularExpressions;
- using System.Drawing;
- using System.Threading;
- namespace Turbo.Plugins.Ez
- {
- public class RepairAndSalvagePlugin : BasePlugin, IKeyEventHandler, IInGameTopPainter
- {
- public bool Debug { get; set; } = false;
- private static IntPtr D3Hwnd = IntPtr.Zero;
- [DllImport("USER32.DLL")]
- private static extern IntPtr FindWindow(string ClassName, string WindowText);
- [DllImport("USER32.DLL")]
- private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
- [DllImport("user32.dll")]
- public static extern bool SetCursorPos(int x, int y);
- [DllImport("user32.dll")]
- public static extern bool GetCursorPos(out Point point);
- [DllImport("user32.dll")]
- private static extern void mouse_event(uint dwFlags, int dx, int dy, uint dwData, IntPtr dwExtraInfo);
- public static void SendPressKey(Keys key)
- {
- SendMessage(D3Hwnd, 256U, (IntPtr)((int)key), IntPtr.Zero);
- SendMessage(D3Hwnd, 257U, (IntPtr)((int)key), IntPtr.Zero);
- }
- public void mouseLClickUiE(IUiElement uie) // D3Hwnd
- {
- if (uie.Visible)
- {
- var x = (int) (uie.Rectangle.X + uie.Rectangle.Width/2.0f);
- var y = (int) (uie.Rectangle.Y + uie.Rectangle.Height/2.0f);
- IntPtr lParam = (IntPtr)(y << 16 | (x & 65535));
- SendMessage(D3Hwnd, 513U, (IntPtr)1, lParam);
- SendMessage(D3Hwnd, 514U, (IntPtr)1, lParam);
- }
- }
- public bool mouseLClickItemInventory(IItem item)
- {
- var rect = Hud.Inventory.GetItemRect(item);
- if (rect != System.Drawing.RectangleF.Empty)
- {
- var x = (int) (rect.X + rect.Width/2.0f + Hud.Window.Offset.X);
- var y = (int) (rect.Y + rect.Height/2.0f + Hud.Window.Offset.Y);
- SetCursorPos(x, y);
- Thread.Sleep(1);
- GetCursorPos(out var point);
- if (point.X == x && point.Y == y) { mouse_event(6U, 0, 0, 0U, IntPtr.Zero); } // It largely eliminates the inconvenience of moving the mouse while processing items
- else return false;
- }
- return true;
- }
- protected IUiElement VENDOR_MAIN;
- protected IUiElement VENDOR_REPAIRALL;
- protected IUiElement VENDOR_TAB_0;
- protected IUiElement VENDOR_TAB_1;
- protected IUiElement VENDOR_TAB_2;
- protected IUiElement VENDOR_TAB_3;
- protected IUiElement VENDOR_TAB_4;
- protected IUiElement VENDOR_DIALOG_0;
- protected IUiElement VENDOR_DIALOG_1;
- protected IUiElement VENDOR_DIALOG_2;
- protected IUiElement VENDOR_DIALOG_3;
- protected IUiElement VENDOR_DIALOG_4;
- protected IUiElement Salvage_Normal_Button;
- protected IUiElement Salvage_Magic_Button;
- protected IUiElement Salvage_Rare_Button;
- protected IUiElement SalvageOneButton;
- protected IUiElement SalvageAuxButton;
- protected IUiElement Chat_Editline;
- protected IUiElement Button_OK;
- private SharpDX.DirectWrite.TextLayout layout { get; set; } = null;
- private List<IItem> ItemsToSalvage { get; set; } = new List<IItem>();
- private int[] dataNMR { get; set; }
- private bool ActionOn { get; set; } = false;
- private long msLapseAction { get; set; } = 0;
- private long msLapseActionOk { get; set; } = 0;
- private bool mRepair { get; set; } = false;
- private bool mSalvage { get; set; } = false;
- private bool waitButtonOk { get; set; } = false;
- private bool mCollect { get; set; } = false;
- private bool StopNow { get; set; } = false;
- private Point PointMousePos { get; set; }
- private bool _doRepairSalvage { get; set; } = false;
- private bool doRepairSalvage
- {
- get { return _doRepairSalvage; }
- set
- {
- if (_doRepairSalvage != value)
- {
- if (value == true)
- {
- ActionOn = true;
- waitButtonOk = false;
- mRepair = false;
- mSalvage = false;
- mCollect = true;
- StopNow = false;
- ItemsToSalvage.Clear();
- GetCursorPos(out var point);
- PointMousePos = point;
- }
- else if (RestoreMousePointer)
- {
- SetCursorPos(PointMousePos.X, PointMousePos.Y);
- }
- if (Chat_Editline.Visible) SendPressKey(Keys.Enter);
- _doRepairSalvage = value;
- }
- }
- }
- private bool _LMod_SalvagePlugin_Disable { get; set; } = false;
- public bool LMod_SalvagePlugin_Disable
- {
- get { return _LMod_SalvagePlugin_Disable; }
- set
- {
- if (Enabled)
- {
- _LMod_SalvagePlugin_Disable = value;
- if (_LMod_SalvagePlugin_Disable)
- {
- var pl = Hud.AllPlugins.FirstOrDefault(p => p.GetType().FullName == "Turbo.Plugins.LightningMod.SalvagePlugin");
- if (pl != null && pl.Enabled)
- {
- pl.Enabled = false;
- }
- }
- }
- }
- }
- public IBrush ItemHighlightBrush { get; set; }
- public IFont InfoFont { get; set; }
- public IKeyEvent ToggleKeyEvent { get; set; }
- public long msLapseMin { get; set; } = 10;
- public long msLapseAnvilEnable { get; set; } = 150;
- public long msLapseAnvilDisable { get; set; } = 150;
- public long msLapseWaitButtonOk { get; set; } = 350;
- public bool saveItemPrimal { get; set; }
- public bool saveItemAncient { get; set; }
- public string TextAvailable { get; set; }
- public string TextNotAvailable { get; set; }
- public bool ConfirmWihtEnterKey { get; set; }
- public bool IgnoreInventoryLockArea { get; set; }
- public bool RestoreMousePointer { get; set; }
- public Dictionary<ISnoItem, int> WhiteList { get; set; } = new Dictionary<ISnoItem, int>();
- public RepairAndSalvagePlugin()
- {
- Enabled = true;
- }
- public override void Load(IController hud)
- {
- base.Load(hud);
- TextAvailable = "Repair&Salvage: Press {0} to Start"; // {0} => Hotkey
- TextNotAvailable = "Repair&Salvage: unavailable, lock area missing";
- saveItemPrimal = true; // Keep primal items , not salvage
- saveItemAncient = false; // Keep Ancient items , not salvage
- ConfirmWihtEnterKey = false; // Use Key Enter or Mouse Virtual for dialog confirmation
- ToggleKeyEvent = Hud.Input.CreateKeyEvent(true, Key.F3, controlPressed: false, altPressed: false, shiftPressed: false); // Hotkey
- IgnoreInventoryLockArea = false; // If false -> Lock area required in the inventory. If true -> Ignore if you have defined (or not) a lock area in the inventory.
- RestoreMousePointer = true; // Restore to original position.
- VENDOR_MAIN = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage", null, null);
- VENDOR_REPAIRALL = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.repair_dialog.RepairAll", VENDOR_DIALOG_3, null);
- VENDOR_TAB_0 = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.tab_0", VENDOR_MAIN, null);
- VENDOR_TAB_1 = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.tab_1", VENDOR_MAIN, null);
- VENDOR_TAB_2 = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.tab_2", VENDOR_MAIN, null);
- VENDOR_TAB_3 = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.tab_3", VENDOR_MAIN, null);
- VENDOR_TAB_4 = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.tab_4", VENDOR_MAIN, null);
- VENDOR_DIALOG_0 = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.craftweapons_dialog", VENDOR_MAIN, null);
- VENDOR_DIALOG_1 = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.craftarmor_dialog", VENDOR_MAIN, null);
- VENDOR_DIALOG_2 = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.salvage_dialog", VENDOR_MAIN, null);
- VENDOR_DIALOG_3 = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.repair_dialog", VENDOR_MAIN, null);
- VENDOR_DIALOG_4 = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.training_dialog", VENDOR_MAIN, null);
- Salvage_Normal_Button = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.salvage_dialog.salvage_all_wrapper.salvage_normal_button", VENDOR_DIALOG_2, null);
- Salvage_Magic_Button = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.salvage_dialog.salvage_all_wrapper.salvage_magic_button", VENDOR_DIALOG_2, null);
- Salvage_Rare_Button = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.salvage_dialog.salvage_all_wrapper.salvage_rare_button", VENDOR_DIALOG_2, null);
- SalvageOneButton = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.salvage_dialog.salvage_all_wrapper.salvage_button", VENDOR_DIALOG_2, null);
- SalvageAuxButton = Hud.Render.RegisterUiElement("Root.NormalLayer.vendor_dialog_mainPage.salvage_dialog.salvage_button", VENDOR_DIALOG_2, null);
- Button_OK = Hud.Render.RegisterUiElement("Root.TopLayer.confirmation.subdlg.stack.wrap.button_ok", null, null);
- Chat_Editline = Hud.Render.RegisterUiElement("Root.NormalLayer.chatentry_dialog_backgroundScreen.chatentry_content.chat_editline", null, null);
- ItemHighlightBrush = Hud.Render.CreateBrush(255, 200, 200, 100, -1.6f);
- InfoFont = Hud.Render.CreateFont("tahoma", 8, 255, 200, 200, 0, true, false, 255, 0, 0, 0, true);
- D3Hwnd = FindWindow("D3 Main Window Class", null); // D3Hwnd = FindWindow(null, "Diablo III");
- }
- public void OnKeyEvent(IKeyEvent keyEvent)
- {
- if (keyEvent.IsPressed && ToggleKeyEvent.Matches(keyEvent))
- {
- if (Hud.Game.IsInTown)
- {
- if (IgnoreInventoryLockArea || (Hud.Inventory.InventoryLockArea.Width > 0 && Hud.Inventory.InventoryLockArea.Height > 0))
- {
- if (VENDOR_MAIN.Visible && VENDOR_TAB_4.Visible)
- {
- if (!doRepairSalvage)
- doRepairSalvage = true;
- else
- StopNow = true;
- }
- }
- }
- }
- }
- private List<IItem> GetItemsToSalvage() // Function to decide which items to salvage
- {
- var result = new List<IItem>();
- var items = Hud.Inventory.ItemsInInventory.Where(i => (i.SnoItem.Kind == ItemKind.loot || i.SnoItem.Kind == ItemKind.potion) && !i.VendorBought && i.ItemsInSocket == null);
- foreach (var item in items)
- {
- var transmogStat = item.StatList.FirstOrDefault(s => s.Id == "TransmogGBID#1048575");
- if (transmogStat != null && transmogStat.DoubleValue != -1) continue; // -1 when undo transmog
- if (item.IsNormal || item.IsMagic || item.IsRare)
- {
- if ( !item.AccountBound && (Hud.Game.Me.CurrentLevelNormal == 70 || IgnoreInventoryLockArea || !item.IsInventoryLocked) )
- result.Add(item);
- }
- else if
- (
- (IgnoreInventoryLockArea || !item.IsInventoryLocked)
- && item.EnchantedAffixCounter == 0 && item.Quantity <= 1 && Hud.Game.Me.ArmorySets.FirstOrDefault(armorySet => armorySet?.ContainsItem(item) == true) == null // Not in Armory
- && (!saveItemPrimal || item.AncientRank != 2) && (!saveItemAncient || item.AncientRank != 1)
- && (item.SnoItem.MainGroupCode != "riftkeystone") && (item.SnoItem.MainGroupCode != "horadriccache") && (item.SnoItem.MainGroupCode != "-") && (item.SnoItem.MainGroupCode != "pony")
- && (item.SnoItem.MainGroupCode != "plans") && !item.SnoItem.MainGroupCode.Contains("cosmetic") && (item.SnoItem.MainGroupCode != "gems_unique") // never salvage Whisper of Atonement
- && (item.SnoItem.NameEnglish != "Staff of Herding") && (item.SnoItem.NameEnglish != "Hellforge Ember")
- && (!item.SnoItem.Code.StartsWith("P72_Soulshard") || item.JewelRank <= 0) // only salvage non-upgraded Soulshard
- && (!WhiteList.TryGetValue(item.SnoItem, out var rank) || item.AncientRank < rank) // White List
- )
- result.Add(item);
- }
- result.Sort((a, b) =>
- {
- var r = a.InventoryX.CompareTo(b.InventoryX);
- if (r == 0) r = a.InventoryY.CompareTo(b.InventoryY);
- return r;
- });
- return result;
- }
- public void PaintTopInGame(ClipState clipState)
- {
- if (clipState != ClipState.Inventory) return;
- if (!Hud.Game.IsInGame || !Hud.Game.IsInTown || !Hud.Inventory.InventoryMainUiElement.Visible) return;
- if (VENDOR_MAIN.Visible && VENDOR_TAB_4.Visible) //BlackSmith (Jeweler and Mystic 3 Tabs, cube 0 Tabs) //Tabs active 16-13-37-34-73 // (+-1) => Values for non-active tab (mouse over or not)
- {
- layout = InfoFont.GetTextLayout( (IgnoreInventoryLockArea || (Hud.Inventory.InventoryLockArea.Width > 0 && Hud.Inventory.InventoryLockArea.Height > 0)) ? String.Format(TextAvailable, ToggleKeyEvent.ToString()) : TextNotAvailable );
- InfoFont.DrawText(layout, VENDOR_MAIN.Rectangle.X + (VENDOR_MAIN.Rectangle.Width * 0.04f), VENDOR_MAIN.Rectangle.X + (VENDOR_MAIN.Rectangle.Height * 0.088f));
- var ItemsToSalvagePaint = GetItemsToSalvage();
- foreach (var item in ItemsToSalvagePaint)
- {
- var itemRect = Hud.Inventory.GetItemRect(item);
- ItemHighlightBrush.DrawRectangle(itemRect);
- }
- if (doRepairSalvage && Hud.Game.Me.AnimationState == AcdAnimationState.Idle)
- {
- if (Hud.Game.CurrentRealTimeMilliseconds > msLapseAction)
- {
- if (Button_OK.Visible)
- {
- if (Button_OK.AnimState != 35) // Minimizing improper press hits of the Accept button
- {
- msLapseAction = Hud.Game.CurrentRealTimeMilliseconds + msLapseMin;
- if (ConfirmWihtEnterKey) // use Enter Key
- SendPressKey(Keys.Enter);
- else // or use Virtual Mouse
- mouseLClickUiE(Button_OK);
- waitButtonOk = false;
- }
- }
- else if (waitButtonOk)
- {
- if (Hud.Game.CurrentRealTimeMilliseconds > msLapseActionOk)
- {
- waitButtonOk = false;
- }
- }
- else
- {
- msLapseAction = Hud.Game.CurrentRealTimeMilliseconds + msLapseMin;
- if (VENDOR_DIALOG_3.Visible) // Repair
- {
- if (!mRepair)
- {
- if (VENDOR_REPAIRALL.Visible)
- {
- var match = Regex.Match(VENDOR_REPAIRALL.ReadText(Encoding.UTF8, true), @"([0-9]+)"); // @"([0-9]+([\.,][0-9]+)*)" for complete number ?
- if (match.Success && match.Groups[1].Value != "0")
- {
- mouseLClickUiE(VENDOR_REPAIRALL);
- }
- else
- {
- mRepair = true;
- mouseLClickUiE(VENDOR_TAB_2);
- }
- }
- }
- else
- {
- mouseLClickUiE(VENDOR_TAB_2);
- }
- }
- else if (VENDOR_DIALOG_2.Visible) // Salvage
- {
- if (!mRepair)
- {
- mouseLClickUiE(VENDOR_TAB_3);
- }
- else
- {
- if (!mSalvage)
- {
- if (mCollect)
- {
- ItemsToSalvage = GetItemsToSalvage();
- if (Hud.Game.Me.CurrentLevelNormal == 70 || IgnoreInventoryLockArea) // Only at level 70 will the buttons be used to save all rare/normal/magic items, and only when their amount is greater than 1
- {
- IItem lastNormal = null; IItem lastMagic = null; IItem lastRare = null;
- dataNMR = new int[] {0, 0, 0};
- foreach (var item in ItemsToSalvage.ToArray())
- {
- if (item.IsNormal) { dataNMR[0] = dataNMR[0] + 1; if (dataNMR[0] == 1) lastNormal = item; else ItemsToSalvage.Remove(item); }
- else if (item.IsMagic) { dataNMR[1] = dataNMR[1] + 1; if (dataNMR[1] == 1) lastMagic = item; else ItemsToSalvage.Remove(item); }
- else if (item.IsRare) { dataNMR[2] = dataNMR[2] + 1; if (dataNMR[2] == 1) lastRare = item; else ItemsToSalvage.Remove(item); }
- }
- if (dataNMR[0] > 1) { ItemsToSalvage.Remove(lastNormal); }
- if (dataNMR[1] > 1) { ItemsToSalvage.Remove(lastMagic); }
- if (dataNMR[2] > 1) { ItemsToSalvage.Remove(lastRare); }
- }
- else mSalvage = true;
- mCollect = false;
- }
- else
- {
- IUiElement uie = null;
- if (dataNMR[0] > 1) { dataNMR[0] = 0; uie = Salvage_Normal_Button; }
- else if (dataNMR[1] > 1) { dataNMR[1] = 0; uie = Salvage_Magic_Button; }
- else if (dataNMR[2] > 1) { dataNMR[2] = 0; uie = Salvage_Rare_Button; }
- if (uie != null && !StopNow)
- {
- if (uie.Visible)
- {
- waitButtonOk = true;
- msLapseActionOk = Hud.Game.CurrentRealTimeMilliseconds + msLapseWaitButtonOk;
- mouseLClickUiE(uie);
- }
- }
- else mSalvage = true;
- }
- }
- else
- {
- if (SalvageOneButton.Visible)
- {
- if (ItemsToSalvage.Any() && !StopNow)
- {
- if (SalvageOneButton.AnimState != 19 && SalvageOneButton.AnimState != 20)
- {
- msLapseAction = Hud.Game.CurrentRealTimeMilliseconds + msLapseAnvilEnable;
- mouseLClickUiE(SalvageOneButton);
- }
- else
- {
- var item = ItemsToSalvage.First();
- if (item.SeenInInventory)
- {
- if (mouseLClickItemInventory(item))
- {
- if (!item.IsNormal && !item.IsMagic && !item.IsRare)
- {
- waitButtonOk = true;
- msLapseActionOk = Hud.Game.CurrentRealTimeMilliseconds + msLapseWaitButtonOk;
- }
- ItemsToSalvage.Remove(item);
- }
- }
- else ItemsToSalvage.Remove(item);
- }
- }
- else // No more items to salvage
- {
- if (ActionOn) // I wait a while to disable the anvil
- {
- ActionOn = false;
- msLapseAction = Hud.Game.CurrentRealTimeMilliseconds + msLapseAnvilDisable;
- }
- else // Disabling anvil
- {
- if (SalvageOneButton.AnimState == 19 || SalvageOneButton.AnimState == 20)
- mouseLClickUiE(SalvageOneButton);
- doRepairSalvage = false;
- }
- }
- }
- else
- {
- doRepairSalvage = false;
- }
- }
- }
- }
- else if (VENDOR_DIALOG_0.Visible || VENDOR_DIALOG_1.Visible || (VENDOR_DIALOG_4.Visible && VENDOR_TAB_4.AnimState == 73))
- {
- mouseLClickUiE(VENDOR_TAB_3);
- }
- else // should never get here
- {
- doRepairSalvage = false;
- }
- }
- }
- }
- }
- else if (doRepairSalvage)
- {
- doRepairSalvage = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment