Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added WealthWatcher Patch (may be toggeld)
Added support for Orbital Trading
Fixed issue when creating ThingOwner
Fixed Typos in Conditional Patches
Fixed Null ref issue for DSU
Moved code to better locations
  • Loading branch information
Sn1p3rr3c0n committed Sep 4, 2022
1 parent c859320 commit 647622e
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 81 deletions.
2 changes: 2 additions & 0 deletions Languages/English/Keyed/AllKeyed_Settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<PRF_Settings_C_Patches_Header>Patches</PRF_Settings_C_Patches_Header>
<PRF_Settings_C_Patches_Reachability_CanReach_ToolTip>Toggels the Advanced IO CanReach Patch\nWhen checked the Patch is active and allowes Pawns to reach DSU contents using Advanced IO Ports even if the Path to the DSU is blocked\nPatch is not active by default as it may cause spikes and is only usefull in some cases.\n(ProjectRimFactory.Common.HarmonyPatches.Patch_Reachability_CanReach:Prefix)</PRF_Settings_C_Patches_Reachability_CanReach_ToolTip>
<PRF_Settings_C_Patches_Reachability_CanReach>Advanced IO CanReach</PRF_Settings_C_Patches_Reachability_CanReach>
<PRF_Settings_C_Patches_WealthWatcher_CalculateWealthItems_ToolTip>Toggels the Cold Storage WealthWatcher Patch\nWhen checked the Patch is active and items in Coldstorage contribute to Colony Wealth.\nFor most Players we reccomend this to be on.\nPeformence load on this is ~1ms per 1k Things stored. This runs every 5k Ticks\n(ProjectRimFactory.Common.HarmonyPatches.Patch_WealthWatcher_CalculateWealthItems:Postfix)</PRF_Settings_C_Patches_WealthWatcher_CalculateWealthItems_ToolTip>
<PRF_Settings_C_Patches_WealthWatcher_CalculateWealthItems>Cold Storage WealthWatcher</PRF_Settings_C_Patches_WealthWatcher_CalculateWealthItems>

<PRF.Common.ForbidOnPlacingDefault>Forbid items on Placment</PRF.Common.ForbidOnPlacingDefault>
<PRF.Common.ForbidOnPlacingDefaultDesc>Toggle this to forbid items on placement</PRF.Common.ForbidOnPlacingDefaultDesc>
Expand Down
10 changes: 8 additions & 2 deletions Source/ProjectRimFactory/Common/ConditionalPatchHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public void PatchHandler(bool patch)
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_Reachability_CanReach), "Prefix")
);

public static TogglePatch Patch_WealthWatcher_CalculateWealthItems = new TogglePatch(
AccessTools.Method(typeof(RimWorld.WealthWatcher), "CalculateWealthItems"),
null,
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_WealthWatcher_CalculateWealthItems), "Postfix")
);

//Storage Patches
public static TogglePatch Patch_MinifiedThing_Print = new TogglePatch(
AccessTools.Method(typeof(RimWorld.MinifiedThing), "Print", new Type[] { typeof(SectionLayer)}),
Expand All @@ -72,15 +78,15 @@ public void PatchHandler(bool patch)
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(Verse.ThingWithComps), "Draw"),
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(Verse.Thing), "DrawGUIOverlay"),
AccessTools.Method(typeof(ProjectRimFactory.Common.HarmonyPatches.Patch_Thing_DrawGUIOverlay), "Prefix")
);
public static TogglePatch Patch_FloatMenuMakerMap_ChoicesAtFor = new TogglePatch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static void Postfix(Map map, ref IEnumerable<Thing> __result)
{
HashSet<Thing> yieldedThings = new HashSet<Thing>();
yieldedThings.AddRange<Thing>(__result);
foreach (Building_MassStorageUnitPowered dsu in Building_MassStorageUnitPowered.AllPowered(map))
foreach (ILinkableStorageParent dsu in TradePatchHelper.AllPowered(map))
{
yieldedThings.AddRange<Thing>(dsu.StoredItems);
}
Expand Down Expand Up @@ -89,7 +89,7 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
yield return new CodeInstruction(OpCodes.Call, AccessTools.PropertyGetter(typeof(PassingShip), "Map"));
//Call --> Building_MassStorageUnitPowered.AnyPowerd with the above as an argument
yield return new CodeInstruction(OpCodes.Call, HarmonyLib.AccessTools
.Method(typeof(Building_MassStorageUnitPowered) ,nameof(Building_MassStorageUnitPowered.AnyPowerd), new[] { typeof(Map)} ));
.Method(typeof(TradePatchHelper) ,nameof(TradePatchHelper.AnyPowerd), new[] { typeof(Map)} ));
yield return new CodeInstruction(OpCodes.Brtrue_S, instruction.operand);
continue;

