示例#1
0
        public static IEnumerable <CodeInstruction> Transpiler(MethodBase original, IEnumerable <CodeInstruction> instructions)
        {
            foreach (CodeInstruction instruction in instructions)
            {
                yield return(instruction);

                /* if (this.crafterLogic.Craft(currentStageTech, craftTime)) {
                 *      GameObject toBuild = this.rocket.StartRocketConstruction();
                 *  ->  RocketConstructor_StartRocketConstruction_Patch.Callback(this.rocket, currentStageTech);
                 *      ItemGoalTracker.OnConstruct(currentStageTech);
                 *      this.SendBuildBots(toBuild);
                 * }
                 */
                if (instruction.opcode == INJECTION_CODE)
                {
                    yield return(new CodeInstruction(OpCodes.Ldarg_0));                                                 //this

                    yield return(new CodeInstruction(OpCodes.Ldfld, Reflect.Field((RocketConstructor t) => t.rocket))); // this.rocket

                    yield return(new CodeInstruction(OpCodes.Ldloc_0));                                                 // techtype

                    yield return(new CodeInstruction(OpCodes.Call, Reflect.Method(() => Callback(default(Rocket), default(TechType)))));
                }
            }
        }
示例#2
0
        /* There is a bug, where Subroot.live is not loaded when starting in a cyclops. Therefore this codepiece needs to check that and jump accordingly if not present
         *
         * For this change
         *
         * this.live.invincible = false
         *
         * to
         *
         * if (this.live != null)
         * {
         *  this.live.invincible = false
         * }
         */
        public static IEnumerable <CodeInstruction> Transpiler(MethodBase original, IEnumerable <CodeInstruction> instructions, ILGenerator generator)
        {
            List <CodeInstruction> instructionList = instructions.ToList();

            int   injectionPoint = 0;
            Label newJumpPoint   = generator.DefineLabel();

            for (int i = 3; i < instructionList.Count; i++)
            {
                if (instructionList[i].opcode == START_INJECTION_CODE_INVINCIBLE &&
                    Equals(instructionList[i].operand, LIVEMIXIN_INVINCIBLE))
                {
                    if (instructionList[i - 3].opcode == START_INJECTION_CODE)
                    {
                        instructionList[i + 1].labels.Add(newJumpPoint);
                        injectionPoint = i - 3;
                    }
                }
            }
            if (injectionPoint != 0)
            {
                MethodInfo             op_inequality_method = typeof(UnityEngine.Object).GetMethod("op_Inequality");
                List <CodeInstruction> injectedInstructions = new()
                {
                    new CodeInstruction(OpCodes.Ldarg_0),
                    new CodeInstruction(OpCodes.Ldfld, Reflect.Field((SubRoot t) => t.live)),
                    new CodeInstruction(OpCodes.Ldnull),
                    new CodeInstruction(OpCodes.Call, op_inequality_method),
                    new CodeInstruction(OpCodes.Brfalse, newJumpPoint)
                };
                instructionList.InsertRange(injectionPoint, injectedInstructions);
            }
            return(instructionList);
        }
示例#3
0
        public static IEnumerable <CodeInstruction> Transpiler(MethodBase original, IEnumerable <CodeInstruction> instructions)
        {
            Validate.NotNull(PLACE_BASE_INJECTION_OPERAND);
            Validate.NotNull(PLACE_FURNITURE_INJECTION_OPERAND);

            foreach (CodeInstruction instruction in instructions)
            {
                yield return(instruction);

                if (instruction.opcode.Equals(PLACE_BASE_INJECTION_OPCODE) && instruction.operand.Equals(PLACE_BASE_INJECTION_OPERAND))
                {
                    /*
                     *  Multiplayer.Logic.Building.PlaceBasePiece(componentInParent, component.TargetBase, CraftData.GetTechType(Builder.prefab), Builder.placeRotation);
                     */
                    yield return(TranspilerHelper.LocateService <Building>());

                    yield return(new CodeInstruction(OpCodes.Ldloc_1));

                    yield return(new CodeInstruction(OpCodes.Ldloc_0));

                    yield return(new CodeInstruction(OpCodes.Ldloc_1));

                    yield return(new CodeInstruction(OpCodes.Callvirt, Reflect.Property((BaseGhost t) => t.TargetBase).GetMethod));

                    yield return(new CodeInstruction(OpCodes.Ldsfld, Reflect.Field(() => Builder.prefab)));

                    yield return(new CodeInstruction(OpCodes.Call, Reflect.Method(() => CraftData.GetTechType(default(GameObject)))));

                    yield return(new CodeInstruction(OpCodes.Ldsfld, Reflect.Field(() => Builder.placeRotation)));

                    yield return(new CodeInstruction(OpCodes.Callvirt, Reflect.Method((Building t) => t.PlaceBasePiece(default(BaseGhost), default(ConstructableBase), default(Base), default(TechType), default(Quaternion)))));
                }

                if (instruction.opcode.Equals(PLACE_FURNITURE_INJECTION_OPCODE) && instruction.operand.Equals(PLACE_FURNITURE_INJECTION_OPERAND))
                {
                    /*
                     *  Multiplayer.Logic.Building.PlaceFurniture(gameObject, CraftData.GetTechType(Builder.prefab), Builder.ghostModel.transform.position, Builder.placeRotation);
                     */
                    yield return(TranspilerHelper.LocateService <Building>());

                    yield return(new CodeInstruction(OpCodes.Ldloc_2));

                    yield return(new CodeInstruction(OpCodes.Ldsfld, Reflect.Field(() => Builder.prefab)));

                    yield return(new CodeInstruction(OpCodes.Call, Reflect.Method(() => CraftData.GetTechType(default(GameObject)))));

                    yield return(new CodeInstruction(OpCodes.Ldsfld, Reflect.Field(() => Builder.ghostModel)));

                    yield return(new CodeInstruction(OpCodes.Callvirt, Reflect.Property((GameObject t) => t.transform).GetMethod));

                    yield return(new CodeInstruction(OpCodes.Callvirt, Reflect.Property((Transform t) => t.position).GetMethod));

                    yield return(new CodeInstruction(OpCodes.Ldsfld, Reflect.Field(() => Builder.placeRotation)));

                    yield return(new CodeInstruction(OpCodes.Callvirt, Reflect.Method((Building t) => t.PlaceFurniture(default(GameObject), default(TechType), default(Vector3), default(Quaternion)))));
                }
            }
        }
