Skip to content

Commit

Permalink
upgarde harmony
Browse files Browse the repository at this point in the history
fix item splurge on load
compile for B19
  • Loading branch information
DoctorVanGogh committed Sep 9, 2018
1 parent 195be8f commit a7b47bb
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 8 deletions.
11 changes: 7 additions & 4 deletions About/About.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<ModMetaData>
<name>Extended Storage</name>
<author>Skullywag, Fluffy, spdskatr, DoctorVanGogh</author>
<targetVersion>1.0.1950</targetVersion>
<description>Adds additional storage buildings that multi stack items. Auto sort by Fluffy, Harmony by spdskatr</description>
<name>Extended Storage</name>
<targetVersion>0.19.2009</targetVersion>
<author>(see description)</author>
<description xml:whitespace="preserve">Adds additional storage buildings that multi stack items. Auto sort by Fluffy, Harmony by spdskatr

Authors: Skullywag, Fluffy, spdskatr, DoctorVanGogh
</description>
</ModMetaData>
Binary file modified Assemblies/0Harmony.dll
Binary file not shown.
Binary file modified Assemblies/ExtendedStorage.dll
Binary file not shown.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![RimWorld 1.0 unstable](https://img.shields.io/badge/RimWorld-1.0%20unstable-yellow.svg)](http://rimworldgame.com/)

# Extended Storage

[![RimWorld B19](https://img.shields.io/badge/RimWorld-B19-green.svg?style=popout-square)](http://rimworldgame.com/)

Adds additional storage buildings that multi stack items.

This works by having an input cell which the storage cell grabs from until it reaches its own internal limit. For example the pallet can hold an amount equal to 8 full stacks of steel in its storage slot and another stack can sit on the input slot, giving it total storage of 8+1 full stacks, other buildings have smaller and larger limits for certain resources, all controlled via thing categories ala stockpiles, pawns are aware of contents and will take from them as per normal.
Expand All @@ -10,4 +10,4 @@ No requirements just subscribe and go.

## Powered by Harmony

![Powered by Harmony](https://camo.githubusercontent.com/074bf079275fa90809f51b74e9dd0deccc70328f/68747470733a2f2f7332342e706f7374696d672e6f72672f3538626c31727a33392f6c6f676f2e706e67)
![Powered by Harmony](https://raw.githubusercontent.com/pardeike/Harmony/master/HarmonyLogo.png)
3 changes: 2 additions & 1 deletion Source/ExtendedStorage.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Transpiler/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
2 changes: 2 additions & 0 deletions Source/ExtendedStorage/ExtendedStorage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
<DependentUpon>LanguageKeys.Generated.tt</DependentUpon>
</Compile>
<Compile Include="Patches\CompressibilityDeciderUtility_IsSaveCompressible.cs" />
<Compile Include="Patches\GenSpawn_Spawn.cs" />
<Compile Include="Patches\Map_FinalizeLoading.cs" />
<Compile Include="Patches\StorageSettings.cs" />
<Compile Include="Patches\Thing_SplitOff.cs" />
<Compile Include="Patches\[GUI]\FloatMenuMakerMap_AddHumanlikeOrders.cs" />
Expand Down
57 changes: 57 additions & 0 deletions Source/ExtendedStorage/Patches/GenSpawn_Spawn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using Harmony;
using Verse;

namespace ExtendedStorage.Patches
{

/// <summary>
/// infrastructure to suppress item splurge during load
/// </summary>
[HarmonyPatch(typeof(GenSpawn), "Spawn", new Type[] {typeof(Thing), typeof(IntVec3), typeof(Map), typeof(Rot4), typeof(WipeMode), typeof(bool)})]
public static class GenSpawn_Spawn
{
public static FieldInfo LoadedFullThingsField = typeof(Map).GetField("loadedFullThings", BindingFlags.NonPublic | BindingFlags.Static);

public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator il)
{
bool ldargsSeen = false;
Label l = il.DefineLabel();
List<CodeInstruction> instrList = instructions.ToList();
for (int i = 0; i < instrList.Count; i++)
{
if (!ldargsSeen && instrList[i].opcode == OpCodes.Ldarg_S && instrList[i].operand.Equals((byte) 4))
{
Label jmpLabel = instrList[i].labels[0];
instrList[i].labels.Clear();
CodeInstruction ins = new CodeInstruction(OpCodes.Ldarg_1)
{
labels = new List<Label>() {jmpLabel}
};
yield return ins;
yield return new CodeInstruction(OpCodes.Ldarg_2);
yield return new CodeInstruction(OpCodes.Ldarg_S, (byte) 5);
yield return new CodeInstruction(OpCodes.Call, typeof(GenSpawn_Spawn).GetMethod("ShouldDisplaceOtherItems"));
yield return new CodeInstruction(OpCodes.Brfalse, l);
ldargsSeen = true;
}

if (i + 2 < instrList.Count && instrList[i + 2].opcode == OpCodes.Callvirt && instrList[i + 2].operand == typeof(Thing).GetProperty("Rotation").GetSetMethod())
{
instrList[i].labels.Add(l);
}

yield return instrList[i];
}
}

public static bool ShouldDisplaceOtherItems(IntVec3 cell, Map map, bool respawningAfterLoad)
{
return !respawningAfterLoad || map?.thingGrid.ThingsListAtFast(cell).OfType<Building_ExtendedStorage>().Any() != true;
}
}
}
66 changes: 66 additions & 0 deletions Source/ExtendedStorage/Patches/Map_FinalizeLoading.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using Harmony;
using Verse;

namespace ExtendedStorage.Patches {
/// <summary>
/// prioritize ES building spawn so genspawn.spawn splurge suppression patch can return a valid discriminator
/// </summary>
/// <seealso cref="GenSpawn_Spawn.ShouldDisplaceOtherItems"/>
[HarmonyPatch(typeof(Map), nameof(Map.FinalizeLoading))]
class Map_FinalizeLoading
{
private static MethodInfo mi = typeof(BackCompatibility).GetMethod(nameof(BackCompatibility.PreCheckSpawnBackCompatibleThingAfterLoading));

public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instr) {
List<CodeInstruction> instructions = new List<CodeInstruction>(instr);

var idxAnchor = instructions.FindIndex(ci => ci.opcode == OpCodes.Call && ci.operand == mi);
if (idxAnchor == -1) {
Log.Warning("Could not find Map_FinalizeLoading transpiler anchor - not patching.");
return instructions;
}

instructions.InsertRange(idxAnchor +1,
new []
{
new CodeInstruction(OpCodes.Ldloc_1),
new CodeInstruction(OpCodes.Ldarg_0),
new CodeInstruction(OpCodes.Call, typeof(Map_FinalizeLoading).GetMethod(nameof(PrioritySpawnStorageBuildings)))
});

return instructions;

}

public static void PrioritySpawnStorageBuildings(List<Thing> things, Map map)
{
var candidates = things.OfType<Building_ExtendedStorage>().ToArray();

foreach (Building_ExtendedStorage current in candidates)
{
try
{
GenSpawn.SpawnBuildingAsPossible(current, map, true);
}
catch (Exception ex2)
{
Log.Error(string.Concat(new object[]
{
"Exception spawning loaded thing ",
current.ToStringSafe<Building>(),
": ",
ex2
}), false);
}

things.Remove(current);
}
}
}
}

0 comments on commit a7b47bb

Please sign in to comment.