Decompiled source of FloatingItems v1.0.1

FloatingItems.dll

Decompiled 21 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyVersion("0.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 FloatingItems
{
	internal static class Buoyancy
	{
		private const string TemplateItem = "Wood";

		private static Floating _template;

		private static bool _templateMissingLogged;

		public static bool TryAdd(GameObject item)
		{
			if (item.GetComponent<IWaterInteractable>() != null)
			{
				return false;
			}
			if ((Object)(object)item.GetComponent<Rigidbody>() == (Object)null || (Object)(object)item.GetComponent<ZNetView>() == (Object)null || (Object)(object)item.GetComponentInChildren<Collider>() == (Object)null)
			{
				return false;
			}
			Floating to = item.AddComponent<Floating>();
			Floating val = Template();
			if ((Object)(object)val != (Object)null)
			{
				CopySettings(val, to);
			}
			return true;
		}

		private static void CopySettings(Floating from, Floating to)
		{
			to.m_waterLevelOffset = from.m_waterLevelOffset;
			to.m_forceDistance = from.m_forceDistance;
			to.m_force = from.m_force;
			to.m_balanceForceFraction = from.m_balanceForceFraction;
			to.m_damping = from.m_damping;
			to.m_impactEffects = from.m_impactEffects;
		}

		private static Floating Template()
		{
			if ((Object)(object)_template != (Object)null)
			{
				return _template;
			}
			ObjectDB instance = ObjectDB.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return null;
			}
			_template = FindTemplate(instance);
			if ((Object)(object)_template != (Object)null)
			{
				Plugin.Log.LogInfo((object)($"Wzor plywania: {((Object)_template).name} (offset {_template.m_waterLevelOffset}, sila {_template.m_force}, " + $"zasieg {_template.m_forceDistance}, tlumienie {_template.m_damping})."));
			}
			else if (!_templateMissingLogged)
			{
				_templateMissingLogged = true;
				Plugin.Log.LogWarning((object)"Zaden przedmiot gry nie ma komponentu Floating - uzywam jego domyslnych ustawien.");
			}
			return _template;
		}

		private static Floating FindTemplate(ObjectDB db)
		{
			GameObject itemPrefab = db.GetItemPrefab("Wood");
			Floating result = default(Floating);
			if ((Object)(object)itemPrefab != (Object)null && itemPrefab.TryGetComponent<Floating>(ref result))
			{
				return result;
			}
			foreach (GameObject item in db.m_items)
			{
				if ((Object)(object)item != (Object)null && item.TryGetComponent<Floating>(ref result))
				{
					return result;
				}
			}
			return null;
		}
	}
	[BepInPlugin("com.ab5olutezer0.valheim.floatingitems", "Floating Items", "1.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(ItemDrop), "Awake")]
		private static class ItemDrop_Awake_Patch
		{
			private static void Prefix(ItemDrop __instance)
			{
				try
				{
					GameObject gameObject = ((Component)__instance).gameObject;
					if (!_excluded.Contains(Utils.GetPrefabName(gameObject)))
					{
						Buoyancy.TryAdd(gameObject);
					}
				}
				catch (Exception arg)
				{
					Log.LogError((object)$"Nie udalo sie dodac plywania do '{((Object)__instance).name}': {arg}");
				}
			}
		}

		public const string PluginGUID = "com.ab5olutezer0.valheim.floatingitems";

		public const string PluginName = "Floating Items";

		public const string PluginVersion = "1.0.1";

		internal static ManualLogSource Log;

		private static ConfigEntry<string> _excludedItems;

		private static HashSet<string> _excluded = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

		private const string LegacyPluginGUID = "com.michal.valheim.floatingitems";

		private void MigrateLegacyConfig()
		{
			string text = Path.Combine(Paths.ConfigPath, "com.michal.valheim.floatingitems.cfg");
			if (File.Exists(((BaseUnityPlugin)this).Config.ConfigFilePath) || !File.Exists(text))
			{
				return;
			}
			try
			{
				File.Move(text, ((BaseUnityPlugin)this).Config.ConfigFilePath);
				((BaseUnityPlugin)this).Config.Reload();
				((BaseUnityPlugin)this).Logger.LogInfo((object)("Przeniesiono ustawienia: " + text + " -> " + ((BaseUnityPlugin)this).Config.ConfigFilePath));
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)$"Nie udalo sie przeniesc ustawien ({text}): {arg}");
			}
		}

		private void Awake()
		{
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Game.isModded = true;
			MigrateLegacyConfig();
			_excludedItems = ((BaseUnityPlugin)this).Config.Bind<string>("General", "ExcludedItems", "", "Comma-separated prefab names of items that should keep sinking, e.g. \"Coins, IronScrap\". Applies to items dropped after the change.");
			_excludedItems.SettingChanged += delegate
			{
				LoadExcluded();
			};
			LoadExcluded();
			new Harmony("com.ab5olutezer0.valheim.floatingitems").PatchAll(typeof(Plugin).Assembly);
		}

		private static void LoadExcluded()
		{
			HashSet<string> hashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
			string[] array = (_excludedItems.Value ?? "").Split(',');
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i].Trim();
				if (text.Length > 0)
				{
					hashSet.Add(text);
				}
			}
			_excluded = hashSet;
		}
	}
}