public void CleanUp() { foreach (var field in structFieldsToFix.GetKeys()) { field.DeclaringType.Fields.Remove(field); } }
Dictionary<FieldDef, bool> FindFieldTypes(FieldDefAndDeclaringTypeDict<FieldDef> fields) { var validFields = new Dictionary<FieldDef, bool>(fields.Count); foreach (var field in fields.GetKeys()) validFields.Add(field, false); foreach (var type in module.GetTypes()) { if (validFields.Count == 0) break; foreach (var method in type.Methods) { var body = method.Body; if (body == null) continue; foreach (var instr in body.Instructions) { if (instr.OpCode.Code == Code.Ldsfld) continue; var field = instr.Operand as IField; if (field == null) continue; var validType = fields.Find(field); if (validType == null) continue; validFields.Remove(validType); } } } return validFields; }
public new void Find() { if (delegateCreatorMethods.Count == 0) return; var cctor = DotNetUtils.GetModuleTypeCctor(module); if (cctor == null) return; Logger.v("Finding all proxy delegates"); var delegateInfos = CreateDelegateInitInfos(cctor); fieldToMethods = CreateFieldToMethodsDictionary(cctor.DeclaringType); if (delegateInfos.Count < fieldToMethods.Count) throw new ApplicationException("Missing proxy delegates"); var delegateToFields = new Dictionary<TypeDef, List<FieldDef>>(); foreach (var field in fieldToMethods.GetKeys()) { if (!delegateToFields.TryGetValue(field.FieldType.TryGetTypeDef(), out var list)) delegateToFields[field.FieldType.TryGetTypeDef()] = list = new List<FieldDef>(); list.Add(field); } foreach (var kv in delegateToFields) { var type = kv.Key; var fields = kv.Value; Logger.v("Found proxy delegate: {0} ({1:X8})", Utils.RemoveNewlines(type), type.MDToken.ToInt32()); RemovedDelegateCreatorCalls++; Logger.Instance.Indent(); foreach (var field in fields) { var proxyMethods = fieldToMethods.Find(field); if (proxyMethods == null) continue; var info = delegateInfos.Find(field); if (info == null) throw new ApplicationException("Missing proxy info"); GetCallInfo(info, field, out var calledMethod, out var callOpcode); if (calledMethod == null) continue; foreach (var proxyMethod in proxyMethods) { Add(proxyMethod, new DelegateInfo(field, calledMethod, callOpcode)); Logger.v("Field: {0}, Opcode: {1}, Method: {2} ({3:X8})", Utils.RemoveNewlines(field.Name), callOpcode, Utils.RemoveNewlines(calledMethod), calledMethod.MDToken.ToUInt32()); } } Logger.Instance.DeIndent(); delegateTypesDict[type] = true; } // 1.2 r54564 (almost 1.3) now moves method proxy init code to the delegate cctors Find2(); }
Dictionary <FieldDef, bool> FindFieldTypes(FieldDefAndDeclaringTypeDict <FieldDef> fields) { var validFields = new Dictionary <FieldDef, bool>(fields.Count); foreach (var field in fields.GetKeys()) { validFields.Add(field, false); } foreach (var type in module.GetTypes()) { if (validFields.Count == 0) { break; } foreach (var method in type.Methods) { var body = method.Body; if (body == null) { continue; } foreach (var instr in body.Instructions) { if (instr.OpCode.Code == Code.Ldsfld) { continue; } var field = instr.Operand as IField; if (field == null) { continue; } var validType = fields.Find(field); if (validType == null) { continue; } validFields.Remove(validType); } } } return(validFields); }
public new void Find() { if (delegateCreatorMethods.Count == 0) return; var cctor = DotNetUtils.GetModuleTypeCctor(module); if (cctor == null) return; Logger.v("Finding all proxy delegates"); var delegateInfos = CreateDelegateInitInfos(cctor); fieldToMethods = CreateFieldToMethodsDictionary(cctor.DeclaringType); if (delegateInfos.Count < fieldToMethods.Count) throw new ApplicationException("Missing proxy delegates"); var delegateToFields = new Dictionary<TypeDef, List<FieldDef>>(); foreach (var field in fieldToMethods.GetKeys()) { List<FieldDef> list; if (!delegateToFields.TryGetValue(field.FieldType.TryGetTypeDef(), out list)) delegateToFields[field.FieldType.TryGetTypeDef()] = list = new List<FieldDef>(); list.Add(field); } foreach (var kv in delegateToFields) { var type = kv.Key; var fields = kv.Value; Logger.v("Found proxy delegate: {0} ({1:X8})", Utils.RemoveNewlines(type), type.MDToken.ToInt32()); RemovedDelegateCreatorCalls++; Logger.Instance.Indent(); foreach (var field in fields) { var proxyMethods = fieldToMethods.Find(field); if (proxyMethods == null) continue; var info = delegateInfos.Find(field); if (info == null) throw new ApplicationException("Missing proxy info"); IMethod calledMethod; OpCode callOpcode; GetCallInfo(info, field, out calledMethod, out callOpcode); if (calledMethod == null) continue; foreach (var proxyMethod in proxyMethods) { Add(proxyMethod, new DelegateInfo(field, calledMethod, callOpcode)); Logger.v("Field: {0}, Opcode: {1}, Method: {2} ({3:X8})", Utils.RemoveNewlines(field.Name), callOpcode, Utils.RemoveNewlines(calledMethod), calledMethod.MDToken.ToUInt32()); } } Logger.Instance.DeIndent(); delegateTypesDict[type] = true; } // 1.2 r54564 (almost 1.3) now moves method proxy init code to the delegate cctors Find2(); }