Decompiled source of BlackMetalBuildables v1.1.0

plugins\BlackMetalBuildables.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
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.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("BlackMetalBuildables")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+0a23f51b344ee1b7083ed2c7ed86168c173e0fab")]
[assembly: AssemblyProduct("BlackMetalBuildables")]
[assembly: AssemblyTitle("BlackMetalBuildables")]
[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;
		}
	}
}
public class BlackMetalCages
{
	public static void RegisterPieces()
	{
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Expected O, but got Unknown
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Expected O, but got Unknown
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: Expected O, but got Unknown
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b5: Expected O, but got Unknown
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00da: Expected O, but got Unknown
		PrefabManager.OnVanillaPrefabsAvailable -= RegisterPieces;
		AddPiece(new CustomPiece("blackmetal_floor_1x1", "iron_floor_1x1_v2", CreateBlackMetalPieceConfig("Black Metal Cage Floor 1x1", "A sturdy cage floor forged from black metal.", 1)));
		AddPiece(new CustomPiece("blackmetal_floor_2x2", "iron_floor_2x2", CreateBlackMetalPieceConfig("Black Metal Cage Floor", "A sturdy cage floor forged from black metal.", 2)));
		AddPiece(new CustomPiece("blackmetal_gate", "iron_grate", CreateBlackMetalPieceConfig("Black Metal Gate", "A sturdy gate forged from black metal.", 2)));
		AddPiece(new CustomPiece("blackmetal_wall_1x1", "iron_wall_1x1", CreateBlackMetalPieceConfig("Black Metal Cage Wall 1x1", "A sturdy cage wall forged from black metal.", 1)));
		AddPiece(new CustomPiece("blackmetal_wall_2x2", "iron_wall_2x2", CreateBlackMetalPieceConfig("Black Metal Cage Wall 2x2", "A sturdy cage wall forged from black metal.", 2)));
	}

	private static PieceConfig CreateBlackMetalPieceConfig(string name, string description, int blackMetalCost)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: 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_0016: 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_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Expected O, but got Unknown
		PieceConfig val = new PieceConfig
		{
			Name = name,
			Description = description,
			PieceTable = "Hammer",
			Category = "HeavyBuild",
			CraftingStation = "Forge"
		};
		val.AddRequirement("BlackMetal", blackMetalCost, true);
		return val;
	}

	private static void AddPiece(CustomPiece piece)
	{
		ApplyBlackMetalLook(piece.PiecePrefab);
		PieceManager.Instance.AddPiece(piece);
	}

	private static void ApplyBlackMetalLook(GameObject prefab)
	{
		Renderer[] componentsInChildren = prefab.GetComponentsInChildren<Renderer>(true);
		foreach (Renderer val in componentsInChildren)
		{
			Material[] sharedMaterials = val.sharedMaterials;
			for (int j = 0; j < sharedMaterials.Length; j++)
			{
				if (!((Object)(object)sharedMaterials[j] == (Object)null))
				{
					Material val2 = Object.Instantiate<Material>(sharedMaterials[j]);
					((Object)val2).name = ((Object)sharedMaterials[j]).name + "_BMB_BlackMetal";
					TryTintMaterial(val2);
					sharedMaterials[j] = val2;
				}
			}
			val.sharedMaterials = sharedMaterials;
		}
	}

	private static void TryTintMaterial(Material mat)
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: 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_0043: Unknown result type (might be due to invalid IL or missing references)
		if (mat.HasProperty("_Color"))
		{
			Color color = mat.color;
			mat.color = new Color(color.r * 0.4f, color.g * 0.7f, color.b * 0.55f, color.a);
		}
	}
}
public class BlackMetalCore
{
	private enum BlackCorePieceShape
	{
		Pole2m,
		Pole4m,
		Beam2m,
		Beam4m
	}

