Decompiled source of MineTurtle v1.0.0

MineTurtle.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using MineTurtle;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("MineTurtle")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MineTurtle")]
[assembly: AssemblyTitle("MineTurtle")]
[assembly: AssemblyVersion("1.0.0.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;
		}
	}
}
[DisallowMultipleComponent]
public class Outline : MonoBehaviour
{
	public enum Mode
	{
		OutlineAll,
		OutlineVisible,
		OutlineHidden,
		OutlineAndSilhouette,
		SilhouetteOnly
	}

	[Serializable]
	private class ListVector3
	{
		public List<Vector3> data;
	}

	private static HashSet<Mesh> registeredMeshes = new HashSet<Mesh>();

	[SerializeField]
	private Mode outlineMode;

	[SerializeField]
	private Color outlineColor = Color.white;

	[SerializeField]
	[Range(0f, 10f)]
	private float outlineWidth = 2f;

	[Header("Optional")]
	[SerializeField]
	[Tooltip("Precompute enabled: Per-vertex calculations are performed in the editor and serialized with the object. Precompute disabled: Per-vertex calculations are performed at runtime in Awake(). This may cause a pause for large meshes.")]
	private bool precomputeOutline;

	[SerializeField]
	[HideInInspector]
	private List<Mesh> bakeKeys = new List<Mesh>();

	[SerializeField]
	[HideInInspector]
	private List<ListVector3> bakeValues = new List<ListVector3>();

	private Renderer[] renderers;

	private Material outlineMaskMaterial;

	private Material outlineFillMaterial;

	private bool needsUpdate;

	public Mode OutlineMode
	{
		get
		{
			return outlineMode;
		}
		set
		{
			outlineMode = value;
			needsUpdate = true;
		}
	}

	public Color OutlineColor
	{
		get
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: 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)
			return outlineColor;
		}
		set
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			outlineColor = value;
			needsUpdate = true;
		}
	}

	public float OutlineWidth
	{
		get
		{
			return outlineWidth;
		}
		set
		{
			outlineWidth = value;
			needsUpdate = true;
		}
	}

	private void Awake()
	{
		renderers = ((Component)this).GetComponentsInChildren<Renderer>();
		outlineMaskMaterial = Object.Instantiate<Material>(global::MineTurtle.MineTurtle.OutlineMask);
		outlineFillMaterial = Object.Instantiate<Material>(global::MineTurtle.MineTurtle.OutlineFill);
		((Object)outlineMaskMaterial).name = "OutlineMask (Instance)";
		((Object)outlineFillMaterial).name = "OutlineFill (Instance)";
		LoadSmoothNormals();
		needsUpdate = true;
	}

	private void OnEnable()
	{
		Renderer[] array = renderers;
		foreach (Renderer val in array)
		{
			List<Material> list = val.sharedMaterials.ToList();
			list.Add(outlineMaskMaterial);
			list.Add(outlineFillMaterial);
			val.materials = list.ToArray();
		}
	}

	private void OnValidate()
	{
		needsUpdate = true;
		if ((!precomputeOutline && bakeKeys.Count != 0) || bakeKeys.Count != bakeValues.Count)
		{
			bakeKeys.Clear();
			bakeValues.Clear();
		}
		if (precomputeOutline && bakeKeys.Count == 0)
		{
			Bake();
		}
	}

	private void Update()
	{
		if (needsUpdate)
		{
			needsUpdate = false;
			UpdateMaterialProperties();
		}
	}

	private void OnDisable()
	{
		Renderer[] array = renderers;
		foreach (Renderer val in array)
		{
			List<Material> list = val.sharedMaterials.ToList();
			list.Remove(outlineMaskMaterial);
			list.Remove(outlineFillMaterial);
			val.materials = list.ToArray();
		}
	}

	private void OnDestroy()
	{
		Object.Destroy((Object)(object)outlineMaskMaterial);
		Object.Destroy((Object)(object)outlineFillMaterial);
	}

	private void Bake()
	{
		HashSet<Mesh> hashSet = new HashSet<Mesh>();
		MeshFilter[] componentsInChildren = ((Component)this).GetComponentsInChildren<MeshFilter>();
		foreach (MeshFilter val in componentsInChildren)
		{
			if (hashSet.Add(val.sharedMesh))
			{
				List<Vector3> data = SmoothNormals(val.sharedMesh);
				bakeKeys.Add(val.sharedMesh);
				bakeValues.Add(new ListVector3
				{
					data = data
				});
			}
		}
	}

	private void LoadSmoothNormals()
	{
		MeshFilter[] componentsInChildren = ((Component)this).GetComponentsInChildren<MeshFilter>();
		foreach (MeshFilter val in componentsInChildren)
		{
			if (registeredMeshes.Add(val.sharedMesh))
			{
				int num = bakeKeys.IndexOf(val.sharedMesh);
				List<Vector3> list = ((num >= 0) ? bakeValues[num].data : SmoothNormals(val.sharedMesh));
				val.sharedMesh.SetUVs(3, list);
				Renderer component = ((Component)val).GetComponent<Renderer>();
				if ((Object)(object)component != (Object)null)
				{
					CombineSubmeshes(val.sharedMesh, component.sharedMaterials);
				}
			}
		}
		SkinnedMeshRenderer[] componentsInChildren2 = ((Component)this).GetComponentsInChildren<SkinnedMeshRenderer>();
		foreach (SkinnedMeshRenderer val2 in componentsInChildren2)
		{
			if (registeredMeshes.Add(val2.sharedMesh))
			{
				val2.sharedMesh.uv4 = (Vector2[])(object)new Vector2[val2.sharedMesh.vertexCount];
				CombineSubmeshes(val2.sharedMesh, ((Renderer)val2).sharedMaterials);
			}
		}
	}

	private List<Vector3> SmoothNormals(Mesh mesh)
	{
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ac: 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_00f6: Unknown result type (might be due to invalid IL or missing references)
		IEnumerable<IGrouping<Vector3, KeyValuePair<Vector3, int>>> enumerable = from pair in mesh.vertices.Select((Vector3 vertex, int index) => new KeyValuePair<Vector3, int>(vertex, index))
			group pair by pair.Key;
		List<Vector3> list = new List<Vector3>(mesh.normals);
		foreach (IGrouping<Vector3, KeyValuePair<Vector3, int>> item in enumerable)
		{
			if (item.Count() == 1)
			{
				continue;
			}
			Vector3 val = Vector3.zero;
			foreach (KeyValuePair<Vector3, int> item2 in item)
			{
				val += list[item2.Value];
			}
			((Vector3)(ref val)).Normalize();
			foreach (KeyValuePair<Vector3, int> item3 in item)
			{
				list[item3.Value] = val;
			}
		}
		return list;
	}

	private void CombineSubmeshes(Mesh mesh, Material[] materials)
	{
		if (mesh.subMeshCount != 1 && mesh.subMeshCount <= materials.Length)
		{
			int subMeshCount = mesh.subMeshCount;
			mesh.subMeshCount = subMeshCount + 1;
			mesh.SetTriangles(mesh.triangles, mesh.subMeshCount - 1);
		}
	}

	private void UpdateMaterialProperties()
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		outlineFillMaterial.SetColor("_OutlineColor", outlineColor);
		switch (outlineMode)
		{
		case Mode.OutlineAll:
			outlineMaskMaterial.SetFloat("_ZTest", 8f);
			outlineFillMaterial.SetFloat("_ZTest", 8f);
			outlineFillMaterial.SetFloat("_OutlineWidth", outlineWidth);
			break;
		case Mode.OutlineVisible:
			outlineMaskMaterial.SetFloat("_ZTest", 8f);
			outlineFillMaterial.SetFloat("_ZTest", 4f);
			outlineFillMaterial.SetFloat("_OutlineWidth", outlineWidth);
			break;
		case Mode.OutlineHidden:
			outlineMaskMaterial.SetFloat("_ZTest", 8f);
			outlineFillMaterial.SetFloat("_ZTest", 5f);
			outlineFillMaterial.SetFloat("_OutlineWidth", outlineWidth);
			break;
		case Mode.OutlineAndSilhouette:
			outlineMaskMaterial.SetFloat("_ZTest", 4f);
			outlineFillMaterial.SetFloat("_ZTest", 8f);
			outlineFillMaterial.SetFloat("_OutlineWidth", outlineWidth);
			break;
		case Mode.SilhouetteOnly:
			outlineMaskMaterial.SetFloat("_ZTest", 4f);
			outlineFillMaterial.SetFloat("_ZTest", 5f);
			outlineFillMaterial.SetFloat("_OutlineWidth", 0f);
			break;
		}
	}
}
namespace MineTurtle
{
	[BepInPlugin("MineTurtle", "MineTurtle", "1.0.0")]
	public class MineTurtle : BaseUnityPlugin
	{
		private static GameObject Mine;

