Decompiled source of PreventLoosingScrap v1.0.2

PreventLoosingScrap.dll

Decompiled 5 months 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 Amrv.ConfigurableCompany.API;
using Amrv.ConfigurableCompany.API.Accesors;
using Amrv.ConfigurableCompany.Core;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
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("PreventLoosingScrap")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("This mod allows you to adjust the loss rate of scrap items in case of a party wipe")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2+60bfef6d7948847d13bdd89bb43de95d2801b82d")]
[assembly: AssemblyProduct("PreventLoosingScrap")]
[assembly: AssemblyTitle("PreventLoosingScrap")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.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 PreventLoosingScrap
{
	[BepInPlugin("PreventLoosingScrap", "PreventLoosingScrap", "1.0.2")]
	public class Plugin : BaseUnityPlugin
	{
		public static ManualLogSource Log;

		public static PluginConfig Cfg;

		private void Awake()
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Cfg = new PluginConfig();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin PreventLoosingScrap is loaded!");
			Harmony val = new Harmony("PreventLoosingScrap");
			val.PatchAll(typeof(SPProtectionPatches));
		}
	}
	public class PluginConfig
	{
		public CPage PAGE;

		public CCategory CATEGORY;

		public CConfig SCRAP_LOST_RATE_ON_LOOSING;

		public PluginConfig()
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Expected O, but got Unknown
			PAGE = ConfigAPI.CreatePageAuto("Prevent Loosing Scrap", "");
			CATEGORY = ConfigAPI.CreateCategoryAuto("Gameplay", PAGE, (Color?)Color.red);
			CConfigBuilder val = new CConfigBuilder();
			val.ID = "vulcapack-scrap-lost-rate";
			val.Name = "Scrap lost rate";
			val.BCategory = BuildCategory.op_Implicit(CATEGORY);
			val.DefaultValue = 100f;
			val.BTooltip = BuildTooltip.op_Implicit(new string[3] { "Percentage of chance to loose item upon party wipe", "", "This rate is the probability of lossing each item inside the ship, with this formula <u>rate <= RAND(0, 100)</u>" });
			val.Type = CTypes.WholePercent();
			SCRAP_LOST_RATE_ON_LOOSING = ((InstanceBuilder<CConfig>)(object)val).Build();
		}
	}
	public class SPProtectionPatches
	{
		[HarmonyPatch(typeof(RoundManager), "DespawnPropsAtEndOfRound")]
		[HarmonyPrefix]
		public static bool ProtectionPrefix(RoundManager __instance, bool despawnAllItems)
		{
			int num = Mathf.RoundToInt(Plugin.Cfg.SCRAP_LOST_RATE_ON_LOOSING.Get<float>(100f));
			Plugin.Log.LogInfo((object)("ProtectionPatch -> Rate = " + num));
			if (num <= 1 || despawnAllItems)
			{
				return true;
			}
			if (((NetworkBehaviour)__instance).IsHost || ((NetworkBehaviour)__instance).IsServer)
			{
				Plugin.Log.LogInfo((object)("ProtectionPatch -> " + despawnAllItems + " : " + StartOfRound.Instance.allPlayersDead));
				if (StartOfRound.Instance.allPlayersDead)
				{
					GrabbableObject[] array = Object.FindObjectsOfType<GrabbableObject>();
					Random rng = new Random(StartOfRound.Instance.randomMapSeed + 83);
					GrabbableObject[] array2 = array;
					foreach (GrabbableObject val in array2)
					{
						if (val.isInShipRoom)
						{
							if (!val.itemProperties.isScrap || ShouldSaveScrap(rng, num))
							{
								Plugin.Log.LogInfo((object)("Preserving ship item: " + ((Object)val).name));
							}
							else
							{
								DeleteItem(val);
							}
						}
						else
						{
							DeleteItem(val);
						}
					}
					GameObject[] array3 = GameObject.FindGameObjectsWithTag("TemporaryEffect");
					for (int j = 0; j < array3.Length; j++)
					{
						Object.Destroy((Object)(object)array3[j]);
					}
					return false;
				}
			}
			return true;
			void DeleteItem(GrabbableObject item)
			{
				Plugin.Log.LogInfo((object)("Despawning item: " + ((Object)item).name + " in ship: " + item.isInShipRoom));
				((Component)item).gameObject.GetComponent<NetworkObject>().Despawn(true);
				if (__instance.spawnedSyncedObjects.Contains(((Component)item).gameObject))
				{
					__instance.spawnedSyncedObjects.Remove(((Component)item).gameObject);
				}
			}
		}

		public static bool ShouldSaveScrap(Random rng, int rate)
		{
			return rng.Next(0, 100) >= rate;
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "PreventLoosingScrap";

		public const string PLUGIN_NAME = "PreventLoosingScrap";

		public const string PLUGIN_VERSION = "1.0.2";
	}
}