Decompiled source of Sunsword v1.0.0

plugins\Sunsword.dll

Decompiled 9 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using LethalLib.Modules;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Sunsword")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Sunsword")]
[assembly: AssemblyTitle("Sunsword")]
[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 Sunsword
{
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("sunsword.secret", "Sunsword", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(GameNetworkManager), "Start")]
		private static class Register
		{
			private static void Prefix()
			{
				//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b3: Expected O, but got Unknown
				if ((Object)(object)Sword != (Object)null)
				{
					return;
				}
				try
				{
					Item val = Resources.FindObjectsOfTypeAll<Item>().First((Item i) => i.itemName == "Shovel" && (Object)(object)i.spawnPrefab != (Object)null);
					Sword = Object.Instantiate<Item>(val);
					((Object)Sword).name = "SecretSunswordItem";
					Sword.itemName = "Sunsword";
					Sword.creditsWorth = 0;
					Sword.weight = 1f;
					Sword.twoHanded = false;
					try
					{
						Sword.itemIcon = MakeIcon();
					}
					catch (Exception e)
					{
						Instance.LogError(e);
					}
					GameObject val2 = new GameObject("SunswordPrefabStorage");
					val2.SetActive(false);
					Object.DontDestroyOnLoad((Object)(object)val2);
					GameObject val3 = Object.Instantiate<GameObject>(val.spawnPrefab, val2.transform);
					((Object)val3).name = "SecretSunswordPrefab";
					Sword.spawnPrefab = val3;
					((GrabbableObject)val3.GetComponent<Shovel>()).itemProperties = Sword;
					RegisterPrefab(val3, 2803696385u);
					try
					{
						val3.AddComponent<SwordAppearance>();
					}
					catch (Exception e2)
					{
						Instance.LogError(e2);
					}
					Items.RegisterShopItem(Sword, (TerminalNode)null, (TerminalNode)null, (TerminalNode)null, 0);
					Tester = Object.Instantiate<Item>(val);
					((Object)Tester).name = "SecretYippeeItem";
					Tester.itemName = "Yippee";
					Tester.creditsWorth = 0;
					GameObject val4 = Object.Instantiate<GameObject>(val.spawnPrefab, val2.transform);
					((Object)val4).name = "SecretYippeePrefab";
					Tester.spawnPrefab = val4;
					((GrabbableObject)val4.GetComponent<Shovel>()).itemProperties = Tester;
					RegisterPrefab(val4, 2803696386u);
					Items.RegisterShopItem(Tester, (TerminalNode)null, (TerminalNode)null, (TerminalNode)null, 0);
				}
				catch (Exception e3)
				{
					Instance.LogError(e3);
					Sword = null;
				}
			}
		}

		[HarmonyPatch(typeof(Terminal), "TextPostProcess")]
		private static class Hide
		{
			private static void Postfix(ref string __result)
			{
				if (__result != null && __result.IndexOf("Shovel", StringComparison.OrdinalIgnoreCase) >= 0 && __result.IndexOf("Flashlight", StringComparison.OrdinalIgnoreCase) >= 0)
				{
					__result = string.Join("\n", from l in __result.Split('\n')
						where l.IndexOf("Sunsword", StringComparison.OrdinalIgnoreCase) < 0 && l.IndexOf("Yippee", StringComparison.OrdinalIgnoreCase) < 0
						select l);
				}
			}
		}

		public class SwordAppearance : MonoBehaviour
		{
			private MeshRenderer target;

			private Material blade;

			private Mesh mesh;

			private Material[] materials;

			private void Start()
			{
				//IL_00a3: 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_00b4: Unknown result type (might be due to invalid IL or missing references)
				//IL_00b9: 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)
				//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
				//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
				//IL_00dc: 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_00ce: 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_0113: Unknown result type (might be due to invalid IL or missing references)
				//IL_0118: Unknown result type (might be due to invalid IL or missing references)
				//IL_011d: 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_00f8: 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_0128: 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_013c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0142: Unknown result type (might be due to invalid IL or missing references)
				//IL_0147: Unknown result type (might be due to invalid IL or missing references)
				//IL_014c: Unknown result type (might be due to invalid IL or missing references)
				//IL_0153: Unknown result type (might be due to invalid IL or missing references)
				//IL_0159: 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_023e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0248: Expected O, but got Unknown
				//IL_02db: Unknown result type (might be due to invalid IL or missing references)
				//IL_02fa: Unknown result type (might be due to invalid IL or missing references)
				//IL_0319: Unknown result type (might be due to invalid IL or missing references)
				try
				{
					if (!IsSword(((Component)this).GetComponent<Shovel>()))
					{
						return;
					}
					MeshFilter[] array = (from f in ((Component)this).GetComponentsInChildren<MeshFilter>(true)
						where (Object)(object)f.sharedMesh != (Object)null && (Object)(object)((Component)f).GetComponent<MeshRenderer>() != (Object)null && (Object)(object)((Renderer)((Component)f).GetComponent<MeshRenderer>()).sharedMaterial != (Object)null
						select f).ToArray();
					if (array.Length == 0)
					{
						throw new InvalidOperationException("No shovel mesh found");
					}
					MeshFilter val = array.OrderByDescending(delegate(MeshFilter f)
					{
						//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_000e: Unknown result type (might be due to invalid IL or missing references)
						//IL_0013: Unknown result type (might be due to invalid IL or missing references)
						Bounds bounds2 = f.sharedMesh.bounds;
						Vector3 size2 = ((Bounds)(ref bounds2)).size;
						return ((Vector3)(ref size2)).sqrMagnitude;
					}).First();
					target = ((Component)val).GetComponent<MeshRenderer>();
					Material source = ((Renderer)target).sharedMaterial;
					Bounds bounds = val.sharedMesh.bounds;
					Vector3 size = ((Bounds)(ref bounds)).size;
					int num = ((!(size.x >= size.y) || !(size.x >= size.z)) ? ((size.y >= size.z) ? 1 : 2) : 0);
					Vector3 along = (Vector3)(num switch
					{
						1 => Vector3.up, 
						0 => Vector3.right, 
						_ => Vector3.forward, 
					});
					along = -along;
					Vector3 across = ((num == 0) ? Vector3.up : Vector3.right);
					Vector3 depth = Vector3.Cross(along, across);
					float length = Mathf.Max(size.x, Mathf.Max(size.y, size.z));
					List<Vector3> verts = new List<Vector3>();
					List<Vector2> uv = new List<Vector2>();
					List<int>[] groups = new List<int>[3]
					{
						new List<int>(),
						new List<int>(),
						new List<int>()
					};
					Box(-0.39f, 0.22f, 0.065f, 0.065f, 0);
					Box(-0.27f, 0.055f, 0.25f, 0.09f, 1);
					Box(-0.51f, 0.045f, 0.1f, 0.09f, 1);
					Box(0.12f, 0.72f, 0.075f, 0.045f, 2);
					Box(0.49f, 0.025f, 0.045f, 0.035f, 2);
					mesh = new Mesh();
					((Object)mesh).name = "SunswordMesh";
					mesh.SetVertices(verts);
					mesh.SetUVs(0, uv);
					mesh.subMeshCount = 3;
					for (int num2 = 0; num2 < 3; num2++)
					{
						mesh.SetTriangles(groups[num2], num2);
					}
					mesh.RecalculateNormals();
					mesh.RecalculateBounds();
					materials = (Material[])(object)new Material[3]
					{
						Make(new Color(0.22f, 0.12f, 0.04f), glow: false),
						Make(new Color(1f, 0.65f, 0.12f), glow: false),
						Make(new Color(1f, 0.88f, 0.22f), glow: true)
					};
					blade = materials[2];
					val.sharedMesh = mesh;
					((Renderer)target).sharedMaterials = materials;
					MeshFilter[] array2 = array;
					foreach (MeshFilter val2 in array2)
					{
						if ((Object)(object)val2 != (Object)(object)val)
						{
							((Renderer)((Component)val2).GetComponent<MeshRenderer>()).forceRenderingOff = true;
						}
					}
					((BaseUnityPlugin)Instance).Logger.LogInfo((object)("Sunsword mesh replaced: " + ((Object)val).name + " shader=" + ((Object)source.shader).name + " axis=" + num + " length=" + length));
					void Box(float y, float h, float w, float d, int group)
					{
						//IL_006b: Unknown result type (might be due to invalid IL or missing references)
						//IL_0079: Unknown result type (might be due to invalid IL or missing references)
						//IL_0080: Unknown result type (might be due to invalid IL or missing references)
						//IL_0087: Unknown result type (might be due to invalid IL or missing references)
						//IL_008d: Unknown result type (might be due to invalid IL or missing references)
						//IL_0092: Unknown result type (might be due to invalid IL or missing references)
						//IL_0099: Unknown result type (might be due to invalid IL or missing references)
						//IL_00a0: 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_00aa: Unknown result type (might be due to invalid IL or missing references)
						//IL_00af: 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)
						int count = verts.Count;
						for (int i = 0; i < 8; i++)
						{
							float num4 = (((i & 1) == 0) ? ((0f - w) / 2f) : (w / 2f));
							float num5 = (((i & 4) == 0) ? ((0f - d) / 2f) : (d / 2f));
							float num6 = y + (((i & 2) == 0) ? ((0f - h) / 2f) : (h / 2f));
							verts.Add(((Bounds)(ref bounds)).center + length * (along * num6 + across * num4 + depth * num5));
							uv.Add(new Vector2((float)(i & 1), (float)((i & 2) / 2)));
						}
						int[] array3 = new int[36]
						{
							0, 2, 1, 1, 2, 3, 4, 5, 6, 5,
							7, 6, 0, 4, 2, 2, 4, 6, 1, 3,
							5, 3, 7, 5, 0, 1, 4, 1, 5, 4,
							2, 6, 3, 3, 6, 7
						};
						for (int j = 0; j < array3.Length; j += 3)
						{
							groups[group].Add(count + array3[j]);
							groups[group].Add(count + array3[j + 2]);
							groups[group].Add(count + array3[j + 1]);
						}
					}
					Material Make(Color color, bool glow)
					{
						//IL_0006: Unknown result type (might be due to invalid IL or missing references)
						//IL_000c: Expected O, but got Unknown
						//IL_009b: 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_00f7: Unknown result type (might be due to invalid IL or missing references)
						Material val3 = new Material(source);
						string[] array3 = new string[7] { "_BaseColorMap", "_BaseMap", "_MainTex", "_MaskMap", "_NormalMap", "_BumpMap", "_EmissiveColorMap" };
						foreach (string text in array3)
						{
							if (val3.HasProperty(text))
							{
								val3.SetTexture(text, (Texture)null);
							}
						}
						array3 = new string[2] { "_BaseColor", "_Color" };
						foreach (string text2 in array3)
						{
							if (val3.HasProperty(text2))
							{
								val3.SetColor(text2, color);
							}
						}
						if (glow)
						{
							val3.EnableKeyword("_EMISSION");
							val3.EnableKeyword("_EMISSIVE_COLOR");
							array3 = new string[2] { "_EmissionColor", "_EmissiveColor" };
							foreach (string text3 in array3)
							{
								if (val3.HasProperty(text3))
								{
									val3.SetColor(text3, color * 4f);
								}
							}
						}
						return val3;
					}
				}
				catch (Exception e)
				{
					Instance.LogError(e);
				}
			}

			private void Update()
			{
				//IL_003b: 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_0046: Unknown result type (might be due to invalid IL or missing references)
				//IL_007e: Unknown result type (might be due to invalid IL or missing references)
				if ((Object)(object)blade == (Object)null)
				{
					return;
				}
				float num = 3.5f + 0.7f * Mathf.Sin(Time.time * 8f);
				Color val = new Color(1f, 0.88f, 0.22f) * num;
				string[] array = new string[2] { "_EmissionColor", "_EmissiveColor" };
				foreach (string text in array)
				{
					if (blade.HasProperty(text))
					{
						blade.SetColor(text, val);
					}
				}
			}

			private void OnDestroy()
			{
				if ((Object)(object)mesh != (Object)null)
				{
					Object.Destroy((Object)(object)mesh);
				}
				if (materials == null)
				{
					return;
				}
				Material[] array = materials;
				foreach (Material val in array)
				{
					if ((Object)(object)val != (Object)null)
					{
						Object.Destroy((Object)(object)val);
					}
				}
			}
		}

		[HarmonyPatch(typeof(Terminal), "LoadNewNode")]
		private static class PurchaseTrace
		{
			private static void Postfix(Terminal __instance, object[] __args)
			{
				try
				{
					TerminalNode val = __args.OfType<TerminalNode>().FirstOrDefault();
					if ((Object)(object)val == (Object)null)
					{
						return;
					}
					IEnumerable enumerable = AccessTools.Field(typeof(Terminal), "orderedItemsFromTerminal")?.GetValue(__instance) as IEnumerable;
					List<string> list = new List<string>();
					if (enumerable != null)
					{
						foreach (object item in enumerable)
						{
							list.Add(item.ToString());
						}
					}
					((BaseUnityPlugin)Instance).Logger.LogInfo((object)("Terminal node: " + ((Object)val).name + "; queued IDs: " + string.Join(",", list)));
				}
				catch (Exception e)
				{
					Instance.LogError(e);
				}
			}
		}

		[HarmonyPatch(typeof(Shovel), "HitShovel")]
		private static class Damage
		{
			private static float lastSpawn = -100f;

			private static bool Prefix(Shovel __instance, ref int __state)
			{
				__state = __instance.shovelHitForce;
				if (IsTester(__instance))
				{
					SpawnBug(__instance);
				}
				if (IsSword(__instance))
				{
					__instance.shovelHitForce = 100000;
				}
				return true;
			}

			private static void SpawnBug(Shovel tool)
			{
				//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
				//IL_013e: Unknown result type (might be due to invalid IL or missing references)
				//IL_0143: Unknown result type (might be due to invalid IL or missing references)
				try
				{
					RoundManager instance = RoundManager.Instance;
					if ((Object)(object)instance == (Object)null || !((NetworkBehaviour)instance).IsServer || Time.time - lastSpawn < 2f)
					{
						return;
					}
					((BaseUnityPlugin)Instance).Logger.LogInfo((object)"Yippee spawner activated");
					EnemyType val = ((IEnumerable<EnemyType>)Resources.FindObjectsOfTypeAll<EnemyType>()).FirstOrDefault((Func<EnemyType, bool>)((EnemyType val4) => val4.enemyName.IndexOf("Hoarding", StringComparison.OrdinalIgnoreCase) >= 0 && (Object)(object)val4.enemyPrefab != (Object)null));
					if ((Object)(object)val == (Object)null)
					{
						((BaseUnityPlugin)Instance).Logger.LogWarning((object)"Hoarding bug type not found");
						return;
					}
					GameObject[] array = GameObject.FindGameObjectsWithTag("OutsideAINode");
					if (array.Length == 0)
					{
						((BaseUnityPlugin)Instance).Logger.LogWarning((object)"Land on a moon before spawning a bug");
						return;
					}
					GameObject val2 = array.OrderBy((GameObject n) => Vector3.Distance(n.transform.position, ((Component)tool).transform.position)).First();
					GameObject val3 = Object.Instantiate<GameObject>(val.enemyPrefab, val2.transform.position, Quaternion.identity);
					NetworkObject component = val3.GetComponent<NetworkObject>();
					if ((Object)(object)component == (Object)null)
					{
						Object.Destroy((Object)(object)val3);
						return;
					}
					component.Spawn(true);
					lastSpawn = Time.time;
					((BaseUnityPlugin)Instance).Logger.LogInfo((object)("Spawned Hoarding Bug at " + ((object)val2.transform.position/*cast due to .constrained prefix*/).ToString()));
				}
				catch (Exception e)
				{
					Instance.LogError(e);
				}
			}

			private static void Postfix(Shovel __instance, int __state)
			{
				__instance.shovelHitForce = __state;
			}
		}

		internal static Plugin Instance;

		internal static Item Sword;

		internal static Item Tester;

		internal static bool IsTester(Shovel s)
		{
			if ((Object)(object)s != (Object)null)
			{
				if (!((Object)s).name.StartsWith("SecretYippeePrefab") && !((Object)(object)((GrabbableObject)s).itemProperties == (Object)(object)Tester))
				{
					return ((GrabbableObject)s).itemProperties?.itemName == "Yippee";
				}
				return true;
			}
			return false;
		}

		private void Awake()
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			Instance = this;
			Harmony val = new Harmony("sunsword.secret");
			Type[] array = new Type[4]
			{
				typeof(Register),
				typeof(Hide),
				typeof(Damage),
				typeof(PurchaseTrace)
			};
			foreach (Type type in array)
			{
				try
				{
					val.CreateClassProcessor(type).Patch();
					((BaseUnityPlugin)this).Logger.LogInfo((object)("Patch installed: " + type.Name));
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)("Patch failed: " + type.Name + " " + ex));
				}
			}
		}

		internal void LogError(Exception e)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)e);
		}

		internal static bool IsSword(Shovel s)
		{
			if ((Object)(object)s != (Object)null)
			{
				if (!((Object)s).name.StartsWith("SecretSunswordPrefab") && !((Object)(object)((GrabbableObject)s).itemProperties == (Object)(object)Sword))
				{
					return ((GrabbableObject)s).itemProperties?.itemName == "Sunsword";
				}
				return true;
			}
			return false;
		}

		private static void RegisterPrefab(GameObject prefab, uint hash)
		{
			NetworkObject component = prefab.GetComponent<NetworkObject>();
			FieldInfo fieldInfo = AccessTools.Field(typeof(NetworkObject), "GlobalObjectIdHash");
			if (fieldInfo == null || (Object)(object)component == (Object)null)
			{
				throw new InvalidOperationException("Cannot assign network prefab hash");
			}
			fieldInfo.SetValue(component, hash);
			NetworkPrefabs.RegisterNetworkPrefab(prefab);
			((BaseUnityPlugin)Instance).Logger.LogInfo((object)$"Registered {((Object)prefab).name} with hash {fieldInfo.GetValue(component)}");
		}

		private static Sprite MakeIcon()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			//IL_0158: 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_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = new Texture2D(64, 64, (TextureFormat)4, false);
			Color32[] array = (Color32[])(object)new Color32[4096];
			Color32 val2 = default(Color32);
			for (int i = 0; i < 64; i++)
			{
				for (int j = 0; j < 64; j++)
				{
					float num = (float)(j + i - 63) * 0.7071f;
					float num2 = (float)(i - j) * 0.7071f;
					((Color32)(ref val2))..ctor((byte)0, (byte)0, (byte)0, (byte)0);
					if (num >= -25f && num <= -9f && Mathf.Abs(num2) < 3f)
					{
						((Color32)(ref val2))..ctor((byte)150, (byte)91, (byte)25, byte.MaxValue);
					}
					if (num >= -11f && num <= -6f && Mathf.Abs(num2) < 11f)
					{
						((Color32)(ref val2))..ctor(byte.MaxValue, (byte)192, (byte)54, byte.MaxValue);
					}
					if (num >= -6f && num <= 29f && Mathf.Abs(num2) < ((num > 23f) ? ((29f - num) * 0.65f) : 4f))
					{
						((Color32)(ref val2))..ctor(byte.MaxValue, (byte)239, (byte)125, byte.MaxValue);
					}
					array[i * 64 + j] = val2;
				}
			}
			val.SetPixels32(array);
			val.Apply();
			((Texture)val).filterMode = (FilterMode)1;
			return Sprite.Create(val, new Rect(0f, 0f, 64f, 64f), new Vector2(0.5f, 0.5f));
		}
	}
}