		private static AudioClip Hello;

		private static AudioClip Kaboom;

		private AssetBundle loadedBundle;

		private static Texture MTTexture;

		public static Material OutlineMask;

		public static Material OutlineFill;

		private void Awake()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			Harmony val = new Harmony("MineTurtle");
			val.PatchAll();
			string path = Path.Combine(Paths.PluginPath, "MineTurtle", "mineturtle.mineturtle");
			LoadAssets(path);
		}

		private void LoadAssets(string path)
		{
			loadedBundle = AssetBundle.LoadFromFile(path);
			if ((Object)(object)loadedBundle == (Object)null)
			{
				Debug.LogError((object)"Failed to load AssetBundle!");
				return;
			}
			Mine = loadedBundle.LoadAsset<GameObject>("MineTurtle");
			Hello = loadedBundle.LoadAsset<AudioClip>("Hello");
			Kaboom = loadedBundle.LoadAsset<AudioClip>("Kaboom");
			MTTexture = loadedBundle.LoadAsset<Texture>("MineTurtle");
			OutlineMask = loadedBundle.LoadAsset<Material>("OutlineMask");
			OutlineFill = loadedBundle.LoadAsset<Material>("OutlineFill");
		}

		public static void TurtleIfy(GameObject landmine)
		{
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			((Renderer)landmine.GetComponent<MeshRenderer>()).enabled = false;
			Type typeFromHandle = typeof(Landmine);
			FieldInfo field = typeFromHandle.GetField("activatedBeep", BindingFlags.Instance | BindingFlags.NonPublic);
			if (field != null)
			{
				field.SetValue(landmine.GetComponent<Landmine>(), Hello);
				Debug.Log((object)"Changed beep to hello!");
			}
			GameObject val = Object.Instantiate<GameObject>(Mine, landmine.transform);
			val.transform.localPosition = new Vector3(0f, 1.1f, 0f);
			Transform val2 = landmine.transform.Find("LightCylinder");
			val2.localPosition = new Vector3(0.5f, 1.35f, 0f);
			Outline outline = landmine.AddComponent<Outline>();
			outline.OutlineColor = Color.black;
			outline.OutlineWidth = 10f;
		}
	}
	[HarmonyPatch(typeof(Landmine), "Start")]
	internal class LandMineCreate
	{
		private static void Postfix(Landmine __instance)
		{
			Debug.Log((object)("New Landmine instance created: " + ((Object)__instance).name));
			MineTurtle.TurtleIfy(((Component)__instance).gameObject);
		}
	}
}