Skip to content

Commit

Permalink
#612 implemented option
Browse files Browse the repository at this point in the history
removed Belts use of patch
changed Storage Patches to be conditional
added Cold Storage Building
  • Loading branch information
Sn1p3rr3c0n committed Sep 1, 2022
1 parent c8380c3 commit 24b3597
Show file tree
Hide file tree
Showing 16 changed files with 844 additions and 76 deletions.
107 changes: 107 additions & 0 deletions Defs/ThingDefs_Buildings/Buildings_Storage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,113 @@
</li>
</modExtensions>
</ThingDef>

<!--==================== Cold Storage ====================-->
<ThingDef ParentName="PRF_BuildingBase">
<thingClass>ProjectRimFactory.Storage.Building_ColdStoragePowered</thingClass>
<defName>PRF_ColdStorageUnit_I</defName>
<label>Digital Cold Storage Unit</label>
<description>A matter-energy conversion based storage unit which can hold up to 10k stacks, consuming 10 W of power per stack of items. In the event of a power outage, items already inside will be safe, but the DSU will not be able to store more. Don't break it.</description>
<designatorDropdown>PRF_IoGroup</designatorDropdown>
<uiIconScale>0.9</uiIconScale>
<tradeTags>
<li>Furniture</li>
</tradeTags>
<altitudeLayer>Building</altitudeLayer>
<passability>Impassable</passability>
<blockWind>true</blockWind>
<fillPercent>1.0</fillPercent>
<castEdgeShadows>true</castEdgeShadows>
<rotatable>false</rotatable>
<canOverlapZones>false</canOverlapZones>
<drawGUIOverlay>true</drawGUIOverlay>
<minifiedDef>MinifiedThing</minifiedDef>
<terrainAffordanceNeeded>Heavy</terrainAffordanceNeeded>
<staticSunShadowHeight>0.5</staticSunShadowHeight>
<surfaceType>Item</surfaceType>
<inspectorTabs>
<li>ITab_Storage</li>
<li>ProjectRimFactory.Storage.UI.ITab_Items</li>
</inspectorTabs>
<graphicData>
<graphicClass>Graphic_Single</graphicClass>
<texPath>Storage/CargoPlatform</texPath>
<drawSize>(3,3)</drawSize>
<damageData>
<cornerTL>Damage/Corner</cornerTL>
<cornerTR>Damage/Corner</cornerTR>
</damageData>
</graphicData>
<statBases>
<Mass>50</Mass>
<MaxHitPoints>450</MaxHitPoints>
<WorkToBuild>5000</WorkToBuild>
<MarketValue>10880</MarketValue>
<Flammability>0.05</Flammability>
<Beauty>0.5</Beauty>
</statBases>
<building>
<preventDeteriorationOnTop>true</preventDeteriorationOnTop>
<ignoreStoredThingsBeauty>true</ignoreStoredThingsBeauty>
<defaultStorageSettings>
<priority>Normal</priority>
<filter>
<categories>
<li>Manufactured</li>
<li>ResourcesRaw</li>
<li>Items</li>
<li>BuildingsArt</li>
<li>Weapons</li>
<li>Apparel</li>
<li>BodyParts</li>
</categories>
<specialFiltersToDisallow>
<li>AllowRotten</li> <!-- Disallow rotten by default -->
</specialFiltersToDisallow>
</filter>
</defaultStorageSettings>
</building>
<size>(3,3)</size>
<pathCost>150</pathCost>
<costList>
<Steel>300</Steel>
<Gold>50</Gold>
<Uranium>20</Uranium>
<Plasteel>100</Plasteel>
<ComponentIndustrial>100</ComponentIndustrial>
<ComponentSpacer>15</ComponentSpacer>
<PRF_MachineFrame_III>1</PRF_MachineFrame_III>
<PRF_ElectronicChip_II>1</PRF_ElectronicChip_II>
</costList>
<tickerType>Normal</tickerType>
<researchPrerequisites>
<li>PRF_StorageIO</li>
</researchPrerequisites>
<comps>
<li Class="CompProperties_Glower">
<glowRadius>6</glowRadius>
<glowColor>(115,198,206,0)</glowColor>
</li>
<li Class="CompProperties_Power">
<compClass>CompPowerTrader</compClass>
<shortCircuitInRain>true</shortCircuitInRain>
<basePowerConsumption>0</basePowerConsumption>
</li>
<li Class="ProjectRimFactory.Common.CompProperties_CompOutputAdjustable"/>
<li Class="CompProperties_Flickable"/>
</comps>
<constructionSkillPrerequisite>10</constructionSkillPrerequisite>
<modExtensions>
<li Class="ProjectRimFactory.Storage.Editables.DefModExtension_CanUseStorageIOPorts" />
<li Class="ProjectRimFactory.Storage.Editables.DefModExtension_Crate">
<limit>10000</limit>
<destroyContainsItems>true</destroyContainsItems>
<hideItems>true</hideItems>
<forbidPawnAccess>false</forbidPawnAccess>
<hideRightClickMenus>true</hideRightClickMenus>
</li>
</modExtensions>
</ThingDef>

