Beispiel #1
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);
        }
    }
}