Decompiled source of Watchtowers v1.0.0

plugins/Watchtowers.dll

Decompiled 6 days ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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 Jotunn;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("JotunnModStub")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("JotunnModStub")]
[assembly: AssemblyCopyright("Copyright ©  2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Watchtowers;

public static class TowerBuilder
{
	public static int Build(GameObject tower, IEnumerable<TowerPart> parts)
	{
		float num = GroundLevel(tower);
		Logger.LogDebug((object)$"{((Object)tower).name}: ground level {num}");
		int num2 = 0;
		foreach (TowerPart part in parts)
		{
			if (AddPart(tower.transform, part, num))
			{
				num2++;
			}
		}
		return num2;
	}

	public static float GroundLevel(GameObject tower)
	{
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		WearNTear component = tower.GetComponent<WearNTear>();
		if ((Object)(object)component != (Object)null && (Object)(object)component.m_new != (Object)null && TryMeasure(tower.transform, component.m_new, out var bounds))
		{
			return ((Bounds)(ref bounds)).min.y;
		}
		return 0f;
	}

	public static bool AddFire(GameObject tower, string prefabName, Vector3 bottom)
	{
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0104: Unknown result type (might be due to invalid IL or missing references)
		//IL_0106: Unknown result type (might be due to invalid IL or missing references)
		//IL_010b: Unknown result type (might be due to invalid IL or missing references)
		//IL_012f: Unknown result type (might be due to invalid IL or missing references)
		//IL_013c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0141: Unknown result type (might be due to invalid IL or missing references)
		//IL_0146: Unknown result type (might be due to invalid IL or missing references)
		//IL_0148: Unknown result type (might be due to invalid IL or missing references)
		//IL_015f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0164: Unknown result type (might be due to invalid IL or missing references)
		//IL_016f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0174: 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_00d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00df: Unknown result type (might be due to invalid IL or missing references)
		GameObject prefab = PrefabManager.Instance.GetPrefab(prefabName);
		if ((Object)(object)prefab == (Object)null)
		{
			Logger.LogWarning((object)("Fire not found: " + prefabName));
			return false;
		}
		Fireplace component = prefab.GetComponent<Fireplace>();
		GameObject val = (((Object)(object)component != (Object)null) ? component.m_enabledObject : null);
		if ((Object)(object)val == (Object)null)
		{
			Logger.LogWarning((object)("Fire has no flames to copy: " + prefabName));
			return false;
		}
		Transform transform = tower.transform;
		float num = GroundLevel(tower);
		AddPart(transform, new TowerPart(prefabName, bottom, Vector3.zero), num);
		Vector3 zero = Vector3.zero;
		WearNTear component2 = prefab.GetComponent<WearNTear>();
		if ((Object)(object)component2 != (Object)null && (Object)(object)component2.m_new != (Object)null && TryMeasure(prefab.transform, component2.m_new, out var bounds))
		{
			((Vector3)(ref zero))..ctor(((Bounds)(ref bounds)).center.x, ((Bounds)(ref bounds)).min.y, ((Bounds)(ref bounds)).center.z);
		}
		Vector3 val2 = prefab.transform.InverseTransformPoint(val.transform.position) - zero;
		GameObject val3 = Object.Instantiate<GameObject>(val, transform);
		((Object)val3).name = "fire_" + prefabName;
		val3.transform.localPosition = bottom + new Vector3(0f, num, 0f) + val2;
		val3.transform.localRotation = Quaternion.Inverse(prefab.transform.rotation) * val.transform.rotation;
		val3.SetActive(true);
		Collider[] componentsInChildren = val3.GetComponentsInChildren<Collider>(true);
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			Object.DestroyImmediate((Object)(object)componentsInChildren[i]);
		}
		MonoBehaviour[] componentsInChildren2 = val3.GetComponentsInChildren<MonoBehaviour>(true);
		foreach (MonoBehaviour val4 in componentsInChildren2)
		{
			string name = ((object)val4).GetType().Name;
			if (name != "LightFlicker" && name != "LightLod")
			{
				Object.DestroyImmediate((Object)(object)val4);
			}
		}
		return true;
	}

	public static void AddLight(GameObject tower, Vector3 position, Color color, float range, float intensity)
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject("beacon_light");
		val.transform.SetParent(tower.transform, false);
		val.transform.localPosition = position;
		Light obj = val.AddComponent<Light>();
		obj.type = (LightType)2;
		obj.color = color;
		obj.range = range;
		obj.intensity = intensity;
		obj.shadows = (LightShadows)0;
	}

	private static bool AddPart(Transform root, TowerPart part, float ground)
	{
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0132: Unknown result type (might be due to invalid IL or missing references)
		//IL_0154: Unknown result type (might be due to invalid IL or missing references)
		//IL_0168: Unknown result type (might be due to invalid IL or missing references)
		//IL_016d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0183: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_023c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0252: Unknown result type (might be due to invalid IL or missing references)
		//IL_025e: Unknown result type (might be due to invalid IL or missing references)
		//IL_026a: Unknown result type (might be due to invalid IL or missing references)
		//IL_027a: Unknown result type (might be due to invalid IL or missing references)
		//IL_028a: Unknown result type (might be due to invalid IL or missing references)
		//IL_028f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0294: Unknown result type (might be due to invalid IL or missing references)
		//IL_0298: Unknown result type (might be due to invalid IL or missing references)
		//IL_029d: Unknown result type (might be due to invalid IL or missing references)
		//IL_029f: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_02bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_020b: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_022b: Unknown result type (might be due to invalid IL or missing references)
		GameObject prefab = PrefabManager.Instance.GetPrefab(part.Prefab);
		if ((Object)(object)prefab == (Object)null)
		{
			Logger.LogWarning((object)("Tower part not found: " + part.Prefab));
			return false;
		}
		WearNTear component = prefab.GetComponent<WearNTear>();
		GameObject val = (((Object)(object)component != (Object)null) ? component.m_new : null);
		if ((Object)(object)val == (Object)null)
		{
			Logger.LogWarning((object)("Tower part has no model to copy: " + part.Prefab));
			return false;
		}
		Transform transform = new GameObject("part_" + part.Prefab).transform;
		transform.SetParent(root, false);
		Transform transform2 = new GameObject("turn").transform;
		transform2.SetParent(transform, false);
		GameObject val2 = Object.Instantiate<GameObject>(val, transform2);
		val2.SetActive(true);
		StripToVisualsOnly(val2);
		if (!TryMeasure(root, val2, out var bounds))
		{
			Logger.LogWarning((object)("Tower part has no mesh: " + part.Prefab));
			Object.DestroyImmediate((Object)(object)((Component)transform).gameObject);
			return false;
		}
		float num = ((((Bounds)(ref bounds)).size.z > ((Bounds)(ref bounds)).size.x + 0.01f) ? 90f : 0f);
		transform2.localRotation = Quaternion.Euler(0f, num + part.RotationY, 0f);
		TryMeasure(root, val2, out bounds);
		Logger.LogDebug((object)$"Tower part {part.Prefab}: vanilla size {((Bounds)(ref bounds)).size}");
		Vector3 one = Vector3.one;
		if (part.Size.x > 0f && ((Bounds)(ref bounds)).size.x > 0.01f)
		{
			one.x = part.Size.x / ((Bounds)(ref bounds)).size.x;
		}
		if (part.Size.y > 0f && ((Bounds)(ref bounds)).size.y > 0.01f)
		{
			one.y = part.Size.y / ((Bounds)(ref bounds)).size.y;
		}
		if (part.Size.z > 0f && ((Bounds)(ref bounds)).size.z > 0.01f)
		{
			one.z = part.Size.z / ((Bounds)(ref bounds)).size.z;
		}
		transform.localScale = one;
		TryMeasure(root, val2, out bounds);
		Vector3 val3 = default(Vector3);
		((Vector3)(ref val3))..ctor(((Bounds)(ref bounds)).center.x, ((Bounds)(ref bounds)).min.y, ((Bounds)(ref bounds)).center.z);
		Vector3 val4 = part.Bottom + new Vector3(0f, ground, 0f);
		transform.localPosition += val4 - val3;
		TryMeasure(root, val2, out bounds);
		AddBoxCollider(root, bounds, part.Prefab);
		return true;
	}

	private static void AddBoxCollider(Transform root, Bounds bounds, string partName)
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: 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_0047: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject("collider_" + partName)
		{
			layer = ((Component)root).gameObject.layer
		};
		val.transform.SetParent(root, false);
		val.transform.localPosition = ((Bounds)(ref bounds)).center;
		val.AddComponent<BoxCollider>().size = ((Bounds)(ref bounds)).size;
	}

	private static void StripToVisualsOnly(GameObject copy)
	{
		Collider[] componentsInChildren = copy.GetComponentsInChildren<Collider>(true);
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			Object.DestroyImmediate((Object)(object)componentsInChildren[i]);
		}
		MonoBehaviour[] componentsInChildren2 = copy.GetComponentsInChildren<MonoBehaviour>(true);
		for (int i = 0; i < componentsInChildren2.Length; i++)
		{
			Object.DestroyImmediate((Object)(object)componentsInChildren2[i]);
		}
		if (!((Object)(object)copy.GetComponentInChildren<LODGroup>(true) == (Object)null))
		{
			return;
		}
		Renderer[] componentsInChildren3 = copy.GetComponentsInChildren<Renderer>(true);
		foreach (Renderer val in componentsInChildren3)
		{
			if (!((Object)(object)val == (Object)null))
			{
				string text = ((Object)((Component)val).gameObject).name.ToLowerInvariant();
				if (text.Contains("lod1") || text.Contains("lod2") || text.Contains("lod3"))
				{
					Object.DestroyImmediate((Object)(object)((Component)val).gameObject);
				}
			}
		}
	}

	private static bool TryMeasure(Transform root, GameObject obj, out Bounds bounds)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: 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_0073: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00df: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_0100: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
		bounds = default(Bounds);
		bool flag = false;
		Matrix4x4 worldToLocalMatrix = root.worldToLocalMatrix;
		MeshFilter[] componentsInChildren = obj.GetComponentsInChildren<MeshFilter>(true);
		Vector3 val3 = default(Vector3);
		foreach (MeshFilter val in componentsInChildren)
		{
			MeshRenderer component = ((Component)val).GetComponent<MeshRenderer>();
			if ((Object)(object)val.sharedMesh == (Object)null || (Object)(object)component == (Object)null || !((Renderer)component).enabled)
			{
				continue;
			}
			Matrix4x4 val2 = worldToLocalMatrix * ((Component)val).transform.localToWorldMatrix;
			Bounds bounds2 = val.sharedMesh.bounds;
			for (int j = 0; j < 8; j++)
			{
				((Vector3)(ref val3))..ctor(((j & 1) == 0) ? (-1f) : 1f, ((j & 2) == 0) ? (-1f) : 1f, ((j & 4) == 0) ? (-1f) : 1f);
				Vector3 val4 = ((Bounds)(ref bounds2)).center + Vector3.Scale(((Bounds)(ref bounds2)).extents, val3);
				Vector3 val5 = ((Matrix4x4)(ref val2)).MultiplyPoint3x4(val4);
				if (!flag)
				{
					bounds = new Bounds(val5, Vector3.zero);
					flag = true;
				}
				else
				{
					((Bounds)(ref bounds)).Encapsulate(val5);
				}
			}
		}
		return flag;
	}
}
public class TowerDesign
{
	public List<TowerPart> Parts = new List<TowerPart>();

