Decompiled source of SOD CustomStateInjector Miami v1.1.0

SOD.CustomStateInjector.Miami.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime;
using Il2CppInterop.Runtime.InteropTypes;
using Microsoft.CodeAnalysis;
using SODCustomStateInjectorMiami.Attributes;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("SOD.CustomStateInjector.Miami")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+9c83f25b89718b7f8ad452f90658eafb1d2348b3")]
[assembly: AssemblyProduct("SOD CustomStateInjector Miami")]
[assembly: AssemblyTitle("SOD.CustomStateInjector.Miami")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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]
	[Microsoft.CodeAnalysis.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 SODCustomStateInjectorMiami
{
	public static class CustomStateInjector
	{
		public static void InjectStates()
		{
			Plugin.Instance.Start();
		}
	}
	[BepInPlugin("SOD.CustomStateInjector.Miami", "SOD CustomStateInjector Miami", "1.0.0")]
	public class Plugin : BasePlugin
	{
		public static ManualLogSource Log;

		private Harmony Harmony;

		public List<CustomStep> CustomSteps;

		private static Dictionary<string, Action> stateGenerateMethods = new Dictionary<string, Action>();

		public static Plugin Instance { get; private set; }

		public override void Load()
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			if (Instance != null)
			{
				throw new Exception("A Plugin instance already exists.");
			}
			Instance = this;
			CustomSteps = new List<CustomStep>();
			Harmony = new Harmony("SOD.CustomStateInjector.Miami");
			Log = ((BasePlugin)this).Log;
			Harmony.PatchAll();
		}

		public void Start()
		{
			RegisterCustomStates();
			RegisterStateGenerateMethods();
			ValidateStateGenerateMethods();
		}

		public static T GetField<T>(Il2CppObjectBase instance, string fieldName)
		{
			FieldInfo fieldIl2cpp = IL2CPPUtils.GetFieldIl2cpp(instance, fieldName);
			return IL2CPPUtils.GetFieldValue<T>(instance, fieldIl2cpp);
		}

		public static void SetField<T>(Il2CppObjectBase instance, string fieldName, T value)
		{
			FieldInfo fieldIl2cpp = IL2CPPUtils.GetFieldIl2cpp(instance, fieldName);
			IL2CPPUtils.SetFieldIl2cpp(instance, fieldIl2cpp, value);
		}

		public LoadState GetLoadState(string name)
		{
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			CustomStep customStep = CustomSteps.Find((CustomStep x) => x.Step.Name == name);
			if (customStep != null)
			{
				return customStep.Step.LoadState;
			}
			try
			{
				return (LoadState)Enum.Parse(typeof(LoadState), name);
			}
			catch (Exception)
			{
				throw new Exception("Step " + name + " not found in CityConstructor.LoadState nor in custom steps");
			}
		}

		private static void RegisterCustomStates()
		{
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
			IEnumerable<Type> enumerable = from type in AppDomain.CurrentDomain.GetAssemblies().SelectMany((Assembly assembly) => assembly.GetTypes())
				where type.GetCustomAttributes<CustomStateAttribute>(inherit: true).Any()
				select type;
			foreach (Type item in enumerable)
			{
				IEnumerable<CustomStateAttribute> customAttributes = item.GetCustomAttributes<CustomStateAttribute>(inherit: true);
				foreach (CustomStateAttribute item2 in customAttributes)
				{
					Step step = new Step
					{
						Name = item2.StepName,
						LoadState = (LoadState)(Utils.LoadStatesLength() + Instance.CustomSteps.Count)
					};
					Step after = new Step
					{
						Name = item2.AfterStepName,
						LoadState = Instance.GetLoadState(item2.AfterStepName)
					};
					Instance.CustomSteps.Add(new CustomStep
					{
						Step = step,
						After = after
					});
				}
			}
		}

		private static void RegisterStateGenerateMethods()
		{
			IEnumerable<MethodInfo> enumerable = from method in AppDomain.CurrentDomain.GetAssemblies().SelectMany((Assembly assembly) => assembly.GetTypes()).SelectMany((Type type) => type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
				where method.GetCustomAttribute<GenerateStateAttribute>() != null
				select method;
			foreach (MethodInfo item in enumerable)
			{
				GenerateStateAttribute customAttribute = item.GetCustomAttribute<GenerateStateAttribute>();
				if (customAttribute != null)
				{
					stateGenerateMethods[customAttribute.StateName] = (Action)Delegate.CreateDelegate(typeof(Action), item);
				}
			}
		}

		private void ValidateStateGenerateMethods()
		{
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Expected O, but got Unknown
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Expected O, but got Unknown
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0162: Expected O, but got Unknown
			List<CustomStep> list = CustomSteps.Where((CustomStep step) => stateGenerateMethods.ContainsKey(step.Step.Name)).ToList();
			List<CustomStep> list2 = CustomSteps.Where((CustomStep step) => !stateGenerateMethods.ContainsKey(step.Step.Name)).ToList();
			bool flag = default(bool);
			if (list2.Count > 0)
			{
				ManualLogSource log = Log;
				BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(32, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Generate Methods not found for: ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(string.Join(", ", list2.Select((CustomStep x) => x.Step.Name)));
				}
				log.LogWarning(val);
			}
			ManualLogSource log2 = Log;
			BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(0, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(string.Join(", ", list.Select((CustomStep x) => x.Step.Name)));
			}
			log2.LogInfo(val2);
			Log.LogInfo((object)"Custom Load Order:");
			foreach (CustomStep item in list)
			{
				ManualLogSource log3 = Log;
				val2 = new BepInExInfoLogInterpolatedStringHandler(12, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Step ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(item.Step.Name);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" after ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(item.After.Name);
				}
				log3.LogInfo(val2);
			}
		}

		public static void Generate(string stateName)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Expected O, but got Unknown
			if (stateGenerateMethods.TryGetValue(stateName, out var value))
			{
				value();
				return;
			}
			ManualLogSource log = Log;
			bool flag = default(bool);
			BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(36, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("No Generate method found for state: ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(stateName);
			}
			log.LogError(val);
		}
	}
	[HarmonyPatch(typeof(CityConstructor), "Update")]
	public class CityConstructor_Update_Patch
	{
		public struct TaskBlittable
		{
			public bool IsCompleted;
		}

		public static void Prefix(Il2CppObjectBase __instance, out int __state)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Expected I4, but got Unknown
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Expected O, but got Unknown
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Expected O, but got Unknown
			//IL_0198: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a2: Expected I4, but got Unknown
			int field = Plugin.GetField<int>(__instance, "loadCursor");
			LoadState loadState = (LoadState)Plugin.GetField<int>(__instance, "loadState");
			List<LoadState> list = Enum.GetValues(typeof(LoadState)).Cast<LoadState>().ToList();
			bool field2 = Plugin.GetField<bool>(__instance, "generateNew");
			bool field3 = Plugin.GetField<bool>(__instance, "loadingOperationActive");
			TaskBlittable field4 = Plugin.GetField<TaskBlittable>(__instance, "loadFullCityDataTask");
			__state = (int)loadState;
			if (field >= list.Count || (field3 && !field4.IsCompleted) || !field2)
			{
				return;
			}
			CustomStep customStep = Plugin.Instance.CustomSteps.Find((CustomStep x) => x.Step.LoadState == loadState);
			if (customStep != null)
			{
				ManualLogSource log = Plugin.Log;
				bool flag = default(bool);
				BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(14, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Generating ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(customStep.Step.Name);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("...");
				}
				log.LogInfo(val);
				Plugin.Generate(customStep.Step.Name);
				ManualLogSource log2 = Plugin.Log;
				val = new BepInExInfoLogInterpolatedStringHandler(24, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Generation of ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(customStep.Step.Name);
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" complete!");
				}
				log2.LogInfo(val);
				Plugin.SetField(__instance, "loadState", (int)customStep.Original.LoadState);
				Plugin.SetField(__instance, "loadCursor", field - 1);
			}
		}

		public static void Postfix(Il2CppObjectBase __instance, int __state)
		{
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: Expected O, but got Unknown
			//IL_01b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bb: Expected I4, but got Unknown
			int field = Plugin.GetField<int>(__instance, "loadCursor");
			List<LoadState> list = Enum.GetValues(typeof(LoadState)).Cast<LoadState>().ToList();
			bool field2 = Plugin.GetField<bool>(__instance, "generateNew");
			bool field3 = Plugin.GetField<bool>(__instance, "loadingOperationActive");
			TaskBlittable field4 = Plugin.GetField<TaskBlittable>(__instance, "loadFullCityDataTask");
			if (field >= list.Count + Plugin.Instance.CustomSteps.Count || (field3 && !field4.IsCompleted) || !field2)
			{
				return;
			}
			CustomStep customStep = Plugin.Instance.CustomSteps.Find((CustomStep x) => (int)x.After.LoadState == __state);
			if (customStep == null)
			{
				return;
			}
			try
			{
				Enum.Parse(typeof(LoadState), customStep.After.Name);
				customStep.Original = customStep.After;
			}
			catch (Exception)
			{
				customStep.Original = Plugin.Instance.CustomSteps.Find((CustomStep x) => (int)x.Step.LoadState == __state).Original;
			}
			ManualLogSource log = Plugin.Log;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(19, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Injecting ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(customStep.Step.Name);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" into ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(customStep.After.Name);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("...");
			}
			log.LogInfo(val);
			Plugin.SetField(__instance, "loadState", (int)customStep.Step.LoadState);
		}
	}
	public class Step
	{
		public string Name;

		public LoadState LoadState;
	}
	public class CustomStep
	{
		public Step Step;

		public Step After;

		public Step Original = new Step();
	}
	public static class IL2CPPUtils
	{
		public static FieldInfo GetFieldIl2cpp(Il2CppObjectBase instance, string fieldName)
		{
			Type type = ((object)instance).GetType();
			FieldInfo field = type.GetField(fieldName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			if (field != null)
			{
				return field;
			}
			field = type.GetField("NativeFieldInfoPtr_" + fieldName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
			if (field == null)
			{
				throw new MissingFieldException($"Field {fieldName} not found in {type.Name}.");
			}
			return field;
		}

		public unsafe static T GetFieldValue<T>(Il2CppObjectBase instance, FieldInfo fieldInfo)
		{
			IntPtr intPtr = (IntPtr)fieldInfo.GetValue(instance);
			void* ptr = stackalloc byte[(int)(uint)Marshal.SizeOf(typeof(T))];
			IL2CPP.il2cpp_field_get_value(instance.Pointer, intPtr, ptr);
			return Marshal.PtrToStructure<T>((IntPtr)ptr);
		}

		public unsafe static T SetFieldIl2cpp<T>(Il2CppObjectBase instance, FieldInfo fieldInfo, T value)
		{
			IntPtr intPtr = (IntPtr)fieldInfo.GetValue(instance);
			void* ptr = stackalloc byte[(int)(uint)Marshal.SizeOf(typeof(T))];
			Marshal.StructureToPtr(value, (IntPtr)ptr, fDeleteOld: false);
			IL2CPP.il2cpp_field_set_value(instance.Pointer, intPtr, ptr);
			return value;
		}
	}
	public static class Utils
	{
		public static int LoadStatesLength()
		{
			return Enum.GetValues(typeof(LoadState)).Length;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "SOD.CustomStateInjector.Miami";

		public const string PLUGIN_NAME = "SOD CustomStateInjector Miami";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}
namespace SODCustomStateInjectorMiami.Attributes
{
	[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
	public class CustomStateAttribute : Attribute
	{
		public string StepName { get; }

		public string AfterStepName { get; }

		public CustomStateAttribute(string stepName, string afterStepName)
		{
			StepName = stepName;
			AfterStepName = afterStepName;
		}
	}
	[AttributeUsage(AttributeTargets.Method)]
	public class GenerateStateAttribute : Attribute
	{
		public string StateName { get; }

		public GenerateStateAttribute(string stateName)
		{
			StateName = stateName;
		}
	}
}