Decompiled source of Tacos Beds v1.0.1

TacosBeds.dll

Decompiled 14 minutes ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using HarmonyLib;
using Il2CppInterop.Runtime;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppScheduleOne.Employees;
using Il2CppScheduleOne.ObjectScripts;
using MelonLoader;
using Microsoft.CodeAnalysis;
using SharedHomes;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: MelonInfo(typeof(Mod), "Tacos Beds", "1.0.1", "Taco", null)]
[assembly: MelonGame("TVGS", "Schedule I")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("TacosBeds")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("TacosBeds")]
[assembly: AssemblyTitle("TacosBeds")]
[assembly: AssemblyVersion("1.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 SharedHomes
{
	public class Mod : MelonMod
	{
		private const string Taken = "Already assigned to";

		private static readonly HashSet<IntPtr> PendingRelabel = new HashSet<IntPtr>();

		private static readonly List<EmployeeHome> PendingHomes = new List<EmployeeHome>();

		private static bool _relabeling;

		private float _next;

		public override void OnInitializeMelon()
		{
			Patch(typeof(EmployeeHome), "IsBuildableEntityAValidEmployeeHome", "AllowTakenHome");
			Patch(typeof(BedItem), "IsBedValid", "AllowTakenHome");
			Patch(typeof(EmployeeHome), "SetAssignedEmployee", "AfterSetAssigned");
			((MelonBase)this).LoggerInstance.Msg("Ready: workers can share beds and lockers.");
		}

		private void Patch(Type type, string method, string postfix)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			try
			{
				((MelonBase)this).HarmonyInstance.Patch((MethodBase)AccessTools.Method(type, method, (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Mod), postfix, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			catch (Exception ex)
			{
				((MelonBase)this).LoggerInstance.Warning("Could not hook " + type.Name + "." + method + ": " + ex.Message);
			}
		}

		public static void AllowTakenHome(ref bool __result, ref string reason)
		{
			if (!__result && reason != null && reason.StartsWith("Already assigned to", StringComparison.Ordinal))
			{
				__result = true;
				reason = string.Empty;
			}
		}

		public static void AfterSetAssigned(EmployeeHome __instance, Employee employee)
		{
			if (!_relabeling && !((Object)(object)employee != (Object)null) && !((Object)(object)__instance == (Object)null) && PendingRelabel.Add(((Il2CppObjectBase)__instance).Pointer))
			{
				PendingHomes.Add(__instance);
			}
		}

		public override void OnUpdate()
		{
			if (PendingHomes.Count == 0 || Time.unscaledTime < _next)
			{
				return;
			}
			_next = Time.unscaledTime + 0.5f;
			List<EmployeeHome> list = new List<EmployeeHome>(PendingHomes);
			PendingHomes.Clear();
			PendingRelabel.Clear();
			try
			{
				Il2CppReferenceArray<Object> val = Object.FindObjectsOfType(Il2CppType.Of<Employee>());
				foreach (EmployeeHome item in list)
				{
					if ((Object)(object)item == (Object)null || (Object)(object)item.AssignedEmployee != (Object)null)
					{
						continue;
					}
					foreach (Object item2 in (Il2CppArrayBase<Object>)(object)val)
					{
						Employee val2 = ((item2 != null) ? ((Il2CppObjectBase)item2).TryCast<Employee>() : null);
						EmployeeHome val3 = null;
						try
						{
							val3 = ((val2 != null) ? val2.GetHome() : null);
						}
						catch
						{
						}
						if (!((Object)(object)val3 == (Object)null) && !(((Il2CppObjectBase)val3).Pointer != ((Il2CppObjectBase)item).Pointer))
						{
							_relabeling = true;
							try
							{
								item.SetAssignedEmployee(val2);
							}
							finally
							{
								_relabeling = false;
							}
							break;
						}
					}
				}
			}
			catch (Exception ex)
			{
				((MelonBase)this).LoggerInstance.Warning("relabel failed: " + ex.Message);
			}
		}
	}
}