	public string FirePrefab;

	public Vector3 FireBottom;
}
public static class TowerDesigns
{
	public static TowerDesign Wood()
	{
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_0136: Unknown result type (might be due to invalid IL or missing references)
		//IL_013b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0162: Unknown result type (might be due to invalid IL or missing references)
		//IL_0167: Unknown result type (might be due to invalid IL or missing references)
		//IL_019d: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
		TowerDesign towerDesign = new TowerDesign();
		for (int i = 0; i < 4; i++)
		{
			towerDesign.Parts.AddRange(WallRing("woodwall", (float)i * 2f, 1.9f, 4f, 2f, 0.4f, 2, 0.15f));
		}
		towerDesign.Parts.Add(new TowerPart("wood_floor", new Vector3(0f, 8f, 0f), new Vector3(4.4f, 0.3f, 4.4f)));
		Vector3 size = default(Vector3);
		((Vector3)(ref size))..ctor(0f, 1.5f, 0f);
		for (int j = 0; j < 5; j++)
		{
			float num = -2f + (float)j;
			towerDesign.Parts.Add(new TowerPart("wood_pole_log", new Vector3(num, 8f, 2f), size));
			towerDesign.Parts.Add(new TowerPart("wood_pole_log", new Vector3(num, 8f, -2f), size));
		}
		for (int k = 1; k < 4; k++)
		{
			float num2 = -2f + (float)k;
			towerDesign.Parts.Add(new TowerPart("wood_pole_log", new Vector3(2f, 8f, num2), size));
			towerDesign.Parts.Add(new TowerPart("wood_pole_log", new Vector3(-2f, 8f, num2), size));
		}
		towerDesign.FirePrefab = "piece_groundtorch_wood";
		towerDesign.FireBottom = new Vector3(0f, 8.3f, 0f);
		return towerDesign;
	}

