Decompiled source of Clean Sweep v1.0.0

Mods/WoodStudios.NoMoreTrash.dll

Decompiled an hour ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using Il2CppFishNet;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppScheduleOne.DevUtilities;
using Il2CppScheduleOne.Trash;
using Il2CppSystem.Collections.Generic;
using MelonLoader;
using MelonLoader.Preferences;
using Microsoft.CodeAnalysis;
using NoMoreTrash;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(Core), "Clean Sweep", "1.0.0", "Wood Studios", null)]
[assembly: MelonGame("TVGS", "Schedule I")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("WoodStudios.NoMoreTrash")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("WoodStudios.NoMoreTrash")]
[assembly: AssemblyTitle("WoodStudios.NoMoreTrash")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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 NoMoreTrash
{
	public sealed class Core : MelonMod
	{
		internal static Instance Log;

		private static MelonPreferences_Entry<bool> _grow;

		private static MelonPreferences_Entry<bool> _street;

		private static MelonPreferences_Entry<bool> _bags;

		private static MelonPreferences_Entry<bool> _bins;

		private static MelonPreferences_Entry<float> _every;

		private static float _next;

		private static float _nextBins;

		private static float _nextReport;

		private static float _warnedAt = -100f;

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

		private static TrashManager _litterFrom;

		private static int _removed;

		private static int _emptied;

		private static readonly List<TrashItem> Doomed = new List<TrashItem>();

		private static TrashContainer[] _binCache;

		private static float _binsScanned = -100f;

		public override void OnInitializeMelon()
		{
			Log = ((MelonBase)this).LoggerInstance;
			MelonPreferences_Category obj = MelonPreferences.CreateCategory("CleanSweep", "Clean Sweep");
			_grow = obj.CreateEntry<bool>("GrowTrash", true, "Grow and work trash", "Empty seed vials, soil bags, bottles, leaves and the other trash your work leaves on the ground.", false, false, (ValueValidator)null, (string)null);
			_street = obj.CreateEntry<bool>("StreetLitter", true, "Street litter", "The cans, bottles and wrappers lying around town.", false, false, (ValueValidator)null, (string)null);
			_bags = obj.CreateEntry<bool>("TrashBags", true, "Trash bags", "Full trash bags left on the ground.", false, false, (ValueValidator)null, (string)null);
			_bins = obj.CreateEntry<bool>("EmptyBins", true, "Bins empty themselves", "Trash cans and bins never fill up.", false, false, (ValueValidator)null, (string)null);
			_every = obj.CreateEntry<float>("CheckSeconds", 1f, "How often (seconds)", "How often it sweeps for trash. 1 = trash vanishes almost as soon as it lands.", false, false, (ValueValidator)null, (string)null);
			Log.Msg("Clean Sweep loaded.");
		}

		public override void OnSceneWasLoaded(int buildIndex, string sceneName)
		{
			Litter.Clear();
			_litterFrom = null;
			_binCache = null;
			_binsScanned = -100f;
		}

		public override void OnUpdate()
		{
			if (Time.unscaledTime < _next)
			{
				return;
			}
			_next = Time.unscaledTime + Mathf.Clamp(_every?.Value ?? 1f, 0.25f, 60f);
			try
			{
				Sweep();
			}
			catch (Exception ex)
			{
				if (Time.unscaledTime - _warnedAt > 60f)
				{
					_warnedAt = Time.unscaledTime;
					Log.Warning("Trash sweep: " + ex.Message);
				}
			}
		}

		private static bool IsHost()
		{
			try
			{
				return !InstanceFinder.IsClient || InstanceFinder.IsServer;
			}
			catch
			{
				return true;
			}
		}

		private static void Sweep()
		{
			bool flag = _grow?.Value ?? true;
			bool flag2 = _street?.Value ?? true;
			bool flag3 = _bags?.Value ?? true;
			bool flag4 = _bins?.Value ?? true;
			if (!flag && !flag2 && !flag3 && !flag4)
			{
				return;
			}
			TrashManager val = null;
			try
			{
				val = NetworkSingleton<TrashManager>.Instance;
			}
			catch
			{
			}
			if ((Object)(object)val == (Object)null || !IsHost())
			{
				return;
			}
			if ((Object)(object)_litterFrom != (Object)(object)val)
			{
				_litterFrom = val;
				Litter.Clear();
				try
				{
					Il2CppReferenceArray<TrashItemData> generateableTrashItems = val.GenerateableTrashItems;
					if (generateableTrashItems != null)
					{
						for (int i = 0; i < ((Il2CppArrayBase<TrashItemData>)(object)generateableTrashItems).Length; i++)
						{
							TrashItemData val2 = ((Il2CppArrayBase<TrashItemData>)(object)generateableTrashItems)[i];
							if (val2 != null && (Object)(object)val2.Item != (Object)null && !string.IsNullOrEmpty(val2.Item.ID))
							{
								Litter.Add(val2.Item.ID);
							}
						}
					}
				}
				catch
				{
				}
			}
			if (flag || flag2 || flag3)
			{
				List<TrashItem> trashItems = val._trashItems;
				if (trashItems != null && trashItems.Count > 0)
				{
					string text = null;
					try
					{
						text = (((Object)(object)val.TrashBagPrefab != (Object)null) ? val.TrashBagPrefab.ID : null);
					}
					catch
					{
					}
					Doomed.Clear();
					for (int j = 0; j < trashItems.Count; j++)
					{
						TrashItem val3 = trashItems[j];
						if ((Object)(object)val3 == (Object)null)
						{
							continue;
						}
						try
						{
							if (!val3.IsBeingDragged())
							{
								string text2 = val3.ID ?? "";
								if (((text != null && text2 == text) || (Object)(object)((Il2CppObjectBase)val3).TryCast<TrashBag>() != (Object)null) ? flag3 : (Litter.Contains(text2) ? flag2 : flag))
								{
									Doomed.Add(val3);
								}
							}
						}
						catch
						{
						}
					}
					foreach (TrashItem item in Doomed)
					{
						try
						{
							item.DestroyTrash();
							_removed++;
						}
						catch
						{
						}
					}
					Doomed.Clear();
				}
			}
			if (flag4 && Time.unscaledTime >= _nextBins)
			{
				_nextBins = Time.unscaledTime + 5f;
				EmptyBins();
			}
			if ((_removed > 0 || _emptied > 0) && Time.unscaledTime >= _nextReport)
			{
				_nextReport = Time.unscaledTime + 300f;
				Log.Msg($"Cleared {_removed} piece(s) of trash{((_emptied > 0) ? $" and emptied {_emptied} bin(s)" : "")}.");
				_removed = 0;
				_emptied = 0;
			}
		}

		private static void EmptyBins()
		{
			if (_binCache == null || Time.unscaledTime - _binsScanned > 30f)
			{
				_binsScanned = Time.unscaledTime;
				try
				{
					_binCache = Il2CppArrayBase<TrashContainer>.op_Implicit(Object.FindObjectsOfType<TrashContainer>());
				}
				catch
				{
					_binCache = null;
				}
			}
			if (_binCache == null)
			{
				return;
			}
			TrashContainer[] binCache = _binCache;
			foreach (TrashContainer val in binCache)
			{
				try
				{
					if (!((Object)(object)val == (Object)null) && val.TrashLevel > 0)
					{
						val.SendClear();
						_emptied++;
					}
				}
				catch
				{
				}
			}
		}
	}
}