Decompiled source of SnowFarm v1.0.0

SnowFarm.dll

Decompiled 2 days ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("SnowFarm")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SnowFarm")]
[assembly: AssemblyTitle("SnowFarm")]
[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 SnowFarm
{
	[BepInPlugin("snowfarm.valheim", "SnowFarm", "1.0.0")]
	public class SnowFarmPlugin : BaseUnityPlugin
	{
		public const string Guid = "snowfarm.valheim";

		public const string Name = "SnowFarm";

		public const string Version = "1.0.0";

		private static readonly string[] FarmPrefabs = new string[2] { "piece_beehive", "piece_birdnest" };

		private const Biome VanillaBiomes = (Biome)25;

		private static ManualLogSource _log;

		private static ConfigEntry<bool> _enabled;

		private static ConfigEntry<bool> _patchPrefabs;

		private static ConfigEntry<string> _extraBiomes;

		private static ConfigEntry<float> _maxCover;

		private static Biome _allowedBiomes;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Expected O, but got Unknown
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Expected O, but got Unknown
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			_log = ((BaseUnityPlugin)this).Logger;
			_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "Enabled", true, "Turn the whole plugin on or off.");
			_patchPrefabs = ((BaseUnityPlugin)this).Config.Bind<bool>("2 - Placement", "PatchPrefabs", true, "Also rewrite the build pieces themselves so they can be placed on snow.");
			_extraBiomes = ((BaseUnityPlugin)this).Config.Bind<string>("2 - Placement", "AdditionalBiomes", "Mountain,DeepNorth,Mistlands,Swamp,AshLands", "Biomes added to the vanilla Meadows/BlackForest/Plains list. Comma separated, case insensitive.");
			_maxCover = ((BaseUnityPlugin)this).Config.Bind<float>("3 - Production", "MaxCover", 1f, new ConfigDescription("Maximum roof/snow coverage for production (0-1). 1 = inside a house works too.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			_allowedBiomes = ParseBiomes(_extraBiomes.Value);
			_harmony = new Harmony("snowfarm.valheim");
			PatchInstances();
			((MonoBehaviour)this).StartCoroutine(PatchPrefabsWhenReady());
			((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("{0} {1} loaded. Allowed biomes: {2}", "SnowFarm", "1.0.0", _allowedBiomes));
		}

		private void OnDestroy()
		{
			if (_harmony != null)
			{
				_harmony.UnpatchSelf();
			}
		}

		private void PatchInstances()
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			Type type = AccessTools.TypeByName("Beehive");
			if (type == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"Type 'Beehive' not found - only the prefab patch is active.");
				return;
			}
			HarmonyMethod val = new HarmonyMethod(AccessTools.Method(typeof(SnowFarmPlugin), "BeehivePostfix", (Type[])null, (Type[])null));
			string[] array = new string[2] { "Awake", "UpdateBees" };
			foreach (string text in array)
			{
				MethodInfo methodInfo = AccessTools.Method(type, text, (Type[])null, (Type[])null);
				if (methodInfo == null)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)("Beehive." + text + " not found, skipped."));
					continue;
				}
				try
				{
					_harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)("Failed to patch Beehive." + text + ": " + ex.Message));
				}
			}
		}

		private static void BeehivePostfix(MonoBehaviour __instance)
		{
			if ((Object)(object)__instance != (Object)null)
			{
				Relax(((Component)__instance).gameObject, "beehive instance");
			}
		}

		private IEnumerator PatchPrefabsWhenReady()
		{
			while ((Object)(object)ZNetScene.instance == (Object)null)
			{
				yield return null;
			}
			if (!_patchPrefabs.Value)
			{
				yield break;
			}
			string[] farmPrefabs = FarmPrefabs;
			foreach (string text in farmPrefabs)
			{
				GameObject prefab = ZNetScene.instance.GetPrefab(text);
				if ((Object)(object)prefab == (Object)null)
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)("Prefab '" + text + "' not found, skipped."));
				}
				else
				{
					Relax(prefab, text);
				}
			}
		}

		private static void Relax(GameObject root, string label)
		{
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			if (!_enabled.Value || (Object)(object)root == (Object)null)
			{
				return;
			}
			MonoBehaviour[] componentsInChildren = root.GetComponentsInChildren<MonoBehaviour>(true);
			foreach (MonoBehaviour val in componentsInChildren)
			{
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				Type type = ((object)val).GetType();
				FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				foreach (FieldInfo fieldInfo in fields)
				{
					try
					{
						if (fieldInfo.FieldType == typeof(Biome))
						{
							fieldInfo.SetValue(val, _allowedBiomes);
						}
						else if (fieldInfo.FieldType == typeof(float) && fieldInfo.Name.IndexOf("cover", StringComparison.OrdinalIgnoreCase) >= 0)
						{
							fieldInfo.SetValue(val, _maxCover.Value);
						}
					}
					catch (Exception ex)
					{
						ManualLogSource log = _log;
						if (log != null)
						{
							log.LogDebug((object)("Skipped " + type.Name + "." + fieldInfo.Name + ": " + ex.Message));
						}
					}
				}
			}
			ManualLogSource log2 = _log;
			if (log2 != null)
			{
				log2.LogDebug((object)("Relaxed " + label));
			}
		}

		private static Biome ParseBiomes(string value)
		{
			//IL_0002: 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_0070: 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)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: 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)
			Biome val = (Biome)25;
			if (string.IsNullOrEmpty(value))
			{
				return val;
			}
			string[] array = value.Split(new char[1] { ',' });
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i].Trim();
				if (text.Length == 0)
				{
					continue;
				}
				if (Enum.TryParse<Biome>(text, ignoreCase: true, out Biome result))
				{
					val |= result;
					continue;
				}
				ManualLogSource log = _log;
				if (log != null)
				{
					log.LogWarning((object)("Unknown biome '" + text + "' in config."));
				}
			}
			return val;
		}
	}
}