Skip to content

Commit

Permalink
Add new wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
marcussacana committed Aug 1, 2019
1 parent 4ce4433 commit 70e993f
Show file tree
Hide file tree
Showing 10 changed files with 1,313 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Help/Readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ At WordWrap
-"Monospaced" When false the FontName, FontSize, Bold are required
-"FakeBreakLine" If the engine don't have any breakline char, the fakebreakline full the line with space to automatically break the line where needed
-"FontName" The font face name to be used in the string measure
-"FontSize" The font size to be used in hte string measure (decimal)
-"FontSize" The font size to be used in the string measure (decimal)
-"Bold" If true, the string will measure using bold font
-"MaxWidth" When Monospaced is true, put the max count of characters per line, when false, the max line width in pixels

Expand Down
4 changes: 3 additions & 1 deletion SRL/Hook/IntroInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ static bool hMoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool

return Rst;
}

#if !DEBUG
static IntPtr hCreateWindowEx(WindowStylesEx dwExStyle, string lpClassName, string lpWindowName, WindowStyles dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam)
{
if (!CreateWindowExWHook.ImportHook)
Expand All @@ -112,7 +114,7 @@ static IntPtr hCreateWindowEx(WindowStylesEx dwExStyle, string lpClassName, stri
ShowIntro(Result);
return Result;
}

#endif
static void ShowIntro(IntPtr hWnd) {
if (IntroInitialized)
return;
Expand Down
6 changes: 3 additions & 3 deletions SRL/PipeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ private static string GetEntry(string Key) {
}

if (Debugging)
Log("Database Changed to {0}, ID: {0}", true, GetDBNameById(DBID), DBID);
Log("Database Changed to {0}, ID: {1}", true, GetDBNameById(DBID), DBID);

return Result;
}
Expand All @@ -356,7 +356,7 @@ private static string GetEntry(string Key) {
if (Debugging) {
int NID = GetDBID();
if (OID != NID) {
Log("Database Changed to {0}, ID: {0}", true, GetDBNameById(NID), NID);
Log("Database Changed to {0}, ID: {1}", true, GetDBNameById(NID), NID);
}
}

Expand All @@ -374,7 +374,7 @@ private static void SetDBID(int ID)
{
if (Debugging)
{
Log("Database Changed to {0}, ID: {0}", true, GetDBNameById(ID), ID);
Log("Database Changed to {0}, ID: {1}", true, GetDBNameById(ID), ID);
}

if (Multithread)
Expand Down
4 changes: 4 additions & 0 deletions SRL/SRL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@
<Compile Include="Types.cs" />
<Compile Include="Unmanaged.cs" />
<Compile Include="Variables.cs" />
<Compile Include="Wrapper\bink.cs" />
<Compile Include="Wrapper\d3d9.cs" />
<Compile Include="Wrapper\dinput8.cs" />
<Compile Include="Wrapper\kirikiri.cs" />
<Compile Include="Wrapper\Util.cs" />
</ItemGroup>
<ItemGroup>
<None Include="SRL.ini">
Expand Down
1 change: 0 additions & 1 deletion SRL/Variables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ struct Range {

#if DEBUG
static bool HookCreateWindow;
static bool HookCreateWindowEx;
static bool HookSendMessage;
#endif
static bool ImportHook;
Expand Down
234 changes: 234 additions & 0 deletions SRL/Wrapper/Util.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

namespace SRL.Wrapper {
static class Tools {

internal static string CurrentDllName = Path.GetFileName(Assembly.GetExecutingAssembly().Location).ToLower();
internal static string CurrentDllPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

static bool WOW64 => !Environment.Is64BitProcess && Environment.Is64BitOperatingSystem;


[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate IntPtr RET_28(IntPtr A, IntPtr B, IntPtr C, IntPtr D, IntPtr E, IntPtr F, IntPtr G, IntPtr H, IntPtr I, IntPtr J, IntPtr K, IntPtr L, IntPtr M, IntPtr N, IntPtr O, IntPtr P, IntPtr Q, IntPtr R, IntPtr S, IntPtr T, IntPtr U, IntPtr V, IntPtr W, IntPtr X, IntPtr Y, IntPtr Z, IntPtr AA, IntPtr AB);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate IntPtr RET_12(IntPtr A, IntPtr B, IntPtr C, IntPtr D, IntPtr E, IntPtr F, IntPtr G, IntPtr H, IntPtr I, IntPtr J, IntPtr K, IntPtr L);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate IntPtr RET_11(IntPtr A, IntPtr B, IntPtr C, IntPtr D, IntPtr E, IntPtr F, IntPtr G, IntPtr H, IntPtr I, IntPtr J, IntPtr K);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate IntPtr RET_10(IntPtr A, IntPtr B, IntPtr C, IntPtr D, IntPtr E, IntPtr F, IntPtr G, IntPtr H, IntPtr I, IntPtr J);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate IntPtr RET_9(IntPtr A, IntPtr B, IntPtr C, IntPtr D, IntPtr E, IntPtr F, IntPtr G, IntPtr H, IntPtr I);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate IntPtr RET_8(IntPtr A, IntPtr B, IntPtr C, IntPtr D, IntPtr E, IntPtr F, IntPtr G, IntPtr H);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate IntPtr RET_7(IntPtr A, IntPtr B, IntPtr C, IntPtr D, IntPtr E, IntPtr F, IntPtr G);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate IntPtr RET_6(IntPtr A, IntPtr B, IntPtr C, IntPtr D, IntPtr E, IntPtr F);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate IntPtr RET_5(IntPtr A, IntPtr B, IntPtr C, IntPtr D, IntPtr E);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate IntPtr RET_4(IntPtr A, IntPtr B, IntPtr C, IntPtr D);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate IntPtr RET_3(IntPtr A, IntPtr B, IntPtr C);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate IntPtr RET_2(IntPtr A, IntPtr B);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate IntPtr RET_1(IntPtr A);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate IntPtr RET_0();


[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate void NULL_12(IntPtr A, IntPtr B, IntPtr C, IntPtr D, IntPtr E, IntPtr F, IntPtr G, IntPtr H, IntPtr I, IntPtr J, IntPtr K, IntPtr L);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate void NULL_8(IntPtr A, IntPtr B, IntPtr C, IntPtr D, IntPtr E, IntPtr F, IntPtr G, IntPtr H);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate void NULL_5(IntPtr A, IntPtr B, IntPtr C, IntPtr D, IntPtr E);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate void NULL_4(IntPtr A, IntPtr B, IntPtr C, IntPtr D);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate void NULL_3(IntPtr A, IntPtr B, IntPtr C);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate void NULL_2(IntPtr A, IntPtr B);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate void NULL_1(IntPtr A);

[UnmanagedFunctionPointer(CallingConvention.Winapi)]
internal delegate void NULL_0();

[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName);

[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern IntPtr LoadLibraryW(string lpFileName);

internal static IntPtr LoadLibrary(string lpFileName) {
string DllPath = lpFileName;
if (lpFileName.Length < 2 || lpFileName[1] != ':') {
string DLL = Path.GetFileNameWithoutExtension(lpFileName);
DllPath = Path.Combine(Environment.CurrentDirectory, $"{DLL}_ori.dll");


if (!File.Exists(DllPath) && CurrentDllName != lpFileName.ToLower())
DllPath = Path.Combine(Environment.CurrentDirectory, $"{DLL}.dll");

if (!File.Exists(DllPath) && CurrentDllName != lpFileName.ToLower())
DllPath = Path.Combine(CurrentDllPath, $"{DLL}_ori.dll");

if (!File.Exists(DllPath) && CurrentDllName != lpFileName.ToLower())
DllPath = Path.Combine(CurrentDllPath, $"{DLL}.dll.ori");

if (!File.Exists(DllPath)) {
DllPath = WOW64 ? Environment.GetFolderPath(Environment.SpecialFolder.SystemX86) : Environment.SystemDirectory;
DllPath = Path.Combine(DllPath, $"{DLL}.dll");
}
}

IntPtr Handler = LoadLibraryW(DllPath);

if (Handler == IntPtr.Zero)
Environment.Exit(0x505);//ERROR_DELAY_LOAD_FAILED

return Handler;
}

internal static T GetDelegate<T>(IntPtr Handler, string Function, bool Optional = true) where T : Delegate {
IntPtr Address = GetProcAddress(Handler, Function);
if (Address == IntPtr.Zero) {
if (Optional) {
return null;
}

Environment.Exit(0x505);//ERROR_DELAY_LOAD_FAILED
}
return (T)Marshal.GetDelegateForFunctionPointer(Address, typeof(T));
}

public static bool IsWow64(Process Target) {
try {
if (IsWow64Process(Target.Handle, out bool WOW64)) {
if (Environment.Is64BitOperatingSystem && WOW64)
return true;
}
} catch { }
return false;
}


static ulong ToUInt64(this IntPtr Ptr) => unchecked((ulong)Ptr.ToInt64());
static uint ToUInt32(this IntPtr Ptr) => unchecked((uint)(Ptr.ToInt64() & 0xFFFFFFFF));

static IntPtr ToIntPtr(this ulong Int) => new IntPtr(unchecked((long)Int));

static IntPtr Sum(this IntPtr Pointer, IntPtr Value) => (Pointer.ToUInt64() + Value.ToUInt64()).ToIntPtr();
static IntPtr Sum(this IntPtr Pointer, long Value) => (Pointer.ToUInt64() + (ulong)Value).ToIntPtr();

static uint ToUInt32(this byte[] Data, int Address = 0) => BitConverter.ToUInt32(Data, Address);
static ushort ToUInt16(this byte[] Data, int Address = 0) => BitConverter.ToUInt16(Data, Address);
static int ToInt32(this byte[] Data, int Address = 0) => BitConverter.ToInt32(Data, Address);
static long ToInt64(this byte[] Data, int Address = 0) => BitConverter.ToInt64(Data, Address);

static IntPtr ToIntPtr(this byte[] Data, bool? x64 = null) {
if (x64.HasValue)
return new IntPtr(x64.Value ? Data.ToInt64() : Data.ToInt32());
if (Data.Length >= 8)
return new IntPtr(IntPtr.Size == 8 ? Data.ToInt64() : Data.ToInt32());
return new IntPtr(Data.ToInt32());
}


static bool ChangeProtection(IntPtr Address, int Range, Protection Protection, out Protection OriginalProtection) {
return VirtualProtect(Address, Range, Protection, out OriginalProtection);
}

static bool ChangeProtection(IntPtr Address, int Range, Protection Protection) {
return VirtualProtect(Address, Range, Protection, out Protection OriginalProtection);
}

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool VirtualProtect(IntPtr lpAddress, int dwSize, Protection flNewProtect, out Protection lpflOldProtect);

[DllImport("psapi.dll", SetLastError = true)]
public static extern bool EnumProcessModulesEx(IntPtr hProcess, [Out] IntPtr lphModule, uint cb, out uint lpcbNeeded, DwFilterFlag dwff);

[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
public static extern bool IsWow64Process([In] IntPtr processHandle, [Out, MarshalAs(UnmanagedType.Bool)] out bool wow64Process);

[DllImport("psapi.dll", SetLastError = true)]
public static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, int nSize);

enum Protection {
PAGE_NOACCESS = 0x01,
PAGE_READONLY = 0x02,
PAGE_READWRITE = 0x04,
PAGE_WRITECOPY = 0x08,
PAGE_EXECUTE = 0x10,
PAGE_EXECUTE_READ = 0x20,
PAGE_EXECUTE_READWRITE = 0x40,
PAGE_EXECUTE_WRITECOPY = 0x80,
PAGE_GUARD = 0x100,
PAGE_NOCACHE = 0x200,
PAGE_WRITECOMBINE = 0x400
}
public enum DwFilterFlag : uint {
LIST_MODULES_DEFAULT = 0x0,
LIST_MODULES_32BIT = 0x01,
LIST_MODULES_64BIT = 0x02,
LIST_MODULES_ALL = (LIST_MODULES_32BIT | LIST_MODULES_64BIT)
}

}

struct ImportEntry {

/// <summary>
/// The Imported Module Name
/// </summary>
public string Module;

/// <summary>
/// The Imported Function Name
/// </summary>
public string Function;

/// <summary>
/// The Import Ordinal Hint
/// </summary>
public ushort Ordinal;

/// <summary>
/// The Address of this Import in the IAT (Import Address Table)
/// </summary>
public IntPtr ImportAddress;

/// <summary>
/// The Address of the Imported Function
/// </summary>
public IntPtr FunctionAddress;
}
}
Loading

0 comments on commit 70e993f

Please sign in to comment.