Decompiled source of Fun Scrap v0.1.4

plugins/funScrapCode.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using funScrapCode.NetcodePatcher;
using funScrapGlobalCode.Items;
using funScrapGlobalCode.Patches;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("funScrapCode")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Scripted elements for funScrap")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0")]
[assembly: AssemblyProduct("funScrapCode")]
[assembly: AssemblyTitle("funScrapCode")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
[module: NetcodePatchedAssembly]
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 funScrapGlobalCode
{
	internal static class ModConfig
	{
		public static ConfigEntry<float> WineInterestRate;

		public static ConfigEntry<float> FragileBoxLoss;

		public static void Bind(ConfigFile config)
		{
			WineInterestRate = config.Bind<float>("Wine", "InterestRate", 0.07f, "How much value wine gains every day (0.07 = 7%).");
			FragileBoxLoss = config.Bind<float>("FragileBox", "LossRate", 0.3f, "Value lost each time the Fragile Box is dropped outside the ship or Company Building (0.30 = 30%).");
		}
	}
	[BepInPlugin("oskiebadoskie.funScrapGlobalCode", "funScrapGlobalCode", "0.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string modGUID = "oskiebadoskie.funScrapGlobalCode";

		public const string modName = "funScrapGlobalCode";

		public const string modVersion = "0.1.0";

		internal static ManualLogSource nls = Logger.CreateLogSource("oskiebadoskie.funScrapGlobalCode");

		private void Awake()
		{
			MonoBehaviour.print((object)"> > > > > FUNSCRAP CODE LOADED");
			ModConfig.Bind(((BaseUnityPlugin)this).Config);
			Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "oskiebadoskie.funScrapGlobalCode");
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "funScrapCode";

		public const string PLUGIN_NAME = "funScrapCode";

		public const string PLUGIN_VERSION = "0.1.0";
	}
}
namespace funScrapGlobalCode.Items
{
	[RequireComponent(typeof(GrabbableObject))]
	public class FragileBoxSync : NetworkBehaviour
	{
		private GrabbableObject grabbableObject;

		private void Awake()
		{
			grabbableObject = ((Component)this).GetComponent<GrabbableObject>();
		}

		public override void OnNetworkSpawn()
		{
			((NetworkBehaviour)this).OnNetworkSpawn();
			Debug.Log((object)$"[FragileBox] FragileBoxSync spawned. NetworkObjectId={((NetworkBehaviour)this).NetworkObjectId}, IsServer={((NetworkBehaviour)this).IsServer}, IsClient={((NetworkBehaviour)this).IsClient}, IsOwner={((NetworkBehaviour)this).IsOwner}");
		}

		[ClientRpc]
		public void SyncClientRpc(int newValue)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Invalid comparison between Unknown and I4
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Invalid comparison between Unknown and I4
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = ((NetworkBehaviour)this).NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				if ((int)base.__rpc_exec_stage != 1 && (networkManager.IsServer || networkManager.IsHost))
				{
					ClientRpcParams val2 = default(ClientRpcParams);
					FastBufferWriter val = ((NetworkBehaviour)this).__beginSendClientRpc(3406126967u, val2, (RpcDelivery)0);
					BytePacker.WriteValueBitPacked(val, newValue);
					((NetworkBehaviour)this).__endSendClientRpc(ref val, 3406126967u, val2, (RpcDelivery)0);
				}
				if ((int)base.__rpc_exec_stage == 1 && (networkManager.IsClient || networkManager.IsHost))
				{
					base.__rpc_exec_stage = (__RpcExecStage)0;
					Debug.Log((object)$"[FragileBox] SyncClientRpc received: {newValue}");
					grabbableObject.SetScrapValue(newValue);
				}
			}
		}

		protected override void __initializeVariables()
		{
			((NetworkBehaviour)this).__initializeVariables();
		}

		protected override void __initializeRpcs()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			((NetworkBehaviour)this).__registerRpc(3406126967u, new RpcReceiveHandler(__rpc_handler_3406126967), "SyncClientRpc");
			((NetworkBehaviour)this).__initializeRpcs();
		}

		private static void __rpc_handler_3406126967(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = target.NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				int newValue = default(int);
				ByteUnpacker.ReadValueBitPacked(reader, ref newValue);
				target.__rpc_exec_stage = (__RpcExecStage)1;
				((FragileBoxSync)(object)target).SyncClientRpc(newValue);
				target.__rpc_exec_stage = (__RpcExecStage)0;
			}
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		protected internal override string __getTypeName()
		{
			return "FragileBoxSync";
		}
	}
	[RequireComponent(typeof(GrabbableObject))]
	public class WineAging : NetworkBehaviour
	{
		private GrabbableObject grabbableObject;

		private void Awake()
		{
			grabbableObject = ((Component)this).GetComponent<GrabbableObject>();
		}

		public override void OnNetworkSpawn()
		{
			if (((NetworkBehaviour)this).IsServer)
			{
				DayChangeEvents.OnDayChanged += AgeOneDay;
			}
		}

		public override void OnNetworkDespawn()
		{
			if (((NetworkBehaviour)this).IsServer)
			{
				DayChangeEvents.OnDayChanged -= AgeOneDay;
			}
		}

		private void AgeOneDay()
		{
			int scrapValue = grabbableObject.scrapValue;
			int num = scrapValue + Mathf.RoundToInt((float)scrapValue * ModConfig.WineInterestRate.Value);
			grabbableObject.SetScrapValue(num);
			SyncClientRpc(num);
		}

		[ClientRpc]
		private void SyncClientRpc(int newValue)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Invalid comparison between Unknown and I4
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Invalid comparison between Unknown and I4
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = ((NetworkBehaviour)this).NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				if ((int)base.__rpc_exec_stage != 1 && (networkManager.IsServer || networkManager.IsHost))
				{
					ClientRpcParams val2 = default(ClientRpcParams);
					FastBufferWriter val = ((NetworkBehaviour)this).__beginSendClientRpc(1797315736u, val2, (RpcDelivery)0);
					BytePacker.WriteValueBitPacked(val, newValue);
					((NetworkBehaviour)this).__endSendClientRpc(ref val, 1797315736u, val2, (RpcDelivery)0);
				}
				if ((int)base.__rpc_exec_stage == 1 && (networkManager.IsClient || networkManager.IsHost))
				{
					base.__rpc_exec_stage = (__RpcExecStage)0;
					grabbableObject.SetScrapValue(newValue);
				}
			}
		}

		protected override void __initializeVariables()
		{
			((NetworkBehaviour)this).__initializeVariables();
		}

		protected override void __initializeRpcs()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			((NetworkBehaviour)this).__registerRpc(1797315736u, new RpcReceiveHandler(__rpc_handler_1797315736), "SyncClientRpc");
			((NetworkBehaviour)this).__initializeRpcs();
		}

		private static void __rpc_handler_1797315736(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			NetworkManager networkManager = target.NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				int newValue = default(int);
				ByteUnpacker.ReadValueBitPacked(reader, ref newValue);
				target.__rpc_exec_stage = (__RpcExecStage)1;
				((WineAging)(object)target).SyncClientRpc(newValue);
				target.__rpc_exec_stage = (__RpcExecStage)0;
			}
		}

		[MethodImpl(MethodImplOptions.NoInlining)]
		protected internal override string __getTypeName()
		{
			return "WineAging";
		}
	}
}
namespace funScrapGlobalCode.Patches
{
	internal static class DayChangeEvents
	{
		public static event Action OnDayChanged;

