Ejemplo n.º 1
0
        private static void Detour(Pawn_JobTracker __instance)
        {
            Profiler pro = null;

            __instance.jobsGivenThisTick        = 0;
            __instance.jobsGivenThisTickTextual = "";
            if (__instance.pawn.IsHashIntervalTick(30))
            {
                pro = ProfileController.Start("DetermineNextConstantThinkTreeJob");
                var thinkResult = __instance.DetermineNextConstantThinkTreeJob();
                pro.Stop();
                if (thinkResult.IsValid)
                {
                    pro = ProfileController.Start("ShouldStartJobFromThinkTree");
                    if (__instance.ShouldStartJobFromThinkTree(thinkResult))
                    {
                        __instance.CheckLeaveJoinableLordBecauseJobIssued(thinkResult);
                        __instance.StartJob(thinkResult.Job, JobCondition.InterruptForced, thinkResult.SourceNode,
                                            false, false, __instance.pawn.thinker.ConstantThinkTree, thinkResult.Tag);
                    }
                    else if (thinkResult.Job != __instance.curJob && !__instance.jobQueue.Contains(thinkResult.Job))
                    {
                        JobMaker.ReturnToPool(thinkResult.Job);
                    }

                    pro.Stop();
                }
            }

            if (__instance.curDriver != null)
            {
                if (__instance.curJob.expiryInterval > 0 &&
                    (Find.TickManager.TicksGame - __instance.curJob.startTick) % __instance.curJob.expiryInterval ==
                    0 && Find.TickManager.TicksGame != __instance.curJob.startTick)
                {
                    pro = ProfileController.Start("EnemiesAreNearby");
                    var enemies = !__instance.curJob.expireRequiresEnemiesNearby ||
                                  PawnUtility.EnemiesAreNearby(__instance.pawn, 25);
                    pro.Stop();
                    if (enemies)
                    {
                        if (__instance.debugLog)
                        {
                            __instance.DebugLogEvent("Job expire");
                        }

                        if (!__instance.curJob.checkOverrideOnExpire)
                        {
                            pro = ProfileController.Start("EndCurrentJob");
                            __instance.EndCurrentJob(JobCondition.Succeeded);
                            pro.Stop();
                        }
                        else
                        {
                            pro = ProfileController.Start("CheckForJobOverride");
                            __instance.CheckForJobOverride();
                            pro.Stop();
                        }

                        pro = ProfileController.Start("FinalizeTick");
                        __instance.FinalizeTick();
                        pro.Stop();
                        return;
                    }

                    if (__instance.debugLog)
                    {
                        __instance.DebugLogEvent("Job expire skipped because there are no enemies nearby");
                    }
                }

                var key = string.Empty;

                if (PerPawn)
                {
                    key = __instance.pawn.LabelShort + __instance.curDriver.job?.def.defName;
                }
                else
                {
                    key = __instance.curDriver.job?.def.defName;
                }

                string label()
                {
                    var daffy = string.Empty;

                    if (PerPawn)
                    {
                        daffy =
                            $"{__instance.pawn.LabelShort} {__instance.curDriver.job?.def?.defName} - {__instance.curDriver.job?.def?.driverClass} - {__instance.curDriver.job?.def?.modContentPack?.Name}";
                    }
                    else
                    {
                        daffy =
                            $"{__instance.curDriver.job?.def?.defName} - {__instance.curDriver.job?.def?.driverClass} - {__instance.curDriver.job?.def?.modContentPack?.Name}";
                    }

                    return(daffy);
                }

                var meth = AccessTools.Method(__instance.curDriver.job?.def?.driverClass, "DriverTick");

                pro = ProfileController.Start(key, label, __instance.curDriver.job?.def?.driverClass,
                                              __instance.curDriver.job?.def, __instance.pawn, meth);
                __instance.curDriver.DriverTick();
                pro.Stop();
            }

            if (__instance.curJob == null && !__instance.pawn.Dead && __instance.pawn.mindState.Active)
            {
                pro = ProfileController.Start("CanDoAnyJob");
                var doit = __instance.CanDoAnyJob();
                pro.Stop();
                if (doit)
                {
                    if (__instance.debugLog)
                    {
                        __instance.DebugLogEvent("Starting job from Tick because curJob == null.");
                    }

                    pro = ProfileController.Start("FinalizeTick");
                    __instance.TryFindAndStartJob();
                    pro.Stop();
                }
            }
            pro = ProfileController.Start("FinalizeTick");
            __instance.FinalizeTick();
            pro.Stop();
        }
