Decompiled source of FlamelessTorch v1.0.0

FlamelessTorch.dll

Decompiled 5 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
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: AssemblyTitle("FlamelessTorch")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("HP")]
[assembly: AssemblyProduct("FlamelessTorch")]
[assembly: AssemblyCopyright("Copyright © HP 2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("54029edb-b86f-4fd1-9df4-f24b8b22f7e0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = "")]
[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;
		}
	}
}
namespace FlamelessTorch
{
	[BepInPlugin("YouDied.FlamelessTorch", "FlamelessTorch", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInProcess("valheim.exe")]
	public class FlamelessTorchPlugin : BaseUnityPlugin
	{
		public const string ModGUID = "YouDied.FlamelessTorch";

		public const string ModName = "FlamelessTorch";

		public const string ModVersion = "1.0.0";

		internal static ManualLogSource Log;

		private static Harmony _harmony;

		internal const string PrefabName = "piece_flamelesstorch";

		internal const string LocalizedName = "Flameless Torch";

		internal const string LocalizedDesc = "A cold torch without a flicker of light. Surely this is useful for something...";

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony = new Harmony("YouDied.FlamelessTorch");
			_harmony.PatchAll();
			PrefabManager.OnVanillaPrefabsAvailable += RegisterTorch;
			Log.LogInfo((object)"FlamelessTorch 1.0.0 loaded.");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			PrefabManager.OnVanillaPrefabsAvailable -= RegisterTorch;
		}

		private static void RegisterTorch()
		{
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0116: Expected I4, but got Unknown
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Expected O, but got Unknown
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			//IL_0186: 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_019a: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a3: Expected O, but got Unknown
			//IL_01a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01be: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c7: Expected O, but got Unknown
			//IL_01d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e2: Expected O, but got Unknown
			PrefabManager.OnVanillaPrefabsAvailable -= RegisterTorch;
			try
			{
				GameObject prefab = PrefabManager.Instance.GetPrefab("piece_groundtorch_wood");
				if ((Object)(object)prefab == (Object)null)
				{
					Log.LogError((object)"[FlamelessTorch] piece_groundtorch_wood not found.");
					return;
				}
				GameObject val = PrefabManager.Instance.CreateClonedPrefab("piece_flamelesstorch", prefab);
				if ((Object)(object)val == (Object)null)
				{
					Log.LogError((object)"[FlamelessTorch] CreateClonedPrefab returned null.");
					return;
				}
				Log.LogInfo((object)("[FlamelessTorch] Cloned: " + ((Object)val).name));
				StripFireAndLight(val);
				Piece val2 = val.GetComponent<Piece>();
				if ((Object)(object)val2 == (Object)null)
				{
					val2 = val.AddComponent<Piece>();
				}
				val2.m_name = "Flameless Torch";
				val2.m_description = "A cold torch without a flicker of light. Surely this is useful for something...";
				val2.m_icon = LoadEmbeddedIcon();
				val2.m_enabled = true;
				val2.m_craftingStation = null;
				Log.LogInfo((object)$"[FlamelessTorch] Category: {val2.m_category} = {(int)val2.m_category}");
				PieceConfig val3 = new PieceConfig();
				val3.Name = "Flameless Torch";
				val3.Description = "A cold torch without a flicker of light. Surely this is useful for something...";
				val3.PieceTable = "Hammer";
				val3.Category = "Furniture";
				val3.CraftingStation = "piece_workbench";
				val3.Icon = val2.m_icon;
				val3.Requirements = (RequirementConfig[])(object)new RequirementConfig[2]
				{
					new RequirementConfig
					{
						Item = "Wood",
						Amount = 2,
						Recover = true
					},
					new RequirementConfig
					{
						Item = "Resin",
						Amount = 2,
						Recover = true
					}
				};
				PieceConfig val4 = val3;
				PieceManager.Instance.AddPiece(new CustomPiece(val, false, val4));
				Log.LogInfo((object)"[FlamelessTorch] Registered. Furniture tab or: spawn piece_flamelesstorch");
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"[FlamelessTorch] Registration error: {arg}");
			}
		}

