internal static HumanAIConnection GetConnection()
        {
            try {
                StartPathFindDelegate startPathFindCitizenAI =
                    TranspilerUtil.CreateDelegate <StartPathFindDelegate>(
                        typeof(CitizenAI),
                        "StartPathFind",
                        true);
                SimulationStepDelegate simulationStepCitizenAI =
                    AccessTools.MethodDelegate <SimulationStepDelegate>(
                        TranspilerUtil.DeclaredMethod <SimulationStepTarget>(typeof(CitizenAI), "SimulationStep"),
                        null,
                        false);
                ArriveAtDestinationDelegate arriveAtDestination =
                    TranspilerUtil.CreateDelegate <ArriveAtDestinationDelegate>(
                        typeof(HumanAI),
                        "ArriveAtDestination",
                        true);
                SpawnDelegate spawnCitizenAI =
                    TranspilerUtil.CreateDelegate <SpawnDelegate>(
                        typeof(HumanAI),
                        "Spawn",
                        true);
                InvalidPathHumanAIDelegate invalidPath =
                    TranspilerUtil.CreateDelegate <InvalidPathHumanAIDelegate>(
                        typeof(CitizenAI),
                        "InvalidPath",
                        true);
                PathfindFailureHumanAIDelegate pathfindFailure =
                    TranspilerUtil.CreateDelegate <PathfindFailureHumanAIDelegate>(
                        typeof(HumanAI),
                        "PathfindFailure",
                        true);
                PathfindSuccessHumanAIDelegate pathfindSuccess =
                    TranspilerUtil.CreateDelegate <PathfindSuccessHumanAIDelegate>(
                        typeof(HumanAI),
                        "PathfindSuccess",
                        true);

                return(new HumanAIConnection(
                           spawnCitizenAI,
                           startPathFindCitizenAI,
                           simulationStepCitizenAI,
                           arriveAtDestination,
                           invalidPath,
                           pathfindFailure,
                           pathfindSuccess));
            } catch (Exception e) {
                Log.Error(e.Message);
                return(null);
            }
        }
        public static bool ApplyPatch(Harmony harmonyInstance, Type simulationStepPatch1Type)
        {
            try {
                var original = TranspilerUtil.DeclaredMethod <TargetDelegate>(simulationStepPatch1Type, "Prefix");
                if (original == null)
                {
                    return(false);
                }

                var transpiler = typeof(RTramAIModPatch).GetMethod("TranspileRTramSimulationStepPatch1");
                harmonyInstance.Patch(original, transpiler: new HarmonyMethod(transpiler));
                return(true);
            }
            catch (Exception ex) {
                ex.LogException();
                return(false);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Revrites instructions adding necessary TMPE calls
        /// </summary>
        /// <param name="il"> Il Generator</param>
        /// <param name="instructions">List of instructions</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static IEnumerable <CodeInstruction> TranspileTramTrainSimulationStep(ILGenerator il, IEnumerable <CodeInstruction> instructions)
        {
            List <CodeInstruction> codes = TranspilerUtil.ToCodeList(instructions);

            MethodBase trySpawnCall = TranspilerUtil.DeclaredMethod <TrySpawnDelegate>(typeof(VehicleAI), "TrySpawn");

            CodeInstruction searchInstruction = new CodeInstruction(OpCodes.Callvirt, trySpawnCall);
            int             index             = codes.FindIndex(instruction => TranspilerUtil.IsSameInstruction(instruction, searchInstruction));

            if (index > -1)
            {
                int          target1   = index + 2;
                Label        label     = il.DefineLabel();
                List <Label> oldLabels = codes[target1].labels.ToList();
                codes[target1].labels.Clear();                 // clear labels -> they are moved to new instruction
                codes[target1].labels.Add(label);              //add label to next instruction (if() false jump)
                List <CodeInstruction> newInstructions = GetUpdatePositionInstructions(label);
                newInstructions[0].labels.AddRange(oldLabels); // add old labels to redirect here
                codes.InsertRange(target1, newInstructions);   // insert new instructions

                CodeInstruction searchInstruction2 = new CodeInstruction(OpCodes.Ldfld, typeof(Vehicle).GetField(nameof(Vehicle.m_blockCounter)));
                int             index2             = codes.FindIndex(instruction => TranspilerUtil.IsSameInstruction(instruction, searchInstruction2));
                if (index2 > -1 && codes[index2 + 1].opcode.Equals(OpCodes.Ldc_I4) && codes[index2 + 2].opcode.Equals(OpCodes.Bne_Un))
                {
                    int   target2  = index2 + 2;
                    Label retLabel = (Label)codes[target2].operand;
                    codes.InsertRange(target2 + 1, GetModifiedBlockCounterInstructions(retLabel));
                }
                else
                {
                    throw new Exception("Could not find m_blockCounter field usage or instructions has been patched");
                }
            }
            else
            {
                throw new Exception("Could not find TrySpawn call or instructions has been patched");
            }

            return(codes);
        }
Ejemplo n.º 4
0
 public static MethodBase TargetMethod() => TranspilerUtil.DeclaredMethod <TargetDelegate>(typeof(HumanAI), "CheckTrafficLights");
Ejemplo n.º 5
0
 public static MethodBase TargetMethod() => TranspilerUtil.DeclaredMethod <TargetDelegate>(typeof(ResidentAI), "GetVehicleInfo");
 public static MethodBase TargetMethod() => TranspilerUtil.DeclaredMethod <TargetDelegate>(typeof(TramBaseAI), nameof(TramBaseAI.SimulationStep));
Ejemplo n.º 7
0
 public static MethodBase TargetMethod() => TranspilerUtil.DeclaredMethod <TargetDelegate>(typeof(TrainAI), "ForceTrafficLights");
Ejemplo n.º 8
0
 public static MethodBase TargetMethod() =>
 TranspilerUtil.DeclaredMethod <TargetDelegate>(typeof(PathManager), nameof(PathManager.CreatePath));
Ejemplo n.º 9
0
 private static MethodBase CreatePathMethod() => TranspilerUtil.DeclaredMethod <CreatePathDelegate>(typeof(PathManager), "CreatePath");
Ejemplo n.º 10
0
 public static MethodBase TargetMethod() => TranspilerUtil.DeclaredMethod <TargetDelegate>(typeof(PathFind), nameof(PathFind.CalculatePath));
Ejemplo n.º 11
0
 public static MethodBase TargetMethod() => TranspilerUtil.DeclaredMethod <TargetDelegate>(typeof(PassengerCarAI), nameof(PassengerCarAI.GetLocalizedStatus));
Ejemplo n.º 12
0
 public static MethodBase GetCitizenAITargetMethod() =>
 TranspilerUtil.DeclaredMethod <CitizenAITargetDelegate>(typeof(CitizenAI), "StartPathFind");
Ejemplo n.º 13
0
 public static MethodBase TargetMethod2 <T>() =>
 TranspilerUtil.DeclaredMethod <TargetDelegate2>(typeof(T), "StartPathFind");
 private static MethodInfo TargetMethod() =>
 TranspilerUtil.DeclaredMethod <FindParkingSpaceDelegate>(typeof(PassengerCarAI), "FindParkingSpace");
Ejemplo n.º 15
0
 public static MethodBase TargetMethod() => TranspilerUtil.DeclaredMethod <SimulationStepDelegate>(typeof(TrainAI), "SimulationStep");
 public static MethodBase TargetMethod() => TranspilerUtil.DeclaredMethod <TargetDelegate>(typeof(TrainAI), "CheckNextLane");
Ejemplo n.º 17
0
 public static MethodBase TargetMethod() => TranspilerUtil.DeclaredMethod <CalculatePositionDelegate>(typeof(CarAI), "CalculateSegmentPosition");
Ejemplo n.º 18
0
 private static MethodBase CheckSegmentProblemsMethod() => TranspilerUtil.DeclaredMethod <CheckSegmentProblemsDelegate>(typeof(TransportLineAI), "CheckSegmentProblems");