	private readonly struct BlackCorePieceDimensions
	{
		public Vector3 TopSnap { get; }

		public Vector3 BottomSnap { get; }

		public Vector3 ColliderCenter { get; }

		public float ColliderRadius { get; }

		public float ColliderHeight { get; }

		public int ColliderDirection { get; }

		public BlackCorePieceDimensions(Vector3 topSnap, Vector3 bottomSnap, Vector3 colliderCenter, float colliderRadius, float colliderHeight, int colliderDirection)
		{
			//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)
			//IL_0009: 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_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			TopSnap = topSnap;
			BottomSnap = bottomSnap;
			ColliderCenter = colliderCenter;
			ColliderRadius = colliderRadius;
			ColliderHeight = colliderHeight;
			ColliderDirection = colliderDirection;
		}
	}

	private readonly struct BlackCorePieceDefinition
	{
		public string PrefabName { get; }

		public string VisualAssetName { get; }

		public string Name { get; }

		public string Description { get; }

		public int CoreWoodCost { get; }

		public int BlackMetalCost { get; }

		public BlackCorePieceShape Shape { get; }

		public BlackCorePieceDefinition(string prefabName, string visualAssetName, string name, string description, int coreWoodCost, int blackMetalCost, BlackCorePieceShape shape)
		{
			PrefabName = prefabName;
			VisualAssetName = visualAssetName;
			Name = name;
			Description = description;
			CoreWoodCost = coreWoodCost;
			BlackMetalCost = blackMetalCost;
			Shape = shape;
		}
	}

	private static readonly bool EnableDebugLogging = false;

	private const string BundleName = "blackcore";

	private const string HammerPieceTable = "Hammer";

	private const string HeavyBuildCategory = "HeavyBuild";

	private const string ForgeStation = "Forge";

	private const string SnapPointTag = "snappoint";

	private const string WoodRoleMarker = "Wood_";

	private const string MetalRoleMarker = "Metal_";

	private static readonly string[] EffectsSourcePrefabs = new string[3] { "woodiron_pole", "woodiron_beam", "wood_pole_log" };

	private const string MetalSourcePrefab = "iron_wall_1x1";

	private const string BlackCorePole2mPrefab = "bmb_black_core_pole_2m";

	private const string BlackCorePole4mPrefab = "bmb_black_core_pole_4m";

	private const string BlackCoreBeam2mPrefab = "bmb_black_core_beam_2m";

	private const string BlackCoreBeam4mPrefab = "bmb_black_core_beam_4m";

	private const string BlackCorePole2mVisualAsset = "assets/blackcore/prefabs/blackmetalcorepole2m.prefab";

	private const string BlackCorePole4mVisualAsset = "assets/blackcore/prefabs/blackmetalcorepole4m.prefab";

	private const string BlackCoreBeam2mVisualAsset = "assets/blackcore/prefabs/blackmetalcorebeam2m.prefab";

	private const string BlackCoreBeam4mVisualAsset = "assets/blackcore/prefabs/blackmetalcorebeam4m.prefab";

	private const string DarkCoreWoodMaterialAsset = "assets/blackcore/materials/bmb_darkcorewood.mat";

	private static AssetBundle assetBundle;

	public static void RegisterPieces()
	{
		PrefabManager.OnVanillaPrefabsAvailable -= RegisterPieces;
		RegisterPiece(new BlackCorePieceDefinition("bmb_black_core_pole_2m", "assets/blackcore/prefabs/blackmetalcorepole2m.prefab", "Black Core Pole 2m", "Core wood reinforced with black metal bands.", 1, 1, BlackCorePieceShape.Pole2m));
		RegisterPiece(new BlackCorePieceDefinition("bmb_black_core_pole_4m", "assets/blackcore/prefabs/blackmetalcorepole4m.prefab", "Black Core Pole 4m", "Core wood reinforced with black metal bands.", 2, 2, BlackCorePieceShape.Pole4m));
		RegisterPiece(new BlackCorePieceDefinition("bmb_black_core_beam_2m", "assets/blackcore/prefabs/blackmetalcorebeam2m.prefab", "Black Core Beam 2m", "Core wood reinforced with black metal bands.", 1, 1, BlackCorePieceShape.Beam2m));
		RegisterPiece(new BlackCorePieceDefinition("bmb_black_core_beam_4m", "assets/blackcore/prefabs/blackmetalcorebeam4m.prefab", "Black Core Beam 4m", "Core wood reinforced with black metal bands.", 2, 2, BlackCorePieceShape.Beam4m));
	}

