using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("ResourceBoost")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+29145c229ca8241f5f2f8509bb5a58d8e681473c")]
[assembly: AssemblyProduct("ResourceBoost")]
[assembly: AssemblyTitle("ResourceBoost")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
internal sealed class NullableAttribute : Attribute
{
public readonly byte[] NullableFlags;
public NullableAttribute(byte P_0)
{
NullableFlags = new byte[1] { P_0 };
}
public NullableAttribute(byte[] P_0)
{
NullableFlags = P_0;
}
}
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
internal sealed class NullableContextAttribute : Attribute
{
public readonly byte Flag;
public NullableContextAttribute(byte P_0)
{
Flag = P_0;
}
}
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
internal sealed class RefSafetyRulesAttribute : Attribute
{
public readonly int Version;
public RefSafetyRulesAttribute(int P_0)
{
Version = P_0;
}
}
}
namespace ResourceBoost
{
[BepInPlugin("tenebrisreverie.resourceboost", "ResourceBoost", "1.2.1")]
public sealed class Plugin : BaseUnityPlugin
{
[HarmonyPatch(typeof(ZNetScene), "Awake")]
private static class ZNetScene_Awake_Patch
{
private static void Postfix(ZNetScene __instance)
{
_lastZNetScene = __instance;
if (!_didApply)
{
_didApply = true;
ReapplyAllMultipliers(__instance);
}
}
internal static void ReapplyAllMultipliers(ZNetScene scene)
{
int num = 0;
int discoveredCount = 0;
int discoveredCount2 = 0;
foreach (GameObject prefab in scene.m_prefabs)
{
if (!((Object)(object)prefab == (Object)null))
{
MineRock component = prefab.GetComponent<MineRock>();
if ((Object)(object)component != (Object)null && component.m_dropItems != null)
{
num += ApplyMultipliersFromDropTable(component.m_dropItems, ((Object)prefab).name, "Mining", MiningItemMultipliersByItemName, ref discoveredCount, "Mining");
}
MineRock5 component2 = prefab.GetComponent<MineRock5>();
if ((Object)(object)component2 != (Object)null && component2.m_dropItems != null)
{
num += ApplyMultipliersFromDropTable(component2.m_dropItems, ((Object)prefab).name, "Mining", MiningItemMultipliersByItemName, ref discoveredCount, "Mining");
}
TreeBase component3 = prefab.GetComponent<TreeBase>();
if ((Object)(object)component3 != (Object)null && component3.m_dropWhenDestroyed != null)
{
num += ApplyMultipliersFromDropTable(component3.m_dropWhenDestroyed, ((Object)prefab).name, "Lumber", LumberItemMultipliersByItemName, ref discoveredCount2, "Lumber");
}
TreeLog component4 = prefab.GetComponent<TreeLog>();
if ((Object)(object)component4 != (Object)null && component4.m_dropWhenDestroyed != null)
{
num += ApplyMultipliersFromDropTable(component4.m_dropWhenDestroyed, ((Object)prefab).name, "Lumber", LumberItemMultipliersByItemName, ref discoveredCount2, "Lumber");
}
}
}
LogInfo($"Discovered Mining items: {discoveredCount}, Lumber items: {discoveredCount2}");
LogInfo($"Reapplied changes. Modified drop entries: {num}");
}
private static int ApplyMultipliersFromDropTable(DropTable dropTable, string ownerPrefabName, string configSectionName, Dictionary<string, ConfigEntry<int>> multiplierCache, ref int discoveredCount, string logPrefix)
{
//IL_0012: Unknown result type (might be due to invalid IL or missing references)
//IL_0017: Unknown result type (might be due to invalid IL or missing references)
//IL_0018: Unknown result type (might be due to invalid IL or missing references)
//IL_002f: Unknown result type (might be due to invalid IL or missing references)
//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
//IL_0086: Unknown result type (might be due to invalid IL or missing references)
//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
//IL_008f: Unknown result type (might be due to invalid IL or missing references)
//IL_009a: Unknown result type (might be due to invalid IL or missing references)
//IL_011a: Unknown result type (might be due to invalid IL or missing references)
//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
int num = 0;
for (int i = 0; i < dropTable.m_drops.Count; i++)
{
DropData val = dropTable.m_drops[i];
if (!((Object)(object)val.m_item == (Object)null))
{
string name = ((Object)val.m_item).name;
int orCreateMultiplier = GetOrCreateMultiplier(configSectionName, name, multiplierCache, ref discoveredCount);
string key = $"{configSectionName}::{ownerPrefabName}::{i}::{name}";
if (!OriginalDropStacksByKey.TryGetValue(key, out (int, int) value))
{
int num2 = ((val.m_stackMin <= 0) ? 1 : val.m_stackMin);
int item = ((val.m_stackMax <= 0) ? num2 : val.m_stackMax);
value = (num2, item);
OriginalDropStacksByKey[key] = value;
}
int num3 = value.Item1 * orCreateMultiplier;
int num4 = value.Item2 * orCreateMultiplier;
if (val.m_stackMin != num3 || val.m_stackMax != num4)
{
val.m_stackMin = num3;
val.m_stackMax = num4;
dropTable.m_drops[i] = val;
num++;
LogInfo($"{logPrefix}: {ownerPrefabName} -> {name} x{orCreateMultiplier} (stack {num3}-{num4})");
}
}
}
return num;
}
private static DropData ScaleDrop(DropData drop, int multiplier)
{
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_000a: Unknown result type (might be due to invalid IL or missing references)
//IL_0014: Unknown result type (might be due to invalid IL or missing references)
//IL_001d: Unknown result type (might be due to invalid IL or missing references)
//IL_003b: Unknown result type (might be due to invalid IL or missing references)
//IL_003c: Unknown result type (might be due to invalid IL or missing references)
//IL_003f: Unknown result type (might be due to invalid IL or missing references)
int num = ((drop.m_stackMin <= 0) ? 1 : drop.m_stackMin);
int num2 = ((drop.m_stackMax <= 0) ? num : drop.m_stackMax);
drop.m_stackMin = num * multiplier;
drop.m_stackMax = num2 * multiplier;
return drop;
}
private static int GetOrCreateMultiplier(string sectionName, string itemPrefabName, Dictionary<string, ConfigEntry<int>> multiplierCache, ref int discoveredCount)
{
if (multiplierCache.TryGetValue(itemPrefabName, out ConfigEntry<int> value))
{
return Math.Max(1, value.Value);
}
if (_configFile == null)
{
return 1;
}
ConfigEntry<int> val2 = (multiplierCache[itemPrefabName] = _configFile.Bind<int>(sectionName, itemPrefabName, 1, "Multiplier for item '" + itemPrefabName + "' when dropped via " + sectionName + " sources."));
discoveredCount++;
return Math.Max(1, val2.Value);
}
}
private static ZNetScene? _lastZNetScene;
private static readonly Dictionary<string, (int min, int max)> OriginalDropStacksByKey = new Dictionary<string, (int, int)>(StringComparer.OrdinalIgnoreCase);
public const string ModGuid = "tenebrisreverie.resourceboost";
public const string ModName = "ResourceBoost";
public const string ModVersion = "1.2.1";
private static bool _didApply;
private static ConfigFile? _configFile;
private static ConfigEntry<bool>? _enableLogging;
private static readonly Dictionary<string, ConfigEntry<int>> MiningItemMultipliersByItemName = new Dictionary<string, ConfigEntry<int>>(StringComparer.OrdinalIgnoreCase);
private static bool _reapplyQueued;
private static float _reapplyAtTime;
private static readonly Dictionary<string, ConfigEntry<int>> LumberItemMultipliersByItemName = new Dictionary<string, ConfigEntry<int>>(StringComparer.OrdinalIgnoreCase);
private void Awake()
{
_configFile = ((BaseUnityPlugin)this).Config;
_enableLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "EnableLogging", false, "Enable extra logging to verify patches and counts.");
_configFile.SettingChanged += delegate
{
if (!((Object)(object)_lastZNetScene == (Object)null))
{
_reapplyQueued = true;
_reapplyAtTime = Time.time + 0.25f;
}
};
Harmony.CreateAndPatchAll(typeof(ZNetScene_Awake_Patch), "tenebrisreverie.resourceboost");
((BaseUnityPlugin)this).Logger.LogInfo((object)"ResourceBoost 1.2.1 loaded");
}
private void Update()
{
if (_reapplyQueued && !(Time.time < _reapplyAtTime) && !((Object)(object)_lastZNetScene == (Object)null))
{
_reapplyQueued = false;
ZNetScene_Awake_Patch.ReapplyAllMultipliers(_lastZNetScene);
}
}
private static void LogInfo(string message)
{
ConfigEntry<bool>? enableLogging = _enableLogging;
if (enableLogging != null && enableLogging.Value)
{
Debug.Log((object)("[ResourceBoost] " + message));
}
}
}
}