using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Configuration;
using TMPro;
using TrainworksReloaded.Core;
using TrainworksReloaded.Core.Extensions;
using UnityEngine;
using UnityEngine.UI;
[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("LogbookWinTracker.Plugin")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+47f711a2202f72c5c8e5004605c47c4d857c3381")]
[assembly: AssemblyProduct("LogbookWinTracker.Plugin")]
[assembly: AssemblyTitle("LogbookWinTracker.Plugin")]
[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 LogbookWinTracker.Plugin
{
[BepInPlugin("LogbookWinTracker.Plugin", "LogbookWinTracker.Plugin", "0.1.0")]
public class Plugin : BaseUnityPlugin
{
internal static ManualLogSource Logger = new ManualLogSource("LogbookWinTracker.Plugin");
public void Awake()
{
//IL_004d: Unknown result type (might be due to invalid IL or missing references)
Logger = ((BaseUnityPlugin)this).Logger;
Railhead.GetBuilder().Configure("LogbookWinTracker.Plugin", (Action<IConfigurationBuilder>)delegate(IConfigurationBuilder c)
{
ConfigurationExtensions.AddMergedJsonFile(c, new List<string>(2) { "json/plugin.json", "json/global.json" });
});
Logger.LogInfo((object)"Plugin LogbookWinTracker.Plugin is loaded!");
new Harmony("LogbookWinTracker.Plugin").PatchAll();
Logger.LogInfo((object)"LogbookWinTracker loaded");
}
}
[HarmonyPatch(typeof(CompendiumSectionChecklist), "InitializeCovenantRankMeter")]
public static class Patch_ReplaceCovenantMeter
{
private static readonly ManualLogSource Log = Logger.CreateLogSource("Mt2WinTracker.Patch");
public static void Postfix(CompendiumSectionChecklist __instance)
{
object? value = AccessTools.Field(typeof(CompendiumSectionChecklist), "covenantRankMeter").GetValue(__instance);
CovenantRankMeter val = (CovenantRankMeter)((value is CovenantRankMeter) ? value : null);
object? value2 = AccessTools.Field(typeof(CompendiumSectionChecklist), "spChallengeProgressUI").GetValue(__instance);
SpChallengeProgressUI val2 = (SpChallengeProgressUI)((value2 is SpChallengeProgressUI) ? value2 : null);
if (!((Object)(object)val == (Object)null) && !((Object)(object)val2 == (Object)null))
{
((Component)val).gameObject.SetActive(false);
Transform parent = ((Component)val2).transform.parent;
(int cov10Wins, int titanWins, int totalCombos) tuple = CalculateWinCounts();
int item = tuple.cov10Wins;
int item2 = tuple.titanWins;
int item3 = tuple.totalCombos;
Transform obj = parent.Find("Custom_Covenant10_Block");
GameObject val3 = ((obj != null) ? ((Component)obj).gameObject : null);
if ((Object)(object)val3 == (Object)null)
{
val3 = Object.Instantiate<GameObject>(((Component)val2).gameObject, parent);
((Object)val3).name = "Custom_Covenant10_Block";
val3.transform.SetSiblingIndex(((Component)val).transform.GetSiblingIndex());
}
UpdateBlockUI(val3, "Covenant 10 Wins", item, item3);
Transform obj2 = parent.Find("Custom_Titan_Block");
GameObject val4 = ((obj2 != null) ? ((Component)obj2).gameObject : null);
if ((Object)(object)val4 == (Object)null)
{
val4 = Object.Instantiate<GameObject>(((Component)val2).gameObject, parent);
((Object)val4).name = "Custom_Titan_Block";
val4.transform.SetSiblingIndex(val3.transform.GetSiblingIndex() + 1);
}
UpdateBlockUI(val4, "Titan Wins", item2, item3);
}
}
private static (int cov10Wins, int titanWins, int totalCombos) CalculateWinCounts()
{
int num = 0;
int num2 = 0;
int num3 = 0;
AllGameManagers val = Object.FindObjectOfType<AllGameManagers>();
if ((Object)(object)val == (Object)null)
{
return (cov10Wins: 0, titanWins: 0, totalCombos: 0);
}
SaveManager saveManager = val.GetSaveManager();
AllGameData allGameData = val.GetAllGameData();
if ((Object)(object)saveManager == (Object)null || (Object)(object)allGameData == (Object)null)
{
return (cov10Wins: 0, titanWins: 0, totalCombos: 0);
}
MetagameSaveData metagameSave = saveManager.GetMetagameSave();
if (metagameSave == null)
{
return (cov10Wins: 0, titanWins: 0, totalCombos: 0);
}
List<ClassData> list = new List<ClassData>();
foreach (ClassData allClassData in allGameData.GetAllClassDatas())
{
if (saveManager.IsUnlockedAndAvailableWhenStartingRun(allClassData))
{
list.Add(allClassData);
}
}
int[] array = new int[2] { 0, 1 };
foreach (ClassData item in list)
{
string iD = ((GameData)item).GetID();
int level = metagameSave.GetLevel(iD);
foreach (ClassData item2 in list)
{
string iD2 = ((GameData)item2).GetID();
if (iD == iD2)
{
continue;
}
int[] array2 = array;
foreach (int num4 in array2)
{
if (num4 != 1 || level >= 5)
{
num3++;
ClassCombinationWinData classCombinationWinAscensionLevel = metagameSave.GetClassCombinationWinAscensionLevel(iD, iD2, num4);
if (classCombinationWinAscensionLevel.highestAscensionLevel >= 10)
{
num++;
}
if (classCombinationWinAscensionLevel.isTfbVictory)
{
num2++;
}
}
}
}
}
return (cov10Wins: num, titanWins: num2, totalCombos: num3);
}
private static void UpdateBlockUI(GameObject block, string headerText, int wins, int total)
{
float fillRatio = ((total > 0) ? ((float)wins / (float)total) : 0f);
Transform val = block.transform.Find("Label root");
if ((Object)(object)val != (Object)null)
{
TMP_Text componentInChildren = ((Component)val).GetComponentInChildren<TMP_Text>(true);
if ((Object)(object)componentInChildren != (Object)null)
{
componentInChildren.text = headerText;
}
else
{
Text componentInChildren2 = ((Component)val).GetComponentInChildren<Text>(true);
if ((Object)(object)componentInChildren2 != (Object)null)
{
componentInChildren2.text = headerText;
}
}
}
TMP_Text[] componentsInChildren = block.GetComponentsInChildren<TMP_Text>(true);
Transform val2 = block.transform.Find("Count root") ?? block.transform.Find("Progress root");
if ((Object)(object)val2 != (Object)null)
{
TMP_Text componentInChildren3 = ((Component)val2).GetComponentInChildren<TMP_Text>(true);
if ((Object)(object)componentInChildren3 != (Object)null)
{
componentInChildren3.text = $"{wins} / {total}";
}
}
else if (componentsInChildren.Length >= 2)
{
componentsInChildren[1].text = $"{wins} / {total}";
}
UpdateMeterProgress(block, fillRatio);
}
private static void UpdateMeterProgress(GameObject block, float fillRatio)
{
//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
//IL_00ce: Invalid comparison between Unknown and I4
Slider componentInChildren = block.GetComponentInChildren<Slider>(true);
if ((Object)(object)componentInChildren != (Object)null)
{
componentInChildren.value = fillRatio;
return;
}
string[] obj = new string[6] { "Fill", "Progress", "Bar", "Meter", "ProgressFill", "BarFill" };
Image val = null;
string[] array = obj;
foreach (string text in array)
{
Transform val2 = block.transform.Find(text) ?? block.transform.Find("Progress root/" + text);
if ((Object)(object)val2 != (Object)null)
{
val = ((Component)val2).GetComponent<Image>();
if ((Object)(object)val != (Object)null)
{
break;
}
}
}
if ((Object)(object)val == (Object)null)
{
Image[] componentsInChildren = block.GetComponentsInChildren<Image>(true);
foreach (Image val3 in componentsInChildren)
{
if ((int)val3.type == 3)
{
val = val3;
break;
}
}
}
if ((Object)(object)val != (Object)null)
{
val.type = (Type)3;
val.fillAmount = fillRatio;
}
else
{
Log.LogWarning((object)("Could not find an Image or Slider to adjust progress fill on " + ((Object)block).name));
}
}
}
[HarmonyPatch(typeof(CompendiumSectionChecklist), "ApplyScreenInput")]
public static class Patch_CompendiumSectionChecklist_ApplyScreenInput
{
public static bool Prefix(ref bool __result)
{
__result = false;
return false;
}
}
public static class MyPluginInfo
{
public const string PLUGIN_GUID = "LogbookWinTracker.Plugin";
public const string PLUGIN_NAME = "LogbookWinTracker.Plugin";
public const string PLUGIN_VERSION = "0.1.0";
}
}