支持2020构建流程

backup
walon 2025-04-19 11:47:05 +08:00
parent 15007eeb72
commit b9351e5066
6 changed files with 40 additions and 24 deletions

View File

@ -18,29 +18,40 @@ namespace Obfuz
#if UNITY_2021 || UNITY_2020_1_OR_NEWER #if UNITY_2021 || UNITY_2020_1_OR_NEWER
internal class ObfuzProcess2021 : IPreprocessBuildWithReport, IPostprocessBuildWithReport internal class ObfuzProcess2021 : IPreprocessBuildWithReport, IPostprocessBuildWithReport
{ {
private static bool s_inBuild = false;
private static bool s_obfuscated = false; private static bool s_obfuscated = false;
public int callbackOrder => 10000; public int callbackOrder => 10000;
[InitializeOnLoadMethod]
private static void Init()
{
CompilationPipeline.compilationFinished += OnCompilationFinished;
}
public void OnPreprocessBuild(BuildReport report) public void OnPreprocessBuild(BuildReport report)
{ {
s_inBuild = true;
s_obfuscated = false; s_obfuscated = false;
CompilationPipeline.compilationFinished += OnCompilationFinished;
} }
private static string GetScriptAssembliesPath(object obj) private static string GetScriptAssembliesPath(object obj)
{ {
#if UNITY_2021
object settings = obj.GetType().GetProperty("settings").GetValue(obj); object settings = obj.GetType().GetProperty("settings").GetValue(obj);
string path = (string)settings.GetType().GetProperty("OutputDirectory").GetValue(settings); string path = (string)settings.GetType().GetProperty("OutputDirectory").GetValue(settings);
return path; #elif UNITY_2020
return "Library/PlayerScriptAssemblies";
#else
throw new Exception();
#endif
} }
private void OnCompilationFinished(object obj) private static void OnCompilationFinished(object obj)
{ {
if (s_inBuild && !s_obfuscated) if (!BuildPipeline.isBuildingPlayer)
{
return;
}
if (!s_obfuscated)
{ {
RunObfuscate(GetScriptAssembliesPath(obj)); RunObfuscate(GetScriptAssembliesPath(obj));
s_obfuscated = true; s_obfuscated = true;
@ -49,9 +60,7 @@ namespace Obfuz
public void OnPostprocessBuild(BuildReport report) public void OnPostprocessBuild(BuildReport report)
{ {
s_inBuild = false;
s_obfuscated = false; s_obfuscated = false;
CompilationPipeline.compilationFinished -= OnCompilationFinished;
} }
private static void RunObfuscate(string scriptAssembliesPath) private static void RunObfuscate(string scriptAssembliesPath)
@ -76,8 +85,15 @@ namespace Obfuz
{ {
AssemblySearchDirs = new List<string> AssemblySearchDirs = new List<string>
{ {
#if UNITY_2021_1_OR_NEWER
Path.Combine(applicationContentsPath, "UnityReferenceAssemblies/unity-4.8-api/Facades"), Path.Combine(applicationContentsPath, "UnityReferenceAssemblies/unity-4.8-api/Facades"),
Path.Combine(applicationContentsPath, "UnityReferenceAssemblies/unity-4.8-api"), Path.Combine(applicationContentsPath, "UnityReferenceAssemblies/unity-4.8-api"),
#elif UNITY_2020
Path.Combine(applicationContentsPath, "MonoBleedingEdge/lib/mono/4.7.1-api/Facades"),
Path.Combine(applicationContentsPath, "MonoBleedingEdge/lib/mono/4.7.1-api"),
#else
#error "Unsupported Unity version"
#endif
Path.Combine(applicationContentsPath, "Managed/UnityEngine"), Path.Combine(applicationContentsPath, "Managed/UnityEngine"),
backupPlayerScriptAssembliesPath, backupPlayerScriptAssembliesPath,
}, },

View File

@ -16,7 +16,7 @@ namespace Obfuz
} }
} }
public static V GetValueOrDefault<K, V>(IDictionary<K, V> dic, K key) public static V GetValueOrDefault<K, V>(this IDictionary<K, V> dic, K key)
{ {
return dic.TryGetValue(key, out V v) ? v : default(V); return dic.TryGetValue(key, out V v) ? v : default(V);
} }

View File

@ -259,7 +259,7 @@ namespace Obfuz
{ {
return type; return type;
} }
return type.IsClassSig ? new ClassSig(typeDef) : new ValueTypeSig(typeDef); return type.IsClassSig ? (TypeSig)new ClassSig(typeDef) : new ValueTypeSig(typeDef);
} }
case ElementType.Array: case ElementType.Array:
{ {

View File

@ -16,7 +16,7 @@ namespace Obfuz
nameOrPattern = "*"; nameOrPattern = "*";
} }
_str = nameOrPattern; _str = nameOrPattern;
_regex = nameOrPattern.Contains('*') || nameOrPattern.Contains('?') ? new Regex(WildcardToRegex(nameOrPattern)) : null; _regex = nameOrPattern.Contains("*") || nameOrPattern.Contains("?") ? new Regex(WildcardToRegex(nameOrPattern)) : null;
} }
public static string WildcardToRegex(string pattern) public static string WildcardToRegex(string pattern)

