Skip to content

Commit

Permalink
Merge pull request #11 from Vapok/vapok/r1.0.3
Browse files Browse the repository at this point in the history
Release 1.0.3
  • Loading branch information
Vapok authored Jan 21, 2023
2 parents 5651a0b + 06a8fae commit ac66a02
Show file tree
Hide file tree
Showing 21 changed files with 286 additions and 207 deletions.
23 changes: 14 additions & 9 deletions AdventureBackpacks/AdventureBackpacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

using AdventureBackpacks.Assets;
using AdventureBackpacks.Configuration;
using AdventureBackpacks.Extensions;
using BepInEx;
using HarmonyLib;
using JetBrains.Annotations;
Expand Down Expand Up @@ -57,39 +58,43 @@ private void Awake()
//I'm awake!
_instance = this;

//Register Configuration Settings
_config = new ConfigRegistry(_instance);

//Register Logger
LogManager.Init(PluginId,out _log);

//Register Translations
Localizer.Load();

//Register Configuration Settings
_config = new ConfigRegistry(_instance);
_config.InitializeConfigurationSettings();

//Load Assets
Backpacks.LoadAssets();

//Enable BoneReorder
BoneReorder.ApplyOnEquipmentChanged(Info.Metadata.GUID);

//Patch Harmony
_harmony = new Harmony(Info.Metadata.GUID);
_harmony.PatchAll();

//???

//Profit
}

private void Update()
{
if (!Player.m_localPlayer || !ZNetScene.instance)
return;

if (!KeyPressTool.IgnoreKeyPresses(true) && KeyPressTool.CheckKeyDown(ConfigRegistry.HotKeyOpen.Value) && Backpacks.CanOpenBackpack())
if (!KeyPressTool.IgnoreKeyPresses(true) && KeyPressTool.CheckKeyDown(ConfigRegistry.HotKeyOpen.Value) && Player.m_localPlayer.CanOpenBackpack())
{
Backpacks.Opening = true;
Backpacks.OpenBackpack();
Player.m_localPlayer.OpenBackpack();
}

if (ConfigRegistry.OutwardMode.Value && !KeyPressTool.IgnoreKeyPresses(true) && KeyPressTool.CheckKeyDown(ConfigRegistry.HotKeyDrop.Value) && Backpacks.CanOpenBackpack())
if (ConfigRegistry.OutwardMode.Value && !KeyPressTool.IgnoreKeyPresses(true) && KeyPressTool.CheckKeyDown(ConfigRegistry.HotKeyDrop.Value) && Player.m_localPlayer.CanOpenBackpack())
{
Backpacks.QuickDropBackpack();
Player.m_localPlayer.QuickDropBackpack();
}

}
Expand Down
8 changes: 7 additions & 1 deletion AdventureBackpacks/AdventureBackpacks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AdventureBackpacks</RootNamespace>
<AssemblyName>AdventureBackpacks</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<LangVersion>latest</LangVersion>
</PropertyGroup>
Expand Down Expand Up @@ -40,6 +40,9 @@
<Reference Include="assembly_guiutils">
<HintPath>..\..\..\References\publicized_assemblies\assembly_guiutils_publicized.dll</HintPath>
</Reference>
<Reference Include="assembly_utils">
<HintPath>..\..\..\References\publicized_assemblies\assembly_utils_publicized.dll</HintPath>
</Reference>
<Reference Include="assembly_valheim">
<HintPath>..\..\..\References\publicized_assemblies\assembly_valheim_publicized.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -76,6 +79,9 @@
<Compile Include="Assets\Backpacks.cs" />
<Compile Include="Components\BackpackComponent.cs" />
<Compile Include="Configuration\ConfigRegistry.cs" />
<Compile Include="Extensions\InventoryExtensions.cs" />
<Compile Include="Extensions\ItemDataExtensions.cs" />
<Compile Include="Extensions\PlayerExtensions.cs" />
<Compile Include="Patches\GuiBar.cs" />
<Compile Include="Patches\Humanoid.cs" />
<Compile Include="Patches\Inventory.cs" />
Expand Down
147 changes: 30 additions & 117 deletions AdventureBackpacks/Assets/Backpacks.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using AdventureBackpacks.Components;
using AdventureBackpacks.Configuration;
using AdventureBackpacks.Extensions;
using ItemManager;
using UnityEngine;
using Vapok.Common.Abstractions;
using Vapok.Common.Managers;
using Vapok.Common.Managers.PieceManager;
using Vapok.Common.Managers.StatusEffects;
using Vapok.Common.Shared;
using CraftingTable = ItemManager.CraftingTable;

