using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
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: AssemblyVersion("0.0.0.0")]
namespace TreasureMapOverhaul;
[HarmonyPatch(typeof(GlobalBank), "LoadBank")]
public static class BankPatch
{
[HarmonyPostfix]
public static void OnBankLoad(GlobalBank __instance)
{
try
{
if ((Object)(object)__instance == (Object)null)
{
Plugin.Log.LogError((object)"[TMO] GlobalBank instance is null in BankPatch.OnBankLoad.");
return;
}
ReplaceOldAndAddBank(__instance);
try
{
__instance.DisplayBankPage();
}
catch (Exception arg)
{
Plugin.Log.LogError((object)$"[TMO] Failed to refresh bank UI: {arg}");
}
}
catch (Exception arg2)
{
Plugin.Log.LogError((object)$"[TMO] Exception in BankPatch.OnBankLoad: {arg2}");
}
}
private static void ReplaceOldAndAddBank(GlobalBank bank)
{
try
{
if (bank.StoredItems == null || bank.Quantities == null)
{
Plugin.Log.LogError((object)"[TMO] StoredItems or Quantities is null in BankPatch.");
return;
}
string[] array = new string[4] { "28362792", "270986", "6188236", "28043030" };
int num = 0;
for (int i = 0; i < bank.StoredItems.Count; i++)
{
Item val = bank.StoredItems[i];
if ((Object)(object)val != (Object)null && Array.IndexOf(array, ((BaseScriptableObject)val).Id) >= 0)
{
num += bank.Quantities[i];
bank.StoredItems[i] = GameData.PlayerInv.Empty;
bank.Quantities[i] = 0;
}
}
if (num <= 0)
{
return;
}
ItemDatabase itemDB = GameData.ItemDB;
Item torn = ((itemDB != null) ? itemDB.GetItemByID("et508.tornmap") : null);
if ((Object)(object)torn == (Object)null)
{
Plugin.Log.LogError((object)"[TMO] Torn map not found in ItemDB in BankPatch.");
return;
}
int num2 = bank.StoredItems.FindIndex((Item x) => (Object)(object)x == (Object)(object)torn);
if (num2 >= 0)
{
bank.Quantities[num2] += num;
return;
}
int num3 = bank.StoredItems.FindIndex((Item x) => (Object)(object)x == (Object)(object)GameData.PlayerInv.Empty);
if (num3 >= 0)
{
bank.StoredItems[num3] = torn;
bank.Quantities[num3] = num;
}
else
{
Plugin.Log.LogError((object)"[TMO] No empty bank slot available to add torn maps in BankPatch.");
}
}
catch (Exception arg)
{
Plugin.Log.LogError((object)$"[TMO] Exception in BankPatch.ReplaceOldAndAddBank: {arg}");
}
}
}
[HarmonyPatch(typeof(CharSelectManager), "Play")]
public static class InventoryPatch
{
[HarmonyPostfix]
public static void OnCharacterEnter()
{
try
{
Inventory playerInv = GameData.PlayerInv;
if ((Object)(object)playerInv == (Object)null)
{
Plugin.Log.LogError((object)"[TMO] PlayerInv is null in InventoryPatch.OnCharacterEnter.");
}
else if (playerInv.StoredSlots == null)
{
Plugin.Log.LogError((object)"[TMO] StoredSlots is null in InventoryPatch.OnCharacterEnter.");
}
else
{
ReplaceOldAndAdd(playerInv);
}
}
catch (Exception arg)
{
Plugin.Log.LogError((object)$"[TMO] Exception in InventoryPatch.OnCharacterEnter: {arg}");
}
}
private static void ReplaceOldAndAdd(Inventory inv)
{
try
{
string[] array = new string[4] { "28362792", "270986", "6188236", "28043030" };
int num = 0;
List<ItemIcon> storedSlots = inv.StoredSlots;
if (storedSlots == null)
{
Plugin.Log.LogError((object)"[TMO] StoredSlots is null in InventoryPatch.ReplaceOldAndAdd.");
return;
}
foreach (ItemIcon item in storedSlots)
{
Item myItem = item.MyItem;
if ((Object)(object)myItem != (Object)null && Array.IndexOf(array, ((BaseScriptableObject)myItem).Id) >= 0)
{
num += item.Quantity;
item.MyItem = inv.Empty;
item.Quantity = 1;
item.UpdateSlotImage();
}
}
if (num == 0)
{
return;
}
ItemDatabase itemDB = GameData.ItemDB;
Item val = ((itemDB != null) ? itemDB.GetItemByID("et508.tornmap") : null);
if ((Object)(object)val == (Object)null)
{
Plugin.Log.LogError((object)"[TMO] Torn map not found in ItemDB in ReplaceOldAndAdd.");
return;
}
for (int i = 0; i < num; i++)
{
if (!inv.AddItemToInv(val))
{
Plugin.Log.LogError((object)$"[TMO] Failed to add torn map instance {i + 1}/{num} in ReplaceOldAndAdd.");
}
}
}
catch (Exception arg)
{
Plugin.Log.LogError((object)$"[TMO] Exception in InventoryPatch.ReplaceOldAndAdd: {arg}");
}
}
}
[HarmonyPatch(typeof(ItemDatabase), "Start")]
public static class ItemDBPatch
{
[HarmonyPostfix]
public static void InjectCustomItem(ItemDatabase __instance)
{
//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
try
{
if ((Object)(object)__instance == (Object)null || __instance.ItemDB == null)
{
Plugin.Log.LogError((object)"[TMO] ItemDatabase instance or ItemDB array is null in InjectCustomItem.");
return;
}
List<Item> list = __instance.ItemDB.ToList();
Item itemByID = __instance.GetItemByID("28362792");
if ((Object)(object)itemByID == (Object)null)
{
Plugin.Log.LogError((object)"[TMO] Base item with ID '28362792' not found in ItemDatabase.");
return;
}
Item val = Object.Instantiate<Item>(itemByID);
if ((Object)(object)val == (Object)null)
{
Plugin.Log.LogError((object)"[TMO] Failed to instantiate clone of base map item.");
return;
}
((BaseScriptableObject)val).Id = "et508.tornmap";
val.ItemName = "A Torn Treasure Map";
val.Lore = "A piece of an old map.";
val.Stackable = true;
val.Unique = true;
val.ItemValue = 0;
val.RequiredSlot = (SlotType)0;
list.Add(val);
__instance.ItemDB = list.ToArray();
}
catch (Exception arg)
{
Plugin.Log.LogError((object)$"[TMO] Exception in ItemDBPatch.InjectCustomItem: {arg}");
}
}
}
[HarmonyPatch(typeof(LootTable), "InitLootTable")]
public static class LootInitPatch
{
[HarmonyPostfix]
public static void OnInitLoot(LootTable __instance)
{
try
{
if ((Object)(object)__instance == (Object)null)
{
Plugin.Log.LogError((object)"[TMO] LootInitPatch: LootTable instance is null.");
return;
}
if (__instance.ActualDrops == null)
{
Plugin.Log.LogError((object)"[TMO] LootInitPatch: ActualDrops list is null.");
return;
}
string[] oldIds = new string[4] { "28362792", "270986", "6188236", "28043030" };
__instance.ActualDrops.RemoveAll((Item item) => (Object)(object)item != (Object)null && Array.IndexOf(oldIds, ((BaseScriptableObject)item).Id) >= 0);
ItemDatabase itemDB = GameData.ItemDB;
Item val = ((itemDB != null) ? itemDB.GetItemByID("et508.tornmap") : null);
if ((Object)(object)val == (Object)null)
{
Plugin.Log.LogError((object)"[TMO] LootInitPatch: Torn map not found in ItemDB.");
return;
}
float normalizedChance = Plugin.GetNormalizedChance(Plugin.TreasureMapDropChancePercent);
if (!__instance.ActualDrops.Contains(val) && Random.value < normalizedChance)
{
__instance.ActualDrops.Add(val);
__instance.special = true;
}
}
catch (Exception arg)
{
Plugin.Log.LogError((object)$"[TMO] Exception in LootInitPatch.OnInitLoot: {arg}");
}
}
}
[BepInPlugin("et508.erenshor.treasuremapoverhaul", "Treasure Map Overhaul", "1.0.3")]
public class Plugin : BaseUnityPlugin
{
internal static ManualLogSource Log;
public static ConfigEntry<float> TreasureMapDropChancePercent;
private static StreamWriter errorLog;
private static string errorLogPath;
private void Awake()
{
//IL_002f: Unknown result type (might be due to invalid IL or missing references)
//IL_0039: Expected O, but got Unknown
//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
//IL_00b8: Expected O, but got Unknown
//IL_00be: Unknown result type (might be due to invalid IL or missing references)
//IL_00c4: Expected O, but got Unknown
TreasureMapDropChancePercent = ((BaseUnityPlugin)this).Config.Bind<float>("Drop Chance", "TreasureMapDropChance", 2f, new ConfigDescription("Chance to drop A Torn Treasure Map (0.0-100%). Default: 2.0%. Reload scene or wait for new respawns for changes to apply.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
Log = ((BaseUnityPlugin)this).Logger;
Log.LogInfo((object)"Treasure Map Overhaul loaded.");
try
{
string directoryName = Path.GetDirectoryName(typeof(Plugin).Assembly.Location);
errorLogPath = Path.Combine(directoryName, "TreasureMapOverhaul_errors.log");
}
catch (Exception ex)
{
Log.LogError((object)("[TMO] Failed to prepare error log path: " + ex.Message));
}
Application.logMessageReceived += new LogCallback(HandleLog);
Harmony val = new Harmony("et508.erenshor.treasuremapoverhaul");
val.PatchAll();
}
private void OnDestroy()
{
//IL_0008: Unknown result type (might be due to invalid IL or missing references)
//IL_0012: Expected O, but got Unknown
Application.logMessageReceived -= new LogCallback(HandleLog);
errorLog?.Close();
}
private void HandleLog(string condition, string stackTrace, LogType type)
{
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_0004: Unknown result type (might be due to invalid IL or missing references)
//IL_0006: Invalid comparison between Unknown and I4
//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
if (((int)type != 0 && (int)type != 4) || (!condition.Contains("[TMO]") && !stackTrace.Contains("TreasureMapOverhaul")))
{
return;
}
if (errorLog == null)
{
try
{
errorLog = new StreamWriter(errorLogPath, append: true)
{
AutoFlush = true
};
Log.LogInfo((object)("[TMO] Error log file initialized at " + errorLogPath));
}
catch (Exception ex)
{
Log.LogError((object)("[TMO] Failed to create error log file: " + ex.Message));
return;
}
}
try
{
errorLog.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{type}] {condition}");
errorLog.WriteLine(stackTrace);
errorLog.WriteLine();
}
catch
{
}
}
public static float GetNormalizedChance(ConfigEntry<float> entry)
{
return Mathf.Clamp01(entry.Value / 100f);
}
}
[HarmonyPatch(typeof(CharSelectManager), "Play")]
public static class QuestPatch
{
[HarmonyPostfix]
public static void OnCharacterEnter()
{
try
{
if ((Object)(object)GameData.QuestDB == (Object)null)
{
Plugin.Log.LogError((object)"[TMO] QuestDB not initialized in QuestPatch.OnCharacterEnter.");
return;
}
if ((Object)(object)GameData.ItemDB == (Object)null)
{
Plugin.Log.LogError((object)"[TMO] ItemDB not initialized in QuestPatch.OnCharacterEnter.");
return;
}
string[] array = new string[4] { "28362792", "270986", "6188236", "28043030" };
Quest questByName = GameData.QuestDB.GetQuestByName("TreasureMap");
if ((Object)(object)questByName == (Object)null)
{
Plugin.Log.LogError((object)"[TMO] Quest 'TreasureMap' not found in QuestDB in QuestPatch.");
return;
}
Item itemByID = GameData.ItemDB.GetItemByID("et508.tornmap");
if ((Object)(object)itemByID == (Object)null)
{
Plugin.Log.LogError((object)"[TMO] Torn map not found in ItemDB in QuestPatch.");
return;
}
if (questByName.RequiredItems == null)
{
Plugin.Log.LogError((object)("[TMO] RequiredItems is null for quest '" + questByName.QuestName + "' in QuestPatch."));
return;
}
for (int i = 0; i < questByName.RequiredItems.Count; i++)
{
Item val = questByName.RequiredItems[i];
if ((Object)(object)val != (Object)null && Array.IndexOf(array, ((BaseScriptableObject)val).Id) >= 0)
{
questByName.RequiredItems[i] = itemByID;
}
}
}
catch (Exception arg)
{
Plugin.Log.LogError((object)$"[TMO] Exception in QuestPatch.OnCharacterEnter: {arg}");
}
}
}
[HarmonyPatch(typeof(Water), "Start")]
public static class WaterPatch
{
[HarmonyPostfix]
public static void OnWaterStart(Water __instance)
{
try
{
if ((Object)(object)__instance == (Object)null)
{
Plugin.Log.LogError((object)"[TMO] WaterPatch failed: Water instance is null.");
return;
}
ItemDatabase itemDB = GameData.ItemDB;
Item tornMap = ((itemDB != null) ? itemDB.GetItemByID("et508.tornmap") : null);
if ((Object)(object)tornMap == (Object)null)
{
Plugin.Log.LogError((object)"[TMO] Torn map not found in ItemDB! Fishable patch aborted.");
return;
}
ReplaceInList(__instance.DayFishables, "DayFishables");
ReplaceInList(__instance.NightFishables, "NightFishables");
ReplaceInList(__instance.Fishables, "Fishables");
void ReplaceInList(List<Item> list, string listName)
{
if (list == null)
{
Plugin.Log.LogError((object)("[TMO] " + listName + " list is null in WaterPatch."));
}
else
{
for (int i = 0; i < list.Count; i++)
{
Item val = list[i];
if ((Object)(object)val != (Object)null && ((BaseScriptableObject)val).Id == "28043030")
{
list[i] = tornMap;
}
}
}
}
}
catch (Exception arg)
{
Plugin.Log.LogError((object)$"[TMO] Exception in WaterPatch.OnWaterStart: {arg}");
}
}
}