using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;
using Zen.Config;
using Zen.Lib;
using Zen.Logging;
using ZenRaids.SpawnProtect;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ZenRaids")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ZenRaids")]
[assembly: AssemblyCopyright("Copyright \ufffd 2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("0.0.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.1.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.Embedded]
internal sealed class EmbeddedAttribute : Attribute
{
}
}
namespace System.Runtime.CompilerServices
{
[CompilerGenerated]
[Microsoft.CodeAnalysis.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]
[Microsoft.CodeAnalysis.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]
[Microsoft.CodeAnalysis.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 ZenRaids
{
[BepInPlugin("ZenDragon.ZenRaids", "ZenRaids", "0.3.12")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
internal class Plugin : ZenMod<Plugin>
{
public const string PluginName = "ZenRaids";
public const string PluginVersion = "0.3.12";
public const string PluginGUID = "ZenDragon.ZenRaids";
protected override void Setup()
{
((ZenMod)this).RunOnServer = true;
((ZenMod)this).PrefabsRegistered += LitArea.SetPrefabObjects;
}
protected override void TitleScene(bool isFirstBoot)
{
}
protected override void WorldStart()
{
}
protected override void Shutdown()
{
}
protected override void HotRestore()
{
LitArea.SetPrefabObjects();
}
}
[HarmonyPatch]
public class RaidRules
{
private static class Configs
{
public static readonly ConfigEntry<StringList> NeverDisableRaids;
public static readonly ConfigEntry<Biome> NoRaidBiomes;
public static readonly ConfigEntry<StringList> NoRaidBiomesExceptionRaids;
public static readonly ConfigEntry<int> MinPlayerCount;
static Configs()
{
//IL_0049: Unknown result type (might be due to invalid IL or missing references)
//IL_004e: Unknown result type (might be due to invalid IL or missing references)
//IL_0059: Expected O, but got Unknown
//IL_0063: Expected O, but got Unknown
MinPlayerCount = Config.Define<int>(true, "General", "Min Player Count", 1, Config.AcceptRange<int>(1, 10), "Minimum number of players required to be online to trigger a raid.");
NoRaidBiomes = Config.Define<Biome>(true, "General", "No Raid Biomes", (Biome)1, "No Raids allowed in these biomes");
StringList val = new StringList();
((List<string>)val).Add("army_eikthyr");
NoRaidBiomesExceptionRaids = Config.Define<StringList>(true, "General", "No Raid Biomes Exeption Raids", val, "These raids are allowed even in NoRaidBiomes. Comma separated list of raid names.");
NeverDisableRaids = Config.Define<StringList>(true, "General", "Never Disable Raids", StringList.Empty, "These raids have their disable condition removed so they are always in rotation to be triggered.\r\nComma separated list of raid names.");
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(RandEventSystem), "Start")]
private static void RandEventSystem_Start(RandEventSystem __instance)
{
foreach (string raidName in (List<string>)(object)Configs.NeverDisableRaids.Value)
{
RandomEvent val = __instance.m_events.Find((RandomEvent ev) => ev.m_name == raidName);
if (val == null)
{
Log.Warning((object)("Raid not found in RandEventSystem events: " + raidName), (ushort)0);
continue;
}
val.m_notRequiredGlobalKeys.Clear();
val.m_altNotRequiredPlayerKeys.Clear();
val.m_altRequiredNotKnownItems.Clear();
}
}
[HarmonyPrefix]
[HarmonyPatch(typeof(RandEventSystem), "StartRandomEvent")]
private static void RandEventSystem_StartRandomEvent(ref bool __runOriginal)
{
int nrOfPlayers = ZNet.instance.GetNrOfPlayers();
int value = Configs.MinPlayerCount.Value;
__runOriginal = nrOfPlayers >= value;
if (!__runOriginal)
{
Log.Message((object)$"Not enough players online for raid. Min player count: {value}, currently online: {nrOfPlayers}", (ushort)0);
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(RandEventSystem), "InValidBiome")]
private static void RandEventSystem_InValidBiome(RandEventSystem __instance, RandomEvent ev, Vector3 point, ref bool __result)
{
//IL_0005: Unknown result type (might be due to invalid IL or missing references)
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
//IL_0011: Unknown result type (might be due to invalid IL or missing references)
//IL_001b: Unknown result type (might be due to invalid IL or missing references)
//IL_0067: Unknown result type (might be due to invalid IL or missing references)
Biome biome = WorldGenerator.instance.GetBiome(point);
if (((Enum)Configs.NoRaidBiomes.Value).HasFlag((Enum)(object)biome))
{
if (Configs.NoRaidBiomesExceptionRaids.Value.Contains(ev.m_name, true))
{
Log.Message((object)("Exception, raid " + ev.m_name + " allowed in Value"), (ushort)0);
return;
}
Log.Message((object)string.Format("{0} {1} suppressed raid: {2}", "NoRaidBiomes", biome, ev.m_name), (ushort)0);
__result = false;
}
}
}
}
namespace ZenRaids.SpawnProtect
{
internal static class Configs
{
public static readonly ConfigEntry<bool> FireMustBeLit;
public static readonly ConfigEntry<bool> InteractViewPerimeter;
public static readonly ConfigEntry<float> LightRangePercent;
public static readonly ConfigEntry<int> PlayerBaseRaidThreshold;
public const int VanillaBaseCount = 3;
static Configs()
{
FireMustBeLit = Config.Define<bool>(true, "Spawn Protection", "Fire Must Be Lit", true, "Workbenches and other objects will no longer protect against monster spawns. (Vanilla: false)\r\nInstead sources of fire (Fireplaces/Torches) must be lit for spawn protection. \r\nIf this is off then vanilla behavior applies.\r\nNOTE: See the detailed explanation in the readme or on thunderstore.\r\n[Logout required for changes to take effect]");
InteractViewPerimeter = Config.Define<bool>(true, "Spawn Protection", "Interact Show Perimeter", true, "Players can interact with fire sources to see the perimeter of the light's protection.\r\n[logout required for changes to take effect]");
LightRangePercent = Config.Define<float>(true, "Spawn Protection", "Light Range Percent", 1f, Config.AcceptRange<float>(0f, 3f), "When \"" + ((ConfigEntryBase)FireMustBeLit).Definition.Key + "\" is enabled this adjusts the light radius size to use for spawn protection.\r\nNote: Setting this to 0 will disable all spawn protection.\r\n[Logout requried for changes to take effect]");
PlayerBaseRaidThreshold = Config.Define<int>(true, "Spawn Protection", "Raid Threshold", 3, Config.AcceptRange<int>(1, 100), $"Number of base pieces the player needs to be near to trigger raids. (Vanilla = {3})\r\nWhen {((ConfigEntryBase)FireMustBeLit).Definition.Key} is enabled it removes spawn protection from vanilla pieces.\r\nTherefor it reduces the number of pieces in proximity to the player that can trigger raids. \r\nHigher numbers would reduce the odds of triggering a raid because you are less likely to be near so many.\r\nHowever, Keep in mind that a lit light source counts as both spawn protection and towards your raid threshold.\r\nTherefor you have two options.\r\n1. Put lights everywhere to keep the monsters from spawning in your base.\r\n2. Have a dimly lit castle and reduce the chance of raids but increase the chance of random spawns inside your base.\r\nCandles are a good example of a dimly lit castle.");
}
}
internal static class Extensions
{
internal static bool TryGetTorch(this Player? player, out ItemData torch)
{
torch = null;
if (!Object.op_Implicit((Object)(object)player))
{
return false;
}
ItemData val = ((IEnumerable<ItemData>)((Humanoid)player).GetInventory().GetAllItems()).FirstOrDefault((Func<ItemData, bool>)((ItemData item) => ItemDataExt.IsItemType(item, (ItemType)15)));
if (val == null)
{
return false;
}
torch = val;
return true;
}
internal static bool IsTorchHeld(this Player? player)
{
if (!Object.op_Implicit((Object)(object)player))
{
return false;
}
ItemData rightItem = ((Humanoid)player).RightItem;
if (rightItem == null || !ItemDataExt.IsItemType(rightItem, (ItemType)15))
{
ItemData leftItem = ((Humanoid)player).LeftItem;
if (leftItem == null)
{
return false;
}
return ItemDataExt.IsItemType(leftItem, (ItemType)15);
}
return true;
}
internal static bool TryEquipTorch(this Player? player)
{
if (!Object.op_Implicit((Object)(object)player))
{
return false;
}
Log.Info((object)"Trying to equip torch", (ushort)0);
if (player.TryGetTorch(out ItemData torch))
{
Log.Info((object)("Torch found in inventory " + ItemDataExt.GetPrefabName(torch)), (ushort)0);
if (!torch.m_equipped)
{
((Humanoid)player).ToggleEquipped(torch);
}
return true;
}
Log.Info((object)"Torch not found in inventory", (ushort)0);
return false;
}
public static bool IsPlacementGhost(this Fireplace fireplace)
{
return Player.IsPlacementGhost(((Component)fireplace).gameObject);
}
public static bool IsLit(this Fireplace fireplace)
{
GameObject enabledObject = fireplace.GetEnabledObject();
bool flag = Object.op_Implicit((Object)(object)enabledObject) && enabledObject.activeInHierarchy;
bool flag2 = GameObjectExt.GetPrefabName(((Component)fireplace).gameObject) == "bonfire";
if (fireplace.IsBurning())
{
return flag || flag2;
}
return false;
}
public static GameObject? GetEnabledObject(this Fireplace fireplace)
{
if (Object.op_Implicit((Object)(object)fireplace.m_enabledObjectHigh))
{
return fireplace.m_enabledObjectHigh;
}
if (Object.op_Implicit((Object)(object)fireplace.m_enabledObject))
{
return fireplace.m_enabledObject;
}
return null;
}
}
[HarmonyPatch]
public class LitArea : MonoBehaviour
{
private static GameObject _prefabAreaMarker;
private static GameObject _prefabPulse;
private Fireplace _fireplace;
private GameObject _litArea;
private GameObject _litPulse;
private float _lastActivated;
private const float Delay = 0.5f;
private const string LitAreaName = "LitAreaMarker";
public bool IsVisible
{
get
{
return _litArea.activeSelf;
}
set
{
_litArea.SetActive(value || Time.time < _lastActivated + 0.5f);
_litPulse.SetActive(_litArea.activeSelf);
if (value)
{
_lastActivated = Time.time;
}
}
}
internal static Action? SetPrefabObjects()
{
_prefabAreaMarker = ((Component)ZNetScene.instance.GetPrefab("piece_workbench").transform.Find("AreaMarker")).gameObject;
_prefabPulse = ((Component)ZNetScene.instance.GetPrefab("guard_stone").transform.Find("WayEffect/pulse (2)")).gameObject;
return null;
}
private void Awake()
{
//IL_0060: Unknown result type (might be due to invalid IL or missing references)
//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
_fireplace = ((Component)this).GetComponent<Fireplace>();
_litArea = Object.Instantiate<GameObject>(_prefabAreaMarker, ((Component)this).transform, false);
((Object)_litArea).name = "LitAreaMarker";
_litArea.SetActive(false);
((Component)_litArea.GetComponentInChildren<ParticleSystem>()).gameObject.SetActive(false);
_litArea.transform.localPosition = Vector3.zero;
_litPulse = Object.Instantiate<GameObject>(_prefabPulse, ((Component)this).transform, false);
((Object)_litPulse).name = "LitAreaMarker_Pulse";
_litPulse.SetActive(false);
_litPulse.transform.localPosition = Vector3.zero;
GameObject obj = Object.Instantiate<GameObject>(((Component)_prefabPulse.transform.parent.Find("sfx")).gameObject, _litPulse.transform, false);
((Object)obj).name = "LitAreaMarker_SFX";
obj.SetActive(true);
obj.transform.localPosition = Vector3.zero;
AudioSource component = obj.GetComponent<AudioSource>();
component.minDistance = 0f;
component.maxDistance = 30f;
component.pitch = 1.7f;
}
public void Init(float lightRange)
{
//IL_0034: Unknown result type (might be due to invalid IL or missing references)
//IL_0039: Unknown result type (might be due to invalid IL or missing references)
//IL_0065: Unknown result type (might be due to invalid IL or missing references)
//IL_006a: Unknown result type (might be due to invalid IL or missing references)
//IL_0076: Unknown result type (might be due to invalid IL or missing references)
//IL_0084: Unknown result type (might be due to invalid IL or missing references)
lightRange *= Configs.LightRangePercent.Value;
Log.Info((object)$"Light Radius: {lightRange:F2} - {GameObjectExt.GetPrefabName(((Component)_fireplace).gameObject)} {MathExt.XZY(((Component)_fireplace).transform.position)}", (ushort)0);
_litArea.GetComponent<CircleProjector>().m_radius = lightRange;
Vector3 lossyScale = ((Component)this).transform.lossyScale;
_litPulse.transform.localScale = Vector3.one * (lightRange / ((Vector3)(ref lossyScale)).magnitude);
if (_fireplace.IsPlacementGhost())
{
return;
}
Collider collider = _fireplace.m_playerBaseObject.GetComponent<EffectArea>().m_collider;
SphereCollider val = (SphereCollider)(object)((collider is SphereCollider) ? collider : null);
if (val == null)
{
CapsuleCollider val2 = (CapsuleCollider)(object)((collider is CapsuleCollider) ? collider : null);
if (val2 != null)
{
val2.radius = lightRange;
}
else
{
Log.Error((object)(((Object)_fireplace).name + " PlayerBase object has no sphere or capsule collider"), (ushort)0);
}
}
else
{
val.radius = lightRange;
}
}
private void LateUpdate()
{
if (!Configs.FireMustBeLit.Value)
{
return;
}
if (_fireplace.IsPlacementGhost())
{
_litArea.SetActive(true);
_litPulse.SetActive(false);
return;
}
_fireplace.m_playerBaseObject.SetActive(_fireplace.IsLit());
if (IsVisible && !Player.m_localPlayer.IsTorchHeld())
{
IsVisible = false;
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(CircleProjector), "Start")]
private static void CircleProjector_Start(CircleProjector __instance)
{
CircleProjector __instance2 = __instance;
if (!(((Object)__instance2).name != "LitAreaMarker"))
{
CollectionExtensions.Do<GameObject>((IEnumerable<GameObject>)__instance2.m_segments, (Action<GameObject>)delegate(GameObject segment)
{
//IL_0021: Unknown result type (might be due to invalid IL or missing references)
//IL_0036: Unknown result type (might be due to invalid IL or missing references)
//IL_003b: Unknown result type (might be due to invalid IL or missing references)
segment.transform.localScale = new Vector3(0.05f, 0.25f, __instance2.m_radius / 20f);
((Renderer)segment.GetComponent<MeshRenderer>()).material.color = ZColor.op_Implicit(UIColor.Orange);
});
}
}
}
[HarmonyPatch]
public static class Patches
{
[HarmonyPostfix]
[HarmonyPatch(typeof(EffectArea), "Awake")]
private static void PlayerBaseEffectArea_Awake(EffectArea __instance)
{
//IL_0020: Unknown result type (might be due to invalid IL or missing references)
EffectArea __instance2 = __instance;
if (!Configs.FireMustBeLit.Value || !((Enum)__instance2.m_type).HasFlag((Enum)(object)(Type)4))
{
return;
}
Piece piece = ((Component)__instance2).GetComponentInParent<Piece>();
Timing.EndOfFrame((MonoBehaviour)(object)__instance2, (Action)delegate
{
if (Object.op_Implicit((Object)(object)piece) && piece.IsPlacedByPlayer())
{
((Component)__instance2).gameObject.SetActive(false);
}
});
}
[HarmonyPostfix]
[HarmonyPatch(typeof(EffectArea), "GetBaseValue")]
private static void EffectArea_GetBaseValue(ref int __result)
{
__result -= 3 - Configs.PlayerBaseRaidThreshold.Value;
Log.Debug((object)$"Nearby PlayerBase: {__result} (Config raid threshold: {Configs.PlayerBaseRaidThreshold.Value})", (ushort)0);
}
[HarmonyPostfix]
[HarmonyPatch(typeof(Fireplace), "Awake")]
[HarmonyPriority(0)]
private static void Fireplace_Awake(Fireplace __instance)
{
FixPlayerBaseObject(__instance);
}
private static void FixPlayerBaseObject(Fireplace fireplace)
{
//IL_0014: Unknown result type (might be due to invalid IL or missing references)
//IL_0019: Unknown result type (might be due to invalid IL or missing references)
if (Object.op_Implicit((Object)(object)fireplace.m_playerBaseObject))
{
Scene scene = fireplace.m_playerBaseObject.scene;
if (!((Scene)(ref scene)).isLoaded)
{
Log.Info((object)(((Object)fireplace).name + " playerBaseObject is pointing to prefab, nulling to fix"), (ushort)0);
fireplace.m_playerBaseObject = null;
}
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(Fireplace), "Start")]
private static void Fireplace_Start(Fireplace __instance)
{
//IL_003f: Unknown result type (might be due to invalid IL or missing references)
//IL_0044: Unknown result type (might be due to invalid IL or missing references)
if (Configs.FireMustBeLit.Value && (__instance.IsValidInteract(requireLit: false) || __instance.IsPlacementGhost()))
{
float lightRange = GetLightRange(__instance);
if (lightRange < 0f)
{
Log.Info((object)$"{((Object)__instance).name} {MathExt.XZY(((Component)__instance).transform.position)} light range not found, skipping", (ushort)0);
}
else
{
((Component)__instance).gameObject.AddComponent<LitArea>().Init(lightRange);
}
}
}
private static float GetLightRange(Fireplace fireplace)
{
GameObject enabledObject = fireplace.GetEnabledObject();
if ((Object)(object)enabledObject == (Object)null)
{
return -1f;
}
LightLod componentInChildren = enabledObject.GetComponentInChildren<LightLod>();
if (!Object.op_Implicit((Object)(object)componentInChildren))
{
return -1f;
}
if (Object.op_Implicit((Object)(object)componentInChildren.m_light))
{
if (!componentInChildren.m_lightLod)
{
return componentInChildren.m_light.range;
}
return componentInChildren.m_baseRange;
}
Log.Debug((object)"LightLod never initialized. Getting the Light component range directly", (ushort)0);
return ((Component)componentInChildren).GetComponent<Light>().range;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(Fireplace), "Interact")]
private static void Fireplace_Interact(Fireplace __instance, bool alt, bool hold, ref bool __runOriginal, ref bool __result)
{
if (!Configs.FireMustBeLit.Value || ZInput.GetKey((KeyCode)304, true) || ZInput.GetKey((KeyCode)303, true))
{
return;
}
if (alt && hold)
{
__runOriginal = false;
}
else if (!(!alt || hold) && __instance.IsValidInteract(requireLit: true))
{
__runOriginal = false;
LitArea litArea = default(LitArea);
if (!Player.m_localPlayer.IsTorchHeld() && !Player.m_localPlayer.TryEquipTorch())
{
((Character)Player.m_localPlayer).Message((MessageType)2, "You need a torch to check the perimeter.", 0, (Sprite)null);
}
else if (((Component)__instance).TryGetComponent<LitArea>(ref litArea))
{
litArea.IsVisible = !litArea.IsVisible;
}
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(Fireplace), "GetHoverText")]
[HarmonyPriority(200)]
private static void Fireplace_GetHoverText(Fireplace __instance, ref string __result)
{
LitArea litArea = default(LitArea);
if (Configs.FireMustBeLit.Value && __instance.IsValidInteract(requireLit: true) && ((Component)__instance).TryGetComponent<LitArea>(ref litArea))
{
string text = (litArea.IsVisible ? "Hide" : "Show") + " perimeter";
__result = StringExt.Localize(UI.InsertInteractAlt(__result, text));
}
}
private static bool IsValidInteract(this Fireplace fireplace, bool requireLit)
{
if (!Configs.InteractViewPerimeter.Value)
{
return false;
}
if (!Object.op_Implicit((Object)(object)fireplace.m_piece) || !fireplace.m_piece.IsPlacedByPlayer() || !Object.op_Implicit((Object)(object)fireplace.m_playerBaseObject))
{
return false;
}
if (requireLit && !fireplace.IsLit())
{
return false;
}
return true;
}
}
}