using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using Construction.Anim;
using HarmonyLib;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Sons.Gameplay.GPS;
using TMPro;
using TheForest;
using TheForest.Items.Inventory;
using TheForest.Items.Special;
using TheForest.Utils;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("LogCarryAmountFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LogCarryAmountFix")]
[assembly: AssemblyTitle("LogCarryAmountFix")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace LogCapacity;
[BepInPlugin("SmokyAce.LogCarryAmountFix", "LogCarryAmountFix", "1.5.1")]
public class LogCapacity : BasePlugin
{
private struct ExtraLogVisual
{
public GameObject Clone;
public GameObject Source;
public int Slot;
}
public const string GUID = "SmokyAce.LogCarryAmountFix";
public const string NAME = "LogCarryAmountFix";
public const string VERSION = "1.5.1";
public const string AUTHOR = "ThirtyTwelve & SmokyAce";
internal static ConfigFile configFile;
internal static ConfigEntry<int> MaxLogCapacity;
internal static ConfigEntry<int> LogsInHand;
internal static ConfigEntry<bool> ToggleMaxLogCapacity;
internal static ConfigEntry<bool> StoreLogs;
internal static ConfigEntry<KeyCode> StoreLogsKey;
internal static ConfigEntry<bool> ToggleUI;
internal static ConfigEntry<bool> LegacyLogHack;
internal static ConfigEntry<bool> LogToConsole;
internal static ConfigEntry<bool> ToggleLogCapacity;
internal static ConfigEntry<float> awaitGiveLog;
internal static ConfigEntry<float> UIPOSX;
internal static ConfigEntry<float> UIPOSY;
internal static ManualLogSource DLog;
private static bool loggedVisibilityFailure;
private static bool loggedExtraVisualFailure;
private static bool dumpedHierarchy;
private static LogVisibilityController extraVisualsOwner;
private static readonly List<ExtraLogVisual> extraVisuals;
public override void Load()
{
//IL_005d: Unknown result type (might be due to invalid IL or missing references)
//IL_0062: Unknown result type (might be due to invalid IL or missing references)
//IL_006a: Unknown result type (might be due to invalid IL or missing references)
//IL_0076: Expected O, but got Unknown
Logger.Sources.Add((ILogSource)(object)DLog);
Harmony.CreateAndPatchAll(typeof(LogPatcher), (string)null);
Harmony.CreateAndPatchAll(typeof(MaxAmountPatch), (string)null);
Harmony.CreateAndPatchAll(typeof(LiftPatch), (string)null);
Harmony.CreateAndPatchAll(typeof(BoltSyncPatch), (string)null);
ClassInjector.RegisterTypeInIl2Cpp<CustomComponent>();
GameObject val = new GameObject("CustomGameObject")
{
hideFlags = (HideFlags)61
};
val.AddComponent<CustomComponent>();
Object.DontDestroyOnLoad((Object)val);
((BasePlugin)this).Log.LogInfo((object)"Loaded LogCarryAmountFix 1.5.1 by ThirtyTwelve & SmokyAce");
}
static LogCapacity()
{
//IL_001a: Unknown result type (might be due to invalid IL or missing references)
//IL_0024: Expected O, but got Unknown
//IL_0046: Unknown result type (might be due to invalid IL or missing references)
//IL_0050: Expected O, but got Unknown
//IL_0076: Unknown result type (might be due to invalid IL or missing references)
//IL_0080: Expected O, but got Unknown
//IL_0148: Unknown result type (might be due to invalid IL or missing references)
//IL_0152: Expected O, but got Unknown
//IL_017b: Unknown result type (might be due to invalid IL or missing references)
//IL_0185: Expected O, but got Unknown
//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
//IL_01ca: Expected O, but got Unknown
//IL_0205: Unknown result type (might be due to invalid IL or missing references)
//IL_020f: Expected O, but got Unknown
//IL_024a: Unknown result type (might be due to invalid IL or missing references)
//IL_0254: Expected O, but got Unknown
//IL_025e: Unknown result type (might be due to invalid IL or missing references)
//IL_0268: Expected O, but got Unknown
extraVisuals = new List<ExtraLogVisual>();
configFile = new ConfigFile(Path.Combine(Paths.ConfigPath, "LogCarryAmountFix.cfg"), true);
MaxLogCapacity = configFile.Bind<int>("General", "LogCapacityAmount", 4, new ConfigDescription("Max amount of logs you can carry", (AcceptableValueBase)(object)new AcceptableValueRange<int>(2, 100), Array.Empty<object>()));
LogsInHand = configFile.Bind<int>("General", "LogsInHand", 4, new ConfigDescription("How many logs are held in hand before the rest go to the stash", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 4), Array.Empty<object>()));
ToggleMaxLogCapacity = configFile.Bind<bool>("General", "MaxLogCapacity", true, "Limits");
StoreLogs = configFile.Bind<bool>("General", "StoreLogs", false, "Makes it possible to store logs");
StoreLogsKey = configFile.Bind<KeyCode>("General", "StoreLogsKey", (KeyCode)262, "Key For Storing Logs");
ToggleUI = configFile.Bind<bool>("General", "LogCounterUI", true, "Log Counter On Screen");
LegacyLogHack = configFile.Bind<bool>("General", "Loghack method", false, "Different Log types not supported, Only recommended if experiencing problems");
LogToConsole = configFile.Bind<bool>("General", "ShowLogs", false, new ConfigDescription("Logs will be displayed in the console", (AcceptableValueBase)null, new object[1] { "Advanced" }));
ToggleLogCapacity = configFile.Bind<bool>("Advanced", "LogCapacity", true, new ConfigDescription("The entire Mod", (AcceptableValueBase)null, new object[1] { "Advanced" }));
awaitGiveLog = configFile.Bind<float>("Advanced", "AwaitLog", 0.8f, new ConfigDescription("Delay against voiding logs", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), new object[1] { "Advanced" }));
UIPOSX = configFile.Bind<float>("UIPos, WIP requires relaunch", "X Position", 540f, new ConfigDescription("defines the x position of logs UI", (AcceptableValueBase)(object)new AcceptableValueRange<float>(-500f, 666f), new object[1] { "Advanced" }));
UIPOSY = configFile.Bind<float>("UIPos, WIP requires relaunch", "Y Position", 160f, new ConfigDescription("defines the z position of logs UI", (AcceptableValueBase)(object)new AcceptableValueRange<float>(-50f, 590f), new object[1] { "Advanced" }));
DLog = new ManualLogSource("DLog");
}
private static LogVisibilityController GetLogVisibilityController()
{
GameObject gameObject = LocalPlayer.GameObject;
if ((Object)(object)gameObject == (Object)null || gameObject.transform.childCount <= 6)
{
return null;
}
Transform child = gameObject.transform.GetChild(6);
if (child.childCount <= 8)
{
return null;
}
return ((Component)child.GetChild(8)).gameObject.GetComponent<LogVisibilityController>();
}
internal static void SetLogVisibility(bool show)
{
try
{
LogVisibilityController logVisibilityController = GetLogVisibilityController();
if ((Object)(object)logVisibilityController == (Object)null)
{
return;
}
for (int i = 0; i < 9; i++)
{
if (i < 4)
{
logVisibilityController.SetAdditionalLogVisibility(show);
}
else
{
if (i <= 4)
{
continue;
}
GameObject heldLogVisibilityOverride = logVisibilityController._heldLogVisibilityOverride;
if (!((Object)(object)heldLogVisibilityOverride == (Object)null) && heldLogVisibilityOverride.transform.childCount > i)
{
Transform child = heldLogVisibilityOverride.transform.GetChild(i);
if (child.childCount > 1)
{
((Component)child.GetChild(1)).gameObject.SetActive(show);
}
}
}
}
}
catch (Exception ex)
{
if (!loggedVisibilityFailure)
{
loggedVisibilityFailure = true;
DLog.LogWarning((object)("Could not update extra log visibility: " + ex.Message));
}
}
}
internal static void UpdateExtraLogVisuals(int heldCount)
{
try
{
LogVisibilityController logVisibilityController = GetLogVisibilityController();
if ((Object)(object)logVisibilityController == (Object)null || (Object)(object)logVisibilityController._heldLogVisibilityOverride == (Object)null)
{
return;
}
if ((Object)(object)logVisibilityController != (Object)(object)extraVisualsOwner)
{
BuildExtraLogVisuals(logVisibilityController);
}
foreach (ExtraLogVisual extraVisual in extraVisuals)
{
if (!((Object)(object)extraVisual.Clone == (Object)null) && !((Object)(object)extraVisual.Source == (Object)null))
{
bool flag = heldCount > extraVisual.Slot && extraVisual.Source.activeSelf;
if (extraVisual.Clone.activeSelf != flag)
{
extraVisual.Clone.SetActive(flag);
}
}
}
}
catch (Exception ex)
{
if (!loggedExtraVisualFailure)
{
loggedExtraVisualFailure = true;
DLog.LogWarning((object)("Could not update logs 3+ in hand: " + ex.Message));
}
}
}
private static void BuildExtraLogVisuals(LogVisibilityController controller)
{
//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
//IL_0113: Unknown result type (might be due to invalid IL or missing references)
//IL_0118: Unknown result type (might be due to invalid IL or missing references)
//IL_011f: Unknown result type (might be due to invalid IL or missing references)
//IL_0124: Unknown result type (might be due to invalid IL or missing references)
//IL_0137: Unknown result type (might be due to invalid IL or missing references)
//IL_014a: Unknown result type (might be due to invalid IL or missing references)
foreach (ExtraLogVisual extraVisual in extraVisuals)
{
if ((Object)(object)extraVisual.Clone != (Object)null)
{
Object.Destroy((Object)(object)extraVisual.Clone);
}
}
extraVisuals.Clear();
extraVisualsOwner = controller;
if (LogToConsole.Value && !dumpedHierarchy)
{
dumpedHierarchy = true;
DumpHierarchy(controller._heldLogVisibilityOverride.transform, 0);
}
Transform transform = controller._heldLogVisibilityOverride.transform;
for (int i = 0; i < transform.childCount; i++)
{
Transform child = transform.GetChild(i);
if (child.childCount >= 2)
{
Transform child2 = child.GetChild(0);
Transform child3 = child.GetChild(1);
Vector3 val = child3.localPosition - child2.localPosition;
for (int j = 2; j < LogsInHand.Value; j++)
{
GameObject val2 = Object.Instantiate<GameObject>(((Component)child3).gameObject, child);
((Object)val2).name = string.Format("{0}_Log{1}", "LogCarryAmountFix", j + 1);
val2.transform.localPosition = child3.localPosition + val * (float)(j - 1);
val2.transform.localRotation = child3.localRotation;
val2.transform.localScale = child3.localScale;
val2.SetActive(false);
extraVisuals.Add(new ExtraLogVisual
{
Clone = val2,
Source = ((Component)child3).gameObject,
Slot = j
});
}
}
}
DLogInfo($"Created {extraVisuals.Count} extra held log models", rateLimit: false);
}
private static void DumpHierarchy(Transform t, int depth)
{
DLog.LogInfo((object)$"{new string(' ', depth * 2)}{((Object)t).name} active={((Component)t).gameObject.activeSelf} children={t.childCount}");
if (depth < 3)
{
for (int i = 0; i < t.childCount; i++)
{
DumpHierarchy(t.GetChild(i), depth + 1);
}
}
}
internal static void DLogInfo(string message, bool rateLimit)
{
if (LogToConsole.Value && (!rateLimit || Time.frameCount % 15 == 0))
{
DLog.LogInfo((object)message);
}
}
}
public class CustomComponent : MonoBehaviour
{
internal static int bufferw;
private static int lastHeldCount;
private static bool infinitewood;
private static int helditem;
internal static GameObject panel;
internal static TextMeshProUGUI LogText;
private bool isLogsStored;
private bool shouldGiveLog;
private bool forceGiveLog;
private float timerGiveLog;
private bool loggedMissingController;
public CustomComponent(IntPtr ptr)
: base(ptr)
{
}
private void Update()
{
//IL_0090: Unknown result type (might be due to invalid IL or missing references)
if (!LocalPlayer.IsInWorld || !LogCapacity.ToggleLogCapacity.Value)
{
return;
}
HeldOnlyItemController controller = GetController();
if ((Object)(object)controller == (Object)null)
{
if (!loggedMissingController)
{
loggedMissingController = true;
LogCapacity.DLog.LogWarning((object)"HeldOnlyItemController was not available. Carry changes are paused until it exists.");
}
return;
}
if (LogCapacity.LegacyLogHack.Value && (Object)(object)DebugConsole.Instance != (Object)null)
{
DebugConsole.Instance._loghack(infinitewood ? "on" : "off");
}
int num = controller._heldCount;
if (LogCapacity.StoreLogs.Value && Input.GetKeyDown(LogCapacity.StoreLogsKey.Value))
{
StoreLog(controller, num);
}
if (isLogsStored)
{
LogCapacity.UpdateExtraLogVisuals(num);
return;
}
int num2 = (LogCapacity.LegacyLogHack.Value ? 1 : LogCapacity.LogsInHand.Value);
if (num >= 1)
{
helditem = controller._heldItemId;
}
if (num > num2)
{
LogCapacity.DLogInfo($"stash {num - num2}", rateLimit: false);
bufferw += num - num2;
controller._heldCount = num2;
num = num2;
}
else if (!LogCapacity.LegacyLogHack.Value && num >= 1 && num < num2 && bufferw > 0)
{
int num3 = Math.Min(num2 - num, bufferw);
LogCapacity.DLogInfo($"unstash {num3}", rateLimit: false);
bufferw -= num3;
num = (controller._heldCount = num + num3);
}
if (num != lastHeldCount)
{
if (num >= 2)
{
LogCapacity.SetLogVisibility(show: true);
}
lastHeldCount = num;
}
LogCapacity.UpdateExtraLogVisuals(num);
if (num == 1)
{
if (infinitewood)
{
infinitewood = false;
}
LogCapacity.SetLogVisibility(show: false);
}
else if (LogCapacity.LegacyLogHack.Value && !infinitewood && bufferw > 0 && !controller.HasItem && !InWater())
{
infinitewood = true;
bufferw--;
}
else if (!LogCapacity.LegacyLogHack.Value && bufferw > 0 && num < 1 && !InWater())
{
LogCapacity.DLogInfo("default", rateLimit: true);
shouldGiveLog = true;
}
if (shouldGiveLog && InWater())
{
shouldGiveLog = false;
forceGiveLog = false;
timerGiveLog = 0f;
}
if (shouldGiveLog)
{
timerGiveLog += Time.deltaTime;
if (timerGiveLog >= LogCapacity.awaitGiveLog.Value || forceGiveLog)
{
GiveLog();
timerGiveLog = 0f;
shouldGiveLog = false;
forceGiveLog = false;
}
else if (AreEmpty())
{
forceGiveLog = true;
}
}
int num5 = bufferw + num;
if (num5 >= 1)
{
if (LogCapacity.ToggleUI.Value && (Object)(object)LogText != (Object)null && (Object)(object)panel != (Object)null)
{
((TMP_Text)LogText).SetText(num5 + WoodType(helditem));
panel.SetActive(true);
}
LogCapacity.DLogInfo(num5 + WoodType(helditem), rateLimit: true);
}
else if ((Object)(object)panel != (Object)null)
{
panel.SetActive(false);
}
}
private void StoreLog(HeldOnlyItemController controller, int heldCount)
{
if (!isLogsStored)
{
isLogsStored = true;
LogCapacity.DLogInfo("In heldCount = " + heldCount, rateLimit: false);
bufferw += heldCount;
controller._heldCount = 1;
LogCapacity.SetLogVisibility(show: false);
PlayerInventory inventory = LocalPlayer.Inventory;
inventory.RemoveItem(helditem, 1, false, true, true, inventory.HeldOnlyItemController.HeldItem, true);
}
else if (isLogsStored)
{
isLogsStored = false;
}
}
private void GiveLog()
{
if (LocalPlayer.Inventory.TryEquip(helditem, false, false, false))
{
bufferw--;
}
}
private static bool InWater()
{
PlayerAnimatorControl animControl = LocalPlayer.AnimControl;
if (!LocalPlayer.IsUnderwater)
{
if ((Object)(object)animControl != (Object)null)
{
return animControl.IsSwimming;
}
return false;
}
return true;
}
private static bool AreEmpty()
{
//IL_0034: Unknown result type (might be due to invalid IL or missing references)
//IL_003a: Invalid comparison between Unknown and I4
PlayerInventory inventory = LocalPlayer.Inventory;
if (inventory.IsRightHandEmpty() && inventory.IsLeftHandEmpty() && inventory.HasRoomFor(helditem, 2) && !inventory.IsInventoryToggleBlocked() && !LocalPlayer.IsUnderwater)
{
return (int)inventory._currentEquipState == 2;
}
return false;
}
private static string WoodType(int itemId)
{
switch (itemId)
{
case 78:
case 406:
case 408:
case 409:
return " Logs";
case 395:
case 576:
case 577:
case 578:
return " Planks";
case 640:
return " Stones";
default:
return " null";
}
}
private static HeldOnlyItemController GetController()
{
PlayerInventory inventory = LocalPlayer.Inventory;
if ((Object)(object)inventory == (Object)null || inventory.HeldOnlyItemController == null)
{
return null;
}
return ((Il2CppObjectBase)inventory.HeldOnlyItemController).TryCast<HeldOnlyItemController>();
}
}
[HarmonyPatch(typeof(GPSTrackerSystem), "OnEnable")]
internal static class LogPatcher
{
[HarmonyPostfix]
private static void OnEnablePostfix()
{
LogCapacity.DLog.LogInfo((object)"IN From GPS OnEnablePostfix");
if ((Object)(object)CustomComponent.panel == (Object)null)
{
loadUI();
}
}
private static void loadUI()
{
//IL_0042: Unknown result type (might be due to invalid IL or missing references)
//IL_004c: Expected O, but got Unknown
//IL_007a: Unknown result type (might be due to invalid IL or missing references)
//IL_0089: Unknown result type (might be due to invalid IL or missing references)
//IL_008e: Unknown result type (might be due to invalid IL or missing references)
//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
if (LogCapacity.LogToConsole.Value)
{
LogCapacity.DLog.LogInfo((object)"IN loadUI");
}
if (LogCapacity.ToggleUI.Value)
{
GameObject val = GameObject.Find("ToggleableHUD");
if (!((Object)(object)val == (Object)null))
{
CustomComponent.panel = new GameObject("LogCountUi");
CustomComponent.panel.transform.SetParent(val.transform);
CustomComponent.panel.transform.localPosition = new Vector3(0f, -460f, 0f);
GameObject val2 = new GameObject("LogCountText");
CustomComponent.LogText = val2.AddComponent<TextMeshProUGUI>();
((TMP_Text)CustomComponent.LogText).SetText("LogCount");
((TMP_Text)CustomComponent.LogText).fontSize = 36f;
val2.transform.SetParent(CustomComponent.panel.transform);
((Transform)((TMP_Text)CustomComponent.LogText).rectTransform).localPosition = new Vector3(LogCapacity.UIPOSX.Value, LogCapacity.UIPOSY.Value, 0f);
CustomComponent.panel.SetActive(false);
}
}
}
}
[HarmonyPatch(typeof(PlayerInventory), "GetMaxAmountOf")]
internal static class MaxAmountPatch
{
private static void Postfix(PlayerInventory __instance, int itemId, ref int __result)
{
if (LogCapacity.ToggleLogCapacity.Value && __result < 2)
{
IHeldOnlyItemController val = ((__instance != null) ? __instance.HeldOnlyItemController : null);
if (val != null && val.Controlling(itemId))
{
__result = 2;
}
}
}
}
[HarmonyPatch(typeof(HeldOnlyItemController), "Lift")]
internal static class LiftPatch
{
private static bool Prefix(HeldOnlyItemController __instance, int itemId, ref bool __result, out int __state)
{
__state = -1;
if (!LogCapacity.ToggleLogCapacity.Value || LogCapacity.LegacyLogHack.Value || !LocalPlayer.IsInWorld)
{
return true;
}
PlayerInventory inventory = LocalPlayer.Inventory;
IHeldOnlyItemController val = ((inventory != null) ? inventory.HeldOnlyItemController : null);
if (val == null || ((Il2CppObjectBase)val).Pointer != ((Il2CppObjectBase)__instance).Pointer)
{
return true;
}
int heldCount = __instance._heldCount;
if (heldCount >= 1 && __instance._heldItemId != itemId)
{
return true;
}
int num = CustomComponent.bufferw + heldCount;
if (LogCapacity.ToggleMaxLogCapacity.Value && num >= LogCapacity.MaxLogCapacity.Value)
{
__result = false;
return false;
}
if (heldCount >= 2)
{
__state = heldCount;
__instance._heldCount = 1;
}
return true;
}
private static void Postfix(HeldOnlyItemController __instance, bool __result, int __state)
{
if (__state >= 0)
{
__instance._heldCount = (__result ? (__state + 1) : __state);
}
}
}
[HarmonyPatch(typeof(HeldOnlyItemController), "UpdateBoltEntity")]
internal static class BoltSyncPatch
{
private static void Prefix(HeldOnlyItemController __instance, out int __state)
{
__state = __instance._heldCount;
if (__state > 2)
{
__instance._heldCount = 2;
}
}
private static void Postfix(HeldOnlyItemController __instance, int __state)
{
if (__state > 2)
{
__instance._heldCount = __state;
}
}
}