Skip to content

Commit

Permalink
Merge pull request #85 from Vapok/vapok/r1.6.13
Browse files Browse the repository at this point in the history
Release 1.6.13
  • Loading branch information
Vapok authored Mar 18, 2023
2 parents f25873f + 769e89a commit 1deefcb
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 76 deletions.
13 changes: 3 additions & 10 deletions AdventureBackpacks/AdventureBackpacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using HarmonyLib;
using ItemManager;
using JetBrains.Annotations;
using UnityEngine;
using Vapok.Common.Abstractions;
using Vapok.Common.Managers;
using Vapok.Common.Managers.Configuration;
Expand All @@ -29,7 +28,7 @@ public class AdventureBackpacks : BaseUnityPlugin, IPluginInfo
//Module Constants
private const string _pluginId = "vapok.mods.adventurebackpacks";
private const string _displayName = "Adventure Backpacks";
private const string _version = "1.6.12";
private const string _version = "1.6.13";

//Interface Properties
public string PluginId => _pluginId;
Expand Down Expand Up @@ -98,16 +97,10 @@ private void Update()
if (backpack != null)
Backpacks.PerformYardSale(Player.m_localPlayer, backpack.Item);
}

if (!KeyPressTool.IgnoreKeyPresses(true) && (ConfigRegistry.HotKeyOpen.Value.IsDown() || ZInput.GetButtonDown(ConfigRegistry.HotKeyOpen.Value.Serialize()) ) && Player.m_localPlayer.CanOpenBackpack())
{
ZInput.ResetButtonStatus(ConfigRegistry.HotKeyOpen.Value.Serialize());
Player.m_localPlayer.OpenBackpack();
}

if (ConfigRegistry.OutwardMode.Value && !KeyPressTool.IgnoreKeyPresses(true) && (ConfigRegistry.HotKeyDrop.Value.IsDown() || ZInput.GetButtonDown(ConfigRegistry.HotKeyDrop.Value.Serialize())) && Player.m_localPlayer.CanOpenBackpack())
if ((ZInput.GetButton("Forward") || ZInput.GetButton("Backward") || ZInput.GetButton("Left") ||ZInput.GetButton("Right"))
&& ZInput.GetKeyDown(ConfigRegistry.HotKeyDrop.Value.MainKey) && ConfigRegistry.OutwardMode.Value)
{
ZInput.ResetButtonStatus(ConfigRegistry.HotKeyOpen.Value.Serialize());
Player.m_localPlayer.QuickDropBackpack();
}

Expand Down
26 changes: 16 additions & 10 deletions AdventureBackpacks/AdventureBackpacks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,44 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\..\References\BepInEx\5.4.2100\BepInEx\core\0Harmony.dll</HintPath>
<HintPath>..\..\References\BepInEx\5.4.2101\BepInEx\core\0Harmony.dll</HintPath>
</Reference>
<Reference Include="assembly_guiutils">
<HintPath>..\..\References\Valheim\0.214.2\assembly_guiutils_publicized.dll</HintPath>
<HintPath>..\..\References\Valheim\0.214.3\assembly_guiutils_publicized.dll</HintPath>
</Reference>
<Reference Include="assembly_utils">
<HintPath>..\..\References\Valheim\0.214.2\assembly_utils_publicized.dll</HintPath>
<HintPath>..\..\References\Valheim\0.214.3\assembly_utils_publicized.dll</HintPath>
</Reference>
<Reference Include="assembly_valheim">
<HintPath>..\..\References\Valheim\0.214.2\assembly_valheim_publicized.dll</HintPath>
<HintPath>..\..\References\Valheim\0.214.3\assembly_valheim_publicized.dll</HintPath>
</Reference>
<Reference Include="BepInEx">
<HintPath>..\..\References\BepInEx\5.4.2100\BepInEx\core\BepInEx.dll</HintPath>
<HintPath>..\..\References\BepInEx\5.4.2101\BepInEx\core\BepInEx.dll</HintPath>
</Reference>
<Reference Include="BepInEx.Harmony">
<HintPath>..\..\References\BepInEx\5.4.2100\BepInEx\core\BepInEx.Harmony.dll</HintPath>
<HintPath>..\..\References\BepInEx\5.4.2101\BepInEx\core\BepInEx.Harmony.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="UnityEngine">
<HintPath>..\..\References\BepInEx\5.4.2100\unstripped_corlib\UnityEngine.dll</HintPath>
<HintPath>..\..\References\BepInEx\5.4.2101\unstripped_corlib\UnityEngine.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.AnimationModule">
<HintPath>..\..\References\BepInEx\5.4.2100\unstripped_corlib\UnityEngine.AnimationModule.dll</HintPath>
<HintPath>..\..\References\BepInEx\5.4.2101\unstripped_corlib\UnityEngine.AnimationModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.AssetBundleModule">
<HintPath>..\..\References\BepInEx\5.4.2100\unstripped_corlib\UnityEngine.AssetBundleModule.dll</HintPath>
<HintPath>..\..\References\BepInEx\5.4.2101\unstripped_corlib\UnityEngine.AssetBundleModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>..\..\References\BepInEx\5.4.2100\unstripped_corlib\UnityEngine.CoreModule.dll</HintPath>
<HintPath>..\..\References\BepInEx\5.4.2101\unstripped_corlib\UnityEngine.CoreModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.InputLegacyModule">
<HintPath>..\..\References\BepInEx\5.4.2101\unstripped_corlib\UnityEngine.InputLegacyModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.PhysicsModule">
<HintPath>..\..\References\BepInEx\5.4.2101\unstripped_corlib\UnityEngine.PhysicsModule.dll</HintPath>
</Reference>
<Reference Include="Vapok.Common">
<HintPath>..\..\References\Vapok.Common\Vapok.Common.dll</HintPath>
Expand Down
10 changes: 6 additions & 4 deletions AdventureBackpacks/Extensions/PlayerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AdventureBackpacks.Components;
using AdventureBackpacks.Patches;
using UnityEngine;
using Vapok.Common.Managers;

