Skip to content

Commit

Permalink
Merge pull request #84 from Vapok/vapok/r1.6.12
Browse files Browse the repository at this point in the history
Release 1.6.12
  • Loading branch information
Vapok authored Mar 17, 2023
2 parents 675e4b6 + 1ebbe01 commit f25873f
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 12 deletions.
9 changes: 6 additions & 3 deletions AdventureBackpacks/AdventureBackpacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using HarmonyLib;
using ItemManager;
using JetBrains.Annotations;
using UnityEngine;
using Vapok.Common.Abstractions;
using Vapok.Common.Managers;
using Vapok.Common.Managers.Configuration;
Expand All @@ -28,7 +29,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.11";
private const string _version = "1.6.12";

//Interface Properties
public string PluginId => _pluginId;
Expand Down Expand Up @@ -98,13 +99,15 @@ private void Update()
Backpacks.PerformYardSale(Player.m_localPlayer, backpack.Item);
}

if (!KeyPressTool.IgnoreKeyPresses(true) && KeyPressTool.CheckKeyDown(ConfigRegistry.HotKeyOpen.Value) && Player.m_localPlayer.CanOpenBackpack())
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) && KeyPressTool.CheckKeyDown(ConfigRegistry.HotKeyDrop.Value) && Player.m_localPlayer.CanOpenBackpack())
if (ConfigRegistry.OutwardMode.Value && !KeyPressTool.IgnoreKeyPresses(true) && (ConfigRegistry.HotKeyDrop.Value.IsDown() || ZInput.GetButtonDown(ConfigRegistry.HotKeyDrop.Value.Serialize())) && Player.m_localPlayer.CanOpenBackpack())
{
ZInput.ResetButtonStatus(ConfigRegistry.HotKeyOpen.Value.Serialize());
Player.m_localPlayer.QuickDropBackpack();
}

