using System;
using System.Collections;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using DM;
using HarmonyLib;
using Landfall.TABS;
using Microsoft.CodeAnalysis;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[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 UnitLauncher
{
[BepInPlugin("subunits.faction.bepinex", "Subunits", "1.0.0")]
public sealed class SubunitsPlugin : BaseUnityPlugin
{
public const string Guid = "subunits.faction.bepinex";
public const string Version = "1.0.0";
internal const string FactionName = "Subunits";
internal static ManualLogSource Log;
internal static bool Ready;
private static readonly int ModId = "subunits.faction.bepinex".GetStableHash();
private bool attempted;
private float nextCheck;
internal static void Info(string m)
{
if (Log != null)
{
Log.LogInfo((object)m);
}
}
internal static void Warn(string m)
{
if (Log != null)
{
Log.LogWarning((object)m);
}
}
private void Awake()
{
Log = ((BaseUnityPlugin)this).Logger;
}
private void Update()
{
if (attempted || Time.realtimeSinceStartup < nextCheck)
{
return;
}
nextCheck = Time.realtimeSinceStartup + 1f;
ContentDatabase val = ContentDatabase.Instance();
if (val == null || val.LandfallContentDatabase == null)
{
return;
}
LandfallContentDatabase landfallContentDatabase = val.LandfallContentDatabase;
if (!landfallContentDatabase.GetUnitBlueprints().Any() || !landfallContentDatabase.GetFactions().Any((Faction x) => Object.op_Implicit((Object)(object)x) && x.m_displayFaction))
{
return;
}
SoundPlayer service = ServiceLocator.GetService<SoundPlayer>();
if (!Object.op_Implicit((Object)(object)service) || !Object.op_Implicit((Object)(object)service.soundBank))
{
return;
}
attempted = true;
try
{
Register(val, landfallContentDatabase);
}
catch (Exception ex)
{
Ready = false;
Info("Subunits faction failed to register: " + ex);
}
}
private static void Register(ContentDatabase content, LandfallContentDatabase db)
{
Faction val = db.GetFactions().FirstOrDefault((Func<Faction, bool>)((Faction f) => Object.op_Implicit((Object)(object)f) && !f.m_displayFaction && f.Entity != null && f.Entity.Name == "FACTION_SUBUNITS"));
if ((Object)(object)val == (Object)null)
{
Warn("FACTION_SUBUNITS was not found in this game build; nothing to expose.");
return;
}
UnitBlueprint[] array = (UnitBlueprint[])((val.Units == null) ? ((Array)new UnitBlueprint[0]) : ((Array)val.Units.Where((UnitBlueprint u) => Object.op_Implicit((Object)(object)u)).ToArray()));
if (array.Length == 0)
{
Warn("FACTION_SUBUNITS is present but empty; nothing to expose.");
return;
}
Faction val2 = Object.Instantiate<Faction>(val);
((Object)val2).name = "Subunits/Subunits";
Identity((IDatabaseEntity)(object)val2, "faction", "Subunits");
val2.Units = array;
val2.m_displayFaction = true;
val2.index = (from f in db.GetFactions()
where Object.op_Implicit((Object)(object)f)
select f).Max((Faction f) => f.index) + 1;
AddEntity((IDatabaseEntity)(object)val2, "m_factions");
Ready = true;
Info("Subunits faction registered: " + array.Length + " placeable units at index " + val2.index + ".");
}
private static void Identity(IDatabaseEntity owner, string kind, string name)
{
//IL_0023: Unknown result type (might be due to invalid IL or missing references)
DatabaseEntity val = owner.Entity.ShallowCopy();
val.GUID = new DatabaseID(ModId, (kind + "/" + name).GetStableHash());
val.Name = name;
AccessTools.Field(typeof(DatabaseEntity), "m_unlockKey").SetValue(val, "");
AccessTools.Field(((object)owner).GetType(), "m_entity").SetValue(owner, val);
}
private static void AddEntity(IDatabaseEntity entity, string field)
{
//IL_0054: Unknown result type (might be due to invalid IL or missing references)
//IL_0082: Unknown result type (might be due to invalid IL or missing references)
//IL_0099: Unknown result type (might be due to invalid IL or missing references)
//IL_006c: Unknown result type (might be due to invalid IL or missing references)
//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
ContentDatabase val = ContentDatabase.Instance();
IDictionary dictionary = (IDictionary)AccessTools.Field(typeof(LandfallContentDatabase), field).GetValue(val.LandfallContentDatabase);
IDictionary dictionary2 = (IDictionary)AccessTools.Field(((object)val.AssetLoader).GetType(), "m_nonStreamableAssets").GetValue(val.AssetLoader);
if (dictionary.Contains(entity.Entity.GUID))
{
dictionary.Remove(entity.Entity.GUID);
}
dictionary.Add(entity.Entity.GUID, entity);
if (dictionary2.Contains(entity.Entity.GUID))
{
dictionary2.Remove(entity.Entity.GUID);
}
dictionary2.Add(entity.Entity.GUID, entity);
}
}
internal static class HashExtensions
{
internal static int GetStableHash(this string text)
{
uint num = 2166136261u;
foreach (char c in text)
{
num ^= c;
num *= 16777619;
}
return (int)(num & 0x7FFFFFFF);
}
}
}