示例#4
0
        public void TestValueTypeField()
        {
            FieldInfo member;

            member = Reflect.Field <StubType>(x => x.ValueTypeField);

            Assert.IsNotNull(member, "Member nullability");
            Assert.AreEqual("ValueTypeField", member.Name, "Member name");
        }
        public void GetValue_gets_field_value(ClassWithAField testClass, int fieldValue)
        {
            var field = Reflect.Field <ClassWithAField>(x => x.MyField);
            var sut   = new ReflectionFieldValueProvider <ClassWithAField, int>(field);

            testClass.MyField = fieldValue;

            var result = sut.GetValue(testClass);

            Assert.That(result, Is.EqualTo(fieldValue));
        }
示例#6
0
        public void Field()
        {
            // Get static field.
            FieldInfo staticField = Reflect.Field(() => AbusedClass.StaticField);

            staticField.Name.Should().BeEquivalentTo(nameof(AbusedClass.StaticField));
            staticField.FieldType.Should().Be <int>();
            // Get instance field.
            FieldInfo instanceField = Reflect.Field((AbusedClass t) => t.InstanceField);

            instanceField.Name.Should().BeEquivalentTo(nameof(AbusedClass.InstanceField));
            instanceField.FieldType.Should().Be <int>();
        }
        private static IEnumerable <CodeInstruction> AssembleNewCode(Label outJumpLabel, Label innerJumpLabel)
        {
            //Code to inject:

            /*
             * if (Player.main.currentSub != this.subRoot)
             * {
             *  return;
             * }
             *
             */
            List <CodeInstruction> injectInstructions = new();

            CodeInstruction instruction = new(OpCodes.Ldsfld);

            instruction.operand = Reflect.Field(() => Player.main);
            instruction.labels.Add(innerJumpLabel);
            injectInstructions.Add(instruction);

            instruction         = new CodeInstruction(OpCodes.Callvirt);
            instruction.operand = Reflect.Property((Player t) => t.currentSub).GetMethod;
            injectInstructions.Add(instruction);

            instruction = new CodeInstruction(OpCodes.Ldarg_0);
            injectInstructions.Add(instruction);

            instruction         = new CodeInstruction(OpCodes.Ldfld);
            instruction.operand = Reflect.Field((CyclopsSonarButton t) => t.subRoot);
            injectInstructions.Add(instruction);

            instruction = new CodeInstruction(OpCodes.Call);
            // Reflect utility class does not supported getting operator-overload methods.
            instruction.operand = typeof(UnityEngine.Object).GetMethod("op_Inequality", BindingFlags.Public | BindingFlags.Static);
            injectInstructions.Add(instruction);

            instruction         = new CodeInstruction(OpCodes.Brfalse);
            instruction.operand = outJumpLabel;
            injectInstructions.Add(instruction);

            instruction = new CodeInstruction(OpCodes.Ret);
            injectInstructions.Add(instruction);

            return(injectInstructions);
        }
示例#8
0
        /// <summary>
        /// Adds an equality rule for a specific field.
        /// </summary>
        /// <returns>The equality builder.</returns>
        /// <param name="builder">The equality builder.</param>
        /// <param name="field">An expression which identifies the field to be tested.</param>
        /// <param name="config">Configuration for how to test the field for equality.</param>
        /// <param name="name">An optional name for the equality rule.</param>
        /// <typeparam name="TParent">The object type being tested for equality.</typeparam>
        /// <typeparam name="TField">The type of the field.</typeparam>
        public static EqualityBuilder <TParent> ForField <TParent, TField>(this EqualityBuilder <TParent> builder,
                                                                           Expression <Func <TParent, TField> > field,
                                                                           Action <IBuildsComparerFactory <TField> > config = null,
                                                                           string name = null)
        {
            var comparerBuilder = builder.GetComparerFactoryBuilder <TParent, TField>();

            config?.Invoke(comparerBuilder);
            var ruleBuilder = new DelegateFieldRuleBuilder <TParent, TField>(Reflect.Field(field),
                                                                             field.Compile(),
                                                                             comparerBuilder.Comparer);

            if (name != null)
            {
                ruleBuilder.Name = name;
            }
            var ruleProvider = builder.AsRuleBuilderProvider().RuleBuilders.Add(ruleBuilder);

            return(builder);
        }