2025-06-28 12:12:31 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Obfuz.Settings;
|
|
|
|
|
using Obfuz.Utils;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
2025-06-28 13:10:37 +08:00
|
|
|
|
using System.Linq;
|
2025-06-28 12:12:31 +08:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Obfuz.GarbageCodeGeneration
|
|
|
|
|
{
|
|
|
|
|
|
2025-06-28 13:10:37 +08:00
|
|
|
|
public class ConfigGarbageCodeGenerator : SpecificGarbageCodeGeneratorBase
|
|
|
|
|
{
|
2025-06-28 12:12:31 +08:00
|
|
|
|
|
2025-06-28 13:10:37 +08:00
|
|
|
|
private readonly string[] _types = new string[]
|
2025-06-28 12:12:31 +08:00
|
|
|
|
{
|
2025-06-28 13:10:37 +08:00
|
|
|
|
"bool",
|
|
|
|
|
"byte",
|
|
|
|
|
"short",
|
|
|
|
|
"int",
|
|
|
|
|
"long",
|
|
|
|
|
"float",
|
|
|
|
|
"double",
|
|
|
|
|
};
|
2025-06-28 12:12:31 +08:00
|
|
|
|
|
2025-06-28 13:10:37 +08:00
|
|
|
|
private string CreateRandomType(IRandom random)
|
2025-06-28 12:12:31 +08:00
|
|
|
|
{
|
2025-06-28 13:10:37 +08:00
|
|
|
|
return _types[random.NextInt(_types.Length)];
|
2025-06-28 12:12:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-28 13:10:37 +08:00
|
|
|
|
private string GetReadMethodNameOfType(string type)
|
2025-06-28 12:12:31 +08:00
|
|
|
|
{
|
2025-06-28 13:10:37 +08:00
|
|
|
|
switch (type)
|
2025-06-28 12:12:31 +08:00
|
|
|
|
{
|
2025-06-28 13:10:37 +08:00
|
|
|
|
case "bool": return "ReadBoolean";
|
|
|
|
|
case "byte": return "ReadByte";
|
|
|
|
|
case "short": return "ReadInt16";
|
|
|
|
|
case "int": return "ReadInt32";
|
|
|
|
|
case "long": return "ReadInt64";
|
|
|
|
|
case "float": return "ReadSingle";
|
|
|
|
|
case "double": return "ReadDouble";
|
|
|
|
|
default: throw new ArgumentException($"Unsupported type: {type}");
|
2025-06-28 12:12:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
class FieldGenerationInfo
|
|
|
|
|
{
|
|
|
|
|
public int index;
|
|
|
|
|
public string name;
|
|
|
|
|
public string type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MethodGenerationInfo
|
|
|
|
|
{
|
|
|
|
|
public int index;
|
|
|
|
|
public string name;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-28 13:10:37 +08:00
|
|
|
|
protected override object CreateField(int index, IRandom random, GenerationParameters parameters)
|
2025-06-28 12:12:31 +08:00
|
|
|
|
{
|
2025-06-28 13:10:37 +08:00
|
|
|
|
return new FieldGenerationInfo
|
2025-06-28 12:12:31 +08:00
|
|
|
|
{
|
2025-06-28 13:10:37 +08:00
|
|
|
|
index = index,
|
|
|
|
|
name = $"x{index}",
|
|
|
|
|
type = CreateRandomType(random),
|
2025-06-28 12:12:31 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-28 13:10:37 +08:00
|
|
|
|
protected override object CreateMethod(int index, IRandom random, GenerationParameters parameters)
|
2025-06-28 12:12:31 +08:00
|
|
|
|
{
|
2025-06-28 13:10:37 +08:00
|
|
|
|
return new MethodGenerationInfo
|
|
|
|
|
{
|
|
|
|
|
index = index,
|
|
|
|
|
name = $"Load{index}",
|
|
|
|
|
};
|
2025-06-28 12:12:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void GenerateUsings(StringBuilder result, IClassGenerationInfo cgi)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-28 13:10:37 +08:00
|
|
|
|
protected override void GenerateField(StringBuilder result, IClassGenerationInfo cgi, IRandom random, object field, string indent)
|
2025-06-28 12:12:31 +08:00
|
|
|
|
{
|
|
|
|
|
var fgi = (FieldGenerationInfo)field;
|
|
|
|
|
result.AppendLine($"{indent}public {fgi.type} {fgi.name};");
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-28 13:10:37 +08:00
|
|
|
|
protected override void GenerateMethod(StringBuilder result, IClassGenerationInfo cgi, IRandom random, object method, string indent)
|
2025-06-28 12:12:31 +08:00
|
|
|
|
{
|
2025-06-28 13:10:37 +08:00
|
|
|
|
var mgi = (MethodGenerationInfo)method;
|
|
|
|
|
result.AppendLine($"{indent}public void {mgi.name}(BinaryReader reader)");
|
2025-06-28 12:12:31 +08:00
|
|
|
|
result.AppendLine($"{indent}{{");
|
|
|
|
|
|
|
|
|
|
string indent2 = indent + " ";
|
2025-06-28 13:10:37 +08:00
|
|
|
|
result.AppendLine($"{indent2}int a = 0;");
|
|
|
|
|
result.AppendLine($"{indent2}int b = 0;");
|
|
|
|
|
int maxN = 100;
|
|
|
|
|
var shuffledFields = cgi.Fields.ToList();
|
|
|
|
|
RandomUtil.ShuffleList(shuffledFields, random);
|
|
|
|
|
foreach (FieldGenerationInfo fgi in shuffledFields)
|
2025-06-28 12:12:31 +08:00
|
|
|
|
{
|
|
|
|
|
result.AppendLine($"{indent2}this.{fgi.name} = reader.{GetReadMethodNameOfType(fgi.type)}();");
|
2025-06-28 13:10:37 +08:00
|
|
|
|
if (random.NextInPercentage(0.5f))
|
|
|
|
|
{
|
|
|
|
|
result.AppendLine($"{indent2}a = b * {random.NextInt(maxN)} + reader.ReadInt32();");
|
|
|
|
|
result.AppendLine($"{indent2}b = a * reader.ReadInt32() + {random.NextInt(maxN)};");
|
|
|
|
|
}
|
|
|
|
|
if (random.NextInPercentage(0.5f))
|
|
|
|
|
{
|
|
|
|
|
result.AppendLine($"{indent2}a += {random.NextInt(0, 10000)};");
|
|
|
|
|
}
|
|
|
|
|
if (random.NextInPercentage(0.5f))
|
|
|
|
|
{
|
|
|
|
|
result.AppendLine($"{indent2}b += {random.NextInt(0, 10000)};");
|
|
|
|
|
}
|
2025-06-28 12:12:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.AppendLine($"{indent}}}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|