Skip to content

Commit 5c68b11

Browse files
committed
Unified bitness check.
1 parent 8f6f6b3 commit 5c68b11

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

ReClass.NET/AddressParser/DynamicCompiler.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,9 @@ private static Expression GenerateMethodBody(IExpression operation, ParameterExp
8484
}
8585
case ConstantExpression constantExpression:
8686
{
87-
#if RECLASSNET64
88-
// long -> IntPtr
89-
return Expression.Convert(Expression.Constant(constantExpression.Value), typeof(IntPtr));
90-
#else
91-
// long -> int -> IntPtr
92-
return Expression.Convert(Expression.Convert(Expression.Constant(constantExpression.Value), typeof(int)), typeof(IntPtr));
93-
#endif
87+
var convertFn = typeof(IntPtrExtension).GetRuntimeMethod(nameof(IntPtrExtension.From), new[] { typeof(long) });
88+
89+
return Expression.Call(null, convertFn, Expression.Constant(constantExpression.Value));
9490
}
9591
case ReadMemoryExpression readMemoryExpression:
9692
{
@@ -101,11 +97,10 @@ private static Expression GenerateMethodBody(IExpression operation, ParameterExp
10197

10298
var callExpression = Expression.Call(processParameter, readRemoteIntFn, argument);
10399

104-
#if RECLASSNET64
105-
return Expression.Convert(callExpression, typeof(IntPtr));
106-
#else
107-
return Expression.Convert(Expression.Convert(callExpression, typeof(int)), typeof(IntPtr));
108-
#endif
100+
var paramType = readMemoryExpression.ByteCount == 4 ? typeof(int) : typeof(long);
101+
var convertFn = typeof(IntPtrExtension).GetRuntimeMethod(nameof(IntPtrExtension.From), new[] { paramType });
102+
103+
return Expression.Call(null, convertFn, callExpression);
109104
}
110105
}
111106

ReClass.NET/AddressParser/Interpreter.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ public IntPtr Execute(IExpression expression, RemoteProcess process)
1515
switch (expression)
1616
{
1717
case ConstantExpression constantExpression:
18-
#if RECLASSNET64
19-
return (IntPtr)constantExpression.Value;
20-
#else
21-
return (IntPtr)unchecked((int)constantExpression.Value);
22-
#endif
18+
return IntPtrExtension.From(constantExpression.Value);
2319
case ModuleExpression moduleExpression:
2420
{
2521
var module = process.GetModuleByName(moduleExpression.Name);
@@ -42,15 +38,11 @@ public IntPtr Execute(IExpression expression, RemoteProcess process)
4238
var readFromAddress = Execute(readMemoryExpression.Expression, process);
4339
if (readMemoryExpression.ByteCount == 4)
4440
{
45-
return (IntPtr)process.ReadRemoteInt32(readFromAddress);
41+
return IntPtrExtension.From(process.ReadRemoteInt32(readFromAddress));
4642
}
4743
else
4844
{
49-
#if RECLASSNET64
50-
return (IntPtr)process.ReadRemoteInt64(readFromAddress);
51-
#else
52-
return (IntPtr)unchecked((int)process.ReadRemoteUInt64(readFromAddress));
53-
#endif
45+
return IntPtrExtension.From(process.ReadRemoteInt64(readFromAddress));
5446
}
5547
default:
5648
throw new ArgumentException($"Unsupported operation '{expression.GetType().FullName}'.");

ReClass.NET/Extensions/IntPtrExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,20 @@ public static long ToInt64Bits(this IntPtr ptr)
142142
}
143143

144144
return value;
145+
#endif
146+
}
147+
148+
public static IntPtr From(int value)
149+
{
150+
return (IntPtr)value;
151+
}
152+
153+
public static IntPtr From(long value)
154+
{
155+
#if RECLASSNET64
156+
return (IntPtr)value;
157+
#else
158+
return (IntPtr)unchecked((int)value);
145159
#endif
146160
}
147161
}

0 commit comments

Comments
 (0)