Skip to content

Commit

Permalink
fixed issue where stacked items would 'fall down' after reloading (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
WulfMarius committed Oct 14, 2018
1 parent f54942f commit df1317e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 73 deletions.
4 changes: 2 additions & 2 deletions VisualStudio/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.1.2")]
[assembly: AssemblyFileVersion("2.1.2")]
[assembly: AssemblyVersion("2.1.3")]
[assembly: AssemblyFileVersion("2.1.3")]
26 changes: 6 additions & 20 deletions VisualStudio/src/BetterPlacing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class BetterPlacing

public static void OnLoad()
{
Debug.Log("[Better-Placing]: Version " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
Debug.Log("[Better-Placing] Version " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);

AddTranslations();
PlaceableFurniture.Initialize();
Expand Down Expand Up @@ -120,21 +120,6 @@ internal static bool IsStackableGearItem(GameObject gameObject)
return true;
}

internal static void PrepareGearItems()
{
GearItem[] gearItems = Resources.FindObjectsOfTypeAll<GearItem>();
foreach (GearItem eachGearItem in gearItems)
{
if (IsStackableGearItem(eachGearItem.gameObject))
{
FixBoxCollider(eachGearItem.gameObject);
}
}

RemovePickupHelper(Resources.Load<GameObject>("GEAR_WoodMatches"));
RemovePickupHelper(Resources.Load<GameObject>("GEAR_PackMatches"));
}

internal static GameObject getFurnitureRoot(GameObject gameObject)
{
if (gameObject.GetComponent<LODGroup>() != null)
Expand Down Expand Up @@ -163,7 +148,8 @@ internal static void PreparePlacableFurniture(GameObject gameObject)
MeshFilter[] meshFilters = gameObject.GetComponentsInChildren<MeshFilter>();
foreach (MeshFilter eachMeshFilter in meshFilters)
{
if (!eachMeshFilter.name.StartsWith(templateMeshFilter.name)) {
if (!eachMeshFilter.name.StartsWith(templateMeshFilter.name))
{
continue;
}

Expand Down Expand Up @@ -281,7 +267,7 @@ private static void AddTranslations()
Localization.dictionary.Add("GAMEPLAY_BlockedByItemAbove", translations);
}

private static void FixBoxCollider(GameObject gameObject)
internal static void FixBoxCollider(GameObject gameObject)
{
if (gameObject == null)
{
Expand Down Expand Up @@ -363,7 +349,7 @@ private static List<GameObject> GetGearItemsAbove(GameObject gameObject, Collide
return result;
}

private static void RemovePickupHelper(GameObject gameObject)
internal static void RemovePickupHelper(GameObject gameObject)
{
if (gameObject == null)
{
Expand All @@ -376,7 +362,7 @@ private static void RemovePickupHelper(GameObject gameObject)
return;
}

Object.Destroy(pickupHelper.GetComponent<Collider>());
pickupHelper.gameObject.SetActive(false);
}

private static void SetRotation(Quaternion rotation)
Expand Down
120 changes: 69 additions & 51 deletions VisualStudio/src/Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,72 @@ public static bool Prefix(BreakDown __instance, ref string __result)
}
}

[HarmonyPatch(typeof(GearManager), "Reset")]
internal class GearManager_Reset
[HarmonyPatch(typeof(BreakDown), "StickToGround")]
internal class BreakDown_StickToGround
{
public static void Prefix()
public static void Postfix()
{
BetterPlacing.RemoveGearItemsFromPhysicalCollisionMask();
}

public static void Prefix(BreakDown __instance, GameObject go)
{
if (BetterPlacing.IsStackableGearItem(go))
{
BetterPlacing.AddGearItemsToPhysicalCollisionMask();
}
}
}

[HarmonyPatch(typeof(Campfire), "Awake")]
internal class Campfire_Awake
{
internal static void Postfix(Campfire __instance)
{
BetterPlacing.ChangeLayer(__instance.gameObject, vp_Layer.Gear, vp_Layer.NPC);
}
}

[HarmonyPatch(typeof(GearManager), "Add")]
internal class GearManager_Add
{
public static void Prefix(GearItem gi)
{
BetterPlacing.FixBoxCollider(gi.gameObject);
BetterPlacing.RemovePickupHelper(gi.gameObject);
}
}

[HarmonyPatch(typeof(GearPlacePoint), "AddDefaultCapsuleCollider")]
internal class GearPlacePoint_AddDefaultCapsuleCollider
{
internal static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
BetterPlacing.PrepareGearItems();
List<CodeInstruction> codeInstructions = new List<CodeInstruction>(instructions);

for (int i = 0; i < codeInstructions.Count; i++)
{
CodeInstruction codeInstruction = codeInstructions[i];

if (codeInstruction.opcode != OpCodes.Call && codeInstruction.opcode != OpCodes.Callvirt)
{
continue;
}

MethodInfo methodInfo = codeInstruction.operand as MethodInfo;
if (methodInfo == null)
{
continue;
}

if (methodInfo.Name == "set_layer" && methodInfo.DeclaringType == typeof(GameObject) && methodInfo.GetParameters().Length == 1)
{
codeInstructions[i - 1].operand = vp_Layer.NPC;
break;
}
}

return instructions;
}
}

Expand Down Expand Up @@ -250,6 +310,11 @@ public static bool Prefix(PlayerManager __instance)
[HarmonyPatch(typeof(PlayerManager), "ObjectToPlaceOverlapsWithObjectsThatBlockPlacement")]
internal class PlayerManager_ObjectToPlaceOverlapsWithObjectsThatBlockPlacement
{
public static void Postfix()
{
BetterPlacing.RemoveNpcFromPhysiclaCollisionMask();
}

public static bool Prefix(PlayerManager __instance, ref bool __result)
{
BetterPlacing.AddNpcToPhysicalCollisionMask();
Expand Down Expand Up @@ -290,11 +355,6 @@ public static bool Prefix(PlayerManager __instance, ref bool __result)
__result = false;
return false;
}

public static void Postfix()
{
BetterPlacing.RemoveNpcFromPhysiclaCollisionMask();
}
}

[HarmonyPatch(typeof(PlayerManager), "ProcessInspectablePickupItem")]
Expand Down Expand Up @@ -355,48 +415,6 @@ private static bool Prefix(PlayerManager __instance, GameObject objectToPlace, r
}
}

[HarmonyPatch(typeof(Campfire), "Awake")]
internal class Campfire_Awake
{
internal static void Postfix(Campfire __instance)
{
BetterPlacing.ChangeLayer(__instance.gameObject, vp_Layer.Gear, vp_Layer.NPC);
}
}

[HarmonyPatch(typeof(GearPlacePoint), "AddDefaultCapsuleCollider")]
internal class GearPlacePoint_AddDefaultCapsuleCollider
{
internal static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> codeInstructions = new List<CodeInstruction>(instructions);

for (int i = 0; i < codeInstructions.Count; i++)
{
CodeInstruction codeInstruction = codeInstructions[i];

if (codeInstruction.opcode != OpCodes.Call && codeInstruction.opcode != OpCodes.Callvirt)
{
continue;
}

MethodInfo methodInfo = codeInstruction.operand as MethodInfo;
if (methodInfo == null)
{
continue;
}

if (methodInfo.Name == "set_layer" && methodInfo.DeclaringType == typeof(GameObject) && methodInfo.GetParameters().Length == 1)
{
codeInstructions[i - 1].operand = vp_Layer.NPC;
break;
}
}

return instructions;
}
}

[HarmonyPatch(typeof(Wind), "IsPositionOccludedFromWind")]
internal class Wind_IsPositionOccludedFromWind
{
Expand Down

0 comments on commit df1317e

Please sign in to comment.