Decompiled source of SpearBlackmetal v1.0.0

SpearBlackmetal.dll

Decompiled 14 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using Jotunn;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
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(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("SpearBlackmetal")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SpearBlackmetal")]
[assembly: AssemblyTitle("SpearBlackmetal")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace SpearBlackmetal
{
	[BepInPlugin("com.satyr451.spearblackmetal", "SpearBlackmetal", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	internal class ModCore : BaseUnityPlugin
	{
		public const string PluginGUID = "com.satyr451.spearblackmetal";

		public const string PluginName = "SpearBlackmetal";

		public const string PluginVersion = "1.0.0";

		public static CustomLocalization Localization = LocalizationManager.Instance.GetLocalization();

		private void Awake()
		{
			Logger.LogInfo((object)"SpearBlackmetal has landed");
			string[] manifestResourceNames = typeof(ModCore).Assembly.GetManifestResourceNames();
			foreach (string text in manifestResourceNames)
			{
				Logger.LogInfo((object)("Found resource: " + text));
			}
			PrefabManager.OnVanillaPrefabsAvailable += AddClonedItems;
		}

		private void AddClonedItems()
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Expected O, but got Unknown
			//IL_06a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_06af: Expected O, but got Unknown
			//IL_06dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_06e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_06ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_06f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_06ff: Expected O, but got Unknown
			//IL_0701: Unknown result type (might be due to invalid IL or missing references)
			//IL_0706: Unknown result type (might be due to invalid IL or missing references)
			//IL_0712: Unknown result type (might be due to invalid IL or missing references)
			//IL_071a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0723: Expected O, but got Unknown
			//IL_0725: Unknown result type (might be due to invalid IL or missing references)
			//IL_072a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0736: Unknown result type (might be due to invalid IL or missing references)
			//IL_073e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0747: Expected O, but got Unknown
			//IL_074f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0756: Expected O, but got Unknown
			//IL_05d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_05e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_030f: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				Logger.LogInfo((object)"Attempting to add SpearBlackmetalWicked...");
				CustomItem val = new CustomItem("SpearBlackmetalWicked", "SpearBronze");
				ItemDrop itemDrop = val.ItemDrop;
				AssetBundle val2 = null;
				try
				{
					val2 = AssetUtils.LoadAssetBundleFromResources("blackmetalspearbundle", typeof(ModCore).Assembly);
				}
				catch (Exception arg)
				{
					Logger.LogError((object)$"Exception loading bundle: {arg}");
				}
				if ((Object)(object)val2 == (Object)null)
				{
					Logger.LogError((object)"Failed to load asset bundle 'blackmetalspearbundle'. Proceeding with vanilla visuals.");
				}
				else
				{
					GameObject val3 = val2.LoadAsset<GameObject>("SpearBlackmetal");
					if ((Object)(object)val3 == (Object)null)
					{
						GameObject[] array = val2.LoadAllAssets<GameObject>();
						if (array.Length != 0)
						{
							val3 = array[0];
						}
					}
					if ((Object)(object)val3 != (Object)null)
					{
						Logger.LogInfo((object)("Found custom model '" + ((Object)val3).name + "'. Attempting mesh and material swap..."));
						Material val4 = null;
						Material val5 = null;
						GameObject prefab = Cache.GetPrefab<GameObject>("AtgeirBlackmetal");
						if (Object.op_Implicit((Object)(object)prefab))
						{
							MeshRenderer componentInChildren = prefab.GetComponentInChildren<MeshRenderer>(true);
							if (Object.op_Implicit((Object)(object)componentInChildren))
							{
								Material[] sharedMaterials = ((Renderer)componentInChildren).sharedMaterials;
								if (sharedMaterials.Length != 0)
								{
									val4 = sharedMaterials[0];
								}
								if (sharedMaterials.Length > 1)
								{
									val5 = sharedMaterials[1];
									Logger.LogInfo((object)("Found Secondary Atgeir Material: " + ((Object)val5).name));
								}
								Logger.LogInfo((object)$"Atgeir Source Materials Count: {sharedMaterials.Length}");
							}
						}
						else
						{
							Logger.LogWarning((object)"Could not find AtgeirBlackmetal prefab!");
						}
						GameObject prefab2 = Cache.GetPrefab<GameObject>("AxeBlackMetal");
						if (Object.op_Implicit((Object)(object)prefab2))
						{
							MeshRenderer componentInChildren2 = prefab2.GetComponentInChildren<MeshRenderer>(true);
							if (Object.op_Implicit((Object)(object)componentInChildren2))
							{
								Material[] sharedMaterials2 = ((Renderer)componentInChildren2).sharedMaterials;
								if ((Object)(object)val5 == (Object)null && sharedMaterials2.Length > 1)
								{
									val5 = sharedMaterials2[1];
								}
							}
						}
						Material val6 = null;
						GameObject prefab3 = Cache.GetPrefab<GameObject>("SpearBronze");
						if (Object.op_Implicit((Object)(object)prefab3))
						{
							MeshRenderer componentInChildren3 = prefab3.GetComponentInChildren<MeshRenderer>(true);
							if (Object.op_Implicit((Object)(object)componentInChildren3))
							{
								val6 = ((Renderer)componentInChildren3).sharedMaterial;
							}
							Logger.LogInfo((object)("Found Bronze Spear Material: " + (Object.op_Implicit((Object)(object)val6) ? ((Object)val6).name : "null")));
						}
						else
						{
							Logger.LogWarning((object)"Could not find SpearBronze prefab!");
						}
						MeshFilter[] componentsInChildren = val.ItemPrefab.GetComponentsInChildren<MeshFilter>(true);
						MeshFilter[] componentsInChildren2 = val3.GetComponentsInChildren<MeshFilter>(true);
						if (componentsInChildren.Length != 0 && componentsInChildren2.Length != 0)
						{
							Logger.LogInfo((object)("Swapping mesh on " + ((Object)componentsInChildren[0]).name + " with " + ((Object)componentsInChildren2[0]).name));
							componentsInChildren[0].sharedMesh = componentsInChildren2[0].sharedMesh;
							((Component)componentsInChildren[0]).transform.localPosition = ((Component)componentsInChildren2[0]).transform.localPosition;
							((Component)componentsInChildren[0]).transform.localRotation = ((Component)componentsInChildren2[0]).transform.localRotation;
							((Component)componentsInChildren[0]).transform.localScale = ((Component)componentsInChildren2[0]).transform.localScale;
							MeshRenderer componentInChildren4 = val.ItemPrefab.GetComponentInChildren<MeshRenderer>(true);
							MeshRenderer componentInChildren5 = val3.GetComponentInChildren<MeshRenderer>(true);
							if ((Object)(object)componentInChildren4 != (Object)null && (Object)(object)componentInChildren5 != (Object)null)
							{
								Material[] sharedMaterials3 = ((Renderer)componentInChildren5).sharedMaterials;
								Material[] array2 = (Material[])(object)new Material[sharedMaterials3.Length];
								for (int i = 0; i < sharedMaterials3.Length; i++)
								{
									string name = ((Object)sharedMaterials3[i]).name;
									if ((name.Contains("AtgeirBlackmetal") || name.Contains("BladeMaterial")) && (Object)(object)val4 != (Object)null)
									{
										array2[i] = val4;
										Logger.LogInfo((object)$"Replaced material {i} ({name}) with Real Blackmetal");
									}
									else if (name.Contains("SpearBronzeHaft") || name.Contains("HaftMaterial"))
									{
										if ((Object)(object)val5 != (Object)null)
										{
											array2[i] = val5;
											Logger.LogInfo((object)$"Replaced material {i} ({name}) with Real Blackmetal HAFT (Mat 2 found!)");
										}
										else if ((Object)(object)val6 != (Object)null)
										{
											array2[i] = val6;
											Logger.LogInfo((object)$"Replaced material {i} ({name}) with Bronze Spear Haft (Wood Mix)");
										}
										else if ((Object)(object)val4 != (Object)null)
										{
											array2[i] = val4;
											Logger.LogInfo((object)$"Replaced material {i} ({name}) with Real Blackmetal (Primary) - No secondary found.");
										}
									}
									else
									{
										array2[i] = sharedMaterials3[i];
										Logger.LogWarning((object)$"Unknown material {name} at index {i}. Keeping original.");
									}
								}
								LogMeshDiagnostics(componentsInChildren2[0].sharedMesh);
								((Renderer)componentInChildren4).sharedMaterials = array2;
								FixCollider(val.ItemPrefab, (Renderer)(object)componentInChildren4);
							}
						}
						else
						{
							Logger.LogWarning((object)"Could not find MeshFilters to swap.");
						}
					}
					else
					{
						Logger.LogError((object)"Could not find any GameObject in the asset bundle.");
					}
				}
				itemDrop.m_itemData.m_shared.m_name = "Blackmetal Spear";
				itemDrop.m_itemData.m_shared.m_description = "A darker, sharper spear forged of wicked blackmetal.";
				Sprite val7 = val2.LoadAsset<Sprite>("BlackmetalSpear_Icon.png");
				if ((Object)(object)val7 == (Object)null)
				{
					val7 = val2.LoadAsset<Sprite>("BlackmetalSpear_Icon");
				}
				if ((Object)(object)val7 == (Object)null)
				{
					Texture2D val8 = val2.LoadAsset<Texture2D>("BlackmetalSpear_Icon.png");
					if ((Object)(object)val8 == (Object)null)
					{
						val8 = val2.LoadAsset<Texture2D>("BlackmetalSpear_Icon");
					}
					if ((Object)(object)val8 != (Object)null)
					{
						Logger.LogInfo((object)"Found icon as Texture2D. Converting to Sprite...");
						val7 = Sprite.Create(val8, new Rect(0f, 0f, (float)((Texture)val8).width, (float)((Texture)val8).height), new Vector2(0.5f, 0.5f));
					}
				}
				if ((Object)(object)val7 != (Object)null)
				{
					itemDrop.m_itemData.m_shared.m_icons = (Sprite[])(object)new Sprite[1] { val7 };
					Logger.LogInfo((object)"Successfully assigned custom icon.");
				}
				else
				{
					Logger.LogWarning((object)"Failed to load sprite OR texture 'BlackmetalSpear_Icon' from bundle. PLEASE COPY THE 'BUNDLE CONTENTS' LIST FROM ABOVE AND SHARE IT!");
				}
				itemDrop.m_itemData.m_shared.m_damages.m_pierce = 95f;
				itemDrop.m_itemData.m_shared.m_damagesPerLevel.m_pierce = 5f;
				itemDrop.m_itemData.m_shared.m_maxDurability = 250f;
				itemDrop.m_itemData.m_shared.m_durabilityPerLevel = 50f;
				itemDrop.m_itemData.m_shared.m_damages.m_slash = 0f;
				RecipeConfig val9 = new RecipeConfig();
				val9.Item = "SpearBlackmetalWicked";
				val9.CraftingStation = "forge";
				val9.MinStationLevel = 4;
				val9.Requirements = (RequirementConfig[])(object)new RequirementConfig[3]
				{
					new RequirementConfig
					{
						Item = "FineWood",
						Amount = 10,
						AmountPerLevel = 0
					},
					new RequirementConfig
					{
						Item = "BlackMetal",
						Amount = 6,
						AmountPerLevel = 3
					},
					new RequirementConfig
					{
						Item = "LinenThread",
						Amount = 5,
						AmountPerLevel = 3
					}
				};
				CustomRecipe val10 = new CustomRecipe(val9);
				ItemManager.Instance.AddItem(val);
				ItemManager.Instance.AddRecipe(val10);
				Logger.LogInfo((object)"Successfully added SpearBlackmetalWicked");
			}
			catch (Exception arg2)
			{
				Logger.LogError((object)$"CRITICAL ERROR adding custom item: {arg2}");
			}
			finally
			{
				PrefabManager.OnVanillaPrefabsAvailable -= AddClonedItems;
			}
		}

		private void LogMeshDiagnostics(Mesh mesh)
		{
			//IL_00f5: 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_0108: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: 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_0163: 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_016b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0189: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)mesh == (Object)null)
			{
				Logger.LogError((object)"Mesh is NULL!");
				return;
			}
			Logger.LogInfo((object)("--- Mesh Diagnostics: " + ((Object)mesh).name + " ---"));
			Logger.LogInfo((object)$"Vertex Count: {mesh.vertexCount}");
			Logger.LogInfo((object)$"Has Normals: {mesh.normals.Length != 0}");
			Logger.LogInfo((object)$"Has Tangents: {mesh.tangents.Length != 0}");
			Logger.LogInfo((object)$"Has Vertex Colors: {mesh.colors.Length != 0} (Count: {mesh.colors.Length})");
			Logger.LogInfo((object)$"Has UVs: {mesh.uv.Length != 0}");
			if (mesh.colors.Length != 0)
			{
				Color val = mesh.colors[0];
				Logger.LogInfo((object)$"Sample Vertex Color [0]: R={val.r:F2}, G={val.g:F2}, B={val.b:F2}, A={val.a:F2}");
				bool flag = false;
				bool flag2 = false;
				int num = 0;
				Color[] colors = mesh.colors;
				foreach (Color val2 in colors)
				{
					if (val2.g > 0.1f)
					{
						flag = true;
						num++;
					}
					if (val2.r > 0.1f)
					{
						flag2 = true;
					}
				}
				Logger.LogInfo((object)$"Color Scan: Found Green? {flag} ({num} verts). Found Red? {flag2}.");
			}
			else
			{
				Logger.LogWarning((object)"!!! MESH HAS NO VERTEX COLORS - THIS CAUSES SHINY TEXTURES !!!");
			}
			Logger.LogInfo((object)"-------------------------------------");
		}

		private void FixCollider(GameObject prefab, Renderer meshRenderer)
		{
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: 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_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b4: 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)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_010d: 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)
			//IL_0116: 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)
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: 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_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0188: Unknown result type (might be due to invalid IL or missing references)
			Collider[] componentsInChildren = prefab.GetComponentsInChildren<Collider>();
			Collider[] array = componentsInChildren;
			foreach (Collider val in array)
			{
				Logger.LogInfo((object)("Destroying old collider: " + ((object)val).GetType().Name + " on " + ((Object)((Component)val).gameObject).name));
				Object.DestroyImmediate((Object)(object)val);
			}
			BoxCollider val2 = prefab.AddComponent<BoxCollider>();
			SkinnedMeshRenderer val3 = (SkinnedMeshRenderer)(object)((meshRenderer is SkinnedMeshRenderer) ? meshRenderer : null);
			Bounds val4;
			if (val3 != null)
			{
				val4 = ((Renderer)val3).localBounds;
				val2.center = ((Bounds)(ref val4)).center;
				val4 = ((Renderer)val3).localBounds;
				val2.size = ((Bounds)(ref val4)).size;
			}
			else
			{
				MeshRenderer val5 = (MeshRenderer)(object)((meshRenderer is MeshRenderer) ? meshRenderer : null);
				if (val5 != null)
				{
					MeshFilter component = ((Component)meshRenderer).GetComponent<MeshFilter>();
					if (Object.op_Implicit((Object)(object)component) && Object.op_Implicit((Object)(object)component.sharedMesh))
					{
						val4 = component.sharedMesh.bounds;
						val2.center = ((Bounds)(ref val4)).center;
						val4 = component.sharedMesh.bounds;
						val2.size = ((Bounds)(ref val4)).size;
						Object.DestroyImmediate((Object)(object)val2);
						BoxCollider val6 = ((Component)meshRenderer).gameObject.AddComponent<BoxCollider>();
						val4 = component.sharedMesh.bounds;
						val6.center = ((Bounds)(ref val4)).center;
						val4 = component.sharedMesh.bounds;
						val6.size = ((Bounds)(ref val4)).size;
						Logger.LogInfo((object)$"Added BoxCollider to {((Object)meshRenderer).name}: Center={val6.center}, Size={val6.size}");
					}
				}
			}
			Rigidbody component2 = prefab.GetComponent<Rigidbody>();
			if (Object.op_Implicit((Object)(object)component2))
			{
				component2.centerOfMass = Vector3.zero;
				component2.collisionDetectionMode = (CollisionDetectionMode)0;
			}
		}
	}
}