namespace AdventureBackpacks.Extensions;
Expand Down Expand Up @@ -38,7 +39,7 @@ public static bool CanOpenBackpack(this Player player)
return IsBackpackEquipped(player);
}

public static void OpenBackpack(this Player player, bool track = true)
public static void OpenBackpack(this Player player, InventoryGui instance)
{
if (player == null)
return;
Expand All @@ -56,8 +57,7 @@ public static void OpenBackpack(this Player player, bool track = true)
backpackContainer.m_bkg = backpack.Item.m_shared.m_icons[0];

InventoryGuiPatches.BackpackIsOpen = true;
InventoryGuiPatches.BackpackIsOpening = track;
InventoryGui.instance.Show(backpackContainer);
instance.Show(backpackContainer);
}

public static void QuickDropBackpack(this Player player)
Expand Down Expand Up @@ -97,7 +97,9 @@ public static void QuickDropBackpack(this Player player)
player.m_inventory.RemoveItem(backpack.Item);

// This drops a copy of the backpack itemDrop.itemData
var itemDrop = ItemDrop.DropItem(backpack.Item, 1, player.transform.position - player.transform.up - player.transform.up, player.transform.rotation);
var itemDrop = ItemDrop.DropItem(backpack.Item, 1, player.transform.position - player.transform.forward + player.transform.up, player.transform.rotation);
itemDrop.GetComponent<Rigidbody>().velocity = (Vector3.up - player.transform.forward) * 5f;
player.m_dropEffects.Create(player.transform.position, Quaternion.identity);
itemDrop.Save();

if (swapItemActivated)
Expand Down
174 changes: 125 additions & 49 deletions AdventureBackpacks/Patches/InventoryGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
using System.Linq;
using System.Reflection.Emit;
using AdventureBackpacks.Assets;
using AdventureBackpacks.Assets.Factories;
using AdventureBackpacks.Components;
using AdventureBackpacks.Configuration;
using AdventureBackpacks.Extensions;
using HarmonyLib;
using JetBrains.Annotations;
using UnityEngine;
using Vapok.Common.Managers;
using Vapok.Common.Tools;

namespace AdventureBackpacks.Patches;

internal static class InventoryGuiPatches
{
public static bool BackpackIsOpen;
public static bool BackpackIsOpening;
public static bool BackpackEquipped = false;
private static bool _showBackpack = false;

[HarmonyPatch(typeof(InventoryGui), nameof(InventoryGui.DoCrafting))]
static class InventoryGuiDoCraftingPrefix
Expand Down Expand Up @@ -67,30 +68,90 @@ static Exception Finalizer(Exception __exception, InventoryGrid grid, ItemDrop.I
}
}

public static void ShowBackpack(Player player)
public static void ShowBackpack(Player player, InventoryGui instance)
{
if (ConfigRegistry.OpenWithInventory.Value && player.CanOpenBackpack())
{
player.OpenBackpack(false);
if (_showBackpack)
{
_showBackpack = false;
player.OpenBackpack(instance);
}
}

public static void HideBackpack(InventoryGui instance)
{
if (ConfigRegistry.OpenWithInventory.Value)
if (BackpackIsOpen)
{
if (BackpackIsOpen)
instance.CloseContainer();
BackpackIsOpen = false;

if (ConfigRegistry.CloseInventory.Value)
instance.Hide();
}
}

public static bool DetectInputToHide(KeyCode defaultKeyCode, Player player, InventoryGui instance)
{
var defaultInputDown = Input.GetKeyDown(defaultKeyCode);
var hotKeyDown = ConfigRegistry.HotKeyOpen.Value.IsDown();
var hotKeyDownOnClose = ConfigRegistry.CloseInventory.Value && hotKeyDown;
var hotKeyDrop = ConfigRegistry.OutwardMode.Value && ConfigRegistry.HotKeyDrop.Value.IsDown();

if (hotKeyDown && !BackpackIsOpen && player.CanOpenBackpack())
{
if (instance.m_currentContainer != null)
{
instance.CloseContainer();
BackpackIsOpen = false;

if (ConfigRegistry.CloseInventory.Value)
instance.Hide();
instance.m_currentContainer.SetInUse(false);
instance.m_currentContainer = null;
}
player.OpenBackpack(instance);
return false;
}

if (hotKeyDown && BackpackIsOpen && !hotKeyDownOnClose)
{
instance.CloseContainer();
BackpackIsOpen = false;
return false;
}

if (hotKeyDrop)
{
player.QuickDropBackpack();
}
}

return defaultInputDown || hotKeyDownOnClose || hotKeyDrop;
}

public static bool DetectInputToShow(string defaultKeyCode, Player player, InventoryGui instance)
{
var zInputDown = ZInput.GetButtonDown(defaultKeyCode);
var hotKeyDown = ConfigRegistry.HotKeyOpen.Value.IsDown();
var hotKeyDrop = ConfigRegistry.OutwardMode.Value && ConfigRegistry.HotKeyDrop.Value.IsDown();

if (hotKeyDrop)
{
player.QuickDropBackpack();
}

if (hotKeyDown && !BackpackIsOpen && player.CanOpenBackpack())
{
_showBackpack = true;
}

if (zInputDown && ConfigRegistry.OpenWithInventory.Value && !BackpackIsOpen && player.CanOpenBackpack())
{
_showBackpack = true;
}

if (_showBackpack && !BackpackIsOpen)
{
if (instance.m_currentContainer != null)
instance.CloseContainer();
}

return zInputDown || hotKeyDown || _showBackpack;
}

