Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding an enum for memory type #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 100 additions & 96 deletions Memory/Methods/Write.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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 bool FreezeValue(string address, string type, string value, string file = "")
public bool FreezeValue(string address, MemoryType type, string value, string file = "")
{
CancellationTokenSource cts = new CancellationTokenSource();

Expand Down Expand Up @@ -89,7 +89,7 @@ public void UnfreezeValue(string address)
///<param name="file">path and name of .ini file (OPTIONAL)</param>
///<param name="stringEncoding">System.Text.Encoding.UTF8 (DEFAULT). Other options: ascii, unicode, utf32, utf7</param>
///<param name="RemoveWriteProtection">If building a trainer on an emulator (Ex: RPCS3) you'll want to set this to false</param>
public bool WriteMemory(string code, string type, string write, string file = "", System.Text.Encoding stringEncoding = null, bool RemoveWriteProtection = true)
public bool WriteMemory(string code, MemoryType type, string write, string file = "", System.Text.Encoding stringEncoding = null, bool RemoveWriteProtection = true)
{
byte[] memory = new byte[4];
int size = 4;
Expand All @@ -100,73 +100,75 @@ public bool WriteMemory(string code, string type, string write, string file = ""
if (theCode == null || theCode == UIntPtr.Zero || theCode.ToUInt64() < 0x10000)
return false;

if (type.ToLower() == "float")
switch (type)
{
write = Convert.ToString(float.Parse(write, CultureInfo.InvariantCulture));
memory = BitConverter.GetBytes(Convert.ToSingle(write));
size = 4;
}
else if (type.ToLower() == "int")
{
memory = BitConverter.GetBytes(Convert.ToInt32(write));
size = 4;
}
else if (type.ToLower() == "byte")
{
memory = new byte[1];
memory[0] = Convert.ToByte(write, 16);
size = 1;
}
else if (type.ToLower() == "2bytes")
{
memory = new byte[2];
memory[0] = (byte)(Convert.ToInt32(write) % 256);
memory[1] = (byte)(Convert.ToInt32(write) / 256);
size = 2;
}
else if (type.ToLower() == "bytes")
{
if (write.Contains(",") || write.Contains(" ")) //check if it's a proper array
{
string[] stringBytes;
if (write.Contains(","))
stringBytes = write.Split(',');
else
stringBytes = write.Split(' ');
//Debug.WriteLine("write:" + write + " stringBytes:" + stringBytes);
case MemoryType.FLOAT:
write = Convert.ToString(float.Parse(write, CultureInfo.InvariantCulture));
memory = BitConverter.GetBytes(Convert.ToSingle(write));
size = 4;
break;

int c = stringBytes.Count();
memory = new byte[c];
for (int i = 0; i < c; i++)
{
memory[i] = Convert.ToByte(stringBytes[i], 16);
}
size = stringBytes.Count();
}
else //wasnt array, only 1 byte
{
case MemoryType.INT:
memory = BitConverter.GetBytes(Convert.ToInt32(write));
size = 4;
break;

case MemoryType.BYTE:
memory = new byte[1];
memory[0] = Convert.ToByte(write, 16);
size = 1;
}
}
else if (type.ToLower() == "double")
{
memory = BitConverter.GetBytes(Convert.ToDouble(write));
size = 8;
}
else if (type.ToLower() == "long")
{
memory = BitConverter.GetBytes(Convert.ToInt64(write));
size = 8;
}
else if (type.ToLower() == "string")
{
if (stringEncoding == null)
memory = System.Text.Encoding.UTF8.GetBytes(write);
else
memory = stringEncoding.GetBytes(write);
size = memory.Length;
break;

case MemoryType.TWOBYTES:
memory = new byte[2];
memory[0] = (byte)(Convert.ToInt32(write) % 256);
memory[1] = (byte)(Convert.ToInt32(write) / 256);
size = 2;
break;

case MemoryType.BYTES:
if (write.Contains(",") || write.Contains(" ")) //check if it's a proper array
{
string[] stringBytes;
if (write.Contains(","))
stringBytes = write.Split(',');
else
stringBytes = write.Split(' ');
//Debug.WriteLine("write:" + write + " stringBytes:" + stringBytes);

int c = stringBytes.Count();
memory = new byte[c];
for (int i = 0; i < c; i++)
{
memory[i] = Convert.ToByte(stringBytes[i], 16);
}
size = stringBytes.Count();
}
else //wasnt array, only 1 byte
{
memory = new byte[1];
memory[0] = Convert.ToByte(write, 16);
size = 1;
}
break;

case MemoryType.DOUBLE:
memory = BitConverter.GetBytes(Convert.ToDouble(write));
size = 8;
break;

case MemoryType.LONG:
memory = BitConverter.GetBytes(Convert.ToInt64(write));
size = 8;
break;

case MemoryType.STRING:
if (stringEncoding == null)
memory = System.Text.Encoding.UTF8.GetBytes(write);
else
memory = stringEncoding.GetBytes(write);
size = memory.Length;
break;
}

//Debug.Write("DEBUG: Writing bytes [TYPE:" + type + " ADDR:" + theCode + "] " + String.Join(",", memory) + Environment.NewLine);
Expand All @@ -190,46 +192,48 @@ public bool WriteMemory(string code, string type, string write, string file = ""
/// <param name="file">path and name of .ini file (OPTIONAL)</param>
/// <param name="SlowDown">milliseconds to sleep between each byte</param>
/// <returns></returns>
public bool WriteMove(string code, string type, string write, int MoveQty, string file = "", int SlowDown = 0)
public bool WriteMove(string code, MemoryType type, string write, int MoveQty, string file = "", int SlowDown = 0)
{
byte[] memory = new byte[4];
int size = 4;

UIntPtr theCode;
theCode = GetCode(code, file);

if (type == "float")
{
memory = new byte[write.Length];
memory = BitConverter.GetBytes(Convert.ToSingle(write));
size = write.Length;
}
else if (type == "int")
{
memory = BitConverter.GetBytes(Convert.ToInt32(write));
size = 4;
}
else if (type == "double")
{
memory = BitConverter.GetBytes(Convert.ToDouble(write));
size = 8;
}
else if (type == "long")
switch (type)
{
memory = BitConverter.GetBytes(Convert.ToInt64(write));
size = 8;
}
else if (type == "byte")
{
memory = new byte[1];
memory[0] = Convert.ToByte(write, 16);
size = 1;
}
else if (type == "string")
{
memory = new byte[write.Length];
memory = System.Text.Encoding.UTF8.GetBytes(write);
size = write.Length;
case MemoryType.FLOAT:
memory = new byte[write.Length];
memory = BitConverter.GetBytes(Convert.ToSingle(write));
size = write.Length;
break;

case MemoryType.INT:
memory = BitConverter.GetBytes(Convert.ToInt32(write));
size = 4;
break;

case MemoryType.BYTE:
memory = new byte[1];
memory[0] = Convert.ToByte(write, 16);
size = 1;
break;

case MemoryType.DOUBLE:
memory = BitConverter.GetBytes(Convert.ToDouble(write));
size = 8;
break;

case MemoryType.LONG:
memory = BitConverter.GetBytes(Convert.ToInt64(write));
size = 8;
break;

case MemoryType.STRING:
memory = new byte[write.Length];
memory = System.Text.Encoding.UTF8.GetBytes(write);
size = write.Length;
break;
}

UIntPtr newCode = UIntPtr.Add(theCode, MoveQty);
Expand Down
12 changes: 11 additions & 1 deletion Memory/Structures/Imports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,16 @@ public enum ThreadInfoClass : int
ThreadQuerySetWin32StartAddress = 9
}


public enum MemoryType
{
FLOAT,
INT,
BYTE,
TWOBYTES,
BYTES,
DOUBLE,
LONG,
STRING
}
}
}