Skip to content

Commit

Permalink
New Hook and Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcussacana committed Oct 10, 2018
1 parent 792917f commit 34a6ca0
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 18 deletions.
11 changes: 11 additions & 0 deletions SRL/FileWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,17 @@ private static void LoadConfig() {
Log("SetWindowText Hook Enabled", true);
} else if (HookSetWindowText)
Warning("SetWindowText Hook Settings Changed - Restart Required");
new Thread(() => {
while (!DialogFound)
Thread.Sleep(100);
if (HookSettings.MultiByteToWideChar) {
if (!HookMultiByteToWideChar)
InstallMultiByteToWideChar();
HookMultiByteToWideChar = true;
Log("MultiByteToWideChar Hook Enabled", true);
} else if (HookMultiByteToWideChar)
Warning("MultiByteToWideChar Hook Settings Changed - Restart Required");
}).Start();

if (HookSettings.UndoChars) {
UndoChars = true;
Expand Down
45 changes: 43 additions & 2 deletions SRL/Hook/GenericHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ static partial class StringReloader {
static CreateFontWDelegate dCreateFontW = null;
static CreateFontIndirectADelegate dCreateFontIndirectA = null;
static CreateFontIndirectWDelegate dCreateFontIndirectW = null;
static MultiByteToWideCharDelegate dMultiByteToWideChar = null;
#if DEBUG
static SendMessageADelegate dSendMessageA = null;
static SendMessageWDelegate dSendMessageW = null;
Expand Down Expand Up @@ -46,6 +47,9 @@ static partial class StringReloader {
static FxHook hSetWindowTextA;
static FxHook hSetWindowTextW;


static FxHook hMultiByteToWideChar;

static void InstallGlyphHooks() {
dOutlineA = new GetGlyphOutlineDelegate(hGetGlyphOutlineA);
dOutlineW = new GetGlyphOutlineDelegate(hGetGlyphOutlineW);
Expand Down Expand Up @@ -147,6 +151,14 @@ static void InstallSetWindowTextHooks() {
hSetWindowTextW.Install();
}

static void InstallMultiByteToWideChar() {
dMultiByteToWideChar = new MultiByteToWideCharDelegate(MultiByteToWideCharHook);

hMultiByteToWideChar = new FxHook("kernel32.dll", "MultiByteToWideChar", dMultiByteToWideChar);

hMultiByteToWideChar.Install();
}

public static uint hGetGlyphOutlineA(IntPtr hdc, uint uChar, uint uFormat, out GLYPHMETRICS lpgm, uint cbBuffer, IntPtr lpvBuffer, ref MAT2 lpmat2) {
uChar = (uint)ParsePtr(ProcessReal(new IntPtr((int)uChar)));

Expand Down Expand Up @@ -381,6 +393,31 @@ static bool SetWindowTextHook(IntPtr hwnd, string lpString) {
return Ret;
}

static int MultiByteToWideCharHook(int Codepage, uint dwFlags, IntPtr Input, int cbMultiByte, IntPtr Output, int cchWideChar) {
hMultiByteToWideChar.Uninstall();
if (!Initialized || cbMultiByte == 0) {
int Rst = MultiByteToWideChar(Codepage, dwFlags, Input, cbMultiByte, Output, cchWideChar);
hMultiByteToWideChar.Install();
return Rst;
}
string Str = GetString(Input, Len: cbMultiByte == -1 ? null : (int?)cbMultiByte, CP: Codepage);
string RStr = Str;
RStr = StrMap(Str, Input, true);

if (RStr == Str) {
int Rst = MultiByteToWideChar(Codepage, dwFlags, Input, cbMultiByte, Output, cchWideChar);
hMultiByteToWideChar.Install();
return Rst;
}

Log("MBTWC Hook: {0}", true, RStr);

Output = GenString(RStr, true, Output == IntPtr.Zero ? null : (IntPtr?)Output);

hMultiByteToWideChar.Install();
return cchWideChar;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct LOGFONTA {
public const int LF_FACESIZE = 32;
Expand Down Expand Up @@ -457,10 +494,10 @@ struct LOGFONTW {


[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true)]
delegate Int32 SendMessageADelegate(int hWnd, int Msg, int wParam, IntPtr lParam);
delegate int SendMessageADelegate(int hWnd, int Msg, int wParam, IntPtr lParam);

[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true)]
delegate Int32 SendMessageWDelegate(int hWnd, int Msg, int wParam, IntPtr lParam);
delegate int SendMessageWDelegate(int hWnd, int Msg, int wParam, IntPtr lParam);


[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
Expand All @@ -481,5 +518,9 @@ struct LOGFONTW {

[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Unicode)]
delegate bool SetWindowTextWDelegate(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)] string lpString);


[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true)]
delegate int MultiByteToWideCharDelegate(int CodePage, uint dwFlags, IntPtr lpMultiByteStr, int cbMultiByte, IntPtr lpWideCharStr, int cchWideChar);
}
}
2 changes: 2 additions & 0 deletions SRL/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ private static IntPtr ProcessReal(IntPtr Target) {
return Target;
}

DialogFound = true;

if (!LiteMode) {
if (StringModifier != null) {
try {
Expand Down
5 changes: 0 additions & 5 deletions SRL/Reloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ internal static string StrMap(string Input, IntPtr InputPtr, bool Native) {
}

if (ContainsKey(Str)) {
DialogFound = true;

string Entry = GetEntry(Str);

string Rst = EnableWordWrap ? WordWrap(Entry) : Entry;
Expand All @@ -124,7 +122,6 @@ internal static string StrMap(string Input, IntPtr InputPtr, bool Native) {

Str = SimplfyMatch(Str);
if (ContainsKey(Str)) {
DialogFound = true;

string Entry = GetEntry(Str);

Expand All @@ -137,7 +134,6 @@ internal static string StrMap(string Input, IntPtr InputPtr, bool Native) {

if (ValidateMask(Input)) {
try {
DialogFound = true;

string Result = ProcesMask(Input);

Expand All @@ -151,7 +147,6 @@ internal static string StrMap(string Input, IntPtr InputPtr, bool Native) {
return Result;
} catch (Exception ex) {
Warning(ex.ToString());
DialogFound = false;
}
}

Expand Down
1 change: 1 addition & 0 deletions SRL/SRL.ini
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ UseDB=true

[Hook]
UndoChars=true
MultiByteToWideChar=false
SetWindowText=false
GetGlyphOutline=false
TextOut=false
Expand Down
42 changes: 31 additions & 11 deletions SRL/StringWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,22 +399,22 @@ private static int BreakLine(string text, int pos, int max) {
/// <param name="Pointer">The pointer to the string</param>
/// <param name="Decode">if False, return the hex of the string</param>
/// <returns>The String</returns>
internal static string GetString(IntPtr Pointer, bool Decode = true) {
internal static string GetString(IntPtr Pointer, bool Decode = true, int? Len = null, int? CP = null) {
if (EncodingModifier != null)
return EncodingModifier.Call("Modifier", "GetString", Pointer, Decode);

if (Unicode)
if (Unicode && CP == null && Len == null)
return GetStringW(Pointer, Decode);
else
return GetStringA(Pointer, Decode);
return GetStringA(Pointer, Decode, CP, Len);
}

/// <summary>
/// Gen a Null Terminated String
/// </summary>
/// <param name="String">The string</param>
/// <returns>The Pointer to the new String</returns>
internal static IntPtr GenString(string String, bool ForceUnicode = false) {
internal static IntPtr GenString(string String, bool ForceUnicode = false, IntPtr? ForcePointer = null) {
byte[] buffer;
if (ForceUnicode) {
buffer = Encoding.Unicode.GetBytes(String + "\x0");
Expand All @@ -441,7 +441,7 @@ internal static IntPtr GenString(string String, bool ForceUnicode = false) {
#endif
}
}
IntPtr Pointer = Marshal.AllocHGlobal(buffer.Length);
IntPtr Pointer = ForcePointer ?? Marshal.AllocHGlobal(buffer.Length);

Marshal.Copy(buffer, 0, Pointer, buffer.Length);

Expand All @@ -459,10 +459,13 @@ internal static IntPtr GenString(string String, bool ForceUnicode = false) {
/// <param name="Pointer">Pointer to the string</param>
/// <param name="Decode">if False, return the hex of the string</param>
/// <returns></returns>
internal static string GetStringA(IntPtr Pointer, bool Decode = true) {
internal static string GetStringA(IntPtr Pointer, bool Decode = true, int? CP = null, int? Len = null) {
int len = 0;
while (Marshal.ReadByte(Pointer, len) != 0)
++len;
if (Len == null) {
while (Marshal.ReadByte(Pointer, len) != 0)
++len;
} else
len = Len.Value;

byte[] buffer = new byte[len];
Marshal.Copy(Pointer, buffer, 0, buffer.Length);
Expand All @@ -471,10 +474,25 @@ internal static string GetStringA(IntPtr Pointer, bool Decode = true) {
Log("Input: {0}", true, ParseBytes(buffer));
}

if (Unicode) {
if (Unicode && CP == null) {
return Encoding.Default.GetString(buffer);
} else {
if (Decode)
if (CP != null) {
Encoding Enco;
switch (CP) {
case 0:
case 3:
case 2:
case 1:
Enco = Encoding.Default;
break;

default:
Enco = (from x in Encoding.GetEncodings() where x.CodePage == CP select x.GetEncoding()).FirstOrDefault() ?? WriteEncoding;
break;
}
return Enco.GetString(buffer);
} else if (Decode)
return ReadEncoding.GetString(buffer);
else
return ParseBytes(buffer);
Expand Down Expand Up @@ -504,7 +522,6 @@ internal static string GetStringW(IntPtr Pointer, bool Decode = true, bool Force
return ParseBytes(buffer);
}


/// <summary>
/// Check if a Char is in the User Acceptable Range
/// </summary>
Expand Down Expand Up @@ -775,6 +792,9 @@ public static bool IsDialog(this string String) {

if (UseDatabase && ContainsKey(String))
return true;

if (!DialogCheck)
return true;

string Str = String.Trim();
foreach (string Ignore in IgnoreList)
Expand Down
2 changes: 2 additions & 0 deletions SRL/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ internal struct HookSettings {
[FieldParmaters(Name = "SendMessage", DefaultValue = false)]
public bool SendMessage;
#endif
[FieldParmaters(Name = "MultiByteToWideChar", DefaultValue = false)]
public bool MultiByteToWideChar;
[FieldParmaters(Name = "SetWindowText", DefaultValue = false)]
public bool SetWindowText;
[FieldParmaters(Name = "GetGlyphOutline", DefaultValue = false)]
Expand Down
3 changes: 3 additions & 0 deletions SRL/Unmanaged.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace SRL {
partial class StringReloader {
[DllImport("kernel32.dll", SetLastError = true)]
private static extern int MultiByteToWideChar(int CodePage, uint dwFlags, IntPtr lpMultiByteStr, int cbMultiByte, IntPtr lpWideCharStr, int cchWideChar);

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
static extern IntPtr CreateWindowExA(WindowStylesEx dwExStyle, [MarshalAs(UnmanagedType.LPStr)] string lpClassName, [MarshalAs(UnmanagedType.LPStr)] string lpWindowName, WindowStyles dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);

Expand Down
1 change: 1 addition & 0 deletions SRL/Variables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ struct Range {
static bool HookCreateWindowEx;
static bool HookSendMessage;
#endif
static bool HookMultiByteToWideChar;
static bool HookSetWindowText;
static bool HookGlyphOutline;
static bool HookTextOut;
Expand Down

0 comments on commit 34a6ca0

Please sign in to comment.