Decompiled source of Persistent Items v1.0.2

BepInEx/plugins/PersistentItems/PersistentItems.dll

Decompiled 11 months ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
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: AssemblyTitle("PersistentItems")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PersistentItems")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("41e93147-946d-4a18-9f1c-6f5aef190fa6")]
[assembly: AssemblyFileVersion("1.0.1")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.1.0")]
namespace PersistentItems;

[BepInPlugin("com.mysteryman36.PersistentItems", "PersistentItems", "1.0.1")]
public class PersistentItemsPlugin : BaseUnityPlugin
{
	private const string MyGUID = "com.mysteryman36.PersistentItems";

	private const string PluginName = "PersistentItems";

	private const string VersionString = "1.0.1";

	private static readonly Harmony Harmony = new Harmony("com.mysteryman36.PersistentItems");

	public static ManualLogSource Log = new ManualLogSource("PersistentItems");

	public static List<GrabbableObject> scrap = new List<GrabbableObject>();

	public static string lastMoon;

	private void Awake()
	{
		Log.LogInfo((object)"PluginName: PersistentItems, VersionString: 1.0.1 is loading...");
		Harmony.PatchAll();
		Log.LogInfo((object)"PluginName: PersistentItems, VersionString: 1.0.1 is loaded.");
		Log = ((BaseUnityPlugin)this).Logger;
	}
}
[HarmonyPatch]
internal class MainPatch
{
	[HarmonyPatch(typeof(StartOfRound), "StartGame")]
	[HarmonyPostfix]
	public static void StartGame(StartOfRound __instance)
	{
		if (!((NetworkBehaviour)__instance).IsServer)
		{
			return;
		}
		if (__instance.currentLevel.PlanetName != PersistentItemsPlugin.lastMoon)
		{
			PersistentItemsPlugin.Log.LogInfo((object)"Different moon. Despawning items.");
			foreach (GrabbableObject item in PersistentItemsPlugin.scrap)
			{
				if ((Object)(object)item != (Object)null)
				{
					PersistentItemsPlugin.Log.LogInfo((object)("Despawning prop '" + ((Object)((Component)item).gameObject).name));
					NetworkObject component = ((Component)item).gameObject.GetComponent<NetworkObject>();
					if ((Object)(object)component != (Object)null && component.IsSpawned)
					{
						((Component)item).gameObject.GetComponent<NetworkObject>().Despawn(true);
						continue;
					}
					PersistentItemsPlugin.Log.LogInfo((object)("Error/warning: prop '" + ((Object)((Component)item).gameObject).name + "' was not spawned or did not have a NetworkObject component! Skipped despawning and destroyed it instead."));
					Object.Destroy((Object)(object)((Component)item).gameObject);
				}
			}
			PersistentItemsPlugin.scrap.Clear();
		}
		else
		{
			PersistentItemsPlugin.Log.LogInfo((object)"Same moon. Keeping items.");
		}
	}

	[HarmonyPatch(typeof(RoundManager), "DespawnPropsAtEndOfRound")]
	[HarmonyPrefix]
	public static bool DespawnPropsAtEndOfRound(RoundManager __instance, bool despawnAllItems = false)
	{
		if (((NetworkBehaviour)__instance).IsServer)
		{
			PersistentItemsPlugin.lastMoon = __instance.currentLevel.PlanetName;
			GrabbableObject[] array = Object.FindObjectsOfType<GrabbableObject>();
			for (int i = 0; i < array.Length; i++)
			{
				PersistentItemsPlugin.Log.LogInfo((object)("is prop '" + ((Object)((Component)array[i]).gameObject).name + "' in ship: " + array[i].isInShipRoom));
				PersistentItemsPlugin.Log.LogInfo((object)("is prop '" + ((Object)((Component)array[i]).gameObject).name + "' scrap: " + array[i].itemProperties.isScrap));
				PersistentItemsPlugin.Log.LogInfo((object)("is prop '" + ((Object)((Component)array[i]).gameObject).name + "' deactivated: " + array[i].deactivated));
				PersistentItemsPlugin.Log.LogInfo((object)("is prop '" + ((Object)((Component)array[i]).gameObject).name + "' held: " + array[i].isHeld));
				PersistentItemsPlugin.Log.LogInfo((object)("is prop '" + ((Object)((Component)array[i]).gameObject).name + "' in facility: " + array[i].isInFactory));
				if (!array[i].isInShipRoom && array[i].isHeld && (Object)(object)array[i].playerHeldBy != (Object)null)
				{
					array[i].playerHeldBy.DropAllHeldItemsAndSync();
				}
				if (despawnAllItems || (StartOfRound.Instance.allPlayersDead && (array[i].itemProperties.isScrap || !array[i].isInShipRoom)) || array[i].deactivated || array[i].isInFactory)
				{
					NetworkObject component = ((Component)array[i]).gameObject.GetComponent<NetworkObject>();
					if ((Object)(object)component != (Object)null && component.IsSpawned)
					{
						((Component)array[i]).gameObject.GetComponent<NetworkObject>().Despawn(true);
					}
					else
					{
						PersistentItemsPlugin.Log.LogInfo((object)("Error/warning: prop '" + ((Object)((Component)array[i]).gameObject).name + "' was not spawned or did not have a NetworkObject component! Skipped despawning and destroyed it instead."));
						Object.Destroy((Object)(object)((Component)array[i]).gameObject);
					}
				}
				else
				{
					array[i].scrapPersistedThroughRounds = true;
					PersistentItemsPlugin.scrap.Add(array[i]);
					PersistentItemsPlugin.Log.LogInfo((object)("prop '" + ((Object)((Component)array[i]).gameObject).name + "' has persisted."));
				}
				if (__instance.spawnedSyncedObjects.Contains(((Component)array[i]).gameObject))
				{
					__instance.spawnedSyncedObjects.Remove(((Component)array[i]).gameObject);
				}
			}
			GameObject[] array2 = GameObject.FindGameObjectsWithTag("TemporaryEffect");
			for (int j = 0; j < array2.Length; j++)
			{
				Object.Destroy((Object)(object)array2[j]);
			}
			return false;
		}
		return true;
	}
}