Decompiled source of CitizenContact v1.1.0

CitizenContact.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text.Json;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime.Injection;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppSystem.Collections.Generic;
using Microsoft.CodeAnalysis;
using SOD.Common;
using SOD.Common.Helpers;
using SOD.Common.Helpers.DialogObjects;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("CitizenContact")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+f4f42d38a2ab82dce8d7b935c5ffede589440c5b")]
[assembly: AssemblyProduct("CitizenContact")]
[assembly: AssemblyTitle("CitizenContact")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 CitizenContact
{
	public sealed class Visit
	{
		internal string Opening = "";

		internal int Priority;

		internal bool Run;

		internal bool Talk = true;

		internal float Reach = 3f;

		internal float Linger;

		internal float SpeakAt = -1f;

		internal float CheckSpeechAt = -1f;

		internal float LingerUntil = -1f;

		internal float GiveUpAt;

		internal NewAIGoal? Goal;

		internal IntPtr SentTo;

		internal IntPtr SentKey;

		internal int Lost;

		internal Action<Visit>? OnTalk;

		internal Action<Visit>? OnGaveUp;

		public Human Who { get; internal set; }

		public string Label { get; internal set; } = "";

		public bool Talking { get; internal set; }

		public bool Over { get; internal set; }

		public string? GaveUp { get; internal set; }
	}
	public static class Approach
	{
		private const string GoalName = "CitizenContactApproach";

		private const float ReachRadius = 3f;

		private const float ReachHeight = 2.5f;

		private const float LingerGap = 1.6f;

		private const float SpeakAfterCalm = 1.5f;

		private static Visit? _current;

		private static AIGoalPreset? _preset;

		private static bool _presetFailed;

		public static bool Busy => _current != null;

		internal static void Install()
		{
			Lib.SaveGame.OnBeforeLoad += delegate
			{
				Cancel("loading a save");
			};
			Lib.SaveGame.OnAfterNewGame += delegate
			{
				_current = null;
			};
		}

		public static bool CameToTalk(Human? who)
		{
			if ((Object)(object)who != (Object)null)
			{
				Visit current = _current;
				if (current != null && current.Talking)
				{
					return current.Who.humanID == who.humanID;
				}
			}
			return false;
		}

		public static Visit? Start(Human who, string opening, string label, Action<Visit>? onTalk = null, Action<Visit>? onGaveUp = null, float giveUpHours = 2f, int priority = 10, bool run = false, bool talk = true, float reach = 3f, float lingerSeconds = 0f)
		{
			//IL_0197: Unknown result type (might be due to invalid IL or missing references)
			//IL_019e: Expected O, but got Unknown
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: Expected O, but got Unknown
			bool flag = default(bool);
			try
			{
				if ((Object)(object)((who != null) ? ((Actor)who).ai : null) == (Object)null || ((Actor)who).isPlayer)
				{
					return Fail(label, "nobody to send");
				}
				if (_current != null)
				{
					return Fail(label, _current.Who.GetCitizenName() + " is already on the way");
				}
				if (((Actor)who).isDead || ((Actor)who).isAsleep || ((Actor)who).ai.ko)
				{
					return Fail(label, who.GetCitizenName() + " can't come now");
				}
				if ((SoCustomComparison)(object)Preset() == (SoCustomComparison)null)
				{
					return Fail(label, "no goal to send them with");
				}
				_current = new Visit
				{
					Who = who,
					Label = label,
					Opening = opening,
					Priority = priority,
					Run = run,
					Talk = talk,
					Reach = Mathf.Max(1f, reach),
					Linger = Mathf.Max(0f, lingerSeconds),
					GiveUpAt = SessionData.Instance.gameTime + Mathf.Max(0.1f, giveUpHours),
					OnTalk = onTalk,
					OnGaveUp = onGaveUp
				};
				ManualLogSource log = Plugin.Log;
				BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(39, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[approach] ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(who.GetCitizenName());
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" is coming to talk to you (");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(label);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(")");
				}
				log.LogInfo(val);
				return _current;
			}
			catch (Exception ex)
			{
				ManualLogSource log2 = Plugin.Log;
				BepInExWarningLogInterpolatedStringHandler val2 = new BepInExWarningLogInterpolatedStringHandler(31, 3, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[approach] could not send ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>((who != null) ? who.GetCitizenName() : null);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" (");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(label);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("): ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(ex.Message);
				}
				log2.LogWarning(val2);
				_current = null;
				return null;
			}
		}

		public static void Cancel(Visit visit, string why = "called off")
		{
			if (_current == visit)
			{
				Cancel(why);
			}
		}

		private static void Cancel(string why)
		{
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			Visit current = _current;
			if (current == null)
			{
				return;
			}
			_current = null;
			DropGoal(current);
			if (current.Over)
			{
				return;
			}
			current.Over = true;
			if (!current.Talking)
			{
				current.GaveUp = why;
				ManualLogSource log = Plugin.Log;
				bool flag = default(bool);
				BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(39, 3, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[approach] ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(current.Who.GetCitizenName());
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" isn't coming after all: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(why);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" (");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(current.Label);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(")");
				}
				log.LogInfo(val);
				Notify(current.OnGaveUp, current);
			}
		}

		private static Visit? Fail(string label, string why)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			if (Plugin.Cfg.DebugLogging.Value)
			{
				ManualLogSource log = Plugin.Log;
				bool flag = default(bool);
				BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(24, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[approach] no visit (");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(label);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("): ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(why);
				}
				log.LogInfo(val);
			}
			return null;
		}

		private static void Notify(Action<Visit>? handler, Visit v)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected O, but got Unknown
			if (handler == null)
			{
				return;
			}
			try
			{
				handler(v);
			}
			catch (Exception ex)
			{
				ManualLogSource log = Plugin.Log;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(30, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[approach] ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(v.Label);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": callback failed: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				log.LogError(val);
			}
		}

		internal static void Tick()
		{
			//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b8: Expected O, but got Unknown
			//IL_0133: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Expected O, but got Unknown
			//IL_02c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cb: Expected O, but got Unknown
			Visit current = _current;
			if (current == null)
			{
				return;
			}
			Human who = current.Who;
			Player instance = Player.Instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			if (current.LingerUntil >= 0f)
			{
				if (Time.unscaledTime < current.LingerUntil && !((Actor)who).isDead)
				{
					if (current.Goal != null && Reached(who, instance, 1.6f))
					{
						DropGoal(current);
					}
					else if (current.Goal == null && !Reached(who, instance, current.Reach + 1f))
					{
						KeepGoing(current, instance);
					}
				}
				else
				{
					DropGoal(current);
					_current = null;
				}
			}
			else if (current.Talking)
			{
				if (current.SpeakAt >= 0f && (instance.transitionActive || instance.spendingTimeMode))
				{
					current.SpeakAt = Time.unscaledTime + 1.5f;
				}
				bool flag = default(bool);
				if (current.SpeakAt >= 0f && Time.unscaledTime >= current.SpeakAt)
				{
					current.SpeakAt = -1f;
					Citizen val = ((Il2CppObjectBase)who).TryCast<Citizen>();
					if ((Object)(object)val != (Object)null)
					{
						Lib.Dialogs.Speak(val, current.Opening, false);
					}
					current.CheckSpeechAt = Time.unscaledTime + 1f;
					ManualLogSource log = Plugin.Log;
					BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(18, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[approach] ");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(who.GetCitizenName());
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" says: ");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(current.Opening);
					}
					log.LogInfo(val2);
				}
				else if (current.CheckSpeechAt >= 0f && Time.unscaledTime >= current.CheckSpeechAt)
				{
					current.CheckSpeechAt = -1f;
					ManualLogSource log2 = Plugin.Log;
					BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(34, 3, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[approach] ");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(who.GetCitizenName());
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" speech active ");
						BepInExInfoLogInterpolatedStringHandler obj = val2;
						SpeechController speechController = ((Actor)who).speechController;
						((BepInExLogInterpolatedStringHandler)obj).AppendFormatted<bool?>((speechController != null) ? new bool?(speechController.speechActive) : ((bool?)null));
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(", ");
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("queue ");
						BepInExInfoLogInterpolatedStringHandler obj2 = val2;
						SpeechController speechController2 = ((Actor)who).speechController;
						((BepInExLogInterpolatedStringHandler)obj2).AppendFormatted<int>(((speechController2 == null) ? ((int?)null) : speechController2.speechQueue?.Count) ?? (-1));
					}
					log2.LogInfo(val2);
				}
				if (((Actor)who).inConversation)
				{
					return;
				}
				InteractionController instance2 = InteractionController.Instance;
				if (instance2 != null && instance2.dialogMode)
				{
					return;
				}
				_current = null;
				current.Over = true;
				if (Plugin.Cfg.DebugLogging.Value)
				{
					ManualLogSource log3 = Plugin.Log;
					BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(29, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[approach] talk with ");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(who.GetCitizenName());
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" over (");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(current.Label);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(")");
					}
					log3.LogInfo(val2);
				}
			}
			else if (((Actor)who).isDead)
			{
				Cancel("died");
			}
			else if ((Object)(object)((Actor)who).ai == (Object)null || ((Actor)who).ai.ko)
			{
				Cancel("knocked out");
			}
			else if (SessionData.Instance.gameTime > current.GiveUpAt)
			{
				Cancel("couldn't reach you in time");
			}
			else if (Reached(who, instance, current.Reach))
			{
				if (!current.Talk)
				{
					Arrive(current);
				}
				else if (CanTalk(who, instance))
				{
					Talk(current, instance);
				}
			}
			else
			{
				KeepGoing(current, instance);
			}
		}

		private static bool Reached(Human who, Player player, float reach)
		{
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: 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_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			bool num;
			if (!((Object)(object)((Actor)who).currentBuilding != (Object)null) || !((Object)(object)((Actor)player).currentBuilding != (Object)null))
			{
				if (!((Object)(object)((Actor)who).currentBuilding == (Object)null))
				{
					goto IL_0058;
				}
				num = (Object)(object)((Actor)player).currentBuilding == (Object)null;
			}
			else
			{
				num = ((Il2CppObjectBase)((Actor)who).currentBuilding).Pointer == ((Il2CppObjectBase)((Actor)player).currentBuilding).Pointer;
			}
			if (num)
			{
				Vector3 position = ((Component)who).transform.position;
				Vector3 position2 = ((Component)player).transform.position;
				if (Mathf.Abs(position.y - position2.y) < 2.5f)
				{
					return Vector2.Distance(new Vector2(position.x, position.z), new Vector2(position2.x, position2.z)) < reach;
				}
				return false;
			}
			goto IL_0058;
			IL_0058:
			return false;
		}

		private static bool CanTalk(Human who, Player player)
		{
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Invalid comparison between Unknown and I4
			NewAIController ai = ((Actor)who).ai;
			InteractionController instance = InteractionController.Instance;
			if ((instance == null || !instance.dialogMode) && ((Actor)player).interactingWith == null && !((Actor)player).isHiding && !player.transitionActive && !player.playerKOInProgress && !((Actor)who).inConversation && !((Actor)who).isAsleep && !((Actor)who).isStunned && (Object)(object)ai != (Object)null && !ai.inCombat && !ai.inFleeState && !ai.ko)
			{
				return (int)ai.reactionState == 0;
			}
			return false;
		}

		private static void Arrive(Visit v)
		{
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Expected O, but got Unknown
			//IL_004b: 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)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			v.Over = true;
			if (v.Linger > 0f)
			{
				v.LingerUntil = Time.unscaledTime + v.Linger;
			}
			else
			{
				DropGoal(v);
				_current = null;
			}
			string text = "";
			try
			{
				Player instance = Player.Instance;
				Vector3 position = ((Component)v.Who).transform.position;
				Vector3 position2 = ((Component)instance).transform.position;
				bool flag = (Object)(object)((Actor)v.Who).currentRoom != (Object)null && (Object)(object)((Actor)instance).currentRoom != (Object)null && ((Il2CppObjectBase)((Actor)v.Who).currentRoom).Pointer == ((Il2CppObjectBase)((Actor)instance).currentRoom).Pointer;
				DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(9, 2);
				defaultInterpolatedStringHandler.AppendLiteral(" at ");
				defaultInterpolatedStringHandler.AppendFormatted(Vector3.Distance(position, position2), "0.0");
				defaultInterpolatedStringHandler.AppendLiteral(" m (");
				object value;
				if (!flag)
				{
					NewRoom currentRoom = ((Actor)v.Who).currentRoom;
					string obj = ((currentRoom != null) ? currentRoom.name : null) ?? "?";
					NewRoom currentRoom2 = ((Actor)instance).currentRoom;
					value = "in " + obj + ", you in " + (((currentRoom2 != null) ? currentRoom2.name : null) ?? "?");
				}
				else
				{
					value = "same room";
				}
				defaultInterpolatedStringHandler.AppendFormatted((string?)value);
				defaultInterpolatedStringHandler.AppendLiteral(")");
				text = defaultInterpolatedStringHandler.ToStringAndClear();
			}
			catch
			{
			}
			ManualLogSource log = Plugin.Log;
			bool flag2 = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(29, 4, ref flag2);
			if (flag2)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[approach] ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(v.Who.GetCitizenName());
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" came up to you");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(text);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" (");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(v.Label);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(")");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>((v.Linger > 0f) ? $", stays {v.Linger:0}s" : "");
			}
			log.LogInfo(val);
			Notify(v.OnTalk, v);
		}

		private static void Talk(Visit v, Player player)
		{
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_011f: Expected O, but got Unknown
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bd: Expected O, but got Unknown
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Expected O, but got Unknown
			Human who = v.Who;
			DropGoal(v);
			bool flag = default(bool);
			try
			{
				((Actor)who).ai.TalkTo((ConversationType)0);
				BepInExInfoLogInterpolatedStringHandler val;
				if (!((Actor)who).inConversation)
				{
					InteractionController instance = InteractionController.Instance;
					if (instance == null || !instance.dialogMode)
					{
						if (Plugin.Cfg.DebugLogging.Value)
						{
							ManualLogSource log = Plugin.Log;
							val = new BepInExInfoLogInterpolatedStringHandler(38, 1, ref flag);
							if (flag)
							{
								((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[approach] ");
								((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(who.GetCitizenName());
								((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" couldn't start talking yet");
							}
							log.LogInfo(val);
						}
						return;
					}
				}
				v.Talking = true;
				if (!string.IsNullOrWhiteSpace(v.Opening))
				{
					v.SpeakAt = Time.unscaledTime + 1.5f;
				}
				ManualLogSource log2 = Plugin.Log;
				val = new BepInExInfoLogInterpolatedStringHandler(29, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[approach] ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(who.GetCitizenName());
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" came up to you (");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(v.Label);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(")");
				}
				log2.LogInfo(val);
				Notify(v.OnTalk, v);
			}
			catch (Exception ex)
			{
				ManualLogSource log3 = Plugin.Log;
				BepInExWarningLogInterpolatedStringHandler val2 = new BepInExWarningLogInterpolatedStringHandler(37, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[approach] ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(who.GetCitizenName());
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" could not start talking: ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(ex.Message);
				}
				log3.LogWarning(val2);
			}
		}

		private static AIGoalPreset? Preset()
		{
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Expected O, but got Unknown
			if ((SoCustomComparison)(object)_preset != (SoCustomComparison)null)
			{
				return _preset;
			}
			if (_presetFailed)
			{
				return null;
			}
			try
			{
				RoutineControls instance = RoutineControls.Instance;
				AIGoalPreset val = ((instance != null) ? instance.enforcerResponse : null);
				if ((SoCustomComparison)(object)val == (SoCustomComparison)null)
				{
					return null;
				}
				AIGoalPreset obj = Object.Instantiate<AIGoalPreset>(val);
				((Object)obj).name = "CitizenContactApproach";
				((SoCustomComparison)obj).presetName = "CitizenContactApproach";
				obj.disableSave = true;
				obj.forcePriorityUpdateOnCreation = true;
				((Object)obj).hideFlags = (HideFlags)32;
				Peaceful(obj);
				_preset = obj;
				return obj;
			}
			catch (Exception ex)
			{
				_presetFailed = true;
				ManualLogSource log = Plugin.Log;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val2 = new BepInExErrorLogInterpolatedStringHandler(45, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[approach] could not make the approach goal: ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<Exception>(ex);
				}
				log.LogError(val2);
				return null;
			}
		}

		private static void Peaceful(AIGoalPreset goal)
		{
			//IL_0118: Unknown result type (might be due to invalid IL or missing references)
			//IL_011f: Expected O, but got Unknown
			goal.overrideLightingBehaviour = false;
			goal.raiseAlarm = false;
			List<GoalActionSetup> actionsSetup = goal.actionsSetup;
			if (actionsSetup == null)
			{
				return;
			}
			List<string> list = new List<string>();
			Enumerator<GoalActionSetup> enumerator = actionsSetup.GetEnumerator();
			while (enumerator.MoveNext())
			{
				GoalActionSetup current = enumerator.Current;
				List<AIActionPreset> val = ((current != null) ? current.actions : null);
				if (val == null)
				{
					continue;
				}
				for (int i = 0; i < val.Count; i++)
				{
					AIActionPreset val2 = val[i];
					if (!((SoCustomComparison)(object)val2 == (SoCustomComparison)null))
					{
						AIActionPreset val3 = Object.Instantiate<AIActionPreset>(val2);
						((Object)val3).name = ((Object)val2).name;
						((Object)val3).hideFlags = (HideFlags)32;
						val3.useCombatPose = (CombatPose)2;
						val3.turnAllGamelocationLightsOff = false;
						val3.overrideGoalLightRule = false;
						bool flag = ((Object)val2).name.IndexOf("Search", StringComparison.OrdinalIgnoreCase) >= 0;
						if (flag)
						{
							val3.actionLocation = (ActionLocation)15;
						}
						val[i] = val3;
						list.Add(flag ? (((Object)val2).name + " -> player") : ((Object)val2).name);
					}
				}
			}
			ManualLogSource log = Plugin.Log;
			bool flag2 = default(bool);
			BepInExInfoLogInterpolatedStringHandler val4 = new BepInExInfoLogInterpolatedStringHandler(40, 1, ref flag2);
			if (flag2)
			{
				((BepInExLogInterpolatedStringHandler)val4).AppendLiteral("[approach] approach goal made peaceful: ");
				((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(string.Join(", ", list));
			}
			log.LogInfo(val4);
		}

		private static IntPtr KeyOf(NewGameLocation loc)
		{
			if (!((Object)(object)loc.building != (Object)null))
			{
				return ((Il2CppObjectBase)loc).Pointer;
			}
			return ((Il2CppObjectBase)loc.building).Pointer;
		}

		private static bool IsTransient(NewGameLocation? loc)
		{
			NewAddress val = ((loc != null) ? loc.thisAsAddress : null);
			if ((Object)(object)val != (Object)null && (Object)(object)val.residence == (Object)null)
			{
				return val.company == null;
			}
			return false;
		}

		private static void KeepGoing(Visit v, Player player)
		{
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_018d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0194: Expected O, but got Unknown
			NewGameLocation currentGameLocation = ((Actor)player).currentGameLocation;
			NewAIController ai = ((Actor)v.Who).ai;
			if ((Object)(object)currentGameLocation == (Object)null || (Object)(object)ai == (Object)null)
			{
				return;
			}
			AIGoalPreset val = Preset();
			if ((SoCustomComparison)(object)val == (SoCustomComparison)null)
			{
				return;
			}
			bool num = v.Goal == null || !HasGoal(ai, v.Goal) || KeyOf(currentGameLocation) != v.SentKey || (((Il2CppObjectBase)currentGameLocation).Pointer != v.SentTo && !IsTransient(currentGameLocation));
			if (((Actor)v.Who).escalationLevel > 0 && (int)ai.reactionState == 0 && !ai.persuit)
			{
				((Actor)v.Who).SetEscalation(0);
			}
			if (!num)
			{
				if (v.Run)
				{
					ForceRun(v.Goal);
				}
				return;
			}
			if (v.Goal != null && !HasGoal(ai, v.Goal) && ++v.Lost > 20)
			{
				Cancel("kept dropping the way to you");
				return;
			}
			DropGoal(v);
			val.basePriority = v.Priority;
			val.minMaxPriority = new Vector2((float)v.Priority, (float)v.Priority);
			NewAIGoal val2 = ai.CreateNewGoal(val, 0f, 0f, (NewNode)null, (Interactable)null, currentGameLocation, (SocialGroup)null, (Murder)null, -2);
			if (val2 == null)
			{
				return;
			}
			v.Goal = val2;
			v.SentTo = ((Il2CppObjectBase)currentGameLocation).Pointer;
			v.SentKey = KeyOf(currentGameLocation);
			if (v.Run)
			{
				ForceRun(val2);
			}
			if (!Plugin.Cfg.DebugLogging.Value)
			{
				return;
			}
			ManualLogSource log = Plugin.Log;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val3 = new BepInExInfoLogInterpolatedStringHandler(41, 4, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("[approach] ");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(v.Who.GetCitizenName());
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(" heading to ");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(((Object)currentGameLocation).name);
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(" ");
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("(goal ");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<float>(val2.priority, "0.#");
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(", current ");
				NewAIGoal currentGoal = ai.currentGoal;
				object obj;
				if (currentGoal == null)
				{
					obj = null;
				}
				else
				{
					AIGoalPreset preset = currentGoal.preset;
					obj = ((preset != null) ? ((Object)preset).name : null);
				}
				if (obj == null)
				{
					obj = "none";
				}
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>((string)obj);
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(")");
			}
			log.LogInfo(val3);
		}

		private static void ForceRun(NewAIGoal goal)
		{
			List<NewAIAction> actions = goal.actions;
			if (actions == null)
			{
				return;
			}
			Enumerator<NewAIAction> enumerator = actions.GetEnumerator();
			while (enumerator.MoveNext())
			{
				NewAIAction current = enumerator.Current;
				if (current != null && !current.forceRun)
				{
					current.forceRun = true;
				}
			}
		}

		private static bool HasGoal(NewAIController ai, NewAIGoal goal)
		{
			List<NewAIGoal> goals = ai.goals;
			if (goals == null)
			{
				return false;
			}
			Enumerator<NewAIGoal> enumerator = goals.GetEnumerator();
			while (enumerator.MoveNext())
			{
				NewAIGoal current = enumerator.Current;
				if (current != null && ((Il2CppObjectBase)current).Pointer == ((Il2CppObjectBase)goal).Pointer)
				{
					return true;
				}
			}
			return false;
		}

		private static void DropGoal(Visit v)
		{
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Expected O, but got Unknown
			NewAIGoal goal = v.Goal;
			v.Goal = null;
			try
			{
				if (goal != null && (Object)(object)((Actor)v.Who).ai != (Object)null && HasGoal(((Actor)v.Who).ai, goal))
				{
					goal.Remove();
				}
			}
			catch (Exception ex)
			{
				ManualLogSource log = Plugin.Log;
				bool flag = default(bool);
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(36, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[approach] could not drop the goal: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
				}
				log.LogWarning(val);
			}
		}
	}
	public static class Dds
	{
		public static bool Ready
		{
			get
			{
				Toolbox instance = Toolbox.Instance;
				return ((instance != null) ? instance.allDDSTrees : null) != null;
			}
		}

		public static void AddBlock(string id, string name, string text)
		{
			Toolbox instance = Toolbox.Instance;
			if (!((Object)(object)instance == (Object)null))
			{
				if (!instance.allDDSBlocks.ContainsKey(id))
				{
					instance.allDDSBlocks[id] = JsonUtility.FromJson<DDSBlockSave>("{\"name\":\"" + name + "\",\"id\":\"" + id + "\",\"replacements\":[]}");
				}
				Lib.DdsStrings.AddOrUpdate("dds.blocks", id, text);
			}
		}

		public static void AddMessage(string id, string name, IEnumerable<(string BlockId, string InstanceId)> blocks)
		{
			Toolbox instance = Toolbox.Instance;
			if (!((Object)(object)instance == (Object)null) && !instance.allDDSMessages.ContainsKey(id))
			{
				string text = "{\"name\":\"" + name + "\",\"id\":\"" + id + "\",\"blocks\":[" + string.Join(",", blocks.Select<(string, string), string>(((string BlockId, string InstanceId) b) => BlockCondition(b.BlockId, b.InstanceId))) + "]}";
				instance.allDDSMessages[id] = JsonUtility.FromJson<DDSMessageSave>(text);
			}
		}

		public static void AddVmailTree(string id, string name, string messageId, string instanceId)
		{
			Toolbox instance = Toolbox.Instance;
			if (!((Object)(object)instance == (Object)null) && !instance.allDDSTrees.ContainsKey(id))
			{
				DDSTreeSave val = JsonUtility.FromJson<DDSTreeSave>(TreeJson(id, name, messageId, instanceId));
				val.messageRef = new Dictionary<string, DDSMessageSettings>();
				Enumerator<DDSMessageSettings> enumerator = val.messages.GetEnumerator();
				while (enumerator.MoveNext())
				{
					DDSMessageSettings current = enumerator.Current;
					val.messageRef[current.instanceID] = current;
				}
				instance.allDDSTrees[id] = val;
			}
		}

		public static bool HasTree(string id)
		{
			Toolbox instance = Toolbox.Instance;
			if (instance == null)
			{
				return false;
			}
			return instance.allDDSTrees?.ContainsKey(id) == true;
		}

		private static string BlockCondition(string blockId, string instanceId)
		{
			return "{\"blockID\":\"" + blockId + "\",\"instanceID\":\"" + instanceId + "\",\"alwaysDisplay\":true,\"group\":0,\"forceScope\":0,\"useTraits\":false,\"traitConditions\":0,\"traits\":[]}";
		}

		private static string Participant(bool required)
		{
			return "{\"required\":" + (required ? "true" : "false") + ",\"connection\":15,\"useJobs\":false,\"disableInbox\":false,\"jobs\":[],\"useTraits\":false,\"traits\":[],\"traitConditions\":0,\"triggers\":[]}";
		}

		private static string TreeJson(string id, string name, string messageId, string instanceId)
		{
			return "{\"name\":\"" + name + "\",\"id\":\"" + id + "\",\"participantA\":" + Participant(required: true) + ",\"participantB\":" + Participant(required: true) + ",\"participantC\":" + Participant(required: false) + ",\"participantD\":" + Participant(required: false) + ",\"repeat\":1,\"triggerPoint\":3,\"messages\":[{\"msgID\":\"" + messageId + "\",\"elementName\":\"\",\"instanceID\":\"" + instanceId + "\",\"saidBy\":1,\"saidTo\":0,\"pos\":{\"x\":0.0,\"y\":-64.0},\"size\":{\"x\":320.0,\"y\":300.0},\"rot\":0.0,\"font\":\"Halogen\",\"col\":{\"r\":0.0,\"g\":0.0,\"b\":0.0,\"a\":1.0},\"fontSize\":22.0,\"charSpace\":0.0,\"wordSpace\":0.0,\"lineSpace\":16.0,\"paraSpace\":0.0,\"alignH\":0,\"alignV\":0,\"fontStyle\":0,\"order\":0,\"usePages\":false,\"isHandwriting\":false,\"links\":[]}],\"stopMovement\":true,\"ignoreGlobalRepeat\":false,\"treeType\":1,\"document\":{\"background\":\"\",\"fill\":1,\"size\":{\"x\":0.0,\"y\":0.0},\"colour\":{\"r\":1.0,\"g\":1.0,\"b\":1.0,\"a\":1.0}},\"startingMessage\":\"" + instanceId + "\",\"treeChance\":0.0,\"priority\":3,\"newspaperCategory\":0,\"newspaperContext\":0}";
		}
	}
	public readonly record struct PhonePlace(NewAddress Address, string Name, bool OnlyIfThere = false);
	public sealed class Call
	{
		internal PhoneCall Game;

		internal Func<Call, string> Answer;

		internal Action<Call>? OnAnswered;

		internal Action<Call>? OnMissed;

		internal float GiveUpAt;

		public Human Caller { get; internal set; }

		public string Where { get; internal set; } = "";

		public string Label { get; internal set; } = "";

		public bool Answered { get; internal set; }

		public bool Over { get; internal set; }

		public string? Missed { get; internal set; }
	}
	public static class Phone
	{
		private sealed class NeverShown : IDialogLogic
		{
			public bool IsDialogShown(DialogPreset preset, Citizen saysTo, SideJob jobRef)
			{
				return false;
			}

			public ForceSuccess ShouldDialogSucceedOverride(DialogController instance, DialogOption dialog, Citizen saysTo, NewNode where, Actor saidBy)
			{
				return (ForceSuccess)1;
			}

			public void OnDialogExecute(DialogController instance, Citizen saysTo, Interactable saysToInteractable, NewNode where, Actor saidBy, bool success, NewRoom roomRef, SideJob jobRef)
			{
			}
		}

		[HarmonyPatch(typeof(DialogController), "ExecuteDialog")]
		internal static class Guard
		{
			[HarmonyPrefix]
			[HarmonyPriority(800)]
			public static void Prefix(DialogOption dialog, ref ForceSuccess forceSuccess)
			{
				//IL_0063: Unknown result type (might be due to invalid IL or missing references)
				//IL_0069: Expected O, but got Unknown
				try
				{
					object obj;
					if (dialog == null)
					{
						obj = null;
					}
					else
					{
						DialogPreset preset = dialog.preset;
						obj = ((preset != null) ? ((Object)preset).name : null);
					}
					string text = (string)obj;
					if (text != null)
					{
						DialogObject? greeting = _greeting;
						if (!((SoCustomComparison)(object)((greeting != null) ? greeting.Preset : null) == (SoCustomComparison)null) && !(text != ((Object)_greeting.Preset).name))
						{
							forceSuccess = (ForceSuccess)1;
							OnGreeting();
						}
					}
				}
				catch (Exception ex)
				{
					ManualLogSource log = Plugin.Log;
					bool flag = default(bool);
					BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(22, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[phone] dialog guard: ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
					}
					log.LogWarning(val);
				}
			}
		}

		private static DialogObject? _greeting;

		private static Call? _current;

		private static readonly List<Func<string?>> OrphanAnswers = new List<Func<string>>();

		public static bool Busy => _current != null;

		internal static void Install()
		{
			_greeting = Lib.Dialogs.Builder("cc_phone_greeting").SetText("Hello?").SetDialogLogic((IDialogLogic)(object)new NeverShown())
				.AddResponse("...", true, true)
				.CreateAndRegister();
			Lib.SaveGame.OnAfterLoad += delegate
			{
				_current = null;
			};
			Lib.SaveGame.OnAfterNewGame += delegate
			{
				_current = null;
			};
		}

		public static void AddLostCallAnswer(Func<string?> answer)
		{
			OrphanAnswers.Add(answer);
		}

		public static Call? Ring(Human caller, IEnumerable<PhonePlace> places, Func<Call, string> answer, string label, Action<Call>? onAnswered = null, Action<Call>? onMissed = null, float ringHours = 0.1f, Telephone? from = null)
		{
			//IL_021b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0222: Expected O, but got Unknown
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Invalid comparison between Unknown and I4
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Expected O, but got Unknown
			//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ae: Expected O, but got Unknown
			bool flag = default(bool);
			try
			{
				Player instance = Player.Instance;
				TelephoneController instance2 = TelephoneController.Instance;
				if (!((Object)(object)caller == (Object)null) && !((Object)(object)instance == (Object)null) && !((Object)(object)instance2 == (Object)null))
				{
					DialogObject? greeting = _greeting;
					if (!((SoCustomComparison)(object)((greeting != null) ? greeting.Preset : null) == (SoCustomComparison)null))
					{
						if (_current != null)
						{
							return Fail(label, _current.Caller.GetCitizenName() + " is already ringing");
						}
						if (instance.activeCall != null && (int)instance.activeCall.state != 4)
						{
							return Fail(label, "you're on the phone");
						}
						string where;
						Telephone val = PlayerPhone(instance, places, out where);
						if (val == null)
						{
							return Fail(label, "no phone where they know to find you");
						}
						if (from == null)
						{
							from = CallerPhone(caller, val);
						}
						if (from == null || ((Il2CppObjectBase)from).Pointer == ((Il2CppObjectBase)val).Pointer)
						{
							return Fail(label, "no phone to call from");
						}
						CallSource val2 = new CallSource((CallType)2, _greeting.Preset, (ConversationType)0);
						float num = Mathf.Max(0.02f, ringHours);
						PhoneCall val3 = instance2.CreateNewCall(from, val, caller, (Human)(object)instance, val2, num, true);
						if (val3 == null)
						{
							return Fail(label, "the game refused the call");
						}
						_current = new Call
						{
							Caller = caller,
							Where = where,
							Label = label,
							Game = val3,
							Answer = answer,
							OnAnswered = onAnswered,
							OnMissed = onMissed,
							GiveUpAt = SessionData.Instance.gameTime + num + 0.25f
						};
						ManualLogSource log = Plugin.Log;
						BepInExInfoLogInterpolatedStringHandler val4 = new BepInExInfoLogInterpolatedStringHandler(30, 3, ref flag);
						if (flag)
						{
							((BepInExLogInterpolatedStringHandler)val4).AppendLiteral("[phone] ");
							((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(caller.GetCitizenName());
							((BepInExLogInterpolatedStringHandler)val4).AppendLiteral(" is ringing you at ");
							((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(where);
							((BepInExLogInterpolatedStringHandler)val4).AppendLiteral(" (");
							((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(label);
							((BepInExLogInterpolatedStringHandler)val4).AppendLiteral(")");
						}
						log.LogInfo(val4);
						return _current;
					}
				}
				return Fail(label, "no phone system yet");
			}
			catch (Exception ex)
			{
				ManualLogSource log2 = Plugin.Log;
				BepInExWarningLogInterpolatedStringHandler val5 = new BepInExWarningLogInterpolatedStringHandler(31, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val5).AppendLiteral("[phone] could not ring you (");
					((BepInExLogInterpolatedStringHandler)val5).AppendFormatted<string>(label);
					((BepInExLogInterpolatedStringHandler)val5).AppendLiteral("): ");
					((BepInExLogInterpolatedStringHandler)val5).AppendFormatted<string>(ex.Message);
				}
				log2.LogWarning(val5);
				_current = null;
				return null;
			}
		}

		public static List<PhonePlace> KnownPlaces()
		{
			List<PhonePlace> list = new List<PhonePlace>();
			Player instance = Player.Instance;
			if ((Object)(object)instance == (Object)null)
			{
				return list;
			}
			if ((Object)(object)((Human)instance).home != (Object)null)
			{
				list.Add(new PhonePlace(((Human)instance).home, "home"));
			}
			foreach (NewAddress item in RentedRooms(instance))
			{
				list.Add(new PhonePlace(item, "hotel room"));
			}
			return list;
		}

		public static Telephone? FreePhoneAt(NewGameLocation? location)
		{
			if (((location != null) ? location.telephones : null) == null)
			{
				return null;
			}
			Enumerator<Telephone> enumerator = location.telephones.GetEnumerator();
			while (enumerator.MoveNext())
			{
				Telephone current = enumerator.Current;
				if (current != null && current.number > 0 && (current.activeCall == null || current.activeCall.Count == 0))
				{
					return current;
				}
			}
			return null;
		}

		private static Call? Fail(string label, string why)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			if (Plugin.Cfg.DebugLogging.Value)
			{
				ManualLogSource log = Plugin.Log;
				bool flag = default(bool);
				BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(20, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[phone] no call (");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(label);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("): ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(why);
				}
				log.LogInfo(val);
			}
			return null;
		}

		private static Telephone? PlayerPhone(Player player, IEnumerable<PhonePlace> places, out string where)
		{
			where = "";
			List<PhonePlace> list = new List<PhonePlace>(places);
			NewGameLocation currentGameLocation = ((Actor)player).currentGameLocation;
			int? obj;
			if (currentGameLocation == null)
			{
				obj = null;
			}
			else
			{
				NewAddress thisAsAddress = currentGameLocation.thisAsAddress;
				obj = ((thisAsAddress != null) ? new int?(thisAsAddress.id) : ((int?)null));
			}
			int num = obj ?? (-1);
			foreach (PhonePlace item in list)
			{
				if ((Object)(object)item.Address != (Object)null && item.Address.id == num)
				{
					Telephone val = FreePhoneAt((NewGameLocation?)(object)item.Address);
					if (val != null)
					{
						where = item.Name;
						return val;
					}
				}
			}
			foreach (PhonePlace item2 in list)
			{
				if (!((Object)(object)item2.Address == (Object)null) && !item2.OnlyIfThere)
				{
					Telephone val2 = FreePhoneAt((NewGameLocation?)(object)item2.Address);
					if (val2 != null)
					{
						where = item2.Name;
						return val2;
					}
				}
			}
			return null;
		}

		private static Telephone? CallerPhone(Human caller, Telephone to)
		{
			Telephone val = Pick(((Actor)caller).currentGameLocation) ?? Pick((NewGameLocation?)(object)caller.home);
			if (val != null)
			{
				return val;
			}
			try
			{
				Toolbox instance = Toolbox.Instance;
				Telephone val2 = ((instance != null) ? instance.GetClosestTelephone((Actor)(object)caller, 1000f, true, false, false) : null);
				return (val2 != null && ((Il2CppObjectBase)val2).Pointer != ((Il2CppObjectBase)to).Pointer) ? val2 : null;
			}
			catch
			{
				return null;
			}
			Telephone? Pick(NewGameLocation? location)
			{
				if (((location != null) ? location.telephones : null) == null)
				{
					return null;
				}
				Enumerator<Telephone> enumerator = location.telephones.GetEnumerator();
				while (enumerator.MoveNext())
				{
					Telephone current = enumerator.Current;
					if (current != null && current.number > 0 && ((Il2CppObjectBase)current).Pointer != ((Il2CppObjectBase)to).Pointer)
					{
						return current;
					}
				}
				return null;
			}
		}

		private static List<NewAddress> RentedRooms(Player player)
		{
			List<NewAddress> list = new List<NewAddress>();
			GameplayController instance = GameplayController.Instance;
			List<HotelGuest> val = ((instance != null) ? instance.hotelGuests : null);
			CityData instance2 = CityData.Instance;
			Dictionary<int, NewAddress> val2 = ((instance2 != null) ? instance2.addressDictionary : null);
			if (val == null || val2 == null)
			{
				return list;
			}
			Enumerator<HotelGuest> enumerator = val.GetEnumerator();
			NewAddress val3 = default(NewAddress);
			while (enumerator.MoveNext())
			{
				HotelGuest current = enumerator.Current;
				if (current != null && current.humanID == ((Human)player).humanID && val2.TryGetValue(current.addID, ref val3) && (Object)(object)val3 != (Object)null)
				{
					list.Add(val3);
				}
			}
			return list;
		}

		private static void OnGreeting()
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Expected O, but got Unknown
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Expected O, but got Unknown
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: Expected O, but got Unknown
			Call current = _current;
			bool flag = default(bool);
			string answer;
			if (current == null)
			{
				answer = "Sorry, wrong number.";
				foreach (Func<string> orphanAnswer in OrphanAnswers)
				{
					try
					{
						string text = orphanAnswer();
						if (text != null && text.Length > 0)
						{
							answer = text;
							break;
						}
					}
					catch (Exception ex)
					{
						ManualLogSource log = Plugin.Log;
						BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(26, 1, ref flag);
						if (flag)
						{
							((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[phone] lost call answer: ");
							((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
						}
						log.LogWarning(val);
					}
				}
				SetAnswer(answer);
				Plugin.Log.LogInfo((object)"[phone] you picked up a call nobody remembers");
				return;
			}
			current.Answered = true;
			try
			{
				answer = current.Answer(current);
			}
			catch (Exception ex2)
			{
				ManualLogSource log2 = Plugin.Log;
				BepInExErrorLogInterpolatedStringHandler val2 = new BepInExErrorLogInterpolatedStringHandler(25, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[phone] ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(current.Label);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(": answer failed: ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<Exception>(ex2);
				}
				log2.LogError(val2);
				answer = "";
			}
			SetAnswer(string.IsNullOrWhiteSpace(answer) ? "Sorry, wrong number." : answer);
			ManualLogSource log3 = Plugin.Log;
			BepInExInfoLogInterpolatedStringHandler val3 = new BepInExInfoLogInterpolatedStringHandler(28, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("[phone] you picked up at ");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(current.Where);
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(" (");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(current.Label);
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(")");
			}
			log3.LogInfo(val3);
			Notify(current.OnAnswered, current);
		}

		private static void SetAnswer(string text)
		{
			if (_greeting != null && _greeting.Responses.Count != 0)
			{
				Lib.DdsStrings.AddOrUpdate("dds.blocks", _greeting.Responses[0].BlockId, text);
			}
		}

		private static void Notify(Action<Call>? handler, Call call)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected O, but got Unknown
			if (handler == null)
			{
				return;
			}
			try
			{
				handler(call);
			}
			catch (Exception ex)
			{
				ManualLogSource log = Plugin.Log;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(27, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[phone] ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(call.Label);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": callback failed: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				log.LogError(val);
			}
		}

		internal static void Tick()
		{
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Expected O, but got Unknown
			//IL_01e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ed: Expected O, but got Unknown
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Invalid comparison between Unknown and I4
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Invalid comparison between Unknown and I4
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Invalid comparison between Unknown and I4
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Expected O, but got Unknown
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Invalid comparison between Unknown and I4
			Call current = _current;
			if (current == null)
			{
				return;
			}
			bool flag2 = default(bool);
			try
			{
				CallState state = current.Game.state;
				if (!current.Answered && (int)state == 3 && (Object)(object)current.Game.recevierNS != (Object)null && ((Actor)current.Game.recevierNS).isPlayer)
				{
					current.Answered = true;
					Notify(current.OnAnswered, current);
				}
				bool flag = !current.Answered && (int)state != 3 && SessionData.Instance.gameTime > current.GiveUpAt;
				if ((int)state != 4 && !flag)
				{
					return;
				}
				_current = null;
				current.Over = true;
				if (flag && (int)state != 4)
				{
					try
					{
						current.Game.EndCall();
					}
					catch (Exception ex)
					{
						ManualLogSource log = Plugin.Log;
						BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(27, 1, ref flag2);
						if (flag2)
						{
							((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[phone] could not hang up: ");
							((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
						}
						log.LogWarning(val);
					}
				}
				if (!current.Answered)
				{
					current.Missed = (((Object)(object)current.Game.recevierNS != (Object)null && !((Actor)current.Game.recevierNS).isPlayer) ? (current.Game.recevierNS.GetCitizenName() + " picked up instead") : "nobody picked up");
					ManualLogSource log2 = Plugin.Log;
					BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(30, 4, ref flag2);
					if (flag2)
					{
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[phone] ");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(current.Caller.GetCitizenName());
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("'s call to ");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(current.Where);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" ended: ");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(current.Missed);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" (");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(current.Label);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(")");
					}
					log2.LogInfo(val2);
					Notify(current.OnMissed, current);
				}
			}
			catch (Exception ex2)
			{
				ManualLogSource log3 = Plugin.Log;
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(14, 1, ref flag2);
				if (flag2)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[phone] tick: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex2.Message);
				}
				log3.LogWarning(val);
				_current = null;
				current.Over = true;
				if (!current.Answered)
				{
					try
					{
						current.Game.EndCall();
					}
					catch
					{
					}
					current.Missed = "lost track of the call";
					Notify(current.OnMissed, current);
				}
			}
		}
	}
	[BepInPlugin("ekem.citizencontact", "Citizen Contact", "1.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BasePlugin
	{
		public static ManualLogSource Log { get; private set; }

		public static Settings Cfg { get; private set; }

		public override void Load()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Expected O, but got Unknown
			Log = ((BasePlugin)this).Log;
			Cfg = new Settings(((BasePlugin)this).Config);
			Vmail.Install();
			Phone.Install();
			Approach.Install();
			new Harmony("ekem.citizencontact").PatchAll();
			InstallPulse();
			ManualLogSource log = Log;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(9, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("Citizen Contact");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("1.1.0");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" loaded.");
			}
			log.LogInfo(val);
		}

		private static void InstallPulse()
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Expected O, but got Unknown
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			ClassInjector.RegisterTypeInIl2Cpp<Pulse>();
			GameObject val = new GameObject("CitizenContactPulse");
			Object.DontDestroyOnLoad((Object)val);
			((Object)val).hideFlags = (HideFlags)61;
			val.AddComponent<Pulse>();
		}
	}
	public class Settings
	{
		public readonly ConfigEntry<bool> DebugLogging;

		public Settings(ConfigFile config)
		{
			DebugLogging = config.Bind<bool>("Debug", "DebugLogging", false, "Log every vmail set up or delivered on behalf of other mods.");
		}
	}
	public static class PluginInfo
	{
		public const string Guid = "ekem.citizencontact";

		public const string Name = "Citizen Contact";

		public const string Version = "1.1.0";
	}
	public class Pulse : MonoBehaviour
	{
		private float _next;

		public Pulse(IntPtr pointer)
			: base(pointer)
		{
		}

		private void Update()
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Expected O, but got Unknown
			if (Time.unscaledTime < _next)
			{
				return;
			}
			_next = Time.unscaledTime + 0.25f;
			bool flag = default(bool);
			try
			{
				Phone.Tick();
			}
			catch (Exception ex)
			{
				ManualLogSource log = Plugin.Log;
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(15, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[pulse] phone: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
				}
				log.LogWarning(val);
			}
			try
			{
				Approach.Tick();
			}
			catch (Exception ex2)
			{
				ManualLogSource log2 = Plugin.Log;
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(18, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[pulse] approach: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex2.Message);
				}
				log2.LogWarning(val);
			}
		}
	}
	public sealed class Block
	{
		public string Id { get; set; } = "";

		public string Instance { get; set; } = "";

		public string Text { get; set; } = "";
	}
	public sealed class Letter
	{
		public string Name { get; set; } = "";

		public string Tree { get; set; } = "";

		public string Message { get; set; } = "";

		public string Instance { get; set; } = "";

		public List<Block> Blocks { get; set; } = new List<Block>();
	}
	public static class Vmail
	{
		private const string FileName = "citizencontact_vmail.json";

		private static readonly Dictionary<string, Letter> Defined = new Dictionary<string, Letter>();

		private static List<Letter> _sent = new List<Letter>();

		internal static void Install()
		{
			Lib.SaveGame.OnBeforeSave += OnBeforeSave;
			Lib.SaveGame.OnAfterLoad += OnAfterLoad;
			Lib.SaveGame.OnAfterNewGame += OnAfterNewGame;
		}

		public static void Define(Letter letter)
		{
			Defined[letter.Tree] = letter;
			if (Dds.Ready)
			{
				Register(letter);
			}
		}

		public static bool Send(Human sender, string treeId, string label, Human? owner = null)
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Expected O, but got Unknown
			EnsureRegistered();
			if (!Dds.HasTree(treeId))
			{
				ManualLogSource log = Plugin.Log;
				bool flag = default(bool);
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(49, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[vmail] no letter with tree ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(treeId);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" (");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(label);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(") - was it defined?");
				}
				log.LogWarning(val);
				return false;
			}
			return Deliver(sender, treeId, label, owner);
		}

		public static bool Send(Human sender, IEnumerable<string> paragraphs, string label, Human? owner = null)
		{
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Expected O, but got Unknown
			try
			{
				Letter letter = new Letter
				{
					Name = "CC_Letter",
					Tree = NewId(),
					Message = NewId(),
					Instance = NewId(),
					Blocks = (from p in paragraphs.SelectMany((string p) => p.Split('\n'))
						select p.TrimEnd('\r') into p
						select new Block
						{
							Id = NewId(),
							Instance = NewId(),
							Text = p
						}).ToList()
				};
				if (letter.Blocks.Count == 0)
				{
					return false;
				}
				EnsureRegistered();
				Register(letter);
				if (!Deliver(sender, letter.Tree, label, owner))
				{
					return false;
				}
				_sent.Add(letter);
				return true;
			}
			catch (Exception ex)
			{
				ManualLogSource log = Plugin.Log;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(25, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[vmail] could not send ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(label);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				log.LogError(val);
				return false;
			}
		}

		private static bool Deliver(Human sender, string treeId, string label, Human? owner)
		{
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Expected O, but got Unknown
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Expected O, but got Unknown
			Player instance = Player.Instance;
			Toolbox instance2 = Toolbox.Instance;
			if ((Object)(object)sender == (Object)null || (Object)(object)instance == (Object)null || (Object)(object)instance2 == (Object)null)
			{
				return false;
			}
			Human? obj = owner ?? ((Il2CppObjectBase)instance).TryCast<Human>();
			SessionData instance3 = SessionData.Instance;
			bool flag = default(bool);
			if (instance2.NewVmailThread(obj, sender, (Human)null, (Human)null, (List<Human>)null, treeId, (instance3 != null) ? instance3.gameTime : 0f, 999, (CustomDataSource)0, -1) == null)
			{
				ManualLogSource log = Plugin.Log;
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(41, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[vmail] the game refused to create ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(label);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" from ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(sender.GetCitizenName());
				}
				log.LogWarning(val);
				return false;
			}
			if (Plugin.Cfg.DebugLogging.Value)
			{
				ManualLogSource log2 = Plugin.Log;
				BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(17, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[vmail] ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(label);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" sent by ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(sender.GetCitizenName());
				}
				log2.LogInfo(val2);
			}
			return true;
		}

		private static void EnsureRegistered()
		{
			if (!Dds.Ready)
			{
				return;
			}
			foreach (Letter value in Defined.Values)
			{
				Register(value);
			}
			foreach (Letter item in _sent)
			{
				Register(item);
			}
		}

		private static void Register(Letter letter)
		{
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Expected O, but got Unknown
			try
			{
				foreach (Block block in letter.Blocks)
				{
					Dds.AddBlock(block.Id, letter.Name, block.Text);
				}
				Dds.AddMessage(letter.Message, letter.Name, letter.Blocks.Select((Block b) => (Id: b.Id, Instance: b.Instance)));
				Dds.AddVmailTree(letter.Tree, letter.Name, letter.Message, letter.Instance);
			}
			catch (Exception ex)
			{
				ManualLogSource log = Plugin.Log;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(30, 3, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[vmail] could not set up ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(letter.Name);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" (");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(letter.Tree);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("): ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
				}
				log.LogError(val);
			}
		}

		private static string NewId()
		{
			return Guid.NewGuid().ToString();
		}

		private static string PathFor(SaveGameArgs args)
		{
			return Lib.SaveGame.GetSaveGameDataPath(args, "citizencontact_vmail.json");
		}

		private static void OnBeforeSave(object? sender, SaveGameArgs e)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			try
			{
				File.WriteAllText(PathFor(e), JsonSerializer.Serialize(_sent));
			}
			catch (Exception ex)
			{
				ManualLogSource log = Plugin.Log;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(32, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[vmail] failed to save letters: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
				}
				log.LogError(val);
			}
		}

		private static void OnAfterLoad(object? sender, SaveGameArgs e)
		{
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008f: Expected O, but got Unknown
			_sent = new List<Letter>();
			bool flag = default(bool);
			try
			{
				string path = PathFor(e);
				if (File.Exists(path))
				{
					_sent = JsonSerializer.Deserialize<List<Letter>>(File.ReadAllText(path)) ?? new List<Letter>();
				}
			}
			catch (Exception ex)
			{
				ManualLogSource log = Plugin.Log;
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(32, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[vmail] failed to load letters: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(ex.Message);
				}
				log.LogError(val);
			}
			EnsureRegistered();
			if (Plugin.Cfg.DebugLogging.Value)
			{
				ManualLogSource log2 = Plugin.Log;
				BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(46, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("[vmail] ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(Defined.Count);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" defined letters, ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(_sent.Count);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" sent letters set up");
				}
				log2.LogInfo(val2);
			}
		}

		private static void OnAfterNewGame(object? sender, EventArgs e)
		{
			_sent = new List<Letter>();
			EnsureRegistered();
		}
	}
}