Ejemplo n.º 2
0
        public static void Prefix()
        {
            if (Analyzer.CurrentlyProfiling)
            {
                long jam = totalBytesOfMemoryUsed;
                totalBytesOfMemoryUsed = GC.GetTotalMemory(false);

                if (jam > totalBytesOfMemoryUsed)
                {
                    LastMinGC             = totalBytesOfMemoryUsed;
                    LastMaxGC             = jam;
                    GarbageCollectionInfo = $"{totalBytesOfMemoryUsed.ToMb()}MB";
                }

                delta += Time.deltaTime;
                if (delta >= 1f)
                {
                    delta -= 1f;

                    long PreviouslyAllocatedMemory = CurrentAllocatedMemory;
                    CurrentAllocatedMemory = GC.GetTotalMemory(false);

                    long MemoryDifference = CurrentAllocatedMemory - PreviouslyAllocatedMemory;
                    if (MemoryDifference < 0)
                    {
                        MemoryDifference = 0;
                    }

                    GarbageCollectionInfo = $"{CurrentAllocatedMemory.ToMb():0.00}MB +{MemoryDifference.ToMb():0.00}MB/s";
                }


                if (Time.unscaledTime > _timer)
                {
                    int fps = (int)(1f / Time.unscaledDeltaTime);
                    _fpsText = "FPS: " + fps;
                    _timer   = Time.unscaledTime + _hudRefreshRate; // Every second we update our fps
                }

                try
                {
                    if (Current.ProgramState == ProgramState.Playing)
                    {
                        float TRM       = Find.TickManager.TickRateMultiplier;
                        int   TPSTarget = (int)Math.Round(TRM == 0f ? 0f : 60f * TRM);

                        DateTime CurrTime = DateTime.Now;

                        if (CurrTime.Second != PrevTime.Second)
                        {
                            PrevTime  = CurrTime;
                            TPSActual = GenTicks.TicksAbs - PrevTicks;
                            PrevTicks = GenTicks.TicksAbs;
                        }

                        tps = $"TPS: {TPSActual}({TPSTarget})";
                    }
                }
                catch (Exception) { }
            }

            if (Active)
            {
                ProfileController.Start("Game Update");
            }

#if DEBUG
            if (GUIController.CurrentCategory != Category.Tick)
            {
                ProfileController.BeginUpdate();
            }
#endif
        }
Ejemplo n.º 3
0
 public Profiler Start(string key, MethodBase info)
 {
     return(ProfileController.Start(key, null, null, null, null, info));
 }