	public static TowerDesign Stone()
	{
		//IL_0058: 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_00ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_0100: Unknown result type (might be due to invalid IL or missing references)
		//IL_0105: Unknown result type (might be due to invalid IL or missing references)
		//IL_012f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0134: Unknown result type (might be due to invalid IL or missing references)
		//IL_015e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0163: Unknown result type (might be due to invalid IL or missing references)
		//IL_018d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0192: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
		TowerDesign towerDesign = new TowerDesign();
		for (int i = 0; i < 5; i++)
		{
			towerDesign.Parts.AddRange(Ring("stone_wall_4x2", (float)i * 2f, 1.75f, 4f, 2f));
		}
		towerDesign.Parts.Add(new TowerPart("stone_floor_2x2", new Vector3(0f, 10f, 0f), new Vector3(4.4f, 0.4f, 4.4f)));
		Vector3 size = default(Vector3);
		((Vector3)(ref size))..ctor(0.8f, 1.5f, 0.8f);
		foreach (Vector3 item in Corners(2f, 10f))
		{
			towerDesign.Parts.Add(new TowerPart("stone_wall_1x1", item, size));
		}
		towerDesign.Parts.Add(new TowerPart("stone_wall_1x1", new Vector3(0f, 10f, 2f), size));
		towerDesign.Parts.Add(new TowerPart("stone_wall_1x1", new Vector3(0f, 10f, -2f), size));
		towerDesign.Parts.Add(new TowerPart("stone_wall_1x1", new Vector3(2f, 10f, 0f), size));
		towerDesign.Parts.Add(new TowerPart("stone_wall_1x1", new Vector3(-2f, 10f, 0f), size));
		towerDesign.FirePrefab = "piece_groundtorch_green";
		towerDesign.FireBottom = new Vector3(0f, 10.4f, 0f);
		return towerDesign;
	}