<!--============================== I/O Port ==============================-->
<ThingDef ParentName="PRF_BuildingBase">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,11 @@ public override void SpawnSetup(Map map, bool respawningAfterLoad) {
}
// already set, but just in case:
this.products = thingOwnerInt.InnerListForReading;
map.GetComponent<PRFMapComponent>().RegisterIHideItemPos(this.Position, this);
}

public override void DeSpawn(DestroyMode mode = DestroyMode.Vanish)
{
var targets = AllNearbyLinkables().ToList();
this.Map.GetComponent<PRFMapComponent>().DeRegisterIHideItemPos(this.Position,this);
base.DeSpawn(mode);

targets.ForEach(x => x.Unlink(this));
Expand Down
123 changes: 109 additions & 14 deletions Source/ProjectRimFactory/Common/ConditionalPatchHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,130 @@
using Verse;
using HarmonyLib;
using Verse.AI;
using ProjectRimFactory.Storage;
using System.Reflection;

namespace ProjectRimFactory.Common
{
public static class ConditionalPatchHelper
{
public class TogglePatch
{
private bool Patched = false;

private readonly MethodInfo base_m;
private readonly HarmonyMethod trans_hm = null;
private readonly HarmonyMethod pre_hm = null;
private readonly HarmonyMethod post_hm = null;
private readonly MethodInfo trans_m = null;
private readonly MethodInfo pre_m = null;
private readonly MethodInfo post_m = null;

public TogglePatch(MethodInfo base_method, MethodInfo prefix = null, MethodInfo postfix = null, MethodInfo Transpiler = null)
{
base_m = base_method;
if (Transpiler != null) trans_hm = new HarmonyMethod(Transpiler);
if (prefix != null) pre_hm = new HarmonyMethod(prefix);
if (postfix != null) post_hm = new HarmonyMethod(postfix);
trans_m = Transpiler;
pre_m = prefix;
post_m = postfix;
}

public void PatchHandler(bool patch)
{
if (patch && !Patched)
{
harmony_instance.Patch(base_m,pre_hm,post_hm,trans_hm);
Patched = true;
}
else if (Patched && !patch)
{
if (trans_m != null) harmony_instance.Unpatch(base_m, trans_m);
if (pre_m != null) harmony_instance.Unpatch(base_m, pre_m);
if (post_m != null) harmony_instance.Unpatch(base_m, post_m);
Patched = false;
}
}

}

//conditional
private static Harmony harmony_instance = null;

private static bool Patch_Reachability_CanReach = false;
public static TogglePatch Patch_Reachability_CanReach = new TogglePatch(
AccessTools.Method(typeof(Verse.Reachability), "CanReach", new Type[] { typeof(IntVec3), typeof(LocalTargetInfo), typeof(PathEndMode), typeof(TraverseParms) }),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_Reachability_CanReach), "Prefix")
);

//Storage Patches
public static TogglePatch Patch_MinifiedThing_Print = new TogglePatch(
AccessTools.Method(typeof(RimWorld.MinifiedThing), "Print", new Type[] { typeof(SectionLayer)}),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_MinifiedThing_Print), "Prefix")
);
public static TogglePatch Patch_Thing_Print = new TogglePatch(
AccessTools.Method(typeof(Verse.Thing), "Print", new Type[] { typeof(SectionLayer) }),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_Thing_Print), "Prefix")
);
public static TogglePatch Patch_ThingWithComps_Draw = new TogglePatch(
AccessTools.Method(typeof(Verse.ThingWithComps), "Print", new Type[] { typeof(SectionLayer) }),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_ThingWithComps_Draw), "Prefix")
);
public static TogglePatch Patch_ThingWithComps_DrawGUIOverlay = new TogglePatch(
AccessTools.Method(typeof(Verse.ThingWithComps), "DrawGUIOverlay"),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_ThingWithComps_DrawGUIOverlay), "Prefix")
);
public static TogglePatch Patch_Thing_DrawGUIOverlay = new TogglePatch(
AccessTools.Method(typeof(Verse.ThingWithComps), "DrawGUIOverlay"),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_Thing_DrawGUIOverlay), "Prefix")
);
public static TogglePatch Patch_FloatMenuMakerMap_ChoicesAtFor = new TogglePatch(
AccessTools.Method(typeof(RimWorld.FloatMenuMakerMap), "ChoicesAtFor", new Type[] { typeof(UnityEngine.Vector3), typeof(Pawn), typeof(bool) }),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_FloatMenuMakerMap_ChoicesAtFor), "Prefix")
);
public static TogglePatch Patch_Building_Storage_Accepts = new TogglePatch(
AccessTools.Method(typeof(RimWorld.Building_Storage), "Accepts", new Type[] { typeof(Verse.Thing)}),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_Building_Storage_Accepts), "Prefix")
);
public static TogglePatch Patch_ForbidUtility_IsForbidden = new TogglePatch(
AccessTools.Method(typeof(RimWorld.ForbidUtility), "IsForbidden", new Type[] { typeof(Thing), typeof(Pawn) }),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_ForbidUtility_IsForbidden), "Prefix")
);