	private static void RegisterPiece(BlackCorePieceDefinition definition)
	{
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Expected O, but got Unknown
		if (!TryGetDimensions(definition.Shape, out var dimensions))
		{
			Logger.LogError((object)$"Black Core shape '{definition.Shape}' is not configured. Skipping '{definition.PrefabName}'.");
			return;
		}
		try
		{
			CustomPiece val = new CustomPiece(definition.PrefabName, true, CreateBlackCorePieceConfig(definition));
			ConfigureStandalonePiece(val.PiecePrefab, dimensions);
			TuneSupport(val.PiecePrefab);
			ApplyBuildEffects(val.PiecePrefab);
			LogSnapPointsIfDebug(val.PiecePrefab, "before visual replacement");
			ApplyBundledVisual(val.PiecePrefab, definition.VisualAssetName);
			LogSnapPointsIfDebug(val.PiecePrefab, "after visual replacement");
			ApplyIcon(val);
			if (PieceManager.Instance.AddPiece(val))
			{
				Logger.LogInfo((object)("Registered Black Core piece '" + definition.Name + "'."));
			}
			else
			{
				Logger.LogError((object)("Jotunn rejected Black Core piece '" + definition.PrefabName + "'."));
			}
		}
		catch (Exception arg)
		{
			Logger.LogError((object)$"Failed to register Black Core piece '{definition.PrefabName}': {arg}");
		}
	}

	private static void ApplyBuildEffects(GameObject prefab)
	{
		GameObject effectsSourcePrefab = GetEffectsSourcePrefab();
		if ((Object)(object)effectsSourcePrefab == (Object)null)
		{
			Logger.LogWarning((object)"Could not find any source prefab for Black Core effects.");
			return;
		}
		CopyPlaceEffect(effectsSourcePrefab, prefab);
		CopyWearNTearEffects(effectsSourcePrefab, prefab);
	}

	private static GameObject GetEffectsSourcePrefab()
	{
		string[] effectsSourcePrefabs = EffectsSourcePrefabs;
		foreach (string text in effectsSourcePrefabs)
		{
			GameObject prefab = PrefabManager.Instance.GetPrefab(text);
			if ((Object)(object)prefab != (Object)null)
			{
				return prefab;
			}
		}
		return null;
	}

	private static void CopyPlaceEffect(GameObject sourcePrefab, GameObject targetPrefab)
	{
		Piece component = sourcePrefab.GetComponent<Piece>();
		Piece component2 = targetPrefab.GetComponent<Piece>();
		if ((Object)(object)component == (Object)null || (Object)(object)component2 == (Object)null)
		{
			Logger.LogWarning((object)("Could not copy Black Core place effect from '" + ((Object)sourcePrefab).name + "' to '" + ((Object)targetPrefab).name + "'. Missing Piece component."));
		}
		else
		{
			component2.m_placeEffect = component.m_placeEffect;
		}
	}

	private static void CopyWearNTearEffects(GameObject sourcePrefab, GameObject targetPrefab)
	{
		WearNTear component = sourcePrefab.GetComponent<WearNTear>();
		WearNTear component2 = targetPrefab.GetComponent<WearNTear>();
		if ((Object)(object)component == (Object)null || (Object)(object)component2 == (Object)null)
		{
			Logger.LogWarning((object)("Could not copy Black Core hit/destroy effects from '" + ((Object)sourcePrefab).name + "' to '" + ((Object)targetPrefab).name + "'. Missing WearNTear component."));
		}
		else
		{
			component2.m_hitEffect = component.m_hitEffect;
			component2.m_destroyedEffect = component.m_destroyedEffect;
		}
	}