	public static TowerDesign Marble()
	{
		//IL_0027: 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)
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: 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)
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_0115: Unknown result type (might be due to invalid IL or missing references)
		//IL_011a: Unknown result type (might be due to invalid IL or missing references)
		TowerDesign towerDesign = new TowerDesign();
		for (int i = 0; i < 6; i++)
		{
			towerDesign.Parts.Add(new TowerPart("blackmarble_2x2x2", new Vector3(0f, (float)i * 2f, 0f), new Vector3(2.2f, 2f, 2.2f)));
		}
		towerDesign.Parts.Add(new TowerPart("blackmarble_floor", new Vector3(0f, 12f, 0f), new Vector3(3.4f, 0.4f, 3.4f)));
		foreach (Vector3 item in Corners(1.4f, 12f))
		{
			towerDesign.Parts.Add(new TowerPart("blackmarble_1x1", item, new Vector3(0.7f, 1.5f, 0.7f)));
		}
		towerDesign.FirePrefab = "piece_groundtorch_blue";
		towerDesign.FireBottom = new Vector3(0f, 12.4f, 0f);
		return towerDesign;
	}

	private static IEnumerable<Vector3> Corners(float half, float y)
	{
		yield return new Vector3(half, y, half);
		yield return new Vector3(0f - half, y, half);
		yield return new Vector3(half, y, 0f - half);
		yield return new Vector3(0f - half, y, 0f - half);
	}

	private static IEnumerable<TowerPart> WallRing(string prefab, float y, float half, float length, float height, float thickness, int panelsPerSide, float overlap = 0f)
	{
		float panel = length / (float)panelsPerSide;
		int level = Mathf.RoundToInt(y / height);
		for (int i = 0; i < panelsPerSide; i++)
		{
			float offset = (0f - length) / 2f + panel * ((float)i + 0.5f);
			float width = panel + overlap;
			float tall = height + overlap;
			float depth = ((thickness > 0f) ? (thickness + (float)((i + level) % 2) * 0.02f) : 0f);
			yield return new TowerPart(prefab, new Vector3(offset, y, half), new Vector3(width, tall, depth));
			yield return new TowerPart(prefab, new Vector3(offset, y, 0f - half), new Vector3(width, tall, depth));
			yield return new TowerPart(prefab, new Vector3(half, y, offset), new Vector3(depth, tall, width), 90f);
			yield return new TowerPart(prefab, new Vector3(0f - half, y, offset), new Vector3(depth, tall, width), 90f);
		}
	}

	private static IEnumerable<TowerPart> Ring(string prefab, float y, float half, float length, float height)
	{
		yield return new TowerPart(prefab, new Vector3(0f, y, half), new Vector3(length, height, 0f));
		yield return new TowerPart(prefab, new Vector3(0f, y, 0f - half), new Vector3(length, height, 0f));
		yield return new TowerPart(prefab, new Vector3(half, y, 0f), new Vector3(0f, height, length), 90f);
		yield return new TowerPart(prefab, new Vector3(0f - half, y, 0f), new Vector3(0f, height, length), 90f);
	}
}
public class TowerPart
{
	public string Prefab;

