Skip to content

Commit

Permalink
Fix AdvHD Auto Install
Browse files Browse the repository at this point in the history
  • Loading branch information
marcussacana committed Mar 14, 2020
1 parent 37c0e97 commit 26c6bcd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
31 changes: 15 additions & 16 deletions StringReloads/AutoInstall/AdvHD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

using StringReloads.AutoInstall.Base;
Expand All @@ -14,6 +15,12 @@ namespace StringReloads.AutoInstall
{
unsafe class AdvHD : IAutoInstall
{

~AdvHD() {
if (Hook.HookFunction != null)
Marshal.FreeHGlobal(new IntPtr(Hook.HookFunction));
}

Config Settings => EntryPoint.SRL.Settings;

SysAllocString Hook;
Expand Down Expand Up @@ -41,16 +48,14 @@ public bool IsCompatible()

public void Uninstall() => Hook.Uninstall();

//+ 0x15 = SRL; + 0x29 = RealFunc
byte[] _HookData = null;
private void Compile()
{
if (_HookData != null)
if (Hook.HookFunction != null)
return;

string[] SkipList = new string[] {
"st", "ev", "bg", "@", "text", "char", "timer", "effect", "movie"
};
};

List<byte> Buffer = new List<byte>();
Buffer.AddRange(HookDataBase);
Expand All @@ -59,22 +64,16 @@ private void Compile()
Buffer.AddRange(Encoding.Unicode.GetBytes(Skip + "\x0"));

Buffer.Add(0x00);
Buffer.Add(0x00);
Buffer.Add(0x00);

_HookData = Buffer.ToArray();

Hook.HookFunction = _HookData.ToPointer();
Hook.HookFunction = Buffer.AllocUnsafe();
Hook.Compile();

var pFunc = (uint)EntryPoint.GetDirectProcess();
BitConverter.GetBytes(pFunc).CopyTo(_HookData, 0x15);

pFunc = (uint)Hook.BypassFunction;
BitConverter.GetBytes(pFunc).CopyTo(_HookData, 0x29);

_HookData.DeprotectMemory();
}
*((uint*)Hook.HookFunction + 0x15) = (uint)EntryPoint.GetDirectProcess();
*((uint*)Hook.HookFunction + 0x29) = (uint)Hook.BypassFunction;
}

//+ 0x15 = SRL; + 0x29 = RealFunc
static byte[] HookDataBase = new byte[] {
0x58, 0x87, 0x04, 0x24, 0x60, 0x50, 0x50, 0xE8, 0x34, 0x00, 0x00, 0x00, 0x85, 0xC0,
0x74, 0x15, 0x90, 0x90, 0x90, 0x90, 0xB8, 0xAA, 0xAA, 0xAA, 0xAA, 0xFF, 0xD0, 0x89,
Expand Down
22 changes: 7 additions & 15 deletions StringReloads/Hook/Base/Hook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Runtime.InteropServices;

using static StringReloads.Hook.Base.Extensions;
using System.Linq;
using System.Collections.Generic;

namespace StringReloads.Hook.Base
{
Expand Down Expand Up @@ -256,28 +258,18 @@ public static InstructionList DecodeMany(this Decoder Decoder, uint MinLength)
return List;
}

public unsafe static byte* ToPointer(this byte[] Buffer) {
fixed (void* pBuff = &Buffer[0])
return (byte*)pBuff;
}

public unsafe static byte* AllocUnsafe(uint Bytes)
{
return VirtualAlloc(null, Bytes, AllocationType.Reserve | AllocationType.Commit, MemoryProtection.ExecuteReadWrite);;
}
public unsafe static byte* AllocUnsafe(this byte[] Buffer) {
var Addr = VirtualAlloc(null, (uint)Buffer.Length, AllocationType.Reserve | AllocationType.Commit, MemoryProtection.ExecuteReadWrite);
for (int i = 0; i < Buffer.Length; i++)
*(Addr + i) = Buffer[i];
public unsafe static byte* AllocUnsafe(this IEnumerable<byte> Buffer) {
var tmp = Buffer.ToArray();
var Addr = VirtualAlloc(null, (uint)tmp.Length, AllocationType.Reserve | AllocationType.Commit, MemoryProtection.ExecuteReadWrite);
for (int i = 0; i < tmp.Length; i++)
*(Addr + i) = tmp[i];
return Addr;
}

public unsafe static void DeprotectMemory(this byte[] Buffer, bool ExecutableOnly = false)
{
var pBuff = Buffer.ToPointer();
VirtualProtect(pBuff, (uint)Buffer.Length, ExecutableOnly ? MemoryProtection.ExecuteRead : MemoryProtection.ExecuteReadWrite, out _);
}

public unsafe static void DeprotectMemory(void* Buffer, uint Length, bool ExecutableOnly = false)
{
VirtualProtect(Buffer, Length, ExecutableOnly ? MemoryProtection.ExecuteRead : MemoryProtection.ExecuteReadWrite, out _);
Expand Down

0 comments on commit 26c6bcd

Please sign in to comment.