Fix issues with nested types

This commit is contained in:
js6pak committed 2021-03-05 18:49:58 +01:00
1 parent d31b0a37b3
commit ea1e202962
8 files changed
+84 -60

No files matched your search

-3
View File
@@ -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
@@ -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<TypeDefinition> AllCleanTypes { get; }
public IEnumerable<TypeDefinition> AllObfuscatedTypes { get; }
public Dictionary<TypeDefinition, TypeContext> Map { get; } = new Dictionary<TypeDefinition, TypeContext>();
public GenerationContext(ModuleDefinition cleanModule, ModuleDefinition obfuscatedModule)
{
CleanModule = cleanModule;
ObfuscatedModule = obfuscatedModule;
AllCleanTypes = CleanModule.GetAllTypes();
AllObfuscatedTypes = ObfuscatedModule.GetAllTypes();
}
}
}
+19 -5
View File
@@ -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;
+45 -40
View File
@@ -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<TypeDefinition, TypeDefinition, TypeContext> 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())
@@ -4,11 +4,13 @@
<LangVersion>latest</LangVersion>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<Version>0.2.8</Version>
<Version>0.2.9</Version>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/NuclearPowered/Reactor.OxygenFilter</RepositoryUrl>
<PackageLicenseExpression>LGPL-3.0-or-later</PackageLicenseExpression>
<Description>Library for using Reactor.OxygenFilter with msbuild</Description>
<DevelopmentDependency>true</DevelopmentDependency>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
+7 -1
View File
@@ -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;
+9 -2
View File
@@ -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)
@@ -3,7 +3,7 @@
<TargetFrameworks>net472;netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Version>0.1.2</Version>
<Version>0.1.3</Version>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/NuclearPowered/Reactor.OxygenFilter</RepositoryUrl>
<PackageLicenseExpression>LGPL-3.0-or-later</PackageLicenseExpression>