Advertisement
lu4kedr

GameWindowSize_Phoenix_UO

Aug 19th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.88 KB | None | 0 0
  1.  
  2.  
  3. /////////////////////////////////////////////////////////////////////////
  4. //
  5. //     www.ultima.smoce.net
  6. //     Name: Game Windows Size
  7. //
  8. /////////////////////////////////////////////////////////////////////////
  9. using System;
  10. using System.ComponentModel;
  11. using System.Diagnostics;
  12. using System.Runtime.InteropServices;
  13. using Phoenix.Gui;
  14.  
  15. namespace Phoenix.Scripts.DP.Misc
  16. {
  17.     [RuntimeObject]
  18.     public class GameWindowSize
  19.     {
  20.         private const int width = 1000;
  21.         private const int height = 600;
  22.  
  23.         public GameWindowSize()//TODO: asi neni moc rychly, pustit v jinym threadu? dost mozna by se to mohlo ukladat
  24.         {
  25.             try
  26.             {
  27.                 int offset;
  28.                 if (FindPattern(sizePatternOld, out offset))
  29.                 {
  30.                     int sizeOffset = Patch((IntPtr)offset, true);
  31.  
  32.                     Marshal.WriteInt32((IntPtr)sizeOffset, 0, width);
  33.                     Marshal.WriteInt32((IntPtr)sizeOffset, 4, height);
  34.                     Debug.WriteLine("Actual GameWindowSize patched.", "GameWindowSize");
  35.  
  36.                     if (!FindPattern(thingiePattern, out offset))
  37.                         throw new Exception("Old thingie pattern not found!");
  38.                     Marshal.WriteInt32(Marshal.ReadIntPtr((IntPtr)offset, 25), width);
  39.                     Marshal.WriteInt32(Marshal.ReadIntPtr((IntPtr)offset, 35), height);
  40.                     Debug.WriteLine("Old thingie patched.", "GameWindowSize");
  41.                 }
  42.                 else if (FindPattern(sizePatternNew, out offset))
  43.                     Patch((IntPtr)offset, false);
  44.             }
  45.             catch (Exception ex) { ExceptionDialog.Show(ex, "GameWindowSize exception."); }
  46.         }
  47.  
  48.         private int Patch(IntPtr offset, bool old)
  49.         {
  50.             Debug.WriteLine("SetGameWindowSize " + (old ? "old" : "new") + " pattern found.", "GameWindowSize");
  51.  
  52.             uint oldProtect;
  53.             if (!VirtualProtect(offset, 22, 0x40, out oldProtect))
  54.                 throw new Win32Exception();
  55.  
  56.             int sizeOffset = Marshal.ReadInt32(offset, old ? 0x16 : 0x18);
  57.  
  58.             Marshal.WriteByte(offset, 0, 0xC7);
  59.             Marshal.WriteByte(offset, 1, 0x05);
  60.             Marshal.WriteInt32(offset, 2, sizeOffset);
  61.             Marshal.WriteInt32(offset, 6, width);
  62.  
  63.             Marshal.WriteByte(offset, 10, 0xC7);
  64.             Marshal.WriteByte(offset, 11, 0x05);
  65.             Marshal.WriteInt32(offset, 12, sizeOffset + 4);
  66.             Marshal.WriteInt32(offset, 16, height);
  67.  
  68.             Marshal.WriteByte(offset, 20, (byte)(old ? 0xC3 : 0xEB));//old ? retn : jmp short -27
  69.             Marshal.WriteByte(offset, 21, unchecked((byte)-27));
  70.  
  71.             Debug.WriteLine("SetGameWindowSize patched.", "GameWindowSize");
  72.             return sizeOffset;
  73.         }
  74.  
  75.         [DllImport("kernel32.dll")]
  76.         private static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);
  77.  
  78.         private bool FindPattern(byte[] pattern, out int offset)
  79.         {
  80.             ProcessModule module = Process.GetCurrentProcess().MainModule;
  81.             for (offset = module.BaseAddress.ToInt32(); offset < module.BaseAddress.ToInt32() + module.ModuleMemorySize - pattern.Length; offset++)
  82.                 for (int x = 0; x < pattern.Length; x++)
  83.                 {
  84.                     if (pattern[x] != 0xFF && Marshal.ReadByte((IntPtr)offset, x) != pattern[x])
  85.                         break;
  86.                     if (x == pattern.Length - 1)
  87.                         return true;
  88.                 }
  89.             return false;
  90.         }
  91.  
  92.         private readonly byte[] sizePatternOld = new byte[]
  93.                                         {
  94.                                             0x3D, 0x20, 0x03, 0x00, 0x00,   //cmp   eax, 320h
  95.                                             0x75, 0x1D,                     //jnz   0x1D
  96.                                             0x8B, 0x4C, 0x24, 0x08,         //mov   ecx, [esp+arg_4]
  97.                                             0xB8, 0x58, 0x02, 0x00, 0x00,   //mov   eax, 258h
  98.                                             0x3B, 0xC8,                     //cmp   ecx, eax
  99.                                             0x75, 0x10                      //jnz   0x10
  100.                                         };
  101.  
  102.         private readonly byte[] sizePatternNew = new byte[]
  103.                                         {
  104.                                             0x3D, 0x20, 0x03, 0x00, 0x00,   //cmp   eax, 320h
  105.                                             0x75, 0xE8,                     //jnz   0xE8
  106.                                             0x56,                           //push  esi
  107.                                             0x8B, 0x74, 0x24, 0x0C,         //mov   esi, [esp+4+arg_4]
  108.                                             0xB8, 0x58, 0x02, 0x00, 0x00,   //mov   eax, 258h
  109.                                             0x3B, 0xF0,                     //cmp   esi, eax
  110.                                             0x5E,                           //pop   esi
  111.                                             0x75, 0xD9                      //jnz   0xD9
  112.                                         };
  113.  
  114.         private readonly byte[] thingiePattern = new byte[]
  115.                                                  {
  116.                                                      0xA0, 0xFF, 0xFF, 0xFF, 0x00,
  117.                                                      0x83, 0xEC, 0x08,
  118.                                                      0xA8, 0x01,
  119.                                                      0x75, 0x1B,
  120.                                                      0x0C, 0x01,
  121.                                                      0xA2, 0xFF, 0xFF, 0xFF, 0x00,
  122.                                                      0xE8, 0xFF, 0xFF, 0xFF, 0x00
  123.                                                  };
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement