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 BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Configuration;
using SimpleInjector;
using TrainworksReloaded.Base.Extensions;
using TrainworksReloaded.Core;
using TrainworksReloaded.Core.Extensions;
using TrainworksReloaded.Core.Interfaces;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("InfusionService")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+18d8056482d73e2c8b254139070028fe20cd5e45")]
[assembly: AssemblyProduct("InfusionService")]
[assembly: AssemblyTitle("InfusionService")]
[assembly: AssemblyVersion("0.1.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[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]
[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]
[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 InfusionService
{
[BepInPlugin("InfusionService", "InfusionService", "0.1.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
public class Plugin : BaseUnityPlugin
{
public const string EssenceDataGuid = "EssenceData";
public const string RewardId = "InfusionService";
internal static ManualLogSource Logger = new ManualLogSource("InfusionService");
public static ConfigEntry<bool>? Enabled;
public static ConfigEntry<string>? Costs;
public static GrantableRewardData? Reward;
public void Awake()
{
//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
Logger = ((BaseUnityPlugin)this).Logger;
Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Whether the Merchant of Steel offers the Infuse service.");
Costs = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Costs", "50, 100, 150, 200, 250", "Gold cost of each successive infusion in a run, comma-separated. The last value repeats. A merchant already generated when the value changes keeps the old prices.");
Railhead.GetBuilder().Configure("InfusionService", (Action<IConfigurationBuilder>)delegate(IConfigurationBuilder c)
{
ConfigurationExtensions.AddMergedJsonFile(c, new List<string>(1) { "json/plugin.json" });
});
Railend.ConfigurePostAction((Action<Container>)delegate(Container c)
{
((IDictionary<string, RewardData>)c.GetInstance<IRegister<RewardData>>()).TryGetValue(StringExtensions.GetId("InfusionService", "RewardData", "InfusionService"), out RewardData value);
Reward = (GrantableRewardData?)(object)((value is GrantableRewardData) ? value : null);
if ((Object)(object)Reward == (Object)null)
{
Logger.LogError((object)string.Format("Reward {0} missing (got {1}); is EssenceData installed? No Infuse service will be offered.", "InfusionService", value));
}
else
{
ApplyConfiguredCosts(Reward);
}
});
new Harmony("InfusionService").PatchAll();
Logger.LogInfo((object)"Plugin InfusionService is loaded!");
}
private static void ApplyConfiguredCosts(GrantableRewardData reward)
{
int[] array = ParseCosts(Costs?.Value);
if (array == null)
{
Logger.LogWarning((object)("Config General.Costs '" + Costs?.Value + "' is not a list of whole numbers; keeping " + string.Join(", ", ((RewardData)reward).Costs) + "."));
}
else
{
AccessTools.Field(typeof(RewardData), "costs").SetValue(reward, array);
}
}
internal static int[]? ParseCosts(string? text)
{
if (string.IsNullOrWhiteSpace(text))
{
return null;
}
string[] array = text.Split(',');
int[] array2 = new int[array.Length];
for (int i = 0; i < array.Length; i++)
{
if (!int.TryParse(array[i].Trim(), out array2[i]) || array2[i] < 0)
{
return null;
}
}
return array2;
}
}
public static class MyPluginInfo
{
public const string PLUGIN_GUID = "InfusionService";
public const string PLUGIN_NAME = "InfusionService";
public const string PLUGIN_VERSION = "0.1.0";
}
}
namespace InfusionService.Patches
{
[HarmonyPatch(typeof(SaveManager), "GenerateMerchantGoodsAtDistance")]
internal static class SaveManager_GenerateMerchantGoodsAtDistance_AddInfusion_Patch
{
private const int MaxServices = 4;
private static readonly string[] SteelMerchants = new string[2] { "UnitUpgradeMerchant", "SoulSavior_UpgradedUnitUpgradeMerchant" };
private static readonly Func<SaveManager, SaveData> GetActiveSaveData = AccessTools.MethodDelegate<Func<SaveManager, SaveData>>(AccessTools.PropertyGetter(typeof(SaveManager), "ActiveSaveData"), (object)null, true);
public static void Postfix(SaveManager __instance, int distance, int branch, int index, bool generateServices)
{
//IL_009b: Unknown result type (might be due to invalid IL or missing references)
//IL_00a6: Expected O, but got Unknown
//IL_014f: Unknown result type (might be due to invalid IL or missing references)
//IL_0156: Expected O, but got Unknown
GrantableRewardData reward = Plugin.Reward;
if (!generateServices || (Object)(object)reward == (Object)null)
{
return;
}
ConfigEntry<bool>? enabled = Plugin.Enabled;
if (enabled != null && !enabled.Value)
{
return;
}
SaveData val = GetActiveSaveData(__instance);
if (val == null)
{
return;
}
List<MerchantData> list = __instance.GetRunState().MerchantsAtDistance(distance, branch, index);
MerchantGoodState val4 = default(MerchantGoodState);
for (int i = 0; i < list.Count; i++)
{
MerchantData val2 = list[i];
if ((Object)(object)val2 == (Object)null || Array.IndexOf(SteelMerchants, ((Object)val2).name) < 0 || !reward.CanBeGranted(__instance))
{
continue;
}
int num = ((index == -1) ? i : index);
int num2 = 0;
foreach (MerchantGoodState merchantGoodState in val.GetMerchantGoodStates(new Location(distance, branch, num), true))
{
if (merchantGoodState.IsService)
{
GrantableRewardData rewardData = ((RewardState)merchantGoodState).RewardData;
if (((rewardData != null) ? ((GameData)rewardData).GetID() : null) != ((GameData)reward).GetID())
{
num2++;
}
}
}
if (num2 >= 4)
{
Plugin.Logger.LogWarning((object)$"{((Object)val2).name} at distance {distance} branch {branch} already has {num2} services; not adding Infuse.");
}
else
{
MerchantGoodState val3 = new MerchantGoodState(reward, __instance, false, string.Empty);
if (val.HasMerchantGood(val3, ref val4))
{
((RewardState)val4).SetActive(distance, branch, num);
continue;
}
((RewardState)val3).SetActive(distance, branch, num);
val.AddMerchantGood(val3);
}
}
}
}
}