Skip to content

Commit

Permalink
Merge pull request #91 from Vapok/vapok/r1.6.15
Browse files Browse the repository at this point in the history
Release 1.6.15
  • Loading branch information
Vapok authored Mar 21, 2023
2 parents 9ad7d64 + 1aeedd9 commit cf2b183
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 91 deletions.
2 changes: 1 addition & 1 deletion AdventureBackpacks/AdventureBackpacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,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.14";
private const string _version = "1.6.15";

//Interface Properties
public string PluginId => _pluginId;
Expand Down
3 changes: 3 additions & 0 deletions AdventureBackpacks/Assets/Backpacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public static void UpdateItemDataConfigValues(object sender, EventArgs e)
if (Player.m_localPlayer == null || Player.m_localPlayer.GetInventory() == null)
return;

//Close Inventory before making changes. Leaving open can have undesirable effects.
InventoryGui.instance.Hide();

void SearchInventory(List<ItemDrop.ItemData> inventory)
{
if (inventory == null)
Expand Down
245 changes: 181 additions & 64 deletions AdventureBackpacks/Patches/InventoryGui.cs

Large diffs are not rendered by default.

91 changes: 70 additions & 21 deletions AdventureBackpacks/Patches/ItemDrop.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using AdventureBackpacks.Assets;
using AdventureBackpacks.Components;
using HarmonyLib;
Expand All @@ -9,35 +11,82 @@ namespace AdventureBackpacks.Patches;
public class ItemDropPatches
{
[HarmonyPatch(typeof(ItemDrop.ItemData), nameof(ItemDrop.ItemData.GetWeight))]
[HarmonyBefore(new string[]{"randyknapp.mods.epicloot"})]
static class GetWeightPatch
static class ItemDataGetWeightTranspiler
{
static void Postfix(ItemDrop.ItemData __instance, ref float __result)
public static float OverrideBackpackWeight(ItemDrop.ItemData item, float originalWeight)
{
if (__instance == null || __instance.m_shared == null)
return;

try
var returnedWeight = originalWeight;

if (!string.IsNullOrEmpty(item.m_shared.m_name) && item.TryGetBackpackItem(out var backpack))
{
if (string.IsNullOrEmpty(__instance.m_shared.m_name))
return;

// If the item in GetWeight() is a backpack, call GetTotalWeight() on its Inventory.
// Note that GetTotalWeight() just returns a the value of m_totalWeight, and doesn't do any calculation on its own.
// If the Inventory has been changed at any point, it calls UpdateTotalWeight(), which should ensure that its m_totalWeight is accurate.
var inventoryWeight = item.Data().GetOrCreate<BackpackComponent>().GetInventory()?.GetTotalWeight() ?? 0;

// To the backpack's item weight, add the backpack's inventory weight multiplied by the weightMultiplier in the configs.
returnedWeight += inventoryWeight * backpack.WeightMultiplier.Value;
}

return returnedWeight;
}

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;
}

if (__instance.TryGetBackpackItem(out var backpack))
var scaleWeightByQualityField = AccessTools.DeclaredField(typeof(ItemDrop.ItemData.SharedData),"m_scaleWeightByQuality");

for (int i = 0; i < instrs.Count; ++i)
{
if (i > 6 && instrs[i].opcode == OpCodes.Ldloc_0 && instrs[i-1].opcode == OpCodes.Stloc_0 && instrs[i-2].opcode == OpCodes.Add &&
instrs[i - 3].opcode == OpCodes.Mul && instrs[i - 4].opcode == OpCodes.Ldfld &&
instrs[i - 4].operand.Equals(scaleWeightByQualityField))
{
//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);

//Insert new instructions first.

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

// If the item in GetWeight() is a backpack, call GetTotalWeight() on its Inventory.
// Note that GetTotalWeight() just returns a the value of m_totalWeight, and doesn't do any calculation on its own.
// If the Inventory has been changed at any point, it calls UpdateTotalWeight(), which should ensure that its m_totalWeight is accurate.
var inventoryWeight = __instance.Data().GetOrCreate<BackpackComponent>().GetInventory()?.GetTotalWeight() ?? 0;
//Get Weight which is ldloc0
yield return LogMessage(new CodeInstruction(OpCodes.Ldloc_0));
counter++;

// To the backpack's item weight, add the backpack's inventory weight multiplied by the weightMultiplier in the configs.
__result += inventoryWeight * backpack.WeightMultiplier.Value;
//Patch Call Method for Overriding the Weight.
yield return LogMessage(new CodeInstruction(OpCodes.Call, AccessTools.DeclaredMethod(typeof(ItemDataGetWeightTranspiler), nameof(OverrideBackpackWeight))));
counter++;

//Set Weight which is stloc0
yield return LogMessage(new CodeInstruction(OpCodes.Stloc_0));
counter++;

//Output current Operation
yield return LogMessage(instrs[i]);
counter++;
}
else
{
yield return LogMessage(instrs[i]);
counter++;
}
}
catch (Exception e)
{
AdventureBackpacks.Log.Debug($"[ItemDrop.ItemData.GetWeight] An Error occurred - {e.Message}");
}
}
}

}
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.14.0")]
[assembly: AssemblyFileVersion("1.6.14.0")]
[assembly: AssemblyVersion("1.6.15.0")]
[assembly: AssemblyFileVersion("1.6.15.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.15.0 - Controller Support!!!, also some bug fixes.
* Fully Implemented Controller/Gamepad Support.
* Set bindings in configuration for opening up the backpack and other settings.
* Rebuilt the mechanism for calculating backpack weight. Now uses transpiler.
* Addition Sign and Tame Rename Interaction Fixes
* Open Backpack With Hover now fully works on extended inventory and quick slot grids.
* Open with Inventory now work with Open/Close Backpack with Hover.

# 1.6.14.0 - Fixing Equippable Items, Adding Hover Over Interaction option, Fixing Signs.
* New Feature: Open Inventory with Hover Interaction
* When enabled, this will override the Open with Inventory and Close Inventory Options.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ learn how to make your very own, Adventure Backpacks! Go forth and wander, ye w
* The default hotkey is `I` to open the equipped backpack.
* Each backpack is completely different in form, function, and size. Upgrading backpacks will unlock additional features depending on the progresion that backpack is intended to be used with.
* Check the Configuration for ALL the different ways that you can modify these packs.
* Keybindings and Actions are Controller Supported

## How To Install Adventure Backpacks
* Install Adventure Backpacks into it's own FOLDER inside of the `BepInEx/plugins` folder.
Expand Down Expand Up @@ -55,8 +56,7 @@ learn how to make your very own, Adventure Backpacks! Go forth and wander, ye w
* Can also set Mouse, Keyboard, and Gamepad bindings.
* Configure Opening of Backpack with Hover + Interaction
* When enabled, will open backpack when hovered over in Player Inventory and the Open Hot Key is pressed.
* This feature overrides Open with Inventory and Close with Inventory.
* This feature will only work when backpack is in player inventory, and NOT in Extended Inventory windows.
* This feature overrides Close with Inventory.
* 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.14",
"version_number": "1.6.15",
"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 cf2b183

Please sign in to comment.