using System;
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 Landfall.TABS;
using Microsoft.CodeAnalysis;
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: AssemblyCompany("UnitRng")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("UnitRng")]
[assembly: AssemblyTitle("UnitRng")]
[assembly: AssemblyVersion("1.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 UnitRng
{
[HarmonyPatch(typeof(Unit), "Start")]
internal static class UnitStartPatch
{
private static void Postfix(Unit __instance)
{
if (Plugin.Enabled.Value && !((Object)(object)__instance == (Object)null) && (Object)(object)((Component)__instance).GetComponent<UnitRngComponent>() == (Object)null)
{
((Component)__instance).gameObject.AddComponent<UnitRngComponent>();
}
}
}
[HarmonyPatch(typeof(ProjectileHit), "DoGameplayEffects")]
internal static class ProjectileHitPatch
{
private static void Prefix(ProjectileHit __instance, HitData hit, float m, Unit unit)
{
if (!Plugin.Enabled.Value || (Object)(object)__instance == (Object)null || (Object)(object)unit == (Object)null)
{
return;
}
UnitRngComponent component = ((Component)unit).GetComponent<UnitRngComponent>();
if (!((Object)(object)component == (Object)null))
{
OriginalDamageTag originalDamageTag = ((Component)__instance).GetComponent<OriginalDamageTag>();
if ((Object)(object)originalDamageTag == (Object)null)
{
originalDamageTag = ((Component)__instance).gameObject.AddComponent<OriginalDamageTag>();
}
if (originalDamageTag.originalDamage < 0f)
{
originalDamageTag.originalDamage = __instance.damage;
}
__instance.damage = originalDamageTag.originalDamage * component.damageMul;
}
}
private static void Postfix(ProjectileHit __instance)
{
OriginalDamageTag originalDamageTag = (((Object)(object)__instance != (Object)null) ? ((Component)__instance).GetComponent<OriginalDamageTag>() : null);
if ((Object)(object)originalDamageTag != (Object)null && originalDamageTag.originalDamage >= 0f)
{
__instance.damage = originalDamageTag.originalDamage;
}
}
}
internal class OriginalDamageTag : MonoBehaviour
{
public float originalDamage = -1f;
}
[BepInPlugin("local.unitrng", "UnitRng", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
public const string GUID = "local.unitrng";
public const string Name = "UnitRng";
public const string Version = "1.0.0";
public static Plugin Instance;
public static ManualLogSource Log;
public static ConfigEntry<bool> Enabled;
public static ConfigEntry<float> SpeedMin;
public static ConfigEntry<float> SpeedMax;
public static ConfigEntry<float> AttackSpeedMin;
public static ConfigEntry<float> AttackSpeedMax;
public static ConfigEntry<float> MassMin;
public static ConfigEntry<float> MassMax;
public static ConfigEntry<float> DamageMin;
public static ConfigEntry<float> DamageMax;
public static ConfigEntry<bool> RerollHealth;
public static ConfigEntry<bool> ShowStats;
private void Awake()
{
//IL_0176: Unknown result type (might be due to invalid IL or missing references)
Instance = this;
Log = ((BaseUnityPlugin)this).Logger;
Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable random stat variation when units are spawned.");
ShowStats = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowStats", false, "Log every stat roll per unit.");
SpeedMin = ((BaseUnityPlugin)this).Config.Bind<float>("Stats", "SpeedMultiplierMin", 0.85f, "Min multiplier applied to turnSpeed.");
SpeedMax = ((BaseUnityPlugin)this).Config.Bind<float>("Stats", "SpeedMultiplierMax", 1.15f, "Max multiplier applied to turnSpeed.");
AttackSpeedMin = ((BaseUnityPlugin)this).Config.Bind<float>("Stats", "AttackSpeedMultiplierMin", 0.85f, "Min multiplier applied to attackSpeedMultiplier.");
AttackSpeedMax = ((BaseUnityPlugin)this).Config.Bind<float>("Stats", "AttackSpeedMultiplierMax", 1.15f, "Max multiplier applied to attackSpeedMultiplier.");
MassMin = ((BaseUnityPlugin)this).Config.Bind<float>("Stats", "MassMultiplierMin", 0.85f, "Min multiplier applied to unit rigidbody mass.");
MassMax = ((BaseUnityPlugin)this).Config.Bind<float>("Stats", "MassMultiplierMax", 1.15f, "Max multiplier applied to unit rigidbody mass.");
DamageMin = ((BaseUnityPlugin)this).Config.Bind<float>("Stats", "DamageMultiplierMin", 0.9f, "Min multiplier applied to per-weapon damage.");
DamageMax = ((BaseUnityPlugin)this).Config.Bind<float>("Stats", "DamageMultiplierMax", 1.1f, "Max multiplier applied to per-weapon damage.");
new Harmony("local.unitrng").PatchAll();
((BaseUnityPlugin)this).Logger.LogInfo((object)"UnitRng 1.0.0 loaded.");
}
public static float Roll(float min, float max)
{
if (max <= min)
{
return min;
}
return Random.Range(min, max);
}
}
public class UnitRngComponent : MonoBehaviour
{
public Unit unit;
public DataHandler data;
public bool applied;
public float speedMul = 1f;
public float attackSpeedMul = 1f;
public float massMul = 1f;
public float damageMul = 1f;
private static readonly FieldInfo UnitDataField = typeof(Unit).GetField("dataHandler", BindingFlags.Instance | BindingFlags.NonPublic);
private static readonly FieldInfo WeaponHandlerField = typeof(Unit).GetField("WeaponHandler", BindingFlags.Instance | BindingFlags.NonPublic);
private void Start()
{
if (applied)
{
return;
}
if ((Object)(object)unit == (Object)null)
{
unit = ((Component)this).GetComponent<Unit>();
}
if ((Object)(object)data == (Object)null)
{
if (UnitDataField != null)
{
ref DataHandler reference = ref data;
object? value = UnitDataField.GetValue(unit);
reference = (DataHandler)((value is DataHandler) ? value : null);
}
if ((Object)(object)data == (Object)null)
{
data = ((Component)this).GetComponentInChildren<DataHandler>();
}
}
if (!((Object)(object)unit == (Object)null))
{
Apply();
applied = true;
}
}
public void Apply()
{
if (!Plugin.Enabled.Value)
{
return;
}
speedMul = Plugin.Roll(Plugin.SpeedMin.Value, Plugin.SpeedMax.Value);
unit.turnSpeedMultiplier = Mathf.Max(0.01f, unit.turnSpeedMultiplier * speedMul);
if (unit.turnSpeed > 0f)
{
unit.turnSpeed = Mathf.Max(0.01f, unit.turnSpeed * speedMul);
}
attackSpeedMul = Plugin.Roll(Plugin.AttackSpeedMin.Value, Plugin.AttackSpeedMax.Value);
unit.attackSpeedMultiplier = Mathf.Max(0.01f, unit.attackSpeedMultiplier * attackSpeedMul);
WeaponHandler val = null;
if (WeaponHandlerField != null)
{
object? value = WeaponHandlerField.GetValue(unit);
val = (WeaponHandler)((value is WeaponHandler) ? value : null);
}
if ((Object)(object)val != (Object)null)
{
val.SetAttackSpeedMultiplier(unit.attackSpeedMultiplier);
if (val.attackSpeedMultiplier <= 0f)
{
val.attackSpeedMultiplier = unit.attackSpeedMultiplier;
}
}
massMul = Plugin.Roll(Plugin.MassMin.Value, Plugin.MassMax.Value);
Rigidbody[] componentsInChildren = ((Component)this).GetComponentsInChildren<Rigidbody>(true);
for (int i = 0; i < componentsInChildren.Length; i++)
{
if (!((Object)(object)componentsInChildren[i] == (Object)null))
{
componentsInChildren[i].mass = Mathf.Max(0.01f, componentsInChildren[i].mass * massMul);
}
}
damageMul = Plugin.Roll(Plugin.DamageMin.Value, Plugin.DamageMax.Value);
Weapon[] componentsInChildren2 = ((Component)this).GetComponentsInChildren<Weapon>(true);
foreach (Weapon val2 in componentsInChildren2)
{
if (!((Object)(object)val2 == (Object)null))
{
CollisionWeapon component = ((Component)val2).GetComponent<CollisionWeapon>();
if ((Object)(object)component != (Object)null && component.damage > 0f)
{
component.damage *= damageMul;
}
}
}
if (Plugin.ShowStats.Value)
{
Plugin.Log.LogInfo((object)("[UnitRng] " + (((Object)(object)unit != (Object)null) ? ((Object)unit).name : ((Object)((Component)this).gameObject).name) + " Speedx=" + speedMul.ToString("0.00") + " AtkSpdx=" + attackSpeedMul.ToString("0.00") + " Massx=" + massMul.ToString("0.00") + " Dmgx=" + damageMul.ToString("0.00")));
}
}
}
}