添加add 表达式

backup
walon 2025-04-20 15:20:13 +08:00
parent c52884d2d2
commit 33f83d5432
15 changed files with 144 additions and 33 deletions

View File

@ -2,7 +2,7 @@
namespace Obfuz.Virtualization namespace Obfuz.Virtualization
{ {
public class ConstPrimitiveDataNode : DataNodeAny public class ConstDataNode : DataNodeAny
{ {
public override void Compile(CompileContext ctx) public override void Compile(CompileContext ctx)
@ -10,11 +10,11 @@ namespace Obfuz.Virtualization
switch (Type) switch (Type)
{ {
case DataNodeType.Byte: //case DataNodeType.Byte:
{ //{
// create ldloc.i4 // // create ldloc.i4
break; // break;
} //}
case DataNodeType.Int32: case DataNodeType.Int32:
{ {
// create ldloc.i4 // create ldloc.i4
@ -40,6 +40,18 @@ namespace Obfuz.Virtualization
// create ldnull // create ldnull
break; break;
} }
case DataNodeType.String:
{
// create ldstr
break;
}
case DataNodeType.Bytes:
{
// create ldstr
// RuntimeHelpers.InitializeArray(array, fieldHandle);
break;
}
default: default:
{ {
throw new NotImplementedException($"Type:{Type} not implemented"); throw new NotImplementedException($"Type:{Type} not implemented");

View File

@ -5,7 +5,16 @@ namespace Obfuz.Virtualization
public class ConstExpression : DataNodeAny public class ConstExpression : DataNodeAny
{ {
public IFunction function; public IFunction function;
public readonly List<IDataNode> Inputs = new List<IDataNode>(); public readonly List<IDataNode> inputs;
public readonly ConstValue result;
public ConstExpression(IFunction function, List<IDataNode> inputs, ConstValue result)
{
this.function = function;
this.inputs = inputs;
Type = function.ReturnType;
this.result = result;
}
public override void Compile(CompileContext ctx) public override void Compile(CompileContext ctx)
{ {

View File

@ -10,11 +10,11 @@ namespace Obfuz.Virtualization
switch (Type) switch (Type)
{ {
case DataNodeType.Byte: //case DataNodeType.Byte:
{ //{
// create ldloc.i4 // // create ldloc.i4
break; // break;
} //}
case DataNodeType.Int32: case DataNodeType.Int32:
{ {
// create ldloc.i4 // create ldloc.i4
@ -40,6 +40,18 @@ namespace Obfuz.Virtualization
// create ldnull // create ldnull
break; break;
} }
case DataNodeType.String:
{
// create ldstr
break;
}
case DataNodeType.Bytes:
{
// create ldstr
// RuntimeHelpers.InitializeArray(array, fieldHandle);
break;
}
default: default:
{ {
throw new NotImplementedException($"Type:{Type} not implemented"); throw new NotImplementedException($"Type:{Type} not implemented");

View File

@ -1,15 +0,0 @@
using System;
namespace Obfuz.Virtualization
{
public class ConstStringDataNode : DataNodeBase<string>
{
public override void Compile(CompileContext ctx)
{
// create ldstr
}
}
}

View File

@ -0,0 +1,13 @@
namespace Obfuz.Virtualization
{
public struct ConstValue
{
public readonly DataNodeType type;
public readonly object value;
public ConstValue(DataNodeType type, object value)
{
this.type = type;
this.value = value;
}
}
}

View File

@ -1,7 +1,9 @@
namespace Obfuz.Virtualization namespace Obfuz.Virtualization
{ {
public class CreateExpressionOptions public struct CreateExpressionOptions
{ {
public IRandom random; public IRandom random;
public IDataNodeCreator expressionCreator;
public int depth;
} }
} }

View File

@ -27,6 +27,7 @@
public abstract class DataNodeAny : DataNodeBase public abstract class DataNodeAny : DataNodeBase
{ {
private object _value; private object _value;
public override object Value public override object Value
{ {
get => _value; get => _value;

View File

@ -2,7 +2,7 @@
{ {
public enum DataNodeType public enum DataNodeType
{ {
Byte, //Byte,
Int32, Int32,
Int64, Int64,
Float32, Float32,

View File

@ -0,0 +1,30 @@
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
namespace Obfuz.Virtualization
{
public abstract class FunctionBase : IFunction
{
public abstract DataNodeType ReturnType { get; }
public abstract void CreateArguments(DataNodeType type, object value, CreateExpressionOptions options, List<ConstValue> args);
public ConstExpression CreateCallable(IDataNode result, CreateExpressionOptions options)
{
var args = new List<ConstValue>();
CreateArguments(result.Type, result.Value, options, args);
options.depth += 1;
var argNodes = new List<IDataNode>();
foreach (ConstValue cv in args)
{
var argNode = options.expressionCreator.CreateRandom(cv.type, cv.value, options);
argNodes.Add(argNode);
}
return new ConstExpression(this, args.Select(a => options.expressionCreator.CreateRandom(a.type, a.value, options)).ToList(), new ConstValue(result.Type, result.Value));
}
}
}

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static UnityEngine.Networking.UnityWebRequest;
namespace Obfuz.Virtualization.Functions
{
public class Int32FunctionAdd : Int32FunctionBase
{
public override DataNodeType ReturnType => DataNodeType.Int32;
public override void CreateArguments(DataNodeType type, object v, CreateExpressionOptions options, List<ConstValue> args)
{
int value = (int)v;
int op1 = value >= 0 ? options.random.NextInt(value) : -options.random.NextInt(-value);
int op2 = value - op1;
args.Add(new ConstValue(DataNodeType.Int32, op1));
args.Add(new ConstValue(DataNodeType.Int32, op2));
}
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz.Virtualization.Functions
{
public abstract class Int32FunctionBase : FunctionBase
{
public override DataNodeType ReturnType => DataNodeType.Int32;
}
}

View File

@ -0,0 +1,7 @@
namespace Obfuz.Virtualization
{
public interface IDataNodeCreator
{
IDataNode CreateRandom(DataNodeType type, object value, CreateExpressionOptions options);
}
}

View File

@ -2,6 +2,8 @@
{ {
public interface IFunction public interface IFunction
{ {
DataNodeType ReturnType { get; }
ConstExpression CreateCallable(IDataNode result, CreateExpressionOptions options); ConstExpression CreateCallable(IDataNode result, CreateExpressionOptions options);
} }
} }

View File

@ -2,6 +2,8 @@
{ {
public interface IRandom public interface IRandom
{ {
int NextInt(int min, int max);
int NextInt(int max);
} }
} }

View File

@ -6,15 +6,12 @@ using System.Threading.Tasks;
namespace Obfuz.Virtualization namespace Obfuz.Virtualization
{ {
public class ConstBytesDataNode : DataNodeAny public class RvaBytesNode : DataNodeBase<byte[]>
{ {
public override void Compile(CompileContext ctx) public override void Compile(CompileContext ctx)
{ {
// create ldstr
// RuntimeHelpers.InitializeArray(array, fieldHandle);
} }
} }
} }