namespace AdventureBackpacks.Assets
{
Expand All @@ -29,29 +29,17 @@ public static class Backpacks

private static ILogIt _log;
private static List<string> _backpackTypes = new List<string>();
private static bool _opening;
private static Container _backpackContainer;
private static BackpackComponent _backpackEquipped; //Backpack currently equipped.

private static HitData.DamageModPair _frostResistance = new() { m_type = HitData.DamageType.Frost, m_modifier = HitData.DamageModifier.Resistant};
public static List<string> BackpackTypes => _backpackTypes;

public static bool Opening
{
get => _opening;
set => _opening = value;
}

public static Container BackpackContainer => _backpackContainer;

public static string BackpacksInventoryName => UiInventoryName;

public static void LoadAssets()
{
_log = AdventureBackpacks.Log;
_log.Info($"Embedded resources: {string.Join(",", Assembly.GetExecutingAssembly().GetManifestResourceNames())}");



BackpackTypes.Add(RuggedBackpackName);
BackpackTypes.Add(ArcticBackpackName);

Expand All @@ -64,6 +52,7 @@ public static void LoadAssets()

_ruggedBackpack.Configurable = Configurability.Disabled;

MaterialReplacer.RegisterGameObjectForShaderSwap(_ruggedBackpack.Prefab,MaterialReplacer.ShaderType.PieceShader);

//Adding Rugged Status Effect
_ruggedBackpackEffect = new CustomSE(Enums.StatusEffects.Stats, "SE_RuggedBackpack");
Expand Down Expand Up @@ -99,6 +88,8 @@ public static void LoadAssets()

_arcticBackpack.Configurable = Configurability.Disabled;

MaterialReplacer.RegisterGameObjectForShaderSwap(_arcticBackpack.Prefab,MaterialReplacer.ShaderType.PieceShader);

//Adding Arctic Status Effect
_arcticBackpackEffect = new CustomSE(Enums.StatusEffects.Stats, "SE_ArcticBackpack");
_arcticBackpackEffect.Effect.m_name = "$vapok_mod_se_arcticbackpack";
Expand Down Expand Up @@ -224,93 +215,40 @@ void SearchInventory(List<ItemDrop.ItemData> inventory)

public static Inventory NewInventoryInstance(string name)
{
if (name.Equals(RuggedBackpackName))
Inventory newInventory = null;
switch (name)
{
return new Inventory(
UiInventoryName,
null,
(int)ConfigRegistry.RuggedBackpackSize.Value.x,
(int)ConfigRegistry.RuggedBackpackSize.Value.y
);
case RuggedBackpackName:
newInventory = new Inventory(
UiInventoryName,
null,
(int)ConfigRegistry.RuggedBackpackSize.Value.x,
(int)ConfigRegistry.RuggedBackpackSize.Value.y
);
break;
case ArcticBackpackName:
newInventory = new Inventory(
UiInventoryName,
null,
(int)ConfigRegistry.ArcticBackpackSize.Value.x,
(int)ConfigRegistry.ArcticBackpackSize.Value.y
);
break;
}

if (name.Equals(ArcticBackpackName))
{
return new Inventory(
UiInventoryName,
null,
(int)ConfigRegistry.ArcticBackpackSize.Value.x,
(int)ConfigRegistry.ArcticBackpackSize.Value.y
);
}
_log.Warning($"Calling method with unknown item name");
return null;
}

public static BackpackComponent GetEquippedBackpack()
{
if (Player.m_localPlayer == null || Player.m_localPlayer.GetInventory() == null)
return null;
if (newInventory != null)
return newInventory;

// Get a list of all equipped items.
List<ItemDrop.ItemData> equippedItems = Player.m_localPlayer.GetInventory().GetEquipedtems();

if (equippedItems is null) return null;

// Go through all the equipped items, match them for any of the names in backpackTypes.
// If a match is found, return the backpack ItemData object.
foreach (ItemDrop.ItemData item in equippedItems)
{
if (BackpackTypes.Contains(item.m_shared.m_name))
{
return item.Data().GetOrCreate<BackpackComponent>();
}
}

// Return null if no backpacks are found.
_log.Warning($"Calling method with unknown item name");
return null;
}

public static void ResetBackpackContainer()
{
_backpackContainer = null;
}

public static bool CanOpenBackpack()
{
_backpackEquipped = GetEquippedBackpack();

// Return true if GetEquippedBackpack() does not return null.
if (_backpackEquipped != null)
{
return true;
}

// Return false if GetEquippedBackpack() returns null.
_log.Message("No backpack equipped. Can't open any.");
return false;

}

public static void OpenBackpack()
{

_backpackContainer = Player.m_localPlayer.gameObject.GetComponent<Container>();

if (_backpackContainer == null)
_backpackContainer = Player.m_localPlayer.gameObject.AddComponent<Container>();

_backpackContainer.m_inventory = _backpackEquipped.GetInventory();

InventoryGui.instance.Show(_backpackContainer);
}

public static bool CheckForInception(Inventory __instance, ItemDrop.ItemData item)
{
if (__instance.m_name.Equals(Backpacks.BackpacksInventoryName))
if (__instance.IsBackPackInventory())
{
// If the item is a backpack...
if (Backpacks.BackpackTypes.Contains(item.m_shared.m_name))
if (item.IsBackpack())
{
if (Player.m_localPlayer != null)
{
Expand All @@ -325,30 +263,5 @@ public static bool CheckForInception(Inventory __instance, ItemDrop.ItemData ite

return true;
}

public static void QuickDropBackpack()
{
_log.Message("Quick dropping backpack.");

if (Player.m_localPlayer == null)
return;

var player = Player.m_localPlayer;
var backpack = GetEquippedBackpack();

if (backpack == null)
return;

// Unequip and remove backpack from player's back
// We need to unequip the item BEFORE we drop it, otherwise when we pick it up again the game thinks
// we had it equipped all along and fails to update player model, resulting in invisible backpack.
player.RemoveEquipAction(backpack.Item);
player.UnequipItem(backpack.Item, true);
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.forward + player.transform.up, player.transform.rotation);
itemDrop.Save();
}
}
}
14 changes: 10 additions & 4 deletions AdventureBackpacks/Configuration/ConfigRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

namespace AdventureBackpacks.Configuration
{
internal class ConfigRegistry : ConfigSyncBase
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<bool> CloseInventory { get; private set;}
internal static ConfigEntry<bool> OutwardMode { get; private set;}
internal static ConfigEntry<Vector2> RuggedBackpackSize { get; private set;}
internal static ConfigEntry<Vector2> ArcticBackpackSize { get; private set;}
Expand All @@ -24,9 +25,9 @@ internal class ConfigRegistry : ConfigSyncBase

public ConfigRegistry(IPluginInfo mod): base(mod)
{

InitializeConfigurationSettings();
}
public override void InitializeConfigurationSettings()
public sealed override void InitializeConfigurationSettings()
{
if (_config == null)
return;
Expand All @@ -41,8 +42,13 @@ public override void InitializeConfigurationSettings()
null,
new ConfigAttributes { 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.",
null, new ConfigAttributes { Order = 1 }));

OutwardMode = _config.Bind(
"Local config", "Outward Mode", false,
"Local Config", "Outward Mode", false,
new ConfigDescription("You can use a hotkey to quickly drop your equipped backpack in order to run faster away from danger.",
null, new ConfigAttributes { Order = 1 }));

Expand Down
11 changes: 11 additions & 0 deletions AdventureBackpacks/Extensions/InventoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using AdventureBackpacks.Assets;

namespace AdventureBackpacks.Extensions;

public static class InventoryExtensions
{
public static bool IsBackPackInventory(this Inventory inventory)
{
return inventory.m_name.Equals(Backpacks.BackpacksInventoryName);
}
}
11 changes: 11 additions & 0 deletions AdventureBackpacks/Extensions/ItemDataExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using AdventureBackpacks.Assets;

namespace AdventureBackpacks.Extensions;

public static class ItemDataExtensions
{
public static bool IsBackpack(this ItemDrop.ItemData item)
{
return Backpacks.BackpackTypes.Contains(item.m_shared.m_name);
}
}
Loading

0 comments on commit ac66a02

Please sign in to comment.