View File

@ -307,7 +307,7 @@ namespace Obfuz
rule.typeRuleSpecs = new List<TypeRuleSpec>(); rule.typeRuleSpecs = new List<TypeRuleSpec>();
foreach (XmlNode node in element.ChildNodes) foreach (XmlNode node in element.ChildNodes)
{ {
if (node is not XmlElement childElement) if (!(node is XmlElement childElement))
{ {
continue; continue;
} }
@ -337,7 +337,7 @@ namespace Obfuz
rule.eventRuleSpecs = new List<EventRuleSpec>(); rule.eventRuleSpecs = new List<EventRuleSpec>();
foreach (XmlNode node in element.ChildNodes) foreach (XmlNode node in element.ChildNodes)
{ {
if (node is not XmlElement childElement) if (!(node is XmlElement childElement))
{ {
continue; continue;
} }
@ -423,7 +423,7 @@ namespace Obfuz
foreach (XmlNode node in element.ChildNodes) foreach (XmlNode node in element.ChildNodes)
{ {
if (node is not XmlElement childElement) if (!(node is XmlElement childElement))
{ {
continue; continue;
} }
@ -461,7 +461,7 @@ namespace Obfuz
foreach (XmlNode node in element.ChildNodes) foreach (XmlNode node in element.ChildNodes)
{ {
if (node is not XmlElement childElement) if (!(node is XmlElement childElement))
{ {
continue; continue;
} }
@ -577,7 +577,7 @@ namespace Obfuz
} }
foreach (XmlNode node in root.ChildNodes) foreach (XmlNode node in root.ChildNodes)
{ {
if (node is not XmlElement element) if (!(node is XmlElement element))
{ {
continue; continue;
} }

View File

@ -252,7 +252,7 @@ namespace Obfuz
var root = doc.DocumentElement; var root = doc.DocumentElement;
foreach (XmlNode node in root.ChildNodes) foreach (XmlNode node in root.ChildNodes)
{ {
if (node is not XmlElement element) if (!(node is XmlElement element))
{ {
continue; continue;
} }
@ -273,11 +273,11 @@ namespace Obfuz
{ {
oldAssName = assemblyName, oldAssName = assemblyName,
newAssName = newAssemblyName, newAssName = newAssemblyName,
status = System.Enum.Parse<RenameStatus>(ele.Attributes["status"].Value), status = (RenameStatus)System.Enum.Parse(typeof(RenameStatus), ele.Attributes["status"].Value),
}; };
foreach (XmlNode node in ele.ChildNodes) foreach (XmlNode node in ele.ChildNodes)
{ {
if (node is not XmlElement element) if (!(node is XmlElement element))
{ {
continue; continue;
} }
@ -298,11 +298,11 @@ namespace Obfuz
{ {
oldFullName = typeName, oldFullName = typeName,
newFullName = newTypeName, newFullName = newTypeName,
status = System.Enum.Parse<RenameStatus>(ele.Attributes["status"].Value), status = (RenameStatus)System.Enum.Parse(typeof(RenameStatus), ele.Attributes["status"].Value),
}; };
foreach (XmlNode node in ele.ChildNodes) foreach (XmlNode node in ele.ChildNodes)
{ {
if (node is not XmlElement c) if (!(node is XmlElement c))
{ {
continue; continue;
} }
@ -330,7 +330,7 @@ namespace Obfuz
}; };
foreach (XmlNode node in ele.ChildNodes) foreach (XmlNode node in ele.ChildNodes)
{ {
if (node is not XmlElement c) if (!(node is XmlElement c))
{ {
continue; continue;
} }