Esempio n. 1
0
        public static bool CanReserve(ref bool __result, Pawn p, LocalTargetInfo target, int maxPawns = 1, int stackCount = -1, ReservationLayerDef layer = null, bool ignoreOtherReservations = false)
        {
            if (!p.Spawned)
            {
                __result = false;
                return(false);
            }
            Map map = p.Map;
            ReservationManager reservationManager = map.reservationManager;

            __result = false;
            if (reservationManager != null)
            {
                __result = reservationManager.CanReserve(
                    p,
                    target,
                    maxPawns,
                    stackCount,
                    layer,
                    ignoreOtherReservations);
            }
            return(false);
        }
Esempio n. 2
0
        public static bool Reserve(ReservationManager __instance, ref bool __result,
                                   Pawn claimant,
                                   Job job,
                                   LocalTargetInfo target,
                                   int maxPawns              = 1,
                                   int stackCount            = -1,
                                   ReservationLayerDef layer = null,
                                   bool errorOnFailed        = true)
        {
            if (maxPawns > 1 && stackCount == -1)
            {
                Log.ErrorOnce("Reserving with maxPawns > 1 and stackCount = All; this will not have a useful effect (suppressing future warnings)", 83269, false);
            }

            if (job == null)
            {
                Log.Warning(claimant.ToStringSafe() + " tried to reserve thing " + target.ToStringSafe() + " without a valid job");
                __result = false;
                return(false);
            }
            int num1 = (!target.HasThing) ? 1 : target.Thing.stackCount;
            int num2 = stackCount == -1 ? num1 : stackCount;

            lock (reservations(__instance))
            {
                for (int index = 0; index < reservations(__instance).Count; ++index)
                {
                    Reservation reservation1;
                    try
                    {
                        reservation1 = reservations(__instance)[index];
                    } catch (ArgumentOutOfRangeException) { break; }
                    if (reservation1 != null && reservation1.Target == target && reservation1.Claimant == claimant && reservation1.Job == job && reservation1.Layer == layer && (reservation1.StackCount == -1 || reservation1.StackCount >= num2))
                    {
                        __result = true;
                        return(false);
                    }
                }
                if (!target.IsValid || target.ThingDestroyed)
                {
                    __result = false;
                    return(false);
                }
                bool canReserveResult = __instance.CanReserve(claimant, target, maxPawns, stackCount, layer);
                if (!canReserveResult)
                {
                    //bool canReserveResult2 = __instance.CanReserve(claimant, target, maxPawns, stackCount, layer);
                    if (job != null && job.playerForced && __instance.CanReserve(claimant, target, maxPawns, stackCount, layer))
                    {
                        reservations(__instance).Add(new Reservation(claimant, job, maxPawns, stackCount, target, layer));
                        //foreach (ReservationManager.Reservation reservation in reservations(__instance).ToList<ReservationManager.Reservation>())
                        Reservation reservation2;
                        for (int index = 0; index < reservations(__instance).Count; index++)
                        {
                            try
                            {
                                reservation2 = reservations(__instance)[index];
                            }
                            catch (ArgumentOutOfRangeException) { break; }
                            if (reservation2.Target == target && reservation2.Claimant != claimant && (reservation2.Layer == layer && RespectsReservationsOf(claimant, reservation2.Claimant)))
                            {
                                reservation2.Claimant.jobs.EndCurrentOrQueuedJob(reservation2.Job, JobCondition.InterruptForced);
                            }
                        }
                        __result = true;
                        return(false);
                    }

                    //HACK - Probably because Reserve is no longer valid after CanReserve time delay with multiple threads.
                    if (errorOnFailed)
                    {
                        //LogCouldNotReserveError(__instance, claimant, job, target, maxPawns, stackCount, layer);
                        Log.Warning("ReservationManager.Reserve cannot reserve. This is likely because reservation is no longer valid after CanReserve was called due to time delay with multiple threads.");
                    }
                    __result = false;
                    return(false);
                }
                reservations(__instance).Add(new ReservationManager.Reservation(claimant, job, maxPawns, stackCount, target, layer));
            }
            __result = true;
            return(false);
        }
