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.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using SunhavenMods.Shared;
using TrinketFortune.Patches;
using UnityEngine;
using UnityEngine.SceneManagement;
using Wish;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("TrinketFortune")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+5c08b5aa5d0be9c4b93df77f697dc55d5ac97088")]
[assembly: AssemblyProduct("TrinketFortune")]
[assembly: AssemblyTitle("TrinketFortune")]
[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 SunhavenMods.Shared
{
public static class ConfigFileHelper
{
public static ConfigFile CreateNamedConfig(string pluginGuid, string configFileName, Action<string> logWarning = null)
{
//IL_005f: Unknown result type (might be due to invalid IL or missing references)
//IL_0065: Expected O, but got Unknown
string text = Path.Combine(Paths.ConfigPath, configFileName);
string text2 = Path.Combine(Paths.ConfigPath, pluginGuid + ".cfg");
try
{
if (!File.Exists(text) && File.Exists(text2))
{
File.Copy(text2, text);
}
}
catch (Exception ex)
{
logWarning?.Invoke("[Config] Migration to " + configFileName + " failed: " + ex.Message);
}
return new ConfigFile(text, true);
}
public static bool ReplacePluginConfig(BaseUnityPlugin plugin, ConfigFile newConfig, Action<string> logWarning = null)
{
if ((Object)(object)plugin == (Object)null || newConfig == null)
{
return false;
}
try
{
Type typeFromHandle = typeof(BaseUnityPlugin);
PropertyInfo property = typeFromHandle.GetProperty("Config", BindingFlags.Instance | BindingFlags.Public);
if (property != null && property.CanWrite)
{
property.SetValue(plugin, newConfig, null);
return true;
}
FieldInfo field = typeFromHandle.GetField("<Config>k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic);
if (field != null)
{
field.SetValue(plugin, newConfig);
return true;
}
FieldInfo[] fields = typeFromHandle.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
foreach (FieldInfo fieldInfo in fields)
{
if (fieldInfo.FieldType == typeof(ConfigFile))
{
fieldInfo.SetValue(plugin, newConfig);
return true;
}
}
}
catch (Exception ex)
{
logWarning?.Invoke("[Config] ReplacePluginConfig failed: " + ex.Message);
}
return false;
}
}
}
namespace TrinketFortune
{
public static class Config
{
public static ConfigEntry<bool> Enabled;
public static ConfigEntry<float> MuseumProgressBonusPercent;
public static ConfigEntry<float> MinimumMuseumProgress;
public static ConfigEntry<float> MaxBonusChancePercent;
public static void Bind(ConfigFile config)
{
//IL_0044: Unknown result type (might be due to invalid IL or missing references)
//IL_004e: Expected O, but got Unknown
//IL_007c: Unknown result type (might be due to invalid IL or missing references)
//IL_0086: Expected O, but got Unknown
//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
//IL_00be: Expected O, but got Unknown
Enabled = config.Bind<bool>("General", "Enabled", true, "Enable Trinket Fortune. When enabled, odds of unowned fishing trinkets increase as you complete the aquarium.");
MuseumProgressBonusPercent = config.Bind<float>("General", "MuseumProgressBonusPercent", 5f, new ConfigDescription("Bonus to unowned trinket/fish odds per 10% aquarium completion (e.g. 5 = +5% per 10%). Applied on top of base drop chance.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 50f), Array.Empty<object>()));
MinimumMuseumProgress = config.Bind<float>("General", "MinimumMuseumProgress", 0.2f, new ConfigDescription("Museum progress below this has no bonus (0.2 = 20% donated). Prevents bonus at very low completion.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
MaxBonusChancePercent = config.Bind<float>("General", "MaxBonusChancePercent", 75f, new ConfigDescription("Maximum bonus chance cap (%). Clamps the computed bonus regardless of aquarium progress. E.g. 75 = never more than a 75% bonus chance.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 100f), Array.Empty<object>()));
}
}
public static class DonationHelper
{
private const string SmutGuid = "com.azraelgodking.sunhavenmuseumutilitytracker";
private static object _donationManager;
private static MethodInfo _hasDonatedByGameId;
private static float _lastResolveAttemptRealtime;
private const float ResolveRetrySeconds = 10f;
public static bool IsAvailable
{
get
{
EnsureResolved();
if (_donationManager != null)
{
return _hasDonatedByGameId != null;
}
return false;
}
}
static DonationHelper()
{
_lastResolveAttemptRealtime = -100f;
EnsureResolved(force: true);
}
private static void EnsureResolved(bool force = false)
{
if (!force && _donationManager != null && _hasDonatedByGameId != null)
{
return;
}
float realtimeSinceStartup = Time.realtimeSinceStartup;
if (!force && realtimeSinceStartup - _lastResolveAttemptRealtime < 10f)
{
return;
}
_lastResolveAttemptRealtime = realtimeSinceStartup;
if (Chainloader.PluginInfos == null || !Chainloader.PluginInfos.TryGetValue("com.azraelgodking.sunhavenmuseumutilitytracker", out var value) || (Object)(object)((value != null) ? value.Instance : null) == (Object)null)
{
return;
}
try
{
MethodInfo method = ((object)value.Instance).GetType().GetMethod("GetDonationManager", BindingFlags.Static | BindingFlags.Public);
if (!(method == null))
{
_donationManager = method.Invoke(null, null);
if (_donationManager != null)
{
_hasDonatedByGameId = _donationManager.GetType().GetMethod("HasDonatedByGameId", BindingFlags.Instance | BindingFlags.Public, null, new Type[1] { typeof(int) }, null);
}
}
}
catch (Exception ex)
{
ManualLogSource log = Plugin.Log;
if (log != null)
{
log.LogDebug((object)("DonationHelper: SMUT bind failed: " + ex.Message));
}
}
}
public static bool HasDonatedByGameId(int gameItemId)
{
EnsureResolved();
if (_donationManager == null || _hasDonatedByGameId == null)
{
return false;
}
try
{
return (bool)_hasDonatedByGameId.Invoke(_donationManager, new object[1] { gameItemId });
}
catch (Exception ex)
{
ManualLogSource log = Plugin.Log;
if (log != null)
{
log.LogDebug((object)$"DonationHelper: HasDonatedByGameId failed for {gameItemId}: {ex.Message}");
}
return false;
}
}
public static int PickBiasedFishingMuseumItem()
{
if (!Config.Enabled.Value)
{
return 0;
}
List<int> fishingMuseumItems = FishingRod.fishingMuseumItems;
if (fishingMuseumItems == null || fishingMuseumItems.Count == 0)
{
return 0;
}
if (!IsAvailable)
{
return 0;
}
List<int> list = fishingMuseumItems.Where((int id) => !HasDonatedByGameId(id)).ToList();
if (list.Count == 0)
{
return fishingMuseumItems[Random.Range(0, fishingMuseumItems.Count)];
}
float aquariumProgress = GetAquariumProgress();
if (aquariumProgress < Config.MinimumMuseumProgress.Value * 100f)
{
return fishingMuseumItems[Random.Range(0, fishingMuseumItems.Count)];
}
float num = Mathf.Min(Config.MuseumProgressBonusPercent.Value * (aquariumProgress / 10f), Config.MaxBonusChancePercent.Value);
if (Random.Range(0f, 100f) < num)
{
return list[Random.Range(0, list.Count)];
}
return fishingMuseumItems[Random.Range(0, fishingMuseumItems.Count)];
}
private static float GetAquariumProgress()
{
try
{
int num = 0;
int num2 = 0;
Type type = Type.GetType("Wish.GameSave, SunHaven.Core");
if (type == null)
{
return 0f;
}
PropertyInfo property = type.GetProperty("Instance", BindingFlags.Static | BindingFlags.Public);
if (property == null)
{
return 0f;
}
object value = property.GetValue(null);
if (value == null)
{
return 0f;
}
MethodInfo method = type.GetMethod("GetProgressIntWorld", new Type[1] { typeof(string) });
if (method == null)
{
return 0f;
}
foreach (var item2 in MuseumCurator.aquaticMuseumProgress)
{
string text = item2.Item1 + "complete";
int item = item2.Item2;
int num3 = (int)method.Invoke(value, new object[1] { text });
num += item;
num2 += num3;
}
return (num > 0) ? ((float)num2 / (float)num * 100f) : 0f;
}
catch (Exception ex)
{
ManualLogSource log = Plugin.Log;
if (log != null)
{
log.LogDebug((object)("DonationHelper: aquarium progress fallback used due to reflection error: " + ex.Message));
}
return 0f;
}
}
public static FishData PickBiasedFish(RandomFishArray array, float level)
{
if (!Config.Enabled.Value || array == null || array.Length == 0)
{
return null;
}
if (!IsAvailable)
{
return null;
}
List<FishData> list = new List<FishData>();
for (int i = 0; i < array.Length; i++)
{
FishLoot val = array.drops[i];
if (!((Object)(object)val?.fish == (Object)null) && !HasDonatedByGameId(((ItemData)val.fish).id))
{
list.Add(val.fish);
}
}
if (list.Count == 0)
{
return null;
}
float aquariumProgress = GetAquariumProgress();
if (aquariumProgress < Config.MinimumMuseumProgress.Value * 100f)
{
return null;
}
float num = Mathf.Min(Config.MuseumProgressBonusPercent.Value * (aquariumProgress / 10f), Config.MaxBonusChancePercent.Value);
if (Random.Range(0f, 100f) >= num)
{
return null;
}
return list[Random.Range(0, list.Count)];
}
}
[BepInPlugin("com.azraelgodking.trinketfortune", "Trinket Fortune", "1.2.2")]
public class Plugin : BaseUnityPlugin
{
public static class PluginInfo
{
public const string PLUGIN_GUID = "com.azraelgodking.trinketfortune";
public const string PLUGIN_NAME = "Trinket Fortune";
public const string PLUGIN_VERSION = "1.2.2";
}
private Harmony _harmony;
private bool _applicationQuitting;
public static ManualLogSource Log { get; private set; }
private void Awake()
{
//IL_0035: Unknown result type (might be due to invalid IL or missing references)
//IL_003f: Expected O, but got Unknown
Log = ((BaseUnityPlugin)this).Logger;
ConfigFile val = CreateNamedConfig();
ConfigFileHelper.ReplacePluginConfig((BaseUnityPlugin)(object)this, val, (Action<string>)Log.LogWarning);
Config.Bind(val);
_harmony = new Harmony("com.azraelgodking.trinketfortune");
FishingTrinketPatches.ApplyPatches(_harmony);
if (Chainloader.PluginInfos != null && Chainloader.PluginInfos.ContainsKey("com.azraelgodking.havendevtools"))
{
Log.LogInfo((object)"HavenDevTools detected. Trinket Fortune runs in standalone-safe mode (no hard API dependency).");
}
Log.LogInfo((object)"Trinket Fortune v1.2.2 loaded. Fishing loot bias active when S.M.U.T. is installed.");
}
private void OnApplicationQuit()
{
_applicationQuitting = true;
}
private void OnDestroy()
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_0005: Unknown result type (might be due to invalid IL or missing references)
Scene activeScene = SceneManager.GetActiveScene();
string text = ((Scene)(ref activeScene)).name ?? string.Empty;
string text2 = text.ToLowerInvariant();
if (_applicationQuitting || !Application.isPlaying || text2.Contains("menu") || text2.Contains("title"))
{
ManualLogSource log = Log;
if (log != null)
{
log.LogInfo((object)("[Lifecycle] Plugin OnDestroy during expected teardown (scene: " + text + ")"));
}
}
else
{
ManualLogSource log2 = Log;
if (log2 != null)
{
log2.LogWarning((object)("[Lifecycle] Plugin OnDestroy outside expected teardown (scene: " + text + ")"));
}
}
Harmony harmony = _harmony;
if (harmony != null)
{
harmony.UnpatchSelf();
}
}
private static ConfigFile CreateNamedConfig()
{
//IL_005e: Unknown result type (might be due to invalid IL or missing references)
//IL_0064: Expected O, but got Unknown
string text = Path.Combine(Paths.ConfigPath, "TrinketFortune.cfg");
string text2 = Path.Combine(Paths.ConfigPath, "com.azraelgodking.trinketfortune.cfg");
try
{
if (!File.Exists(text) && File.Exists(text2))
{
File.Copy(text2, text);
}
}
catch (Exception ex)
{
ManualLogSource log = Log;
if (log != null)
{
log.LogWarning((object)("[Config] Migration to TrinketFortune.cfg failed: " + ex.Message));
}
}
return new ConfigFile(text, true);
}
}
}
namespace TrinketFortune.Patches
{
public static class FishingTrinketPatches
{
public static void ApplyPatches(Harmony harmony)
{
//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
//IL_00cf: Expected O, but got Unknown
//IL_0145: Unknown result type (might be due to invalid IL or missing references)
//IL_0152: Expected O, but got Unknown
MethodInfo methodInfo = null;
MethodInfo[] methods = typeof(Utilities).GetMethods(BindingFlags.Static | BindingFlags.Public);
foreach (MethodInfo methodInfo2 in methods)
{
if (methodInfo2.Name != "RandomItem" || !methodInfo2.IsGenericMethodDefinition)
{
continue;
}
ParameterInfo[] parameters = methodInfo2.GetParameters();
if (parameters.Length != 1)
{
continue;
}
Type parameterType = parameters[0].ParameterType;
if (parameterType.IsGenericType && !(parameterType.GetGenericTypeDefinition() != typeof(IList<>)))
{
try
{
methodInfo = methodInfo2.MakeGenericMethod(typeof(int));
}
catch
{
}
if (methodInfo != null)
{
break;
}
}
}
if (methodInfo != null)
{
HarmonyMethod val = new HarmonyMethod(typeof(FishingTrinketPatches), "RandomItem_Prefix", (Type[])null);
harmony.Patch((MethodBase)methodInfo, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
Plugin.Log.LogInfo((object)"Patched Utilities.RandomItem<int> for fishing museum items");
}
else
{
Plugin.Log.LogWarning((object)"Could not find Utilities.RandomItem for museum item bias");
}
MethodInfo method = typeof(RandomFishArray).GetMethod("RandomItem", BindingFlags.Instance | BindingFlags.Public, null, new Type[1] { typeof(float) }, null);
if (method != null)
{
harmony.Patch((MethodBase)method, (HarmonyMethod)null, new HarmonyMethod(typeof(FishingTrinketPatches), "RandomFishArray_RandomItem_Postfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
Plugin.Log.LogInfo((object)"Patched RandomFishArray.RandomItem for fish bias");
}
else
{
Plugin.Log.LogWarning((object)"Could not find RandomFishArray.RandomItem");
}
}
private static bool RandomItem_Prefix<T>(IList<T> list, ref T __result)
{
if (!Config.Enabled.Value)
{
return true;
}
if (typeof(T) != typeof(int))
{
return true;
}
if (list == null || list != FishingRod.fishingMuseumItems)
{
return true;
}
int num = DonationHelper.PickBiasedFishingMuseumItem();
if (num == 0)
{
return true;
}
__result = (T)(object)num;
return false;
}
private static void RandomFishArray_RandomItem_Postfix(RandomFishArray __instance, float level, ref FishData __result)
{
FishData val = DonationHelper.PickBiasedFish(__instance, level);
if ((Object)(object)val != (Object)null)
{
__result = val;
}
}
}
}