public static void InitHarmony(Harmony harmony)
{
harmony_instance = harmony;
}

public static void Update_Patch_Reachability_CanReach()
static List<Building_MassStorageUnit> building_MassStorages = new List<Building_MassStorageUnit>();

private static void updatePatchStorage()
{
var patch = AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_Reachability_CanReach), "Prefix");
var base_m = AccessTools.Method(typeof(Verse.Reachability), "CanReach", new Type[] { typeof(IntVec3), typeof(LocalTargetInfo), typeof(PathEndMode), typeof(TraverseParms) });
if (ProjectRimFactory_ModSettings.PRF_Patch_Reachability_CanReach)
{
harmony_instance.Patch(base_m, new HarmonyMethod(patch) );
Patch_Reachability_CanReach = true;
}
else if(Patch_Reachability_CanReach)
{
harmony_instance.Unpatch(base_m, patch);
Patch_Reachability_CanReach = false;
}
bool state = building_MassStorages.Count > 0;

Patch_MinifiedThing_Print.PatchHandler(state);
Patch_Thing_Print.PatchHandler(state);
Patch_ThingWithComps_Draw.PatchHandler(state);
Patch_ThingWithComps_DrawGUIOverlay.PatchHandler(state);
Patch_Thing_DrawGUIOverlay.PatchHandler(state);
Patch_FloatMenuMakerMap_ChoicesAtFor.PatchHandler(state);
Patch_Building_Storage_Accepts.PatchHandler(state);
Patch_ForbidUtility_IsForbidden.PatchHandler(state);
}

public static void Register(Building_MassStorageUnit building)
{
building_MassStorages.Add(building);
updatePatchStorage();
}
public static void Deregister(Building_MassStorageUnit building)
{
building_MassStorages.Remove(building);
updatePatchStorage();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static bool CanMoveItem(Building_AdvancedStorageUnitIOPort port, Thing th
/// <returns></returns>
public static bool CanMoveItem(Building_AdvancedStorageUnitIOPort port, IntVec3 thingPos)
{
return port.boundStorageUnit?.AllSlotCells()?.Contains(thingPos) ?? false;
return port.boundStorageUnit?.HoldsPos(thingPos) ?? false;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

namespace ProjectRimFactory.Common.HarmonyPatches
{
[HarmonyPatch(typeof(ForbidUtility), "IsForbidden", new Type[] { typeof(Thing), typeof(Pawn) })]
class Patch_ForbidUtility_IsForbidden
{
static bool Prefix(Thing t, Pawn pawn, out bool __result)
Expand All @@ -33,7 +32,6 @@ static bool Prefix(Thing t, Pawn pawn, out bool __result)
}
}

[HarmonyPatch(typeof(Building_Storage), "Accepts")]
class Patch_Building_Storage_Accepts
{
static bool Prefix(Building_Storage __instance, Thing t, out bool __result)
Expand All @@ -50,7 +48,6 @@ static bool Prefix(Building_Storage __instance, Thing t, out bool __result)
}
}

[HarmonyPatch(typeof(FloatMenuMakerMap), "ChoicesAtFor")]
class Patch_FloatMenuMakerMap_ChoicesAtFor
{
static bool Prefix(Vector3 clickPos, Pawn pawn, out List<FloatMenuOption> __result)
Expand All @@ -65,7 +62,6 @@ static bool Prefix(Vector3 clickPos, Pawn pawn, out List<FloatMenuOption> __resu
}
}

[HarmonyPatch(typeof(Thing), "DrawGUIOverlay")]
class Patch_Thing_DrawGUIOverlay
{
static bool Prefix(Thing __instance)
Expand All @@ -81,7 +77,6 @@ static bool Prefix(Thing __instance)
}
}

[HarmonyPatch(typeof(ThingWithComps), "DrawGUIOverlay")]
class Patch_ThingWithComps_DrawGUIOverlay
{
static bool Prefix(Thing __instance)
Expand All @@ -97,7 +92,6 @@ static bool Prefix(Thing __instance)
}
}

