Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace URWSSSelector
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- ReadWriteMem RWMain;
- Process URW;
- public bool Active = false;
- public bool MoveLocation = false;
- public RECT WindowRect;
- Rectangle GameRect = new Rectangle();
- byte[] LoadedMap;
- private Rectangle GameMap;
- Point ClosestLand = new Point(0, 0);
- private int[] TileIDFilter = new[] { 180, 181, 182, 183, 177, 244, 247, 178 };
- const int DWMWA_EXTENDED_FRAME_BOUNDS = 9;
- [DllImport("dwmapi.dll")]
- static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out RECT pvAttribute, int cbAttribute);
- [DllImport("user32.dll", SetLastError = true)]
- static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
- [DllImport("user32.dll")]
- public static extern IntPtr GetForegroundWindow();
- [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
- static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, int wParam, IntPtr lParam);
- [DllImport("user32.dll")]
- static extern bool SetForegroundWindow(IntPtr hWnd);
- [StructLayout(LayoutKind.Sequential)]
- public struct RECT
- {
- public int Left; // x position of upper-left corner
- public int Top; // y position of upper-left corner
- public int Right; // x position of lower-right corner
- public int Bottom; // y position of lower-right corner
- }
- Point StartLocation
- {
- get
- {
- return new Point(RWMain.Read<int>((int)Address.XStart), RWMain.Read<int>((int)Address.YStart));
- }
- set
- {
- RWMain.Write((int)Address.XStart, value.X);
- RWMain.Write((int)Address.YStart, value.Y);
- }
- }
- byte[] MapIDArray
- {
- get
- {
- return RWMain.Read<byte[]>((int)Address.MapTileIDArray, 6293502);
- }
- set
- {
- RWMain.Write((int)Address.MapTileIDArray, value);
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- try
- {
- URW = Process.GetProcessesByName("urw")[0];
- RWMain = new ReadWriteMem(URW);
- string VersionInfo = VersionCheck();
- if (VersionInfo.Length > 0) { MessageBox.Show(VersionInfo); Environment.Exit(0); }
- new Thread(WindowThread) { IsBackground = true }.Start();
- LoadedMap = MapIDArray;
- if (LoadedMap[0] == 0)
- {
- MessageBox.Show("Map has not been generated. Exiting.");
- Environment.Exit(0);
- }
- SetForegroundWindow(URW.MainWindowHandle);
- }
- catch
- {
- MessageBox.Show("Unreal world not found running. Closing URWSSSelector.");
- Environment.Exit(0);
- }
- }
- private string VersionCheck()
- {
- FileVersionInfo Info = URW.MainModule.FileVersionInfo;
- string Folder = Info.FileName.Substring(0, Info.FileName.Length - 8);
- List<string> Lines = new List<string>();
- Lines.AddRange(File.ReadAllLines(Folder + "\\news.txt"));
- for (int i = 0; i < Lines.Count; i++)
- {
- if (Lines[i].ToLower().Contains("3.63") && i < 10)
- {
- return "";
- }
- }
- return "Game version mismatch, please check thread on official forum for updates.";
- }
- private Rectangle GetMap()
- {
- Bitmap bmpScreen = new Bitmap(GameRect.Width, GameRect.Height);
- Point TopLeft = new Point(0, 0);
- Point BottomRight = new Point(0, 0);
- using (Graphics g = Graphics.FromImage(bmpScreen))
- {
- g.CopyFromScreen(GameRect.Location, new Point(0,0), GameRect.Size, CopyPixelOperation.SourceCopy);
- }
- for (int x = 0; x < bmpScreen.Width; x++)
- {
- for (int y = 0; y < bmpScreen.Height; y++)
- {
- Color Pixel = bmpScreen.GetPixel(x, y);
- if (Pixel.B > 190 && Pixel.R < 20 && Pixel.G < 30)
- {
- if (TopLeft.X == 0) { TopLeft = new Point(x, y); }
- else { BottomRight = new Point(x, y); }
- }
- }
- }
- MoveLocation = true;
- return new Rectangle(TopLeft, new Size(BottomRight.X - TopLeft.X, BottomRight.Y - TopLeft.Y));
- }
- private Point GetMouseXYMap(MouseEventArgs e)
- {
- int X = 0, Y = 0;
- if (GameMap.Width > 0)
- {
- X = (int)((e.X - GameMap.X) * (3073m / GameMap.Width));
- Y = (int)((e.Y - GameMap.Y) * (2049m / GameMap.Height));
- if (MoveLocation)
- {
- if (X < 48) { X = 48; }
- if (X > 3024) { X = 3024; }
- if (Y < 48) { Y = 48; }
- if (Y > 2000) { Y = 2000; }
- int LocationIndex = (Y * 3073) + X;
- if (!TileIDFilter.Contains((int)LoadedMap[LocationIndex]))
- {
- StartLocation = new Point(X, Y);
- }
- else // find nearest landmass
- {
- for (int XSearch = X - 200; XSearch < X + 200; XSearch++)
- {
- for (int YSearch = Y - 200; YSearch < Y + 200; YSearch++)
- {
- int SearchIndex = (YSearch * 3073) + XSearch;
- if (SearchIndex > 0 && SearchIndex < LoadedMap.Length)
- {
- if (!TileIDFilter.Contains((int)LoadedMap[SearchIndex]))
- {
- if (XSearch > 48 && XSearch < 3024 && YSearch > 48 && YSearch < 2000)
- {
- Point OldCursorDistance = new Point(Math.Abs(ClosestLand.X - X), Math.Abs(ClosestLand.Y - Y));
- Point NewCursorDistance = new Point(Math.Abs(XSearch - X), Math.Abs(YSearch - Y));
- if (OldCursorDistance.X + OldCursorDistance.Y > NewCursorDistance.X + NewCursorDistance.Y)
- {
- ClosestLand = new Point(XSearch, YSearch);
- StartLocation = ClosestLand;
- }
- }
- }
- }
- }
- }
- }
- SendMessage(URW.MainWindowHandle, 0x0102, '-', IntPtr.Zero);
- }
- }
- return new Point(X, Y);
- }
- private void WindowThread()
- {
- IntPtr ThisHandle = IntPtr.Zero;
- this.Invoke(new Action(() => ThisHandle = this.Handle));
- while (Thread.CurrentThread.IsAlive)
- {
- if (DwmGetWindowAttribute(RWMain.TargetProcess.MainWindowHandle, DWMWA_EXTENDED_FRAME_BOUNDS, out WindowRect, Marshal.SizeOf(typeof(RECT))) != 0)
- {
- GetWindowRect(RWMain.TargetProcess.MainWindowHandle, out WindowRect);
- }
- GameRect = new Rectangle(new Point(WindowRect.Left, WindowRect.Top), new Size(WindowRect.Right - WindowRect.Left, WindowRect.Bottom - WindowRect.Top));
- this.Invoke(new Action(() => this.Location = GameRect.Location));
- this.Invoke(new Action(() => this.Size = GameRect.Size));
- if (GetForegroundWindow() != RWMain.TargetProcess.MainWindowHandle && GetForegroundWindow() != ThisHandle)
- {
- this.Invoke(new Action(() => this.Visible = false));
- }
- else if (GetForegroundWindow() == RWMain.TargetProcess.MainWindowHandle)
- {
- this.Invoke(new Action(() => this.Visible = true));
- }
- if (GameMap.Width <= 0)
- {
- GameMap = GetMap();
- }
- Thread.Sleep(10);
- }
- }
- private void Form1_MouseMove(object sender, MouseEventArgs e)
- {
- Point MouseLocation = GetMouseXYMap(e);
- }
- private void Form1_MouseDown(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Right)
- {
- Environment.Exit(0);
- }
- }
- }
- public static class Extensions
- {
- public static string ReplaceModifiedString(this string s)
- {
- return s.Replace('Σ', 'ä').Replace('Ø', '¥').Replace('─', 'Ä').Replace('÷', 'ö');
- }
- }
- public class ReadWriteMem
- {
- [DllImport("Kernel32.dll")]
- private static extern bool ReadProcessMemory(IntPtr hProcess, int BaseAddress, byte[] Buffer, int Size, ref int NumberOfBytesRead);
- [DllImport("Kernel32.dll")]
- private static extern bool WriteProcessMemory(IntPtr hProcess, int BaseAddress, byte[] Buffer, int Size, ref int NumberOfBytesWritten);
- private Process Proc;
- public Process TargetProcess
- {
- get { return Proc; }
- set { Proc = value; }
- }
- public IntPtr WindowHandle = IntPtr.Zero;
- public int ProcBaseAddress = 0;
- public const int VersionOffset = 0x0000;
- public ReadWriteMem(Process T)
- {
- TargetProcess = T;
- ProcBaseAddress = T.MainModule.BaseAddress.ToInt32();
- WindowHandle = T.Handle;
- }
- public bool Write(int BaseAddress, dynamic obj, byte pb = 1)
- {
- byte[] Buffer = BitConverter.GetBytes(obj);
- int BytesWritten = 0;
- if (Buffer.Length == 2 && Buffer[1] == 0) { WriteProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, new byte[] { (byte)obj }, 1, ref BytesWritten); }
- else { WriteProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, Buffer.Length, ref BytesWritten); }
- switch (BytesWritten)
- {
- case 0: return false;
- default: return true;
- }
- }
- public bool Write(int BaseAddress, byte[] Buffer, byte pb = 1)
- {
- int BytesWritten = 0;
- WriteProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, Buffer.Length, ref BytesWritten);
- switch (BytesWritten)
- {
- case 0: return false;
- default: return true;
- }
- }
- public bool Write(int BaseAddress, string Text, byte pb = 1)
- {
- byte[] Buffer = Encoding.GetEncoding("IBM865").GetBytes(Text.ReplaceModifiedString());
- int BytesWritten = 0;
- WriteProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, Buffer.Length, ref BytesWritten);
- switch (BytesWritten)
- {
- case 0: return false;
- default: return true;
- }
- }
- public T Read<T>(int BaseAddress, byte pb = 1)
- {
- byte[] Buffer;
- int BytesRead = 0;
- switch (Type.GetTypeCode(typeof(T)))
- {
- case TypeCode.Byte:
- Buffer = new byte[1];
- ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, 1, ref BytesRead);
- return (T)Convert.ChangeType(Buffer[0], typeof(T));
- case TypeCode.Int16:
- Buffer = new byte[2];
- ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, 2, ref BytesRead);
- return (T)Convert.ChangeType(BitConverter.ToUInt16(Buffer, 0), typeof(T));
- case TypeCode.Int32:
- Buffer = new byte[4];
- ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, 4, ref BytesRead);
- return (T)Convert.ChangeType(BitConverter.ToUInt32(Buffer, 0), typeof(T));
- case TypeCode.UInt32:
- Buffer = new byte[4];
- ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, 4, ref BytesRead);
- return (T)Convert.ChangeType(BitConverter.ToUInt32(Buffer, 0), typeof(T));
- case TypeCode.Single:
- Buffer = new byte[4];
- ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, 4, ref BytesRead);
- return (T)Convert.ChangeType(BitConverter.ToSingle(Buffer, 0), typeof(T));
- default:
- return (T)Convert.ChangeType(false, typeof(T));
- }
- }
- public T Read<T>(int BaseAddress, int Length, byte pb = 1)
- {
- byte[] Buffer;
- int BytesRead = 0;
- switch (Type.GetTypeCode(typeof(T)))
- {
- case TypeCode.Object:
- Buffer = new byte[Length];
- ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, Length, ref BytesRead);
- return (T)Convert.ChangeType(Buffer, typeof(T));
- case TypeCode.String:
- Buffer = new byte[Length];
- ReadProcessMemory(Proc.Handle, (pb * ProcBaseAddress) + BaseAddress + VersionOffset, Buffer, Length, ref BytesRead);
- return (T)Convert.ChangeType(Encoding.GetEncoding("IBM865").GetString(Buffer, 0, Buffer.Length).Split(new char[] { '\0' })[0].ReplaceModifiedString(), typeof(T));
- default:
- return (T)Convert.ChangeType(false, typeof(T));
- }
- }
- }
- public enum Address
- {
- MapTileIDArray = 0x8C6928,
- XStart = 0x1B4BA0,
- YStart = 0x1B4BA4
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement