Decompiled source of Valerius Custom Prefabs v0.1.4

plugins\ValeriusCustomPrefabs.dll

Decompiled 3 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using Jotunn;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("ValeriusCustomPrefabs")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ValeriusCustomPrefabs")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("0.1.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.0.0")]
namespace ValeriusCustomPrefabs;

[BepInPlugin("com.valerius.customprefabs", "Valerius Custom Prefabs", "0.1.4")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
internal class ValeriusCustomPrefabs : BaseUnityPlugin
{
	public const string PluginGUID = "com.valerius.customprefabs";

	public const string PluginName = "Valerius Custom Prefabs";

	public const string PluginVersion = "0.1.4";

	private void Awake()
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Expected O, but got Unknown
		Logger.LogInfo((object)"Valerius Custom Prefabs is loading");
		Harmony val = new Harmony("com.valerius.customprefabs");
		val.PatchAll();
		PrefabManager.OnVanillaPrefabsAvailable += CreateInvisibleWall;
		PrefabManager.OnVanillaPrefabsAvailable += CreateInvisibleFloor;
		PrefabManager.OnVanillaPrefabsAvailable += CreateInvisibleWall4x4;
		PrefabManager.OnVanillaPrefabsAvailable += CreateInvisibleFloor4x4;
		PrefabManager.OnVanillaPrefabsAvailable += CreateDungeonMarker;
	}

	private void CreateInvisibleWall()
	{
		PrefabManager.OnVanillaPrefabsAvailable -= CreateInvisibleWall;
		try
		{
			AddInvisiblePiece("ValeriusInvisibleWall", "woodwall", "Invisible Boundary Wall", "A physical invisible boundary wall.");
		}
		catch (Exception arg)
		{
			Logger.LogError((object)$"Failed to create Invisible Boundary Wall: {arg}");
		}
	}

	private void CreateInvisibleFloor()
	{
		PrefabManager.OnVanillaPrefabsAvailable -= CreateInvisibleFloor;
		try
		{
			AddInvisiblePiece("ValeriusInvisibleFloor", "wood_floor", "Invisible Boundary Floor", "A physical invisible boundary floor.");
		}
		catch (Exception arg)
		{
			Logger.LogError((object)$"Failed to create Invisible Boundary Floor: {arg}");
		}
	}

	private void CreateInvisibleWall4x4()
	{
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		PrefabManager.OnVanillaPrefabsAvailable -= CreateInvisibleWall4x4;
		try
		{
			AddInvisiblePiece("ValeriusInvisibleWall4x4", "woodwall", "Invisible Boundary Wall 4x4", "A physical invisible boundary wall.", (Vector3?)new Vector3(2f, 2f, 1f));
		}
		catch (Exception arg)
		{
			Logger.LogError((object)$"Failed to create Invisible Boundary Wall 4x4: {arg}");
		}
	}

	private void CreateInvisibleFloor4x4()
	{
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		PrefabManager.OnVanillaPrefabsAvailable -= CreateInvisibleFloor4x4;
		try
		{
			AddInvisiblePiece("ValeriusInvisibleFloor4x4", "wood_floor", "Invisible Boundary Floor 4x4", "A physical invisible boundary floor.", (Vector3?)new Vector3(2f, 1f, 2f));
		}
		catch (Exception arg)
		{
			Logger.LogError((object)$"Failed to create Invisible Boundary Floor 4x4: {arg}");
		}
	}

	private void AddInvisiblePiece(string prefabName, string basePrefabName, string displayName, string description, Vector3? scale = null)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: 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_0042: 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_0057: Expected O, but got Unknown
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Expected O, but got Unknown
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = GameObject.CreatePrimitive((PrimitiveType)3);
		Sprite icon = RenderManager.Instance.Render(val, RenderManager.IsometricRotation);
		Object.Destroy((Object)(object)val);
		PieceConfig val2 = new PieceConfig
		{
			Name = displayName,
			Description = description,
			PieceTable = "Hammer",
			Category = "Misc",
			Icon = icon
		};
		CustomPiece val3 = new CustomPiece(prefabName, basePrefabName, val2);
		if ((Object)(object)val3.PiecePrefab == (Object)null)
		{
			throw new InvalidOperationException("Failed to create prefab '" + prefabName + "'.");
		}
		if (scale.HasValue)
		{
			val3.PiecePrefab.transform.localScale = scale.Value;
		}
		WearNTear component = val3.PiecePrefab.GetComponent<WearNTear>();
		if ((Object)(object)component != (Object)null)
		{
			component.m_noSupportWear = true;
		}
		val3.PiecePrefab.AddComponent<InvisiblePrefabBehaviour>();
		PieceManager.Instance.AddPiece(val3);
		Logger.LogInfo((object)("Registered piece: " + displayName + " (" + prefabName + ")"));
	}

	private void CreateDungeonMarker()
	{
		//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)
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Expected O, but got Unknown
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Expected O, but got Unknown
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: Unknown result type (might be due to invalid IL or missing references)
		PrefabManager.OnVanillaPrefabsAvailable -= CreateDungeonMarker;
		try
		{
			PieceConfig val = new PieceConfig
			{
				Name = "Dungeon Marker",
				Description = "A marker used to identify dungeon areas when scripting with Expand World Prefabs.",
				PieceTable = "Hammer",
				Category = "Misc"
			};
			CustomPiece val2 = new CustomPiece("ValeriusDungeonMarker", "guard_stone", val);
			if ((Object)(object)val2.PiecePrefab == (Object)null)
			{
				throw new InvalidOperationException("Failed to create Dungeon Marker.");
			}
			if ((Object)(object)val2.PiecePrefab.GetComponent<TeleportWorld>() == (Object)null)
			{
				val2.PiecePrefab.AddComponent<TeleportWorld>();
			}
			TeleportWorldTrigger[] componentsInChildren = val2.PiecePrefab.GetComponentsInChildren<TeleportWorldTrigger>(true);
			foreach (TeleportWorldTrigger val3 in componentsInChildren)
			{
				Object.DestroyImmediate((Object)(object)val3, true);
			}
			PrivateArea component = val2.PiecePrefab.GetComponent<PrivateArea>();
			if ((Object)(object)component != (Object)null)
			{
				Object.DestroyImmediate((Object)(object)component, true);
			}
			val2.PiecePrefab.transform.localScale = Vector3.one * 0.5f;
			PieceManager.Instance.AddPiece(val2);
			Logger.LogInfo((object)"Registered piece: Dungeon Marker (ValeriusDungeonMarker)");
		}
		catch (Exception arg)
		{
			Logger.LogError((object)$"Failed to create Dungeon Marker: {arg}");
		}
	}
}
internal class InvisiblePrefabBehaviour : MonoBehaviour
{
	private WearNTear wearNTear;