Ejemplo n.º 4
0
        public static bool Prefix(MethodBase __originalMethod, SectionLayer_Things __instance)
        {
            if (!Active)
            {
                return(true);
            }

            if (ByDef)
            {
                if (!__instance.Visible)
                {
                    return(false);
                }
                int count = __instance.subMeshes.Count;



                for (int i = 0; i < count; i++)
                {
                    LayerSubMesh layerSubMesh = __instance.subMeshes[i];
                    if (layerSubMesh.finalized && !layerSubMesh.disabled)
                    {
                        string Namer()
                        {
                            var n = layerSubMesh.material?.mainTexture?.name ?? layerSubMesh.GetType().Name;

                            return(n);
                        }

                        // todo material.mainTexture is a non trivial lookup
                        var name = layerSubMesh.material?.mainTexture?.name ?? layerSubMesh.GetType().Name;

                        var prof = ProfileController.Start(name, Namer, __originalMethod.GetType(), __originalMethod);
                        Graphics.Internal_DrawMesh_Injected(layerSubMesh.mesh, 0, ref johnmatrix, layerSubMesh.material, 0, null, null, ShadowCastingMode.Off, false, null, LightProbeUsage.Off, null);

                        // Graphics.DrawMesh(layerSubMesh.mesh, Vector3.zeroVector, Quaternion.identityQuaternion, layerSubMesh.material, 0);
                        prof.Stop();
                    }
                }



                return(false);
            }

            {
                if (!__instance.Visible)
                {
                    return(false);
                }

                int count = __instance.subMeshes.Count;



                for (int i = 0; i < count; i++)
                {
                    LayerSubMesh layerSubMesh = __instance.subMeshes[i];
                    if (layerSubMesh.finalized && !layerSubMesh.disabled)
                    {
                        string Namer()
                        {
                            var n = layerSubMesh.material?.mainTexture?.name ?? layerSubMesh.GetType().Name;

                            return(n);
                        }

                        var name = layerSubMesh.material?.mainTexture?.name ?? layerSubMesh.GetType().Name;

                        var prof = ProfileController.Start(name, Namer, __originalMethod.GetType(),
                                                           __originalMethod);
                        Graphics.DrawMesh(layerSubMesh.mesh, Vector3.zero, Quaternion.identity, layerSubMesh.material,
                                          0);
                        prof.Stop();
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 5
0
        public static bool Prefix(MethodBase __originalMethod, WindowStack __instance)
        {
            if (!Active)
            {
                return(true);
            }

            __instance.windowStackOnGUITmpList.Clear();
            __instance.windowStackOnGUITmpList.AddRange(__instance.windows);
            for (int i = __instance.windowStackOnGUITmpList.Count - 1; i >= 0; i--)
            {
                string name = string.Empty;
                if (__instance.windowStackOnGUITmpList[i] is ImmediateWindow)
                {
                    name = string.Intern(
                        $"{__instance.windowStackOnGUITmpList[i].GetType()} ExtraOnGUI {__instance.windowStackOnGUITmpList[i].ID}");
                }
                else
                {
                    name = string.Intern($"{__instance.windowStackOnGUITmpList[i].GetType()} ExtraOnGUI");
                }

                Profiler prof = ProfileController.Start(name, null, null, null, null, __originalMethod);
                __instance.windowStackOnGUITmpList[i].ExtraOnGUI();
                prof.Stop();
            }

            __instance.UpdateImmediateWindowsList();
            __instance.windowStackOnGUITmpList.Clear();
            __instance.windowStackOnGUITmpList.AddRange(__instance.windows);
            for (int j = 0; j < __instance.windowStackOnGUITmpList.Count; j++)
            {
                if (__instance.windowStackOnGUITmpList[j].drawShadow)
                {
                    GUI.color = new Color(1f, 1f, 1f, __instance.windowStackOnGUITmpList[j].shadowAlpha);
                    Widgets.DrawShadowAround(__instance.windowStackOnGUITmpList[j].windowRect);
                    GUI.color = Color.white;
                }

                string name = string.Empty;
                if (__instance.windowStackOnGUITmpList[j] is ImmediateWindow)
                {
                    name = string.Intern(
                        $"{__instance.windowStackOnGUITmpList[j].GetType()} WindowOnGUI {__instance.windowStackOnGUITmpList[j].ID}");
                }
                else
                {
                    name = string.Intern($"{__instance.windowStackOnGUITmpList[j].GetType()} WindowOnGUI");
                }

                Profiler prof = ProfileController.Start(name, null, null, null, null, __originalMethod);
                __instance.windowStackOnGUITmpList[j].WindowOnGUI();
                prof.Stop();
            }

            if (__instance.updateInternalWindowsOrderLater)
            {
                __instance.updateInternalWindowsOrderLater = false;
                __instance.UpdateInternalWindowsOrder();
            }

            return(false);
        }
Ejemplo n.º 6
0
        private static ThinkResult Detour(JobGiver_Work __instance, Pawn pawn)
        {
            if (__instance.emergency && pawn.mindState.priorityWork.IsPrioritized)
            {
                List <WorkGiverDef> workGiversByPriority = pawn.mindState.priorityWork.WorkGiver.workType.workGiversByPriority;
                for (int i = 0; i < workGiversByPriority.Count; i++)
                {
                    WorkGiver worker = workGiversByPriority[i].Worker;
                    Job       job    = __instance.GiverTryGiveJobPrioritized(pawn, worker, pawn.mindState.priorityWork.Cell);
                    if (job != null)
                    {
                        job.playerForced = true;
                        return(new ThinkResult(job, __instance, workGiversByPriority[i].tagToGive));
                    }
                }

                pawn.mindState.priorityWork.Clear();
            }

            List <WorkGiver> list = __instance.emergency ? pawn.workSettings.WorkGiversInOrderEmergency : pawn.workSettings.WorkGiversInOrderNormal;

            int               num = -999;
            TargetInfo        bestTargetOfLastPriority = TargetInfo.Invalid;
            WorkGiver_Scanner scannerWhoProvidedTarget = null;
            int               coo = list.Count;

            Profiler prof = null;

            for (int j = 0; j < coo; j++)
            {
                WorkGiver workGiver = list[j];

                if (!H_TryIssueJobPackageTrans.meths.TryGetValue(workGiver, out var meth))
                {
                    if (workGiver is WorkGiver_Scanner)
                    {
                        if (workGiver.def.scanThings)
                        {
                            meth = AccessTools.Method(workGiver.GetType(), "PotentialWorkThingsGlobal");
                        }
                        else
                        {
                            meth = AccessTools.Method(workGiver.GetType(), "PotentialWorkCellsGlobal");
                        }
                    }
                    else
                    {
                        meth = AccessTools.Method(workGiver.GetType(), "NonScanJob");
                    }
                    H_TryIssueJobPackageTrans.meths.Add(workGiver, meth);
                }

                string namer()
                {
                    string daffy = string.Empty;

                    if (ByWorkType)
                    {
                        daffy = workGiver.def?.workType?.defName;
                    }
                    else
                    {
                        daffy = $"{workGiver.def?.defName} - {workGiver.def?.workType.defName} - {workGiver.def?.modContentPack?.Name}";
                    }

                    if (RequestTypes && workGiver is WorkGiver_Scanner scan)
                    {
                        daffy += $" - {scan.PotentialWorkThingRequest}";
                        if (scan.PotentialWorkThingRequest.group ==
                            ThingRequestGroup.BuildingArtificial)
                        {
                            daffy += " VERY BAD!";
                        }
                    }

                    return(daffy);
                }

                if (workGiver.def.priorityInType != num && bestTargetOfLastPriority.IsValid)
                {
                    break;
                }

                if (__instance.PawnCanUseWorkGiver(pawn, workGiver))
                {
                    string name = string.Empty;

                    if (ByWorkType)
                    {
                        name = workGiver.def.workType.defName;
                    }
                    else
                    {
                        name = workGiver.def.defName;
                    }

                    //if (true)
                    //{
                    //    name = string.Intern(name + pawn.Name.ToStringShort);
                    //}
                    if (workGiver is WorkGiver_Scanner scanny)
                    {
                        name += $"{scanny.PotentialWorkThingRequest}";
                    }

                    try
                    {
                        Job job2 = workGiver.NonScanJob(pawn);
                        if (job2 != null)
                        {
                            return(new ThinkResult(job2, __instance, list[j].def.tagToGive));
                        }

                        if (workGiver is WorkGiver_Scanner scanner)
                        {
                            if (scanner.def.scanThings)
                            {
                                bool Predicate(Thing t)
                                {
                                    return(!t.IsForbidden(pawn) && scanner.HasJobOnThing(pawn, t));
                                }
                                prof = ProfileController.Start(name, namer, workGiver.GetType(), workGiver.def, pawn, meth);
                                IEnumerable <Thing> enumerable = scanner.PotentialWorkThingsGlobal(pawn)?.Where(x => scanner.HasJobOnThing(pawn, x));

                                //if (scanner is WorkGiver_Repair repair)
                                //{
                                //    foreach (var repairableBuilding in pawn.Map.listerBuildingsRepairable.RepairableBuildings(pawn.Faction))
                                //    {
                                //        Log.Warning($"repair scan on {repairableBuilding}");
                                //    }
                                //}

                                Thing thing;
                                if (scanner.Prioritized)
                                {
                                    IEnumerable <Thing> enumerable2 = enumerable;
                                    if (enumerable2 == null)
                                    {
                                        enumerable2 = pawn.Map.listerThings.ThingsMatching(scanner.PotentialWorkThingRequest);
                                    }

                                    if (scanner.AllowUnreachable)
                                    {
                                        thing = GenClosest.ClosestThing_Global(pawn.Position, enumerable2, 99999f, Predicate, x => scanner.GetPriority(pawn, x));
                                    }
                                    else
                                    {
                                        TraverseParms traverseParams = TraverseParms.For(pawn, scanner.MaxPathDanger(pawn));
                                        // var validator = (Predicate<Thing>)Predicate;
                                        thing = GenClosest.ClosestThing_Global_Reachable(pawn.Position, pawn.Map,
                                                                                         enumerable2,
                                                                                         scanner.PathEndMode, traverseParams, 9999f, Predicate,
                                                                                         x => scanner.GetPriority(pawn, x));
                                    }
                                }
                                else if (scanner.AllowUnreachable)
                                {
                                    IEnumerable <Thing> enumerable3 = enumerable;
                                    if (enumerable3 == null)
                                    {
                                        enumerable3 = pawn.Map.listerThings.ThingsMatching(scanner.PotentialWorkThingRequest);
                                    }

                                    thing = GenClosest.ClosestThing_Global(pawn.Position, enumerable3, 99999f, Predicate);
                                }
                                else
                                {
                                    giver = workGiver;
                                    key   = name;

                                    thing = GenClosest.ClosestThingReachable(pawn.Position, pawn.Map,
                                                                             scanner.PotentialWorkThingRequest,
                                                                             scanner.PathEndMode, TraverseParms.For(pawn, scanner.MaxPathDanger(pawn)),
                                                                             9999f, Predicate, enumerable, 0,
                                                                             scanner.MaxRegionsToScanBeforeGlobalSearch, enumerable != null);

                                    giver = null;
                                }

                                prof.Stop();

                                if (thing != null)
                                {
                                    bestTargetOfLastPriority = thing;
                                    scannerWhoProvidedTarget = scanner;
                                }
                            }


                            if (scanner.def.scanCells)
                            {
                                prof = ProfileController.Start(name, namer, workGiver.GetType(), workGiver.def, pawn, meth);
                                float  closestDistSquared = 99999f;
                                float  bestPriority       = float.MinValue;
                                bool   prioritized        = scanner.Prioritized;
                                bool   allowUnreachable   = scanner.AllowUnreachable;
                                Danger maxDanger          = scanner.MaxPathDanger(pawn);
                                foreach (IntVec3 intVec in scanner.PotentialWorkCellsGlobal(pawn))
                                {
                                    bool  flag = false;
                                    int   num4 = (intVec - pawn.Position).LengthHorizontalSquared;
                                    float num5 = 0f;
                                    if (prioritized)
                                    {
                                        if (!intVec.IsForbidden(pawn) && scanner.HasJobOnCell(pawn, intVec))
                                        {
                                            if (!allowUnreachable &&
                                                !pawn.CanReach(intVec, scanner.PathEndMode, maxDanger))
                                            {
                                                continue;
                                            }

                                            num5 = scanner.GetPriority(pawn, intVec);
                                            if (num5 > bestPriority || num5 == bestPriority && num4 < closestDistSquared)
                                            {
                                                flag = true;
                                            }
                                        }
                                    }
                                    else if (num4 < closestDistSquared && !intVec.IsForbidden(pawn) && scanner.HasJobOnCell(pawn, intVec))
                                    {
                                        if (!allowUnreachable && !pawn.CanReach(intVec, scanner.PathEndMode, maxDanger))
                                        {
                                            continue;
                                        }

                                        flag = true;
                                    }

                                    if (flag)
                                    {
                                        bestTargetOfLastPriority = new TargetInfo(intVec, pawn.Map);
                                        scannerWhoProvidedTarget = scanner;
                                        closestDistSquared       = num4;
                                        bestPriority             = num5;
                                    }
                                }
                                prof.Stop();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error(string.Concat(pawn, " threw exception in WorkGiver ", workGiver.def.defName, ": ",
                                                ex.ToString()));
                    }

                    if (bestTargetOfLastPriority.IsValid)
                    {
                        //  pawn.mindState.lastGivenWorkType = workGiver.def.workType;
                        prof = ProfileController.Start(name, namer, workGiver.GetType(), workGiver.def, pawn, meth);
                        Job job3;
                        if (bestTargetOfLastPriority.HasThing)
                        {
                            job3 = scannerWhoProvidedTarget.JobOnThing(pawn, bestTargetOfLastPriority.Thing);
                        }
                        else
                        {
                            job3 = scannerWhoProvidedTarget.JobOnCell(pawn, bestTargetOfLastPriority.Cell);
                        }
                        prof.Stop();
                        if (job3 != null)
                        {
                            job3.workGiverDef = scannerWhoProvidedTarget.def;
                            return(new ThinkResult(job3, __instance, list[j].def.tagToGive));
                        }

                        Log.ErrorOnce(
                            string.Concat("Analyzer: ", scannerWhoProvidedTarget, " provided target ", bestTargetOfLastPriority,
                                          " but yielded no actual job for pawn ", pawn,
                                          ". The CanGiveJob and JobOnX methods may not be synchronized."), 6112651);
                    }

                    num = workGiver.def.priorityInType;
                }
            }

            return(ThinkResult.NoJob);
        }
Ejemplo n.º 7
0
        public static bool Prefix(MethodBase __originalMethod, DynamicDrawManager __instance)
        {
            if (!Active)
            {
                return(true);
            }

            __instance.drawingNow = true;
            try
            {
                bool[]   fogGrid  = __instance.map.fogGrid.fogGrid;
                CellRect cellRect = Find.CameraDriver.CurrentViewRect;
                cellRect.ClipInsideMap(__instance.map);
                cellRect = cellRect.ExpandedBy(1);
                CellIndices cellIndices = __instance.map.cellIndices;
                foreach (Thing thing in __instance.drawThings)
                {
                    IntVec3 position = thing.Position;
                    if (cellRect.Contains(position) || thing.def.drawOffscreen)
                    {
                        if (!fogGrid[cellIndices.CellToIndex(position)] || thing.def.seeThroughFog)
                        {
                            if (thing.def.hideAtSnowDepth >= 1f || __instance.map.snowGrid.GetDepth(thing.Position) <=
                                thing.def.hideAtSnowDepth)
                            {
                                try
                                {
                                    string Namer()
                                    {
                                        string n = string.Empty;

                                        if (ByDef)
                                        {
                                            n = $"{thing.def.defName} - {thing?.def?.modContentPack?.Name}";
                                        }
                                        else
                                        {
                                            n = thing.GetType().ToString();
                                        }

                                        return(n);
                                    }

                                    string name = string.Empty;
                                    if (ByDef)
                                    {
                                        name = thing.def.defName;
                                    }
                                    else
                                    {
                                        name = thing.GetType().Name;
                                    }

                                    var prof = ProfileController.Start(name, Namer, thing.GetType(), thing.def, null, __originalMethod);
                                    thing.Draw();
                                    prof.Stop();
                                }
                                catch (Exception ex)
                                {
                                    Log.Error(string.Concat("Exception drawing ", thing, ": ", ex.ToString()));
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception arg)
            {
                Log.Error("Exception drawing dynamic things: " + arg);
            }

            __instance.drawingNow = false;
            return(false);
        }