Skip to content

Commit

Permalink
Merge pull request #24 from DoctorVanGogh/master
Browse files Browse the repository at this point in the history
bugfixes
  • Loading branch information
Skullywag authored Aug 21, 2017
2 parents 8224df2 + 2d94d62 commit e9af45f
Show file tree
Hide file tree
Showing 19 changed files with 232 additions and 98 deletions.
Binary file modified Assemblies/0Harmony.dll
Binary file not shown.
Binary file modified Assemblies/ExtendedStorage.dll
Binary file not shown.
Binary file added Assemblies/_harmonycheck.dll
Binary file not shown.
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
# Version 2.5(???)
# Version 3.0.1


## Bugfixes
- Changes to storage priority correctly trigger hauling jobs to & from Extended Storage buildings
- Correctly update total item count label when partial stack is used from output slot.
- Move single non-max stacks from input cell to output.
- Fix error for reinstalled non empty Extended Storage buildings.
- Disallowing & reallowing a stored item while paused will no longer eject the stored item from storage after unpause.

## New features
- Skip now officially supports storing stone chunks
- (Debug) options in God mode (Allow switching displayed filter between User & Storage settings. User settings are default, storage settings are what is actually currently stored in the building).
- Upgrade Harmony to 1.0.9.1

# Version 3.0

_Upgrading to this version in an existing savegame is fully supported._

Expand Down
1 change: 1 addition & 0 deletions Defs/ThingDefs/Storage/Storage_Skip.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<filter>
<categories>
<li>ResourcesRaw</li>
<li>StoneChunks</li>
</categories>
</filter>
</fixedStorageSettings>
Expand Down
102 changes: 57 additions & 45 deletions Source/ExtendedStorage/Building_ExtendedStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@

namespace ExtendedStorage
{
public class Building_ExtendedStorage : Building_Storage
public interface IUserSettingsOwner : IStoreSettingsParent
{
void Notify_UserSettingsChanged();
}


public class Building_ExtendedStorage : Building_Storage, IUserSettingsOwner {
#region fields

internal Graphic _gfxStoredThing;
private string _label;

private ThingDef _storedThingDef;
internal Thing _suppressedDrawCandidate;

private Func<IEnumerable<Gizmo>> Building_GetGizmos;
private IntVec3 inputSlot;
Expand All @@ -27,23 +31,18 @@ public class Building_ExtendedStorage : Building_Storage
private Action queuedTickAction;
internal string label;

public StorageSettings userSettings;

internal static Action<ThingFilter, Action> SetSettingsChanged;
public UserSettings userSettings;

#endregion

static Building_ExtendedStorage()
{
SetSettingsChanged = Access.GetFieldSetter<ThingFilter, Action>("settingsChangedCallback");
}


#region Properties

public bool AtCapacity => StoredThingTotal >= ApparentMaxStorage;

public int ApparentMaxStorage => (int) (StoredThingDef?.stackLimit*this.GetStatValue(DefReferences.Stat_ES_StorageFactor) ?? Int32.MaxValue);
public int ApparentMaxStorage => StoredThingDef == null
? Int32.MaxValue
: (int) (StoredThingDef.stackLimit*this.GetStatValue(DefReferences.Stat_ES_StorageFactor));

public IntVec3 OutputSlot => outputSlot;

Expand Down Expand Up @@ -85,7 +84,6 @@ public IEnumerable<Thing> StoredThings

public override string LabelNoCount => label;


#endregion

#region Base overrides
Expand All @@ -107,38 +105,27 @@ public override void DrawGUIOverlay()
GenMapUI.DrawThingLabel(StoredThings.First(), _label, labelColor);
}

public override void Draw()
{
base.Draw();
if (true == StoredThingDef?.IsApparel)
return;

_gfxStoredThing?.DrawFromDef(
Gen.TrueCenter(OutputSlot, Rot4.North, IntVec2.One, Altitudes.AltitudeFor(AltitudeLayer.Item)),
Rot4.North,
StoredThingDef);
}

public override void ExposeData()
{
base.ExposeData();
Scribe_Defs.Look(ref _storedThingDef, "storedThingDef");
Scribe_Deep.Look(ref userSettings, "userSettings");
Scribe_Deep.Look(ref userSettings, "userSettings", this);

if (Scribe.mode != LoadSaveMode.Saving || this.label != null) {
Scribe_Values.Look<string>(ref label, "label", def.label, false);
}

// we need to re-apply our callback on the userSettings after load.
// in addition, we need some migration code for handling mid-save upgrades.
// todo: the migration part of this can be removed on the A17 update.
if (Scribe.mode == LoadSaveMode.PostLoadInit)
{
// migration
if (userSettings == null)
{
// create 'user' storage settings
userSettings = new StorageSettings(this);

// copy over previous filter/priority
userSettings.filter.CopyAllowancesFrom(settings.filter);
userSettings.Priority = settings.Priority;

// apply currently stored logic
Notify_StoredThingDefChanged();
}

// re-apply callback
SetSettingsChanged(userSettings.filter, Notify_UserSettingsChanged);
}
}

public override IEnumerable<Gizmo> GetGizmos()
Expand Down Expand Up @@ -204,14 +191,11 @@ public override void PostMake()
base.PostMake();

// create 'user' storage settings
userSettings = new StorageSettings(this);
userSettings = new UserSettings(this);

// copy over default filter/priority
if (def.building.defaultStorageSettings != null)
userSettings.CopyFrom(def.building.defaultStorageSettings);

// change callback to point to our custom logic
SetSettingsChanged(userSettings.filter, Notify_UserSettingsChanged);
}