	public Vector3 Bottom;

	public Vector3 Size;

	public float RotationY;

	public TowerPart(string prefab, Vector3 bottom, Vector3 size, float rotationY = 0f)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		Prefab = prefab;
		Bottom = bottom;
		Size = size;
		RotationY = rotationY;
	}
}
[BepInPlugin("guimas.Watchtowers", "Watchtowers", "1.0.0")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
internal class WatchtowersPlugin : BaseUnityPlugin
{
	public const string PluginGUID = "guimas.Watchtowers";

	public const string PluginName = "Watchtowers";

	public const string PluginVersion = "1.0.0";

	private ConfigEntry<float> woodRadius;

	private ConfigEntry<float> stoneRadius;

	private ConfigEntry<float> marbleRadius;

	private ConfigEntry<bool> showFire;

	private static readonly MethodInfo LoadImageMethod = Type.GetType("UnityEngine.ImageConversion, UnityEngine.ImageConversionModule")?.GetMethod("LoadImage", new Type[2]
	{
		typeof(Texture2D),
		typeof(byte[])
	});

	private void Awake()
	{
		woodRadius = BindRadius("Wood", 300f);
		stoneRadius = BindRadius("Stone", 600f);
		marbleRadius = BindRadius("Marble", 1200f);
		showFire = ((BaseUnityPlugin)this).Config.Bind<bool>("Visuals", "Fire on top", true, "Show an always-lit decorative fire on top of each watchtower. Restart the game after changing.");
		PrefabManager.OnVanillaPrefabsAvailable += AddWatchtowers;
		Logger.LogInfo((object)"Watchtowers loaded");
	}

	private ConfigEntry<float> BindRadius(string tier, float defaultValue)
	{
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Expected O, but got Unknown
		return ((BaseUnityPlugin)this).Config.Bind<float>("Reveal radius", tier, defaultValue, new ConfigDescription("Meters of map revealed around a " + tier + " Watchtower. Restart the game after changing.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(50f, 5000f), Array.Empty<object>()));
	}

	private void AddWatchtowers()
	{
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Expected O, but got Unknown
		//IL_0081: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Expected O, but got Unknown
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: Expected O, but got Unknown
		//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: Expected O, but got Unknown
		//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ef: Expected O, but got Unknown
		AddTower("piece_watchtower_wood", "wood_pole_log_4", "Wood Watchtower", "watchtower_wood.png", CraftingStations.Workbench, woodRadius.Value, TowerDesigns.Wood(), new RequirementConfig("Wood", 30, 0, true));
		AddTower("piece_watchtower_stone", "stone_pillar", "Stone Watchtower", "watchtower_stone.png", CraftingStations.Stonecutter, stoneRadius.Value, TowerDesigns.Stone(), new RequirementConfig("Stone", 30, 0, true), new RequirementConfig("Wood", 10, 0, true));
		AddTower("piece_watchtower_marble", "blackmarble_column_1", "Marble Watchtower", "watchtower_marble.png", CraftingStations.Stonecutter, marbleRadius.Value, TowerDesigns.Marble(), new RequirementConfig("BlackMarble", 20, 0, true), new RequirementConfig("FineWood", 10, 0, true));
		PrefabManager.OnVanillaPrefabsAvailable -= AddWatchtowers;
	}

	private void AddTower(string prefabName, string basePrefab, string displayName, string iconFile, string craftingStation, float radius, TowerDesign design, params RequirementConfig[] requirements)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: 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_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Expected O, but got Unknown
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Expected O, but got Unknown
		//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
		PieceConfig val = new PieceConfig
		{
			Name = displayName,
			Description = $"Reveals the surrounding land ({radius} m).",
			PieceTable = PieceTables.Hammer,
			Category = PieceCategories.Misc,
			CraftingStation = craftingStation,
			Requirements = requirements
		};
		Sprite val2 = LoadEmbeddedSprite(iconFile);
		if ((Object)(object)val2 != (Object)null)
		{
			val.Icon = val2;
		}
		CustomPiece val3 = new CustomPiece(prefabName, basePrefab, val);
		int num = TowerBuilder.Build(val3.PiecePrefab, design.Parts);
		Logger.LogInfo((object)$"{displayName}: built from {num} parts");
		if (showFire.Value && design.FirePrefab != null)
		{
			TowerBuilder.AddFire(val3.PiecePrefab, design.FirePrefab, design.FireBottom);
		}
		val3.PiecePrefab.AddComponent<WatchtowerReveal>().Radius = radius;
		PieceManager.Instance.AddPiece(val3);
	}

	private static Sprite LoadEmbeddedSprite(string fileName)
	{
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Expected O, but got Unknown
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
		if (LoadImageMethod == null)
		{
			Logger.LogWarning((object)"Could not find Unity's LoadImage method; using vanilla icons.");
			return null;
		}
		Assembly executingAssembly = Assembly.GetExecutingAssembly();
		string text = executingAssembly.GetManifestResourceNames().FirstOrDefault((string name) => name.EndsWith(fileName));
		if (text == null)
		{
			Logger.LogWarning((object)("Icon not found inside the dll: " + fileName));
			return null;
		}
		using Stream stream = executingAssembly.GetManifestResourceStream(text);
		using MemoryStream memoryStream = new MemoryStream();
		stream.CopyTo(memoryStream);
		Texture2D val = new Texture2D(2, 2);
		LoadImageMethod.Invoke(null, new object[2]
		{
			val,
			memoryStream.ToArray()
		});
		return Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f));
	}
}
public class WatchtowerReveal : MonoBehaviour
{
	public float Radius = 200f;

	private ZNetView nview;

	private void Awake()
	{
		nview = ((Component)this).GetComponent<ZNetView>();
	}

	private IEnumerator Start()
	{
		if ((Object)(object)nview == (Object)null || !nview.IsValid())
		{
			yield break;
		}
		while ((Object)(object)Player.m_localPlayer == (Object)null || (Object)(object)Minimap.instance == (Object)null)
		{
			if ((Object)(object)ZNet.instance != (Object)null && ZNet.instance.IsDedicated())
			{
				yield break;
			}
			yield return (object)new WaitForSeconds(1f);
		}
		Minimap.instance.Explore(((Component)this).transform.position, Radius);
	}
}