Esempio n. 1
0
        private Instruction[] ParseInstructions(XmlNode opList, ILProcessor proc)
        {
            List <Instruction>     ret = new List <Instruction>();
            List <QueuedGenerator> queuedGenerators = new List <QueuedGenerator>();

            foreach (XmlNode opNode in opList.ChildNodes)
            {
                if (opNode is XmlComment)
                {
                    continue;
                }
                //if (opNode.Attributes["name"] == null)
                //	continue;
                IInstructionGenerator instGen = ParseOpcode(opNode);
                Instruction           inst    = instGen.Generate(proc, ret);
                if (inst == null)
                {
                    inst = proc.Create(OpCodes.Nop);
                    queuedGenerators.Add(new QueuedGenerator(instGen, ret.Count));
                }
                ret.Add(inst);
            }
            foreach (QueuedGenerator curGen in queuedGenerators)
            {
                Instruction newInstr = curGen.generator.Generate(proc, ret);
                if (newInstr == null)
                {
                    throw new ArgumentOutOfRangeException("xmlpatchers.xml : Instruction reference out of range!");
                }
                Instruction prevInstr = ret[curGen.index];
                prevInstr.OpCode  = newInstr.OpCode;
                prevInstr.Operand = newInstr.Operand;
            }
            return(ret.ToArray());
        }
Esempio n. 2
0
        private bool Matches(MethodDefinition method, XmlNode opList)
        {
            ILProcessor        proc   = method.Body.GetILProcessor();
            List <Instruction> instrs = new List <Instruction>(method.Body.Instructions);
            int i = 0;

            foreach (XmlNode opNode in opList.ChildNodes)
            {
                if (opNode is XmlComment)
                {
                    continue;
                }
                int          index;
                XmlAttribute indexAttribute = opNode.Attributes["index"];
                if (indexAttribute == null)
                {
                    index = i++;
                }
                else
                {
                    index = Int32.Parse(indexAttribute.Value);
                    if (index < 0)
                    {
                        index += instrs.Count;
                    }
                    i = index + 1;
                }
                if (index >= instrs.Count || index < 0)
                {
                    return(false);
                }
                //if (opNode.Attributes["name"] == null)
                //	continue;
                IInstructionGenerator instGen = ParseOpcode(opNode);
                if (instGen.GetOpCode() != instrs[index].OpCode)
                {
                    return(false);
                }
                if (instGen.HasOperand() != (instrs[index].Operand != null))
                {
                    continue;
                }
                Instruction inst = instGen.Generate(proc, instrs);
                if (inst == null)
                {
                    return(false);
                }

                object methodOp     = instrs[index].Operand;
                object compareOp    = inst.Operand;
                Type   methodOpType = instrs[index].Operand.GetType();
                if (methodOpType != inst.Operand.GetType())
                {
                    return(false);
                }
                switch (methodOpType.Name)
                {
                case "Byte":
                    if ((Byte)methodOp != (Byte)compareOp)
                    {
                        return(false);
                    }
                    break;

                case "SByte":
                    if ((SByte)methodOp != (SByte)compareOp)
                    {
                        return(false);
                    }
                    break;

                case "Int32":
                    if ((Int32)methodOp != (Int32)compareOp)
                    {
                        return(false);
                    }
                    break;

                case "Int64":
                    if ((Int64)methodOp != (Int64)compareOp)
                    {
                        return(false);
                    }
                    break;

                case "Single":
                    if ((Single)methodOp != (Single)compareOp)
                    {
                        return(false);
                    }
                    break;

                case "Double":
                    if ((Double)methodOp != (Double)compareOp)
                    {
                        return(false);
                    }
                    break;

                case "String":
                    if (!methodOp.Equals(compareOp))
                    {
                        return(false);
                    }
                    break;

                case "Instruction":
                    if (methodOp != compareOp)
                    {
                        return(false);
                    }
                    break;

                case "TypeReference":
                    if (!((TypeReference)methodOp).FullName.Equals(((TypeReference)compareOp).FullName))
                    {
                        return(false);
                    }
                    break;

                case "FieldReference":
                    if (!((FieldReference)methodOp).FullName.Equals(((FieldReference)compareOp).FullName))
                    {
                        return(false);
                    }
                    break;

                case "MethodReference":
                    if (!((MethodReference)methodOp).ToString().Equals(((MethodReference)compareOp).ToString()))                             //ToString includes the method
                    {
                        return(false);
                    }
                    break;

                default:
                    logger.Warning("XmlPatches : Nobody told me how to compare two " + methodOpType.Name + "s!");
                    break;
                }
            }
            return(true);
        }