Expand Down Expand Up @@ -269,6 +253,8 @@ public override void Tick()
base.Tick();
if (this.IsHashIntervalTick(10))
{
TryGrabOutputItem();

queuedTickAction?.Invoke();
queuedTickAction = null;

Expand All @@ -278,12 +264,22 @@ public override void Tick()
}
}

private void TryGrabOutputItem()
{
if (StoredThingDef == null)
{
StoredThingDef = Find.VisibleMap.thingGrid.ThingsAt(outputSlot).Where(userSettings.AllowedToAccept).FirstOrDefault()?.def;
InvalidateThingSection(_storedThingDef);
}
}

#endregion

#region Notification handlers

public void Notify_UserSettingsChanged()
{

// the vanilla StorageSettings.TryNotifyChanged will alert the SlotGroupManager that
// storage settings have changed. We don't need this behaviour for user settings, as these
// don't directly influence the slotgroup, and any changes we make are propagated to the
Expand All @@ -303,7 +299,24 @@ public void Notify_UserSettingsChanged()
else
{
TryUnstackStoredItems();
var storedDef = StoredThingDef;
StoredThingDef = null;
InvalidateThingSection(storedDef);
}
}

/// <summary>
/// Checks if the storedDef has a mapMesh painting - if so, invalidate the apppropriate SectionLayer (needed for
/// chunks to appear immediately while game is paused &amp; exclusion by filter)
/// </summary>
private void InvalidateThingSection(ThingDef storedDef)
{
switch (storedDef?.drawerType)
{
case DrawerType.MapMeshOnly:
case DrawerType.MapMeshAndRealTime:
Map?.mapDrawer.SectionAt(OutputSlot).RegenerateLayers(MapMeshFlag.Things);
break;
}
}

Expand Down Expand Up @@ -460,6 +473,7 @@ private IEnumerable<Thing> SplurgeThings(IEnumerable<Thing> things, IntVec3 cent
private void TryMoveItem()
{
Thing input = StoredThingAtInput;

if (input == null)
return;

Expand Down Expand Up @@ -500,9 +514,9 @@ internal void TrySplurgeStoredItems()

SplurgeThings(storedThings, outputSlot, true);
SoundDef.Named("DropPodOpen").PlayOneShot(new TargetInfo(outputSlot, Map, false));
StoredThingDef = null;
}


/// <remarks>
/// we can't really dump items immediately - otherwise typical use scenarios like "clear all, reselect X" would dump items immediately
/// </remarks>
Expand All @@ -519,12 +533,13 @@ private void TryUnstackStoredItems()
if (validThings.Length != 0)
SoundDef.Named("DropPodOpen").PlayOneShot(new TargetInfo(outputSlot, Map, false));
};

}

