Advertisement
Nighticus

URW_SSSelector1.0.1Source

Dec 7th, 2020
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.23 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.IO;
  8. using System.Linq;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14.  
  15. namespace URWSSSelector
  16. {
  17.     public partial class Form1 : Form
  18.     {
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         ReadWriteMem RWMain;
  25.         Process URW;
  26.         public bool Active = false;
  27.         public bool MoveLocation = false;
  28.         public RECT WindowRect;
  29.         Rectangle GameRect = new Rectangle();
  30.         byte[] LoadedMap;
  31.         private Rectangle GameMap;
  32.         Point ClosestLand = new Point(0, 0);
  33.         private int[] TileIDFilter = new[] { 180, 181, 182, 183, 177, 244, 247, 178 };
  34.  
  35.         const int DWMWA_EXTENDED_FRAME_BOUNDS = 9;
  36.  
  37.         [DllImport("dwmapi.dll")]
  38.         static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out RECT pvAttribute, int cbAttribute);
  39.  
  40.         [DllImport("user32.dll", SetLastError = true)]
  41.         static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
  42.  
  43.         [DllImport("user32.dll")]
  44.         public static extern IntPtr GetForegroundWindow();
  45.  
  46.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
  47.         static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, int wParam, IntPtr lParam);
  48.  
  49.         [DllImport("user32.dll")]
  50.         static extern bool SetForegroundWindow(IntPtr hWnd);
  51.  
  52.         [StructLayout(LayoutKind.Sequential)]
  53.         public struct RECT
  54.         {
  55.             public int Left;        // x position of upper-left corner
  56.             public int Top;         // y position of upper-left corner
  57.             public int Right;       // x position of lower-right corner
  58.             public int Bottom;      // y position of lower-right corner
  59.         }
  60.  
  61.         Point StartLocation
  62.         {
  63.             get
  64.             {
  65.                 return new Point(RWMain.Read<int>((int)Address.XStart), RWMain.Read<int>((int)Address.YStart));
  66.             }
  67.             set
  68.             {
  69.                 RWMain.Write((int)Address.XStart, value.X);
  70.                 RWMain.Write((int)Address.YStart, value.Y);
  71.             }
  72.         }
  73.  
  74.         byte[] MapIDArray
  75.         {
  76.             get
  77.             {
  78.                 return RWMain.Read<byte[]>((int)Address.MapTileIDArray, 6293502);
  79.             }
  80.             set
  81.             {
  82.                 RWMain.Write((int)Address.MapTileIDArray, value);
  83.             }
  84.         }
  85.  
  86.         private void Form1_Load(object sender, EventArgs e)
  87.         {
  88.             try
  89.             {
  90.                 URW = Process.GetProcessesByName("urw")[0];
  91.                 RWMain = new ReadWriteMem(URW);
  92.                 string VersionInfo = VersionCheck();
  93.                 if (VersionInfo.Length > 0) { MessageBox.Show(VersionInfo); Environment.Exit(0); }
  94.                 new Thread(WindowThread) { IsBackground = true }.Start();
  95.                 LoadedMap = MapIDArray;
  96.                 if (LoadedMap[0] == 0)
  97.                 {
  98.                     MessageBox.Show("Map has not been generated. Exiting.");
  99.                     Environment.Exit(0);
  100.                 }
  101.                 SetForegroundWindow(URW.MainWindowHandle);
  102.  
  103.             }
  104.             catch
  105.             {
  106.                 MessageBox.Show("Unreal world not found running. Closing URWSSSelector.");
  107.                 Environment.Exit(0);
  108.             }
  109.         }
  110.  
  111.         private string VersionCheck()
  112.         {
  113.             FileVersionInfo Info = URW.MainModule.FileVersionInfo;
  114.             string Folder = Info.FileName.Substring(0, Info.FileName.Length - 8);
  115.             List<string> Lines = new List<string>();
  116.             Lines.AddRange(File.ReadAllLines(Folder + "\\news.txt"));
  117.             for (int i = 0; i < Lines.Count; i++)
  118.             {
  119.                 if (Lines[i].ToLower().Contains("3.63") && i < 10)
  120.                 {
  121.                     return "";
  122.                 }
  123.             }
  124.             return "Game version mismatch, please check thread on official forum for updates.";
  125.         }
  126.  
  127.         private Rectangle GetMap()
  128.         {
  129.             Bitmap bmpScreen = new Bitmap(GameRect.Width, GameRect.Height);
  130.             Point TopLeft = new Point(0, 0);
  131.             Point BottomRight = new Point(0, 0);
  132.             using (Graphics g = Graphics.FromImage(bmpScreen))
  133.             {
  134.                 g.CopyFromScreen(GameRect.Location, new Point(0,0), GameRect.Size, CopyPixelOperation.SourceCopy);
  135.             }
  136.             for (int x = 0; x < bmpScreen.Width; x++)
  137.             {
  138.                 for (int y = 0; y < bmpScreen.Height; y++)
  139.                 {
  140.                     Color Pixel = bmpScreen.GetPixel(x, y);
  141.                     if (Pixel.B > 190 && Pixel.R < 20 && Pixel.G < 30)
  142.                     {
  143.                         if (TopLeft.X == 0) { TopLeft = new Point(x, y); }
  144.                         else { BottomRight = new Point(x, y); }
  145.                     }
  146.                 }
  147.             }
  148.             MoveLocation = true;
  149.             return new Rectangle(TopLeft, new Size(BottomRight.X - TopLeft.X, BottomRight.Y - TopLeft.Y));
  150.         }
  151.  
  152.         private Point GetMouseXYMap(MouseEventArgs e)
  153.         {
  154.             int X = 0, Y = 0;
  155.             if (GameMap.Width > 0)
  156.             {
  157.                 X = (int)((e.X - GameMap.X) * (3073m / GameMap.Width));
  158.                 Y = (int)((e.Y - GameMap.Y) * (2049m / GameMap.Height));
  159.                 if (MoveLocation)
  160.                 {
  161.                     if (X < 48) { X = 48; }
  162.                     if (X > 3024) { X = 3024; }
  163.                     if (Y < 48) { Y = 48; }
  164.                     if (Y > 2000) { Y = 2000; }
  165.                     int LocationIndex = (Y * 3073) + X;
  166.                     if (!TileIDFilter.Contains((int)LoadedMap[LocationIndex]))
  167.                     {
  168.                         StartLocation = new Point(X, Y);
  169.                     }
  170.                     else // find nearest landmass
  171.                     {
  172.                         for (int XSearch = X - 200; XSearch < X + 200; XSearch++)
  173.                         {
  174.                             for (int YSearch = Y - 200; YSearch < Y + 200; YSearch++)
  175.                             {
  176.                                 int SearchIndex = (YSearch * 3073) + XSearch;
  177.                                 if (SearchIndex > 0 && SearchIndex < LoadedMap.Length)
  178.                                 {
  179.                                     if (!TileIDFilter.Contains((int)LoadedMap[SearchIndex]))
  180.                                     {
  181.                                         if (XSearch > 48 && XSearch < 3024 && YSearch > 48 && YSearch < 2000)
  182.                                         {
  183.                                             Point OldCursorDistance = new Point(Math.Abs(ClosestLand.X - X), Math.Abs(ClosestLand.Y - Y));
  184.                                             Point NewCursorDistance = new Point(Math.Abs(XSearch - X), Math.Abs(YSearch - Y));
  185.  
  186.                                             if (OldCursorDistance.X + OldCursorDistance.Y > NewCursorDistance.X + NewCursorDistance.Y)
  187.                                             {
  188.                                                 ClosestLand = new Point(XSearch, YSearch);
  189.                                                 StartLocation = ClosestLand;
  190.                                             }
  191.                                         }
  192.                                     }
  193.                                 }
  194.                             }
  195.                         }
  196.                     }
  197.                     SendMessage(URW.MainWindowHandle, 0x0102, '-', IntPtr.Zero);
  198.                 }
  199.             }
  200.             return new Point(X, Y);
  201.         }
  202.  
  203.         private void WindowThread()
  204.         {
  205.             IntPtr ThisHandle = IntPtr.Zero;
  206.             this.Invoke(new Action(() => ThisHandle = this.Handle));
  207.             while (Thread.CurrentThread.IsAlive)
  208.             {
  209.                 if (DwmGetWindowAttribute(RWMain.TargetProcess.MainWindowHandle, DWMWA_EXTENDED_FRAME_BOUNDS, out WindowRect, Marshal.SizeOf(typeof(RECT))) != 0)
  210.                 {
  211.                     GetWindowRect(RWMain.TargetProcess.MainWindowHandle, out WindowRect);
  212.                 }
  213.  
  214.                 GameRect = new Rectangle(new Point(WindowRect.Left, WindowRect.Top), new Size(WindowRect.Right - WindowRect.Left, WindowRect.Bottom - WindowRect.Top));
  215.                 this.Invoke(new Action(() => this.Location = GameRect.Location));
  216.                 this.Invoke(new Action(() => this.Size = GameRect.Size));
  217.  
  218.                 if (GetForegroundWindow() != RWMain.TargetProcess.MainWindowHandle && GetForegroundWindow() != ThisHandle)
  219.                 {
  220.                     this.Invoke(new Action(() => this.Visible = false));
  221.                 }
  222.                 else if (GetForegroundWindow() == RWMain.TargetProcess.MainWindowHandle)
  223.                 {
  224.                     this.Invoke(new Action(() => this.Visible = true));
  225.                 }
  226.  
  227.                 if (GameMap.Width <= 0)
  228.                 {
  229.                     GameMap = GetMap();
  230.                 }
  231.  
  232.                 Thread.Sleep(10);
  233.             }
  234.         }
  235.  
  236.         private void Form1_MouseMove(object sender, MouseEventArgs e)
  237.         {
  238.             Point MouseLocation = GetMouseXYMap(e);
  239.         }
  240.  
  241.         private void Form1_MouseDown(object sender, MouseEventArgs e)
  242.         {
  243.             if (e.Button == MouseButtons.Right)
  244.             {
  245.                 Environment.Exit(0);
  246.             }
  247.         }
  248.     }
  249.  
  250.     public static class Extensions
  251.     {
  252.         public static string ReplaceModifiedString(this string s)
  253.         {
  254.             return s.Replace('Σ', 'ä').Replace('Ø', '¥').Replace('─', 'Ä').Replace('÷', 'ö');
  255.         }
  256.     }
  257.  
  258.     public class ReadWriteMem
  259.     {
  260.         [DllImport("Kernel32.dll")]
  261.         private static extern bool ReadProcessMemory(IntPtr hProcess, int BaseAddress, byte[] Buffer, int Size, ref int NumberOfBytesRead);
  262.  
  263.         [DllImport("Kernel32.dll")]
  264.         private static extern bool WriteProcessMemory(IntPtr hProcess, int BaseAddress, byte[] Buffer, int Size, ref int NumberOfBytesWritten);
  265.  
  266.         private Process Proc;
  267.         public Process TargetProcess
  268.         {
  269.             get { return Proc; }
  270.             set { Proc = value; }
  271.         }
  272.         public IntPtr WindowHandle = IntPtr.Zero;
  273.  
  274.         public int ProcBaseAddress = 0;
  275.         public const int VersionOffset = 0x0000;
  276.  
  277.         public ReadWriteMem(Process T)
  278.         {
  279.             TargetProcess = T;
  280.             ProcBaseAddress = T.MainModule.BaseAddress.ToInt32();
  281.             WindowHandle = T.Handle;
  282.         }
  283.  
  284.         public bool Write(int BaseAddress, dynamic obj, byte pb = 1)
  285.         {
  286.             byte[] Buffer = BitConverter.GetBytes(obj);
  287.             int BytesWritten = 0;
  288.             if (Buffer.Length == 2 && Buffer[1] == 0) { WriteProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, new byte[] { (byte)obj }, 1, ref BytesWritten); }
  289.             else { WriteProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, Buffer.Length, ref BytesWritten); }
  290.             switch (BytesWritten)
  291.             {
  292.                 case 0: return false;
  293.                 default: return true;
  294.             }
  295.         }
  296.  
  297.         public bool Write(int BaseAddress, byte[] Buffer, byte pb = 1)
  298.         {
  299.             int BytesWritten = 0;
  300.             WriteProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, Buffer.Length, ref BytesWritten);
  301.             switch (BytesWritten)
  302.             {
  303.                 case 0: return false;
  304.                 default: return true;
  305.             }
  306.         }
  307.  
  308.         public bool Write(int BaseAddress, string Text, byte pb = 1)
  309.         {
  310.             byte[] Buffer = Encoding.GetEncoding("IBM865").GetBytes(Text.ReplaceModifiedString());
  311.             int BytesWritten = 0;
  312.             WriteProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, Buffer.Length, ref BytesWritten);
  313.             switch (BytesWritten)
  314.             {
  315.                 case 0: return false;
  316.                 default: return true;
  317.             }
  318.         }
  319.  
  320.         public T Read<T>(int BaseAddress, byte pb = 1)
  321.         {
  322.             byte[] Buffer;
  323.             int BytesRead = 0;
  324.  
  325.             switch (Type.GetTypeCode(typeof(T)))
  326.             {
  327.                 case TypeCode.Byte:
  328.                     Buffer = new byte[1];
  329.                     ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, 1, ref BytesRead);
  330.                     return (T)Convert.ChangeType(Buffer[0], typeof(T));
  331.                 case TypeCode.Int16:
  332.                     Buffer = new byte[2];
  333.                     ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, 2, ref BytesRead);
  334.                     return (T)Convert.ChangeType(BitConverter.ToUInt16(Buffer, 0), typeof(T));
  335.                 case TypeCode.Int32:
  336.                     Buffer = new byte[4];
  337.                     ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, 4, ref BytesRead);
  338.                     return (T)Convert.ChangeType(BitConverter.ToUInt32(Buffer, 0), typeof(T));
  339.                 case TypeCode.UInt32:
  340.                     Buffer = new byte[4];
  341.                     ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, 4, ref BytesRead);
  342.                     return (T)Convert.ChangeType(BitConverter.ToUInt32(Buffer, 0), typeof(T));
  343.                 case TypeCode.Single:
  344.                     Buffer = new byte[4];
  345.                     ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, 4, ref BytesRead);
  346.                     return (T)Convert.ChangeType(BitConverter.ToSingle(Buffer, 0), typeof(T));
  347.                 default:
  348.                     return (T)Convert.ChangeType(false, typeof(T));
  349.             }
  350.         }
  351.  
  352.         public T Read<T>(int BaseAddress, int Length, byte pb = 1)
  353.         {
  354.             byte[] Buffer;
  355.             int BytesRead = 0;
  356.  
  357.             switch (Type.GetTypeCode(typeof(T)))
  358.             {
  359.                 case TypeCode.Object:
  360.                     Buffer = new byte[Length];
  361.                     ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, Length, ref BytesRead);
  362.                     return (T)Convert.ChangeType(Buffer, typeof(T));
  363.                 case TypeCode.String:
  364.                     Buffer = new byte[Length];
  365.                     ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, Length, ref BytesRead);
  366.                     return (T)Convert.ChangeType(Encoding.GetEncoding("IBM865").GetString(Buffer, 0, Buffer.Length).Split(new char[] { '\0' })[0].ReplaceModifiedString(), typeof(T));
  367.                 default:
  368.                     return (T)Convert.ChangeType(false, typeof(T));
  369.             }
  370.         }
  371.  
  372.     }
  373.  
  374.     public enum Address
  375.     {
  376.         MapTileIDArray = 0x8C6928,
  377.         XStart = 0x1B4BA0,
  378.         YStart = 0x1B4BA4
  379.     }
  380.  
  381. }
  382.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement