Advertisement
Nighticus

URW_SSSelector1.0.0Source

Dec 5th, 2020
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13.  
  14. namespace URWSSSelector
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         ReadWriteMem RWMain;
  24.  
  25.         Rectangle Map = new Rectangle();
  26.         Process URW;
  27.         public bool Active = false;
  28.         public bool MoveLocation = false;
  29.         public RECT WindowRect;
  30.         Rectangle GameRect = new Rectangle();
  31.  
  32.         private bool BoxCanDraw;
  33.         private int BoxStartX, BoxStartY;
  34.         private Rectangle BoxRect;
  35.         private Rectangle BoxRectFill;
  36.  
  37.         const int DWMWA_EXTENDED_FRAME_BOUNDS = 9;
  38.  
  39.         [DllImport("dwmapi.dll")]
  40.         static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out RECT pvAttribute, int cbAttribute);
  41.  
  42.         [DllImport("user32.dll", SetLastError = true)]
  43.         static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
  44.  
  45.         [DllImport("user32.dll")]
  46.         public static extern IntPtr GetForegroundWindow();
  47.  
  48.         [DllImport("user32.dll", SetLastError = true)]
  49.         static extern int GetWindowLong(IntPtr hWnd, int nIndex);
  50.  
  51.         [DllImport("user32.dll")]
  52.         static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
  53.  
  54.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
  55.         static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, int wParam, IntPtr lParam);
  56.  
  57.         [DllImport("user32.dll")]
  58.         static extern bool SetForegroundWindow(IntPtr hWnd);
  59.  
  60.         [StructLayout(LayoutKind.Sequential)]
  61.         public struct RECT
  62.         {
  63.             public int Left;        // x position of upper-left corner
  64.             public int Top;         // y position of upper-left corner
  65.             public int Right;       // x position of lower-right corner
  66.             public int Bottom;      // y position of lower-right corner
  67.         }
  68.  
  69.         Point StartLocation
  70.         {
  71.             get
  72.             {
  73.                 return new Point(RWMain.Read<int>((int)Address.XStart), RWMain.Read<int>((int)Address.YStart));
  74.             }
  75.             set
  76.             {
  77.                 RWMain.Write((int)Address.XStart, value.X);
  78.                 RWMain.Write((int)Address.YStart, value.Y);
  79.             }
  80.         }
  81.  
  82.         byte[] MapIDArray
  83.         {
  84.             get
  85.             {
  86.                 return RWMain.Read<byte[]>((int)Address.MapTileIDArray, 6293502);
  87.             }
  88.             set
  89.             {
  90.                 RWMain.Write((int)Address.MapTileIDArray, value);
  91.             }
  92.         }
  93.  
  94.         private void Form1_Load(object sender, EventArgs e)
  95.         {
  96.             try
  97.             {
  98.                 URW = Process.GetProcessesByName("urw")[0];
  99.                 RWMain = new ReadWriteMem(URW);
  100.                 new Thread(WindowThread) { IsBackground = true }.Start();
  101.                 if (MapIDArray[0] == 0)
  102.                 {
  103.                     MessageBox.Show("Map has not been generated. Exiting.");
  104.                     Environment.Exit(0);
  105.                 }
  106.                 SetForegroundWindow(URW.MainWindowHandle);
  107.             }
  108.             catch
  109.             {
  110.                 MessageBox.Show("Unreal world not found running. Closing URWSSSelector.");
  111.                 Environment.Exit(0);
  112.             }
  113.         }
  114.  
  115.  
  116.         private Point GetMouseXYMap(MouseEventArgs e)
  117.         {
  118.             int X = 0, Y = 0;
  119.             if (BoxRectFill.Width > 0 && BoxRectFill.Contains(e.X, e.Y))
  120.             {
  121.                 X = (int)((e.X - BoxRectFill.X) * (3072m / BoxRectFill.Width));
  122.                 Y = (int)((e.Y - BoxRectFill.Y) * (2048m / BoxRectFill.Height));
  123.                 if (MoveLocation)
  124.                 {
  125.                     StartLocation = new Point(X, Y);
  126.                     SendMessage(URW.MainWindowHandle, 0x0102, '-', IntPtr.Zero);
  127.                 }
  128.             }
  129.             return new Point(X, Y);
  130.         }
  131.  
  132.         private void WindowThread()
  133.         {
  134.             IntPtr ThisHandle = IntPtr.Zero;
  135.             this.Invoke(new Action(() => ThisHandle = this.Handle));
  136.             while (Thread.CurrentThread.IsAlive)
  137.             {
  138.                 if (DwmGetWindowAttribute(RWMain.TargetProcess.MainWindowHandle, DWMWA_EXTENDED_FRAME_BOUNDS, out WindowRect, Marshal.SizeOf(typeof(RECT))) != 0)
  139.                 {
  140.                     GetWindowRect(RWMain.TargetProcess.MainWindowHandle, out WindowRect);
  141.                 }
  142.  
  143.                 GameRect = new Rectangle(new Point(WindowRect.Left, WindowRect.Top), new Size(WindowRect.Right - WindowRect.Left, WindowRect.Bottom - WindowRect.Top));
  144.                 this.Invoke(new Action(() => this.Location = GameRect.Location));
  145.                 this.Invoke(new Action(() => this.Size = GameRect.Size));
  146.  
  147.                 if (GetForegroundWindow() != RWMain.TargetProcess.MainWindowHandle && GetForegroundWindow() != ThisHandle)
  148.                 {
  149.                     this.Invoke(new Action(() => this.Visible = false));
  150.                 }
  151.                 else if (GetForegroundWindow() == RWMain.TargetProcess.MainWindowHandle)
  152.                 {
  153.                     this.Invoke(new Action(() => this.Visible = true));
  154.                 }
  155.  
  156.                 Thread.Sleep(10);
  157.             }
  158.         }
  159.  
  160.         private void Form1_MouseMove(object sender, MouseEventArgs e)
  161.         {
  162.             Point MouseLocation = GetMouseXYMap(e);
  163.             lblMapMouse.Text = string.Format("X: {0}, Y: {1}", MouseLocation.X, MouseLocation.Y);
  164.             lblMapMouse.Location = new Point (this.Width / 2 - 50, lblMapMouse.Location.Y);
  165.  
  166.             if (BoxCanDraw)
  167.             {
  168.                 int Box_X = Math.Min(BoxStartX, e.X);
  169.                 int Box_Y = Math.Min(BoxStartY, e.Y);
  170.                 int Box_Width = Math.Max(BoxStartX, e.X) - Math.Min(BoxStartX, e.X);
  171.                 int Box_Height = Math.Max(BoxStartY, e.Y) - Math.Min(BoxStartY, e.Y);
  172.  
  173.                 BoxRect = new Rectangle(Box_X, Box_Y, Box_Width, Box_Height);
  174.                 BoxRectFill = new Rectangle(Box_X - 1, Box_Y - 1, Box_Width + 2, Box_Height + 2);
  175.                 Refresh();
  176.             }
  177.         }
  178.  
  179.         private void Form1_MouseDown(object sender, MouseEventArgs e)
  180.         {
  181.             MoveLocation = false;
  182.             if (e.Button == MouseButtons.Left)
  183.             {
  184.                 BoxCanDraw = true;
  185.                 BoxStartX = e.X;
  186.                 BoxStartY = e.Y;
  187.             }
  188.  
  189.             else if (e.Button == MouseButtons.Right)
  190.             {
  191.                 Environment.Exit(0);
  192.             }
  193.         }
  194.  
  195.         private void Form1_MouseUp(object sender, MouseEventArgs e)
  196.         {
  197.             MoveLocation = true;
  198.             if (e.Button == MouseButtons.Left)
  199.             {
  200.                 BoxCanDraw = false;
  201.                 BoxRect = new Rectangle(0, 0, 0, 0);
  202.                 Refresh();
  203.             }
  204.         }
  205.  
  206.  
  207.         private void Form1_Paint(object sender, PaintEventArgs e)
  208.         {
  209.             //Create a new 'pen' to draw our rectangle with, give it the color red and a width of 2
  210.             using (Pen BoxPen = new Pen(Color.Red, 2))
  211.             {
  212.                 using (SolidBrush BoxFill = new SolidBrush(Color.FromArgb((int)(255 * .45m), Color.Red)))
  213.                 {
  214.                     e.Graphics.FillRectangle(BoxFill, BoxRectFill);
  215.                 }
  216.                 e.Graphics.DrawRectangle(BoxPen, BoxRect);
  217.             }
  218.         }
  219.     }
  220.  
  221.     public static class Extensions
  222.     {
  223.         public static string ReplaceModifiedString(this string s)
  224.         {
  225.             return s.Replace('Σ', 'ä').Replace('Ø', '¥').Replace('─', 'Ä').Replace('÷', 'ö');
  226.         }
  227.     }
  228.  
  229.     public class ReadWriteMem
  230.     {
  231.         [DllImport("Kernel32.dll")]
  232.         private static extern bool ReadProcessMemory(IntPtr hProcess, int BaseAddress, byte[] Buffer, int Size, ref int NumberOfBytesRead);
  233.  
  234.         [DllImport("Kernel32.dll")]
  235.         private static extern bool WriteProcessMemory(IntPtr hProcess, int BaseAddress, byte[] Buffer, int Size, ref int NumberOfBytesWritten);
  236.  
  237.         private Process Proc;
  238.         public Process TargetProcess
  239.         {
  240.             get { return Proc; }
  241.             set { Proc = value; }
  242.         }
  243.         public IntPtr WindowHandle = IntPtr.Zero;
  244.  
  245.         public int ProcBaseAddress = 0;
  246.         public const int VersionOffset = 0x0000;
  247.  
  248.         public ReadWriteMem(Process T)
  249.         {
  250.             TargetProcess = T;
  251.             ProcBaseAddress = T.MainModule.BaseAddress.ToInt32();
  252.             WindowHandle = T.Handle;
  253.         }
  254.  
  255.         public bool Write(int BaseAddress, dynamic obj, byte pb = 1)
  256.         {
  257.             byte[] Buffer = BitConverter.GetBytes(obj);
  258.             int BytesWritten = 0;
  259.             if (Buffer.Length == 2 && Buffer[1] == 0) { WriteProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, new byte[] { (byte)obj }, 1, ref BytesWritten); }
  260.             else { WriteProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, Buffer.Length, ref BytesWritten); }
  261.             switch (BytesWritten)
  262.             {
  263.                 case 0: return false;
  264.                 default: return true;
  265.             }
  266.         }
  267.  
  268.         public bool Write(int BaseAddress, byte[] Buffer, byte pb = 1)
  269.         {
  270.             int BytesWritten = 0;
  271.             WriteProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, Buffer.Length, ref BytesWritten);
  272.             switch (BytesWritten)
  273.             {
  274.                 case 0: return false;
  275.                 default: return true;
  276.             }
  277.         }
  278.  
  279.         public bool Write(int BaseAddress, string Text, byte pb = 1)
  280.         {
  281.             byte[] Buffer = Encoding.GetEncoding("IBM865").GetBytes(Text.ReplaceModifiedString());
  282.             int BytesWritten = 0;
  283.             WriteProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, Buffer.Length, ref BytesWritten);
  284.             switch (BytesWritten)
  285.             {
  286.                 case 0: return false;
  287.                 default: return true;
  288.             }
  289.         }
  290.  
  291.         public T Read<T>(int BaseAddress, byte pb = 1)
  292.         {
  293.             byte[] Buffer;
  294.             int BytesRead = 0;
  295.  
  296.             switch (Type.GetTypeCode(typeof(T)))
  297.             {
  298.                 case TypeCode.Byte:
  299.                     Buffer = new byte[1];
  300.                     ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, 1, ref BytesRead);
  301.                     return (T)Convert.ChangeType(Buffer[0], typeof(T));
  302.                 case TypeCode.Int16:
  303.                     Buffer = new byte[2];
  304.                     ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, 2, ref BytesRead);
  305.                     return (T)Convert.ChangeType(BitConverter.ToUInt16(Buffer, 0), typeof(T));
  306.                 case TypeCode.Int32:
  307.                     Buffer = new byte[4];
  308.                     ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, 4, ref BytesRead);
  309.                     return (T)Convert.ChangeType(BitConverter.ToUInt32(Buffer, 0), typeof(T));
  310.                 case TypeCode.UInt32:
  311.                     Buffer = new byte[4];
  312.                     ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, 4, ref BytesRead);
  313.                     return (T)Convert.ChangeType(BitConverter.ToUInt32(Buffer, 0), typeof(T));
  314.                 case TypeCode.Single:
  315.                     Buffer = new byte[4];
  316.                     ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, 4, ref BytesRead);
  317.                     return (T)Convert.ChangeType(BitConverter.ToSingle(Buffer, 0), typeof(T));
  318.                 default:
  319.                     return (T)Convert.ChangeType(false, typeof(T));
  320.             }
  321.         }
  322.  
  323.         public T Read<T>(int BaseAddress, int Length, byte pb = 1)
  324.         {
  325.             byte[] Buffer;
  326.             int BytesRead = 0;
  327.  
  328.             switch (Type.GetTypeCode(typeof(T)))
  329.             {
  330.                 case TypeCode.Object:
  331.                     Buffer = new byte[Length];
  332.                     ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, Length, ref BytesRead);
  333.                     return (T)Convert.ChangeType(Buffer, typeof(T));
  334.                 case TypeCode.String:
  335.                     Buffer = new byte[Length];
  336.                     ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, Length, ref BytesRead);
  337.                     return (T)Convert.ChangeType(Encoding.GetEncoding("IBM865").GetString(Buffer, 0, Buffer.Length).Split(new char[] { '\0' })[0].ReplaceModifiedString(), typeof(T));
  338.                 default:
  339.                     return (T)Convert.ChangeType(false, typeof(T));
  340.             }
  341.         }
  342.  
  343.     }
  344.  
  345.     public enum Address
  346.     {
  347.         MapTileIDArray = 0x8C6928,
  348.         MouseXCoord = 0x5D1C4E4,
  349.         MouseYCoord = 0x5F20434,
  350.         TimeDay = 0xA2F1534,
  351.         TimeHour = 0xA2F150C,
  352.         TimeMinute = 0xA2F150D,
  353.         TimeMonth = 0xA2F1540,
  354.         TimeYear = 0xA2F1541,
  355.         XLocation = 0xA297824,
  356.         XStart = 0x1B4BA0,
  357.         YLocation = 0xA2F20C8,
  358.         YStart = 0x1B4BA4
  359.     }
  360.  
  361. }
  362.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement