From 771ff6f8bebddccfe23d027d75b81e585c1bb19d Mon Sep 17 00:00:00 2001 From: Jake Date: Mon, 24 Jan 2022 22:48:48 -0500 Subject: [PATCH] Added ReadMemory and BindToUI methods --- Memory/Methods/Read.cs | 76 +++++++++++++++++++++++++++++++++++++++++ Memory/Methods/Write.cs | 35 ++++++++++--------- 2 files changed, 94 insertions(+), 17 deletions(-) diff --git a/Memory/Methods/Read.cs b/Memory/Methods/Read.cs index f1b01d2..3c34461 100644 --- a/Memory/Methods/Read.cs +++ b/Memory/Methods/Read.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; @@ -7,6 +8,7 @@ using System.Threading; using System.Threading.Tasks; using static Memory.Imps; +using System.Reflection; namespace Memory { @@ -365,5 +367,79 @@ public string ReadPString(UIntPtr address, string code, string file = "") else return ""; } + + public T ReadMemory(string address, string file = "") + { + object ReadOutput = null; + + switch (Type.GetTypeCode(typeof(T))) + { + case TypeCode.String: + ReadOutput = ReadString(address, file); + break; + case TypeCode.Int32: + ReadOutput = ReadInt(address, file); + break; + case TypeCode.Int64: + ReadOutput = ReadLong(address, file); + break; + case TypeCode.Byte: + ReadOutput = ReadByte(address, file); + break; + case TypeCode.Double: + ReadOutput = ReadDouble(address, file); + break; + case TypeCode.Decimal: + ReadOutput = ReadFloat(address, file); + break; + case TypeCode.UInt32: + ReadOutput = ReadUInt(address, file); + break; + default: + break; + } + + return (T) Convert.ChangeType(ReadOutput, typeof(T)); + } + + ConcurrentDictionary ReadTokenSrcs = new ConcurrentDictionary(); + /// + /// Reads a memory address, keeps value in UI object. Ex: BindToUI("0x12345678,0x02,0x05", v => this.Invoke((MethodInvoker) delegate { this.name_label.Text = v; })); + /// + /// + /// + /// + public void BindToUI(string address, Action UIObject, string file = "") + { + CancellationTokenSource cts = new CancellationTokenSource(); + if (ReadTokenSrcs.ContainsKey(address)) + { + try + { + ReadTokenSrcs[address].Cancel(); + ReadTokenSrcs.TryRemove(address, out _); + } + catch + { + Debug.WriteLine("ERROR: Avoided a crash. Address " + address + " was not bound."); + } + } + else + { + Debug.WriteLine("Adding Bound Address " + address); + } + + ReadTokenSrcs.TryAdd(address, cts); + + Task.Factory.StartNew(() => + { + while (!cts.Token.IsCancellationRequested) + { + UIObject(ReadMemory(address, file)); + Thread.Sleep(100); + } + }, + cts.Token); + } } } diff --git a/Memory/Methods/Write.cs b/Memory/Methods/Write.cs index d058b35..8d2bbc3 100644 --- a/Memory/Methods/Write.cs +++ b/Memory/Methods/Write.cs @@ -22,30 +22,29 @@ public partial class Mem /// byte, 2bytes, bytes, float, int, string, double or long. /// Value to freeze /// ini file to read address from (OPTIONAL) - public void FreezeValue(string address, string type, string value, string file = "") + public bool FreezeValue(string address, string type, string value, string file = "") { CancellationTokenSource cts = new CancellationTokenSource(); - lock (FreezeTokenSrcs) + if (FreezeTokenSrcs.ContainsKey(address)) { - if (FreezeTokenSrcs.ContainsKey(address)) + Debug.WriteLine("Changing Freezing Address " + address + " Value " + value); + try { - Debug.WriteLine("Changing Freezing Address " + address + " Value " + value); - try - { - FreezeTokenSrcs[address].Cancel(); - FreezeTokenSrcs.TryRemove(address, out _); - } - catch - { - Debug.WriteLine("ERROR: Avoided a crash. Address " + address + " was not frozen."); - } + FreezeTokenSrcs[address].Cancel(); + FreezeTokenSrcs.TryRemove(address, out _); + } + catch + { + Debug.WriteLine("ERROR: Avoided a crash. Address " + address + " was not frozen."); + return false; } - else - Debug.WriteLine("Adding Freezing Address " + address + " Value " + value); - - FreezeTokenSrcs.TryAdd(address, cts); } + else { + Debug.WriteLine("Adding Freezing Address " + address + " Value " + value); + } + + FreezeTokenSrcs.TryAdd(address, cts); Task.Factory.StartNew(() => { @@ -56,6 +55,8 @@ public void FreezeValue(string address, string type, string value, string file = } }, cts.Token); + + return true; } ///