/// <summary>
        /// Caches <c>MaxNaturalFertility</c> on startup by getting the most fertile natural terrain.
        /// </summary>
        /// <remarks>
        /// Where "natural" is defined as "generated by a <c>BiomeDef</c>".
        /// </remarks>
        static NegativeFertilityPatch()
        {
            HashSet <TerrainDef> allPossibleNaturalTerrains = new HashSet <TerrainDef>();

            foreach (BiomeDef bd in DefDatabase <BiomeDef> .AllDefsListForReading)
            {
                foreach (TerrainThreshold tt in bd.terrainsByFertility)
                {
                    allPossibleNaturalTerrains.Add(tt.terrain);
                }
                foreach (TerrainPatchMaker tpm in bd.terrainPatchMakers)
                {
                    foreach (TerrainThreshold tt in tpm.thresholds)
                    {
                        allPossibleNaturalTerrains.Add(tt.terrain);
                    }
                }
            }
            IEnumerable <TerrainDef> terrainsByFertility = (from td in DefDatabase <TerrainDef> .AllDefsListForReading
                                                            where allPossibleNaturalTerrains.Contains(td)
                                                            orderby td.fertility descending
                                                            select td);

            if (terrainsByFertility.EnumerableNullOrEmpty())
            {
                ULog.Error("Negative Fertility Patch: terrainsByFertility was empty. Setting EffectiveMaxFertility to 1.");
                MaxNaturalFertility = 1f;
            }
            else
            {
                MaxNaturalFertility = terrainsByFertility.First().fertility;
            }
        }
Example #2
0
 public static bool ShouldPatch(string patchkey)
 {
     if (!DEBUG)
     {
         return(true);
     }
     if (!Patches.ContainsKey(patchkey))
     {
         ULog.Warning("ShouldPatch called for non-initialized patchkey.");
         return(true);
     }
     return(Patches[patchkey].apply);
 }
Example #3
0
        static HarmonyLoader()
        {
            ULog.Message("Applying Harmony patches...");
            var harmony = new Harmony("com.dninemfive.D9Framework");

            // https://stackoverflow.com/questions/2639418/use-reflection-to-get-a-list-of-static-classes
            foreach (Type type in typeof(HarmonyLoader).Assembly.GetTypes().Where(t => t.IsClass && t.IsSealed && t.IsAbstract))
            {
                ClassWithPatchesAttribute attr;
                if ((attr = type.TryGetAttribute <ClassWithPatchesAttribute>()) != null)
                {
                    if (!D9FModSettings.Patches.ContainsKey(attr.SaveKey))
                    {
                        D9FModSettings.Patches[attr.SaveKey] = new D9FModSettings.PatchInfo(attr.SaveKey, true, attr.LabelKey, attr.DescKey);
                    }
                    else
                    {
                        D9FModSettings.Patches[attr.SaveKey].apply = D9FModSettings.ShouldPatch(attr.SaveKey);
                    }
                    if (D9FModSettings.ShouldPatch(attr.SaveKey))
                    {
                        PatchAll(harmony, type);
                        ULog.DebugMessage("\t" + attr.PlainName + " enabled.", false);
                    }
                }
            }
            if (D9FModSettings.ApplyCarryMassFramework)
            {
                CMFHarmonyPatch.DoPatch(harmony);
                ULog.DebugMessage("\tCarry Mass Framework enabled.", false);
            }
            if (D9FModSettings.PrintPatchedMethods)
            {
                Log.Message("The following methods were successfully patched:", false);
                foreach (MethodBase mb in harmony.GetPatchedMethods())
                {
                    Log.Message("\t" + mb.DeclaringType.Name + "." + mb.Name, false);
                }
            }
        }
