using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
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: AssemblyTitle("DiscountREPOV2")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("DiscountREPOV2")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e7656a77-4f1c-4909-adb9-24163cf601b1")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace RepoDiscountMod;
[BepInPlugin("com.maze.repodiscountmod", "Repo Discount Mod", "2.1.1")]
public class RepoDiscountMod : BaseUnityPlugin
{
internal static ManualLogSource Log;
private void Awake()
{
//IL_0011: Unknown result type (might be due to invalid IL or missing references)
//IL_0017: Expected O, but got Unknown
Log = ((BaseUnityPlugin)this).Logger;
Harmony val = new Harmony("com.maze.repodiscountmod");
try
{
val.PatchAll();
Log.LogInfo((object)"[RepoDiscountMod] Patches appliqués, remise active (réflexion).");
}
catch (Exception ex)
{
Log.LogError((object)("[RepoDiscountMod] Erreur lors du PatchAll : " + ex));
}
}
}
internal static class ShopManagerExtensions
{
private static readonly FieldInfo ShoppingListField = AccessTools.Field(typeof(ShopManager), "shoppingList");
public static IList GetShoppingListSafe(this ShopManager shop)
{
if ((Object)(object)shop == (Object)null)
{
ManualLogSource log = RepoDiscountMod.Log;
if (log != null)
{
log.LogWarning((object)"[RepoDiscountMod] ShopManager.instance est null.");
}
return null;
}
if (ShoppingListField == null)
{
ManualLogSource log2 = RepoDiscountMod.Log;
if (log2 != null)
{
log2.LogError((object)"[RepoDiscountMod] Champ 'ShopManager.shoppingList' introuvable.");
}
return null;
}
return ShoppingListField.GetValue(shop) as IList;
}
}
internal static class DiscountLogic
{
public const float DiscountFactor = 0.3f;
private static readonly FieldInfo ValueField = AccessTools.Field(typeof(ItemAttributes), "value");
private static readonly FieldInfo InExtractionField = AccessTools.Field(typeof(RoomVolumeCheck), "inExtractionPoint");
public static int GetDiscountedPrice(object obj)
{
ItemAttributes val = (ItemAttributes)((obj is ItemAttributes) ? obj : null);
if ((Object)(object)val == (Object)null)
{
return 0;
}
if (ValueField == null)
{
ManualLogSource log = RepoDiscountMod.Log;
if (log != null)
{
log.LogError((object)"[RepoDiscountMod] Champ 'ItemAttributes.value' introuvable.");
}
return 0;
}
int num = (int)ValueField.GetValue(val);
if (InExtractionField == null)
{
return num;
}
RoomVolumeCheck component = ((Component)val).GetComponent<RoomVolumeCheck>();
if ((Object)(object)component != (Object)null && (bool)InExtractionField.GetValue(component))
{
return Mathf.CeilToInt((float)num * 0.3f);
}
return num;
}
}
[HarmonyPatch(typeof(SemiFunc), "ShopGetTotalCost")]
internal static class Patch_ShopGetTotalCost
{
private static bool Prefix(ref int __result)
{
try
{
ShopManager instance = ShopManager.instance;
IList list = (((Object)(object)instance != (Object)null) ? instance.GetShoppingListSafe() : null);
if (list == null || list.Count == 0)
{
__result = 0;
return false;
}
int num = 0;
foreach (object item in list)
{
num += DiscountLogic.GetDiscountedPrice(item);
}
__result = num;
return false;
}
catch (Exception ex)
{
ManualLogSource log = RepoDiscountMod.Log;
if (log != null)
{
log.LogError((object)("[RepoDiscountMod] Erreur dans ShopGetTotalCost Prefix : " + ex));
}
return true;
}
}
}
[HarmonyPatch(typeof(ExtractionPoint), "DestroyTheFirstPhysObjectsInShopList")]
internal static class Patch_DestroyFirstShopItem
{
private static readonly FieldInfo ValueField = AccessTools.Field(typeof(ItemAttributes), "value");
private static readonly FieldInfo InExtractionField = AccessTools.Field(typeof(RoomVolumeCheck), "inExtractionPoint");
private static bool Prefix()
{
//IL_0156: Unknown result type (might be due to invalid IL or missing references)
//IL_015c: Invalid comparison between Unknown and I4
//IL_0182: Unknown result type (might be due to invalid IL or missing references)
//IL_0188: Invalid comparison between Unknown and I4
try
{
if (!SemiFunc.IsMasterClientOrSingleplayer())
{
return false;
}
ShopManager instance = ShopManager.instance;
IList list = (((Object)(object)instance != (Object)null) ? instance.GetShoppingListSafe() : null);
if (list == null || list.Count == 0)
{
return false;
}
object obj = list[0];
ItemAttributes val = (ItemAttributes)((obj is ItemAttributes) ? obj : null);
if ((Object)(object)val == (Object)null)
{
return false;
}
if (ValueField == null)
{
ManualLogSource log = RepoDiscountMod.Log;
if (log != null)
{
log.LogError((object)"[RepoDiscountMod] Champ 'ItemAttributes.value' introuvable.");
}
return false;
}
int num = (int)ValueField.GetValue(val);
int num2 = num;
if (InExtractionField != null)
{
RoomVolumeCheck component = ((Component)val).GetComponent<RoomVolumeCheck>();
if ((Object)(object)component != (Object)null && (bool)InExtractionField.GetValue(component))
{
num2 = Mathf.CeilToInt((float)num * 0.3f);
}
}
int num3 = SemiFunc.StatGetRunCurrency();
if (num3 - num2 < 0)
{
SemiFunc.ShopUpdateCost();
return false;
}
SemiFunc.StatSetRunCurrency(num3 - num2);
StatsManager.instance.ItemPurchase(((Object)val.item).name);
if ((int)val.item.itemType == 3)
{
StatsManager.instance.AddItemsUpgradesPurchased(((Object)val.item).name);
}
if ((int)val.item.itemType == 5)
{
Dictionary<string, int> runStats = StatsManager.instance.runStats;
runStats["chargingStationChargeTotal"] += 17;
if (runStats["chargingStationChargeTotal"] > 100)
{
runStats["chargingStationChargeTotal"] = 100;
}
Debug.Log((object)("Charging station charge total: " + StatsManager.instance.runStats["chargingStationChargeTotal"]));
}
PhysGrabObject component2 = ((Component)val).GetComponent<PhysGrabObject>();
if ((Object)(object)component2 != (Object)null)
{
component2.DestroyPhysGrabObject();
}
list.RemoveAt(0);
SemiFunc.ShopUpdateCost();
return false;
}
catch (Exception ex)
{
ManualLogSource log2 = RepoDiscountMod.Log;
if (log2 != null)
{
log2.LogError((object)("[RepoDiscountMod] Erreur dans DestroyTheFirstPhysObjectsInShopList Prefix : " + ex));
}
return false;
}
}
}
[HarmonyPatch(typeof(ExtractionPoint), "DestroyAllPhysObjectsInShoppingList")]
internal static class Patch_DestroyAllShopItems
{
private static readonly FieldInfo ValueField = AccessTools.Field(typeof(ItemAttributes), "value");
private static readonly FieldInfo InExtractionField = AccessTools.Field(typeof(RoomVolumeCheck), "inExtractionPoint");
private static bool Prefix()
{
//IL_01ca: Unknown result type (might be due to invalid IL or missing references)
//IL_01d0: Invalid comparison between Unknown and I4
//IL_01f8: Unknown result type (might be due to invalid IL or missing references)
//IL_01fe: Invalid comparison between Unknown and I4
try
{
if (!SemiFunc.IsMasterClientOrSingleplayer())
{
return false;
}
foreach (PlayerAvatar player in GameDirector.instance.PlayerList)
{
player.playerDeathHead.Revive();
}
ShopManager instance = ShopManager.instance;
IList list = (((Object)(object)instance != (Object)null) ? instance.GetShoppingListSafe() : null);
if (list == null || list.Count == 0)
{
SemiFunc.ShopUpdateCost();
return false;
}
List<ItemAttributes> list2 = new List<ItemAttributes>();
foreach (object item in list)
{
ItemAttributes val = (ItemAttributes)((item is ItemAttributes) ? item : null);
if ((Object)(object)val == (Object)null)
{
continue;
}
PhysGrabObject component = ((Component)val).GetComponent<PhysGrabObject>();
if ((Object)(object)component == (Object)null)
{
continue;
}
if (ValueField == null)
{
ManualLogSource log = RepoDiscountMod.Log;
if (log != null)
{
log.LogError((object)"[RepoDiscountMod] Champ 'ItemAttributes.value' introuvable.");
}
break;
}
int num = (int)ValueField.GetValue(val);
int num2 = num;
if (InExtractionField != null)
{
RoomVolumeCheck component2 = ((Component)val).GetComponent<RoomVolumeCheck>();
if ((Object)(object)component2 != (Object)null && (bool)InExtractionField.GetValue(component2))
{
num2 = Mathf.CeilToInt((float)num * 0.3f);
}
}
int num3 = SemiFunc.StatGetRunCurrency();
if (num3 - num2 < 0)
{
continue;
}
SemiFunc.StatSetRunCurrency(num3 - num2);
StatsManager.instance.ItemPurchase(((Object)val.item).name);
if ((int)val.item.itemType == 3)
{
StatsManager.instance.AddItemsUpgradesPurchased(((Object)val.item).name);
}
if ((int)val.item.itemType == 5)
{
Dictionary<string, int> runStats = StatsManager.instance.runStats;
runStats["chargingStationChargeTotal"] += 17;
if (runStats["chargingStationChargeTotal"] > 100)
{
runStats["chargingStationChargeTotal"] = 100;
}
}
component.DestroyPhysGrabObject();
list2.Add(val);
}
foreach (ItemAttributes item2 in list2)
{
list.Remove(item2);
}
SemiFunc.ShopUpdateCost();
return false;
}
catch (Exception ex)
{
ManualLogSource log2 = RepoDiscountMod.Log;
if (log2 != null)
{
log2.LogError((object)("[RepoDiscountMod] Erreur dans DestroyAllPhysObjectsInShoppingList Prefix : " + ex));
}
return false;
}
}
}