Expand All @@ -103,6 +103,31 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
}
}

public static class TradePatchHelper
{
public static bool AnyPowerd(Map map)
{
return AllPowered(map,true).Any();
}

public static IEnumerable<ILinkableStorageParent> AllPowered(Map map, bool any = false)
{
foreach (ILinkableStorageParent item in map.listerBuildings.AllBuildingsColonistOfClass<Building_MassStorageUnitPowered>())
{
if (item.Powered)
{
yield return item;
if (any) break;
}
}
var cs = PatchStorageUtil.GetPRFMapComponent(map).ColdStorageBuildings.Select(b => b as ILinkableStorageParent);
foreach (var item in cs)
{
yield return item;
}
}
}




Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RimWorld;
using HarmonyLib;
using Verse;
using ProjectRimFactory.Storage;
using System.Reflection.Emit;
using System.Reflection;

namespace ProjectRimFactory.Common.HarmonyPatches
{

[HarmonyPatch(typeof(TradeDeal), "InSellablePosition")]
class Patch_TradeDeal_InSellablePosition
{
public static bool Prefix(Thing t, out string reason, ref bool __result)
{
if (!t.Spawned)
{
var buildings = PatchStorageUtil.GetPRFMapComponent(t.MapHeld).ColdStorageBuildings;
foreach(var building in buildings)
{
if (building.StoredItems.Contains(t))
{
reason = null;
__result = true;
return false;
}
}
}

reason = null;
return true;
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectRimFactory.Common.HarmonyPatches
{
/// <summary>
/// Patch to ensure Items in Cold Storage Contribute to Wealth
/// 1k Items ~ 1ms (every 5k Ticks)
/// </summary>
class Patch_WealthWatcher_CalculateWealthItems
{

public static void Postfix(Verse.Map ___map, ref float __result)
{
var buildings = PatchStorageUtil.GetPRFMapComponent(___map).ColdStorageBuildings;
var cnt = buildings.Count;
for(int i = 0; i < cnt; i++)
{
var building = buildings[i];
__result += building.GetItemWealth();
}

}
}
}
20 changes: 20 additions & 0 deletions Source/ProjectRimFactory/Common/PRFMapComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class PRFMapComponent : MapComponent
// iHideRightMenus: see HarmonyPatches/PatchStorage.cs
public HashSet<IntVec3> iHideRightMenus = new HashSet<IntVec3>();

public List<Storage.Building_ColdStorage> ColdStorageBuildings = new List<Storage.Building_ColdStorage>();

private Dictionary<IntVec3,List< HarmonyPatches.IHideItem>> hideItemLocations = new Dictionary<IntVec3,List< HarmonyPatches.IHideItem>>();

private Dictionary<IntVec3, List<HarmonyPatches.IForbidPawnOutputItem>> ForbidPawnOutputItemLocations = new Dictionary<IntVec3, List<HarmonyPatches.IForbidPawnOutputItem>>();
Expand All @@ -23,6 +25,24 @@ public class PRFMapComponent : MapComponent

public Dictionary<IntVec3, ProjectRimFactory.Storage.Building_AdvancedStorageUnitIOPort> GetadvancedIOLocations => advancedIOLocations;

public void RegisterColdStorageBuilding(ProjectRimFactory.Storage.Building_ColdStorage port)
{
if (!ColdStorageBuildings.Contains(port))
{
ColdStorageBuildings.Add(port);
}
}
public void DeRegisterColdStorageBuilding(ProjectRimFactory.Storage.Building_ColdStorage port)
{
if (ColdStorageBuildings.Contains(port))
{
ColdStorageBuildings.Remove(port);
}

}



public void RegisteradvancedIOLocations(IntVec3 pos, ProjectRimFactory.Storage.Building_AdvancedStorageUnitIOPort port)
{
if (!advancedIOLocations.ContainsKey(pos))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public ProjectRimFactory_ModComponent(ModContentPack content) : base(content)
LoadModSupport();
ConditionalPatchHelper.InitHarmony(this.HarmonyInstance);
ConditionalPatchHelper.Patch_Reachability_CanReach.PatchHandler(ProjectRimFactory_ModSettings.PRF_Patch_Reachability_CanReach);
ConditionalPatchHelper.Patch_Reachability_CanReach.PatchHandler(ProjectRimFactory_ModSettings.PRF_Patch_WealthWatcher_CalculateWealthItems);

}
catch (Exception ex)
Expand Down
20 changes: 13 additions & 7 deletions Source/ProjectRimFactory/Common/ProjectRimFactory_ModSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,18 @@ private static void CSharpSettings(Listing_Standard list) {
}
TooltipHandler.TipRegion(rect, "PRF_Settings_C_Patches_Reachability_CanReach_ToolTip".Translate());
Widgets.CheckboxLabeled(rect, "PRF_Settings_C_Patches_Reachability_CanReach".Translate(), ref PRF_Patch_Reachability_CanReach);
list.Gap();
if (PRF_Patch_Reachability_CanReach != PRF_Patch_Reachability_CanReach_last)
list.Gap();
ConditionalPatchHelper.Patch_Reachability_CanReach.PatchHandler(ProjectRimFactory_ModSettings.PRF_Patch_Reachability_CanReach);

rect = list.GetRect(20);
if (Mouse.IsOver(rect))
{
ConditionalPatchHelper.Patch_Reachability_CanReach.PatchHandler(ProjectRimFactory_ModSettings.PRF_Patch_Reachability_CanReach);
}
PRF_Patch_Reachability_CanReach_last = PRF_Patch_Reachability_CanReach;
Widgets.DrawHighlight(rect);
}
TooltipHandler.TipRegion(rect, "PRF_Settings_C_Patches_WealthWatcher_CalculateWealthItems_ToolTip".Translate());
Widgets.CheckboxLabeled(rect, "PRF_Settings_C_Patches_WealthWatcher_CalculateWealthItems".Translate(), ref PRF_Patch_WealthWatcher_CalculateWealthItems);
list.Gap();
ConditionalPatchHelper.Patch_Reachability_CanReach.PatchHandler(ProjectRimFactory_ModSettings.PRF_Patch_WealthWatcher_CalculateWealthItems);

}

Expand All @@ -104,7 +110,7 @@ private static ContainerRow ParseSettingRows(ModContentPack content)
public static bool PRF_LiteMode = false;
private static bool PRF_LiteMode_last = false;
public static bool PRF_Patch_Reachability_CanReach = false;
private static bool PRF_Patch_Reachability_CanReach_last = false;
public static bool PRF_Patch_WealthWatcher_CalculateWealthItems = true;

public override void ExposeData()
{
Expand All @@ -113,8 +119,8 @@ public override void ExposeData()
Scribe_Values.Look<Debug.Flag>(ref Debug.activeFlags, "debugFlags", 0);
Scribe_Values.Look(ref PRF_LiteMode, "PRF_LiteMode", false);
Scribe_Values.Look(ref PRF_Patch_Reachability_CanReach, "PRF_Patch_Reachability_CanReach", false);
Scribe_Values.Look(ref PRF_Patch_WealthWatcher_CalculateWealthItems, "PRF_Patch_WealthWatcher_CalculateWealthItems", true);
PRF_LiteMode_last = PRF_LiteMode;
PRF_Patch_Reachability_CanReach_last = PRF_Patch_Reachability_CanReach;
}

public void DoWindowContents(Rect inRect)
Expand Down
67 changes: 18 additions & 49 deletions Source/ProjectRimFactory/Storage/Building_ColdStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,12 @@

namespace ProjectRimFactory.Storage
{
public interface ILinkableStorageParent
{
public List<Thing> StoredItems { get; }

public bool AdvancedIOAllowed { get; }

public void HandleNewItem(Thing item);

public void HandleMoveItem(Thing item);

public bool CanReciveThing(Thing item);

public bool HoldsPos(IntVec3 pos);

//What is that even for ??
public void DeregisterPort(Building_StorageUnitIOBase port);
public void RegisterPort(Building_StorageUnitIOBase port);

public StorageSettings GetSettings { get; }

public IntVec3 GetPosition { get; }

public string LabelCap { get; }
public bool CanReceiveIO { get; }
public Map Map { get; }

public int StoredItemsCount { get; }

public string GetITabString(int itemsSelected);

public LocalTargetInfo GetTargetInfo { get; }

public bool OutputItem(Thing item);

public bool Powered { get; }

public bool CanUseIOPort { get; }

}


[StaticConstructorOnStartup]
public abstract class Building_ColdStorage : Building, IRenameBuilding, IHaulDestination, IStoreSettingsParent, ILinkableStorageParent, IThingHolder
{
private static readonly Texture2D RenameTex = ContentFinder<Texture2D>.Get("UI/Buttons/Rename");

protected ThingOwner<Thing> thingOwner = new ThingOwner<Thing>();
protected ThingOwner<Thing> thingOwner;

private List<Thing> items => thingOwner.InnerListForReading;

Expand Down Expand Up @@ -199,25 +158,22 @@ public override string GetInspectString()

public override void DeSpawn(DestroyMode mode = DestroyMode.Vanish)
{
var thingsToSplurge = new List<Thing>(Position.GetThingList(Map));
var thingsToSplurge = items;
for (var i = 0; i < thingsToSplurge.Count; i++)
if (thingsToSplurge[i].def.category == ThingCategory.Item)
{
thingsToSplurge[i].DeSpawn();
//thingsToSplurge[i].DeSpawn();
GenPlace.TryPlaceThing(thingsToSplurge[i], Position, Map, ThingPlaceMode.Near);
}
PatchStorageUtil.GetPRFMapComponent(Map).DeRegisterColdStorageBuilding(this);
base.DeSpawn(mode);
}

public override void SpawnSetup(Map map, bool respawningAfterLoad)
{
base.SpawnSetup(map, respawningAfterLoad);
PatchStorageUtil.GetPRFMapComponent(Map).RegisterColdStorageBuilding(this);
ModExtension_Crate ??= def.GetModExtension<DefModExtension_Crate>();
/*Log.Message("----------------");
foreach (var item in this.StoredItems)
{
Log.Message(item.ToString());
}*/
foreach (var port in ports)
{
if (port?.Spawned ?? false)
Expand All @@ -231,6 +187,19 @@ public override void SpawnSetup(Map map, bool respawningAfterLoad)

}

public float GetItemWealth()
{
float output = 0;
var itemsc = items.Count;
for (int i = 0;i< itemsc; i++)
{
var item = items[i];
output += item.MarketValue * item.stackCount;
}

return output;
}

public override void DrawGUIOverlay()
{
base.DrawGUIOverlay();
Expand Down
10 changes: 10 additions & 0 deletions Source/ProjectRimFactory/Storage/Building_ColdStoragePowerd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,15 @@ public static IEnumerable<Building_MassStorageUnitPowered> AllPowered(Map map)
}
}

public override void SpawnSetup(Map map, bool respawningAfterLoad)
{
base.SpawnSetup(map, respawningAfterLoad);
}

public override void PostMake()
{
base.PostMake();
thingOwner ??= new ThingOwner<Thing>(this);
}
}
}
Loading

0 comments on commit 647622e

Please sign in to comment.