Example #4
0
        static HarmonyLoader()
        {
            ULog.Message("Applying Harmony patches...");
            var harmony = new Harmony("com.dninemfive.D9Framework");

            if (D9FModSettings.ApplyCompFromStuff)
            {
                PatchAll(harmony, typeof(CompFromStuff));
                ULog.DebugMessage("\tCompFromStuff enabled.", false);
            }
            if (D9FModSettings.ApplyDeconstructReturnFix)
            {
                PatchAll(harmony, typeof(DeconstructReturnFix));
                ULog.DebugMessage("\tDeconstruct Return Fix enabled.", false);
            }
            if (D9FModSettings.ApplyOrbitalTradeHook)
            {
                PatchAll(harmony, typeof(OrbitalTradeHook));
                ULog.DebugMessage("\tOrbital Trade Hook enabled.", false);
            }
            if (D9FModSettings.ApplyForceAllowPlaceOverFix)
            {
                PatchAll(harmony, typeof(ForceAllowPlaceOverFix));
                ULog.DebugMessage("\tForce Allow Place Over Fix", false);
            }
            if (D9FModSettings.ApplyCarryMassFramework)
            {
                CMFHarmonyPatch.DoPatch(harmony);
                ULog.DebugMessage("\tCarry Mass Framework", false);
            }
            if (D9FModSettings.PrintPatchedMethods)
            {
                Log.Message("The following methods were successfully patched:", false);
                foreach (MethodBase mb in harmony.GetPatchedMethods())
                {
                    Log.Message("\t" + mb.DeclaringType.Name + "." + mb.Name, false);
                }
            }
        }
Example #5
0
            public static bool DoLeavingsForPrefix(Thing diedThing, Map map, DestroyMode mode, CellRect leavingsRect, Predicate <IntVec3> nearPlaceValidator = null)
            {
                if (mode != DestroyMode.Deconstruct)
                {
                    return(true);                                 //just to make it more readable
                }
                ThingOwner <Thing> thingOwner = new ThingOwner <Thing>();

                if (GenLeaving.CanBuildingLeaveResources(diedThing, mode))
                {
                    Frame frame = diedThing as Frame;
                    if (frame != null)
                    {
                        for (int frameResCt = frame.resourceContainer.Count - 1; frameResCt >= 0; frameResCt--)
                        {
                            int gblrc;
                            if ((gblrc = GBRLC(diedThing)(frame.resourceContainer[frameResCt].stackCount)) > 0)
                            {
                                frame.resourceContainer.TryTransferToContainer(frame.resourceContainer[frameResCt], thingOwner, gblrc, true);
                            }
                        }
                        frame.resourceContainer.ClearAndDestroyContents(mode);
                    }
                    else
                    {
                        // TODO: ModExtension specifying drop rates per ThingDef. Needs to be relatively optimized.
                        List <ThingDefCountClass> list = diedThing.CostListAdjusted();
                        for (int l = 0; l < list.Count; l++)
                        {
                            ThingDefCountClass tdcc = list[l];
                            int gblrc;
                            if ((gblrc = GBRLC(diedThing)(tdcc.count)) > 0)
                            {
                                Thing thing = ThingMaker.MakeThing(tdcc.thingDef, null);
                                thing.stackCount = gblrc;
                                thingOwner.TryAdd(thing, true);
                            }
                        }
                    }
                }
                List <IntVec3> cellList = leavingsRect.Cells.InRandomOrder(null).ToList();
                int            cellInd  = 0;

                while (true)
                {
                    if (thingOwner.Count > 0)
                    {
                        ThingOwner <Thing> thingOwner2 = thingOwner;
                        Thing          thing           = thingOwner[0];
                        IntVec3        dropLoc         = cellList[cellInd];
                        ThingPlaceMode mode2           = ThingPlaceMode.Near;
                        Thing          thing4          = default(Thing);
                        if (thingOwner2.TryDrop(thing, dropLoc, map, mode2, out thing4, null, nearPlaceValidator))
                        {
                            cellInd++;
                            if (cellInd >= cellList.Count)
                            {
                                cellInd = 0;
                            }
                            continue;
                        }
                        break;
                    }
                    return(false);
                }
                ULog.Warning("Deconstruct Return Fix: Failed to place all leavings for destroyed thing " + diedThing + " at " + leavingsRect.CenterCell, false);
                return(false);
            }//end DoLeavingsForPrefix