Expand Down
16 changes: 11 additions & 5 deletions AdventureBackpacks/Configuration/ConfigRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ namespace AdventureBackpacks.Configuration
public class ConfigRegistry : ConfigSyncBase
{
//Configuration Entry Privates
internal static ConfigEntry<KeyCode> HotKeyOpen { get; private set; }
internal static ConfigEntry<KeyCode> HotKeyDrop { get; private set;}
internal static ConfigEntry<KeyboardShortcut> HotKeyOpen { get; private set; }
internal static ConfigEntry<KeyboardShortcut> HotKeyDrop { get; private set;}
internal static ConfigEntry<bool> OpenWithInventory { get; private set;}
internal static ConfigEntry<bool> CloseInventory { get; private set;}
internal static ConfigEntry<bool> OutwardMode { get; private set;}

Expand All @@ -31,18 +32,23 @@ public sealed override void InitializeConfigurationSettings()
return;

//User Configs
HotKeyOpen = _config.Bind("Local Config", "Open Backpack", KeyCode.I,
HotKeyOpen = _config.Bind("Local Config", "Open Backpack", new KeyboardShortcut(KeyCode.I),
new ConfigDescription("Hotkey to open backpack.", null, new ConfigurationManagerAttributes{ Order = 3 }));

HotKeyDrop = _config.Bind(
"Local Config", "Quickdrop Backpack", KeyCode.Y,
"Local Config", "Quickdrop Backpack", new KeyboardShortcut(KeyCode.Y),
new ConfigDescription("Hotkey to quickly drop backpack while on the run.",
null,
new ConfigurationManagerAttributes { Order = 2 }));

OpenWithInventory = _config.Bind(
"Local Config", "Open with Inventory", false,
new ConfigDescription("If enabled, both backpack and inventory will open when Inventory is opened.",
null, new ConfigurationManagerAttributes { Order = 2 }));

CloseInventory = _config.Bind(
"Local Config", "Close Inventory", true,
new ConfigDescription("If set to true, both backpack and inventory will close with Open Backpack keybind is pressed while Inventory is open.",
new ConfigDescription("If enabled, both backpack and inventory will close with Open Backpack keybind is pressed while Inventory is open.",
null, new ConfigurationManagerAttributes { Order = 1 }));

OutwardMode = _config.Bind(
Expand Down
106 changes: 105 additions & 1 deletion AdventureBackpacks/Patches/InventoryGui.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using AdventureBackpacks.Assets;
using AdventureBackpacks.Components;
using AdventureBackpacks.Configuration;
Expand Down Expand Up @@ -64,14 +67,115 @@ static Exception Finalizer(Exception __exception, InventoryGrid grid, ItemDrop.I
}
}

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

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

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


[HarmonyPatch(typeof(InventoryGui), nameof(InventoryGui.Update))]
static class InventoryGuiUpdateTranspiler
{
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
var instrs = instructions.ToList();

var counter = 0;

CodeInstruction LogMessage(CodeInstruction instruction)
{
AdventureBackpacks.Log.Debug($"IL_{counter}: Opcode: {instruction.opcode} Operand: {instruction.operand}");
return 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));

for (int i = 0; i < instrs.Count; ++i)
{
if (i > 6 && instrs[i].opcode == OpCodes.Call && instrs[i].operand.Equals(resetButtonStatus) &&
instrs[i + 1].opcode == OpCodes.Ldarg_0 && instrs[i + 2].opcode == OpCodes.Call &&
instrs[i + 2].operand.Equals(hideMethod))
{
//Call to Hide Backpack
var ldArgInstruction = new CodeInstruction(OpCodes.Ldarg_0);
//Move Any Labels from the instruction position being patched to new instruction.
if (instrs[i].labels.Count > 0)
instrs[i].MoveLabelsTo(ldArgInstruction);

//Output current Operation
yield return LogMessage(instrs[i]);
counter++;

//Patch ldarg_0 this is instance of InventoryGui.
yield return LogMessage(ldArgInstruction);
counter++;

//Patch Call Method for Hiding.
yield return LogMessage(new CodeInstruction(OpCodes.Call, AccessTools.DeclaredMethod(typeof(InventoryGuiPatches), nameof(HideBackpack))));
counter++;
} else if (i > 6 && instrs[i].opcode == OpCodes.Call && instrs[i].operand.Equals(showMethod) &&
instrs[i - 1].opcode == OpCodes.Ldc_I4_1 && instrs[i - 2].opcode == OpCodes.Ldnull &&
instrs[i - 3].opcode == OpCodes.Ldarg_0)
{
//Call to Show Backpack
//Get localPlayer at ldloc.1
var localPlayerInstruction = 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(localPlayerInstruction);

//Output current Operation
yield return LogMessage(instrs[i]);
counter++;

//Patch ldloc_1 this is localPlayer.
yield return LogMessage(localPlayerInstruction);
counter++;

//Patch Call Method for Hiding.
yield return LogMessage(new CodeInstruction(OpCodes.Call, AccessTools.DeclaredMethod(typeof(InventoryGuiPatches), nameof(ShowBackpack))));
counter++;
}
else
{
yield return LogMessage(instrs[i]);
counter++;
}
}
}
}


[HarmonyPatch(typeof(InventoryGui), nameof(InventoryGui.Update))]
static class InventoryGuiUpdatePatch
{
static void Postfix(Animator ___m_animator, ref Container ___m_currentContainer)
{
if (!KeyPressTool.CheckKeyDown(ConfigRegistry.HotKeyOpen.Value) || !Player.m_localPlayer || !___m_animator.GetBool("visible"))
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;
Expand Down
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.11.0")]
[assembly: AssemblyFileVersion("1.6.11.0")]
[assembly: AssemblyVersion("1.6.12.0")]
[assembly: AssemblyFileVersion("1.6.12.0")]
6 changes: 6 additions & 0 deletions PATCHNOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Adventure Backpacks Patchnotes

# 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.
* Added an additional configuration option to Open Backpack Inventory when Player Inventory is Opened.
* This defaults to Disabled. Set to Enabled (true) to open the backpack at the same time as inventory.

# 1.6.11.0 - Clean Up and Upgrade of BepInEx
* Updates to BepInEx 5.4.21
* Various Clean Up
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ learn how to make your very own, Adventure Backpacks! Go forth and wander, ye w
* Configure Speed Modification
* Configure Speed Modification (slowness).
* Upon each quality upgrade of backpack, speed modification is reduced (never eliminated).
* Configure Opening of Backpack with Inventory
* When enabled, opens backpack inventory with player inventory without additional interaction
* Can also set Mouse, Keyboard, and Gamepad bindings.
* Backpack Inventory Protection Guard
* Every backpack inventory is specially handled by Thor himself and is monitored for any interactions that might otherwise harm the existence of items in your backpacks.
* Backpacks in Backpacks is not allowed and the only feature that is not configurable. This is how the Allfather dreamt of it.
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.11",
"version_number": "1.6.12",
"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 f25873f

Please sign in to comment.