示例#1
0
        private void DoTest(MethodDefinition method, params string [] userNames)
        {
            int count = 0;

            foreach (Instruction ins in method.Body.Instructions)
            {
                VariableDefinition v = ins.GetVariable(method);
                if (v != null)
                {
                    bool userName = userNames.Any(n => n == v.ToString());
                    if (userName)
                    {
                        Assert.IsFalse(v.IsGeneratedName(method.DebugInformation), "{0} was reported as a generated name", v.ToString());
                    }
                    else
                    {
                        ++count;
                        Assert.IsTrue(v.IsGeneratedName(method.DebugInformation), "{0} was not reported as a generated name", v.ToString());
                    }
                }
            }

            if (count == 0)
            {
                Assert.Fail("Didn't find any generated locals for VariableDefinitionRocksTest::{0}", method.Name);
            }
        }
示例#2
0
        public void InitForm(MethodDefinition md, int varIndex, EditModes mode)
        {
            if (_method == md && _varIndex == varIndex)
            {
                return;
            }

            _method   = md;
            _varIndex = varIndex;
            _mode     = mode;

            Collection <VariableDefinition> vdc = _method.Body.Variables;

            VariableDefinition vd = null;

            InitTypeSpecification();

            if (_varIndex >= 0 && _varIndex < vdc.Count)
            {
                vd = vdc[_varIndex];
                cboType.Items.Add(vd.VariableType);

                if (vd.VariableType.IsArray)
                {
                    cboSpecification.SelectedItem = "Array";
                }
                else if (vd.VariableType.IsByReference)
                {
                    cboSpecification.SelectedItem = "Reference";
                }
                else if (vd.VariableType.IsPointer)
                {
                    cboSpecification.SelectedItem = "Pointer";
                }
            }

            switch (mode)
            {
            case EditModes.Edit:
                if (vd != null)
                {
                    txtIndex.Text        = vd.Index.ToString();
                    txtName.Text         = vd.ToString();
                    cboType.SelectedItem = vd.VariableType;
                }
                break;

            case EditModes.Duplicate:
                if (vd != null)
                {
                    cboType.SelectedItem = vd.VariableType;
                }
                break;

            default:
                break;
            }
        }
示例#3
0
        string GetVariableName(VariableDefinition variable, MethodBody body)
        {
            if (body.Method.DebugInformation.TryGetName(variable, out var name))
            {
                return(name);
            }

            return(variable.ToString());
        }
示例#4
0
        /// <summary>
        /// Adds new variable to the given MethodBody.
        /// </summary>
        /// <param name="type">
        /// A <see cref="TypeReference"/>
        /// </param>
        /// <param name="method">
        /// A <see cref="MethodDefinition"/>
        /// </param>
        /// <returns>
        /// A <see cref="VariableDefinition"/>
        /// </returns>
        internal VariableDefinition RegisterVariable(TypeReference type, MethodDefinition method)
        {
            VariableDefinition variable = new VariableDefinition(type);

            //Mono.Cecil 0.9.3 migration: variable.Method = method;
            //variable.Method = method;
            //variable.Index = method.Body.Variables.Count;
            variable.Name = variable.ToString();
            method.Body.Variables.Add(variable);

            return(variable);
        }
示例#5
0
        static string GetVariableName(VariableDefinition variable, MethodBody body)
        {
            foreach (var scope in body.Method.DebugInformation.GetScopes())
            {
                foreach (var variable_symbol in scope.Variables)
                {
                    if (variable_symbol.Index == variable.Index)
                    {
                        return(variable_symbol.Name);
                    }
                }
            }

            return(variable.ToString());
        }
