From ea1e202962ab36e4718fdc84864ceed407d2d502 Mon Sep 17 00:00:00 2001 From: js6pak Date: Fri, 5 Mar 2021 18:49:58 +0100 Subject: [PATCH] Fix issues with nested types --- Reactor.Greenhouse/Compiler.cs | 3 - .../Generation/GenerationContext.cs | 7 -- Reactor.Greenhouse/Generation/Generator.cs | 24 ++++-- Reactor.Greenhouse/Generation/TypeContext.cs | 85 ++++++++++--------- .../Reactor.OxygenFilter.MSBuild.csproj | 4 +- Reactor.OxygenFilter/Mappings.cs | 8 +- Reactor.OxygenFilter/ObfuscationMapper.cs | 11 ++- .../Reactor.OxygenFilter.csproj | 2 +- 8 files changed, 84 insertions(+), 60 deletions(-) diff --git a/Reactor.Greenhouse/Compiler.cs b/Reactor.Greenhouse/Compiler.cs index d989e96..f8b2571 100644 --- a/Reactor.Greenhouse/Compiler.cs +++ b/Reactor.Greenhouse/Compiler.cs @@ -134,11 +134,8 @@ namespace Reactor.Greenhouse ); nested.Compile(nestedDef, mappings); - mappings.Types.Add(nested); } - type.Nested.Clear(); - foreach (var property in type.Properties) { try diff --git a/Reactor.Greenhouse/Generation/GenerationContext.cs b/Reactor.Greenhouse/Generation/GenerationContext.cs index 49a88c1..0a807f1 100644 --- a/Reactor.Greenhouse/Generation/GenerationContext.cs +++ b/Reactor.Greenhouse/Generation/GenerationContext.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using Mono.Cecil; -using Mono.Cecil.Rocks; namespace Reactor.Greenhouse.Generation { @@ -9,18 +8,12 @@ namespace Reactor.Greenhouse.Generation public ModuleDefinition CleanModule { get; } public ModuleDefinition ObfuscatedModule { get; } - public IEnumerable AllCleanTypes { get; } - public IEnumerable AllObfuscatedTypes { get; } - public Dictionary Map { get; } = new Dictionary(); public GenerationContext(ModuleDefinition cleanModule, ModuleDefinition obfuscatedModule) { CleanModule = cleanModule; ObfuscatedModule = obfuscatedModule; - - AllCleanTypes = CleanModule.GetAllTypes(); - AllObfuscatedTypes = ObfuscatedModule.GetAllTypes(); } } } diff --git a/Reactor.Greenhouse/Generation/Generator.cs b/Reactor.Greenhouse/Generation/Generator.cs index caa8ef2..67c880e 100644 --- a/Reactor.Greenhouse/Generation/Generator.cs +++ b/Reactor.Greenhouse/Generation/Generator.cs @@ -1,5 +1,7 @@ using System; +using System.Linq; using Mono.Cecil; +using Mono.Cecil.Rocks; using Reactor.OxygenFilter; namespace Reactor.Greenhouse.Generation @@ -37,10 +39,22 @@ namespace Reactor.Greenhouse.Generation Console.WriteLine("LookupTypes took " + lookupTypes); - foreach (var (obfuscatedType, typeContext) in context.Map) + foreach (var (obfuscatedType, typeContext) in context.Map.ToDictionary(k => k.Key, v => v.Value)) { - var mappedType = typeContext.ToMappedType(obfuscatedType); - if (mappedType == null) + var mappedType = typeContext.ToMappedType( + obfuscatedType, + (obfuscatedNested, cleanNested) => + { + if (!context.Map.TryGetValue(obfuscatedNested, out var nested)) + { + nested = new TypeContext(context, double.MaxValue, cleanNested); + context.Map[obfuscatedNested] = nested; + } + + return nested; + }); + + if (mappedType == null || obfuscatedType.DeclaringType != null) continue; result.Types.Add(mappedType); @@ -225,7 +239,7 @@ namespace Reactor.Greenhouse.Generation private static void LookupTypes(GenerationContext context) { - foreach (var cleanType in context.AllCleanTypes) + foreach (var cleanType in context.CleanModule.GetAllTypes()) { if (cleanType.Name.StartsWith("<")) continue; @@ -233,7 +247,7 @@ namespace Reactor.Greenhouse.Generation TypeDefinition winner = null; var winnerPoints = 0d; - foreach (var obfuscatedType in context.AllObfuscatedTypes) + foreach (var obfuscatedType in context.ObfuscatedModule.GetAllTypes()) { if (obfuscatedType.Name.StartsWith("<") && obfuscatedType.Name.EndsWith(">")) continue; diff --git a/Reactor.Greenhouse/Generation/TypeContext.cs b/Reactor.Greenhouse/Generation/TypeContext.cs index f7c4f74..8272a7a 100644 --- a/Reactor.Greenhouse/Generation/TypeContext.cs +++ b/Reactor.Greenhouse/Generation/TypeContext.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; @@ -29,15 +30,40 @@ namespace Reactor.Greenhouse.Generation private static readonly Regex CompilerGeneratedRegex = new Regex(@"^<([\w\d]+)>.__\d+$", RegexOptions.Compiled); - public MappedType ToMappedType(TypeDefinition obfuscatedType) + public MappedType ToMappedType(TypeDefinition obfuscatedType, Func nestedFunc) { - var mappedType = new MappedType(obfuscatedType.FullName, CleanType.Name); + var match = CompilerGeneratedRegex.Match(CleanType.Name); + var mappedType = new MappedType(obfuscatedType.FullName, match.Success ? (match.Groups[1].Value + "__d") : CleanType.Name.Replace("<>", "")); for (var i = 0; i < CleanType.Fields.Count; i++) { var cleanField = CleanType.Fields[i]; var obfuscatedField = obfuscatedType.Fields[i]; + if (obfuscatedType.DeclaringType != null) + { + switch (obfuscatedField.Name) + { + case "<>1__state": + mappedType.Fields.Add(new MappedMember(obfuscatedField.Name, "__state")); + continue; + case "<>2__current": + mappedType.Fields.Add(new MappedMember(obfuscatedField.Name, "__current")); + continue; + case "<>4__this": + mappedType.Fields.Add(new MappedMember(obfuscatedField.Name, "__this")); + continue; + } + + var fieldMatch = CompilerGeneratedRegex.Match(obfuscatedField.Name); + + if (fieldMatch.Success) + { + mappedType.Fields.Add(new MappedMember(obfuscatedField.Name, fieldMatch.Groups[1].Value)); + continue; + } + } + if (!obfuscatedField.Name.IsObfuscated()) continue; @@ -106,6 +132,19 @@ namespace Reactor.Greenhouse.Generation } } + if (obfuscatedType.DeclaringType != null) + { + foreach (var obfuscatedMethod in obfuscatedType.Methods) + { + var methodMatch = CompilerGeneratedRegex.Match(obfuscatedMethod.Name); + + if (methodMatch.Success) + { + mappedType.Methods.Add(new MappedMethod(obfuscatedMethod, methodMatch.Groups[1].Value)); + } + } + } + for (var i = 0; i < CleanType.NestedTypes.Count; i++) { var cleanNested = CleanType.NestedTypes[i]; @@ -116,47 +155,13 @@ namespace Reactor.Greenhouse.Generation break; } - var match = CompilerGeneratedRegex.Match(cleanNested.Name); + var nestedContext = nestedFunc(obfuscatedNested, cleanNested); + var mappedNested = nestedContext.ToMappedType(obfuscatedNested, nestedFunc); - var nested = new MappedType(obfuscatedNested.FullName, match.Success ? (match.Groups[1].Value + "__d") : cleanNested.Name.Replace("<>", "")); - - foreach (var nestedField in obfuscatedNested.Fields) + if (mappedNested != null) { - switch (nestedField.Name) - { - case "<>1__state": - nested.Fields.Add(new MappedMember(nestedField.Name, "__state")); - continue; - case "<>2__current": - nested.Fields.Add(new MappedMember(nestedField.Name, "__current")); - continue; - case "<>4__this": - nested.Fields.Add(new MappedMember(nestedField.Name, "__this")); - continue; - } - - var fieldMatch = CompilerGeneratedRegex.Match(nestedField.Name); - - if (fieldMatch.Success) - { - nested.Fields.Add(new MappedMember(nestedField.Name, fieldMatch.Groups[1].Value)); - } + mappedType.Nested.Add(mappedNested); } - - foreach (var nestedMethod in obfuscatedNested.Methods) - { - var methodMatch = CompilerGeneratedRegex.Match(nestedMethod.Name); - - if (methodMatch.Success) - { - nested.Methods.Add(new MappedMethod(nestedMethod, methodMatch.Groups[1].Value)); - } - } - - if (!obfuscatedNested.Name.IsObfuscated() && nested.Mapped == cleanNested.Name && !nested.Fields.Any() && !nested.Methods.Any()) - continue; - - mappedType.Nested.Add(nested); } if (!obfuscatedType.Name.IsObfuscated() && !mappedType.Fields.Any() && !mappedType.Methods.Any() && !mappedType.Properties.Any() && !mappedType.Nested.Any()) diff --git a/Reactor.OxygenFilter.MSBuild/Reactor.OxygenFilter.MSBuild.csproj b/Reactor.OxygenFilter.MSBuild/Reactor.OxygenFilter.MSBuild.csproj index 050133b..0853f42 100644 --- a/Reactor.OxygenFilter.MSBuild/Reactor.OxygenFilter.MSBuild.csproj +++ b/Reactor.OxygenFilter.MSBuild/Reactor.OxygenFilter.MSBuild.csproj @@ -4,11 +4,13 @@ latest true - 0.2.8 + 0.2.9 git https://github.com/NuclearPowered/Reactor.OxygenFilter LGPL-3.0-or-later Library for using Reactor.OxygenFilter with msbuild + true + true diff --git a/Reactor.OxygenFilter/Mappings.cs b/Reactor.OxygenFilter/Mappings.cs index d49519c..ffc3ead 100644 --- a/Reactor.OxygenFilter/Mappings.cs +++ b/Reactor.OxygenFilter/Mappings.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text; using Mono.Cecil; using Newtonsoft.Json; @@ -17,17 +18,22 @@ namespace Reactor.OxygenFilter if (split.Length > 1) { MappedType type = null; + var current = new StringBuilder(); foreach (var s in split) { + current.Append(s); + if (type == null) { type = Find(s, predicate); } else { - type = type.Nested.SingleOrDefault(x => predicate(x) == s); + type = type.Nested.SingleOrDefault(x => predicate(x) == current.ToString()); } + + current.Append('/'); } return type; diff --git a/Reactor.OxygenFilter/ObfuscationMapper.cs b/Reactor.OxygenFilter/ObfuscationMapper.cs index 26ca27e..e072d72 100644 --- a/Reactor.OxygenFilter/ObfuscationMapper.cs +++ b/Reactor.OxygenFilter/ObfuscationMapper.cs @@ -67,8 +67,10 @@ namespace Reactor.OxygenFilter } } - void MapType(MappedType type, TypeDefinition typeDef) + void MapType(MappedType type) { + var typeDef = ModuleDef.GetType(type.Original.Name); + if (typeDef == null) { throw new NullReferenceException($"Type {type} was not found!"); @@ -143,11 +145,16 @@ namespace Reactor.OxygenFilter } } } + + foreach (var nestedType in type.Nested) + { + MapType(nestedType); + } } foreach (var type in Mappings.Types) { - MapType(type, ModuleDef.GetType(type.Original.Name)); + MapType(type); } foreach (var typeDef in ModuleDef.Types) diff --git a/Reactor.OxygenFilter/Reactor.OxygenFilter.csproj b/Reactor.OxygenFilter/Reactor.OxygenFilter.csproj index 9009231..23d75ef 100644 --- a/Reactor.OxygenFilter/Reactor.OxygenFilter.csproj +++ b/Reactor.OxygenFilter/Reactor.OxygenFilter.csproj @@ -3,7 +3,7 @@ net472;netstandard2.0 latest - 0.1.2 + 0.1.3 git https://github.com/NuclearPowered/Reactor.OxygenFilter LGPL-3.0-or-later