		private static void StripFireAndLight(GameObject go)
		{
			List<Component> list = new List<Component>();
			Light[] componentsInChildren = go.GetComponentsInChildren<Light>(true);
			foreach (Light val in componentsInChildren)
			{
				if ((Object)(object)val != (Object)null)
				{
					list.Add((Component)(object)val);
				}
			}
			Component[] componentsInChildren2 = go.GetComponentsInChildren<Component>(true);
			foreach (Component val2 in componentsInChildren2)
			{
				if ((Object)(object)val2 == (Object)null)
				{
					continue;
				}
				string fullName = ((object)val2).GetType().FullName;
				switch (fullName)
				{
				default:
					if (!(fullName == "LightLod"))
					{
						continue;
					}
					break;
				case "UnityEngine.ParticleSystem":
				case "UnityEngine.AudioSource":
				case "LightFlicker":
					break;
				}
				list.Add(val2);
			}
			foreach (Component item in list)
			{
				if ((Object)(object)item != (Object)null)
				{
					Object.DestroyImmediate((Object)(object)item);
				}
			}
			string[] kws = new string[6] { "fire", "flame", "smoke", "ember", "glow", "spark" };
			DeactivateByKeyword(go.transform, kws);
			Fireplace component = go.GetComponent<Fireplace>();
			if ((Object)(object)component != (Object)null)
			{
				TrySetField(component, "m_enabledObjectMask", 0);
				TrySetField(component, "m_disabledObjectMask", 0);
				object? obj = typeof(Fireplace).GetField("m_light", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(component);
				Light val3 = (Light)((obj is Light) ? obj : null);
				if (val3 != null)
				{
					val3.intensity = 0f;
				}
			}
		}

		private static void DeactivateByKeyword(Transform t, string[] kws)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			foreach (Transform item in t)
			{
				Transform val = item;
				string text = ((Object)val).name.ToLowerInvariant();
				foreach (string value in kws)
				{
					if (text.Contains(value))
					{
						((Component)val).gameObject.SetActive(false);
						break;
					}
				}
				DeactivateByKeyword(val, kws);
			}
		}

		private static void TrySetField(object obj, string field, object value)
		{
			try
			{
				obj.GetType().GetField(field, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.SetValue(obj, value);
			}
			catch
			{
			}
		}

		internal static Sprite LoadEmbeddedIcon()
		{
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Expected O, but got Unknown
			//IL_00db: 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)
			try
			{
				Assembly executingAssembly = Assembly.GetExecutingAssembly();
				using Stream stream = executingAssembly.GetManifestResourceStream("FlamelessTorch.Assets.flamelesstorch.png");
				if (stream == null)
				{
					return CreateFallbackSprite();
				}
				byte[] array = new byte[stream.Length];
				stream.Read(array, 0, array.Length);
				Texture2D val = new Texture2D(2, 2);
				(Type.GetType("UnityEngine.ImageConversion, UnityEngine.CoreModule") ?? Type.GetType("UnityEngine.ImageConversion, UnityEngine.ImageConversionModule"))?.GetMethod("LoadImage", BindingFlags.Static | BindingFlags.Public, null, new Type[2]
				{
					typeof(Texture2D),
					typeof(byte[])
				}, null)?.Invoke(null, new object[2] { val, array });
				((Object)val).name = "flamelesstorch_icon";
				return Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f));
			}
			catch
			{
				return CreateFallbackSprite();
			}
		}

		private static Sprite CreateFallbackSprite()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Expected O, but got Unknown
			//IL_001c: 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_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = new Texture2D(32, 32);
			Color[] array = (Color[])(object)new Color[1024];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = Color.magenta;
			}
			val.SetPixels(array);
			val.Apply();
			return Sprite.Create(val, new Rect(0f, 0f, 32f, 32f), new Vector2(0.5f, 0.5f));
		}
	}
	[HarmonyPatch(typeof(ZNetScene), "GetPrefabNames")]
	public static class ZNetScene_GetPrefabNames_Patch
	{
		public static void Prefix(ZNetScene __instance)
		{
			if (typeof(ZNetScene).GetField("m_namedPrefabs", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.GetValue(__instance) is Dictionary<int, GameObject> dictionary)
			{
				List<int> list = new List<int>();
				foreach (KeyValuePair<int, GameObject> item in dictionary)
				{
					if ((Object)(object)item.Value == (Object)null)
					{
						list.Add(item.Key);
					}
				}
				foreach (int item2 in list)
				{
					dictionary.Remove(item2);
				}
			}
			__instance.m_prefabs.RemoveAll((GameObject p) => (Object)(object)p == (Object)null);
		}
	}
}