├── Assemblies
├── Mono.Cecil.dll
├── Mono.Cecil.Mdb.dll
├── Mono.Cecil.Pdb.dll
├── nunit.framework.dll
└── Mono.Cecil.Rocks.dll
├── IRewriteStep.cs
├── DebugSymbolFormat.cs
├── IMethodDefinitionVisitor.cs
├── packages
└── repositories.config
├── packages.config
├── .gitignore
├── Test
├── Test.Driver
│ ├── packages.config
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── ReferenceRewriting.cs
│ └── Test.Driver.csproj
├── Test.Target
│ ├── ObjectStore.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Test.Target.csproj
├── Test.UnitTests
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── TypeAliasesTest.cs
│ └── Test.UnitTests.csproj
└── Test.Support
│ ├── Properties
│ └── AssemblyInfo.cs
│ ├── ArrayList.cs
│ └── Test.Support.csproj
├── RewriteStep.cs
├── IReferenceVisitor.cs
├── MethodAliases.cs
├── RewriteOperation.cs
├── MethodDefinitionDispatcher.cs
├── Properties
└── AssemblyInfo.cs
├── NuGetAssemblyResolver.cs
├── RemoveStrongNamesFromAssemblyReferences.cs
├── SearchPathAssemblyResolver.cs
├── NuGetPackageResolver.cs
├── RewriteAssemblyManifest.cs
├── RewriteMethodSpecMemberRefs.cs
├── rrw.csproj
├── TypeAliases.cs
├── Program.cs
├── RewriteContext.cs
├── ReferenceDispatcher.cs
├── EnumeratorGenericConstraintsFixer.cs
├── rrw.sln
├── MiniJSON.cs
├── RewriteTypeReferences.cs
└── Options.cs
/Assemblies/Mono.Cecil.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Unity-Technologies/ReferenceRewriter/HEAD/Assemblies/Mono.Cecil.dll
--------------------------------------------------------------------------------
/Assemblies/Mono.Cecil.Mdb.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Unity-Technologies/ReferenceRewriter/HEAD/Assemblies/Mono.Cecil.Mdb.dll
--------------------------------------------------------------------------------
/Assemblies/Mono.Cecil.Pdb.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Unity-Technologies/ReferenceRewriter/HEAD/Assemblies/Mono.Cecil.Pdb.dll
--------------------------------------------------------------------------------
/Assemblies/nunit.framework.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Unity-Technologies/ReferenceRewriter/HEAD/Assemblies/nunit.framework.dll
--------------------------------------------------------------------------------
/Assemblies/Mono.Cecil.Rocks.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Unity-Technologies/ReferenceRewriter/HEAD/Assemblies/Mono.Cecil.Rocks.dll
--------------------------------------------------------------------------------
/IRewriteStep.cs:
--------------------------------------------------------------------------------
1 | namespace Unity.ReferenceRewriter
2 | {
3 | interface IRewriteStep
4 | {
5 | void Execute(RewriteContext context);
6 | }
7 | }
--------------------------------------------------------------------------------
/DebugSymbolFormat.cs:
--------------------------------------------------------------------------------
1 | namespace Unity.ReferenceRewriter
2 | {
3 | public enum DebugSymbolFormat
4 | {
5 | None,
6 | Pdb,
7 | Mdb,
8 | }
9 | }
--------------------------------------------------------------------------------
/IMethodDefinitionVisitor.cs:
--------------------------------------------------------------------------------
1 | using Mono.Cecil;
2 |
3 | namespace Unity.ReferenceRewriter
4 | {
5 | interface IMethodDefinitionVisitor
6 | {
7 | void Visit(MethodDefinition method);
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/packages/repositories.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | bin
2 | obj
3 | *.suo
4 | *.user
5 | *.pidb
6 | *.userprefs
7 | *.xml
8 | *.nupkg
9 | **/test-results/*
10 | *Resharper*
11 | Test/*.dll
12 | Test/*.mdb
13 | Test/*.pdb
14 | packages/*
15 | !packages/repositories.config
16 |
--------------------------------------------------------------------------------
/Test/Test.Driver/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/RewriteStep.cs:
--------------------------------------------------------------------------------
1 | namespace Unity.ReferenceRewriter
2 | {
3 | abstract class RewriteStep : IRewriteStep
4 | {
5 | protected RewriteContext Context { get; private set; }
6 |
7 | public void Execute(RewriteContext context)
8 | {
9 | Context = context;
10 |
11 | Run();
12 | }
13 |
14 | protected abstract void Run();
15 | }
16 | }
--------------------------------------------------------------------------------
/Test/Test.Target/ObjectStore.cs:
--------------------------------------------------------------------------------
1 | using System.Collections;
2 |
3 | namespace Test.Target
4 | {
5 | public class ObjectStore
6 | {
7 | private readonly ArrayList _list = new ArrayList();
8 |
9 | public void AddObject(object obj)
10 | {
11 | _list.Add(obj);
12 | }
13 |
14 | public void DeleteObject(object obj)
15 | {
16 | _list.Remove(obj);
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/IReferenceVisitor.cs:
--------------------------------------------------------------------------------
1 | using Mono.Cecil;
2 | using Mono.Cecil.Cil;
3 |
4 | namespace Unity.ReferenceRewriter
5 | {
6 | interface IReferenceVisitor
7 | {
8 | void Visit(TypeReference type, string referencingEntityName);
9 | void Visit(FieldReference field, string referencingEntityName);
10 | void Visit(MethodReference method, string referencingEntityName);
11 |
12 | bool MethodChanged { get; }
13 | MethodReference ParamsMethod { get; }
14 |
15 | void RewriteObjectListToParamsCall(MethodBody methodBody, int instructionIndex);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/MethodAliases.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 |
6 | namespace Unity.ReferenceRewriter
7 | {
8 | class MethodAliases
9 | {
10 | private static readonly SortedSet> _aliases;
11 |
12 | static MethodAliases()
13 | {
14 | _aliases = new SortedSet>
15 | {
16 | new Tuple(
17 | "Dispose", "Close")
18 | };
19 | }
20 |
21 |
22 | public static bool AreAliases(string typeA, string typeB)
23 | {
24 | var areAliases = _aliases.Contains(new Tuple(typeA, typeB)) ||
25 | _aliases.Contains(new Tuple(typeB, typeA));
26 |
27 | return areAliases;
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/RewriteOperation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace Unity.ReferenceRewriter
5 | {
6 | public class RewriteOperation
7 | {
8 | private readonly List _steps;
9 |
10 | private RewriteOperation(params IRewriteStep[] steps) : this(steps as IEnumerable)
11 | {
12 | }
13 |
14 | private RewriteOperation(IEnumerable steps)
15 | {
16 | _steps = new List(steps);
17 | }
18 |
19 | public void Execute(RewriteContext context)
20 | {
21 | if (context == null)
22 | throw new ArgumentNullException("context");
23 |
24 | foreach (var step in _steps)
25 | step.Execute(context);
26 | }
27 |
28 | public static RewriteOperation Create(Func supportNamespaceMapper)
29 | {
30 | if (supportNamespaceMapper == null)
31 | throw new ArgumentNullException("supportNamespaceMapper");
32 |
33 | return new RewriteOperation(
34 | new RewriteAssemblyManifest(),
35 | new RewriteTypeReferences(supportNamespaceMapper),
36 | new RewriteMethodSpecMemberRefs(),
37 | new EnumeratorGenericConstraintsFixer(),
38 | new RemoveStrongNamesFromAssemblyReferences()
39 | );
40 | }
41 | }
42 | }
--------------------------------------------------------------------------------
/MethodDefinitionDispatcher.cs:
--------------------------------------------------------------------------------
1 | using Mono.Cecil;
2 |
3 | namespace Unity.ReferenceRewriter
4 | {
5 | class MethodDefinitionDispatcher
6 | {
7 | private readonly ModuleDefinition _module;
8 | private readonly IMethodDefinitionVisitor _visitor;
9 |
10 | public static void DispatchOn(ModuleDefinition module, IMethodDefinitionVisitor visitor)
11 | {
12 | new MethodDefinitionDispatcher(module, visitor).Dispatch();
13 | }
14 |
15 | private MethodDefinitionDispatcher(ModuleDefinition module, IMethodDefinitionVisitor visitor)
16 | {
17 | _module = module;
18 | _visitor = visitor;
19 | }
20 |
21 | private void Dispatch()
22 | {
23 | foreach (var type in _module.Types)
24 | Dispatch(type);
25 | }
26 |
27 | private void Dispatch(TypeDefinition type)
28 | {
29 | foreach (var nestedType in type.NestedTypes)
30 | Dispatch(nestedType);
31 |
32 | foreach (var method in type.Methods)
33 | Visit(method);
34 |
35 | foreach (var property in type.Properties)
36 | {
37 | Visit(property.GetMethod);
38 | Visit(property.SetMethod);
39 | }
40 |
41 | foreach (var @event in type.Events)
42 | {
43 | Visit(@event.AddMethod);
44 | Visit(@event.InvokeMethod);
45 | Visit(@event.RemoveMethod);
46 | }
47 | }
48 |
49 | private void Visit(MethodDefinition method)
50 | {
51 | if (method == null)
52 | return;
53 |
54 | _visitor.Visit(method);
55 | }
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("rrw")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("rrw")]
13 | [assembly: AssemblyCopyright("Copyright © 2012")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("162e964d-d0ab-49dc-9f1f-207ca039f151")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Test/Test.UnitTests/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Test.UnitTests")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Test.UnitTests")]
13 | [assembly: AssemblyCopyright("Copyright © 2015")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("4709f0f7-f40b-4bdb-a42e-c8a47022b08f")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Test/Test.Driver/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Test.Driver")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Test.Driver")]
13 | [assembly: AssemblyCopyright("Copyright © 2012")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("24055476-9996-4074-b26e-e7111dd0cfab")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Test/Test.Support/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Test.Support")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Test.Support")]
13 | [assembly: AssemblyCopyright("Copyright © 2012")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("8c99bed6-952c-4deb-a751-e6a611b85cd8")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Test/Test.Target/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Test.Target")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Test.Target")]
13 | [assembly: AssemblyCopyright("Copyright © 2012")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("1cd969b4-ff17-42dd-913d-f3d4c878f0ef")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/NuGetAssemblyResolver.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using Mono.Cecil;
5 |
6 | namespace Unity.ReferenceRewriter
7 | {
8 | internal class NuGetAssemblyResolver : SearchPathAssemblyResolver
9 | {
10 | private readonly Dictionary _references;
11 | private readonly Dictionary _assemblies = new Dictionary(StringComparer.InvariantCulture);
12 |
13 | public NuGetAssemblyResolver(string projectLockFile)
14 | {
15 | var resolver = new NuGetPackageResolver
16 | {
17 | ProjectLockFile = projectLockFile,
18 | };
19 | resolver.Resolve();
20 | var references = resolver.ResolvedReferences;
21 |
22 | _references = new Dictionary(references.Length, StringComparer.InvariantCultureIgnoreCase);
23 | foreach (var reference in references)
24 | {
25 | var fileName = Path.GetFileName(reference);
26 | string existingReference;
27 | if (_references.TryGetValue(fileName, out existingReference))
28 | throw new Exception(string.Format("Reference \"{0}\" already added as \"{1}\".", reference, existingReference));
29 | _references.Add(fileName, reference);
30 | }
31 | }
32 |
33 | public override AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
34 | {
35 | AssemblyDefinition assembly;
36 | if (_assemblies.TryGetValue(name.Name, out assembly))
37 | return assembly;
38 |
39 | var fileName = name.Name + (name.IsWindowsRuntime ? ".winmd" : ".dll");
40 | string reference;
41 | if (_references.TryGetValue(fileName, out reference))
42 | {
43 | assembly = AssemblyDefinition.ReadAssembly(reference, parameters);
44 | if (string.Equals(assembly.Name.Name, name.Name, StringComparison.InvariantCulture))
45 | {
46 | _assemblies.Add(name.Name, assembly);
47 | return assembly;
48 | }
49 | }
50 |
51 | return base.Resolve(name, parameters);
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/RemoveStrongNamesFromAssemblyReferences.cs:
--------------------------------------------------------------------------------
1 | using Mono.Cecil;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Text;
7 |
8 | namespace Unity.ReferenceRewriter
9 | {
10 | class RemoveStrongNamesFromAssemblyReferences : RewriteStep
11 | {
12 | protected override void Run()
13 | {
14 | foreach (var reference in Context.TargetModule.AssemblyReferences)
15 | {
16 | if (ShouldRemoveStrongName(reference))
17 | {
18 | RemoveStrongName(reference);
19 | }
20 | }
21 | }
22 |
23 | private bool ShouldRemoveStrongName(AssemblyNameReference reference)
24 | {
25 | // Strong name is not present already
26 | if (reference.PublicKeyToken == null || reference.PublicKeyToken.Length == 0)
27 | {
28 | return false;
29 | }
30 |
31 | // Strong name must be kept
32 | if (Context.StrongNameReferences.Any(r => r == reference.Name))
33 | {
34 | return false;
35 | }
36 |
37 | AssemblyDefinition assembly;
38 |
39 | try
40 | {
41 | // Can't find target assembly
42 | assembly = Context.AssemblyResolver.Resolve(reference);
43 | }
44 | catch (AssemblyResolutionException)
45 | {
46 | return false;
47 | }
48 |
49 | // Don't remove strong name to framework references
50 | var assemblyDir = NormalizePath(Path.GetDirectoryName(assembly.MainModule.FullyQualifiedName));
51 | if (Context.FrameworkPaths.Any(path => NormalizePath(path) == assemblyDir))
52 | {
53 | return false;
54 | }
55 |
56 | return true;
57 | }
58 |
59 | private void RemoveStrongName(AssemblyNameReference reference)
60 | {
61 | Context.RewriteTarget = true;
62 | reference.PublicKeyToken = new byte[0];
63 | }
64 |
65 | public static string NormalizePath(string path)
66 | {
67 | return Path.GetFullPath(path).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).ToUpperInvariant();
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/Test/Test.Support/ArrayList.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Collections.Generic;
4 |
5 | namespace Test.Support.Collections
6 | {
7 | public class ArrayList : IList
8 | {
9 | private readonly List