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; }
static short[] FindKey(MethodDef initMethod, FieldDefAndDeclaringTypeDict<bool> fields) { var instrs = initMethod.Body.Instructions; for (int i = 0; i < instrs.Count - 2; i++) { var ldci4 = instrs[i]; if (!ldci4.IsLdcI4()) continue; var newarr = instrs[i + 1]; if (newarr.OpCode.Code != Code.Newarr) continue; if (newarr.Operand.ToString() != "System.Char") continue; var stloc = instrs[i + 2]; if (!stloc.IsStloc()) continue; var local = stloc.GetLocal(initMethod.Body.Variables); int startInitIndex = i; i++; var array = ArrayFinder.GetInitializedInt16Array(ldci4.GetLdcI4Value(), initMethod, ref i); if (array == null) continue; var field = GetStoreField(initMethod, startInitIndex, local); if (field == null) continue; if (fields.Find(field)) return array; } return null; }
FieldDefAndDeclaringTypeDict<FieldDef> FindFieldTypes() { var dict = new FieldDefAndDeclaringTypeDict<FieldDef>(); foreach (var type in module.GetTypes()) { 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 FieldDef; if (field == null) continue; var declType = field.DeclaringType; if (declType == null) continue; if (!InlinedMethodTypes.IsValidFieldType(declType)) continue; dict.Add(field, field); } } } return dict; }
public void initializeEventHandlerNames() { var ourFields = new FieldDefAndDeclaringTypeDict <MFieldDef>(); foreach (var fieldDef in type.AllFields) { ourFields.add(fieldDef.FieldDef, fieldDef); } var ourMethods = new MethodDefAndDeclaringTypeDict <MMethodDef>(); foreach (var methodDef in type.AllMethods) { ourMethods.add(methodDef.MethodDef, methodDef); } initVbEventHandlers(ourFields, ourMethods); initFieldEventHandlers(ourFields, ourMethods); initTypeEventHandlers(ourFields, ourMethods); }
static internal IEnumerable<FieldDef> GetFields(TypeDef type) { var typeFields = new FieldDefAndDeclaringTypeDict<FieldDef>(); foreach (var field in type.Fields) typeFields.Add(field, field); var realFields = new Dictionary<FieldDef, bool>(); foreach (var method in type.Methods) { if (method.Body == null) continue; foreach (var instr in method.Body.Instructions) { var fieldRef = instr.Operand as IField; if (fieldRef == null) continue; var field = typeFields.Find(fieldRef); if (field == null) continue; realFields[field] = true; } } return realFields.Keys; }
FieldDefAndDeclaringTypeDict <FieldDef> FindFieldTypes() { var dict = new FieldDefAndDeclaringTypeDict <FieldDef>(); foreach (var type in module.GetTypes()) { 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 FieldDef; if (field == null) { continue; } var declType = field.DeclaringType; if (declType == null) { continue; } if (!InlinedMethodTypes.IsValidFieldType(declType)) { continue; } dict.Add(field, field); } } } return(dict); }
int?FindOffsetValue(MethodDef method, FieldDef offsetField, FieldDefAndDeclaringTypeDict <IField> fields) { var instructions = method.Body.Instructions; for (int i = 0; i <= instructions.Count - 2; i++) { var ldstr = instructions[i]; if (ldstr.OpCode.Code != Code.Ldstr) { continue; } var stringVal = ldstr.Operand as string; if (stringVal == null) { continue; } var stsfld = instructions[i + 1]; if (stsfld.OpCode.Code != Code.Stsfld) { continue; } var field = stsfld.Operand as IField; if (field == null || fields.Find(field) != offsetField) { continue; } int value; if (!int.TryParse(stringVal, System.Globalization.NumberStyles.Integer, null, out value)) { continue; } return(value); } return(null); }
public void Initialize() { foreach (var kv in GetMovedTypes()) { var structType = kv.Key; structToOwners.Add(structType, kv.Value); foreach (var ownerType in kv.Value) { foreach (var ownerField in ownerType.Fields) { if (DotNetUtils.GetType(module, ownerField.FieldSig.GetFieldType()) != structType) continue; structFieldsToFix.Add(ownerField, true); break; } var fieldsDict = new FieldDefAndDeclaringTypeDict<FieldDef>(); typeToFieldsDict.Add(ownerType, fieldsDict); foreach (var structField in structType.Fields) { var newField = module.UpdateRowId(new FieldDefUser(structField.Name, structField.FieldSig.Clone(), structField.Attributes)); ownerType.Fields.Add(newField); fieldsDict.Add(structField, newField); } } } }
static FieldDefAndDeclaringTypeDict <bool> GetFields(MethodDef method) { var fields = new FieldDefAndDeclaringTypeDict <bool>(); foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Ldsfld && instr.OpCode.Code != Code.Stsfld) { continue; } var field = instr.Operand as FieldDef; if (field == null) { continue; } if (field.DeclaringType != method.DeclaringType) { continue; } fields.Add(field, true); } return(fields); }
public bool Initialize() { if (!FindMagic(Method, out arg1, out arg2, out magic)) { return(false); } fields = GetFields(Method); if (fields == null) { return(false); } arrayInfo = GetArrayInfo(cctor); if (arrayInfo == null) { return(false); } if (arrayInfo.initField.InitialValue.Length % 2 == 1) { return(false); } encryptedData = new ushort[arrayInfo.initField.InitialValue.Length / 2]; Buffer.BlockCopy(arrayInfo.initField.InitialValue, 0, encryptedData, 0, arrayInfo.initField.InitialValue.Length); decryptType = GetDecryptType(Method); keyShift = FindKeyShift(cctor); key = FindKey(); if (key == null || key.Length == 0) { return(false); } return(true); }
// Find the string decrypter string offset value or null if none found int? FindOffsetValue(MethodDef method) { var fieldDict = new FieldDefAndDeclaringTypeDict<IField>(); foreach (var field in method.DeclaringType.Fields) fieldDict.Add(field, field); var offsetField = FindOffsetField(method); if (offsetField == null) return null; return FindOffsetValue(method, (FieldDef)fieldDict.Find(offsetField), fieldDict); }
int? FindOffsetValue(MethodDef method, FieldDef offsetField, FieldDefAndDeclaringTypeDict<IField> fields) { var instructions = method.Body.Instructions; for (int i = 0; i <= instructions.Count - 2; i++) { var ldstr = instructions[i]; if (ldstr.OpCode.Code != Code.Ldstr) continue; var stringVal = ldstr.Operand as string; if (stringVal == null) continue; var stsfld = instructions[i + 1]; if (stsfld.OpCode.Code != Code.Stsfld) continue; var field = stsfld.Operand as IField; if (field == null || fields.Find(field) != offsetField) continue; int value; if (!int.TryParse(stringVal, System.Globalization.NumberStyles.Integer, null, out value)) continue; return value; } return null; }
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(); }
protected override object CheckCctor(TypeDef type, MethodDef cctor) { // Here if 1.2 r54564 (almost 1.3) or later var fieldToInfo = new FieldDefAndDeclaringTypeDict<DelegateInitInfo>(); var instrs = cctor.Body.Instructions; for (int i = 0; i < instrs.Count - 1; i++) { var ldtoken = instrs[i]; if (ldtoken.OpCode.Code != Code.Ldtoken) continue; var field = ldtoken.Operand as FieldDef; if (field == null || field.DeclaringType != cctor.DeclaringType) continue; var call = instrs[i + 1]; if (call.OpCode.Code != Code.Call) continue; var calledMethod = call.Operand as MethodDef; if (calledMethod == null) continue; if (!IsDelegateCreatorMethod(calledMethod)) continue; var info = methodToInfo.Find(calledMethod); if (info == null) continue; i++; fieldToInfo.Add(field, new DelegateInitInfo(field, calledMethod)); } return fieldToInfo.Count == 0 ? null : fieldToInfo; }
void InitFieldEventHandlers(FieldDefAndDeclaringTypeDict<MFieldDef> ourFields, MethodDefAndDeclaringTypeDict<MMethodDef> ourMethods) { var checker = NameChecker; foreach (var methodDef in type.AllMethods) { if (methodDef.MethodDef.Body == null) continue; if (methodDef.MethodDef.IsStatic) continue; var instructions = methodDef.MethodDef.Body.Instructions; for (int i = 0; i < instructions.Count - 6; i++) { // We're looking for this code pattern: // ldarg.0 // ldfld field // ldarg.0 // ldftn method / ldarg.0 + ldvirtftn // newobj event_handler_ctor // callvirt add_SomeEvent if (instructions[i].GetParameterIndex() != 0) continue; int index = i + 1; var ldfld = instructions[index++]; if (ldfld.OpCode.Code != Code.Ldfld) continue; var fieldRef = ldfld.Operand as IField; if (fieldRef == null) continue; var fieldDef = ourFields.Find(fieldRef); if (fieldDef == null) continue; if (instructions[index++].GetParameterIndex() != 0) continue; IMethod methodRef; var instr = instructions[index + 1]; if (instr.OpCode.Code == Code.Ldvirtftn) { if (!IsThisOrDup(instructions[index++])) continue; var ldvirtftn = instructions[index++]; methodRef = ldvirtftn.Operand as IMethod; } else { var ldftn = instructions[index++]; if (ldftn.OpCode.Code != Code.Ldftn) continue; methodRef = ldftn.Operand as IMethod; } if (methodRef == null) continue; var handlerMethod = ourMethods.Find(methodRef); if (handlerMethod == null) continue; var newobj = instructions[index++]; if (newobj.OpCode.Code != Code.Newobj) continue; if (!IsEventHandlerCtor(newobj.Operand as IMethod)) continue; var call = instructions[index++]; if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt) continue; var addHandler = call.Operand as IMethod; if (addHandler == null) continue; if (!Utils.StartsWith(addHandler.Name.String, "add_", StringComparison.Ordinal)) continue; var eventName = addHandler.Name.String.Substring(4); if (!checker.IsValidEventName(eventName)) continue; memberInfos.Method(handlerMethod).suggestedName = string.Format("{0}_{1}", memberInfos.Field(fieldDef).newName, eventName); } } }
void InitTypeEventHandlers(FieldDefAndDeclaringTypeDict<MFieldDef> ourFields, MethodDefAndDeclaringTypeDict<MMethodDef> ourMethods) { var checker = NameChecker; foreach (var methodDef in type.AllMethods) { if (methodDef.MethodDef.Body == null) continue; if (methodDef.MethodDef.IsStatic) continue; var method = methodDef.MethodDef; var instructions = method.Body.Instructions; for (int i = 0; i < instructions.Count - 5; i++) { // ldarg.0 // ldarg.0 / dup // ldarg.0 / dup // ldvirtftn handler // newobj event handler ctor // call add_Xyz if (instructions[i].GetParameterIndex() != 0) continue; int index = i + 1; if (!IsThisOrDup(instructions[index++])) continue; IMethod handler; if (instructions[index].OpCode.Code == Code.Ldftn) { handler = instructions[index++].Operand as IMethod; } else { if (!IsThisOrDup(instructions[index++])) continue; var instr = instructions[index++]; if (instr.OpCode.Code != Code.Ldvirtftn) continue; handler = instr.Operand as IMethod; } if (handler == null) continue; var handlerDef = ourMethods.Find(handler); if (handlerDef == null) continue; var newobj = instructions[index++]; if (newobj.OpCode.Code != Code.Newobj) continue; if (!IsEventHandlerCtor(newobj.Operand as IMethod)) continue; var call = instructions[index++]; if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt) continue; var addMethod = call.Operand as IMethod; if (addMethod == null) continue; if (!Utils.StartsWith(addMethod.Name.String, "add_", StringComparison.Ordinal)) continue; var eventName = addMethod.Name.String.Substring(4); if (!checker.IsValidEventName(eventName)) continue; memberInfos.Method(handlerDef).suggestedName = string.Format("{0}_{1}", newName, eventName); } } }
public void InitializeEventHandlerNames() { var ourFields = new FieldDefAndDeclaringTypeDict<MFieldDef>(); foreach (var fieldDef in type.AllFields) ourFields.Add(fieldDef.FieldDef, fieldDef); var ourMethods = new MethodDefAndDeclaringTypeDict<MMethodDef>(); foreach (var methodDef in type.AllMethods) ourMethods.Add(methodDef.MethodDef, methodDef); InitVbEventHandlers(ourFields, ourMethods); InitFieldEventHandlers(ourFields, ourMethods); InitTypeEventHandlers(ourFields, ourMethods); }
static short[] FindKey(MethodDef initMethod, FieldDef keyField) { var fields = new FieldDefAndDeclaringTypeDict<bool>(); fields.Add(keyField, true); return FindKey(initMethod, fields); }
void InitFieldEventHandlers(FieldDefAndDeclaringTypeDict <MFieldDef> ourFields, MethodDefAndDeclaringTypeDict <MMethodDef> ourMethods) { var checker = NameChecker; foreach (var methodDef in type.AllMethods) { if (methodDef.MethodDef.Body == null) { continue; } if (methodDef.MethodDef.IsStatic) { continue; } var instructions = methodDef.MethodDef.Body.Instructions; for (int i = 0; i < instructions.Count - 6; i++) { // We're looking for this code pattern: // ldarg.0 // ldfld field // ldarg.0 // ldftn method / ldarg.0 + ldvirtftn // newobj event_handler_ctor // callvirt add_SomeEvent if (instructions[i].GetParameterIndex() != 0) { continue; } int index = i + 1; var ldfld = instructions[index++]; if (ldfld.OpCode.Code != Code.Ldfld) { continue; } var fieldRef = ldfld.Operand as IField; if (fieldRef == null) { continue; } var fieldDef = ourFields.Find(fieldRef); if (fieldDef == null) { continue; } if (instructions[index++].GetParameterIndex() != 0) { continue; } IMethod methodRef; var instr = instructions[index + 1]; if (instr.OpCode.Code == Code.Ldvirtftn) { if (!IsThisOrDup(instructions[index++])) { continue; } var ldvirtftn = instructions[index++]; methodRef = ldvirtftn.Operand as IMethod; } else { var ldftn = instructions[index++]; if (ldftn.OpCode.Code != Code.Ldftn) { continue; } methodRef = ldftn.Operand as IMethod; } if (methodRef == null) { continue; } var handlerMethod = ourMethods.Find(methodRef); if (handlerMethod == null) { continue; } var newobj = instructions[index++]; if (newobj.OpCode.Code != Code.Newobj) { continue; } if (!IsEventHandlerCtor(newobj.Operand as IMethod)) { continue; } var call = instructions[index++]; if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt) { continue; } var addHandler = call.Operand as IMethod; if (addHandler == null) { continue; } if (!Utils.StartsWith(addHandler.Name.String, "add_", StringComparison.Ordinal)) { continue; } var eventName = addHandler.Name.String.Substring(4); if (!checker.IsValidEventName(eventName)) { continue; } memberInfos.Method(handlerMethod).suggestedName = $"{memberInfos.Field(fieldDef).newName}_{eventName}"; } } }
void InitializeWindowsFormsFieldsAndProps() { var checker = NameChecker; var ourFields = new FieldDefAndDeclaringTypeDict<MFieldDef>(); foreach (var fieldDef in type.AllFields) ourFields.Add(fieldDef.FieldDef, fieldDef); var ourMethods = new MethodDefAndDeclaringTypeDict<MMethodDef>(); foreach (var methodDef in type.AllMethods) ourMethods.Add(methodDef.MethodDef, methodDef); foreach (var methodDef in type.AllMethods) { if (methodDef.MethodDef.Body == null) continue; if (methodDef.MethodDef.IsStatic || methodDef.MethodDef.IsVirtual) continue; var instructions = methodDef.MethodDef.Body.Instructions; for (int i = 2; i < instructions.Count; i++) { var call = instructions[i]; if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt) continue; if (!IsWindowsFormsSetNameMethod(call.Operand as IMethod)) continue; var ldstr = instructions[i - 1]; if (ldstr.OpCode.Code != Code.Ldstr) continue; var fieldName = ldstr.Operand as string; if (fieldName == null || !checker.IsValidFieldName(fieldName)) continue; var instr = instructions[i - 2]; IField fieldRef = null; if (instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt) { var calledMethod = instr.Operand as IMethod; if (calledMethod == null) continue; var calledMethodDef = ourMethods.Find(calledMethod); if (calledMethodDef == null) continue; fieldRef = GetFieldRef(calledMethodDef.MethodDef); var propDef = calledMethodDef.Property; if (propDef == null) continue; memberInfos.Property(propDef).suggestedName = fieldName; fieldName = "_" + fieldName; } else if (instr.OpCode.Code == Code.Ldfld) { fieldRef = instr.Operand as IField; } if (fieldRef == null) continue; var fieldDef = ourFields.Find(fieldRef); if (fieldDef == null) continue; var fieldInfo = memberInfos.Field(fieldDef); if (fieldInfo.renamed) continue; fieldInfo.suggestedName = variableNameState.GetNewFieldName(fieldInfo.oldName, new NameCreator2(fieldName)); } } }
void InitializeWindowsFormsFieldsAndProps() { var checker = NameChecker; var ourFields = new FieldDefAndDeclaringTypeDict <MFieldDef>(); foreach (var fieldDef in type.AllFields) { ourFields.Add(fieldDef.FieldDef, fieldDef); } var ourMethods = new MethodDefAndDeclaringTypeDict <MMethodDef>(); foreach (var methodDef in type.AllMethods) { ourMethods.Add(methodDef.MethodDef, methodDef); } foreach (var methodDef in type.AllMethods) { if (methodDef.MethodDef.Body == null) { continue; } if (methodDef.MethodDef.IsStatic || methodDef.MethodDef.IsVirtual) { continue; } var instructions = methodDef.MethodDef.Body.Instructions; for (int i = 2; i < instructions.Count; i++) { var call = instructions[i]; if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt) { continue; } if (!IsWindowsFormsSetNameMethod(call.Operand as IMethod)) { continue; } var ldstr = instructions[i - 1]; if (ldstr.OpCode.Code != Code.Ldstr) { continue; } var fieldName = ldstr.Operand as string; if (fieldName == null || !checker.IsValidFieldName(fieldName)) { continue; } var instr = instructions[i - 2]; IField fieldRef = null; if (instr.OpCode.Code == Code.Call || instr.OpCode.Code == Code.Callvirt) { var calledMethod = instr.Operand as IMethod; if (calledMethod == null) { continue; } var calledMethodDef = ourMethods.Find(calledMethod); if (calledMethodDef == null) { continue; } fieldRef = GetFieldRef(calledMethodDef.MethodDef); var propDef = calledMethodDef.Property; if (propDef == null) { continue; } memberInfos.Property(propDef).suggestedName = fieldName; fieldName = "_" + fieldName; } else if (instr.OpCode.Code == Code.Ldfld) { fieldRef = instr.Operand as IField; } if (fieldRef == null) { continue; } var fieldDef = ourFields.Find(fieldRef); if (fieldDef == null) { continue; } var fieldInfo = memberInfos.Field(fieldDef); if (fieldInfo.renamed) { continue; } fieldInfo.suggestedName = variableNameState.GetNewFieldName(fieldInfo.oldName, new NameCreator2(fieldName)); } } }
FieldDefAndDeclaringTypeDict<DelegateInitInfo> CreateDelegateInitInfos_v10_r42915(MethodDef method) { var infos = new FieldDefAndDeclaringTypeDict<DelegateInitInfo>(); var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 2; i++) { var ldstr = instrs[i]; if (ldstr.OpCode.Code != Code.Ldstr) continue; var info = ldstr.Operand as string; if (info == null) continue; var ldtoken = instrs[i + 1]; if (ldtoken.OpCode.Code != Code.Ldtoken) continue; var delegateField = ldtoken.Operand as FieldDef; if (delegateField == null) continue; var delegateType = delegateField.FieldType.TryGetTypeDef(); if (!DotNetUtils.DerivesFromDelegate(delegateType)) continue; var call = instrs[i + 2]; if (call.OpCode.Code != Code.Call) continue; var delegateCreatorMethod = call.Operand as MethodDef; if (delegateCreatorMethod == null || !IsDelegateCreatorMethod(delegateCreatorMethod)) continue; infos.Add(delegateField, new DelegateInitInfo(info, delegateField, delegateCreatorMethod)); i += 2; } return infos; }
static FieldDefAndDeclaringTypeDict<bool> GetFields(MethodDef method) { var fields = new FieldDefAndDeclaringTypeDict<bool>(); foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Ldsfld && instr.OpCode.Code != Code.Stsfld) continue; var field = instr.Operand as FieldDef; if (field == null) continue; if (field.DeclaringType != method.DeclaringType) continue; fields.Add(field, true); } return fields; }
static FieldDefAndDeclaringTypeDict<List<MethodDef>> CreateFieldToMethodsDictionary(TypeDef type) { var dict = new FieldDefAndDeclaringTypeDict<List<MethodDef>>(); foreach (var method in type.Methods) { if (!method.IsStatic || method.Body == null || method.Name == ".cctor") continue; var delegateField = GetDelegateField(method); if (delegateField == null) continue; var methods = dict.Find(delegateField); if (methods == null) dict.Add(delegateField, methods = new List<MethodDef>()); methods.Add(method); } return dict; }
// VB initializes the handlers in the property setter, where it first removes the handler // from the previous control, and then adds the handler to the new control. void InitVbEventHandlers(FieldDefAndDeclaringTypeDict<MFieldDef> ourFields, MethodDefAndDeclaringTypeDict<MMethodDef> ourMethods) { var checker = NameChecker; foreach (var propDef in type.AllProperties) { var setterDef = propDef.SetMethod; if (setterDef == null) continue; string eventName; var handler = GetVbHandler(setterDef.MethodDef, out eventName); if (handler == null) continue; var handlerDef = ourMethods.Find(handler); if (handlerDef == null) continue; if (!checker.IsValidEventName(eventName)) continue; memberInfos.Method(handlerDef).suggestedName = string.Format("{0}_{1}", memberInfos.Property(propDef).newName, eventName); } }
void InitTypeEventHandlers(FieldDefAndDeclaringTypeDict <MFieldDef> ourFields, MethodDefAndDeclaringTypeDict <MMethodDef> ourMethods) { var checker = NameChecker; foreach (var methodDef in type.AllMethods) { if (methodDef.MethodDef.Body == null) { continue; } if (methodDef.MethodDef.IsStatic) { continue; } var method = methodDef.MethodDef; var instructions = method.Body.Instructions; for (int i = 0; i < instructions.Count - 5; i++) { // ldarg.0 // ldarg.0 / dup // ldarg.0 / dup // ldvirtftn handler // newobj event handler ctor // call add_Xyz if (instructions[i].GetParameterIndex() != 0) { continue; } int index = i + 1; if (!IsThisOrDup(instructions[index++])) { continue; } IMethod handler; if (instructions[index].OpCode.Code == Code.Ldftn) { handler = instructions[index++].Operand as IMethod; } else { if (!IsThisOrDup(instructions[index++])) { continue; } var instr = instructions[index++]; if (instr.OpCode.Code != Code.Ldvirtftn) { continue; } handler = instr.Operand as IMethod; } if (handler == null) { continue; } var handlerDef = ourMethods.Find(handler); if (handlerDef == null) { continue; } var newobj = instructions[index++]; if (newobj.OpCode.Code != Code.Newobj) { continue; } if (!IsEventHandlerCtor(newobj.Operand as IMethod)) { continue; } var call = instructions[index++]; if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt) { continue; } var addMethod = call.Operand as IMethod; if (addMethod == null) { continue; } if (!Utils.StartsWith(addMethod.Name.String, "add_", StringComparison.Ordinal)) { continue; } var eventName = addMethod.Name.String.Substring(4); if (!checker.IsValidEventName(eventName)) { continue; } memberInfos.Method(handlerDef).suggestedName = $"{newName}_{eventName}"; } } }
public bool Initialize() { if (!FindMagic(Method, out arg1, out arg2, out magic)) return false; fields = GetFields(Method); if (fields == null) return false; arrayInfo = GetArrayInfo(cctor); if (arrayInfo == null) return false; if (arrayInfo.initField.InitialValue.Length % 2 == 1) return false; encryptedData = new ushort[arrayInfo.initField.InitialValue.Length / 2]; Buffer.BlockCopy(arrayInfo.initField.InitialValue, 0, encryptedData, 0, arrayInfo.initField.InitialValue.Length); decryptType = GetDecryptType(Method); keyShift = FindKeyShift(cctor); key = FindKey(); if (key == null || key.Length == 0) return false; return true; }