2025-06-28 12:12:31 +08:00
|
|
|
|
using Obfuz.Settings;
|
|
|
|
|
using Obfuz.Utils;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace Obfuz.GarbageCodeGeneration
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class GarbageCodeGenerator
|
|
|
|
|
{
|
|
|
|
|
private const int CodeGenerationSecretKeyLength = 1024;
|
|
|
|
|
|
2025-06-28 19:01:57 +08:00
|
|
|
|
private readonly GarbageCodeGenerationSettings _settings;
|
2025-06-28 12:12:31 +08:00
|
|
|
|
private readonly int[] _intGenerationSecretKey;
|
|
|
|
|
|
2025-06-28 19:01:57 +08:00
|
|
|
|
public GarbageCodeGenerator(GarbageCodeGenerationSettings settings)
|
2025-06-28 12:12:31 +08:00
|
|
|
|
{
|
|
|
|
|
_settings = settings;
|
|
|
|
|
|
|
|
|
|
byte[] byteGenerationSecretKey = KeyGenerator.GenerateKey(settings.codeGenerationSecret, CodeGenerationSecretKeyLength);
|
|
|
|
|
_intGenerationSecretKey = KeyGenerator.ConvertToIntKey(byteGenerationSecretKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Generate()
|
|
|
|
|
{
|
|
|
|
|
GenerateTask(_settings.defaultTask);
|
|
|
|
|
if (_settings.additionalTasks != null && _settings.additionalTasks.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (var task in _settings.additionalTasks)
|
|
|
|
|
{
|
|
|
|
|
GenerateTask(task);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-28 13:10:37 +08:00
|
|
|
|
public void CleanCodes()
|
|
|
|
|
{
|
|
|
|
|
Debug.Log($"Cleaning generated garbage codes begin.");
|
|
|
|
|
if (_settings.defaultTask != null)
|
|
|
|
|
{
|
|
|
|
|
FileUtil.RemoveDir(_settings.defaultTask.outputPath, true);
|
|
|
|
|
}
|
|
|
|
|
if (_settings.additionalTasks != null && _settings.additionalTasks.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
foreach (var task in _settings.additionalTasks)
|
|
|
|
|
{
|
|
|
|
|
FileUtil.RemoveDir(task.outputPath, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-28 12:12:31 +08:00
|
|
|
|
private void GenerateTask(GarbageCodeGenerationTask task)
|
|
|
|
|
{
|
2025-06-28 19:01:57 +08:00
|
|
|
|
Debug.Log($"Generating garbage code with seed: {task.codeGenerationRandomSeed}, class count: {task.classCount}, method count per class: {task.methodCountPerClass}, types: {task.garbageCodeType}, output path: {task.outputPath}");
|
2025-06-28 12:12:31 +08:00
|
|
|
|
|
2025-06-28 13:43:32 +08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(task.outputPath))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("outputPath of GarbageCodeGenerationTask is empty!");
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-28 19:01:57 +08:00
|
|
|
|
var generator = CreateSpecificCodeGenerator(task.garbageCodeType);
|
2025-06-28 12:12:31 +08:00
|
|
|
|
|
|
|
|
|
var parameters = new GenerationParameters
|
|
|
|
|
{
|
|
|
|
|
random = new RandomWithKey(_intGenerationSecretKey, task.codeGenerationRandomSeed),
|
|
|
|
|
classNamespace = task.classNamespace,
|
|
|
|
|
classNamePrefix = task.classNamePrefix,
|
|
|
|
|
classCount = task.classCount,
|
|
|
|
|
methodCountPerClass = task.methodCountPerClass,
|
|
|
|
|
fieldCountPerClass = task.fieldCountPerClass,
|
2025-06-28 13:43:32 +08:00
|
|
|
|
outputPath = task.outputPath,
|
2025-06-28 12:12:31 +08:00
|
|
|
|
};
|
|
|
|
|
generator.Generate(parameters);
|
|
|
|
|
|
|
|
|
|
Debug.Log($"Generate garbage code end.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ISpecificGarbageCodeGenerator CreateSpecificCodeGenerator(GarbageCodeType type)
|
|
|
|
|
{
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
2025-06-28 13:43:32 +08:00
|
|
|
|
case GarbageCodeType.Config: return new ConfigGarbageCodeGenerator();
|
|
|
|
|
case GarbageCodeType.UI: return new UIGarbageCodeGenerator();
|
|
|
|
|
default: throw new NotSupportedException($"Garbage code type {type} is not supported.");
|
2025-06-28 12:12:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|