2025-05-08 12:36:06 +08:00
|
|
|
|
using dnlib.DotNet;
|
2025-05-10 17:54:53 +08:00
|
|
|
|
using Obfuz.Conf;
|
2025-05-08 12:36:06 +08:00
|
|
|
|
using Obfuz.Utils;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2025-05-09 19:32:25 +08:00
|
|
|
|
namespace Obfuz.ObfusPasses.ConstEncrypt
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
2025-05-10 17:54:53 +08:00
|
|
|
|
|
2025-05-09 19:26:59 +08:00
|
|
|
|
public class ConfigurableEncryptPolicy : EncryptPolicyBase
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
2025-05-10 17:54:53 +08:00
|
|
|
|
class ObfuscationRule : IRule<ObfuscationRule>
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
public bool? disableEncrypt;
|
|
|
|
|
public bool? encryptInt;
|
|
|
|
|
public bool? encryptLong;
|
|
|
|
|
public bool? encryptFloat;
|
|
|
|
|
public bool? encryptDouble;
|
|
|
|
|
public bool? encryptArray;
|
|
|
|
|
public bool? encryptString;
|
2025-05-09 11:03:05 +08:00
|
|
|
|
|
|
|
|
|
public bool? encryptConstInLoop;
|
|
|
|
|
public bool? encryptStringInLoop;
|
|
|
|
|
|
2025-05-08 12:36:06 +08:00
|
|
|
|
public bool? cacheConstInLoop;
|
|
|
|
|
public bool? cacheConstNotInLoop;
|
2025-05-09 11:03:05 +08:00
|
|
|
|
public bool? cacheStringInLoop;
|
2025-05-08 12:36:06 +08:00
|
|
|
|
public bool? cacheStringNotInLoop;
|
2025-05-09 11:03:05 +08:00
|
|
|
|
|
2025-05-08 12:36:06 +08:00
|
|
|
|
public void InheritParent(ObfuscationRule parentRule)
|
|
|
|
|
{
|
|
|
|
|
if (disableEncrypt == null)
|
|
|
|
|
disableEncrypt = parentRule.disableEncrypt;
|
|
|
|
|
if (encryptInt == null)
|
|
|
|
|
encryptInt = parentRule.encryptInt;
|
|
|
|
|
if (encryptLong == null)
|
|
|
|
|
encryptLong = parentRule.encryptLong;
|
|
|
|
|
if (encryptFloat == null)
|
|
|
|
|
encryptFloat = parentRule.encryptFloat;
|
|
|
|
|
if (encryptDouble == null)
|
|
|
|
|
encryptDouble = parentRule.encryptDouble;
|
|
|
|
|
if (encryptArray == null)
|
|
|
|
|
encryptArray = parentRule.encryptArray;
|
|
|
|
|
if (encryptString == null)
|
|
|
|
|
encryptString = parentRule.encryptString;
|
2025-05-09 11:03:05 +08:00
|
|
|
|
|
|
|
|
|
if (encryptConstInLoop == null)
|
|
|
|
|
encryptConstInLoop = parentRule.encryptConstInLoop;
|
|
|
|
|
if (encryptStringInLoop == null)
|
|
|
|
|
encryptStringInLoop = parentRule.encryptStringInLoop;
|
|
|
|
|
|
2025-05-08 12:36:06 +08:00
|
|
|
|
if (cacheConstInLoop == null)
|
|
|
|
|
cacheConstInLoop = parentRule.cacheConstInLoop;
|
|
|
|
|
if (cacheConstNotInLoop == null)
|
|
|
|
|
cacheConstNotInLoop = parentRule.cacheConstNotInLoop;
|
2025-05-09 11:03:05 +08:00
|
|
|
|
if (cacheStringInLoop == null)
|
|
|
|
|
cacheStringInLoop = parentRule.cacheStringInLoop;
|
2025-05-08 12:36:06 +08:00
|
|
|
|
if (cacheStringNotInLoop == null)
|
|
|
|
|
cacheStringNotInLoop = parentRule.cacheStringNotInLoop;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-10 17:54:53 +08:00
|
|
|
|
class MethodSpec : MethodRuleBase<ObfuscationRule>
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-10 17:54:53 +08:00
|
|
|
|
class TypeSpec : TypeRuleBase<MethodSpec, ObfuscationRule>
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-10 17:54:53 +08:00
|
|
|
|
class AssemblySpec : AssemblyRuleBase<TypeSpec, MethodSpec, ObfuscationRule>
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static readonly ObfuscationRule s_default = new ObfuscationRule()
|
|
|
|
|
{
|
|
|
|
|
disableEncrypt = false,
|
|
|
|
|
encryptInt = true,
|
|
|
|
|
encryptLong = true,
|
|
|
|
|
encryptFloat = true,
|
|
|
|
|
encryptDouble = true,
|
|
|
|
|
encryptArray = true,
|
|
|
|
|
encryptString = true,
|
2025-05-09 11:03:05 +08:00
|
|
|
|
encryptConstInLoop = true,
|
|
|
|
|
encryptStringInLoop = true,
|
2025-05-08 12:36:06 +08:00
|
|
|
|
cacheConstInLoop = true,
|
|
|
|
|
cacheConstNotInLoop = false,
|
2025-05-09 11:03:05 +08:00
|
|
|
|
cacheStringInLoop = true,
|
2025-05-08 12:36:06 +08:00
|
|
|
|
cacheStringNotInLoop = true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private ObfuscationRule _global;
|
2025-05-10 17:54:53 +08:00
|
|
|
|
|
|
|
|
|
public HashSet<int> notEncryptInts = new HashSet<int>();
|
|
|
|
|
public HashSet<long> notEncryptLongs = new HashSet<long>();
|
|
|
|
|
public HashSet<string> notEncryptStrings = new HashSet<string>();
|
|
|
|
|
public List<NumberRange<int>> notEncryptIntRanges = new List<NumberRange<int>>();
|
|
|
|
|
public List<NumberRange<long>> notEncryptLongRanges = new List<NumberRange<long>>();
|
|
|
|
|
public List<NumberRange<float>> notEncryptFloatRanges = new List<NumberRange<float>>();
|
|
|
|
|
public List<NumberRange<double>> notEncryptDoubleRanges = new List<NumberRange<double>>();
|
|
|
|
|
public List<NumberRange<int>> notEncryptArrayLengthRanges = new List<NumberRange<int>>();
|
|
|
|
|
public List<NumberRange<int>> notEncryptStringLengthRanges = new List<NumberRange<int>>();
|
|
|
|
|
|
|
|
|
|
private readonly XmlAssemblyTypeMethodRuleParser<AssemblySpec, TypeSpec, MethodSpec, ObfuscationRule> _xmlParser;
|
|
|
|
|
|
2025-05-08 12:36:06 +08:00
|
|
|
|
private readonly Dictionary<string, AssemblySpec> _assemblySpecs = new Dictionary<string, AssemblySpec>();
|
|
|
|
|
private readonly Dictionary<MethodDef, ObfuscationRule> _methodRuleCache = new Dictionary<MethodDef, ObfuscationRule>();
|
|
|
|
|
|
2025-05-10 09:41:45 +08:00
|
|
|
|
public ConfigurableEncryptPolicy(List<string> toObfuscatedAssemblyNames, List<string> xmlConfigFiles)
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
2025-05-10 17:54:53 +08:00
|
|
|
|
_xmlParser = new XmlAssemblyTypeMethodRuleParser<AssemblySpec, TypeSpec, MethodSpec, ObfuscationRule>(
|
|
|
|
|
toObfuscatedAssemblyNames, ParseObfuscationRule, ParseGlobalElement);
|
2025-05-10 09:41:45 +08:00
|
|
|
|
LoadConfigs(xmlConfigFiles);
|
2025-05-08 12:36:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-10 09:41:45 +08:00
|
|
|
|
private void LoadConfigs(List<string> configFiles)
|
|
|
|
|
{
|
2025-05-10 17:54:53 +08:00
|
|
|
|
_xmlParser.LoadConfigs(configFiles);
|
2025-05-08 12:36:06 +08:00
|
|
|
|
if (_global == null)
|
|
|
|
|
{
|
|
|
|
|
_global = s_default;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_global.InheritParent(s_default);
|
|
|
|
|
}
|
2025-05-10 17:54:53 +08:00
|
|
|
|
_xmlParser.InheritParentRules(_global);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ParseGlobalElement(string configFile, XmlElement ele)
|
|
|
|
|
{
|
|
|
|
|
switch (ele.Name)
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
2025-05-10 17:54:53 +08:00
|
|
|
|
case "global": _global = ParseObfuscationRule(configFile, ele); break;
|
2025-05-10 20:02:57 +08:00
|
|
|
|
case "whitelist": ParseWhitelist(configFile, ele); break;
|
2025-05-10 17:54:53 +08:00
|
|
|
|
default: throw new Exception($"Invalid xml file {configFile}, unknown node {ele.Name}");
|
2025-05-08 12:36:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-10 17:54:53 +08:00
|
|
|
|
private ObfuscationRule ParseObfuscationRule(string configFile, XmlElement ele)
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
var rule = new ObfuscationRule();
|
|
|
|
|
if (ele.HasAttribute("disableEncrypt"))
|
|
|
|
|
{
|
2025-05-10 09:41:45 +08:00
|
|
|
|
rule.disableEncrypt = ConfigUtil.ParseBool(ele.GetAttribute("disableEncrypt"));
|
2025-05-08 12:36:06 +08:00
|
|
|
|
}
|
|
|
|
|
if (ele.HasAttribute("encryptInt"))
|
|
|
|
|
{
|
2025-05-10 09:41:45 +08:00
|
|
|
|
rule.encryptInt = ConfigUtil.ParseBool(ele.GetAttribute("encryptInt"));
|
2025-05-08 12:36:06 +08:00
|
|
|
|
}
|
|
|
|
|
if (ele.HasAttribute("encryptLong"))
|
|
|
|
|
{
|
2025-05-10 09:41:45 +08:00
|
|
|
|
rule.encryptLong = ConfigUtil.ParseBool(ele.GetAttribute("encryptLong"));
|
2025-05-08 12:36:06 +08:00
|
|
|
|
}
|
|
|
|
|
if (ele.HasAttribute("encryptFloat"))
|
|
|
|
|
{
|
2025-05-10 09:41:45 +08:00
|
|
|
|
rule.encryptFloat = ConfigUtil.ParseBool(ele.GetAttribute("encryptFloat"));
|
2025-05-08 12:36:06 +08:00
|
|
|
|
}
|
|
|
|
|
if (ele.HasAttribute("encryptDouble"))
|
|
|
|
|
{
|
2025-05-10 09:41:45 +08:00
|
|
|
|
rule.encryptDouble = ConfigUtil.ParseBool(ele.GetAttribute("encryptDouble"));
|
2025-05-08 12:36:06 +08:00
|
|
|
|
}
|
|
|
|
|
if (ele.HasAttribute("encryptBytes"))
|
|
|
|
|
{
|
2025-05-10 09:41:45 +08:00
|
|
|
|
rule.encryptArray = ConfigUtil.ParseBool(ele.GetAttribute("encryptArray"));
|
2025-05-08 12:36:06 +08:00
|
|
|
|
}
|
|
|
|
|
if (ele.HasAttribute("encryptString"))
|
|
|
|
|
{
|
2025-05-10 09:41:45 +08:00
|
|
|
|
rule.encryptString = ConfigUtil.ParseBool(ele.GetAttribute("encryptString"));
|
2025-05-08 12:36:06 +08:00
|
|
|
|
}
|
2025-05-09 11:03:05 +08:00
|
|
|
|
|
|
|
|
|
if (ele.HasAttribute("encryptConstInLoop"))
|
|
|
|
|
{
|
2025-05-10 09:41:45 +08:00
|
|
|
|
rule.encryptConstInLoop = ConfigUtil.ParseBool(ele.GetAttribute("encryptConstInLoop"));
|
2025-05-09 11:03:05 +08:00
|
|
|
|
}
|
|
|
|
|
if (ele.HasAttribute("encryptStringInLoop"))
|
|
|
|
|
{
|
2025-05-10 09:41:45 +08:00
|
|
|
|
rule.encryptStringInLoop = ConfigUtil.ParseBool(ele.GetAttribute("encryptStringInLoop"));
|
2025-05-09 11:03:05 +08:00
|
|
|
|
}
|
2025-05-08 12:36:06 +08:00
|
|
|
|
if (ele.HasAttribute("cacheConstInLoop"))
|
|
|
|
|
{
|
2025-05-10 09:41:45 +08:00
|
|
|
|
rule.cacheConstInLoop = ConfigUtil.ParseBool(ele.GetAttribute("cacheConstInLoop"));
|
2025-05-08 12:36:06 +08:00
|
|
|
|
}
|
|
|
|
|
if (ele.HasAttribute("cacheConstNotInLoop"))
|
|
|
|
|
{
|
2025-05-10 09:41:45 +08:00
|
|
|
|
rule.cacheConstNotInLoop = ConfigUtil.ParseBool(ele.GetAttribute("cacheConstNotInLoop"));
|
2025-05-08 12:36:06 +08:00
|
|
|
|
}
|
2025-05-09 11:03:05 +08:00
|
|
|
|
if (ele.HasAttribute("cacheStringInLoop"))
|
|
|
|
|
{
|
2025-05-10 09:41:45 +08:00
|
|
|
|
rule.cacheStringInLoop = ConfigUtil.ParseBool(ele.GetAttribute("cacheStringInLoop"));
|
2025-05-09 11:03:05 +08:00
|
|
|
|
}
|
2025-05-08 12:36:06 +08:00
|
|
|
|
if (ele.HasAttribute("cacheStringNotInLoop"))
|
|
|
|
|
{
|
2025-05-10 09:41:45 +08:00
|
|
|
|
rule.cacheStringNotInLoop = ConfigUtil.ParseBool(ele.GetAttribute("cacheStringNotInLoop"));
|
2025-05-08 12:36:06 +08:00
|
|
|
|
}
|
|
|
|
|
return rule;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-10 20:02:57 +08:00
|
|
|
|
private void ParseWhitelist(string configFile, XmlElement childEle)
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
2025-05-10 20:02:57 +08:00
|
|
|
|
string type = childEle.GetAttribute("type");
|
|
|
|
|
if (string.IsNullOrEmpty(type))
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
2025-05-10 20:02:57 +08:00
|
|
|
|
throw new Exception($"Invalid xml file, whitelist type is empty");
|
|
|
|
|
}
|
|
|
|
|
string value = childEle.InnerText;
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case "int":
|
|
|
|
|
{
|
2025-05-16 17:44:28 +08:00
|
|
|
|
notEncryptInts.AddRange(value.Split(',').Select(s => int.Parse(s.Trim())));
|
2025-05-10 20:02:57 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "long":
|
|
|
|
|
{
|
2025-05-16 17:44:28 +08:00
|
|
|
|
notEncryptLongs.AddRange(value.Split(',').Select(s => long.Parse(s.Trim())));
|
2025-05-10 20:02:57 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "string":
|
|
|
|
|
{
|
2025-05-16 17:44:28 +08:00
|
|
|
|
notEncryptStrings.AddRange(value.Split(',').Select(s => s.Trim()));
|
2025-05-10 20:02:57 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "int-range":
|
|
|
|
|
{
|
2025-05-16 17:44:28 +08:00
|
|
|
|
var parts = value.Split(',');
|
2025-05-10 20:02:57 +08:00
|
|
|
|
if (parts.Length != 2)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception($"Invalid xml file, int-range {value} is invalid");
|
|
|
|
|
}
|
|
|
|
|
notEncryptIntRanges.Add(new NumberRange<int>(ConfigUtil.ParseNullableInt(parts[0]), ConfigUtil.ParseNullableInt(parts[1])));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "long-range":
|
|
|
|
|
{
|
2025-05-16 17:44:28 +08:00
|
|
|
|
var parts = value.Split(',');
|
2025-05-10 20:02:57 +08:00
|
|
|
|
if (parts.Length != 2)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception($"Invalid xml file, long-range {value} is invalid");
|
|
|
|
|
}
|
|
|
|
|
notEncryptLongRanges.Add(new NumberRange<long>(ConfigUtil.ParseNullableLong(parts[0]), ConfigUtil.ParseNullableLong(parts[1])));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "float-range":
|
|
|
|
|
{
|
2025-05-16 17:44:28 +08:00
|
|
|
|
var parts = value.Split(',');
|
2025-05-10 20:02:57 +08:00
|
|
|
|
if (parts.Length != 2)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception($"Invalid xml file, float-range {value} is invalid");
|
|
|
|
|
}
|
|
|
|
|
notEncryptFloatRanges.Add(new NumberRange<float>(ConfigUtil.ParseNullableFloat(parts[0]), ConfigUtil.ParseNullableFloat(parts[1])));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "double-range":
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
2025-05-16 17:44:28 +08:00
|
|
|
|
var parts = value.Split(',');
|
2025-05-10 20:02:57 +08:00
|
|
|
|
if (parts.Length != 2)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception($"Invalid xml file, double-range {value} is invalid");
|
|
|
|
|
}
|
|
|
|
|
notEncryptDoubleRanges.Add(new NumberRange<double>(ConfigUtil.ParseNullableDouble(parts[0]), ConfigUtil.ParseNullableDouble(parts[1])));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "string-length-range":
|
|
|
|
|
{
|
2025-05-16 17:44:28 +08:00
|
|
|
|
var parts = value.Split(',');
|
2025-05-10 20:02:57 +08:00
|
|
|
|
if (parts.Length != 2)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception($"Invalid xml file, string-length-range {value} is invalid");
|
|
|
|
|
}
|
|
|
|
|
notEncryptStringLengthRanges.Add(new NumberRange<int>(ConfigUtil.ParseNullableInt(parts[0]), ConfigUtil.ParseNullableInt(parts[1])));
|
|
|
|
|
break;
|
2025-05-08 12:36:06 +08:00
|
|
|
|
}
|
2025-05-10 20:02:57 +08:00
|
|
|
|
case "array-length-range":
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
2025-05-16 17:44:28 +08:00
|
|
|
|
var parts = value.Split(',');
|
2025-05-10 20:02:57 +08:00
|
|
|
|
if (parts.Length != 2)
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
2025-05-10 20:02:57 +08:00
|
|
|
|
throw new Exception($"Invalid xml file, array-length-range {value} is invalid");
|
2025-05-08 12:36:06 +08:00
|
|
|
|
}
|
2025-05-10 20:02:57 +08:00
|
|
|
|
notEncryptArrayLengthRanges.Add(new NumberRange<int>(ConfigUtil.ParseNullableInt(parts[0]), ConfigUtil.ParseNullableInt(parts[1])));
|
|
|
|
|
break;
|
2025-05-08 12:36:06 +08:00
|
|
|
|
}
|
2025-05-10 20:02:57 +08:00
|
|
|
|
default: throw new Exception($"Invalid xml file, unknown whitelist type {type} in {childEle.Name} node");
|
2025-05-08 12:36:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ObfuscationRule GetMethodObfuscationRule(MethodDef method)
|
|
|
|
|
{
|
|
|
|
|
if (!_methodRuleCache.TryGetValue(method, out var rule))
|
|
|
|
|
{
|
2025-05-10 17:54:53 +08:00
|
|
|
|
rule = _xmlParser.GetMethodRule(method, _global);
|
2025-05-08 12:36:06 +08:00
|
|
|
|
_methodRuleCache[method] = rule;
|
|
|
|
|
}
|
|
|
|
|
return rule;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool NeedObfuscateMethod(MethodDef method)
|
|
|
|
|
{
|
|
|
|
|
ObfuscationRule rule = GetMethodObfuscationRule(method);
|
|
|
|
|
return rule.disableEncrypt != true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 11:03:05 +08:00
|
|
|
|
public override ConstCachePolicy GetMethodConstCachePolicy(MethodDef method)
|
|
|
|
|
{
|
|
|
|
|
ObfuscationRule rule = GetMethodObfuscationRule(method);
|
|
|
|
|
return new ConstCachePolicy
|
|
|
|
|
{
|
|
|
|
|
cacheConstInLoop = rule.cacheConstInLoop.Value,
|
|
|
|
|
cacheConstNotInLoop = rule.cacheConstNotInLoop.Value,
|
|
|
|
|
cacheStringInLoop = rule.cacheStringInLoop.Value,
|
|
|
|
|
cacheStringNotInLoop = rule.cacheStringNotInLoop.Value,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool NeedObfuscateInt(MethodDef method, bool currentInLoop, int value)
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
ObfuscationRule rule = GetMethodObfuscationRule(method);
|
|
|
|
|
if (rule.encryptInt == false)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-05-09 11:03:05 +08:00
|
|
|
|
if (currentInLoop && rule.encryptConstInLoop == false)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-05-10 17:54:53 +08:00
|
|
|
|
if (notEncryptInts.Contains(value))
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-05-10 17:54:53 +08:00
|
|
|
|
foreach (var range in notEncryptIntRanges)
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
if (range.min != null && value < range.min)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (range.max != null && value > range.max)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 11:03:05 +08:00
|
|
|
|
public override bool NeedObfuscateLong(MethodDef method, bool currentInLoop, long value)
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
ObfuscationRule rule = GetMethodObfuscationRule(method);
|
|
|
|
|
if (rule.encryptLong == false)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-05-09 11:03:05 +08:00
|
|
|
|
if (currentInLoop && rule.encryptConstInLoop == false)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-05-10 17:54:53 +08:00
|
|
|
|
if (notEncryptLongs.Contains(value))
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-05-10 17:54:53 +08:00
|
|
|
|
foreach (var range in notEncryptLongRanges)
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
if (range.min != null && value < range.min)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (range.max != null && value > range.max)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 11:03:05 +08:00
|
|
|
|
public override bool NeedObfuscateFloat(MethodDef method, bool currentInLoop, float value)
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
ObfuscationRule rule = GetMethodObfuscationRule(method);
|
|
|
|
|
if (rule.encryptFloat == false)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-05-09 11:03:05 +08:00
|
|
|
|
if (currentInLoop && rule.encryptConstInLoop == false)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-05-10 17:54:53 +08:00
|
|
|
|
foreach (var range in notEncryptFloatRanges)
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
if (range.min != null && value < range.min)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (range.max != null && value > range.max)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 11:03:05 +08:00
|
|
|
|
public override bool NeedObfuscateDouble(MethodDef method, bool currentInLoop, double value)
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
ObfuscationRule rule = GetMethodObfuscationRule(method);
|
|
|
|
|
if (rule.encryptDouble == false)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-05-09 11:03:05 +08:00
|
|
|
|
if (currentInLoop && rule.encryptConstInLoop == false)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-05-10 17:54:53 +08:00
|
|
|
|
foreach (var range in notEncryptDoubleRanges)
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
if (range.min != null && value < range.min)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (range.max != null && value > range.max)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 11:03:05 +08:00
|
|
|
|
public override bool NeedObfuscateString(MethodDef method, bool currentInLoop, string value)
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
ObfuscationRule rule = GetMethodObfuscationRule(method);
|
|
|
|
|
if (rule.encryptString == false)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-05-09 11:03:05 +08:00
|
|
|
|
if (currentInLoop && rule.encryptConstInLoop == false)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-05-10 17:54:53 +08:00
|
|
|
|
if (notEncryptStrings.Contains(value))
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-05-10 17:54:53 +08:00
|
|
|
|
foreach (var range in notEncryptStringLengthRanges)
|
2025-05-09 10:17:08 +08:00
|
|
|
|
{
|
|
|
|
|
if (range.min != null && value.Length < range.min)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (range.max != null && value.Length > range.max)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-05-08 12:36:06 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 11:03:05 +08:00
|
|
|
|
public override bool NeedObfuscateArray(MethodDef method, bool currentInLoop, byte[] array)
|
2025-05-08 12:36:06 +08:00
|
|
|
|
{
|
|
|
|
|
ObfuscationRule rule = GetMethodObfuscationRule(method);
|
2025-05-09 10:17:08 +08:00
|
|
|
|
if (rule.encryptArray == false)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-05-09 11:03:05 +08:00
|
|
|
|
if (currentInLoop && rule.encryptConstInLoop == false)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-05-10 17:54:53 +08:00
|
|
|
|
foreach (var range in notEncryptArrayLengthRanges)
|
2025-05-09 10:17:08 +08:00
|
|
|
|
{
|
|
|
|
|
if (range.min != null && array.Length < range.min)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (range.max != null && array.Length > range.max)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2025-05-08 12:36:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|