using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using ModdingUtils.AIMinion;
using ModdingUtils.AIMinion.Extensions;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[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 StuckMatchFix
{
[BepInPlugin("com.mrnoa.rounds.stuckmatchfix", "StuckMatchFix", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInProcess("Rounds.exe")]
public class StuckMatchFix : BaseUnityPlugin
{
[HarmonyPatch(typeof(AIMinionHandler), "RemoveAIsFromPlayerManager")]
private static class RemovePatch
{
private static bool Prefix(ref IEnumerator __result)
{
__result = Run();
return false;
}
private static IEnumerator Run()
{
PlayerManager instance = PlayerManager.instance;
if ((Object)(object)instance != (Object)null && instance.players != null)
{
List<Player> list = instance.players.ToList();
List<Player> list2 = (from p in list.Where((Player p) => (Object)(object)p != (Object)null && !IsMinion(p)).Distinct()
orderby p.playerID
select p).ToList();
Report("Remove minions", list, list2);
instance.players = list2;
}
yield break;
}
}
[HarmonyPatch(typeof(AIMinionHandler), "AddAIsToPlayerManager")]
private static class AddPatch
{
private static bool Prefix(ref IEnumerator __result)
{
__result = Run();
return false;
}
private static IEnumerator Run()
{
PlayerManager instance = PlayerManager.instance;
if (!((Object)(object)instance != (Object)null) || instance.players == null)
{
yield break;
}
List<Player> list = (from p in instance.players.ToList()
where (Object)(object)p != (Object)null && !IsMinion(p)
select p).ToList();
foreach (Player item in list.ToList())
{
List<Player> minions = CharacterDataExtension.GetAdditionalData(item.data).minions;
if (minions != null)
{
list.AddRange(minions);
}
}
List<Player> list2 = (from p in list.Where((Player p) => (Object)(object)p != (Object)null && IsEnabled(p)).Distinct()
orderby p.playerID
select p).ToList();
Report("Add minions", list, list2);
instance.players = list2;
}
}
public const string Guid = "com.mrnoa.rounds.stuckmatchfix";
public const string Version = "1.0.0";
internal static ManualLogSource Log;
private void Awake()
{
//IL_0010: Unknown result type (might be due to invalid IL or missing references)
Log = ((BaseUnityPlugin)this).Logger;
new Harmony("com.mrnoa.rounds.stuckmatchfix").PatchAll(typeof(StuckMatchFix).Assembly);
Log.LogInfo((object)"StuckMatchFix 1.0.0 loaded: AI-minion player list rebuild made gap-safe.");
}
private static bool IsMinion(Player p)
{
if ((Object)(object)p != (Object)null && (Object)(object)p.data != (Object)null)
{
return CharacterDataExtension.GetAdditionalData(p.data).isAIMinion;
}
return false;
}
private static bool IsEnabled(Player p)
{
if ((Object)(object)p != (Object)null && (Object)(object)p.data != (Object)null)
{
return CharacterDataExtension.GetAdditionalData(p.data).isEnabled;
}
return false;
}
private static void Report(string what, List<Player> before, List<Player> after)
{
List<int> list = (from p in before
where (Object)(object)p != (Object)null
select p.playerID into i
orderby i
select i).ToList();
if (list.Where((int id, int i) => id != i).Any())
{
Log.LogWarning((object)(what + ": player IDs had gaps [" + string.Join(",", list) + "]; the original code would have crashed here. Kept " + after.Count + " players."));
}
}
}
}