/// <summary>
/// Update necessary data for label &amp; icon overrides
/// </summary>
private void UpdateCachedAttributes()
public void UpdateCachedAttributes()
{
if (StoredThingDef != null)
{
Expand Down Expand Up @@ -561,19 +576,16 @@ private void UpdateCachedAttributes()
_gfxStoredThing = (StoredThingDef.graphic as Graphic_StackCount)
?.SubGraphicForStackCount(Math.Min(total, StoredThingDef.stackLimit), StoredThingDef)
?? StoredThingDef.graphic;
_suppressedDrawCandidate = items[0];
}
else
{
_gfxStoredThing = null;
_suppressedDrawCandidate = null;
}
}
else
{
_label = null;
_gfxStoredThing = null;
_suppressedDrawCandidate = null;
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions Source/ExtendedStorage/ExtendedStorage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,18 @@
<DesignTime>True</DesignTime>
<DependentUpon>LanguageKeys.Generated.tt</DependentUpon>
</Compile>
<Compile Include="Patches\FloatMenuMakerMap_AddHumanlikeOrders.cs" />
<Compile Include="Patches\CompressibilityDeciderUtility_IsSaveCompressible.cs" />
<Compile Include="Patches\StorageSettings.cs" />
<Compile Include="Patches\Thing_SplitOff.cs" />
<Compile Include="Patches\[GUI]\FloatMenuMakerMap_AddHumanlikeOrders.cs" />
<Compile Include="Patches\ITab_Storage_FillTab.cs" />
<Compile Include="Patches\MinifyUtility_MakeMinified.cs" />
<Compile Include="Patches\Thing_DrawAt.cs" />
<Compile Include="Patches\[GUI]\Thing_DrawAt.cs" />
<Compile Include="Patches\Thing_SpawnSetup.cs" />
<Compile Include="Patches\Thing_DrawGUIOverlay.cs" />
<Compile Include="Patches\[GUI]\Thing_DrawGUIOverlay.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Dialog_Rename.cs" />
<Compile Include="Dialog_Rename.cs" />
<Compile Include="UserSettings.cs" />
<Compile Include="Utility\AccessUtility.cs" />
<Compile Include="Utility\EnumUtility.cs" />
<Compile Include="Utility\StorageUtility.cs" />
Expand Down
1 change: 1 addition & 0 deletions Source/ExtendedStorage/ExtendedStorage.csproj.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=languages/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=patches/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=patches_005C_005Bgui_005D/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=utility/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Reflection;
using Harmony;
using RimWorld;
using Verse;

namespace ExtendedStorage {

[HarmonyPatch(typeof(CompressibilityDeciderUtility), nameof(CompressibilityDeciderUtility.IsSaveCompressible))]
class CompressibilityDeciderUtility_IsSaveCompressible {

public static void Postfix(ref bool __result, Thing t)
{
__result = __result && !(t.GetSlotGroup()?.parent is Building_ExtendedStorage);
}
}
}
30 changes: 28 additions & 2 deletions Source/ExtendedStorage/Patches/ITab_Storage_FillTab.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System.Collections.Generic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using Harmony;
using RimWorld;
using UnityEngine;
using Verse;

namespace ExtendedStorage
{
Expand All @@ -11,6 +15,8 @@ public class ITab_Storage_FillTab
{
private static readonly PropertyInfo piSelStoreSettingsParent;

public static bool showStoreSettings = false;

static ITab_Storage_FillTab()
{
// accessor for private field
Expand Down Expand Up @@ -41,7 +47,7 @@ public static StorageSettings GetSettings(RimWorld.ITab_Storage tab, out IStoreS
{
ITab_Storage extended = tab as ITab_Storage;

if (extended == null)
if (extended == null || (DebugSettings.godMode && showStoreSettings))
{
parent = GetSelStoreSettingsParent(tab);
return parent.GetStoreSettings();
Expand All @@ -52,6 +58,26 @@ public static StorageSettings GetSettings(RimWorld.ITab_Storage tab, out IStoreS
return building?.userSettings;
}

/// <summary>
/// Add debug dropdown to toggle displayed settings to tab. Only visible in GodMode.
/// </summary>
public static void Postfix(RimWorld.ITab_Storage __instance) {
if (!DebugSettings.godMode || !(__instance is ITab_Storage))
return;

Rect rect = new Rect(160f+10f+5f, 10f, 100f, 29f);

Text.Font = GameFont.Tiny;
if (Widgets.ButtonText(rect, $"[Debug] {(showStoreSettings ? "Store" : "User")}", true, false, true)) {
List<FloatMenuOption> list = new List<FloatMenuOption>
{
new FloatMenuOption("User", () => { showStoreSettings = false; }),
new FloatMenuOption("Store", () => { showStoreSettings = true; })
};
Find.WindowStack.Add(new FloatMenu(list));
}
}


/// <remarks>
/// Changes the IL code so the patched method beginning is functionally changed from
Expand Down
Loading

0 comments on commit e9af45f

Please sign in to comment.