Decompiled source of CityEditorSaveFix v1.0.5

plugins/CityEditorSaveFix/SoDCityEditorSaveFix.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppSystem.Collections.Generic;
using Il2CppSystem.IO;
using Microsoft.CodeAnalysis;
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("SoDCityEditorSaveFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2")]
[assembly: AssemblyProduct("SoDCityEditorSaveFix")]
[assembly: AssemblyTitle("SoDCityEditorSaveFix")]
[assembly: AssemblyVersion("1.0.2.0")]
[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 SoDCityEditorSaveFix
{
	[BepInPlugin("sm3gm.sod.cityeditorsavefix", "City Editor Save Fix", "1.0.4")]
	public sealed class Plugin : BasePlugin
	{
		public const string VER = "1.0.4";

		internal static ManualLogSource L;

		public override void Load()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Expected O, but got Unknown
			L = ((BasePlugin)this).Log;
			Harmony val = new Harmony("sm3gm.sod.cityeditorsavefix");
			val.PatchAll(typeof(GetCityFilePatch));
			val.PatchAll(typeof(CityDataNamePatch));
			val.PatchAll(typeof(SaveNamePatch));
			val.PatchAll(typeof(SaveDialogPatch));
			val.PatchAll(typeof(BirthdayCardPatch));
			val.PatchAll(typeof(CitizenDetailsPatch));
			ManualLogSource l = L;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(28, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("City Editor Save Fix ");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>("1.0.4");
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" loaded");
			}
			l.LogInfo(val2);
		}
	}
	internal static class Cities
	{
		internal const string Editor = "CityEditor";

		internal static string? Current;

		internal static string Dir => Path.Combine(Application.persistentDataPath, "Cities");

		internal static bool Exists(string name)
		{
			try
			{
				return Directory.Exists(Dir) && Files(name + ".*.CityEditor").Length != 0;
			}
			catch
			{
				return false;
			}
		}

		internal static string[] Files(string stem)
		{
			List<string> list = new List<string>();
			string[] files = Directory.GetFiles(Dir, stem + ".cit*");
			foreach (string text in files)
			{
				string text2 = Path.GetExtension(text).ToLowerInvariant();
				if (text2 == ".cit" || text2 == ".citb")
				{
					list.Add(text);
				}
			}
			return list.ToArray();
		}

		internal static string CityName(string fileName)
		{
			string[] array = fileName.Split('.');
			if (array.Length >= 5)
			{
				return string.Join(".", array, 0, array.Length - 4);
			}
			return "";
		}
	}
	[HarmonyPatch(/*Could not decode attribute arguments.*/)]
	internal static class CityDataNamePatch
	{
		private static void Prefix(ref CitySaveData cityData)
		{
			try
			{
				if (cityData != null && cityData.seed == "CityEditor" && !string.IsNullOrEmpty(cityData.cityName))
				{
					Cities.Current = cityData.cityName;
				}
			}
			catch
			{
			}
		}
	}
	[HarmonyPatch(typeof(Toolbox), "GetShareCode", new Type[]
	{
		typeof(string),
		typeof(int),
		typeof(int),
		typeof(string),
		typeof(string)
	})]
	internal static class SaveNamePatch
	{
		private static void Prefix(ref string cityName, string seed)
		{
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Expected O, but got Unknown
			string current = Cities.Current;
			if (!(seed != "CityEditor") && current != null && !(cityName == current) && !string.IsNullOrEmpty(cityName) && !Cities.Exists(cityName) && Cities.Exists(current))
			{
				ManualLogSource l = Plugin.L;
				bool flag = default(bool);
				BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(69, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Game was about to store city '");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(cityName);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("'; storing city editor city '");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(current);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' instead.");
				}
				l.LogInfo(val);
				cityName = current;
			}
		}
	}
	[HarmonyPatch(typeof(MainMenuController), "OnSaveButton")]
	internal static class SaveDialogPatch
	{
		private static void Prefix()
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			try
			{
				CityData instance = CityData.Instance;
				string current = Cities.Current;
				if (!((Object)(object)instance == (Object)null) && current != null && !(instance.cityName == current) && !(instance.seed != "CityEditor") && Cities.Exists(current))
				{
					ManualLogSource l = Plugin.L;
					bool flag = default(bool);
					BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(54, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("City name in memory was '");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(instance.cityName);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("'; restored '");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(current);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' before saving.");
					}
					l.LogInfo(val);
					instance.cityName = current;
				}
			}
			catch
			{
			}
		}
	}
	[HarmonyPatch(typeof(Toolbox), "GetCityFile", new Type[]
	{
		typeof(string),
		typeof(string),
		typeof(int),
		typeof(int),
		typeof(string)
	})]
	internal static class GetCityFilePatch
	{
		private static bool busy;

		private static void Postfix(Toolbox __instance, string codeName, string codeSeed, int codeSizeX, int codeSizeY, string codeVersion, ref FileInfo __result)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Expected O, but got Unknown
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Expected O, but got Unknown
			//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ce: Expected O, but got Unknown
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_014a: Expected O, but got Unknown
			if (busy || codeSeed != "CityEditor" || string.IsNullOrEmpty(codeName))
			{
				return;
			}
			ManualLogSource l = Plugin.L;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(42, 5, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Game looks for city editor city '");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(codeName);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' (");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(codeSizeX);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("x");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(codeSizeY);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(", ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(codeVersion);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("): ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>((__result != null) ? "found" : "not found");
			}
			l.LogInfo(val);
			if (__result != null)
			{
				Cities.Current = codeName;
				return;
			}
			try
			{
				if (!Directory.Exists(Cities.Dir))
				{
					return;
				}
				string text = ByName(codeName);
				string text2 = "its name is part of the stored name";
				if (text == null)
				{
					text = ByTime(codeName);
					text2 = "it was the city editor city saved last before this save";
				}
				if (text == null)
				{
					ManualLogSource l2 = Plugin.L;
					BepInExWarningLogInterpolatedStringHandler val2 = new BepInExWarningLogInterpolatedStringHandler(33, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("No city editor city found for '");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(codeName);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("'.");
					}
					l2.LogWarning(val2);
					return;
				}
				busy = true;
				FileInfo cityFile = __instance.GetCityFile(text, codeSeed, codeSizeX, codeSizeY, codeVersion);
				if (cityFile == null)
				{
					ManualLogSource l3 = Plugin.L;
					BepInExWarningLogInterpolatedStringHandler val2 = new BepInExWarningLogInterpolatedStringHandler(60, 4, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("City '");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(text);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("' (");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(text2);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(") exists, but the game did not accept it at size ");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(codeSizeX);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("x");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(codeSizeY);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(".");
					}
					l3.LogWarning(val2);
				}
				else
				{
					__result = cityFile;
					Cities.Current = text;
					ManualLogSource l4 = Plugin.L;
					val = new BepInExInfoLogInterpolatedStringHandler(64, 3, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Save pointed to city '");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(codeName);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("'; loading city editor city '");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(text);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("' instead (");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(text2);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(").");
					}
					l4.LogInfo(val);
				}
			}
			catch (Exception ex)
			{
				Plugin.L.LogWarning((object)("Could not look for the city file: " + ex.Message));
			}
			finally
			{
				busy = false;
			}
		}

		private static string? ByName(string codeName)
		{
			string text = null;
			string[] array = Cities.Files("*.CityEditor");
			for (int i = 0; i < array.Length; i++)
			{
				string text2 = Cities.CityName(Path.GetFileName(array[i]));
				if (text2.Length != 0 && !(text2 == codeName) && ContainsWord(codeName, text2) && (text == null || text2.Length > text.Length))
				{
					text = text2;
				}
			}
			return text;
		}

		private static string? ByTime(string codeName)
		{
			string text = Path.Combine(Application.persistentDataPath, "Save", codeName + ".sod");
			if (!File.Exists(text))
			{
				text += "b";
			}
			if (!File.Exists(text))
			{
				return null;
			}
			DateTime lastWriteTimeUtc = File.GetLastWriteTimeUtc(text);
			string result = null;
			DateTime dateTime = DateTime.MinValue;
			string[] array = Cities.Files("*.CityEditor");
			foreach (string path in array)
			{
				DateTime lastWriteTimeUtc2 = File.GetLastWriteTimeUtc(path);
				string text2 = Cities.CityName(Path.GetFileName(path));
				if (text2.Length != 0 && !(text2 == codeName) && !(lastWriteTimeUtc2 > lastWriteTimeUtc) && !(lastWriteTimeUtc2 <= dateTime))
				{
					result = text2;
					dateTime = lastWriteTimeUtc2;
				}
			}
			return result;
		}

		private static bool ContainsWord(string text, string word)
		{
			for (int num = text.IndexOf(word, StringComparison.Ordinal); num >= 0; num = text.IndexOf(word, num + 1, StringComparison.Ordinal))
			{
				bool num2 = num == 0 || text[num - 1] == ' ';
				int num3 = num + word.Length;
				if (num2 && (num3 == text.Length || text[num3] == ' '))
				{
					return true;
				}
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(EvidenceBirthdayCard), "BuildDataSources")]
	internal static class BirthdayCardPatch
	{
		private static int told;

		private static bool Prefix(EvidenceBirthdayCard __instance)
		{
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Expected O, but got Unknown
			try
			{
				Citizen birthdayCitizen = __instance.birthdayCitizen;
				Dictionary<string, Fact> val = ((birthdayCitizen != null) ? ((Human)birthdayCitizen).factDictionary : null);
				if (val == null || val.ContainsKey("LivesAt"))
				{
					return true;
				}
				if (told++ < 20)
				{
					ManualLogSource l = Plugin.L;
					bool flag = default(bool);
					BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(88, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Birthday card for '");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(((Human)birthdayCitizen).GetCitizenName());
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("', who has no home: built without the home link so loading can go on.");
					}
					l.LogInfo(val2);
				}
				return false;
			}
			catch
			{
				return true;
			}
		}
	}
	[HarmonyPatch(typeof(Human), "CreateDetails")]
	internal static class CitizenDetailsPatch
	{
		private static int told;

		private static void Prefix(Human __instance, out NewAddress? __state)
		{
			__state = null;
			try
			{
				NewAddress home = __instance.home;
				if (!((Object)(object)home == (Object)null) && !((Object)(object)__instance.residence != (Object)null))
				{
					if ((Object)(object)home.residence != (Object)null)
					{
						__instance.residence = home.residence;
						Tell("Citizen '" + __instance.GetCitizenName() + "' had a home but no residence; took the residence from the home.");
					}
					else
					{
						__state = home;
						__instance.home = null;
						Tell("Citizen '" + __instance.GetCitizenName() + "' has a home without a residence; set up without a resident record.");
					}
				}
			}
			catch
			{
			}
		}

		private static void Tell(string line)
		{
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			told++;
			if (told <= 10)
			{
				Plugin.L.LogInfo((object)line);
			}
			else if (told % 25 == 0)
			{
				ManualLogSource l = Plugin.L;
				bool flag = default(bool);
				BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(26, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(told);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" citizens repaired so far.");
				}
				l.LogInfo(val);
			}
		}

		private static void Postfix(Human __instance, NewAddress? __state)
		{
			if ((Object)(object)__state != (Object)null)
			{
				try
				{
					__instance.home = __state;
				}
				catch
				{
				}
			}
		}
	}
}