	private static void ApplyIcon(CustomPiece customPiece)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		Sprite val = RenderManager.Instance.Render(customPiece.PiecePrefab, RenderManager.IsometricRotation);
		if ((Object)(object)val == (Object)null)
		{
			Logger.LogWarning((object)("Failed to render icon for Black Core piece '" + ((Object)customPiece.PiecePrefab).name + "'."));
		}
		else
		{
			customPiece.Piece.m_icon = val;
		}
	}

	private static PieceConfig CreateBlackCorePieceConfig(BlackCorePieceDefinition definition)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0014: 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_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Expected O, but got Unknown
		PieceConfig val = new PieceConfig
		{
			Name = definition.Name,
			Description = definition.Description,
			PieceTable = "Hammer",
			Category = "HeavyBuild",
			CraftingStation = "Forge"
		};
		val.AddRequirement("RoundLog", definition.CoreWoodCost, true);
		val.AddRequirement("BlackMetal", definition.BlackMetalCost, true);
		return val;
	}

	private static void TuneSupport(GameObject prefab)
	{
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		WearNTear val = prefab.GetComponent<WearNTear>() ?? prefab.AddComponent<WearNTear>();
		if ((Object)(object)val == (Object)null)
		{
			Logger.LogWarning((object)("Black Core prefab '" + ((Object)prefab).name + "' has no WearNTear component. Support tuning was skipped."));
			return;
		}
		val.m_materialType = (MaterialType)2;
		val.m_supports = true;
		if (val.m_health <= 0f)
		{
			val.m_health = 1000f;
		}
	}

	private static void ConfigureStandalonePiece(GameObject prefab, BlackCorePieceDimensions dimensions)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		AddSnapPoint(prefab.transform, "$hud_snappoint_top", dimensions.TopSnap);
		AddSnapPoint(prefab.transform, "$hud_snappoint_bottom", dimensions.BottomSnap);
		EnsureStandaloneCollider(prefab, dimensions);
		DisablePlaceholderRenderers(prefab);
	}

	private static void AddSnapPoint(Transform parent, string name, Vector3 localPosition)
	{
		//IL_0060: Expected O, but got Unknown
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		Transform val = parent.Find(name);
		Transform val2 = (((Object)(object)val != (Object)null) ? val : new GameObject(name).transform);
		val2.SetParent(parent, false);
		val2.localPosition = localPosition;
		val2.localRotation = Quaternion.identity;
		val2.localScale = Vector3.one;
		try
		{
			((Component)val2).gameObject.tag = "snappoint";
		}
		catch (UnityException ex)
		{
			UnityException ex2 = ex;
			Logger.LogWarning((object)("Could not tag Black Core snap point '" + name + "' as 'snappoint': " + ((Exception)(object)ex2).Message));
		}
	}

	private static void EnsureStandaloneCollider(GameObject prefab, BlackCorePieceDimensions dimensions)
	{
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		Collider[] components = prefab.GetComponents<Collider>();
		foreach (Collider val in components)
		{
			val.enabled = false;
		}
		CapsuleCollider val2 = prefab.GetComponent<CapsuleCollider>() ?? prefab.AddComponent<CapsuleCollider>();
		val2.radius = dimensions.ColliderRadius;
		val2.height = dimensions.ColliderHeight;
		val2.direction = dimensions.ColliderDirection;
		val2.center = dimensions.ColliderCenter;
		((Collider)val2).enabled = true;
	}

	private static bool TryGetDimensions(BlackCorePieceShape shape, out BlackCorePieceDimensions dimensions)
	{
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: 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_00c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d8: 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_010b: Unknown result type (might be due to invalid IL or missing references)
		//IL_011f: 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)
		switch (shape)
		{
		case BlackCorePieceShape.Pole2m:
			dimensions = new BlackCorePieceDimensions(new Vector3(0f, 1f, 0f), new Vector3(0f, -1f, 0f), Vector3.zero, 0.3f, 2f, 1);
			return true;
		case BlackCorePieceShape.Pole4m:
			dimensions = new BlackCorePieceDimensions(new Vector3(0f, 2f, 0f), new Vector3(0f, -2f, 0f), Vector3.zero, 0.3f, 4f, 1);
			return true;
		case BlackCorePieceShape.Beam2m:
			dimensions = new BlackCorePieceDimensions(new Vector3(1f, 0f, 0f), new Vector3(-1f, 0f, 0f), Vector3.zero, 0.3f, 2f, 0);
			return true;
		case BlackCorePieceShape.Beam4m:
			dimensions = new BlackCorePieceDimensions(new Vector3(2f, 0f, 0f), new Vector3(-2f, 0f, 0f), Vector3.zero, 0.3f, 4f, 0);
			return true;
		default:
			dimensions = default(BlackCorePieceDimensions);
			return false;
		}
	}

	private static void DisablePlaceholderRenderers(GameObject prefab)
	{
		Renderer[] componentsInChildren = prefab.GetComponentsInChildren<Renderer>(true);
		foreach (Renderer val in componentsInChildren)
		{
			val.enabled = false;
		}
	}

	private static void LogSnapPointsIfDebug(GameObject prefab, string phase)
	{
		//IL_00fb: 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_0129: Unknown result type (might be due to invalid IL or missing references)
		if (!EnableDebugLogging)
		{
			return;
		}
		Piece component = prefab.GetComponent<Piece>();
		if ((Object)(object)component == (Object)null)
		{
			Logger.LogWarning((object)("Black Core prefab '" + ((Object)prefab).name + "' has no Piece component while logging snap points (" + phase + ")."));
			return;
		}
		List<Transform> list = new List<Transform>();
		component.GetSnapPoints(list);
		Logger.LogInfo((object)$"Black Core prefab '{((Object)prefab).name}' has {list.Count} snap points {phase}.");
		for (int i = 0; i < list.Count; i++)
		{
			Transform val = list[i];
			if ((Object)(object)val == (Object)null)
			{
				Logger.LogInfo((object)$"Black Core snap point {i} is null {phase}.");
				continue;
			}
			Logger.LogInfo((object)($"Black Core snap point {i} {phase}: path '{GetTransformPath(val)}', " + "localPosition " + FormatVector(val.localPosition) + ", localRotation " + FormatVector(val.localEulerAngles) + ", localScale " + FormatVector(val.localScale) + "."));
		}
	}

	private static void ApplyBundledVisual(GameObject prefab, string assetName)
	{
		//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
		AssetBundle val = GetAssetBundle();
		if ((Object)(object)val == (Object)null)
		{
			Logger.LogError((object)("Failed to load Black Core AssetBundle 'blackcore' from embedded resources. Leaving placeholder visuals for '" + ((Object)prefab).name + "'."));
			return;
		}
		LogBundleAssetsIfDebug(val);
		GameObject val2 = val.LoadAsset<GameObject>(assetName);
		if ((Object)(object)val2 == (Object)null)
		{
			Logger.LogError((object)("Failed to load Black Core visual prefab '" + assetName + "' from AssetBundle 'blackcore'. Leaving placeholder visuals for '" + ((Object)prefab).name + "'."));
			return;
		}
		Logger.LogInfo((object)("Loaded Black Core visual prefab '" + assetName + "' from AssetBundle 'blackcore'."));
		DisablePlaceholderRenderers(prefab);
		GameObject val3 = Object.Instantiate<GameObject>(val2, prefab.transform, false);
		((Object)val3).name = assetName;
		val3.transform.localPosition = Vector3.zero;
		val3.transform.localRotation = Quaternion.identity;
		val3.transform.localScale = Vector3.one;
		LogVisualHierarchyIfDebug(val3);
		DisableVisualColliders(val3);
		ApplyBundledVisualMaterials(val3, val);
		Logger.LogInfo((object)("Applied bundled Black Core visual '" + assetName + "' to '" + ((Object)prefab).name + "'."));
	}

	private static void DisableVisualColliders(GameObject visual)
	{
		Collider[] componentsInChildren = visual.GetComponentsInChildren<Collider>(true);
		foreach (Collider val in componentsInChildren)
		{
			val.enabled = false;
		}
	}

	private static AssetBundle GetAssetBundle()
	{
		if ((Object)(object)assetBundle != (Object)null)
		{
			return assetBundle;
		}
		assetBundle = AssetUtils.LoadAssetBundleFromResources("blackcore", Assembly.GetExecutingAssembly());
		if ((Object)(object)assetBundle != (Object)null)
		{
			Logger.LogInfo((object)"Loaded Black Core AssetBundle 'blackcore'.");
		}
		return assetBundle;
	}

	private static void LogBundleAssetsIfDebug(AssetBundle bundle)
	{
		if (EnableDebugLogging)
		{
			string[] allAssetNames = bundle.GetAllAssetNames();
			foreach (string text in allAssetNames)
			{
				Logger.LogInfo((object)("Black Core AssetBundle contains asset '" + text + "'."));
			}
		}
	}

	private static void LogVisualHierarchyIfDebug(GameObject visual)
	{
		if (EnableDebugLogging)
		{
			Transform[] componentsInChildren = visual.GetComponentsInChildren<Transform>(true);
			foreach (Transform transform in componentsInChildren)
			{
				Logger.LogInfo((object)("Black Core visual hierarchy node '" + GetTransformPath(transform) + "'."));
			}
		}
	}

	private static void ApplyBundledVisualMaterials(GameObject visual, AssetBundle bundle)
	{
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: 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_00aa: Unknown result type (might be due to invalid IL or missing references)
		Material val = CreateTexturedWoodMaterial("BMB_BlackCore_Wood", bundle, "assets/blackcore/materials/bmb_darkcorewood.mat", new Color(0.2f, 0.1f, 0.03f, 1f));
		Material val2 = TryCreateClonedMaterialFromPrefab("iron_wall_1x1", "BMB_BlackCore_Metal", new Color(0.35f, 0.65f, 0.5f, 1f));
		if ((Object)(object)val == (Object)null)
		{
			val = CreateDiffuseMaterial("BMB_Fallback_BlackCore_Wood", new Color(0.2f, 0.1f, 0.03f, 1f));
		}
		if ((Object)(object)val2 == (Object)null)
		{
			val2 = CreateDiffuseMaterial("BMB_Fallback_BlackCore_Metal", new Color(0.08f, 0.16f, 0.13f, 1f));
		}
		Renderer[] componentsInChildren = visual.GetComponentsInChildren<Renderer>(true);
		foreach (Renderer val3 in componentsInChildren)
		{
			string transformPath = GetTransformPath(((Component)val3).transform);
			string text = ClassifyRenderer(transformPath);
			LogRendererIfDebug(val3, transformPath, text);
			if (text == "Wood")
			{
				AssignMaterial(val3, val, transformPath);
			}
			else if (text == "Metal")
			{
				AssignMaterial(val3, val2, transformPath);
			}
			else
			{
				Logger.LogWarning((object)("Black Core renderer '" + transformPath + "' has no Wood_/Metal_ role marker. Leaving bundled material unchanged."));
			}
		}
	}

	private static string ClassifyRenderer(string path)
	{
		if (path.Contains("Wood_"))
		{
			return "Wood";
		}
		if (path.Contains("Metal_"))
		{
			return "Metal";
		}
		return "Unknown";
	}

	private static void AssignMaterial(Renderer renderer, Material material, string path)
	{
		if ((Object)(object)material == (Object)null)
		{
			Logger.LogWarning((object)("Could not assign null Black Core material to renderer '" + path + "'. Leaving bundled material unchanged."));
		}
		else
		{
			renderer.sharedMaterial = material;
		}
	}

	private static void LogRendererIfDebug(Renderer renderer, string path, string classification)
	{
		if (EnableDebugLogging)
		{
			Logger.LogInfo((object)("Black Core renderer '" + path + "' type '" + ((object)renderer).GetType().Name + "' materials [" + DescribeMaterials(renderer.sharedMaterials) + "] classified as '" + classification + "'."));
		}
	}

	private static string GetTransformPath(Transform transform)
	{
		string text = ((Object)transform).name;
		while ((Object)(object)transform.parent != (Object)null)
		{
			transform = transform.parent;
			text = ((Object)transform).name + "/" + text;
		}
		return text;
	}

	private static string DescribeMaterials(Material[] materials)
	{
		if (materials == null || materials.Length == 0)
		{
			return "none";
		}
		string[] array = new string[materials.Length];
		for (int i = 0; i < materials.Length; i++)
		{
			Material val = materials[i];
			int num = i;
			object obj;
			if (!((Object)(object)val == (Object)null))
			{
				string name = ((Object)val).name;
				Shader shader = val.shader;
				obj = name + " shader '" + ((shader != null) ? ((Object)shader).name : null) + "'";
			}
			else
			{
				obj = "null";
			}
			array[num] = (string)obj;
		}
		return string.Join(", ", array);
	}

	private static Material CreateTexturedWoodMaterial(string name, AssetBundle bundle, string materialAssetName, Color fallbackColor)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		Material val = CreateDiffuseMaterial(name, fallbackColor);
		if ((Object)(object)val == (Object)null)
		{
			return null;
		}
		ApplyBundledMaterialTexture(val, bundle, materialAssetName);
		return val;
	}

	private static void ApplyBundledMaterialTexture(Material targetMaterial, AssetBundle bundle, string materialAssetName)
	{
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		Material val = bundle.LoadAsset<Material>(materialAssetName);
		if ((Object)(object)val == (Object)null)
		{
			Logger.LogWarning((object)("Could not load bundled material '" + materialAssetName + "' for '" + ((Object)targetMaterial).name + "'. Keeping cloned Valheim texture."));
			return;
		}
		Texture materialTexture = GetMaterialTexture(val);
		if ((Object)(object)materialTexture == (Object)null)
		{
			Logger.LogWarning((object)("Bundled material '" + ((Object)val).name + "' has no _MainTex or _BaseMap texture. Keeping cloned Valheim texture for '" + ((Object)targetMaterial).name + "'."));
			return;
		}
		if (targetMaterial.HasProperty("_MainTex"))
		{
			targetMaterial.SetTexture("_MainTex", materialTexture);
		}
		if (targetMaterial.HasProperty("_BaseMap"))
		{
			targetMaterial.SetTexture("_BaseMap", materialTexture);
		}
		if (targetMaterial.HasProperty("_Color"))
		{
			targetMaterial.color = GetMaterialColor(val);
		}
		string[] obj = new string[9]
		{
			"Applied bundled texture '",
			((Object)materialTexture).name,
			"' from '",
			((Object)val).name,
			"' to '",
			((Object)targetMaterial).name,
			"' while preserving shader '",
			null,
			null
		};
		Shader shader = targetMaterial.shader;
		obj[7] = ((shader != null) ? ((Object)shader).name : null);
		obj[8] = "'.";
		Logger.LogInfo((object)string.Concat(obj));
	}

	private static Texture GetMaterialTexture(Material material)
	{
		if (material.HasProperty("_MainTex") && (Object)(object)material.GetTexture("_MainTex") != (Object)null)
		{
			return material.GetTexture("_MainTex");
		}
		if (material.HasProperty("_BaseMap") && (Object)(object)material.GetTexture("_BaseMap") != (Object)null)
		{
			return material.GetTexture("_BaseMap");
		}
		return null;
	}

	private static Color GetMaterialColor(Material material)
	{
		//IL_0017: 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_0038: 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_0030: 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)
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		if (material.HasProperty("_BaseColor"))
		{
			return material.GetColor("_BaseColor");
		}
		if (material.HasProperty("_Color"))
		{
			return material.color;
		}
		return Color.white;
	}

	private static string FormatVector(Vector3 vector)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: 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)
		return $"({vector.x:0.###}, {vector.y:0.###}, {vector.z:0.###})";
	}

	private static Material TryCreateClonedMaterialFromPrefab(string sourcePrefabName, string materialName, Color tint)
	{
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		GameObject prefab = PrefabManager.Instance.GetPrefab(sourcePrefabName);
		if ((Object)(object)prefab == (Object)null)
		{
			Logger.LogWarning((object)("Could not find source prefab '" + sourcePrefabName + "' for Black Core material '" + materialName + "'."));
			return null;
		}
		Renderer[] componentsInChildren = prefab.GetComponentsInChildren<Renderer>(true);
		foreach (Renderer val in componentsInChildren)
		{
			Material[] sharedMaterials = val.sharedMaterials;
			foreach (Material val2 in sharedMaterials)
			{
				if (!((Object)(object)val2 == (Object)null))
				{
					Material val3 = Object.Instantiate<Material>(val2);
					((Object)val3).name = materialName;
					TintMaterial(val3, tint);
					string[] obj = new string[9]
					{
						"Created Black Core material '",
						((Object)val3).name,
						"' from '",
						sourcePrefabName,
						"' material '",
						((Object)val2).name,
						"' using shader '",
						null,
						null
					};
					Shader shader = val2.shader;
					obj[7] = ((shader != null) ? ((Object)shader).name : null);
					obj[8] = "'.";
					Logger.LogInfo((object)string.Concat(obj));
					return val3;
				}
			}
		}
		Logger.LogWarning((object)("Could not find renderer materials on source prefab '" + sourcePrefabName + "' for Black Core material '" + materialName + "'."));
		return null;
	}

	private static void TintMaterial(Material material, Color tint)
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: 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_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: 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_0040: 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_004d: Unknown result type (might be due to invalid IL or missing references)
		if (material.HasProperty("_Color"))
		{
			Color color = material.color;
			material.color = new Color(color.r * tint.r, color.g * tint.g, color.b * tint.b, color.a * tint.a);
		}
	}

	private static Material CreateDiffuseMaterial(string name, Color color)
	{
		//IL_0049: 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_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Expected O, but got Unknown
		Shader val = Shader.Find("Diffuse");
		if ((Object)(object)val == (Object)null)
		{
			Logger.LogError((object)("Could not find Diffuse fallback shader for Black Core material '" + name + "'."));
			return null;
		}
		Logger.LogWarning((object)("Using Diffuse material '" + name + "' for Black Core visuals."));
		return new Material(val)
		{
			name = name,
			color = color
		};
	}
}
namespace BlackMetalBuildables
{
	[BepInPlugin("haydenc52.BlackMetalBuildables", "Black Metal Buildables", "1.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		private const string PluginGuid = "haydenc52.BlackMetalBuildables";

		private const string PluginName = "Black Metal Buildables";

		private const string PluginVersion = "1.1.0";

		private void Awake()
		{
			PrefabManager.OnVanillaPrefabsAvailable += BlackMetalCages.RegisterPieces;
			PrefabManager.OnVanillaPrefabsAvailable += BlackMetalCore.RegisterPieces;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Black Metal Buildables loaded");
		}
	}
}