[HarmonyPatch(typeof(InventoryGui), nameof(InventoryGui.Update))]
static class InventoryGuiUpdateTranspiler
{
Expand All @@ -109,6 +170,8 @@ CodeInstruction LogMessage(CodeInstruction instruction)
var resetButtonStatus = AccessTools.DeclaredMethod(typeof(ZInput), nameof(ZInput.ResetButtonStatus));
var hideMethod = AccessTools.DeclaredMethod(typeof(InventoryGui), nameof(InventoryGui.Hide));
var showMethod = AccessTools.DeclaredMethod(typeof(InventoryGui), nameof(InventoryGui.Show));
var inputKeyDown = AccessTools.DeclaredMethod(typeof(Input), nameof(Input.GetKeyDown), new []{typeof(KeyCode)});
var zInputButtonDown = AccessTools.DeclaredMethod(typeof(ZInput), nameof(ZInput.GetButtonDown), new []{typeof(string)});

for (int i = 0; i < instrs.Count; ++i)
{
Expand Down Expand Up @@ -151,10 +214,58 @@ CodeInstruction LogMessage(CodeInstruction instruction)
//Patch ldloc_1 this is localPlayer.
yield return LogMessage(localPlayerInstruction);
counter++;

//InventoryGui Argument.
yield return LogMessage(new CodeInstruction(OpCodes.Ldarg_0));
counter++;

//Patch Call Method for Hiding.
yield return LogMessage(new CodeInstruction(OpCodes.Call, AccessTools.DeclaredMethod(typeof(InventoryGuiPatches), nameof(ShowBackpack))));
counter++;
} else if (i > 6 && instrs[i].opcode == OpCodes.Call && instrs[i].operand.Equals(inputKeyDown) && instrs[i - 1].operand.Equals((sbyte)KeyCode.Escape) && instrs[i + 2].opcode == OpCodes.Ldstr && instrs[i + 2].operand.Equals("Use"))
{
//Add Inventory Open Key from Config
//Get Player
var ldLocInstruction = new CodeInstruction(OpCodes.Ldloc_1);
//Move Any Labels from the instruction position being patched to new instruction.
if (instrs[i].labels.Count > 0)
instrs[i].MoveLabelsTo(ldLocInstruction);

//Patch Call Method for Detecting Key Press
yield return LogMessage(ldLocInstruction);
counter++;

//InventoryGui Argument.
yield return LogMessage(new CodeInstruction(OpCodes.Ldarg_0));
counter++;

//Patch Call Method for Detect Hide.
yield return LogMessage(new CodeInstruction(OpCodes.Call, AccessTools.DeclaredMethod(typeof(InventoryGuiPatches), nameof(DetectInputToHide))));
counter++;

} else if (i > 6 && instrs[i].opcode == OpCodes.Call && instrs[i].operand.Equals(zInputButtonDown)
&& instrs[i - 1].operand.Equals("Inventory") && instrs[i + 1].opcode == OpCodes.Brtrue
&& instrs[i + 2].opcode == OpCodes.Ldstr && instrs[i + 2].operand.Equals("JoyButtonY"))
{

//Add Inventory Open Key from Config
//Get Player
var ldLocInstruction = new CodeInstruction(OpCodes.Ldloc_1);
//Move Any Labels from the instruction position being patched to new instruction.
if (instrs[i].labels.Count > 0)
instrs[i].MoveLabelsTo(ldLocInstruction);

//Patch Call Method for Detecting Key Press
yield return LogMessage(ldLocInstruction);
counter++;

//InventoryGui Argument.
yield return LogMessage(new CodeInstruction(OpCodes.Ldarg_0));
counter++;

//Patch Call Method for Detect Show.
yield return LogMessage(new CodeInstruction(OpCodes.Call, AccessTools.DeclaredMethod(typeof(InventoryGuiPatches), nameof(DetectInputToShow))));
counter++;
}
else
{
Expand All @@ -164,39 +275,4 @@ CodeInstruction LogMessage(CodeInstruction instruction)
}
}
}


[HarmonyPatch(typeof(InventoryGui), nameof(InventoryGui.Update))]
static class InventoryGuiUpdatePatch
{
static void Postfix(Animator ___m_animator, ref Container ___m_currentContainer)
{
if ( !ConfigRegistry.HotKeyOpen.Value.IsDown() || !ZInput.GetButtonDown(ConfigRegistry.HotKeyOpen.Value.Serialize()) || !Player.m_localPlayer || !___m_animator.GetBool("visible"))
return;

ZInput.ResetButtonStatus(ConfigRegistry.HotKeyOpen.Value.Serialize());

if (BackpackIsOpening)
{
BackpackIsOpening = false;
return;
}

if (BackpackIsOpen)
{
InventoryGui.instance.CloseContainer();
BackpackIsOpen = false;

if (ConfigRegistry.CloseInventory.Value)
InventoryGui.instance.Hide();

return;
}

if (Player.m_localPlayer.CanOpenBackpack())
{
Player.m_localPlayer.OpenBackpack(false);
}
}
}
}
4 changes: 2 additions & 2 deletions AdventureBackpacks/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,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("1.6.12.0")]
[assembly: AssemblyFileVersion("1.6.12.0")]
[assembly: AssemblyVersion("1.6.13.0")]
[assembly: AssemblyFileVersion("1.6.13.0")]
8 changes: 8 additions & 0 deletions PATCHNOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Adventure Backpacks Patchnotes

# 1.6.13.0 - Refactor of Backpack Interaction Controls
* Enhanced and improved the mechanics behind how backpacks open.
* Fixed the backpack not closing when open.
* All Configuration settings for backpack controls have been validated.
* **Please double check your settings if interactions have changed for you**
* Community has verified controller support from previous version.
* Fixed an issue with bags falling through the floor when dropped with Outward Mode

# 1.6.12.0 - Redefining Keymappings and Open with Inventory Option
* Adjusted Config Keymappings to allow for Gamepad, Mouse, or Keyboard to be set.
* Untested Controller Support - I'm hoping this works, but it might not. Please provide feedback.
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "AdventureBackpacks",
"version_number": "1.6.12",
"version_number": "1.6.13",
"website_url": "https://github.com/Vapok/AdventureBackpacks",
"description": "A Valheim Mod to add a catalogue of Adventuring Backpacks to the Game. These packs will grow and become more useful as the game progresses.",
"dependencies": [
Expand Down

0 comments on commit 1deefcb

Please sign in to comment.