[HarmonyPatch(typeof(ThingWithComps), "Draw")]
class Patch_ThingWithComps_Draw
{
static bool Prefix(Thing __instance)
Expand All @@ -113,7 +107,6 @@ static bool Prefix(Thing __instance)
}
}

[HarmonyPatch(typeof(Thing), "Print")]
class Patch_Thing_Print
{
static bool Prefix(Thing __instance, SectionLayer layer)
Expand All @@ -129,7 +122,6 @@ static bool Prefix(Thing __instance, SectionLayer layer)
}
}

[HarmonyPatch(typeof(MinifiedThing), "Print")]
class Patch_MinifiedThing_Print
{
static bool Prefix(Thing __instance, SectionLayer layer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static bool Prefix(IntVec3 start, LocalTargetInfo dest, PathEndMode peMod
if (mapcomp.ShouldHideItemsAtPos(ThingPos))
{
//Is in a DSU
var pathToIO = mapcomp.GetadvancedIOLocations.Where(p => p.Value.boundStorageUnit?.Position == ThingPos && __instance.CanReach(start, p.Key, PathEndMode.Touch, traverseParams)).Any();
var pathToIO = mapcomp.GetadvancedIOLocations.Where(p => p.Value.boundStorageUnit?.GetPosition == ThingPos && __instance.CanReach(start, p.Key, PathEndMode.Touch, traverseParams)).Any();
if (pathToIO)
{
__result = true;
Expand Down
2 changes: 2 additions & 0 deletions Source/ProjectRimFactory/Common/PRFGameComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class PRFGameComponent : GameComponent {
public static Pawn PRF_StaticPawn = null;
public static Job PRF_StaticJob = null;



public static void GenStaticPawn()
{
PRF_StaticPawn = PawnGenerator.GeneratePawn(PRFDefOf.PRFSlavePawn, Faction.OfPlayer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ProjectRimFactory_ModComponent(ModContentPack content) : base(content)
availableSpecialSculptures = SpecialSculpture.LoadAvailableSpecialSculptures(content);
LoadModSupport();
ConditionalPatchHelper.InitHarmony(this.HarmonyInstance);
ConditionalPatchHelper.Update_Patch_Reachability_CanReach();
ConditionalPatchHelper.Patch_Reachability_CanReach.PatchHandler(ProjectRimFactory_ModSettings.PRF_Patch_Reachability_CanReach);

}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static void CSharpSettings(Listing_Standard list) {
list.Gap();
if (PRF_Patch_Reachability_CanReach != PRF_Patch_Reachability_CanReach_last)
{
ConditionalPatchHelper.Update_Patch_Reachability_CanReach();
ConditionalPatchHelper.Patch_Reachability_CanReach.PatchHandler(ProjectRimFactory_ModSettings.PRF_Patch_Reachability_CanReach);
}
PRF_Patch_Reachability_CanReach_last = PRF_Patch_Reachability_CanReach;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ private Thing GetstoredItem()

public bool CanGetNewItem => GetstoredItem() == null && (powerComp?.PowerOn ?? false);

public override bool IsAdvancedPort => true;

private void updateQueue()
{
if (CanGetNewItem && placementQueue.Count > 0)
Expand Down
Loading

0 comments on commit 24b3597

Please sign in to comment.