Esempio n. 3
0
        public static bool Reserve(ReservationManager __instance, ref bool __result,
                                   Pawn claimant,
                                   Job job,
                                   LocalTargetInfo target,
                                   int maxPawns              = 1,
                                   int stackCount            = -1,
                                   ReservationLayerDef layer = null,
                                   bool errorOnFailed        = true)
        {
            lock (__instance)
            {
                if (maxPawns > 1 && stackCount == -1)
                {
                    Log.ErrorOnce("Reserving with maxPawns > 1 and stackCount = All; this will not have a useful effect (suppressing future warnings)", 83269);
                }

                if (job == null)
                {
                    Log.Warning(claimant.ToStringSafe() + " tried to reserve thing " + target.ToStringSafe() + " without a valid job");
                    __result = false;
                    return(false);
                }
                int num1 = (!target.HasThing) ? 1 : target.Thing.stackCount;
                int num2 = stackCount == -1 ? num1 : stackCount;

                List <Reservation> reservationTargetList = getReservationTargetList(__instance, target);
                foreach (Reservation reservation1 in reservationTargetList)
                {
                    if (reservation1.Claimant == claimant && reservation1.Job == job && reservation1.Layer == layer)
                    {
                        if (reservation1.StackCount == -1 || reservation1.StackCount >= num2)
                        {
                            __result = true;
                            return(false);
                        }
                        Log.Warning("Debug reservation");
                    }
                }
                if (!target.IsValid || target.ThingDestroyed)                 //TODO reservation should be removed when thing is destroyed
                {
                    __result = false;
                    return(false);
                }
                bool        canReserveResult = __instance.CanReserve(claimant, target, maxPawns, stackCount, layer);
                Reservation reservation;
                Dictionary <LocalTargetInfo, List <Reservation> > reservationTargetDict;
                Dictionary <Pawn, List <Reservation> >            reservationClaimantDict;
                Thing haulableThing;
                if (!canReserveResult)
                {
                    if (job.playerForced && __instance.CanReserve(claimant, target, maxPawns, stackCount, layer, true))
                    {
                        reservation = new Reservation(claimant, job, maxPawns, stackCount, target, layer);
                        List <Reservation> claimantReservations;

                        reservationTargetDict = getReservationTargetDict(__instance);
                        //newReservationTargetList = new List<Reservation>(getReservationTargetList(reservationTargetDict, target))
                        //{
                        //    reservation
                        //};
                        getReservationTargetList(reservationTargetDict, target).Add(reservation);
                        reservationClaimantDict = getReservationClaimantDict(__instance);
                        //newReservationClaimantList =
                        //    new List<Reservation>(getReservationClaimantList(reservationClaimantDict, claimant))
                        //    {
                        //        reservation
                        //    };
                        claimantReservations = getReservationClaimantList(reservationClaimantDict, claimant);
                        claimantReservations.Add(reservation);

                        foreach (Reservation reservation2 in claimantReservations)
                        {
                            if (reservation2.Claimant != claimant && (reservation2.Layer == layer && RespectsReservationsOf(claimant, reservation2.Claimant)))
                            {
                                reservation2.Claimant.jobs.EndCurrentOrQueuedJob(reservation2.Job, JobCondition.InterruptForced);
                            }
                        }
                        haulableThing = target.Thing;
                        if (haulableThing != null && haulableThing.def.EverHaulable && haulableThing.Map != null)
                        {
                            //Log.Message("DeregisterHaulableItem " + haulableThing.ToString());
                            HaulingCache.DeregisterHaulableItem(haulableThing);
                        }
                        __result = true;
                        return(false);
                    }

                    //HACK - Probably because Reserve is no longer valid after CanReserve time delay with multiple threads.
                    if (errorOnFailed)
                    {
                        //LogCouldNotReserveError(__instance, claimant, job, target, maxPawns, stackCount, layer);
                        Log.Warning("ReservationManager.Reserve cannot reserve. This is likely because reservation is no longer valid after CanReserve was called due to time delay with multiple threads.");
                    }
                    __result = false;
                    return(false);
                }
                reservation = new Reservation(claimant, job, maxPawns, stackCount, target, layer);

                reservationTargetDict = getReservationTargetDict(__instance);
                //newReservationTargetList = new List<Reservation>(getReservationTargetList(reservationTargetDict, target))
                //	{
                //		reservation
                //	};
                getReservationTargetList(reservationTargetDict, target).Add(reservation);
                reservationClaimantDict = getReservationClaimantDict(__instance);
                //newReservationClaimantList = new List<Reservation>(getReservationClaimantList(reservationClaimantDict, claimant))
                //	{
                //		reservation
                //	};
                getReservationClaimantList(reservationClaimantDict, claimant).Add(reservation);


                haulableThing = target.Thing;
                if (haulableThing != null && haulableThing.def.EverHaulable && haulableThing.Map != null)
                {
                    //Log.Message("DeregisterHaulableItem " + haulableThing.ToString());
                    HaulingCache.DeregisterHaulableItem(haulableThing);
                }
            }
            __result = true;
            return(false);
        }