Skip to content

Commit

Permalink
Added ReadMemory and BindToUI methods
Browse files Browse the repository at this point in the history
  • Loading branch information
erfg12 committed Jan 25, 2022
1 parent c4ef051 commit 771ff6f
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 17 deletions.
76 changes: 76 additions & 0 deletions Memory/Methods/Read.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand All @@ -7,6 +8,7 @@
using System.Threading;
using System.Threading.Tasks;
using static Memory.Imps;
using System.Reflection;

namespace Memory
{
Expand Down Expand Up @@ -365,5 +367,79 @@ public string ReadPString(UIntPtr address, string code, string file = "")
else
return "";
}

public T ReadMemory<T>(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<string, CancellationTokenSource> ReadTokenSrcs = new ConcurrentDictionary<string, CancellationTokenSource>();
/// <summary>
/// Reads a memory address, keeps value in UI object. Ex: BindToUI("0x12345678,0x02,0x05", v => this.Invoke((MethodInvoker) delegate { this.name_label.Text = v; }));
/// </summary>
/// <param name="address"></param>
/// <param name="UIObject"></param>
/// <param name="file"></param>
public void BindToUI(string address, Action<string> 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<string>(address, file));
Thread.Sleep(100);
}
},
cts.Token);
}
}
}
35 changes: 18 additions & 17 deletions Memory/Methods/Write.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,29 @@ public partial class Mem
/// <param name="type">byte, 2bytes, bytes, float, int, string, double or long.</param>
/// <param name="value">Value to freeze</param>
/// <param name="file">ini file to read address from (OPTIONAL)</param>
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(() =>
{
Expand All @@ -56,6 +55,8 @@ public void FreezeValue(string address, string type, string value, string file =
}
},
cts.Token);

return true;
}

/// <summary>
Expand Down

0 comments on commit 771ff6f

Please sign in to comment.