2025-10-29 20:40:37 +08:00
|
|
|
|
// Copyright 2025 Code Philosophy
|
|
|
|
|
|
//
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
|
//
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
//
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
|
// SOFTWARE.
|
|
|
|
|
|
|
2025-06-28 12:12:31 +08:00
|
|
|
|
using Obfuz.Settings;
|
|
|
|
|
|
using Obfuz.Utils;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|