Exemplo n.º 1
0
 /// <summary>
 /// loads a field of the given type or its address (ldfld/ldflda and their variants).
 /// </summary>
 /// <param name="type">type of variable being loaded</param>
 /// <param name="method">method containing the local variable</param>
 public static bool IsLdLoc(this CodeInstruction code, Type type, MethodBase method)
 {
     if (!code.IsLdloc())
     {
         return(false);
     }
     if (code.opcode == OpCodes.Ldloc_0)
     {
         return(method.GetMethodBody().LocalVariables[0].LocalType == type);
     }
     else if (code.opcode == OpCodes.Ldloc_1)
     {
         return(method.GetMethodBody().LocalVariables[1].LocalType == type);
     }
     else if (code.opcode == OpCodes.Ldloc_2)
     {
         return(method.GetMethodBody().LocalVariables[2].LocalType == type);
     }
     else if (code.opcode == OpCodes.Ldloc_3)
     {
         return(method.GetMethodBody().LocalVariables[3].LocalType == type);
     }
     else
     {
         return(code.operand is LocalBuilder lb && lb.LocalType == type);
     }
 }
Exemplo n.º 2
0
 public static bool IsLdloc(this CodeInstruction instruction, LocalVar local)
 {
     if (!instruction.IsLdloc(local.builder))
     {
         return(false);
     }
     if (local.builder != null)
     {
         return(true);
     }
     return(LocalVar.OpCodeToInt[instruction.opcode] == local.index);
 }
Exemplo n.º 3
0
        public LocalVar(MethodInfo m, CodeInstruction instruction)
        {
            method = m;
            if (!instruction.IsLdloc() && !instruction.IsStloc())
            {
                throw new Exception("Code instruction is not a Ldloc or Stloc");
            }
            var t = instruction.ToLocalVar();

            index   = t.index;
            builder = t.builder;
        }
Exemplo n.º 4
0
 public static LocalVar ToLocalVar(this CodeInstruction instruction, MethodInfo method = null)
 {
     if (!instruction.IsLdloc() && !instruction.IsStloc())
     {
         return(null);
     }
     if (instruction.operand is LocalBuilder builder)
     {
         return(new LocalVar(builder));
     }
     return(new LocalVar(LocalVar.OpCodeToInt[instruction.opcode])
     {
         method = method
     });
 }
Exemplo n.º 5
0
        public virtual bool SearchMethod(MethodInfo searchMethod, out List <ItemPos <StatDef> > Results, out List <CodeInstruction> PawnIns)
        {
            Results = new List <ItemPos <StatDef> >();
            PawnIns = new List <CodeInstruction>();
            bool foundResult = false;
            bool foundPawn   = false;
            List <CodeInstruction> instructionList;

            try { instructionList = PatchProcessor.GetCurrentInstructions(searchMethod); }
            catch { instructionList = PatchProcessor.GetOriginalInstructions(searchMethod); }
            List <int?>         ParamList = searchMethod.GetParameters()?.Where(t => t.ParameterType.IsAssignableFrom(typeof(Pawn)))?.Select(t => new int?(t.Position))?.ToList() ?? new List <int?>();
            List <LocalBuilder> LocalList = new List <LocalBuilder>();

            if (!ParamList.NullOrEmpty())
            {
                foundPawn = true;
            }
            for (int i = 0; i < instructionList.Count; i++)
            {
                CodeInstruction instruction = instructionList[i];
                if (instruction.opcode == OpCodes.Ldfld && instruction.operand is FieldInfo pawnField &&
                    pawnField.FieldType.IsAssignableFrom(typeof(Pawn)))
                {
                    foundPawn = true;
                    for (int j = 1; j < i; j++)
                    {
                        if (instructionList[i - j].IsLdarg(0))
                        {
                            PawnIns = new List <CodeInstruction>(instructionList.GetRange(i - j, j + 1));
                            break;
                        }
                    }
                }
                if (ParamList.Any(t => instruction.IsLdarg(t)))
                {
                    PawnIns = new List <CodeInstruction>()
                    {
                        instruction
                    }
                }
                ;
                if (instruction.IsStloc() && instruction.operand is LocalBuilder local && !LocalList.Contains(local) && local.LocalType.IsAssignableFrom(typeof(Pawn)))
                {
                    LocalList.Add(local);
                }
                if (LocalList.Any(t => instruction.IsLdloc(t)))
                {
                    PawnIns = new List <CodeInstruction>()
                    {
                        instruction
                    }
                }
                ;
                // Ambiguous method call
                if ((instruction.opcode == OpCodes.Call || instruction.opcode == OpCodes.Callvirt) &&
                    instruction.operand is MethodInfo calledMethod)
                {
                    if (calledMethod.ReturnType == typeof(StatDef))
                    {
                        foundResult = true;
                        Results.Add((ItemPos <StatDef>)(i, null));
                    }
                    else if (calledMethod.ReturnType.IsAssignableFrom(typeof(Pawn)))
                    {
                        foundPawn = true;
                        if (calledMethod.IsStatic)
                        {
                            PawnIns = new List <CodeInstruction>()
                            {
                                instruction
                            }
                        }
                        ;
                        else
                        {
                            for (int j = 1; j < i; j++)
                            {
                                if (instructionList[i - j].IsLdarg(0))
                                {
                                    PawnIns = new List <CodeInstruction>(instructionList.GetRange(i - j, j + 1));
                                    break;
                                }
                            }
                        }
                    }
                }
                // StatDefOf call
                if (instruction.opcode == OpCodes.Ldsfld && instruction.operand is FieldInfo statField &&
                    statField.FieldType == typeof(StatDef) && StatDefOfFieldInfo.Contains(statField))
                {
                    Results.Add((ItemPos <StatDef>)(i, FieldToStat[statField]));
                    foundResult = true;
                }
                if (!FindAll && foundResult && (!findPawn || foundPawn))
                {
                    return(foundResult);
                }
            }
            return(foundResult);
        }
    }
}
Exemplo n.º 6
0
 public bool IsLdlocOrLdloca(CodeInstruction instruction)
 {
     return(instruction.IsLdloc());
 }