示例#6
0
        private static bool AnalyzeMethodLocalFieldSets(MethodDefinition methodDefinition, XElement entity, XElement newMethodNode)
        {
            bool result = false;

            Mono.Cecil.Cil.MethodBody body = methodDefinition.Body;
            if (null != body)
            {
                foreach (Instruction itemInstruction in body.Instructions)
                {
                    if ((itemInstruction.OpCode.Name.StartsWith("stloc")) && (!itemInstruction.OpCode.Name.StartsWith("stloc.s")))
                    {
                        Mono.Cecil.Cil.Instruction methodInstruction  = itemInstruction as Mono.Cecil.Cil.Instruction;
                        VariableDefinition         variableDefinition = GetLocalVariableDefinition(methodDefinition, itemInstruction);
                        if ((null != variableDefinition) && (variableDefinition.VariableType.IsValueType))
                        {
                            Mono.Cecil.Cil.Instruction paramInstruction = GetParameterInstructionForField(methodInstruction);
                            if (null != paramInstruction)
                            {
                                bool sucseed = false;
                                int  opValue = GetOperatorValue(paramInstruction, out sucseed);
                                if (sucseed)
                                {
                                    string[] supportByLibrary = _netOfficeSupportTable.GetEnumMemberSupport(variableDefinition.VariableType.FullName, opValue);
                                    if (null != supportByLibrary)
                                    {
                                        XElement newParameter  = new XElement("Field", new XAttribute("Name", variableDefinition.ToString()));
                                        string   componentName = NetOfficeSupportTable.GetLibrary(variableDefinition.VariableType.FullName);
                                        XElement supportByNode = new XElement("SupportByLibrary", new XAttribute("Api", componentName));
                                        string   memberName    = _netOfficeSupportTable.GetEnumMemberNameFromValue(variableDefinition.VariableType.FullName, opValue);
                                        supportByNode.Add(new XAttribute("Name", variableDefinition.VariableType.FullName + "." + memberName));
                                        foreach (string item in supportByLibrary)
                                        {
                                            supportByNode.Add(new XElement("Version", item));
                                        }
                                        newParameter.Add(supportByNode);
                                        newMethodNode.Element("LocalFieldSets").Add(newParameter);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }
示例#7
0
        /// <summary>
        /// Edits the NormalUpdate method in Player (handling the player state when not doing anything like climbing etc.)
        /// to handle the X speed part.
        /// </summary>
        /// <param name="il">Object allowing CIL patching</param>
        private void modNormalUpdate(ILContext il)
        {
            ILCursor cursor = new ILCursor(il);

            // we use 90 as an anchor (an "if" before the instruction we want to mod loads 90 in the stack)
            // then we jump to the next usage of V_6 to get the reference to it (no idea how to build it otherwise)
            // (actually, this is V_31 in the FNA version)
            if (cursor.TryGotoNext(MoveType.After, instr => instr.MatchLdcR4(90f)) &&
                cursor.TryGotoNext(MoveType.Before, instr => instr.OpCode == OpCodes.Stloc_S &&
                                   (((VariableDefinition)instr.Operand).Index == 6 || ((VariableDefinition)instr.Operand).Index == 31)))
            {
                VariableDefinition variable = (VariableDefinition)cursor.Next.Operand;

                // we jump before the next ldflda, which is between the "if (this.level.InSpace)" and the next one
                if (cursor.TryGotoNext(MoveType.Before, instr => instr.OpCode == OpCodes.Ldflda))
                {
                    Logger.Log("ExtendedVariantMode/SpeedX", $"Applying X speed modding to variable {variable.ToString()} at {cursor.Index} in CIL code for NormalUpdate");

                    // pop ldarg.0
                    cursor.Emit(OpCodes.Pop);

                    // modify variable 6 to apply X factor
                    cursor.Emit(OpCodes.Ldloc_S, variable);
                    cursor.EmitDelegate <Func <float> >(determineSpeedXFactor);
                    cursor.Emit(OpCodes.Mul);
                    cursor.Emit(OpCodes.Stloc_S, variable);

                    // execute ldarg.0 again
                    cursor.Emit(OpCodes.Ldarg_0);
                }
            }
        }