		internal static void RaiseDayChanged()
		{
			if (DayChangeEvents.OnDayChanged == null)
			{
				return;
			}
			Delegate[] invocationList = DayChangeEvents.OnDayChanged.GetInvocationList();
			for (int i = 0; i < invocationList.Length; i++)
			{
				Action action = (Action)invocationList[i];
				try
				{
					action();
				}
				catch (Exception arg)
				{
					Debug.LogError((object)$"[DayChangeEvents] A subscriber threw: {arg}");
				}
			}
		}
	}
	[HarmonyPatch(typeof(GrabbableObject))]
	internal class FragileBoxPatch
	{
		private static AudioClip cachedShatterClip;

		private static AudioClip ShatterClip()
		{
			if ((Object)(object)cachedShatterClip == (Object)null)
			{
				cachedShatterClip = ((IEnumerable<AudioClip>)Resources.FindObjectsOfTypeAll<AudioClip>()).FirstOrDefault((Func<AudioClip, bool>)((AudioClip clip) => ((Object)clip).name == "WindshieldShatter"));
			}
			return cachedShatterClip;
		}

		[HarmonyPatch("PlayDropSFX")]
		[HarmonyPostfix]
		private static void OnDropped(GrabbableObject __instance)
		{
			if ((Object)(object)__instance.itemProperties == (Object)null || __instance.itemProperties.itemName != "Fragile Package")
			{
				return;
			}
			bool flag = ((Component)__instance).transform.IsChildOf(StartOfRound.Instance.elevatorTransform);
			SelectableLevel currentLevel = StartOfRound.Instance.currentLevel;
			bool flag2 = (Object)(object)currentLevel != (Object)null && !currentLevel.planetHasTime;
			if (!(flag || flag2))
			{
				AudioClip val = ShatterClip();
				AudioSource component = ((Component)__instance).GetComponent<AudioSource>();
				if ((Object)(object)val != (Object)null && (Object)(object)component != (Object)null)
				{
					component.pitch = Random.Range(0.95f, 1.05f);
					component.PlayOneShot(val);
				}
				if (!((Object)(object)NetworkManager.Singleton == (Object)null) && NetworkManager.Singleton.IsServer)
				{
					int scrapValue = __instance.scrapValue;
					int num = Mathf.Max(0, scrapValue - Mathf.RoundToInt((float)scrapValue * ModConfig.FragileBoxLoss.Value));
					__instance.SetScrapValue(num);
					((Component)__instance).GetComponent<FragileBoxSync>()?.SyncClientRpc(num);
					Debug.Log((object)$"[FragileBox] {scrapValue} -> {num}");
				}
			}
		}
	}
	[HarmonyPatch(typeof(TimeOfDay))]
	internal class TimeOfDayPatch
	{
		[HarmonyPatch("OnDayChanged")]
		[HarmonyPostfix]
		private static void OnDayChanged()
		{
			DayChangeEvents.RaiseDayChanged();
		}
	}
}
namespace __GEN
{
	internal class NetworkVariableSerializationHelper
	{
		[RuntimeInitializeOnLoadMethod]
		internal static void InitializeSerialization()
		{
		}
	}
}
namespace funScrapCode.NetcodePatcher
{
	[AttributeUsage(AttributeTargets.Module)]
	internal class NetcodePatchedAssemblyAttribute : Attribute
	{
	}
}