	private Renderer[] renderers;

	private void Awake()
	{
		wearNTear = ((Component)this).GetComponent<WearNTear>();
		renderers = ((Component)this).GetComponentsInChildren<Renderer>(true);
	}

	private void Update()
	{
		if ((Object)(object)wearNTear == (Object)null || wearNTear.m_health <= 0f)
		{
			return;
		}
		bool enabled = wearNTear.GetHealthPercentage() < 1f;
		Renderer[] array = renderers;
		foreach (Renderer val in array)
		{
			if ((Object)(object)val != (Object)null)
			{
				val.enabled = enabled;
			}
		}
	}
}
[HarmonyPatch(typeof(WearNTear), "OnPlaced")]
internal static class WearNTearOnPlacedPatch
{
	private static void Postfix(WearNTear __instance)
	{
		InvisiblePrefabBehaviour invisiblePrefabBehaviour = default(InvisiblePrefabBehaviour);
		if (!((Object)(object)__instance == (Object)null) && ((Component)__instance).TryGetComponent<InvisiblePrefabBehaviour>(ref invisiblePrefabBehaviour))
		{
			ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
			if (!((Object)(object)component == (Object)null) && component.IsValid() && !(__instance.m_health <= 0f))
			{
				float num = __instance.m_health * 0.5f;
				component.GetZDO().Set(ZDOVars.s_health, num);
				component.InvokeRPC(ZNetView.Everybody, "RPC_HealthChanged", new object[1] { num });
			}
		}
	}
}