Beispiel #1
0
 private void InitializeOffsets()
 {
     if (OffsetsInitialized)
     {
         return;
     }
     foreach (var t in Assembly.GetExecutingAssembly().DefinedTypes)
     {
         if (!t.Namespace.StartsWith("QTRHacker.Functions.GameObjects"))
         {
             continue;
         }
         var typeNameAttr = t.GetCustomAttributes(typeof(GameFieldOffsetTypeNameAttribute), false);
         foreach (var f in t.GetFields(BindingFlags.Static | BindingFlags.Public))
         {
             if (!f.IsStatic ||
                 !(f.FieldType == typeof(int) || f.FieldType == typeof(uint) ||
                   f.FieldType == typeof(long) || f.FieldType == typeof(ulong)))
             {
                 continue;
             }
             var fieldNameAttr = f.GetCustomAttributes(typeof(GameFieldOffsetFieldNameAttribute), false);
             if (fieldNameAttr.Length == 0)
             {
                 continue;
             }
             GameFieldOffsetFieldNameAttribute gfofna = fieldNameAttr[0] as GameFieldOffsetFieldNameAttribute;
             if (gfofna.TypeName == null && typeNameAttr.Length == 0)                    //信息不足
             {
                 continue;
             }
             Microsoft.Diagnostics.Runtime.ClrType clrType = null;
             if (gfofna.TypeName != null)
             {
                 clrType = HContext.MainAddressHelper.Module.GetTypeByName(gfofna.TypeName);
             }
             else
             {
                 clrType = HContext.MainAddressHelper.Module.GetTypeByName((typeNameAttr[0] as GameFieldOffsetTypeNameAttribute).TypeName);
             }
             var field = clrType.GetFieldByName(gfofna.FieldName);
             if (field == null)
             {
                 GameContextExceptionHandler(new FieldNotFoundException("Field named " + gfofna.FieldName + " not found.(" + f.DeclaringType.FullName + "." + f.Name + ")"));
             }
             else
             {
                 f.SetValue(null, field.Offset + 4);                        //需要+4,原因是Offset比真实的"偏移"要少了一个指针的位置
             }
         }
     }
 }